2012-08-01 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / cp / parser.c
blobd8c3305006774988e8998da3408e873388f9ffaf
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 *);
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, enum tag_types, bool);
2066 static tree cp_parser_template_name
2067 (cp_parser *, bool, bool, bool, enum tag_types, 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, enum tag_types, 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,
2555 enum tag_types tag_type,
2556 location_t location)
2558 cp_token_position start = 0;
2560 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2562 if (TYPE_P (type))
2563 error_at (location, "%qT is not a template", type);
2564 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2566 if (tag_type != none_type)
2567 error_at (location, "%qE is not a class template", type);
2568 else
2569 error_at (location, "%qE is not a template", type);
2571 else
2572 error_at (location, "invalid template-id");
2573 /* Remember the location of the invalid "<". */
2574 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2575 start = cp_lexer_token_position (parser->lexer, true);
2576 /* Consume the "<". */
2577 cp_lexer_consume_token (parser->lexer);
2578 /* Parse the template arguments. */
2579 cp_parser_enclosed_template_argument_list (parser);
2580 /* Permanently remove the invalid template arguments so that
2581 this error message is not issued again. */
2582 if (start)
2583 cp_lexer_purge_tokens_after (parser->lexer, start);
2587 /* If parsing an integral constant-expression, issue an error message
2588 about the fact that THING appeared and return true. Otherwise,
2589 return false. In either case, set
2590 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2592 static bool
2593 cp_parser_non_integral_constant_expression (cp_parser *parser,
2594 non_integral_constant thing)
2596 parser->non_integral_constant_expression_p = true;
2597 if (parser->integral_constant_expression_p)
2599 if (!parser->allow_non_integral_constant_expression_p)
2601 const char *msg = NULL;
2602 switch (thing)
2604 case NIC_FLOAT:
2605 error ("floating-point literal "
2606 "cannot appear in a constant-expression");
2607 return true;
2608 case NIC_CAST:
2609 error ("a cast to a type other than an integral or "
2610 "enumeration type cannot appear in a "
2611 "constant-expression");
2612 return true;
2613 case NIC_TYPEID:
2614 error ("%<typeid%> operator "
2615 "cannot appear in a constant-expression");
2616 return true;
2617 case NIC_NCC:
2618 error ("non-constant compound literals "
2619 "cannot appear in a constant-expression");
2620 return true;
2621 case NIC_FUNC_CALL:
2622 error ("a function call "
2623 "cannot appear in a constant-expression");
2624 return true;
2625 case NIC_INC:
2626 error ("an increment "
2627 "cannot appear in a constant-expression");
2628 return true;
2629 case NIC_DEC:
2630 error ("an decrement "
2631 "cannot appear in a constant-expression");
2632 return true;
2633 case NIC_ARRAY_REF:
2634 error ("an array reference "
2635 "cannot appear in a constant-expression");
2636 return true;
2637 case NIC_ADDR_LABEL:
2638 error ("the address of a label "
2639 "cannot appear in a constant-expression");
2640 return true;
2641 case NIC_OVERLOADED:
2642 error ("calls to overloaded operators "
2643 "cannot appear in a constant-expression");
2644 return true;
2645 case NIC_ASSIGNMENT:
2646 error ("an assignment cannot appear in a constant-expression");
2647 return true;
2648 case NIC_COMMA:
2649 error ("a comma operator "
2650 "cannot appear in a constant-expression");
2651 return true;
2652 case NIC_CONSTRUCTOR:
2653 error ("a call to a constructor "
2654 "cannot appear in a constant-expression");
2655 return true;
2656 case NIC_TRANSACTION:
2657 error ("a transaction expression "
2658 "cannot appear in a constant-expression");
2659 return true;
2660 case NIC_THIS:
2661 msg = "this";
2662 break;
2663 case NIC_FUNC_NAME:
2664 msg = "__FUNCTION__";
2665 break;
2666 case NIC_PRETTY_FUNC:
2667 msg = "__PRETTY_FUNCTION__";
2668 break;
2669 case NIC_C99_FUNC:
2670 msg = "__func__";
2671 break;
2672 case NIC_VA_ARG:
2673 msg = "va_arg";
2674 break;
2675 case NIC_ARROW:
2676 msg = "->";
2677 break;
2678 case NIC_POINT:
2679 msg = ".";
2680 break;
2681 case NIC_STAR:
2682 msg = "*";
2683 break;
2684 case NIC_ADDR:
2685 msg = "&";
2686 break;
2687 case NIC_PREINCREMENT:
2688 msg = "++";
2689 break;
2690 case NIC_PREDECREMENT:
2691 msg = "--";
2692 break;
2693 case NIC_NEW:
2694 msg = "new";
2695 break;
2696 case NIC_DEL:
2697 msg = "delete";
2698 break;
2699 default:
2700 gcc_unreachable ();
2702 if (msg)
2703 error ("%qs cannot appear in a constant-expression", msg);
2704 return true;
2707 return false;
2710 /* Emit a diagnostic for an invalid type name. SCOPE is the
2711 qualifying scope (or NULL, if none) for ID. This function commits
2712 to the current active tentative parse, if any. (Otherwise, the
2713 problematic construct might be encountered again later, resulting
2714 in duplicate error messages.) LOCATION is the location of ID. */
2716 static void
2717 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2718 tree scope, tree id,
2719 location_t location)
2721 tree decl, old_scope;
2722 cp_parser_commit_to_tentative_parse (parser);
2723 /* Try to lookup the identifier. */
2724 old_scope = parser->scope;
2725 parser->scope = scope;
2726 decl = cp_parser_lookup_name_simple (parser, id, location);
2727 parser->scope = old_scope;
2728 /* If the lookup found a template-name, it means that the user forgot
2729 to specify an argument list. Emit a useful error message. */
2730 if (TREE_CODE (decl) == TEMPLATE_DECL)
2731 error_at (location,
2732 "invalid use of template-name %qE without an argument list",
2733 decl);
2734 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2735 error_at (location, "invalid use of destructor %qD as a type", id);
2736 else if (TREE_CODE (decl) == TYPE_DECL)
2737 /* Something like 'unsigned A a;' */
2738 error_at (location, "invalid combination of multiple type-specifiers");
2739 else if (!parser->scope)
2741 /* Issue an error message. */
2742 error_at (location, "%qE does not name a type", id);
2743 /* If we're in a template class, it's possible that the user was
2744 referring to a type from a base class. For example:
2746 template <typename T> struct A { typedef T X; };
2747 template <typename T> struct B : public A<T> { X x; };
2749 The user should have said "typename A<T>::X". */
2750 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2751 inform (location, "C++11 %<constexpr%> only available with "
2752 "-std=c++11 or -std=gnu++11");
2753 else if (processing_template_decl && current_class_type
2754 && TYPE_BINFO (current_class_type))
2756 tree b;
2758 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2760 b = TREE_CHAIN (b))
2762 tree base_type = BINFO_TYPE (b);
2763 if (CLASS_TYPE_P (base_type)
2764 && dependent_type_p (base_type))
2766 tree field;
2767 /* Go from a particular instantiation of the
2768 template (which will have an empty TYPE_FIELDs),
2769 to the main version. */
2770 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2771 for (field = TYPE_FIELDS (base_type);
2772 field;
2773 field = DECL_CHAIN (field))
2774 if (TREE_CODE (field) == TYPE_DECL
2775 && DECL_NAME (field) == id)
2777 inform (location,
2778 "(perhaps %<typename %T::%E%> was intended)",
2779 BINFO_TYPE (b), id);
2780 break;
2782 if (field)
2783 break;
2788 /* Here we diagnose qualified-ids where the scope is actually correct,
2789 but the identifier does not resolve to a valid type name. */
2790 else if (parser->scope != error_mark_node)
2792 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2793 error_at (location, "%qE in namespace %qE does not name a type",
2794 id, parser->scope);
2795 else if (CLASS_TYPE_P (parser->scope)
2796 && constructor_name_p (id, parser->scope))
2798 /* A<T>::A<T>() */
2799 error_at (location, "%<%T::%E%> names the constructor, not"
2800 " the type", parser->scope, id);
2801 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2802 error_at (location, "and %qT has no template constructors",
2803 parser->scope);
2805 else if (TYPE_P (parser->scope)
2806 && dependent_scope_p (parser->scope))
2807 error_at (location, "need %<typename%> before %<%T::%E%> because "
2808 "%qT is a dependent scope",
2809 parser->scope, id, parser->scope);
2810 else if (TYPE_P (parser->scope))
2811 error_at (location, "%qE in %q#T does not name a type",
2812 id, parser->scope);
2813 else
2814 gcc_unreachable ();
2818 /* Check for a common situation where a type-name should be present,
2819 but is not, and issue a sensible error message. Returns true if an
2820 invalid type-name was detected.
2822 The situation handled by this function are variable declarations of the
2823 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2824 Usually, `ID' should name a type, but if we got here it means that it
2825 does not. We try to emit the best possible error message depending on
2826 how exactly the id-expression looks like. */
2828 static bool
2829 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2831 tree id;
2832 cp_token *token = cp_lexer_peek_token (parser->lexer);
2834 /* Avoid duplicate error about ambiguous lookup. */
2835 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2837 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2838 if (next->type == CPP_NAME && next->ambiguous_p)
2839 goto out;
2842 cp_parser_parse_tentatively (parser);
2843 id = cp_parser_id_expression (parser,
2844 /*template_keyword_p=*/false,
2845 /*check_dependency_p=*/true,
2846 /*template_p=*/NULL,
2847 /*declarator_p=*/true,
2848 /*optional_p=*/false);
2849 /* If the next token is a (, this is a function with no explicit return
2850 type, i.e. constructor, destructor or conversion op. */
2851 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2852 || TREE_CODE (id) == TYPE_DECL)
2854 cp_parser_abort_tentative_parse (parser);
2855 return false;
2857 if (!cp_parser_parse_definitely (parser))
2858 return false;
2860 /* Emit a diagnostic for the invalid type. */
2861 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2862 id, token->location);
2863 out:
2864 /* If we aren't in the middle of a declarator (i.e. in a
2865 parameter-declaration-clause), skip to the end of the declaration;
2866 there's no point in trying to process it. */
2867 if (!parser->in_declarator_p)
2868 cp_parser_skip_to_end_of_block_or_statement (parser);
2869 return true;
2872 /* Consume tokens up to, and including, the next non-nested closing `)'.
2873 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2874 are doing error recovery. Returns -1 if OR_COMMA is true and we
2875 found an unnested comma. */
2877 static int
2878 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2879 bool recovering,
2880 bool or_comma,
2881 bool consume_paren)
2883 unsigned paren_depth = 0;
2884 unsigned brace_depth = 0;
2885 unsigned square_depth = 0;
2887 if (recovering && !or_comma
2888 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2889 return 0;
2891 while (true)
2893 cp_token * token = cp_lexer_peek_token (parser->lexer);
2895 switch (token->type)
2897 case CPP_EOF:
2898 case CPP_PRAGMA_EOL:
2899 /* If we've run out of tokens, then there is no closing `)'. */
2900 return 0;
2902 /* This is good for lambda expression capture-lists. */
2903 case CPP_OPEN_SQUARE:
2904 ++square_depth;
2905 break;
2906 case CPP_CLOSE_SQUARE:
2907 if (!square_depth--)
2908 return 0;
2909 break;
2911 case CPP_SEMICOLON:
2912 /* This matches the processing in skip_to_end_of_statement. */
2913 if (!brace_depth)
2914 return 0;
2915 break;
2917 case CPP_OPEN_BRACE:
2918 ++brace_depth;
2919 break;
2920 case CPP_CLOSE_BRACE:
2921 if (!brace_depth--)
2922 return 0;
2923 break;
2925 case CPP_COMMA:
2926 if (recovering && or_comma && !brace_depth && !paren_depth
2927 && !square_depth)
2928 return -1;
2929 break;
2931 case CPP_OPEN_PAREN:
2932 if (!brace_depth)
2933 ++paren_depth;
2934 break;
2936 case CPP_CLOSE_PAREN:
2937 if (!brace_depth && !paren_depth--)
2939 if (consume_paren)
2940 cp_lexer_consume_token (parser->lexer);
2941 return 1;
2943 break;
2945 default:
2946 break;
2949 /* Consume the token. */
2950 cp_lexer_consume_token (parser->lexer);
2954 /* Consume tokens until we reach the end of the current statement.
2955 Normally, that will be just before consuming a `;'. However, if a
2956 non-nested `}' comes first, then we stop before consuming that. */
2958 static void
2959 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2961 unsigned nesting_depth = 0;
2963 while (true)
2965 cp_token *token = cp_lexer_peek_token (parser->lexer);
2967 switch (token->type)
2969 case CPP_EOF:
2970 case CPP_PRAGMA_EOL:
2971 /* If we've run out of tokens, stop. */
2972 return;
2974 case CPP_SEMICOLON:
2975 /* If the next token is a `;', we have reached the end of the
2976 statement. */
2977 if (!nesting_depth)
2978 return;
2979 break;
2981 case CPP_CLOSE_BRACE:
2982 /* If this is a non-nested '}', stop before consuming it.
2983 That way, when confronted with something like:
2985 { 3 + }
2987 we stop before consuming the closing '}', even though we
2988 have not yet reached a `;'. */
2989 if (nesting_depth == 0)
2990 return;
2992 /* If it is the closing '}' for a block that we have
2993 scanned, stop -- but only after consuming the token.
2994 That way given:
2996 void f g () { ... }
2997 typedef int I;
2999 we will stop after the body of the erroneously declared
3000 function, but before consuming the following `typedef'
3001 declaration. */
3002 if (--nesting_depth == 0)
3004 cp_lexer_consume_token (parser->lexer);
3005 return;
3008 case CPP_OPEN_BRACE:
3009 ++nesting_depth;
3010 break;
3012 default:
3013 break;
3016 /* Consume the token. */
3017 cp_lexer_consume_token (parser->lexer);
3021 /* This function is called at the end of a statement or declaration.
3022 If the next token is a semicolon, it is consumed; otherwise, error
3023 recovery is attempted. */
3025 static void
3026 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3028 /* Look for the trailing `;'. */
3029 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3031 /* If there is additional (erroneous) input, skip to the end of
3032 the statement. */
3033 cp_parser_skip_to_end_of_statement (parser);
3034 /* If the next token is now a `;', consume it. */
3035 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3036 cp_lexer_consume_token (parser->lexer);
3040 /* Skip tokens until we have consumed an entire block, or until we
3041 have consumed a non-nested `;'. */
3043 static void
3044 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3046 int nesting_depth = 0;
3048 while (nesting_depth >= 0)
3050 cp_token *token = cp_lexer_peek_token (parser->lexer);
3052 switch (token->type)
3054 case CPP_EOF:
3055 case CPP_PRAGMA_EOL:
3056 /* If we've run out of tokens, stop. */
3057 return;
3059 case CPP_SEMICOLON:
3060 /* Stop if this is an unnested ';'. */
3061 if (!nesting_depth)
3062 nesting_depth = -1;
3063 break;
3065 case CPP_CLOSE_BRACE:
3066 /* Stop if this is an unnested '}', or closes the outermost
3067 nesting level. */
3068 nesting_depth--;
3069 if (nesting_depth < 0)
3070 return;
3071 if (!nesting_depth)
3072 nesting_depth = -1;
3073 break;
3075 case CPP_OPEN_BRACE:
3076 /* Nest. */
3077 nesting_depth++;
3078 break;
3080 default:
3081 break;
3084 /* Consume the token. */
3085 cp_lexer_consume_token (parser->lexer);
3089 /* Skip tokens until a non-nested closing curly brace is the next
3090 token, or there are no more tokens. Return true in the first case,
3091 false otherwise. */
3093 static bool
3094 cp_parser_skip_to_closing_brace (cp_parser *parser)
3096 unsigned nesting_depth = 0;
3098 while (true)
3100 cp_token *token = cp_lexer_peek_token (parser->lexer);
3102 switch (token->type)
3104 case CPP_EOF:
3105 case CPP_PRAGMA_EOL:
3106 /* If we've run out of tokens, stop. */
3107 return false;
3109 case CPP_CLOSE_BRACE:
3110 /* If the next token is a non-nested `}', then we have reached
3111 the end of the current block. */
3112 if (nesting_depth-- == 0)
3113 return true;
3114 break;
3116 case CPP_OPEN_BRACE:
3117 /* If it the next token is a `{', then we are entering a new
3118 block. Consume the entire block. */
3119 ++nesting_depth;
3120 break;
3122 default:
3123 break;
3126 /* Consume the token. */
3127 cp_lexer_consume_token (parser->lexer);
3131 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3132 parameter is the PRAGMA token, allowing us to purge the entire pragma
3133 sequence. */
3135 static void
3136 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3138 cp_token *token;
3140 parser->lexer->in_pragma = false;
3143 token = cp_lexer_consume_token (parser->lexer);
3144 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3146 /* Ensure that the pragma is not parsed again. */
3147 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3150 /* Require pragma end of line, resyncing with it as necessary. The
3151 arguments are as for cp_parser_skip_to_pragma_eol. */
3153 static void
3154 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3156 parser->lexer->in_pragma = false;
3157 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3158 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3161 /* This is a simple wrapper around make_typename_type. When the id is
3162 an unresolved identifier node, we can provide a superior diagnostic
3163 using cp_parser_diagnose_invalid_type_name. */
3165 static tree
3166 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3167 tree id, location_t id_location)
3169 tree result;
3170 if (TREE_CODE (id) == IDENTIFIER_NODE)
3172 result = make_typename_type (scope, id, typename_type,
3173 /*complain=*/tf_none);
3174 if (result == error_mark_node)
3175 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3176 return result;
3178 return make_typename_type (scope, id, typename_type, tf_error);
3181 /* This is a wrapper around the
3182 make_{pointer,ptrmem,reference}_declarator functions that decides
3183 which one to call based on the CODE and CLASS_TYPE arguments. The
3184 CODE argument should be one of the values returned by
3185 cp_parser_ptr_operator. */
3186 static cp_declarator *
3187 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3188 cp_cv_quals cv_qualifiers,
3189 cp_declarator *target)
3191 if (code == ERROR_MARK)
3192 return cp_error_declarator;
3194 if (code == INDIRECT_REF)
3195 if (class_type == NULL_TREE)
3196 return make_pointer_declarator (cv_qualifiers, target);
3197 else
3198 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3199 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3200 return make_reference_declarator (cv_qualifiers, target, false);
3201 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3202 return make_reference_declarator (cv_qualifiers, target, true);
3203 gcc_unreachable ();
3206 /* Create a new C++ parser. */
3208 static cp_parser *
3209 cp_parser_new (void)
3211 cp_parser *parser;
3212 cp_lexer *lexer;
3213 unsigned i;
3215 /* cp_lexer_new_main is called before doing GC allocation because
3216 cp_lexer_new_main might load a PCH file. */
3217 lexer = cp_lexer_new_main ();
3219 /* Initialize the binops_by_token so that we can get the tree
3220 directly from the token. */
3221 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3222 binops_by_token[binops[i].token_type] = binops[i];
3224 parser = ggc_alloc_cleared_cp_parser ();
3225 parser->lexer = lexer;
3226 parser->context = cp_parser_context_new (NULL);
3228 /* For now, we always accept GNU extensions. */
3229 parser->allow_gnu_extensions_p = 1;
3231 /* The `>' token is a greater-than operator, not the end of a
3232 template-id. */
3233 parser->greater_than_is_operator_p = true;
3235 parser->default_arg_ok_p = true;
3237 /* We are not parsing a constant-expression. */
3238 parser->integral_constant_expression_p = false;
3239 parser->allow_non_integral_constant_expression_p = false;
3240 parser->non_integral_constant_expression_p = false;
3242 /* Local variable names are not forbidden. */
3243 parser->local_variables_forbidden_p = false;
3245 /* We are not processing an `extern "C"' declaration. */
3246 parser->in_unbraced_linkage_specification_p = false;
3248 /* We are not processing a declarator. */
3249 parser->in_declarator_p = false;
3251 /* We are not processing a template-argument-list. */
3252 parser->in_template_argument_list_p = false;
3254 /* We are not in an iteration statement. */
3255 parser->in_statement = 0;
3257 /* We are not in a switch statement. */
3258 parser->in_switch_statement_p = false;
3260 /* We are not parsing a type-id inside an expression. */
3261 parser->in_type_id_in_expr_p = false;
3263 /* Declarations aren't implicitly extern "C". */
3264 parser->implicit_extern_c = false;
3266 /* String literals should be translated to the execution character set. */
3267 parser->translate_strings_p = true;
3269 /* We are not parsing a function body. */
3270 parser->in_function_body = false;
3272 /* We can correct until told otherwise. */
3273 parser->colon_corrects_to_scope_p = true;
3275 /* The unparsed function queue is empty. */
3276 push_unparsed_function_queues (parser);
3278 /* There are no classes being defined. */
3279 parser->num_classes_being_defined = 0;
3281 /* No template parameters apply. */
3282 parser->num_template_parameter_lists = 0;
3284 return parser;
3287 /* Create a cp_lexer structure which will emit the tokens in CACHE
3288 and push it onto the parser's lexer stack. This is used for delayed
3289 parsing of in-class method bodies and default arguments, and should
3290 not be confused with tentative parsing. */
3291 static void
3292 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3294 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3295 lexer->next = parser->lexer;
3296 parser->lexer = lexer;
3298 /* Move the current source position to that of the first token in the
3299 new lexer. */
3300 cp_lexer_set_source_position_from_token (lexer->next_token);
3303 /* Pop the top lexer off the parser stack. This is never used for the
3304 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3305 static void
3306 cp_parser_pop_lexer (cp_parser *parser)
3308 cp_lexer *lexer = parser->lexer;
3309 parser->lexer = lexer->next;
3310 cp_lexer_destroy (lexer);
3312 /* Put the current source position back where it was before this
3313 lexer was pushed. */
3314 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3317 /* Lexical conventions [gram.lex] */
3319 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3320 identifier. */
3322 static tree
3323 cp_parser_identifier (cp_parser* parser)
3325 cp_token *token;
3327 /* Look for the identifier. */
3328 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3329 /* Return the value. */
3330 return token ? token->u.value : error_mark_node;
3333 /* Parse a sequence of adjacent string constants. Returns a
3334 TREE_STRING representing the combined, nul-terminated string
3335 constant. If TRANSLATE is true, translate the string to the
3336 execution character set. If WIDE_OK is true, a wide string is
3337 invalid here.
3339 C++98 [lex.string] says that if a narrow string literal token is
3340 adjacent to a wide string literal token, the behavior is undefined.
3341 However, C99 6.4.5p4 says that this results in a wide string literal.
3342 We follow C99 here, for consistency with the C front end.
3344 This code is largely lifted from lex_string() in c-lex.c.
3346 FUTURE: ObjC++ will need to handle @-strings here. */
3347 static tree
3348 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3350 tree value;
3351 size_t count;
3352 struct obstack str_ob;
3353 cpp_string str, istr, *strs;
3354 cp_token *tok;
3355 enum cpp_ttype type, curr_type;
3356 int have_suffix_p = 0;
3357 tree string_tree;
3358 tree suffix_id = NULL_TREE;
3359 bool curr_tok_is_userdef_p = false;
3361 tok = cp_lexer_peek_token (parser->lexer);
3362 if (!cp_parser_is_string_literal (tok))
3364 cp_parser_error (parser, "expected string-literal");
3365 return error_mark_node;
3368 if (cpp_userdef_string_p (tok->type))
3370 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3371 curr_type = cpp_userdef_string_remove_type (tok->type);
3372 curr_tok_is_userdef_p = true;
3374 else
3376 string_tree = tok->u.value;
3377 curr_type = tok->type;
3379 type = curr_type;
3381 /* Try to avoid the overhead of creating and destroying an obstack
3382 for the common case of just one string. */
3383 if (!cp_parser_is_string_literal
3384 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3386 cp_lexer_consume_token (parser->lexer);
3388 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3389 str.len = TREE_STRING_LENGTH (string_tree);
3390 count = 1;
3392 if (curr_tok_is_userdef_p)
3394 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3395 have_suffix_p = 1;
3396 curr_type = cpp_userdef_string_remove_type (tok->type);
3398 else
3399 curr_type = tok->type;
3401 strs = &str;
3403 else
3405 gcc_obstack_init (&str_ob);
3406 count = 0;
3410 cp_lexer_consume_token (parser->lexer);
3411 count++;
3412 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3413 str.len = TREE_STRING_LENGTH (string_tree);
3415 if (curr_tok_is_userdef_p)
3417 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3418 if (have_suffix_p == 0)
3420 suffix_id = curr_suffix_id;
3421 have_suffix_p = 1;
3423 else if (have_suffix_p == 1
3424 && curr_suffix_id != suffix_id)
3426 error ("inconsistent user-defined literal suffixes"
3427 " %qD and %qD in string literal",
3428 suffix_id, curr_suffix_id);
3429 have_suffix_p = -1;
3431 curr_type = cpp_userdef_string_remove_type (tok->type);
3433 else
3434 curr_type = tok->type;
3436 if (type != curr_type)
3438 if (type == CPP_STRING)
3439 type = curr_type;
3440 else if (curr_type != CPP_STRING)
3441 error_at (tok->location,
3442 "unsupported non-standard concatenation "
3443 "of string literals");
3446 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3448 tok = cp_lexer_peek_token (parser->lexer);
3449 if (cpp_userdef_string_p (tok->type))
3451 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3452 curr_type = cpp_userdef_string_remove_type (tok->type);
3453 curr_tok_is_userdef_p = true;
3455 else
3457 string_tree = tok->u.value;
3458 curr_type = tok->type;
3459 curr_tok_is_userdef_p = false;
3462 while (cp_parser_is_string_literal (tok));
3464 strs = (cpp_string *) obstack_finish (&str_ob);
3467 if (type != CPP_STRING && !wide_ok)
3469 cp_parser_error (parser, "a wide string is invalid in this context");
3470 type = CPP_STRING;
3473 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3474 (parse_in, strs, count, &istr, type))
3476 value = build_string (istr.len, (const char *)istr.text);
3477 free (CONST_CAST (unsigned char *, istr.text));
3479 switch (type)
3481 default:
3482 case CPP_STRING:
3483 case CPP_UTF8STRING:
3484 TREE_TYPE (value) = char_array_type_node;
3485 break;
3486 case CPP_STRING16:
3487 TREE_TYPE (value) = char16_array_type_node;
3488 break;
3489 case CPP_STRING32:
3490 TREE_TYPE (value) = char32_array_type_node;
3491 break;
3492 case CPP_WSTRING:
3493 TREE_TYPE (value) = wchar_array_type_node;
3494 break;
3497 value = fix_string_type (value);
3499 if (have_suffix_p)
3501 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3502 tok->u.value = literal;
3503 return cp_parser_userdef_string_literal (tok);
3506 else
3507 /* cpp_interpret_string has issued an error. */
3508 value = error_mark_node;
3510 if (count > 1)
3511 obstack_free (&str_ob, 0);
3513 return value;
3516 /* Look up a literal operator with the name and the exact arguments. */
3518 static tree
3519 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3521 tree decl, fns;
3522 decl = lookup_name (name);
3523 if (!decl || !is_overloaded_fn (decl))
3524 return error_mark_node;
3526 for (fns = decl; fns; fns = OVL_NEXT (fns))
3528 unsigned int ix;
3529 bool found = true;
3530 tree fn = OVL_CURRENT (fns);
3531 tree argtypes = NULL_TREE;
3532 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3533 if (argtypes != NULL_TREE)
3535 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3536 ++ix, argtypes = TREE_CHAIN (argtypes))
3538 tree targ = TREE_VALUE (argtypes);
3539 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3540 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3541 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3542 if ((ptr || arr || !same_type_p (targ, tparm))
3543 && (!ptr || !arr
3544 || !same_type_p (TREE_TYPE (targ),
3545 TREE_TYPE (tparm))))
3546 found = false;
3548 if (found
3549 && ix == VEC_length (tree, args)
3550 /* May be this should be sufficient_parms_p instead,
3551 depending on how exactly should user-defined literals
3552 work in presence of default arguments on the literal
3553 operator parameters. */
3554 && argtypes == void_list_node)
3555 return fn;
3559 return error_mark_node;
3562 /* Parse a user-defined char constant. Returns a call to a user-defined
3563 literal operator taking the character as an argument. */
3565 static tree
3566 cp_parser_userdef_char_literal (cp_parser *parser)
3568 cp_token *token = cp_lexer_consume_token (parser->lexer);
3569 tree literal = token->u.value;
3570 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3571 tree value = USERDEF_LITERAL_VALUE (literal);
3572 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3573 tree decl, result;
3575 /* Build up a call to the user-defined operator */
3576 /* Lookup the name we got back from the id-expression. */
3577 VEC(tree,gc) *args = make_tree_vector ();
3578 VEC_safe_push (tree, gc, args, value);
3579 decl = lookup_literal_operator (name, args);
3580 if (!decl || decl == error_mark_node)
3582 error ("unable to find character literal operator %qD with %qT argument",
3583 name, TREE_TYPE (value));
3584 release_tree_vector (args);
3585 return error_mark_node;
3587 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3588 release_tree_vector (args);
3589 if (result != error_mark_node)
3590 return result;
3592 error ("unable to find character literal operator %qD with %qT argument",
3593 name, TREE_TYPE (value));
3594 return error_mark_node;
3597 /* A subroutine of cp_parser_userdef_numeric_literal to
3598 create a char... template parameter pack from a string node. */
3600 static tree
3601 make_char_string_pack (tree value)
3603 tree charvec;
3604 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3605 const char *str = TREE_STRING_POINTER (value);
3606 int i, len = TREE_STRING_LENGTH (value) - 1;
3607 tree argvec = make_tree_vec (1);
3609 /* Fill in CHARVEC with all of the parameters. */
3610 charvec = make_tree_vec (len);
3611 for (i = 0; i < len; ++i)
3612 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3614 /* Build the argument packs. */
3615 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3616 TREE_TYPE (argpack) = char_type_node;
3618 TREE_VEC_ELT (argvec, 0) = argpack;
3620 return argvec;
3623 /* Parse a user-defined numeric constant. returns a call to a user-defined
3624 literal operator. */
3626 static tree
3627 cp_parser_userdef_numeric_literal (cp_parser *parser)
3629 cp_token *token = cp_lexer_consume_token (parser->lexer);
3630 tree literal = token->u.value;
3631 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3632 tree value = USERDEF_LITERAL_VALUE (literal);
3633 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3634 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3635 tree decl, result;
3636 VEC(tree,gc) *args;
3638 /* Look for a literal operator taking the exact type of numeric argument
3639 as the literal value. */
3640 args = make_tree_vector ();
3641 VEC_safe_push (tree, gc, args, value);
3642 decl = lookup_literal_operator (name, args);
3643 if (decl && decl != error_mark_node)
3645 result = finish_call_expr (decl, &args, false, true, tf_none);
3646 if (result != error_mark_node)
3648 release_tree_vector (args);
3649 return result;
3652 release_tree_vector (args);
3654 /* If the numeric argument didn't work, look for a raw literal
3655 operator taking a const char* argument consisting of the number
3656 in string format. */
3657 args = make_tree_vector ();
3658 VEC_safe_push (tree, gc, args, num_string);
3659 decl = lookup_literal_operator (name, args);
3660 if (decl && decl != error_mark_node)
3662 result = finish_call_expr (decl, &args, false, true, tf_none);
3663 if (result != error_mark_node)
3665 release_tree_vector (args);
3666 return result;
3669 release_tree_vector (args);
3671 /* If the raw literal didn't work, look for a non-type template
3672 function with parameter pack char.... Call the function with
3673 template parameter characters representing the number. */
3674 args = make_tree_vector ();
3675 decl = lookup_literal_operator (name, args);
3676 if (decl && decl != error_mark_node)
3678 tree tmpl_args = make_char_string_pack (num_string);
3679 decl = lookup_template_function (decl, tmpl_args);
3680 result = finish_call_expr (decl, &args, false, true, tf_none);
3681 if (result != error_mark_node)
3683 release_tree_vector (args);
3684 return result;
3687 release_tree_vector (args);
3689 error ("unable to find numeric literal operator %qD", name);
3690 return error_mark_node;
3693 /* Parse a user-defined string constant. Returns a call to a user-defined
3694 literal operator taking a character pointer and the length of the string
3695 as arguments. */
3697 static tree
3698 cp_parser_userdef_string_literal (cp_token *token)
3700 tree literal = token->u.value;
3701 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3702 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3703 tree value = USERDEF_LITERAL_VALUE (literal);
3704 int len = TREE_STRING_LENGTH (value)
3705 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3706 tree decl, result;
3708 /* Build up a call to the user-defined operator */
3709 /* Lookup the name we got back from the id-expression. */
3710 VEC(tree,gc) *args = make_tree_vector ();
3711 VEC_safe_push (tree, gc, args, value);
3712 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3713 decl = lookup_name (name);
3714 if (!decl || decl == error_mark_node)
3716 error ("unable to find string literal operator %qD", name);
3717 release_tree_vector (args);
3718 return error_mark_node;
3720 result = finish_call_expr (decl, &args, false, true, tf_none);
3721 release_tree_vector (args);
3722 if (result != error_mark_node)
3723 return result;
3725 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3726 name, TREE_TYPE (value), size_type_node);
3727 return error_mark_node;
3731 /* Basic concepts [gram.basic] */
3733 /* Parse a translation-unit.
3735 translation-unit:
3736 declaration-seq [opt]
3738 Returns TRUE if all went well. */
3740 static bool
3741 cp_parser_translation_unit (cp_parser* parser)
3743 /* The address of the first non-permanent object on the declarator
3744 obstack. */
3745 static void *declarator_obstack_base;
3747 bool success;
3749 /* Create the declarator obstack, if necessary. */
3750 if (!cp_error_declarator)
3752 gcc_obstack_init (&declarator_obstack);
3753 /* Create the error declarator. */
3754 cp_error_declarator = make_declarator (cdk_error);
3755 /* Create the empty parameter list. */
3756 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3757 /* Remember where the base of the declarator obstack lies. */
3758 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3761 cp_parser_declaration_seq_opt (parser);
3763 /* If there are no tokens left then all went well. */
3764 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3766 /* Get rid of the token array; we don't need it any more. */
3767 cp_lexer_destroy (parser->lexer);
3768 parser->lexer = NULL;
3770 /* This file might have been a context that's implicitly extern
3771 "C". If so, pop the lang context. (Only relevant for PCH.) */
3772 if (parser->implicit_extern_c)
3774 pop_lang_context ();
3775 parser->implicit_extern_c = false;
3778 /* Finish up. */
3779 finish_translation_unit ();
3781 success = true;
3783 else
3785 cp_parser_error (parser, "expected declaration");
3786 success = false;
3789 /* Make sure the declarator obstack was fully cleaned up. */
3790 gcc_assert (obstack_next_free (&declarator_obstack)
3791 == declarator_obstack_base);
3793 /* All went well. */
3794 return success;
3797 /* Expressions [gram.expr] */
3799 /* Parse a primary-expression.
3801 primary-expression:
3802 literal
3803 this
3804 ( expression )
3805 id-expression
3807 GNU Extensions:
3809 primary-expression:
3810 ( compound-statement )
3811 __builtin_va_arg ( assignment-expression , type-id )
3812 __builtin_offsetof ( type-id , offsetof-expression )
3814 C++ Extensions:
3815 __has_nothrow_assign ( type-id )
3816 __has_nothrow_constructor ( type-id )
3817 __has_nothrow_copy ( type-id )
3818 __has_trivial_assign ( type-id )
3819 __has_trivial_constructor ( type-id )
3820 __has_trivial_copy ( type-id )
3821 __has_trivial_destructor ( type-id )
3822 __has_virtual_destructor ( type-id )
3823 __is_abstract ( type-id )
3824 __is_base_of ( type-id , type-id )
3825 __is_class ( type-id )
3826 __is_convertible_to ( type-id , type-id )
3827 __is_empty ( type-id )
3828 __is_enum ( type-id )
3829 __is_final ( type-id )
3830 __is_literal_type ( type-id )
3831 __is_pod ( type-id )
3832 __is_polymorphic ( type-id )
3833 __is_std_layout ( type-id )
3834 __is_trivial ( type-id )
3835 __is_union ( type-id )
3837 Objective-C++ Extension:
3839 primary-expression:
3840 objc-expression
3842 literal:
3843 __null
3845 ADDRESS_P is true iff this expression was immediately preceded by
3846 "&" and therefore might denote a pointer-to-member. CAST_P is true
3847 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3848 true iff this expression is a template argument.
3850 Returns a representation of the expression. Upon return, *IDK
3851 indicates what kind of id-expression (if any) was present. */
3853 static tree
3854 cp_parser_primary_expression (cp_parser *parser,
3855 bool address_p,
3856 bool cast_p,
3857 bool template_arg_p,
3858 cp_id_kind *idk)
3860 cp_token *token = NULL;
3862 /* Assume the primary expression is not an id-expression. */
3863 *idk = CP_ID_KIND_NONE;
3865 /* Peek at the next token. */
3866 token = cp_lexer_peek_token (parser->lexer);
3867 switch (token->type)
3869 /* literal:
3870 integer-literal
3871 character-literal
3872 floating-literal
3873 string-literal
3874 boolean-literal
3875 pointer-literal
3876 user-defined-literal */
3877 case CPP_CHAR:
3878 case CPP_CHAR16:
3879 case CPP_CHAR32:
3880 case CPP_WCHAR:
3881 case CPP_NUMBER:
3882 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3883 return cp_parser_userdef_numeric_literal (parser);
3884 token = cp_lexer_consume_token (parser->lexer);
3885 if (TREE_CODE (token->u.value) == FIXED_CST)
3887 error_at (token->location,
3888 "fixed-point types not supported in C++");
3889 return error_mark_node;
3891 /* Floating-point literals are only allowed in an integral
3892 constant expression if they are cast to an integral or
3893 enumeration type. */
3894 if (TREE_CODE (token->u.value) == REAL_CST
3895 && parser->integral_constant_expression_p
3896 && pedantic)
3898 /* CAST_P will be set even in invalid code like "int(2.7 +
3899 ...)". Therefore, we have to check that the next token
3900 is sure to end the cast. */
3901 if (cast_p)
3903 cp_token *next_token;
3905 next_token = cp_lexer_peek_token (parser->lexer);
3906 if (/* The comma at the end of an
3907 enumerator-definition. */
3908 next_token->type != CPP_COMMA
3909 /* The curly brace at the end of an enum-specifier. */
3910 && next_token->type != CPP_CLOSE_BRACE
3911 /* The end of a statement. */
3912 && next_token->type != CPP_SEMICOLON
3913 /* The end of the cast-expression. */
3914 && next_token->type != CPP_CLOSE_PAREN
3915 /* The end of an array bound. */
3916 && next_token->type != CPP_CLOSE_SQUARE
3917 /* The closing ">" in a template-argument-list. */
3918 && (next_token->type != CPP_GREATER
3919 || parser->greater_than_is_operator_p)
3920 /* C++0x only: A ">>" treated like two ">" tokens,
3921 in a template-argument-list. */
3922 && (next_token->type != CPP_RSHIFT
3923 || (cxx_dialect == cxx98)
3924 || parser->greater_than_is_operator_p))
3925 cast_p = false;
3928 /* If we are within a cast, then the constraint that the
3929 cast is to an integral or enumeration type will be
3930 checked at that point. If we are not within a cast, then
3931 this code is invalid. */
3932 if (!cast_p)
3933 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3935 return token->u.value;
3937 case CPP_CHAR_USERDEF:
3938 case CPP_CHAR16_USERDEF:
3939 case CPP_CHAR32_USERDEF:
3940 case CPP_WCHAR_USERDEF:
3941 return cp_parser_userdef_char_literal (parser);
3943 case CPP_STRING:
3944 case CPP_STRING16:
3945 case CPP_STRING32:
3946 case CPP_WSTRING:
3947 case CPP_UTF8STRING:
3948 case CPP_STRING_USERDEF:
3949 case CPP_STRING16_USERDEF:
3950 case CPP_STRING32_USERDEF:
3951 case CPP_WSTRING_USERDEF:
3952 case CPP_UTF8STRING_USERDEF:
3953 /* ??? Should wide strings be allowed when parser->translate_strings_p
3954 is false (i.e. in attributes)? If not, we can kill the third
3955 argument to cp_parser_string_literal. */
3956 return cp_parser_string_literal (parser,
3957 parser->translate_strings_p,
3958 true);
3960 case CPP_OPEN_PAREN:
3962 tree expr;
3963 bool saved_greater_than_is_operator_p;
3965 /* Consume the `('. */
3966 cp_lexer_consume_token (parser->lexer);
3967 /* Within a parenthesized expression, a `>' token is always
3968 the greater-than operator. */
3969 saved_greater_than_is_operator_p
3970 = parser->greater_than_is_operator_p;
3971 parser->greater_than_is_operator_p = true;
3972 /* If we see `( { ' then we are looking at the beginning of
3973 a GNU statement-expression. */
3974 if (cp_parser_allow_gnu_extensions_p (parser)
3975 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3977 /* Statement-expressions are not allowed by the standard. */
3978 pedwarn (token->location, OPT_Wpedantic,
3979 "ISO C++ forbids braced-groups within expressions");
3981 /* And they're not allowed outside of a function-body; you
3982 cannot, for example, write:
3984 int i = ({ int j = 3; j + 1; });
3986 at class or namespace scope. */
3987 if (!parser->in_function_body
3988 || parser->in_template_argument_list_p)
3990 error_at (token->location,
3991 "statement-expressions are not allowed outside "
3992 "functions nor in template-argument lists");
3993 cp_parser_skip_to_end_of_block_or_statement (parser);
3994 expr = error_mark_node;
3996 else
3998 /* Start the statement-expression. */
3999 expr = begin_stmt_expr ();
4000 /* Parse the compound-statement. */
4001 cp_parser_compound_statement (parser, expr, false, false);
4002 /* Finish up. */
4003 expr = finish_stmt_expr (expr, false);
4006 else
4008 /* Parse the parenthesized expression. */
4009 expr = cp_parser_expression (parser, cast_p, idk);
4010 /* Let the front end know that this expression was
4011 enclosed in parentheses. This matters in case, for
4012 example, the expression is of the form `A::B', since
4013 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4014 not. */
4015 finish_parenthesized_expr (expr);
4016 /* DR 705: Wrapping an unqualified name in parentheses
4017 suppresses arg-dependent lookup. We want to pass back
4018 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4019 (c++/37862), but none of the others. */
4020 if (*idk != CP_ID_KIND_QUALIFIED)
4021 *idk = CP_ID_KIND_NONE;
4023 /* The `>' token might be the end of a template-id or
4024 template-parameter-list now. */
4025 parser->greater_than_is_operator_p
4026 = saved_greater_than_is_operator_p;
4027 /* Consume the `)'. */
4028 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4029 cp_parser_skip_to_end_of_statement (parser);
4031 return expr;
4034 case CPP_OPEN_SQUARE:
4035 if (c_dialect_objc ())
4036 /* We have an Objective-C++ message. */
4037 return cp_parser_objc_expression (parser);
4039 tree lam = cp_parser_lambda_expression (parser);
4040 /* Don't warn about a failed tentative parse. */
4041 if (cp_parser_error_occurred (parser))
4042 return error_mark_node;
4043 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4044 return lam;
4047 case CPP_OBJC_STRING:
4048 if (c_dialect_objc ())
4049 /* We have an Objective-C++ string literal. */
4050 return cp_parser_objc_expression (parser);
4051 cp_parser_error (parser, "expected primary-expression");
4052 return error_mark_node;
4054 case CPP_KEYWORD:
4055 switch (token->keyword)
4057 /* These two are the boolean literals. */
4058 case RID_TRUE:
4059 cp_lexer_consume_token (parser->lexer);
4060 return boolean_true_node;
4061 case RID_FALSE:
4062 cp_lexer_consume_token (parser->lexer);
4063 return boolean_false_node;
4065 /* The `__null' literal. */
4066 case RID_NULL:
4067 cp_lexer_consume_token (parser->lexer);
4068 return null_node;
4070 /* The `nullptr' literal. */
4071 case RID_NULLPTR:
4072 cp_lexer_consume_token (parser->lexer);
4073 return nullptr_node;
4075 /* Recognize the `this' keyword. */
4076 case RID_THIS:
4077 cp_lexer_consume_token (parser->lexer);
4078 if (parser->local_variables_forbidden_p)
4080 error_at (token->location,
4081 "%<this%> may not be used in this context");
4082 return error_mark_node;
4084 /* Pointers cannot appear in constant-expressions. */
4085 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4086 return error_mark_node;
4087 return finish_this_expr ();
4089 /* The `operator' keyword can be the beginning of an
4090 id-expression. */
4091 case RID_OPERATOR:
4092 goto id_expression;
4094 case RID_FUNCTION_NAME:
4095 case RID_PRETTY_FUNCTION_NAME:
4096 case RID_C99_FUNCTION_NAME:
4098 non_integral_constant name;
4100 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4101 __func__ are the names of variables -- but they are
4102 treated specially. Therefore, they are handled here,
4103 rather than relying on the generic id-expression logic
4104 below. Grammatically, these names are id-expressions.
4106 Consume the token. */
4107 token = cp_lexer_consume_token (parser->lexer);
4109 switch (token->keyword)
4111 case RID_FUNCTION_NAME:
4112 name = NIC_FUNC_NAME;
4113 break;
4114 case RID_PRETTY_FUNCTION_NAME:
4115 name = NIC_PRETTY_FUNC;
4116 break;
4117 case RID_C99_FUNCTION_NAME:
4118 name = NIC_C99_FUNC;
4119 break;
4120 default:
4121 gcc_unreachable ();
4124 if (cp_parser_non_integral_constant_expression (parser, name))
4125 return error_mark_node;
4127 /* Look up the name. */
4128 return finish_fname (token->u.value);
4131 case RID_VA_ARG:
4133 tree expression;
4134 tree type;
4135 source_location type_location;
4137 /* The `__builtin_va_arg' construct is used to handle
4138 `va_arg'. Consume the `__builtin_va_arg' token. */
4139 cp_lexer_consume_token (parser->lexer);
4140 /* Look for the opening `('. */
4141 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4142 /* Now, parse the assignment-expression. */
4143 expression = cp_parser_assignment_expression (parser,
4144 /*cast_p=*/false, NULL);
4145 /* Look for the `,'. */
4146 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4147 type_location = cp_lexer_peek_token (parser->lexer)->location;
4148 /* Parse the type-id. */
4149 type = cp_parser_type_id (parser);
4150 /* Look for the closing `)'. */
4151 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4152 /* Using `va_arg' in a constant-expression is not
4153 allowed. */
4154 if (cp_parser_non_integral_constant_expression (parser,
4155 NIC_VA_ARG))
4156 return error_mark_node;
4157 return build_x_va_arg (type_location, expression, type);
4160 case RID_OFFSETOF:
4161 return cp_parser_builtin_offsetof (parser);
4163 case RID_HAS_NOTHROW_ASSIGN:
4164 case RID_HAS_NOTHROW_CONSTRUCTOR:
4165 case RID_HAS_NOTHROW_COPY:
4166 case RID_HAS_TRIVIAL_ASSIGN:
4167 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4168 case RID_HAS_TRIVIAL_COPY:
4169 case RID_HAS_TRIVIAL_DESTRUCTOR:
4170 case RID_HAS_VIRTUAL_DESTRUCTOR:
4171 case RID_IS_ABSTRACT:
4172 case RID_IS_BASE_OF:
4173 case RID_IS_CLASS:
4174 case RID_IS_CONVERTIBLE_TO:
4175 case RID_IS_EMPTY:
4176 case RID_IS_ENUM:
4177 case RID_IS_FINAL:
4178 case RID_IS_LITERAL_TYPE:
4179 case RID_IS_POD:
4180 case RID_IS_POLYMORPHIC:
4181 case RID_IS_STD_LAYOUT:
4182 case RID_IS_TRIVIAL:
4183 case RID_IS_UNION:
4184 return cp_parser_trait_expr (parser, token->keyword);
4186 /* Objective-C++ expressions. */
4187 case RID_AT_ENCODE:
4188 case RID_AT_PROTOCOL:
4189 case RID_AT_SELECTOR:
4190 return cp_parser_objc_expression (parser);
4192 case RID_TEMPLATE:
4193 if (parser->in_function_body
4194 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4195 == CPP_LESS))
4197 error_at (token->location,
4198 "a template declaration cannot appear at block scope");
4199 cp_parser_skip_to_end_of_block_or_statement (parser);
4200 return error_mark_node;
4202 default:
4203 cp_parser_error (parser, "expected primary-expression");
4204 return error_mark_node;
4207 /* An id-expression can start with either an identifier, a
4208 `::' as the beginning of a qualified-id, or the "operator"
4209 keyword. */
4210 case CPP_NAME:
4211 case CPP_SCOPE:
4212 case CPP_TEMPLATE_ID:
4213 case CPP_NESTED_NAME_SPECIFIER:
4215 tree id_expression;
4216 tree decl;
4217 const char *error_msg;
4218 bool template_p;
4219 bool done;
4220 cp_token *id_expr_token;
4222 id_expression:
4223 /* Parse the id-expression. */
4224 id_expression
4225 = cp_parser_id_expression (parser,
4226 /*template_keyword_p=*/false,
4227 /*check_dependency_p=*/true,
4228 &template_p,
4229 /*declarator_p=*/false,
4230 /*optional_p=*/false);
4231 if (id_expression == error_mark_node)
4232 return error_mark_node;
4233 id_expr_token = token;
4234 token = cp_lexer_peek_token (parser->lexer);
4235 done = (token->type != CPP_OPEN_SQUARE
4236 && token->type != CPP_OPEN_PAREN
4237 && token->type != CPP_DOT
4238 && token->type != CPP_DEREF
4239 && token->type != CPP_PLUS_PLUS
4240 && token->type != CPP_MINUS_MINUS);
4241 /* If we have a template-id, then no further lookup is
4242 required. If the template-id was for a template-class, we
4243 will sometimes have a TYPE_DECL at this point. */
4244 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4245 || TREE_CODE (id_expression) == TYPE_DECL)
4246 decl = id_expression;
4247 /* Look up the name. */
4248 else
4250 tree ambiguous_decls;
4252 /* If we already know that this lookup is ambiguous, then
4253 we've already issued an error message; there's no reason
4254 to check again. */
4255 if (id_expr_token->type == CPP_NAME
4256 && id_expr_token->ambiguous_p)
4258 cp_parser_simulate_error (parser);
4259 return error_mark_node;
4262 decl = cp_parser_lookup_name (parser, id_expression,
4263 none_type,
4264 template_p,
4265 /*is_namespace=*/false,
4266 /*check_dependency=*/true,
4267 &ambiguous_decls,
4268 id_expr_token->location);
4269 /* If the lookup was ambiguous, an error will already have
4270 been issued. */
4271 if (ambiguous_decls)
4272 return error_mark_node;
4274 /* In Objective-C++, we may have an Objective-C 2.0
4275 dot-syntax for classes here. */
4276 if (c_dialect_objc ()
4277 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4278 && TREE_CODE (decl) == TYPE_DECL
4279 && objc_is_class_name (decl))
4281 tree component;
4282 cp_lexer_consume_token (parser->lexer);
4283 component = cp_parser_identifier (parser);
4284 if (component == error_mark_node)
4285 return error_mark_node;
4287 return objc_build_class_component_ref (id_expression, component);
4290 /* In Objective-C++, an instance variable (ivar) may be preferred
4291 to whatever cp_parser_lookup_name() found. */
4292 decl = objc_lookup_ivar (decl, id_expression);
4294 /* If name lookup gives us a SCOPE_REF, then the
4295 qualifying scope was dependent. */
4296 if (TREE_CODE (decl) == SCOPE_REF)
4298 /* At this point, we do not know if DECL is a valid
4299 integral constant expression. We assume that it is
4300 in fact such an expression, so that code like:
4302 template <int N> struct A {
4303 int a[B<N>::i];
4306 is accepted. At template-instantiation time, we
4307 will check that B<N>::i is actually a constant. */
4308 return decl;
4310 /* Check to see if DECL is a local variable in a context
4311 where that is forbidden. */
4312 if (parser->local_variables_forbidden_p
4313 && local_variable_p (decl))
4315 /* It might be that we only found DECL because we are
4316 trying to be generous with pre-ISO scoping rules.
4317 For example, consider:
4319 int i;
4320 void g() {
4321 for (int i = 0; i < 10; ++i) {}
4322 extern void f(int j = i);
4325 Here, name look up will originally find the out
4326 of scope `i'. We need to issue a warning message,
4327 but then use the global `i'. */
4328 decl = check_for_out_of_scope_variable (decl);
4329 if (local_variable_p (decl))
4331 error_at (id_expr_token->location,
4332 "local variable %qD may not appear in this context",
4333 decl);
4334 return error_mark_node;
4339 decl = (finish_id_expression
4340 (id_expression, decl, parser->scope,
4341 idk,
4342 parser->integral_constant_expression_p,
4343 parser->allow_non_integral_constant_expression_p,
4344 &parser->non_integral_constant_expression_p,
4345 template_p, done, address_p,
4346 template_arg_p,
4347 &error_msg,
4348 id_expr_token->location));
4349 if (error_msg)
4350 cp_parser_error (parser, error_msg);
4351 return decl;
4354 /* Anything else is an error. */
4355 default:
4356 cp_parser_error (parser, "expected primary-expression");
4357 return error_mark_node;
4361 /* Parse an id-expression.
4363 id-expression:
4364 unqualified-id
4365 qualified-id
4367 qualified-id:
4368 :: [opt] nested-name-specifier template [opt] unqualified-id
4369 :: identifier
4370 :: operator-function-id
4371 :: template-id
4373 Return a representation of the unqualified portion of the
4374 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4375 a `::' or nested-name-specifier.
4377 Often, if the id-expression was a qualified-id, the caller will
4378 want to make a SCOPE_REF to represent the qualified-id. This
4379 function does not do this in order to avoid wastefully creating
4380 SCOPE_REFs when they are not required.
4382 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4383 `template' keyword.
4385 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4386 uninstantiated templates.
4388 If *TEMPLATE_P is non-NULL, it is set to true iff the
4389 `template' keyword is used to explicitly indicate that the entity
4390 named is a template.
4392 If DECLARATOR_P is true, the id-expression is appearing as part of
4393 a declarator, rather than as part of an expression. */
4395 static tree
4396 cp_parser_id_expression (cp_parser *parser,
4397 bool template_keyword_p,
4398 bool check_dependency_p,
4399 bool *template_p,
4400 bool declarator_p,
4401 bool optional_p)
4403 bool global_scope_p;
4404 bool nested_name_specifier_p;
4406 /* Assume the `template' keyword was not used. */
4407 if (template_p)
4408 *template_p = template_keyword_p;
4410 /* Look for the optional `::' operator. */
4411 global_scope_p
4412 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4413 != NULL_TREE);
4414 /* Look for the optional nested-name-specifier. */
4415 nested_name_specifier_p
4416 = (cp_parser_nested_name_specifier_opt (parser,
4417 /*typename_keyword_p=*/false,
4418 check_dependency_p,
4419 /*type_p=*/false,
4420 declarator_p)
4421 != NULL_TREE);
4422 /* If there is a nested-name-specifier, then we are looking at
4423 the first qualified-id production. */
4424 if (nested_name_specifier_p)
4426 tree saved_scope;
4427 tree saved_object_scope;
4428 tree saved_qualifying_scope;
4429 tree unqualified_id;
4430 bool is_template;
4432 /* See if the next token is the `template' keyword. */
4433 if (!template_p)
4434 template_p = &is_template;
4435 *template_p = cp_parser_optional_template_keyword (parser);
4436 /* Name lookup we do during the processing of the
4437 unqualified-id might obliterate SCOPE. */
4438 saved_scope = parser->scope;
4439 saved_object_scope = parser->object_scope;
4440 saved_qualifying_scope = parser->qualifying_scope;
4441 /* Process the final unqualified-id. */
4442 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4443 check_dependency_p,
4444 declarator_p,
4445 /*optional_p=*/false);
4446 /* Restore the SAVED_SCOPE for our caller. */
4447 parser->scope = saved_scope;
4448 parser->object_scope = saved_object_scope;
4449 parser->qualifying_scope = saved_qualifying_scope;
4451 return unqualified_id;
4453 /* Otherwise, if we are in global scope, then we are looking at one
4454 of the other qualified-id productions. */
4455 else if (global_scope_p)
4457 cp_token *token;
4458 tree id;
4460 /* Peek at the next token. */
4461 token = cp_lexer_peek_token (parser->lexer);
4463 /* If it's an identifier, and the next token is not a "<", then
4464 we can avoid the template-id case. This is an optimization
4465 for this common case. */
4466 if (token->type == CPP_NAME
4467 && !cp_parser_nth_token_starts_template_argument_list_p
4468 (parser, 2))
4469 return cp_parser_identifier (parser);
4471 cp_parser_parse_tentatively (parser);
4472 /* Try a template-id. */
4473 id = cp_parser_template_id (parser,
4474 /*template_keyword_p=*/false,
4475 /*check_dependency_p=*/true,
4476 none_type,
4477 declarator_p);
4478 /* If that worked, we're done. */
4479 if (cp_parser_parse_definitely (parser))
4480 return id;
4482 /* Peek at the next token. (Changes in the token buffer may
4483 have invalidated the pointer obtained above.) */
4484 token = cp_lexer_peek_token (parser->lexer);
4486 switch (token->type)
4488 case CPP_NAME:
4489 return cp_parser_identifier (parser);
4491 case CPP_KEYWORD:
4492 if (token->keyword == RID_OPERATOR)
4493 return cp_parser_operator_function_id (parser);
4494 /* Fall through. */
4496 default:
4497 cp_parser_error (parser, "expected id-expression");
4498 return error_mark_node;
4501 else
4502 return cp_parser_unqualified_id (parser, template_keyword_p,
4503 /*check_dependency_p=*/true,
4504 declarator_p,
4505 optional_p);
4508 /* Parse an unqualified-id.
4510 unqualified-id:
4511 identifier
4512 operator-function-id
4513 conversion-function-id
4514 ~ class-name
4515 template-id
4517 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4518 keyword, in a construct like `A::template ...'.
4520 Returns a representation of unqualified-id. For the `identifier'
4521 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4522 production a BIT_NOT_EXPR is returned; the operand of the
4523 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4524 other productions, see the documentation accompanying the
4525 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4526 names are looked up in uninstantiated templates. If DECLARATOR_P
4527 is true, the unqualified-id is appearing as part of a declarator,
4528 rather than as part of an expression. */
4530 static tree
4531 cp_parser_unqualified_id (cp_parser* parser,
4532 bool template_keyword_p,
4533 bool check_dependency_p,
4534 bool declarator_p,
4535 bool optional_p)
4537 cp_token *token;
4539 /* Peek at the next token. */
4540 token = cp_lexer_peek_token (parser->lexer);
4542 switch (token->type)
4544 case CPP_NAME:
4546 tree id;
4548 /* We don't know yet whether or not this will be a
4549 template-id. */
4550 cp_parser_parse_tentatively (parser);
4551 /* Try a template-id. */
4552 id = cp_parser_template_id (parser, template_keyword_p,
4553 check_dependency_p,
4554 none_type,
4555 declarator_p);
4556 /* If it worked, we're done. */
4557 if (cp_parser_parse_definitely (parser))
4558 return id;
4559 /* Otherwise, it's an ordinary identifier. */
4560 return cp_parser_identifier (parser);
4563 case CPP_TEMPLATE_ID:
4564 return cp_parser_template_id (parser, template_keyword_p,
4565 check_dependency_p,
4566 none_type,
4567 declarator_p);
4569 case CPP_COMPL:
4571 tree type_decl;
4572 tree qualifying_scope;
4573 tree object_scope;
4574 tree scope;
4575 bool done;
4577 /* Consume the `~' token. */
4578 cp_lexer_consume_token (parser->lexer);
4579 /* Parse the class-name. The standard, as written, seems to
4580 say that:
4582 template <typename T> struct S { ~S (); };
4583 template <typename T> S<T>::~S() {}
4585 is invalid, since `~' must be followed by a class-name, but
4586 `S<T>' is dependent, and so not known to be a class.
4587 That's not right; we need to look in uninstantiated
4588 templates. A further complication arises from:
4590 template <typename T> void f(T t) {
4591 t.T::~T();
4594 Here, it is not possible to look up `T' in the scope of `T'
4595 itself. We must look in both the current scope, and the
4596 scope of the containing complete expression.
4598 Yet another issue is:
4600 struct S {
4601 int S;
4602 ~S();
4605 S::~S() {}
4607 The standard does not seem to say that the `S' in `~S'
4608 should refer to the type `S' and not the data member
4609 `S::S'. */
4611 /* DR 244 says that we look up the name after the "~" in the
4612 same scope as we looked up the qualifying name. That idea
4613 isn't fully worked out; it's more complicated than that. */
4614 scope = parser->scope;
4615 object_scope = parser->object_scope;
4616 qualifying_scope = parser->qualifying_scope;
4618 /* Check for invalid scopes. */
4619 if (scope == error_mark_node)
4621 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4622 cp_lexer_consume_token (parser->lexer);
4623 return error_mark_node;
4625 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4627 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4628 error_at (token->location,
4629 "scope %qT before %<~%> is not a class-name",
4630 scope);
4631 cp_parser_simulate_error (parser);
4632 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4633 cp_lexer_consume_token (parser->lexer);
4634 return error_mark_node;
4636 gcc_assert (!scope || TYPE_P (scope));
4638 /* If the name is of the form "X::~X" it's OK even if X is a
4639 typedef. */
4640 token = cp_lexer_peek_token (parser->lexer);
4641 if (scope
4642 && token->type == CPP_NAME
4643 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4644 != CPP_LESS)
4645 && (token->u.value == TYPE_IDENTIFIER (scope)
4646 || (CLASS_TYPE_P (scope)
4647 && constructor_name_p (token->u.value, scope))))
4649 cp_lexer_consume_token (parser->lexer);
4650 return build_nt (BIT_NOT_EXPR, scope);
4653 /* If there was an explicit qualification (S::~T), first look
4654 in the scope given by the qualification (i.e., S).
4656 Note: in the calls to cp_parser_class_name below we pass
4657 typename_type so that lookup finds the injected-class-name
4658 rather than the constructor. */
4659 done = false;
4660 type_decl = NULL_TREE;
4661 if (scope)
4663 cp_parser_parse_tentatively (parser);
4664 type_decl = cp_parser_class_name (parser,
4665 /*typename_keyword_p=*/false,
4666 /*template_keyword_p=*/false,
4667 typename_type,
4668 /*check_dependency=*/false,
4669 /*class_head_p=*/false,
4670 declarator_p);
4671 if (cp_parser_parse_definitely (parser))
4672 done = true;
4674 /* In "N::S::~S", look in "N" as well. */
4675 if (!done && scope && qualifying_scope)
4677 cp_parser_parse_tentatively (parser);
4678 parser->scope = qualifying_scope;
4679 parser->object_scope = NULL_TREE;
4680 parser->qualifying_scope = NULL_TREE;
4681 type_decl
4682 = cp_parser_class_name (parser,
4683 /*typename_keyword_p=*/false,
4684 /*template_keyword_p=*/false,
4685 typename_type,
4686 /*check_dependency=*/false,
4687 /*class_head_p=*/false,
4688 declarator_p);
4689 if (cp_parser_parse_definitely (parser))
4690 done = true;
4692 /* In "p->S::~T", look in the scope given by "*p" as well. */
4693 else if (!done && object_scope)
4695 cp_parser_parse_tentatively (parser);
4696 parser->scope = object_scope;
4697 parser->object_scope = NULL_TREE;
4698 parser->qualifying_scope = NULL_TREE;
4699 type_decl
4700 = cp_parser_class_name (parser,
4701 /*typename_keyword_p=*/false,
4702 /*template_keyword_p=*/false,
4703 typename_type,
4704 /*check_dependency=*/false,
4705 /*class_head_p=*/false,
4706 declarator_p);
4707 if (cp_parser_parse_definitely (parser))
4708 done = true;
4710 /* Look in the surrounding context. */
4711 if (!done)
4713 parser->scope = NULL_TREE;
4714 parser->object_scope = NULL_TREE;
4715 parser->qualifying_scope = NULL_TREE;
4716 if (processing_template_decl)
4717 cp_parser_parse_tentatively (parser);
4718 type_decl
4719 = cp_parser_class_name (parser,
4720 /*typename_keyword_p=*/false,
4721 /*template_keyword_p=*/false,
4722 typename_type,
4723 /*check_dependency=*/false,
4724 /*class_head_p=*/false,
4725 declarator_p);
4726 if (processing_template_decl
4727 && ! cp_parser_parse_definitely (parser))
4729 /* We couldn't find a type with this name, so just accept
4730 it and check for a match at instantiation time. */
4731 type_decl = cp_parser_identifier (parser);
4732 if (type_decl != error_mark_node)
4733 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4734 return type_decl;
4737 /* If an error occurred, assume that the name of the
4738 destructor is the same as the name of the qualifying
4739 class. That allows us to keep parsing after running
4740 into ill-formed destructor names. */
4741 if (type_decl == error_mark_node && scope)
4742 return build_nt (BIT_NOT_EXPR, scope);
4743 else if (type_decl == error_mark_node)
4744 return error_mark_node;
4746 /* Check that destructor name and scope match. */
4747 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4749 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4750 error_at (token->location,
4751 "declaration of %<~%T%> as member of %qT",
4752 type_decl, scope);
4753 cp_parser_simulate_error (parser);
4754 return error_mark_node;
4757 /* [class.dtor]
4759 A typedef-name that names a class shall not be used as the
4760 identifier in the declarator for a destructor declaration. */
4761 if (declarator_p
4762 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4763 && !DECL_SELF_REFERENCE_P (type_decl)
4764 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4765 error_at (token->location,
4766 "typedef-name %qD used as destructor declarator",
4767 type_decl);
4769 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4772 case CPP_KEYWORD:
4773 if (token->keyword == RID_OPERATOR)
4775 tree id;
4777 /* This could be a template-id, so we try that first. */
4778 cp_parser_parse_tentatively (parser);
4779 /* Try a template-id. */
4780 id = cp_parser_template_id (parser, template_keyword_p,
4781 /*check_dependency_p=*/true,
4782 none_type,
4783 declarator_p);
4784 /* If that worked, we're done. */
4785 if (cp_parser_parse_definitely (parser))
4786 return id;
4787 /* We still don't know whether we're looking at an
4788 operator-function-id or a conversion-function-id. */
4789 cp_parser_parse_tentatively (parser);
4790 /* Try an operator-function-id. */
4791 id = cp_parser_operator_function_id (parser);
4792 /* If that didn't work, try a conversion-function-id. */
4793 if (!cp_parser_parse_definitely (parser))
4794 id = cp_parser_conversion_function_id (parser);
4795 else if (UDLIT_OPER_P (id))
4797 /* 17.6.3.3.5 */
4798 const char *name = UDLIT_OP_SUFFIX (id);
4799 if (name[0] != '_' && !in_system_header)
4800 warning (0, "literal operator suffixes not preceded by %<_%>"
4801 " are reserved for future standardization");
4804 return id;
4806 /* Fall through. */
4808 default:
4809 if (optional_p)
4810 return NULL_TREE;
4811 cp_parser_error (parser, "expected unqualified-id");
4812 return error_mark_node;
4816 /* Parse an (optional) nested-name-specifier.
4818 nested-name-specifier: [C++98]
4819 class-or-namespace-name :: nested-name-specifier [opt]
4820 class-or-namespace-name :: template nested-name-specifier [opt]
4822 nested-name-specifier: [C++0x]
4823 type-name ::
4824 namespace-name ::
4825 nested-name-specifier identifier ::
4826 nested-name-specifier template [opt] simple-template-id ::
4828 PARSER->SCOPE should be set appropriately before this function is
4829 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4830 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4831 in name lookups.
4833 Sets PARSER->SCOPE to the class (TYPE) or namespace
4834 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4835 it unchanged if there is no nested-name-specifier. Returns the new
4836 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4838 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4839 part of a declaration and/or decl-specifier. */
4841 static tree
4842 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4843 bool typename_keyword_p,
4844 bool check_dependency_p,
4845 bool type_p,
4846 bool is_declaration)
4848 bool success = false;
4849 cp_token_position start = 0;
4850 cp_token *token;
4852 /* Remember where the nested-name-specifier starts. */
4853 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4855 start = cp_lexer_token_position (parser->lexer, false);
4856 push_deferring_access_checks (dk_deferred);
4859 while (true)
4861 tree new_scope;
4862 tree old_scope;
4863 tree saved_qualifying_scope;
4864 bool template_keyword_p;
4866 /* Spot cases that cannot be the beginning of a
4867 nested-name-specifier. */
4868 token = cp_lexer_peek_token (parser->lexer);
4870 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4871 the already parsed nested-name-specifier. */
4872 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4874 /* Grab the nested-name-specifier and continue the loop. */
4875 cp_parser_pre_parsed_nested_name_specifier (parser);
4876 /* If we originally encountered this nested-name-specifier
4877 with IS_DECLARATION set to false, we will not have
4878 resolved TYPENAME_TYPEs, so we must do so here. */
4879 if (is_declaration
4880 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4882 new_scope = resolve_typename_type (parser->scope,
4883 /*only_current_p=*/false);
4884 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4885 parser->scope = new_scope;
4887 success = true;
4888 continue;
4891 /* Spot cases that cannot be the beginning of a
4892 nested-name-specifier. On the second and subsequent times
4893 through the loop, we look for the `template' keyword. */
4894 if (success && token->keyword == RID_TEMPLATE)
4896 /* A template-id can start a nested-name-specifier. */
4897 else if (token->type == CPP_TEMPLATE_ID)
4899 /* DR 743: decltype can be used in a nested-name-specifier. */
4900 else if (token_is_decltype (token))
4902 else
4904 /* If the next token is not an identifier, then it is
4905 definitely not a type-name or namespace-name. */
4906 if (token->type != CPP_NAME)
4907 break;
4908 /* If the following token is neither a `<' (to begin a
4909 template-id), nor a `::', then we are not looking at a
4910 nested-name-specifier. */
4911 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4913 if (token->type == CPP_COLON
4914 && parser->colon_corrects_to_scope_p
4915 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4917 error_at (token->location,
4918 "found %<:%> in nested-name-specifier, expected %<::%>");
4919 token->type = CPP_SCOPE;
4922 if (token->type != CPP_SCOPE
4923 && !cp_parser_nth_token_starts_template_argument_list_p
4924 (parser, 2))
4925 break;
4928 /* The nested-name-specifier is optional, so we parse
4929 tentatively. */
4930 cp_parser_parse_tentatively (parser);
4932 /* Look for the optional `template' keyword, if this isn't the
4933 first time through the loop. */
4934 if (success)
4935 template_keyword_p = cp_parser_optional_template_keyword (parser);
4936 else
4937 template_keyword_p = false;
4939 /* Save the old scope since the name lookup we are about to do
4940 might destroy it. */
4941 old_scope = parser->scope;
4942 saved_qualifying_scope = parser->qualifying_scope;
4943 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4944 look up names in "X<T>::I" in order to determine that "Y" is
4945 a template. So, if we have a typename at this point, we make
4946 an effort to look through it. */
4947 if (is_declaration
4948 && !typename_keyword_p
4949 && parser->scope
4950 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4951 parser->scope = resolve_typename_type (parser->scope,
4952 /*only_current_p=*/false);
4953 /* Parse the qualifying entity. */
4954 new_scope
4955 = cp_parser_qualifying_entity (parser,
4956 typename_keyword_p,
4957 template_keyword_p,
4958 check_dependency_p,
4959 type_p,
4960 is_declaration);
4961 /* Look for the `::' token. */
4962 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4964 /* If we found what we wanted, we keep going; otherwise, we're
4965 done. */
4966 if (!cp_parser_parse_definitely (parser))
4968 bool error_p = false;
4970 /* Restore the OLD_SCOPE since it was valid before the
4971 failed attempt at finding the last
4972 class-or-namespace-name. */
4973 parser->scope = old_scope;
4974 parser->qualifying_scope = saved_qualifying_scope;
4976 /* If the next token is a decltype, and the one after that is a
4977 `::', then the decltype has failed to resolve to a class or
4978 enumeration type. Give this error even when parsing
4979 tentatively since it can't possibly be valid--and we're going
4980 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4981 won't get another chance.*/
4982 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4983 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4984 == CPP_SCOPE))
4986 token = cp_lexer_consume_token (parser->lexer);
4987 error_at (token->location, "decltype evaluates to %qT, "
4988 "which is not a class or enumeration type",
4989 token->u.value);
4990 parser->scope = error_mark_node;
4991 error_p = true;
4992 /* As below. */
4993 success = true;
4994 cp_lexer_consume_token (parser->lexer);
4997 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4998 break;
4999 /* If the next token is an identifier, and the one after
5000 that is a `::', then any valid interpretation would have
5001 found a class-or-namespace-name. */
5002 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5003 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5004 == CPP_SCOPE)
5005 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5006 != CPP_COMPL))
5008 token = cp_lexer_consume_token (parser->lexer);
5009 if (!error_p)
5011 if (!token->ambiguous_p)
5013 tree decl;
5014 tree ambiguous_decls;
5016 decl = cp_parser_lookup_name (parser, token->u.value,
5017 none_type,
5018 /*is_template=*/false,
5019 /*is_namespace=*/false,
5020 /*check_dependency=*/true,
5021 &ambiguous_decls,
5022 token->location);
5023 if (TREE_CODE (decl) == TEMPLATE_DECL)
5024 error_at (token->location,
5025 "%qD used without template parameters",
5026 decl);
5027 else if (ambiguous_decls)
5029 error_at (token->location,
5030 "reference to %qD is ambiguous",
5031 token->u.value);
5032 print_candidates (ambiguous_decls);
5033 decl = error_mark_node;
5035 else
5037 if (cxx_dialect != cxx98)
5038 cp_parser_name_lookup_error
5039 (parser, token->u.value, decl, NLE_NOT_CXX98,
5040 token->location);
5041 else
5042 cp_parser_name_lookup_error
5043 (parser, token->u.value, decl, NLE_CXX98,
5044 token->location);
5047 parser->scope = error_mark_node;
5048 error_p = true;
5049 /* Treat this as a successful nested-name-specifier
5050 due to:
5052 [basic.lookup.qual]
5054 If the name found is not a class-name (clause
5055 _class_) or namespace-name (_namespace.def_), the
5056 program is ill-formed. */
5057 success = true;
5059 cp_lexer_consume_token (parser->lexer);
5061 break;
5063 /* We've found one valid nested-name-specifier. */
5064 success = true;
5065 /* Name lookup always gives us a DECL. */
5066 if (TREE_CODE (new_scope) == TYPE_DECL)
5067 new_scope = TREE_TYPE (new_scope);
5068 /* Uses of "template" must be followed by actual templates. */
5069 if (template_keyword_p
5070 && !(CLASS_TYPE_P (new_scope)
5071 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5072 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5073 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5074 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5075 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5076 == TEMPLATE_ID_EXPR)))
5077 permerror (input_location, TYPE_P (new_scope)
5078 ? G_("%qT is not a template")
5079 : G_("%qD is not a template"),
5080 new_scope);
5081 /* If it is a class scope, try to complete it; we are about to
5082 be looking up names inside the class. */
5083 if (TYPE_P (new_scope)
5084 /* Since checking types for dependency can be expensive,
5085 avoid doing it if the type is already complete. */
5086 && !COMPLETE_TYPE_P (new_scope)
5087 /* Do not try to complete dependent types. */
5088 && !dependent_type_p (new_scope))
5090 new_scope = complete_type (new_scope);
5091 /* If it is a typedef to current class, use the current
5092 class instead, as the typedef won't have any names inside
5093 it yet. */
5094 if (!COMPLETE_TYPE_P (new_scope)
5095 && currently_open_class (new_scope))
5096 new_scope = TYPE_MAIN_VARIANT (new_scope);
5098 /* Make sure we look in the right scope the next time through
5099 the loop. */
5100 parser->scope = new_scope;
5103 /* If parsing tentatively, replace the sequence of tokens that makes
5104 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5105 token. That way, should we re-parse the token stream, we will
5106 not have to repeat the effort required to do the parse, nor will
5107 we issue duplicate error messages. */
5108 if (success && start)
5110 cp_token *token;
5112 token = cp_lexer_token_at (parser->lexer, start);
5113 /* Reset the contents of the START token. */
5114 token->type = CPP_NESTED_NAME_SPECIFIER;
5115 /* Retrieve any deferred checks. Do not pop this access checks yet
5116 so the memory will not be reclaimed during token replacing below. */
5117 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5118 token->u.tree_check_value->value = parser->scope;
5119 token->u.tree_check_value->checks = get_deferred_access_checks ();
5120 token->u.tree_check_value->qualifying_scope =
5121 parser->qualifying_scope;
5122 token->keyword = RID_MAX;
5124 /* Purge all subsequent tokens. */
5125 cp_lexer_purge_tokens_after (parser->lexer, start);
5128 if (start)
5129 pop_to_parent_deferring_access_checks ();
5131 return success ? parser->scope : NULL_TREE;
5134 /* Parse a nested-name-specifier. See
5135 cp_parser_nested_name_specifier_opt for details. This function
5136 behaves identically, except that it will an issue an error if no
5137 nested-name-specifier is present. */
5139 static tree
5140 cp_parser_nested_name_specifier (cp_parser *parser,
5141 bool typename_keyword_p,
5142 bool check_dependency_p,
5143 bool type_p,
5144 bool is_declaration)
5146 tree scope;
5148 /* Look for the nested-name-specifier. */
5149 scope = cp_parser_nested_name_specifier_opt (parser,
5150 typename_keyword_p,
5151 check_dependency_p,
5152 type_p,
5153 is_declaration);
5154 /* If it was not present, issue an error message. */
5155 if (!scope)
5157 cp_parser_error (parser, "expected nested-name-specifier");
5158 parser->scope = NULL_TREE;
5161 return scope;
5164 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5165 this is either a class-name or a namespace-name (which corresponds
5166 to the class-or-namespace-name production in the grammar). For
5167 C++0x, it can also be a type-name that refers to an enumeration
5168 type or a simple-template-id.
5170 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5171 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5172 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5173 TYPE_P is TRUE iff the next name should be taken as a class-name,
5174 even the same name is declared to be another entity in the same
5175 scope.
5177 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5178 specified by the class-or-namespace-name. If neither is found the
5179 ERROR_MARK_NODE is returned. */
5181 static tree
5182 cp_parser_qualifying_entity (cp_parser *parser,
5183 bool typename_keyword_p,
5184 bool template_keyword_p,
5185 bool check_dependency_p,
5186 bool type_p,
5187 bool is_declaration)
5189 tree saved_scope;
5190 tree saved_qualifying_scope;
5191 tree saved_object_scope;
5192 tree scope;
5193 bool only_class_p;
5194 bool successful_parse_p;
5196 /* DR 743: decltype can appear in a nested-name-specifier. */
5197 if (cp_lexer_next_token_is_decltype (parser->lexer))
5199 scope = cp_parser_decltype (parser);
5200 if (TREE_CODE (scope) != ENUMERAL_TYPE
5201 && !MAYBE_CLASS_TYPE_P (scope))
5203 cp_parser_simulate_error (parser);
5204 return error_mark_node;
5206 if (TYPE_NAME (scope))
5207 scope = TYPE_NAME (scope);
5208 return scope;
5211 /* Before we try to parse the class-name, we must save away the
5212 current PARSER->SCOPE since cp_parser_class_name will destroy
5213 it. */
5214 saved_scope = parser->scope;
5215 saved_qualifying_scope = parser->qualifying_scope;
5216 saved_object_scope = parser->object_scope;
5217 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5218 there is no need to look for a namespace-name. */
5219 only_class_p = template_keyword_p
5220 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5221 if (!only_class_p)
5222 cp_parser_parse_tentatively (parser);
5223 scope = cp_parser_class_name (parser,
5224 typename_keyword_p,
5225 template_keyword_p,
5226 type_p ? class_type : none_type,
5227 check_dependency_p,
5228 /*class_head_p=*/false,
5229 is_declaration);
5230 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5231 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5232 if (!only_class_p
5233 && cxx_dialect != cxx98
5234 && !successful_parse_p)
5236 /* Restore the saved scope. */
5237 parser->scope = saved_scope;
5238 parser->qualifying_scope = saved_qualifying_scope;
5239 parser->object_scope = saved_object_scope;
5241 /* Parse tentatively. */
5242 cp_parser_parse_tentatively (parser);
5244 /* Parse a type-name */
5245 scope = cp_parser_type_name (parser);
5247 /* "If the name found does not designate a namespace or a class,
5248 enumeration, or dependent type, the program is ill-formed."
5250 We cover classes and dependent types above and namespaces below,
5251 so this code is only looking for enums. */
5252 if (!scope || TREE_CODE (scope) != TYPE_DECL
5253 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5254 cp_parser_simulate_error (parser);
5256 successful_parse_p = cp_parser_parse_definitely (parser);
5258 /* If that didn't work, try for a namespace-name. */
5259 if (!only_class_p && !successful_parse_p)
5261 /* Restore the saved scope. */
5262 parser->scope = saved_scope;
5263 parser->qualifying_scope = saved_qualifying_scope;
5264 parser->object_scope = saved_object_scope;
5265 /* If we are not looking at an identifier followed by the scope
5266 resolution operator, then this is not part of a
5267 nested-name-specifier. (Note that this function is only used
5268 to parse the components of a nested-name-specifier.) */
5269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5270 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5271 return error_mark_node;
5272 scope = cp_parser_namespace_name (parser);
5275 return scope;
5278 /* Parse a postfix-expression.
5280 postfix-expression:
5281 primary-expression
5282 postfix-expression [ expression ]
5283 postfix-expression ( expression-list [opt] )
5284 simple-type-specifier ( expression-list [opt] )
5285 typename :: [opt] nested-name-specifier identifier
5286 ( expression-list [opt] )
5287 typename :: [opt] nested-name-specifier template [opt] template-id
5288 ( expression-list [opt] )
5289 postfix-expression . template [opt] id-expression
5290 postfix-expression -> template [opt] id-expression
5291 postfix-expression . pseudo-destructor-name
5292 postfix-expression -> pseudo-destructor-name
5293 postfix-expression ++
5294 postfix-expression --
5295 dynamic_cast < type-id > ( expression )
5296 static_cast < type-id > ( expression )
5297 reinterpret_cast < type-id > ( expression )
5298 const_cast < type-id > ( expression )
5299 typeid ( expression )
5300 typeid ( type-id )
5302 GNU Extension:
5304 postfix-expression:
5305 ( type-id ) { initializer-list , [opt] }
5307 This extension is a GNU version of the C99 compound-literal
5308 construct. (The C99 grammar uses `type-name' instead of `type-id',
5309 but they are essentially the same concept.)
5311 If ADDRESS_P is true, the postfix expression is the operand of the
5312 `&' operator. CAST_P is true if this expression is the target of a
5313 cast.
5315 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5316 class member access expressions [expr.ref].
5318 Returns a representation of the expression. */
5320 static tree
5321 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5322 bool member_access_only_p,
5323 cp_id_kind * pidk_return)
5325 cp_token *token;
5326 enum rid keyword;
5327 cp_id_kind idk = CP_ID_KIND_NONE;
5328 tree postfix_expression = NULL_TREE;
5329 bool is_member_access = false;
5331 /* Peek at the next token. */
5332 token = cp_lexer_peek_token (parser->lexer);
5333 /* Some of the productions are determined by keywords. */
5334 keyword = token->keyword;
5335 switch (keyword)
5337 case RID_DYNCAST:
5338 case RID_STATCAST:
5339 case RID_REINTCAST:
5340 case RID_CONSTCAST:
5342 tree type;
5343 tree expression;
5344 const char *saved_message;
5346 /* All of these can be handled in the same way from the point
5347 of view of parsing. Begin by consuming the token
5348 identifying the cast. */
5349 cp_lexer_consume_token (parser->lexer);
5351 /* New types cannot be defined in the cast. */
5352 saved_message = parser->type_definition_forbidden_message;
5353 parser->type_definition_forbidden_message
5354 = G_("types may not be defined in casts");
5356 /* Look for the opening `<'. */
5357 cp_parser_require (parser, CPP_LESS, RT_LESS);
5358 /* Parse the type to which we are casting. */
5359 type = cp_parser_type_id (parser);
5360 /* Look for the closing `>'. */
5361 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5362 /* Restore the old message. */
5363 parser->type_definition_forbidden_message = saved_message;
5365 /* And the expression which is being cast. */
5366 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5367 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5368 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5370 /* Only type conversions to integral or enumeration types
5371 can be used in constant-expressions. */
5372 if (!cast_valid_in_integral_constant_expression_p (type)
5373 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5374 return error_mark_node;
5376 switch (keyword)
5378 case RID_DYNCAST:
5379 postfix_expression
5380 = build_dynamic_cast (type, expression, tf_warning_or_error);
5381 break;
5382 case RID_STATCAST:
5383 postfix_expression
5384 = build_static_cast (type, expression, tf_warning_or_error);
5385 break;
5386 case RID_REINTCAST:
5387 postfix_expression
5388 = build_reinterpret_cast (type, expression,
5389 tf_warning_or_error);
5390 break;
5391 case RID_CONSTCAST:
5392 postfix_expression
5393 = build_const_cast (type, expression, tf_warning_or_error);
5394 break;
5395 default:
5396 gcc_unreachable ();
5399 break;
5401 case RID_TYPEID:
5403 tree type;
5404 const char *saved_message;
5405 bool saved_in_type_id_in_expr_p;
5407 /* Consume the `typeid' token. */
5408 cp_lexer_consume_token (parser->lexer);
5409 /* Look for the `(' token. */
5410 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5411 /* Types cannot be defined in a `typeid' expression. */
5412 saved_message = parser->type_definition_forbidden_message;
5413 parser->type_definition_forbidden_message
5414 = G_("types may not be defined in a %<typeid%> expression");
5415 /* We can't be sure yet whether we're looking at a type-id or an
5416 expression. */
5417 cp_parser_parse_tentatively (parser);
5418 /* Try a type-id first. */
5419 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5420 parser->in_type_id_in_expr_p = true;
5421 type = cp_parser_type_id (parser);
5422 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5423 /* Look for the `)' token. Otherwise, we can't be sure that
5424 we're not looking at an expression: consider `typeid (int
5425 (3))', for example. */
5426 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5427 /* If all went well, simply lookup the type-id. */
5428 if (cp_parser_parse_definitely (parser))
5429 postfix_expression = get_typeid (type);
5430 /* Otherwise, fall back to the expression variant. */
5431 else
5433 tree expression;
5435 /* Look for an expression. */
5436 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5437 /* Compute its typeid. */
5438 postfix_expression = build_typeid (expression);
5439 /* Look for the `)' token. */
5440 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5442 /* Restore the saved message. */
5443 parser->type_definition_forbidden_message = saved_message;
5444 /* `typeid' may not appear in an integral constant expression. */
5445 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5446 return error_mark_node;
5448 break;
5450 case RID_TYPENAME:
5452 tree type;
5453 /* The syntax permitted here is the same permitted for an
5454 elaborated-type-specifier. */
5455 type = cp_parser_elaborated_type_specifier (parser,
5456 /*is_friend=*/false,
5457 /*is_declaration=*/false);
5458 postfix_expression = cp_parser_functional_cast (parser, type);
5460 break;
5462 case RID_BUILTIN_SHUFFLE:
5464 VEC(tree,gc)* vec;
5465 unsigned int i;
5466 tree p;
5467 location_t loc = token->location;
5469 cp_lexer_consume_token (parser->lexer);
5470 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5471 /*cast_p=*/false, /*allow_expansion_p=*/true,
5472 /*non_constant_p=*/NULL);
5473 if (vec == NULL)
5474 return error_mark_node;
5476 FOR_EACH_VEC_ELT (tree, vec, i, p)
5477 mark_exp_read (p);
5479 if (VEC_length (tree, vec) == 2)
5480 return
5481 c_build_vec_perm_expr
5482 (loc, VEC_index (tree, vec, 0),
5483 NULL_TREE, VEC_index (tree, vec, 1));
5485 else if (VEC_length (tree, vec) == 3)
5486 return
5487 c_build_vec_perm_expr
5488 (loc, VEC_index (tree, vec, 0),
5489 VEC_index (tree, vec, 1),
5490 VEC_index (tree, vec, 2));
5491 else
5493 error_at (loc, "wrong number of arguments to "
5494 "%<__builtin_shuffle%>");
5495 return error_mark_node;
5497 break;
5500 default:
5502 tree type;
5504 /* If the next thing is a simple-type-specifier, we may be
5505 looking at a functional cast. We could also be looking at
5506 an id-expression. So, we try the functional cast, and if
5507 that doesn't work we fall back to the primary-expression. */
5508 cp_parser_parse_tentatively (parser);
5509 /* Look for the simple-type-specifier. */
5510 type = cp_parser_simple_type_specifier (parser,
5511 /*decl_specs=*/NULL,
5512 CP_PARSER_FLAGS_NONE);
5513 /* Parse the cast itself. */
5514 if (!cp_parser_error_occurred (parser))
5515 postfix_expression
5516 = cp_parser_functional_cast (parser, type);
5517 /* If that worked, we're done. */
5518 if (cp_parser_parse_definitely (parser))
5519 break;
5521 /* If the functional-cast didn't work out, try a
5522 compound-literal. */
5523 if (cp_parser_allow_gnu_extensions_p (parser)
5524 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5526 VEC(constructor_elt,gc) *initializer_list = NULL;
5527 bool saved_in_type_id_in_expr_p;
5529 cp_parser_parse_tentatively (parser);
5530 /* Consume the `('. */
5531 cp_lexer_consume_token (parser->lexer);
5532 /* Parse the type. */
5533 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5534 parser->in_type_id_in_expr_p = true;
5535 type = cp_parser_type_id (parser);
5536 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5537 /* Look for the `)'. */
5538 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5539 /* Look for the `{'. */
5540 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5541 /* If things aren't going well, there's no need to
5542 keep going. */
5543 if (!cp_parser_error_occurred (parser))
5545 bool non_constant_p;
5546 /* Parse the initializer-list. */
5547 initializer_list
5548 = cp_parser_initializer_list (parser, &non_constant_p);
5549 /* Allow a trailing `,'. */
5550 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5551 cp_lexer_consume_token (parser->lexer);
5552 /* Look for the final `}'. */
5553 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5555 /* If that worked, we're definitely looking at a
5556 compound-literal expression. */
5557 if (cp_parser_parse_definitely (parser))
5559 /* Warn the user that a compound literal is not
5560 allowed in standard C++. */
5561 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5562 /* For simplicity, we disallow compound literals in
5563 constant-expressions. We could
5564 allow compound literals of integer type, whose
5565 initializer was a constant, in constant
5566 expressions. Permitting that usage, as a further
5567 extension, would not change the meaning of any
5568 currently accepted programs. (Of course, as
5569 compound literals are not part of ISO C++, the
5570 standard has nothing to say.) */
5571 if (cp_parser_non_integral_constant_expression (parser,
5572 NIC_NCC))
5574 postfix_expression = error_mark_node;
5575 break;
5577 /* Form the representation of the compound-literal. */
5578 postfix_expression
5579 = (finish_compound_literal
5580 (type, build_constructor (init_list_type_node,
5581 initializer_list),
5582 tf_warning_or_error));
5583 break;
5587 /* It must be a primary-expression. */
5588 postfix_expression
5589 = cp_parser_primary_expression (parser, address_p, cast_p,
5590 /*template_arg_p=*/false,
5591 &idk);
5593 break;
5596 /* Keep looping until the postfix-expression is complete. */
5597 while (true)
5599 if (idk == CP_ID_KIND_UNQUALIFIED
5600 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5601 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5602 /* It is not a Koenig lookup function call. */
5603 postfix_expression
5604 = unqualified_name_lookup_error (postfix_expression);
5606 /* Peek at the next token. */
5607 token = cp_lexer_peek_token (parser->lexer);
5609 switch (token->type)
5611 case CPP_OPEN_SQUARE:
5612 postfix_expression
5613 = cp_parser_postfix_open_square_expression (parser,
5614 postfix_expression,
5615 false);
5616 idk = CP_ID_KIND_NONE;
5617 is_member_access = false;
5618 break;
5620 case CPP_OPEN_PAREN:
5621 /* postfix-expression ( expression-list [opt] ) */
5623 bool koenig_p;
5624 bool is_builtin_constant_p;
5625 bool saved_integral_constant_expression_p = false;
5626 bool saved_non_integral_constant_expression_p = false;
5627 VEC(tree,gc) *args;
5629 is_member_access = false;
5631 is_builtin_constant_p
5632 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5633 if (is_builtin_constant_p)
5635 /* The whole point of __builtin_constant_p is to allow
5636 non-constant expressions to appear as arguments. */
5637 saved_integral_constant_expression_p
5638 = parser->integral_constant_expression_p;
5639 saved_non_integral_constant_expression_p
5640 = parser->non_integral_constant_expression_p;
5641 parser->integral_constant_expression_p = false;
5643 args = (cp_parser_parenthesized_expression_list
5644 (parser, non_attr,
5645 /*cast_p=*/false, /*allow_expansion_p=*/true,
5646 /*non_constant_p=*/NULL));
5647 if (is_builtin_constant_p)
5649 parser->integral_constant_expression_p
5650 = saved_integral_constant_expression_p;
5651 parser->non_integral_constant_expression_p
5652 = saved_non_integral_constant_expression_p;
5655 if (args == NULL)
5657 postfix_expression = error_mark_node;
5658 break;
5661 /* Function calls are not permitted in
5662 constant-expressions. */
5663 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5664 && cp_parser_non_integral_constant_expression (parser,
5665 NIC_FUNC_CALL))
5667 postfix_expression = error_mark_node;
5668 release_tree_vector (args);
5669 break;
5672 koenig_p = false;
5673 if (idk == CP_ID_KIND_UNQUALIFIED
5674 || idk == CP_ID_KIND_TEMPLATE_ID)
5676 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5678 if (!VEC_empty (tree, args))
5680 koenig_p = true;
5681 if (!any_type_dependent_arguments_p (args))
5682 postfix_expression
5683 = perform_koenig_lookup (postfix_expression, args,
5684 /*include_std=*/false,
5685 tf_warning_or_error);
5687 else
5688 postfix_expression
5689 = unqualified_fn_lookup_error (postfix_expression);
5691 /* We do not perform argument-dependent lookup if
5692 normal lookup finds a non-function, in accordance
5693 with the expected resolution of DR 218. */
5694 else if (!VEC_empty (tree, args)
5695 && is_overloaded_fn (postfix_expression))
5697 tree fn = get_first_fn (postfix_expression);
5698 fn = STRIP_TEMPLATE (fn);
5700 /* Do not do argument dependent lookup if regular
5701 lookup finds a member function or a block-scope
5702 function declaration. [basic.lookup.argdep]/3 */
5703 if (!DECL_FUNCTION_MEMBER_P (fn)
5704 && !DECL_LOCAL_FUNCTION_P (fn))
5706 koenig_p = true;
5707 if (!any_type_dependent_arguments_p (args))
5708 postfix_expression
5709 = perform_koenig_lookup (postfix_expression, args,
5710 /*include_std=*/false,
5711 tf_warning_or_error);
5716 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5718 tree instance = TREE_OPERAND (postfix_expression, 0);
5719 tree fn = TREE_OPERAND (postfix_expression, 1);
5721 if (processing_template_decl
5722 && (type_dependent_expression_p (instance)
5723 || (!BASELINK_P (fn)
5724 && TREE_CODE (fn) != FIELD_DECL)
5725 || type_dependent_expression_p (fn)
5726 || any_type_dependent_arguments_p (args)))
5728 postfix_expression
5729 = build_nt_call_vec (postfix_expression, args);
5730 release_tree_vector (args);
5731 break;
5734 if (BASELINK_P (fn))
5736 postfix_expression
5737 = (build_new_method_call
5738 (instance, fn, &args, NULL_TREE,
5739 (idk == CP_ID_KIND_QUALIFIED
5740 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5741 : LOOKUP_NORMAL),
5742 /*fn_p=*/NULL,
5743 tf_warning_or_error));
5745 else
5746 postfix_expression
5747 = finish_call_expr (postfix_expression, &args,
5748 /*disallow_virtual=*/false,
5749 /*koenig_p=*/false,
5750 tf_warning_or_error);
5752 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5753 || TREE_CODE (postfix_expression) == MEMBER_REF
5754 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5755 postfix_expression = (build_offset_ref_call_from_tree
5756 (postfix_expression, &args));
5757 else if (idk == CP_ID_KIND_QUALIFIED)
5758 /* A call to a static class member, or a namespace-scope
5759 function. */
5760 postfix_expression
5761 = finish_call_expr (postfix_expression, &args,
5762 /*disallow_virtual=*/true,
5763 koenig_p,
5764 tf_warning_or_error);
5765 else
5766 /* All other function calls. */
5767 postfix_expression
5768 = finish_call_expr (postfix_expression, &args,
5769 /*disallow_virtual=*/false,
5770 koenig_p,
5771 tf_warning_or_error);
5773 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5774 idk = CP_ID_KIND_NONE;
5776 release_tree_vector (args);
5778 break;
5780 case CPP_DOT:
5781 case CPP_DEREF:
5782 /* postfix-expression . template [opt] id-expression
5783 postfix-expression . pseudo-destructor-name
5784 postfix-expression -> template [opt] id-expression
5785 postfix-expression -> pseudo-destructor-name */
5787 /* Consume the `.' or `->' operator. */
5788 cp_lexer_consume_token (parser->lexer);
5790 postfix_expression
5791 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5792 postfix_expression,
5793 false, &idk,
5794 token->location);
5796 is_member_access = true;
5797 break;
5799 case CPP_PLUS_PLUS:
5800 /* postfix-expression ++ */
5801 /* Consume the `++' token. */
5802 cp_lexer_consume_token (parser->lexer);
5803 /* Generate a representation for the complete expression. */
5804 postfix_expression
5805 = finish_increment_expr (postfix_expression,
5806 POSTINCREMENT_EXPR);
5807 /* Increments may not appear in constant-expressions. */
5808 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5809 postfix_expression = error_mark_node;
5810 idk = CP_ID_KIND_NONE;
5811 is_member_access = false;
5812 break;
5814 case CPP_MINUS_MINUS:
5815 /* postfix-expression -- */
5816 /* Consume the `--' token. */
5817 cp_lexer_consume_token (parser->lexer);
5818 /* Generate a representation for the complete expression. */
5819 postfix_expression
5820 = finish_increment_expr (postfix_expression,
5821 POSTDECREMENT_EXPR);
5822 /* Decrements may not appear in constant-expressions. */
5823 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5824 postfix_expression = error_mark_node;
5825 idk = CP_ID_KIND_NONE;
5826 is_member_access = false;
5827 break;
5829 default:
5830 if (pidk_return != NULL)
5831 * pidk_return = idk;
5832 if (member_access_only_p)
5833 return is_member_access? postfix_expression : error_mark_node;
5834 else
5835 return postfix_expression;
5839 /* We should never get here. */
5840 gcc_unreachable ();
5841 return error_mark_node;
5844 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5845 by cp_parser_builtin_offsetof. We're looking for
5847 postfix-expression [ expression ]
5848 postfix-expression [ braced-init-list ] (C++11)
5850 FOR_OFFSETOF is set if we're being called in that context, which
5851 changes how we deal with integer constant expressions. */
5853 static tree
5854 cp_parser_postfix_open_square_expression (cp_parser *parser,
5855 tree postfix_expression,
5856 bool for_offsetof)
5858 tree index;
5859 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5861 /* Consume the `[' token. */
5862 cp_lexer_consume_token (parser->lexer);
5864 /* Parse the index expression. */
5865 /* ??? For offsetof, there is a question of what to allow here. If
5866 offsetof is not being used in an integral constant expression context,
5867 then we *could* get the right answer by computing the value at runtime.
5868 If we are in an integral constant expression context, then we might
5869 could accept any constant expression; hard to say without analysis.
5870 Rather than open the barn door too wide right away, allow only integer
5871 constant expressions here. */
5872 if (for_offsetof)
5873 index = cp_parser_constant_expression (parser, false, NULL);
5874 else
5876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5878 bool expr_nonconst_p;
5879 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5880 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5882 else
5883 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5886 /* Look for the closing `]'. */
5887 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5889 /* Build the ARRAY_REF. */
5890 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5892 /* When not doing offsetof, array references are not permitted in
5893 constant-expressions. */
5894 if (!for_offsetof
5895 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5896 postfix_expression = error_mark_node;
5898 return postfix_expression;
5901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5902 by cp_parser_builtin_offsetof. We're looking for
5904 postfix-expression . template [opt] id-expression
5905 postfix-expression . pseudo-destructor-name
5906 postfix-expression -> template [opt] id-expression
5907 postfix-expression -> pseudo-destructor-name
5909 FOR_OFFSETOF is set if we're being called in that context. That sorta
5910 limits what of the above we'll actually accept, but nevermind.
5911 TOKEN_TYPE is the "." or "->" token, which will already have been
5912 removed from the stream. */
5914 static tree
5915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5916 enum cpp_ttype token_type,
5917 tree postfix_expression,
5918 bool for_offsetof, cp_id_kind *idk,
5919 location_t location)
5921 tree name;
5922 bool dependent_p;
5923 bool pseudo_destructor_p;
5924 tree scope = NULL_TREE;
5926 /* If this is a `->' operator, dereference the pointer. */
5927 if (token_type == CPP_DEREF)
5928 postfix_expression = build_x_arrow (location, postfix_expression,
5929 tf_warning_or_error);
5930 /* Check to see whether or not the expression is type-dependent. */
5931 dependent_p = type_dependent_expression_p (postfix_expression);
5932 /* The identifier following the `->' or `.' is not qualified. */
5933 parser->scope = NULL_TREE;
5934 parser->qualifying_scope = NULL_TREE;
5935 parser->object_scope = NULL_TREE;
5936 *idk = CP_ID_KIND_NONE;
5938 /* Enter the scope corresponding to the type of the object
5939 given by the POSTFIX_EXPRESSION. */
5940 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5942 scope = TREE_TYPE (postfix_expression);
5943 /* According to the standard, no expression should ever have
5944 reference type. Unfortunately, we do not currently match
5945 the standard in this respect in that our internal representation
5946 of an expression may have reference type even when the standard
5947 says it does not. Therefore, we have to manually obtain the
5948 underlying type here. */
5949 scope = non_reference (scope);
5950 /* The type of the POSTFIX_EXPRESSION must be complete. */
5951 if (scope == unknown_type_node)
5953 error_at (location, "%qE does not have class type",
5954 postfix_expression);
5955 scope = NULL_TREE;
5957 /* Unlike the object expression in other contexts, *this is not
5958 required to be of complete type for purposes of class member
5959 access (5.2.5) outside the member function body. */
5960 else if (scope != current_class_ref
5961 && !(processing_template_decl && scope == current_class_type))
5962 scope = complete_type_or_else (scope, NULL_TREE);
5963 /* Let the name lookup machinery know that we are processing a
5964 class member access expression. */
5965 parser->context->object_type = scope;
5966 /* If something went wrong, we want to be able to discern that case,
5967 as opposed to the case where there was no SCOPE due to the type
5968 of expression being dependent. */
5969 if (!scope)
5970 scope = error_mark_node;
5971 /* If the SCOPE was erroneous, make the various semantic analysis
5972 functions exit quickly -- and without issuing additional error
5973 messages. */
5974 if (scope == error_mark_node)
5975 postfix_expression = error_mark_node;
5978 /* Assume this expression is not a pseudo-destructor access. */
5979 pseudo_destructor_p = false;
5981 /* If the SCOPE is a scalar type, then, if this is a valid program,
5982 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5983 is type dependent, it can be pseudo-destructor-name or something else.
5984 Try to parse it as pseudo-destructor-name first. */
5985 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5987 tree s;
5988 tree type;
5990 cp_parser_parse_tentatively (parser);
5991 /* Parse the pseudo-destructor-name. */
5992 s = NULL_TREE;
5993 cp_parser_pseudo_destructor_name (parser, &s, &type);
5994 if (dependent_p
5995 && (cp_parser_error_occurred (parser)
5996 || TREE_CODE (type) != TYPE_DECL
5997 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5998 cp_parser_abort_tentative_parse (parser);
5999 else if (cp_parser_parse_definitely (parser))
6001 pseudo_destructor_p = true;
6002 postfix_expression
6003 = finish_pseudo_destructor_expr (postfix_expression,
6004 s, TREE_TYPE (type));
6008 if (!pseudo_destructor_p)
6010 /* If the SCOPE is not a scalar type, we are looking at an
6011 ordinary class member access expression, rather than a
6012 pseudo-destructor-name. */
6013 bool template_p;
6014 cp_token *token = cp_lexer_peek_token (parser->lexer);
6015 /* Parse the id-expression. */
6016 name = (cp_parser_id_expression
6017 (parser,
6018 cp_parser_optional_template_keyword (parser),
6019 /*check_dependency_p=*/true,
6020 &template_p,
6021 /*declarator_p=*/false,
6022 /*optional_p=*/false));
6023 /* In general, build a SCOPE_REF if the member name is qualified.
6024 However, if the name was not dependent and has already been
6025 resolved; there is no need to build the SCOPE_REF. For example;
6027 struct X { void f(); };
6028 template <typename T> void f(T* t) { t->X::f(); }
6030 Even though "t" is dependent, "X::f" is not and has been resolved
6031 to a BASELINK; there is no need to include scope information. */
6033 /* But we do need to remember that there was an explicit scope for
6034 virtual function calls. */
6035 if (parser->scope)
6036 *idk = CP_ID_KIND_QUALIFIED;
6038 /* If the name is a template-id that names a type, we will get a
6039 TYPE_DECL here. That is invalid code. */
6040 if (TREE_CODE (name) == TYPE_DECL)
6042 error_at (token->location, "invalid use of %qD", name);
6043 postfix_expression = error_mark_node;
6045 else
6047 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6049 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6051 error_at (token->location, "%<%D::%D%> is not a class member",
6052 parser->scope, name);
6053 postfix_expression = error_mark_node;
6055 else
6056 name = build_qualified_name (/*type=*/NULL_TREE,
6057 parser->scope,
6058 name,
6059 template_p);
6060 parser->scope = NULL_TREE;
6061 parser->qualifying_scope = NULL_TREE;
6062 parser->object_scope = NULL_TREE;
6064 if (parser->scope && name && BASELINK_P (name))
6065 adjust_result_of_qualified_name_lookup
6066 (name, parser->scope, scope);
6067 postfix_expression
6068 = finish_class_member_access_expr (postfix_expression, name,
6069 template_p,
6070 tf_warning_or_error);
6074 /* We no longer need to look up names in the scope of the object on
6075 the left-hand side of the `.' or `->' operator. */
6076 parser->context->object_type = NULL_TREE;
6078 /* Outside of offsetof, these operators may not appear in
6079 constant-expressions. */
6080 if (!for_offsetof
6081 && (cp_parser_non_integral_constant_expression
6082 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6083 postfix_expression = error_mark_node;
6085 return postfix_expression;
6088 /* Parse a parenthesized expression-list.
6090 expression-list:
6091 assignment-expression
6092 expression-list, assignment-expression
6094 attribute-list:
6095 expression-list
6096 identifier
6097 identifier, expression-list
6099 CAST_P is true if this expression is the target of a cast.
6101 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6102 argument pack.
6104 Returns a vector of trees. Each element is a representation of an
6105 assignment-expression. NULL is returned if the ( and or ) are
6106 missing. An empty, but allocated, vector is returned on no
6107 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6108 if we are parsing an attribute list for an attribute that wants a
6109 plain identifier argument, normal_attr for an attribute that wants
6110 an expression, or non_attr if we aren't parsing an attribute list. If
6111 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6112 not all of the expressions in the list were constant. */
6114 static VEC(tree,gc) *
6115 cp_parser_parenthesized_expression_list (cp_parser* parser,
6116 int is_attribute_list,
6117 bool cast_p,
6118 bool allow_expansion_p,
6119 bool *non_constant_p)
6121 VEC(tree,gc) *expression_list;
6122 bool fold_expr_p = is_attribute_list != non_attr;
6123 tree identifier = NULL_TREE;
6124 bool saved_greater_than_is_operator_p;
6126 /* Assume all the expressions will be constant. */
6127 if (non_constant_p)
6128 *non_constant_p = false;
6130 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6131 return NULL;
6133 expression_list = make_tree_vector ();
6135 /* Within a parenthesized expression, a `>' token is always
6136 the greater-than operator. */
6137 saved_greater_than_is_operator_p
6138 = parser->greater_than_is_operator_p;
6139 parser->greater_than_is_operator_p = true;
6141 /* Consume expressions until there are no more. */
6142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6143 while (true)
6145 tree expr;
6147 /* At the beginning of attribute lists, check to see if the
6148 next token is an identifier. */
6149 if (is_attribute_list == id_attr
6150 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6152 cp_token *token;
6154 /* Consume the identifier. */
6155 token = cp_lexer_consume_token (parser->lexer);
6156 /* Save the identifier. */
6157 identifier = token->u.value;
6159 else
6161 bool expr_non_constant_p;
6163 /* Parse the next assignment-expression. */
6164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6166 /* A braced-init-list. */
6167 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6168 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6169 if (non_constant_p && expr_non_constant_p)
6170 *non_constant_p = true;
6172 else if (non_constant_p)
6174 expr = (cp_parser_constant_expression
6175 (parser, /*allow_non_constant_p=*/true,
6176 &expr_non_constant_p));
6177 if (expr_non_constant_p)
6178 *non_constant_p = true;
6180 else
6181 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6183 if (fold_expr_p)
6184 expr = fold_non_dependent_expr (expr);
6186 /* If we have an ellipsis, then this is an expression
6187 expansion. */
6188 if (allow_expansion_p
6189 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6191 /* Consume the `...'. */
6192 cp_lexer_consume_token (parser->lexer);
6194 /* Build the argument pack. */
6195 expr = make_pack_expansion (expr);
6198 /* Add it to the list. We add error_mark_node
6199 expressions to the list, so that we can still tell if
6200 the correct form for a parenthesized expression-list
6201 is found. That gives better errors. */
6202 VEC_safe_push (tree, gc, expression_list, expr);
6204 if (expr == error_mark_node)
6205 goto skip_comma;
6208 /* After the first item, attribute lists look the same as
6209 expression lists. */
6210 is_attribute_list = non_attr;
6212 get_comma:;
6213 /* If the next token isn't a `,', then we are done. */
6214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6215 break;
6217 /* Otherwise, consume the `,' and keep going. */
6218 cp_lexer_consume_token (parser->lexer);
6221 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6223 int ending;
6225 skip_comma:;
6226 /* We try and resync to an unnested comma, as that will give the
6227 user better diagnostics. */
6228 ending = cp_parser_skip_to_closing_parenthesis (parser,
6229 /*recovering=*/true,
6230 /*or_comma=*/true,
6231 /*consume_paren=*/true);
6232 if (ending < 0)
6233 goto get_comma;
6234 if (!ending)
6236 parser->greater_than_is_operator_p
6237 = saved_greater_than_is_operator_p;
6238 return NULL;
6242 parser->greater_than_is_operator_p
6243 = saved_greater_than_is_operator_p;
6245 if (identifier)
6246 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6248 return expression_list;
6251 /* Parse a pseudo-destructor-name.
6253 pseudo-destructor-name:
6254 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6255 :: [opt] nested-name-specifier template template-id :: ~ type-name
6256 :: [opt] nested-name-specifier [opt] ~ type-name
6258 If either of the first two productions is used, sets *SCOPE to the
6259 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6260 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6261 or ERROR_MARK_NODE if the parse fails. */
6263 static void
6264 cp_parser_pseudo_destructor_name (cp_parser* parser,
6265 tree* scope,
6266 tree* type)
6268 bool nested_name_specifier_p;
6270 /* Assume that things will not work out. */
6271 *type = error_mark_node;
6273 /* Look for the optional `::' operator. */
6274 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6275 /* Look for the optional nested-name-specifier. */
6276 nested_name_specifier_p
6277 = (cp_parser_nested_name_specifier_opt (parser,
6278 /*typename_keyword_p=*/false,
6279 /*check_dependency_p=*/true,
6280 /*type_p=*/false,
6281 /*is_declaration=*/false)
6282 != NULL_TREE);
6283 /* Now, if we saw a nested-name-specifier, we might be doing the
6284 second production. */
6285 if (nested_name_specifier_p
6286 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6288 /* Consume the `template' keyword. */
6289 cp_lexer_consume_token (parser->lexer);
6290 /* Parse the template-id. */
6291 cp_parser_template_id (parser,
6292 /*template_keyword_p=*/true,
6293 /*check_dependency_p=*/false,
6294 class_type,
6295 /*is_declaration=*/true);
6296 /* Look for the `::' token. */
6297 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6299 /* If the next token is not a `~', then there might be some
6300 additional qualification. */
6301 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6303 /* At this point, we're looking for "type-name :: ~". The type-name
6304 must not be a class-name, since this is a pseudo-destructor. So,
6305 it must be either an enum-name, or a typedef-name -- both of which
6306 are just identifiers. So, we peek ahead to check that the "::"
6307 and "~" tokens are present; if they are not, then we can avoid
6308 calling type_name. */
6309 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6310 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6311 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6313 cp_parser_error (parser, "non-scalar type");
6314 return;
6317 /* Look for the type-name. */
6318 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6319 if (*scope == error_mark_node)
6320 return;
6322 /* Look for the `::' token. */
6323 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6325 else
6326 *scope = NULL_TREE;
6328 /* Look for the `~'. */
6329 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6331 /* Once we see the ~, this has to be a pseudo-destructor. */
6332 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6333 cp_parser_commit_to_tentative_parse (parser);
6335 /* Look for the type-name again. We are not responsible for
6336 checking that it matches the first type-name. */
6337 *type = cp_parser_nonclass_name (parser);
6340 /* Parse a unary-expression.
6342 unary-expression:
6343 postfix-expression
6344 ++ cast-expression
6345 -- cast-expression
6346 unary-operator cast-expression
6347 sizeof unary-expression
6348 sizeof ( type-id )
6349 alignof ( type-id ) [C++0x]
6350 new-expression
6351 delete-expression
6353 GNU Extensions:
6355 unary-expression:
6356 __extension__ cast-expression
6357 __alignof__ unary-expression
6358 __alignof__ ( type-id )
6359 alignof unary-expression [C++0x]
6360 __real__ cast-expression
6361 __imag__ cast-expression
6362 && identifier
6364 ADDRESS_P is true iff the unary-expression is appearing as the
6365 operand of the `&' operator. CAST_P is true if this expression is
6366 the target of a cast.
6368 Returns a representation of the expression. */
6370 static tree
6371 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6372 cp_id_kind * pidk)
6374 cp_token *token;
6375 enum tree_code unary_operator;
6377 /* Peek at the next token. */
6378 token = cp_lexer_peek_token (parser->lexer);
6379 /* Some keywords give away the kind of expression. */
6380 if (token->type == CPP_KEYWORD)
6382 enum rid keyword = token->keyword;
6384 switch (keyword)
6386 case RID_ALIGNOF:
6387 case RID_SIZEOF:
6389 tree operand;
6390 enum tree_code op;
6392 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6393 /* Consume the token. */
6394 cp_lexer_consume_token (parser->lexer);
6395 /* Parse the operand. */
6396 operand = cp_parser_sizeof_operand (parser, keyword);
6398 if (TYPE_P (operand))
6399 return cxx_sizeof_or_alignof_type (operand, op, true);
6400 else
6402 /* ISO C++ defines alignof only with types, not with
6403 expressions. So pedwarn if alignof is used with a non-
6404 type expression. However, __alignof__ is ok. */
6405 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6406 pedwarn (token->location, OPT_Wpedantic,
6407 "ISO C++ does not allow %<alignof%> "
6408 "with a non-type");
6410 return cxx_sizeof_or_alignof_expr (operand, op, true);
6414 case RID_NEW:
6415 return cp_parser_new_expression (parser);
6417 case RID_DELETE:
6418 return cp_parser_delete_expression (parser);
6420 case RID_EXTENSION:
6422 /* The saved value of the PEDANTIC flag. */
6423 int saved_pedantic;
6424 tree expr;
6426 /* Save away the PEDANTIC flag. */
6427 cp_parser_extension_opt (parser, &saved_pedantic);
6428 /* Parse the cast-expression. */
6429 expr = cp_parser_simple_cast_expression (parser);
6430 /* Restore the PEDANTIC flag. */
6431 pedantic = saved_pedantic;
6433 return expr;
6436 case RID_REALPART:
6437 case RID_IMAGPART:
6439 tree expression;
6441 /* Consume the `__real__' or `__imag__' token. */
6442 cp_lexer_consume_token (parser->lexer);
6443 /* Parse the cast-expression. */
6444 expression = cp_parser_simple_cast_expression (parser);
6445 /* Create the complete representation. */
6446 return build_x_unary_op (token->location,
6447 (keyword == RID_REALPART
6448 ? REALPART_EXPR : IMAGPART_EXPR),
6449 expression,
6450 tf_warning_or_error);
6452 break;
6454 case RID_TRANSACTION_ATOMIC:
6455 case RID_TRANSACTION_RELAXED:
6456 return cp_parser_transaction_expression (parser, keyword);
6458 case RID_NOEXCEPT:
6460 tree expr;
6461 const char *saved_message;
6462 bool saved_integral_constant_expression_p;
6463 bool saved_non_integral_constant_expression_p;
6464 bool saved_greater_than_is_operator_p;
6466 cp_lexer_consume_token (parser->lexer);
6467 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6469 saved_message = parser->type_definition_forbidden_message;
6470 parser->type_definition_forbidden_message
6471 = G_("types may not be defined in %<noexcept%> expressions");
6473 saved_integral_constant_expression_p
6474 = parser->integral_constant_expression_p;
6475 saved_non_integral_constant_expression_p
6476 = parser->non_integral_constant_expression_p;
6477 parser->integral_constant_expression_p = false;
6479 saved_greater_than_is_operator_p
6480 = parser->greater_than_is_operator_p;
6481 parser->greater_than_is_operator_p = true;
6483 ++cp_unevaluated_operand;
6484 ++c_inhibit_evaluation_warnings;
6485 expr = cp_parser_expression (parser, false, NULL);
6486 --c_inhibit_evaluation_warnings;
6487 --cp_unevaluated_operand;
6489 parser->greater_than_is_operator_p
6490 = saved_greater_than_is_operator_p;
6492 parser->integral_constant_expression_p
6493 = saved_integral_constant_expression_p;
6494 parser->non_integral_constant_expression_p
6495 = saved_non_integral_constant_expression_p;
6497 parser->type_definition_forbidden_message = saved_message;
6499 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6500 return finish_noexcept_expr (expr, tf_warning_or_error);
6503 default:
6504 break;
6508 /* Look for the `:: new' and `:: delete', which also signal the
6509 beginning of a new-expression, or delete-expression,
6510 respectively. If the next token is `::', then it might be one of
6511 these. */
6512 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6514 enum rid keyword;
6516 /* See if the token after the `::' is one of the keywords in
6517 which we're interested. */
6518 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6519 /* If it's `new', we have a new-expression. */
6520 if (keyword == RID_NEW)
6521 return cp_parser_new_expression (parser);
6522 /* Similarly, for `delete'. */
6523 else if (keyword == RID_DELETE)
6524 return cp_parser_delete_expression (parser);
6527 /* Look for a unary operator. */
6528 unary_operator = cp_parser_unary_operator (token);
6529 /* The `++' and `--' operators can be handled similarly, even though
6530 they are not technically unary-operators in the grammar. */
6531 if (unary_operator == ERROR_MARK)
6533 if (token->type == CPP_PLUS_PLUS)
6534 unary_operator = PREINCREMENT_EXPR;
6535 else if (token->type == CPP_MINUS_MINUS)
6536 unary_operator = PREDECREMENT_EXPR;
6537 /* Handle the GNU address-of-label extension. */
6538 else if (cp_parser_allow_gnu_extensions_p (parser)
6539 && token->type == CPP_AND_AND)
6541 tree identifier;
6542 tree expression;
6543 location_t loc = token->location;
6545 /* Consume the '&&' token. */
6546 cp_lexer_consume_token (parser->lexer);
6547 /* Look for the identifier. */
6548 identifier = cp_parser_identifier (parser);
6549 /* Create an expression representing the address. */
6550 expression = finish_label_address_expr (identifier, loc);
6551 if (cp_parser_non_integral_constant_expression (parser,
6552 NIC_ADDR_LABEL))
6553 expression = error_mark_node;
6554 return expression;
6557 if (unary_operator != ERROR_MARK)
6559 tree cast_expression;
6560 tree expression = error_mark_node;
6561 non_integral_constant non_constant_p = NIC_NONE;
6562 location_t loc = token->location;
6564 /* Consume the operator token. */
6565 token = cp_lexer_consume_token (parser->lexer);
6566 /* Parse the cast-expression. */
6567 cast_expression
6568 = cp_parser_cast_expression (parser,
6569 unary_operator == ADDR_EXPR,
6570 /*cast_p=*/false, pidk);
6571 /* Now, build an appropriate representation. */
6572 switch (unary_operator)
6574 case INDIRECT_REF:
6575 non_constant_p = NIC_STAR;
6576 expression = build_x_indirect_ref (loc, cast_expression,
6577 RO_UNARY_STAR,
6578 tf_warning_or_error);
6579 break;
6581 case ADDR_EXPR:
6582 non_constant_p = NIC_ADDR;
6583 /* Fall through. */
6584 case BIT_NOT_EXPR:
6585 expression = build_x_unary_op (loc, unary_operator,
6586 cast_expression,
6587 tf_warning_or_error);
6588 break;
6590 case PREINCREMENT_EXPR:
6591 case PREDECREMENT_EXPR:
6592 non_constant_p = unary_operator == PREINCREMENT_EXPR
6593 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6594 /* Fall through. */
6595 case UNARY_PLUS_EXPR:
6596 case NEGATE_EXPR:
6597 case TRUTH_NOT_EXPR:
6598 expression = finish_unary_op_expr (loc, unary_operator,
6599 cast_expression);
6600 break;
6602 default:
6603 gcc_unreachable ();
6606 if (non_constant_p != NIC_NONE
6607 && cp_parser_non_integral_constant_expression (parser,
6608 non_constant_p))
6609 expression = error_mark_node;
6611 return expression;
6614 return cp_parser_postfix_expression (parser, address_p, cast_p,
6615 /*member_access_only_p=*/false,
6616 pidk);
6619 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6620 unary-operator, the corresponding tree code is returned. */
6622 static enum tree_code
6623 cp_parser_unary_operator (cp_token* token)
6625 switch (token->type)
6627 case CPP_MULT:
6628 return INDIRECT_REF;
6630 case CPP_AND:
6631 return ADDR_EXPR;
6633 case CPP_PLUS:
6634 return UNARY_PLUS_EXPR;
6636 case CPP_MINUS:
6637 return NEGATE_EXPR;
6639 case CPP_NOT:
6640 return TRUTH_NOT_EXPR;
6642 case CPP_COMPL:
6643 return BIT_NOT_EXPR;
6645 default:
6646 return ERROR_MARK;
6650 /* Parse a new-expression.
6652 new-expression:
6653 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6654 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6656 Returns a representation of the expression. */
6658 static tree
6659 cp_parser_new_expression (cp_parser* parser)
6661 bool global_scope_p;
6662 VEC(tree,gc) *placement;
6663 tree type;
6664 VEC(tree,gc) *initializer;
6665 tree nelts = NULL_TREE;
6666 tree ret;
6668 /* Look for the optional `::' operator. */
6669 global_scope_p
6670 = (cp_parser_global_scope_opt (parser,
6671 /*current_scope_valid_p=*/false)
6672 != NULL_TREE);
6673 /* Look for the `new' operator. */
6674 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6675 /* There's no easy way to tell a new-placement from the
6676 `( type-id )' construct. */
6677 cp_parser_parse_tentatively (parser);
6678 /* Look for a new-placement. */
6679 placement = cp_parser_new_placement (parser);
6680 /* If that didn't work out, there's no new-placement. */
6681 if (!cp_parser_parse_definitely (parser))
6683 if (placement != NULL)
6684 release_tree_vector (placement);
6685 placement = NULL;
6688 /* If the next token is a `(', then we have a parenthesized
6689 type-id. */
6690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6692 cp_token *token;
6693 const char *saved_message = parser->type_definition_forbidden_message;
6695 /* Consume the `('. */
6696 cp_lexer_consume_token (parser->lexer);
6698 /* Parse the type-id. */
6699 parser->type_definition_forbidden_message
6700 = G_("types may not be defined in a new-expression");
6701 type = cp_parser_type_id (parser);
6702 parser->type_definition_forbidden_message = saved_message;
6704 /* Look for the closing `)'. */
6705 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6706 token = cp_lexer_peek_token (parser->lexer);
6707 /* There should not be a direct-new-declarator in this production,
6708 but GCC used to allowed this, so we check and emit a sensible error
6709 message for this case. */
6710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6712 error_at (token->location,
6713 "array bound forbidden after parenthesized type-id");
6714 inform (token->location,
6715 "try removing the parentheses around the type-id");
6716 cp_parser_direct_new_declarator (parser);
6719 /* Otherwise, there must be a new-type-id. */
6720 else
6721 type = cp_parser_new_type_id (parser, &nelts);
6723 /* If the next token is a `(' or '{', then we have a new-initializer. */
6724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6725 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6726 initializer = cp_parser_new_initializer (parser);
6727 else
6728 initializer = NULL;
6730 /* A new-expression may not appear in an integral constant
6731 expression. */
6732 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6733 ret = error_mark_node;
6734 else
6736 /* Create a representation of the new-expression. */
6737 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6738 tf_warning_or_error);
6741 if (placement != NULL)
6742 release_tree_vector (placement);
6743 if (initializer != NULL)
6744 release_tree_vector (initializer);
6746 return ret;
6749 /* Parse a new-placement.
6751 new-placement:
6752 ( expression-list )
6754 Returns the same representation as for an expression-list. */
6756 static VEC(tree,gc) *
6757 cp_parser_new_placement (cp_parser* parser)
6759 VEC(tree,gc) *expression_list;
6761 /* Parse the expression-list. */
6762 expression_list = (cp_parser_parenthesized_expression_list
6763 (parser, non_attr, /*cast_p=*/false,
6764 /*allow_expansion_p=*/true,
6765 /*non_constant_p=*/NULL));
6767 return expression_list;
6770 /* Parse a new-type-id.
6772 new-type-id:
6773 type-specifier-seq new-declarator [opt]
6775 Returns the TYPE allocated. If the new-type-id indicates an array
6776 type, *NELTS is set to the number of elements in the last array
6777 bound; the TYPE will not include the last array bound. */
6779 static tree
6780 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6782 cp_decl_specifier_seq type_specifier_seq;
6783 cp_declarator *new_declarator;
6784 cp_declarator *declarator;
6785 cp_declarator *outer_declarator;
6786 const char *saved_message;
6788 /* The type-specifier sequence must not contain type definitions.
6789 (It cannot contain declarations of new types either, but if they
6790 are not definitions we will catch that because they are not
6791 complete.) */
6792 saved_message = parser->type_definition_forbidden_message;
6793 parser->type_definition_forbidden_message
6794 = G_("types may not be defined in a new-type-id");
6795 /* Parse the type-specifier-seq. */
6796 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6797 /*is_trailing_return=*/false,
6798 &type_specifier_seq);
6799 /* Restore the old message. */
6800 parser->type_definition_forbidden_message = saved_message;
6802 if (type_specifier_seq.type == error_mark_node)
6803 return error_mark_node;
6805 /* Parse the new-declarator. */
6806 new_declarator = cp_parser_new_declarator_opt (parser);
6808 /* Determine the number of elements in the last array dimension, if
6809 any. */
6810 *nelts = NULL_TREE;
6811 /* Skip down to the last array dimension. */
6812 declarator = new_declarator;
6813 outer_declarator = NULL;
6814 while (declarator && (declarator->kind == cdk_pointer
6815 || declarator->kind == cdk_ptrmem))
6817 outer_declarator = declarator;
6818 declarator = declarator->declarator;
6820 while (declarator
6821 && declarator->kind == cdk_array
6822 && declarator->declarator
6823 && declarator->declarator->kind == cdk_array)
6825 outer_declarator = declarator;
6826 declarator = declarator->declarator;
6829 if (declarator && declarator->kind == cdk_array)
6831 *nelts = declarator->u.array.bounds;
6832 if (*nelts == error_mark_node)
6833 *nelts = integer_one_node;
6835 if (outer_declarator)
6836 outer_declarator->declarator = declarator->declarator;
6837 else
6838 new_declarator = NULL;
6841 return groktypename (&type_specifier_seq, new_declarator, false);
6844 /* Parse an (optional) new-declarator.
6846 new-declarator:
6847 ptr-operator new-declarator [opt]
6848 direct-new-declarator
6850 Returns the declarator. */
6852 static cp_declarator *
6853 cp_parser_new_declarator_opt (cp_parser* parser)
6855 enum tree_code code;
6856 tree type;
6857 cp_cv_quals cv_quals;
6859 /* We don't know if there's a ptr-operator next, or not. */
6860 cp_parser_parse_tentatively (parser);
6861 /* Look for a ptr-operator. */
6862 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6863 /* If that worked, look for more new-declarators. */
6864 if (cp_parser_parse_definitely (parser))
6866 cp_declarator *declarator;
6868 /* Parse another optional declarator. */
6869 declarator = cp_parser_new_declarator_opt (parser);
6871 return cp_parser_make_indirect_declarator
6872 (code, type, cv_quals, declarator);
6875 /* If the next token is a `[', there is a direct-new-declarator. */
6876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6877 return cp_parser_direct_new_declarator (parser);
6879 return NULL;
6882 /* Parse a direct-new-declarator.
6884 direct-new-declarator:
6885 [ expression ]
6886 direct-new-declarator [constant-expression]
6890 static cp_declarator *
6891 cp_parser_direct_new_declarator (cp_parser* parser)
6893 cp_declarator *declarator = NULL;
6895 while (true)
6897 tree expression;
6898 cp_token *token;
6900 /* Look for the opening `['. */
6901 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6903 token = cp_lexer_peek_token (parser->lexer);
6904 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6905 /* The standard requires that the expression have integral
6906 type. DR 74 adds enumeration types. We believe that the
6907 real intent is that these expressions be handled like the
6908 expression in a `switch' condition, which also allows
6909 classes with a single conversion to integral or
6910 enumeration type. */
6911 if (!processing_template_decl)
6913 expression
6914 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6915 expression,
6916 /*complain=*/true);
6917 if (!expression)
6919 error_at (token->location,
6920 "expression in new-declarator must have integral "
6921 "or enumeration type");
6922 expression = error_mark_node;
6926 /* Look for the closing `]'. */
6927 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6929 /* Add this bound to the declarator. */
6930 declarator = make_array_declarator (declarator, expression);
6932 /* If the next token is not a `[', then there are no more
6933 bounds. */
6934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6935 break;
6938 return declarator;
6941 /* Parse a new-initializer.
6943 new-initializer:
6944 ( expression-list [opt] )
6945 braced-init-list
6947 Returns a representation of the expression-list. */
6949 static VEC(tree,gc) *
6950 cp_parser_new_initializer (cp_parser* parser)
6952 VEC(tree,gc) *expression_list;
6954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6956 tree t;
6957 bool expr_non_constant_p;
6958 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6959 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6960 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6961 expression_list = make_tree_vector_single (t);
6963 else
6964 expression_list = (cp_parser_parenthesized_expression_list
6965 (parser, non_attr, /*cast_p=*/false,
6966 /*allow_expansion_p=*/true,
6967 /*non_constant_p=*/NULL));
6969 return expression_list;
6972 /* Parse a delete-expression.
6974 delete-expression:
6975 :: [opt] delete cast-expression
6976 :: [opt] delete [ ] cast-expression
6978 Returns a representation of the expression. */
6980 static tree
6981 cp_parser_delete_expression (cp_parser* parser)
6983 bool global_scope_p;
6984 bool array_p;
6985 tree expression;
6987 /* Look for the optional `::' operator. */
6988 global_scope_p
6989 = (cp_parser_global_scope_opt (parser,
6990 /*current_scope_valid_p=*/false)
6991 != NULL_TREE);
6992 /* Look for the `delete' keyword. */
6993 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6994 /* See if the array syntax is in use. */
6995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6997 /* Consume the `[' token. */
6998 cp_lexer_consume_token (parser->lexer);
6999 /* Look for the `]' token. */
7000 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7001 /* Remember that this is the `[]' construct. */
7002 array_p = true;
7004 else
7005 array_p = false;
7007 /* Parse the cast-expression. */
7008 expression = cp_parser_simple_cast_expression (parser);
7010 /* A delete-expression may not appear in an integral constant
7011 expression. */
7012 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7013 return error_mark_node;
7015 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7016 tf_warning_or_error);
7019 /* Returns true if TOKEN may start a cast-expression and false
7020 otherwise. */
7022 static bool
7023 cp_parser_token_starts_cast_expression (cp_token *token)
7025 switch (token->type)
7027 case CPP_COMMA:
7028 case CPP_SEMICOLON:
7029 case CPP_QUERY:
7030 case CPP_COLON:
7031 case CPP_CLOSE_SQUARE:
7032 case CPP_CLOSE_PAREN:
7033 case CPP_CLOSE_BRACE:
7034 case CPP_DOT:
7035 case CPP_DOT_STAR:
7036 case CPP_DEREF:
7037 case CPP_DEREF_STAR:
7038 case CPP_DIV:
7039 case CPP_MOD:
7040 case CPP_LSHIFT:
7041 case CPP_RSHIFT:
7042 case CPP_LESS:
7043 case CPP_GREATER:
7044 case CPP_LESS_EQ:
7045 case CPP_GREATER_EQ:
7046 case CPP_EQ_EQ:
7047 case CPP_NOT_EQ:
7048 case CPP_EQ:
7049 case CPP_MULT_EQ:
7050 case CPP_DIV_EQ:
7051 case CPP_MOD_EQ:
7052 case CPP_PLUS_EQ:
7053 case CPP_MINUS_EQ:
7054 case CPP_RSHIFT_EQ:
7055 case CPP_LSHIFT_EQ:
7056 case CPP_AND_EQ:
7057 case CPP_XOR_EQ:
7058 case CPP_OR_EQ:
7059 case CPP_XOR:
7060 case CPP_OR:
7061 case CPP_OR_OR:
7062 case CPP_EOF:
7063 return false;
7065 /* '[' may start a primary-expression in obj-c++. */
7066 case CPP_OPEN_SQUARE:
7067 return c_dialect_objc ();
7069 default:
7070 return true;
7074 /* Parse a cast-expression.
7076 cast-expression:
7077 unary-expression
7078 ( type-id ) cast-expression
7080 ADDRESS_P is true iff the unary-expression is appearing as the
7081 operand of the `&' operator. CAST_P is true if this expression is
7082 the target of a cast.
7084 Returns a representation of the expression. */
7086 static tree
7087 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7088 cp_id_kind * pidk)
7090 /* If it's a `(', then we might be looking at a cast. */
7091 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7093 tree type = NULL_TREE;
7094 tree expr = NULL_TREE;
7095 bool compound_literal_p;
7096 const char *saved_message;
7098 /* There's no way to know yet whether or not this is a cast.
7099 For example, `(int (3))' is a unary-expression, while `(int)
7100 3' is a cast. So, we resort to parsing tentatively. */
7101 cp_parser_parse_tentatively (parser);
7102 /* Types may not be defined in a cast. */
7103 saved_message = parser->type_definition_forbidden_message;
7104 parser->type_definition_forbidden_message
7105 = G_("types may not be defined in casts");
7106 /* Consume the `('. */
7107 cp_lexer_consume_token (parser->lexer);
7108 /* A very tricky bit is that `(struct S) { 3 }' is a
7109 compound-literal (which we permit in C++ as an extension).
7110 But, that construct is not a cast-expression -- it is a
7111 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7112 is legal; if the compound-literal were a cast-expression,
7113 you'd need an extra set of parentheses.) But, if we parse
7114 the type-id, and it happens to be a class-specifier, then we
7115 will commit to the parse at that point, because we cannot
7116 undo the action that is done when creating a new class. So,
7117 then we cannot back up and do a postfix-expression.
7119 Therefore, we scan ahead to the closing `)', and check to see
7120 if the token after the `)' is a `{'. If so, we are not
7121 looking at a cast-expression.
7123 Save tokens so that we can put them back. */
7124 cp_lexer_save_tokens (parser->lexer);
7125 /* Skip tokens until the next token is a closing parenthesis.
7126 If we find the closing `)', and the next token is a `{', then
7127 we are looking at a compound-literal. */
7128 compound_literal_p
7129 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7130 /*consume_paren=*/true)
7131 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7132 /* Roll back the tokens we skipped. */
7133 cp_lexer_rollback_tokens (parser->lexer);
7134 /* If we were looking at a compound-literal, simulate an error
7135 so that the call to cp_parser_parse_definitely below will
7136 fail. */
7137 if (compound_literal_p)
7138 cp_parser_simulate_error (parser);
7139 else
7141 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7142 parser->in_type_id_in_expr_p = true;
7143 /* Look for the type-id. */
7144 type = cp_parser_type_id (parser);
7145 /* Look for the closing `)'. */
7146 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7147 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7150 /* Restore the saved message. */
7151 parser->type_definition_forbidden_message = saved_message;
7153 /* At this point this can only be either a cast or a
7154 parenthesized ctor such as `(T ())' that looks like a cast to
7155 function returning T. */
7156 if (!cp_parser_error_occurred (parser)
7157 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7158 (parser->lexer)))
7160 cp_parser_parse_definitely (parser);
7161 expr = cp_parser_cast_expression (parser,
7162 /*address_p=*/false,
7163 /*cast_p=*/true, pidk);
7165 /* Warn about old-style casts, if so requested. */
7166 if (warn_old_style_cast
7167 && !in_system_header
7168 && !VOID_TYPE_P (type)
7169 && current_lang_name != lang_name_c)
7170 warning (OPT_Wold_style_cast, "use of old-style cast");
7172 /* Only type conversions to integral or enumeration types
7173 can be used in constant-expressions. */
7174 if (!cast_valid_in_integral_constant_expression_p (type)
7175 && cp_parser_non_integral_constant_expression (parser,
7176 NIC_CAST))
7177 return error_mark_node;
7179 /* Perform the cast. */
7180 expr = build_c_cast (input_location, type, expr);
7181 return expr;
7183 else
7184 cp_parser_abort_tentative_parse (parser);
7187 /* If we get here, then it's not a cast, so it must be a
7188 unary-expression. */
7189 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7192 /* Parse a binary expression of the general form:
7194 pm-expression:
7195 cast-expression
7196 pm-expression .* cast-expression
7197 pm-expression ->* cast-expression
7199 multiplicative-expression:
7200 pm-expression
7201 multiplicative-expression * pm-expression
7202 multiplicative-expression / pm-expression
7203 multiplicative-expression % pm-expression
7205 additive-expression:
7206 multiplicative-expression
7207 additive-expression + multiplicative-expression
7208 additive-expression - multiplicative-expression
7210 shift-expression:
7211 additive-expression
7212 shift-expression << additive-expression
7213 shift-expression >> additive-expression
7215 relational-expression:
7216 shift-expression
7217 relational-expression < shift-expression
7218 relational-expression > shift-expression
7219 relational-expression <= shift-expression
7220 relational-expression >= shift-expression
7222 GNU Extension:
7224 relational-expression:
7225 relational-expression <? shift-expression
7226 relational-expression >? shift-expression
7228 equality-expression:
7229 relational-expression
7230 equality-expression == relational-expression
7231 equality-expression != relational-expression
7233 and-expression:
7234 equality-expression
7235 and-expression & equality-expression
7237 exclusive-or-expression:
7238 and-expression
7239 exclusive-or-expression ^ and-expression
7241 inclusive-or-expression:
7242 exclusive-or-expression
7243 inclusive-or-expression | exclusive-or-expression
7245 logical-and-expression:
7246 inclusive-or-expression
7247 logical-and-expression && inclusive-or-expression
7249 logical-or-expression:
7250 logical-and-expression
7251 logical-or-expression || logical-and-expression
7253 All these are implemented with a single function like:
7255 binary-expression:
7256 simple-cast-expression
7257 binary-expression <token> binary-expression
7259 CAST_P is true if this expression is the target of a cast.
7261 The binops_by_token map is used to get the tree codes for each <token> type.
7262 binary-expressions are associated according to a precedence table. */
7264 #define TOKEN_PRECEDENCE(token) \
7265 (((token->type == CPP_GREATER \
7266 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7267 && !parser->greater_than_is_operator_p) \
7268 ? PREC_NOT_OPERATOR \
7269 : binops_by_token[token->type].prec)
7271 static tree
7272 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7273 bool no_toplevel_fold_p,
7274 enum cp_parser_prec prec,
7275 cp_id_kind * pidk)
7277 cp_parser_expression_stack stack;
7278 cp_parser_expression_stack_entry *sp = &stack[0];
7279 cp_parser_expression_stack_entry current;
7280 tree rhs;
7281 cp_token *token;
7282 enum tree_code rhs_type;
7283 enum cp_parser_prec new_prec, lookahead_prec;
7284 tree overload;
7286 /* Parse the first expression. */
7287 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7288 cast_p, pidk);
7289 current.lhs_type = ERROR_MARK;
7290 current.prec = prec;
7292 if (cp_parser_error_occurred (parser))
7293 return error_mark_node;
7295 for (;;)
7297 /* Get an operator token. */
7298 token = cp_lexer_peek_token (parser->lexer);
7300 if (warn_cxx0x_compat
7301 && token->type == CPP_RSHIFT
7302 && !parser->greater_than_is_operator_p)
7304 if (warning_at (token->location, OPT_Wc__0x_compat,
7305 "%<>>%> operator is treated"
7306 " as two right angle brackets in C++11"))
7307 inform (token->location,
7308 "suggest parentheses around %<>>%> expression");
7311 new_prec = TOKEN_PRECEDENCE (token);
7313 /* Popping an entry off the stack means we completed a subexpression:
7314 - either we found a token which is not an operator (`>' where it is not
7315 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7316 will happen repeatedly;
7317 - or, we found an operator which has lower priority. This is the case
7318 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7319 parsing `3 * 4'. */
7320 if (new_prec <= current.prec)
7322 if (sp == stack)
7323 break;
7324 else
7325 goto pop;
7328 get_rhs:
7329 current.tree_type = binops_by_token[token->type].tree_type;
7330 current.loc = token->location;
7332 /* We used the operator token. */
7333 cp_lexer_consume_token (parser->lexer);
7335 /* For "false && x" or "true || x", x will never be executed;
7336 disable warnings while evaluating it. */
7337 if (current.tree_type == TRUTH_ANDIF_EXPR)
7338 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7339 else if (current.tree_type == TRUTH_ORIF_EXPR)
7340 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7342 /* Extract another operand. It may be the RHS of this expression
7343 or the LHS of a new, higher priority expression. */
7344 rhs = cp_parser_simple_cast_expression (parser);
7345 rhs_type = ERROR_MARK;
7347 /* Get another operator token. Look up its precedence to avoid
7348 building a useless (immediately popped) stack entry for common
7349 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7350 token = cp_lexer_peek_token (parser->lexer);
7351 lookahead_prec = TOKEN_PRECEDENCE (token);
7352 if (lookahead_prec > new_prec)
7354 /* ... and prepare to parse the RHS of the new, higher priority
7355 expression. Since precedence levels on the stack are
7356 monotonically increasing, we do not have to care about
7357 stack overflows. */
7358 *sp = current;
7359 ++sp;
7360 current.lhs = rhs;
7361 current.lhs_type = rhs_type;
7362 current.prec = new_prec;
7363 new_prec = lookahead_prec;
7364 goto get_rhs;
7366 pop:
7367 lookahead_prec = new_prec;
7368 /* If the stack is not empty, we have parsed into LHS the right side
7369 (`4' in the example above) of an expression we had suspended.
7370 We can use the information on the stack to recover the LHS (`3')
7371 from the stack together with the tree code (`MULT_EXPR'), and
7372 the precedence of the higher level subexpression
7373 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7374 which will be used to actually build the additive expression. */
7375 rhs = current.lhs;
7376 rhs_type = current.lhs_type;
7377 --sp;
7378 current = *sp;
7381 /* Undo the disabling of warnings done above. */
7382 if (current.tree_type == TRUTH_ANDIF_EXPR)
7383 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7384 else if (current.tree_type == TRUTH_ORIF_EXPR)
7385 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7387 overload = NULL;
7388 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7389 ERROR_MARK for everything that is not a binary expression.
7390 This makes warn_about_parentheses miss some warnings that
7391 involve unary operators. For unary expressions we should
7392 pass the correct tree_code unless the unary expression was
7393 surrounded by parentheses.
7395 if (no_toplevel_fold_p
7396 && lookahead_prec <= current.prec
7397 && sp == stack
7398 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7399 current.lhs = build2 (current.tree_type, boolean_type_node,
7400 current.lhs, rhs);
7401 else
7402 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7403 current.lhs, current.lhs_type,
7404 rhs, rhs_type, &overload,
7405 tf_warning_or_error);
7406 current.lhs_type = current.tree_type;
7408 /* If the binary operator required the use of an overloaded operator,
7409 then this expression cannot be an integral constant-expression.
7410 An overloaded operator can be used even if both operands are
7411 otherwise permissible in an integral constant-expression if at
7412 least one of the operands is of enumeration type. */
7414 if (overload
7415 && cp_parser_non_integral_constant_expression (parser,
7416 NIC_OVERLOADED))
7417 return error_mark_node;
7420 return current.lhs;
7424 /* Parse the `? expression : assignment-expression' part of a
7425 conditional-expression. The LOGICAL_OR_EXPR is the
7426 logical-or-expression that started the conditional-expression.
7427 Returns a representation of the entire conditional-expression.
7429 This routine is used by cp_parser_assignment_expression.
7431 ? expression : assignment-expression
7433 GNU Extensions:
7435 ? : assignment-expression */
7437 static tree
7438 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7440 tree expr;
7441 tree assignment_expr;
7442 struct cp_token *token;
7443 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7445 /* Consume the `?' token. */
7446 cp_lexer_consume_token (parser->lexer);
7447 token = cp_lexer_peek_token (parser->lexer);
7448 if (cp_parser_allow_gnu_extensions_p (parser)
7449 && token->type == CPP_COLON)
7451 pedwarn (token->location, OPT_Wpedantic,
7452 "ISO C++ does not allow ?: with omitted middle operand");
7453 /* Implicit true clause. */
7454 expr = NULL_TREE;
7455 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7456 warn_for_omitted_condop (token->location, logical_or_expr);
7458 else
7460 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7461 parser->colon_corrects_to_scope_p = false;
7462 /* Parse the expression. */
7463 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7464 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7465 c_inhibit_evaluation_warnings +=
7466 ((logical_or_expr == truthvalue_true_node)
7467 - (logical_or_expr == truthvalue_false_node));
7468 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7471 /* The next token should be a `:'. */
7472 cp_parser_require (parser, CPP_COLON, RT_COLON);
7473 /* Parse the assignment-expression. */
7474 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7475 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7477 /* Build the conditional-expression. */
7478 return build_x_conditional_expr (loc, logical_or_expr,
7479 expr,
7480 assignment_expr,
7481 tf_warning_or_error);
7484 /* Parse an assignment-expression.
7486 assignment-expression:
7487 conditional-expression
7488 logical-or-expression assignment-operator assignment_expression
7489 throw-expression
7491 CAST_P is true if this expression is the target of a cast.
7493 Returns a representation for the expression. */
7495 static tree
7496 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7497 cp_id_kind * pidk)
7499 tree expr;
7501 /* If the next token is the `throw' keyword, then we're looking at
7502 a throw-expression. */
7503 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7504 expr = cp_parser_throw_expression (parser);
7505 /* Otherwise, it must be that we are looking at a
7506 logical-or-expression. */
7507 else
7509 /* Parse the binary expressions (logical-or-expression). */
7510 expr = cp_parser_binary_expression (parser, cast_p, false,
7511 PREC_NOT_OPERATOR, pidk);
7512 /* If the next token is a `?' then we're actually looking at a
7513 conditional-expression. */
7514 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7515 return cp_parser_question_colon_clause (parser, expr);
7516 else
7518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7520 /* If it's an assignment-operator, we're using the second
7521 production. */
7522 enum tree_code assignment_operator
7523 = cp_parser_assignment_operator_opt (parser);
7524 if (assignment_operator != ERROR_MARK)
7526 bool non_constant_p;
7527 location_t saved_input_location;
7529 /* Parse the right-hand side of the assignment. */
7530 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7532 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7533 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7535 /* An assignment may not appear in a
7536 constant-expression. */
7537 if (cp_parser_non_integral_constant_expression (parser,
7538 NIC_ASSIGNMENT))
7539 return error_mark_node;
7540 /* Build the assignment expression. Its default
7541 location is the location of the '=' token. */
7542 saved_input_location = input_location;
7543 input_location = loc;
7544 expr = build_x_modify_expr (loc, expr,
7545 assignment_operator,
7546 rhs,
7547 tf_warning_or_error);
7548 input_location = saved_input_location;
7553 return expr;
7556 /* Parse an (optional) assignment-operator.
7558 assignment-operator: one of
7559 = *= /= %= += -= >>= <<= &= ^= |=
7561 GNU Extension:
7563 assignment-operator: one of
7564 <?= >?=
7566 If the next token is an assignment operator, the corresponding tree
7567 code is returned, and the token is consumed. For example, for
7568 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7569 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7570 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7571 operator, ERROR_MARK is returned. */
7573 static enum tree_code
7574 cp_parser_assignment_operator_opt (cp_parser* parser)
7576 enum tree_code op;
7577 cp_token *token;
7579 /* Peek at the next token. */
7580 token = cp_lexer_peek_token (parser->lexer);
7582 switch (token->type)
7584 case CPP_EQ:
7585 op = NOP_EXPR;
7586 break;
7588 case CPP_MULT_EQ:
7589 op = MULT_EXPR;
7590 break;
7592 case CPP_DIV_EQ:
7593 op = TRUNC_DIV_EXPR;
7594 break;
7596 case CPP_MOD_EQ:
7597 op = TRUNC_MOD_EXPR;
7598 break;
7600 case CPP_PLUS_EQ:
7601 op = PLUS_EXPR;
7602 break;
7604 case CPP_MINUS_EQ:
7605 op = MINUS_EXPR;
7606 break;
7608 case CPP_RSHIFT_EQ:
7609 op = RSHIFT_EXPR;
7610 break;
7612 case CPP_LSHIFT_EQ:
7613 op = LSHIFT_EXPR;
7614 break;
7616 case CPP_AND_EQ:
7617 op = BIT_AND_EXPR;
7618 break;
7620 case CPP_XOR_EQ:
7621 op = BIT_XOR_EXPR;
7622 break;
7624 case CPP_OR_EQ:
7625 op = BIT_IOR_EXPR;
7626 break;
7628 default:
7629 /* Nothing else is an assignment operator. */
7630 op = ERROR_MARK;
7633 /* If it was an assignment operator, consume it. */
7634 if (op != ERROR_MARK)
7635 cp_lexer_consume_token (parser->lexer);
7637 return op;
7640 /* Parse an expression.
7642 expression:
7643 assignment-expression
7644 expression , assignment-expression
7646 CAST_P is true if this expression is the target of a cast.
7648 Returns a representation of the expression. */
7650 static tree
7651 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7653 tree expression = NULL_TREE;
7654 location_t loc = UNKNOWN_LOCATION;
7656 while (true)
7658 tree assignment_expression;
7660 /* Parse the next assignment-expression. */
7661 assignment_expression
7662 = cp_parser_assignment_expression (parser, cast_p, pidk);
7663 /* If this is the first assignment-expression, we can just
7664 save it away. */
7665 if (!expression)
7666 expression = assignment_expression;
7667 else
7668 expression = build_x_compound_expr (loc, expression,
7669 assignment_expression,
7670 tf_warning_or_error);
7671 /* If the next token is not a comma, then we are done with the
7672 expression. */
7673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7674 break;
7675 /* Consume the `,'. */
7676 loc = cp_lexer_peek_token (parser->lexer)->location;
7677 cp_lexer_consume_token (parser->lexer);
7678 /* A comma operator cannot appear in a constant-expression. */
7679 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7680 expression = error_mark_node;
7683 return expression;
7686 /* Parse a constant-expression.
7688 constant-expression:
7689 conditional-expression
7691 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7692 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7693 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7694 is false, NON_CONSTANT_P should be NULL. */
7696 static tree
7697 cp_parser_constant_expression (cp_parser* parser,
7698 bool allow_non_constant_p,
7699 bool *non_constant_p)
7701 bool saved_integral_constant_expression_p;
7702 bool saved_allow_non_integral_constant_expression_p;
7703 bool saved_non_integral_constant_expression_p;
7704 tree expression;
7706 /* It might seem that we could simply parse the
7707 conditional-expression, and then check to see if it were
7708 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7709 one that the compiler can figure out is constant, possibly after
7710 doing some simplifications or optimizations. The standard has a
7711 precise definition of constant-expression, and we must honor
7712 that, even though it is somewhat more restrictive.
7714 For example:
7716 int i[(2, 3)];
7718 is not a legal declaration, because `(2, 3)' is not a
7719 constant-expression. The `,' operator is forbidden in a
7720 constant-expression. However, GCC's constant-folding machinery
7721 will fold this operation to an INTEGER_CST for `3'. */
7723 /* Save the old settings. */
7724 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7725 saved_allow_non_integral_constant_expression_p
7726 = parser->allow_non_integral_constant_expression_p;
7727 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7728 /* We are now parsing a constant-expression. */
7729 parser->integral_constant_expression_p = true;
7730 parser->allow_non_integral_constant_expression_p
7731 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7732 parser->non_integral_constant_expression_p = false;
7733 /* Although the grammar says "conditional-expression", we parse an
7734 "assignment-expression", which also permits "throw-expression"
7735 and the use of assignment operators. In the case that
7736 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7737 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7738 actually essential that we look for an assignment-expression.
7739 For example, cp_parser_initializer_clauses uses this function to
7740 determine whether a particular assignment-expression is in fact
7741 constant. */
7742 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7743 /* Restore the old settings. */
7744 parser->integral_constant_expression_p
7745 = saved_integral_constant_expression_p;
7746 parser->allow_non_integral_constant_expression_p
7747 = saved_allow_non_integral_constant_expression_p;
7748 if (cxx_dialect >= cxx0x)
7750 /* Require an rvalue constant expression here; that's what our
7751 callers expect. Reference constant expressions are handled
7752 separately in e.g. cp_parser_template_argument. */
7753 bool is_const = potential_rvalue_constant_expression (expression);
7754 parser->non_integral_constant_expression_p = !is_const;
7755 if (!is_const && !allow_non_constant_p)
7756 require_potential_rvalue_constant_expression (expression);
7758 if (allow_non_constant_p)
7759 *non_constant_p = parser->non_integral_constant_expression_p;
7760 parser->non_integral_constant_expression_p
7761 = saved_non_integral_constant_expression_p;
7763 return expression;
7766 /* Parse __builtin_offsetof.
7768 offsetof-expression:
7769 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7771 offsetof-member-designator:
7772 id-expression
7773 | offsetof-member-designator "." id-expression
7774 | offsetof-member-designator "[" expression "]"
7775 | offsetof-member-designator "->" id-expression */
7777 static tree
7778 cp_parser_builtin_offsetof (cp_parser *parser)
7780 int save_ice_p, save_non_ice_p;
7781 tree type, expr;
7782 cp_id_kind dummy;
7783 cp_token *token;
7785 /* We're about to accept non-integral-constant things, but will
7786 definitely yield an integral constant expression. Save and
7787 restore these values around our local parsing. */
7788 save_ice_p = parser->integral_constant_expression_p;
7789 save_non_ice_p = parser->non_integral_constant_expression_p;
7791 /* Consume the "__builtin_offsetof" token. */
7792 cp_lexer_consume_token (parser->lexer);
7793 /* Consume the opening `('. */
7794 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7795 /* Parse the type-id. */
7796 type = cp_parser_type_id (parser);
7797 /* Look for the `,'. */
7798 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7799 token = cp_lexer_peek_token (parser->lexer);
7801 /* Build the (type *)null that begins the traditional offsetof macro. */
7802 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7803 tf_warning_or_error);
7805 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7806 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7807 true, &dummy, token->location);
7808 while (true)
7810 token = cp_lexer_peek_token (parser->lexer);
7811 switch (token->type)
7813 case CPP_OPEN_SQUARE:
7814 /* offsetof-member-designator "[" expression "]" */
7815 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7816 break;
7818 case CPP_DEREF:
7819 /* offsetof-member-designator "->" identifier */
7820 expr = grok_array_decl (token->location, expr, integer_zero_node);
7821 /* FALLTHRU */
7823 case CPP_DOT:
7824 /* offsetof-member-designator "." identifier */
7825 cp_lexer_consume_token (parser->lexer);
7826 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7827 expr, true, &dummy,
7828 token->location);
7829 break;
7831 case CPP_CLOSE_PAREN:
7832 /* Consume the ")" token. */
7833 cp_lexer_consume_token (parser->lexer);
7834 goto success;
7836 default:
7837 /* Error. We know the following require will fail, but
7838 that gives the proper error message. */
7839 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7840 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7841 expr = error_mark_node;
7842 goto failure;
7846 success:
7847 /* If we're processing a template, we can't finish the semantics yet.
7848 Otherwise we can fold the entire expression now. */
7849 if (processing_template_decl)
7850 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7851 else
7852 expr = finish_offsetof (expr);
7854 failure:
7855 parser->integral_constant_expression_p = save_ice_p;
7856 parser->non_integral_constant_expression_p = save_non_ice_p;
7858 return expr;
7861 /* Parse a trait expression.
7863 Returns a representation of the expression, the underlying type
7864 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7866 static tree
7867 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7869 cp_trait_kind kind;
7870 tree type1, type2 = NULL_TREE;
7871 bool binary = false;
7872 cp_decl_specifier_seq decl_specs;
7874 switch (keyword)
7876 case RID_HAS_NOTHROW_ASSIGN:
7877 kind = CPTK_HAS_NOTHROW_ASSIGN;
7878 break;
7879 case RID_HAS_NOTHROW_CONSTRUCTOR:
7880 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7881 break;
7882 case RID_HAS_NOTHROW_COPY:
7883 kind = CPTK_HAS_NOTHROW_COPY;
7884 break;
7885 case RID_HAS_TRIVIAL_ASSIGN:
7886 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7887 break;
7888 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7889 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7890 break;
7891 case RID_HAS_TRIVIAL_COPY:
7892 kind = CPTK_HAS_TRIVIAL_COPY;
7893 break;
7894 case RID_HAS_TRIVIAL_DESTRUCTOR:
7895 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7896 break;
7897 case RID_HAS_VIRTUAL_DESTRUCTOR:
7898 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7899 break;
7900 case RID_IS_ABSTRACT:
7901 kind = CPTK_IS_ABSTRACT;
7902 break;
7903 case RID_IS_BASE_OF:
7904 kind = CPTK_IS_BASE_OF;
7905 binary = true;
7906 break;
7907 case RID_IS_CLASS:
7908 kind = CPTK_IS_CLASS;
7909 break;
7910 case RID_IS_CONVERTIBLE_TO:
7911 kind = CPTK_IS_CONVERTIBLE_TO;
7912 binary = true;
7913 break;
7914 case RID_IS_EMPTY:
7915 kind = CPTK_IS_EMPTY;
7916 break;
7917 case RID_IS_ENUM:
7918 kind = CPTK_IS_ENUM;
7919 break;
7920 case RID_IS_FINAL:
7921 kind = CPTK_IS_FINAL;
7922 break;
7923 case RID_IS_LITERAL_TYPE:
7924 kind = CPTK_IS_LITERAL_TYPE;
7925 break;
7926 case RID_IS_POD:
7927 kind = CPTK_IS_POD;
7928 break;
7929 case RID_IS_POLYMORPHIC:
7930 kind = CPTK_IS_POLYMORPHIC;
7931 break;
7932 case RID_IS_STD_LAYOUT:
7933 kind = CPTK_IS_STD_LAYOUT;
7934 break;
7935 case RID_IS_TRIVIAL:
7936 kind = CPTK_IS_TRIVIAL;
7937 break;
7938 case RID_IS_UNION:
7939 kind = CPTK_IS_UNION;
7940 break;
7941 case RID_UNDERLYING_TYPE:
7942 kind = CPTK_UNDERLYING_TYPE;
7943 break;
7944 case RID_BASES:
7945 kind = CPTK_BASES;
7946 break;
7947 case RID_DIRECT_BASES:
7948 kind = CPTK_DIRECT_BASES;
7949 break;
7950 default:
7951 gcc_unreachable ();
7954 /* Consume the token. */
7955 cp_lexer_consume_token (parser->lexer);
7957 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7959 type1 = cp_parser_type_id (parser);
7961 if (type1 == error_mark_node)
7962 return error_mark_node;
7964 /* Build a trivial decl-specifier-seq. */
7965 clear_decl_specs (&decl_specs);
7966 decl_specs.type = type1;
7968 /* Call grokdeclarator to figure out what type this is. */
7969 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7970 /*initialized=*/0, /*attrlist=*/NULL);
7972 if (binary)
7974 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7976 type2 = cp_parser_type_id (parser);
7978 if (type2 == error_mark_node)
7979 return error_mark_node;
7981 /* Build a trivial decl-specifier-seq. */
7982 clear_decl_specs (&decl_specs);
7983 decl_specs.type = type2;
7985 /* Call grokdeclarator to figure out what type this is. */
7986 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7987 /*initialized=*/0, /*attrlist=*/NULL);
7990 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7992 /* Complete the trait expression, which may mean either processing
7993 the trait expr now or saving it for template instantiation. */
7994 switch(kind)
7996 case CPTK_UNDERLYING_TYPE:
7997 return finish_underlying_type (type1);
7998 case CPTK_BASES:
7999 return finish_bases (type1, false);
8000 case CPTK_DIRECT_BASES:
8001 return finish_bases (type1, true);
8002 default:
8003 return finish_trait_expr (kind, type1, type2);
8007 /* Lambdas that appear in variable initializer or default argument scope
8008 get that in their mangling, so we need to record it. We might as well
8009 use the count for function and namespace scopes as well. */
8010 static GTY(()) tree lambda_scope;
8011 static GTY(()) int lambda_count;
8012 typedef struct GTY(()) tree_int
8014 tree t;
8015 int i;
8016 } tree_int;
8017 DEF_VEC_O(tree_int);
8018 DEF_VEC_ALLOC_O(tree_int,gc);
8019 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8021 static void
8022 start_lambda_scope (tree decl)
8024 tree_int ti;
8025 gcc_assert (decl);
8026 /* Once we're inside a function, we ignore other scopes and just push
8027 the function again so that popping works properly. */
8028 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8029 decl = current_function_decl;
8030 ti.t = lambda_scope;
8031 ti.i = lambda_count;
8032 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
8033 if (lambda_scope != decl)
8035 /* Don't reset the count if we're still in the same function. */
8036 lambda_scope = decl;
8037 lambda_count = 0;
8041 static void
8042 record_lambda_scope (tree lambda)
8044 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8045 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8048 static void
8049 finish_lambda_scope (void)
8051 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8052 if (lambda_scope != p->t)
8054 lambda_scope = p->t;
8055 lambda_count = p->i;
8057 VEC_pop (tree_int, lambda_scope_stack);
8060 /* Parse a lambda expression.
8062 lambda-expression:
8063 lambda-introducer lambda-declarator [opt] compound-statement
8065 Returns a representation of the expression. */
8067 static tree
8068 cp_parser_lambda_expression (cp_parser* parser)
8070 tree lambda_expr = build_lambda_expr ();
8071 tree type;
8072 bool ok;
8074 LAMBDA_EXPR_LOCATION (lambda_expr)
8075 = cp_lexer_peek_token (parser->lexer)->location;
8077 if (cp_unevaluated_operand)
8078 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8079 "lambda-expression in unevaluated context");
8081 /* We may be in the middle of deferred access check. Disable
8082 it now. */
8083 push_deferring_access_checks (dk_no_deferred);
8085 cp_parser_lambda_introducer (parser, lambda_expr);
8087 type = begin_lambda_type (lambda_expr);
8088 if (type == error_mark_node)
8089 return error_mark_node;
8091 record_lambda_scope (lambda_expr);
8093 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8094 determine_visibility (TYPE_NAME (type));
8096 /* Now that we've started the type, add the capture fields for any
8097 explicit captures. */
8098 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8101 /* Inside the class, surrounding template-parameter-lists do not apply. */
8102 unsigned int saved_num_template_parameter_lists
8103 = parser->num_template_parameter_lists;
8104 unsigned char in_statement = parser->in_statement;
8105 bool in_switch_statement_p = parser->in_switch_statement_p;
8107 parser->num_template_parameter_lists = 0;
8108 parser->in_statement = 0;
8109 parser->in_switch_statement_p = false;
8111 /* By virtue of defining a local class, a lambda expression has access to
8112 the private variables of enclosing classes. */
8114 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8116 if (ok)
8117 cp_parser_lambda_body (parser, lambda_expr);
8118 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8119 cp_parser_skip_to_end_of_block_or_statement (parser);
8121 /* The capture list was built up in reverse order; fix that now. */
8123 tree newlist = NULL_TREE;
8124 tree elt, next;
8126 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8127 elt; elt = next)
8129 next = TREE_CHAIN (elt);
8130 TREE_CHAIN (elt) = newlist;
8131 newlist = elt;
8133 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8136 if (ok)
8137 maybe_add_lambda_conv_op (type);
8139 type = finish_struct (type, /*attributes=*/NULL_TREE);
8141 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8142 parser->in_statement = in_statement;
8143 parser->in_switch_statement_p = in_switch_statement_p;
8146 pop_deferring_access_checks ();
8148 /* This field is only used during parsing of the lambda. */
8149 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8151 /* This lambda shouldn't have any proxies left at this point. */
8152 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8153 /* And now that we're done, push proxies for an enclosing lambda. */
8154 insert_pending_capture_proxies ();
8156 if (ok)
8157 return build_lambda_object (lambda_expr);
8158 else
8159 return error_mark_node;
8162 /* Parse the beginning of a lambda expression.
8164 lambda-introducer:
8165 [ lambda-capture [opt] ]
8167 LAMBDA_EXPR is the current representation of the lambda expression. */
8169 static void
8170 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8172 /* Need commas after the first capture. */
8173 bool first = true;
8175 /* Eat the leading `['. */
8176 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8178 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8179 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8180 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8181 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8182 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8183 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8185 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8187 cp_lexer_consume_token (parser->lexer);
8188 first = false;
8191 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8193 cp_token* capture_token;
8194 tree capture_id;
8195 tree capture_init_expr;
8196 cp_id_kind idk = CP_ID_KIND_NONE;
8197 bool explicit_init_p = false;
8199 enum capture_kind_type
8201 BY_COPY,
8202 BY_REFERENCE
8204 enum capture_kind_type capture_kind = BY_COPY;
8206 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8208 error ("expected end of capture-list");
8209 return;
8212 if (first)
8213 first = false;
8214 else
8215 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8217 /* Possibly capture `this'. */
8218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8220 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8221 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8222 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8223 "with by-copy capture default");
8224 cp_lexer_consume_token (parser->lexer);
8225 add_capture (lambda_expr,
8226 /*id=*/this_identifier,
8227 /*initializer=*/finish_this_expr(),
8228 /*by_reference_p=*/false,
8229 explicit_init_p);
8230 continue;
8233 /* Remember whether we want to capture as a reference or not. */
8234 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8236 capture_kind = BY_REFERENCE;
8237 cp_lexer_consume_token (parser->lexer);
8240 /* Get the identifier. */
8241 capture_token = cp_lexer_peek_token (parser->lexer);
8242 capture_id = cp_parser_identifier (parser);
8244 if (capture_id == error_mark_node)
8245 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8246 delimiters, but I modified this to stop on unnested ']' as well. It
8247 was already changed to stop on unnested '}', so the
8248 "closing_parenthesis" name is no more misleading with my change. */
8250 cp_parser_skip_to_closing_parenthesis (parser,
8251 /*recovering=*/true,
8252 /*or_comma=*/true,
8253 /*consume_paren=*/true);
8254 break;
8257 /* Find the initializer for this capture. */
8258 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8260 /* An explicit expression exists. */
8261 cp_lexer_consume_token (parser->lexer);
8262 pedwarn (input_location, OPT_Wpedantic,
8263 "ISO C++ does not allow initializers "
8264 "in lambda expression capture lists");
8265 capture_init_expr = cp_parser_assignment_expression (parser,
8266 /*cast_p=*/true,
8267 &idk);
8268 explicit_init_p = true;
8270 else
8272 const char* error_msg;
8274 /* Turn the identifier into an id-expression. */
8275 capture_init_expr
8276 = cp_parser_lookup_name
8277 (parser,
8278 capture_id,
8279 none_type,
8280 /*is_template=*/false,
8281 /*is_namespace=*/false,
8282 /*check_dependency=*/true,
8283 /*ambiguous_decls=*/NULL,
8284 capture_token->location);
8286 if (capture_init_expr == error_mark_node)
8288 unqualified_name_lookup_error (capture_id);
8289 continue;
8291 else if (DECL_P (capture_init_expr)
8292 && (TREE_CODE (capture_init_expr) != VAR_DECL
8293 && TREE_CODE (capture_init_expr) != PARM_DECL))
8295 error_at (capture_token->location,
8296 "capture of non-variable %qD ",
8297 capture_init_expr);
8298 inform (0, "%q+#D declared here", capture_init_expr);
8299 continue;
8301 if (TREE_CODE (capture_init_expr) == VAR_DECL
8302 && decl_storage_duration (capture_init_expr) != dk_auto)
8304 pedwarn (capture_token->location, 0, "capture of variable "
8305 "%qD with non-automatic storage duration",
8306 capture_init_expr);
8307 inform (0, "%q+#D declared here", capture_init_expr);
8308 continue;
8311 capture_init_expr
8312 = finish_id_expression
8313 (capture_id,
8314 capture_init_expr,
8315 parser->scope,
8316 &idk,
8317 /*integral_constant_expression_p=*/false,
8318 /*allow_non_integral_constant_expression_p=*/false,
8319 /*non_integral_constant_expression_p=*/NULL,
8320 /*template_p=*/false,
8321 /*done=*/true,
8322 /*address_p=*/false,
8323 /*template_arg_p=*/false,
8324 &error_msg,
8325 capture_token->location);
8328 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8329 && !explicit_init_p)
8331 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8332 && capture_kind == BY_COPY)
8333 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8334 "of %qD redundant with by-copy capture default",
8335 capture_id);
8336 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8337 && capture_kind == BY_REFERENCE)
8338 pedwarn (capture_token->location, 0, "explicit by-reference "
8339 "capture of %qD redundant with by-reference capture "
8340 "default", capture_id);
8343 add_capture (lambda_expr,
8344 capture_id,
8345 capture_init_expr,
8346 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8347 explicit_init_p);
8350 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8353 /* Parse the (optional) middle of a lambda expression.
8355 lambda-declarator:
8356 ( parameter-declaration-clause [opt] )
8357 attribute-specifier [opt]
8358 mutable [opt]
8359 exception-specification [opt]
8360 lambda-return-type-clause [opt]
8362 LAMBDA_EXPR is the current representation of the lambda expression. */
8364 static bool
8365 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8367 /* 5.1.1.4 of the standard says:
8368 If a lambda-expression does not include a lambda-declarator, it is as if
8369 the lambda-declarator were ().
8370 This means an empty parameter list, no attributes, and no exception
8371 specification. */
8372 tree param_list = void_list_node;
8373 tree attributes = NULL_TREE;
8374 tree exception_spec = NULL_TREE;
8375 tree t;
8377 /* The lambda-declarator is optional, but must begin with an opening
8378 parenthesis if present. */
8379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8381 cp_lexer_consume_token (parser->lexer);
8383 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8385 /* Parse parameters. */
8386 param_list = cp_parser_parameter_declaration_clause (parser);
8388 /* Default arguments shall not be specified in the
8389 parameter-declaration-clause of a lambda-declarator. */
8390 for (t = param_list; t; t = TREE_CHAIN (t))
8391 if (TREE_PURPOSE (t))
8392 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8393 "default argument specified for lambda parameter");
8395 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8397 attributes = cp_parser_attributes_opt (parser);
8399 /* Parse optional `mutable' keyword. */
8400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8402 cp_lexer_consume_token (parser->lexer);
8403 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8406 /* Parse optional exception specification. */
8407 exception_spec = cp_parser_exception_specification_opt (parser);
8409 /* Parse optional trailing return type. */
8410 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8412 cp_lexer_consume_token (parser->lexer);
8413 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8416 /* The function parameters must be in scope all the way until after the
8417 trailing-return-type in case of decltype. */
8418 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8419 pop_binding (DECL_NAME (t), t);
8421 leave_scope ();
8424 /* Create the function call operator.
8426 Messing with declarators like this is no uglier than building up the
8427 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8428 other code. */
8430 cp_decl_specifier_seq return_type_specs;
8431 cp_declarator* declarator;
8432 tree fco;
8433 int quals;
8434 void *p;
8436 clear_decl_specs (&return_type_specs);
8437 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8438 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8439 else
8440 /* Maybe we will deduce the return type later. */
8441 return_type_specs.type = make_auto ();
8443 p = obstack_alloc (&declarator_obstack, 0);
8445 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8446 sfk_none);
8448 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8449 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8450 declarator = make_call_declarator (declarator, param_list, quals,
8451 VIRT_SPEC_UNSPECIFIED,
8452 exception_spec,
8453 /*late_return_type=*/NULL_TREE);
8454 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8456 fco = grokmethod (&return_type_specs,
8457 declarator,
8458 attributes);
8459 if (fco != error_mark_node)
8461 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8462 DECL_ARTIFICIAL (fco) = 1;
8463 /* Give the object parameter a different name. */
8464 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8467 finish_member_declaration (fco);
8469 obstack_free (&declarator_obstack, p);
8471 return (fco != error_mark_node);
8475 /* Parse the body of a lambda expression, which is simply
8477 compound-statement
8479 but which requires special handling.
8480 LAMBDA_EXPR is the current representation of the lambda expression. */
8482 static void
8483 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8485 bool nested = (current_function_decl != NULL_TREE);
8486 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8487 if (nested)
8488 push_function_context ();
8489 else
8490 /* Still increment function_depth so that we don't GC in the
8491 middle of an expression. */
8492 ++function_depth;
8493 /* Clear this in case we're in the middle of a default argument. */
8494 parser->local_variables_forbidden_p = false;
8496 /* Finish the function call operator
8497 - class_specifier
8498 + late_parsing_for_member
8499 + function_definition_after_declarator
8500 + ctor_initializer_opt_and_function_body */
8502 tree fco = lambda_function (lambda_expr);
8503 tree body;
8504 bool done = false;
8505 tree compound_stmt;
8506 tree cap;
8508 /* Let the front end know that we are going to be defining this
8509 function. */
8510 start_preparsed_function (fco,
8511 NULL_TREE,
8512 SF_PRE_PARSED | SF_INCLASS_INLINE);
8514 start_lambda_scope (fco);
8515 body = begin_function_body ();
8517 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8518 goto out;
8520 /* Push the proxies for any explicit captures. */
8521 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8522 cap = TREE_CHAIN (cap))
8523 build_capture_proxy (TREE_PURPOSE (cap));
8525 compound_stmt = begin_compound_stmt (0);
8527 /* 5.1.1.4 of the standard says:
8528 If a lambda-expression does not include a trailing-return-type, it
8529 is as if the trailing-return-type denotes the following type:
8530 * if the compound-statement is of the form
8531 { return attribute-specifier [opt] expression ; }
8532 the type of the returned expression after lvalue-to-rvalue
8533 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8534 (_conv.array_ 4.2), and function-to-pointer conversion
8535 (_conv.func_ 4.3);
8536 * otherwise, void. */
8538 /* In a lambda that has neither a lambda-return-type-clause
8539 nor a deducible form, errors should be reported for return statements
8540 in the body. Since we used void as the placeholder return type, parsing
8541 the body as usual will give such desired behavior. */
8542 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8543 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8544 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8546 tree expr = NULL_TREE;
8547 cp_id_kind idk = CP_ID_KIND_NONE;
8549 /* Parse tentatively in case there's more after the initial return
8550 statement. */
8551 cp_parser_parse_tentatively (parser);
8553 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8555 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8557 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8558 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8560 if (cp_parser_parse_definitely (parser))
8562 if (!processing_template_decl)
8563 apply_deduced_return_type (fco, lambda_return_type (expr));
8565 /* Will get error here if type not deduced yet. */
8566 finish_return_stmt (expr);
8568 done = true;
8572 if (!done)
8574 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8575 cp_parser_label_declaration (parser);
8576 cp_parser_statement_seq_opt (parser, NULL_TREE);
8577 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8580 finish_compound_stmt (compound_stmt);
8582 out:
8583 finish_function_body (body);
8584 finish_lambda_scope ();
8586 /* Finish the function and generate code for it if necessary. */
8587 expand_or_defer_fn (finish_function (/*inline*/2));
8590 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8591 if (nested)
8592 pop_function_context();
8593 else
8594 --function_depth;
8597 /* Statements [gram.stmt.stmt] */
8599 /* Parse a statement.
8601 statement:
8602 labeled-statement
8603 expression-statement
8604 compound-statement
8605 selection-statement
8606 iteration-statement
8607 jump-statement
8608 declaration-statement
8609 try-block
8611 TM Extension:
8613 statement:
8614 atomic-statement
8616 IN_COMPOUND is true when the statement is nested inside a
8617 cp_parser_compound_statement; this matters for certain pragmas.
8619 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8620 is a (possibly labeled) if statement which is not enclosed in braces
8621 and has an else clause. This is used to implement -Wparentheses. */
8623 static void
8624 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8625 bool in_compound, bool *if_p)
8627 tree statement;
8628 cp_token *token;
8629 location_t statement_location;
8631 restart:
8632 if (if_p != NULL)
8633 *if_p = false;
8634 /* There is no statement yet. */
8635 statement = NULL_TREE;
8636 /* Peek at the next token. */
8637 token = cp_lexer_peek_token (parser->lexer);
8638 /* Remember the location of the first token in the statement. */
8639 statement_location = token->location;
8640 /* If this is a keyword, then that will often determine what kind of
8641 statement we have. */
8642 if (token->type == CPP_KEYWORD)
8644 enum rid keyword = token->keyword;
8646 switch (keyword)
8648 case RID_CASE:
8649 case RID_DEFAULT:
8650 /* Looks like a labeled-statement with a case label.
8651 Parse the label, and then use tail recursion to parse
8652 the statement. */
8653 cp_parser_label_for_labeled_statement (parser);
8654 goto restart;
8656 case RID_IF:
8657 case RID_SWITCH:
8658 statement = cp_parser_selection_statement (parser, if_p);
8659 break;
8661 case RID_WHILE:
8662 case RID_DO:
8663 case RID_FOR:
8664 statement = cp_parser_iteration_statement (parser);
8665 break;
8667 case RID_BREAK:
8668 case RID_CONTINUE:
8669 case RID_RETURN:
8670 case RID_GOTO:
8671 statement = cp_parser_jump_statement (parser);
8672 break;
8674 /* Objective-C++ exception-handling constructs. */
8675 case RID_AT_TRY:
8676 case RID_AT_CATCH:
8677 case RID_AT_FINALLY:
8678 case RID_AT_SYNCHRONIZED:
8679 case RID_AT_THROW:
8680 statement = cp_parser_objc_statement (parser);
8681 break;
8683 case RID_TRY:
8684 statement = cp_parser_try_block (parser);
8685 break;
8687 case RID_NAMESPACE:
8688 /* This must be a namespace alias definition. */
8689 cp_parser_declaration_statement (parser);
8690 return;
8692 case RID_TRANSACTION_ATOMIC:
8693 case RID_TRANSACTION_RELAXED:
8694 statement = cp_parser_transaction (parser, keyword);
8695 break;
8696 case RID_TRANSACTION_CANCEL:
8697 statement = cp_parser_transaction_cancel (parser);
8698 break;
8700 default:
8701 /* It might be a keyword like `int' that can start a
8702 declaration-statement. */
8703 break;
8706 else if (token->type == CPP_NAME)
8708 /* If the next token is a `:', then we are looking at a
8709 labeled-statement. */
8710 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8711 if (token->type == CPP_COLON)
8713 /* Looks like a labeled-statement with an ordinary label.
8714 Parse the label, and then use tail recursion to parse
8715 the statement. */
8716 cp_parser_label_for_labeled_statement (parser);
8717 goto restart;
8720 /* Anything that starts with a `{' must be a compound-statement. */
8721 else if (token->type == CPP_OPEN_BRACE)
8722 statement = cp_parser_compound_statement (parser, NULL, false, false);
8723 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8724 a statement all its own. */
8725 else if (token->type == CPP_PRAGMA)
8727 /* Only certain OpenMP pragmas are attached to statements, and thus
8728 are considered statements themselves. All others are not. In
8729 the context of a compound, accept the pragma as a "statement" and
8730 return so that we can check for a close brace. Otherwise we
8731 require a real statement and must go back and read one. */
8732 if (in_compound)
8733 cp_parser_pragma (parser, pragma_compound);
8734 else if (!cp_parser_pragma (parser, pragma_stmt))
8735 goto restart;
8736 return;
8738 else if (token->type == CPP_EOF)
8740 cp_parser_error (parser, "expected statement");
8741 return;
8744 /* Everything else must be a declaration-statement or an
8745 expression-statement. Try for the declaration-statement
8746 first, unless we are looking at a `;', in which case we know that
8747 we have an expression-statement. */
8748 if (!statement)
8750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8752 cp_parser_parse_tentatively (parser);
8753 /* Try to parse the declaration-statement. */
8754 cp_parser_declaration_statement (parser);
8755 /* If that worked, we're done. */
8756 if (cp_parser_parse_definitely (parser))
8757 return;
8759 /* Look for an expression-statement instead. */
8760 statement = cp_parser_expression_statement (parser, in_statement_expr);
8763 /* Set the line number for the statement. */
8764 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8765 SET_EXPR_LOCATION (statement, statement_location);
8768 /* Parse the label for a labeled-statement, i.e.
8770 identifier :
8771 case constant-expression :
8772 default :
8774 GNU Extension:
8775 case constant-expression ... constant-expression : statement
8777 When a label is parsed without errors, the label is added to the
8778 parse tree by the finish_* functions, so this function doesn't
8779 have to return the label. */
8781 static void
8782 cp_parser_label_for_labeled_statement (cp_parser* parser)
8784 cp_token *token;
8785 tree label = NULL_TREE;
8786 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8788 /* The next token should be an identifier. */
8789 token = cp_lexer_peek_token (parser->lexer);
8790 if (token->type != CPP_NAME
8791 && token->type != CPP_KEYWORD)
8793 cp_parser_error (parser, "expected labeled-statement");
8794 return;
8797 parser->colon_corrects_to_scope_p = false;
8798 switch (token->keyword)
8800 case RID_CASE:
8802 tree expr, expr_hi;
8803 cp_token *ellipsis;
8805 /* Consume the `case' token. */
8806 cp_lexer_consume_token (parser->lexer);
8807 /* Parse the constant-expression. */
8808 expr = cp_parser_constant_expression (parser,
8809 /*allow_non_constant_p=*/false,
8810 NULL);
8812 ellipsis = cp_lexer_peek_token (parser->lexer);
8813 if (ellipsis->type == CPP_ELLIPSIS)
8815 /* Consume the `...' token. */
8816 cp_lexer_consume_token (parser->lexer);
8817 expr_hi =
8818 cp_parser_constant_expression (parser,
8819 /*allow_non_constant_p=*/false,
8820 NULL);
8821 /* We don't need to emit warnings here, as the common code
8822 will do this for us. */
8824 else
8825 expr_hi = NULL_TREE;
8827 if (parser->in_switch_statement_p)
8828 finish_case_label (token->location, expr, expr_hi);
8829 else
8830 error_at (token->location,
8831 "case label %qE not within a switch statement",
8832 expr);
8834 break;
8836 case RID_DEFAULT:
8837 /* Consume the `default' token. */
8838 cp_lexer_consume_token (parser->lexer);
8840 if (parser->in_switch_statement_p)
8841 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8842 else
8843 error_at (token->location, "case label not within a switch statement");
8844 break;
8846 default:
8847 /* Anything else must be an ordinary label. */
8848 label = finish_label_stmt (cp_parser_identifier (parser));
8849 break;
8852 /* Require the `:' token. */
8853 cp_parser_require (parser, CPP_COLON, RT_COLON);
8855 /* An ordinary label may optionally be followed by attributes.
8856 However, this is only permitted if the attributes are then
8857 followed by a semicolon. This is because, for backward
8858 compatibility, when parsing
8859 lab: __attribute__ ((unused)) int i;
8860 we want the attribute to attach to "i", not "lab". */
8861 if (label != NULL_TREE
8862 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8864 tree attrs;
8866 cp_parser_parse_tentatively (parser);
8867 attrs = cp_parser_attributes_opt (parser);
8868 if (attrs == NULL_TREE
8869 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8870 cp_parser_abort_tentative_parse (parser);
8871 else if (!cp_parser_parse_definitely (parser))
8873 else
8874 cplus_decl_attributes (&label, attrs, 0);
8877 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8880 /* Parse an expression-statement.
8882 expression-statement:
8883 expression [opt] ;
8885 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8886 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8887 indicates whether this expression-statement is part of an
8888 expression statement. */
8890 static tree
8891 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8893 tree statement = NULL_TREE;
8894 cp_token *token = cp_lexer_peek_token (parser->lexer);
8896 /* If the next token is a ';', then there is no expression
8897 statement. */
8898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8899 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8901 /* Give a helpful message for "A<T>::type t;" and the like. */
8902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8903 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8905 if (TREE_CODE (statement) == SCOPE_REF)
8906 error_at (token->location, "need %<typename%> before %qE because "
8907 "%qT is a dependent scope",
8908 statement, TREE_OPERAND (statement, 0));
8909 else if (is_overloaded_fn (statement)
8910 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8912 /* A::A a; */
8913 tree fn = get_first_fn (statement);
8914 error_at (token->location,
8915 "%<%T::%D%> names the constructor, not the type",
8916 DECL_CONTEXT (fn), DECL_NAME (fn));
8920 /* Consume the final `;'. */
8921 cp_parser_consume_semicolon_at_end_of_statement (parser);
8923 if (in_statement_expr
8924 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8925 /* This is the final expression statement of a statement
8926 expression. */
8927 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8928 else if (statement)
8929 statement = finish_expr_stmt (statement);
8930 else
8931 finish_stmt ();
8933 return statement;
8936 /* Parse a compound-statement.
8938 compound-statement:
8939 { statement-seq [opt] }
8941 GNU extension:
8943 compound-statement:
8944 { label-declaration-seq [opt] statement-seq [opt] }
8946 label-declaration-seq:
8947 label-declaration
8948 label-declaration-seq label-declaration
8950 Returns a tree representing the statement. */
8952 static tree
8953 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8954 bool in_try, bool function_body)
8956 tree compound_stmt;
8958 /* Consume the `{'. */
8959 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8960 return error_mark_node;
8961 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8962 && !function_body)
8963 pedwarn (input_location, OPT_Wpedantic,
8964 "compound-statement in constexpr function");
8965 /* Begin the compound-statement. */
8966 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8967 /* If the next keyword is `__label__' we have a label declaration. */
8968 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8969 cp_parser_label_declaration (parser);
8970 /* Parse an (optional) statement-seq. */
8971 cp_parser_statement_seq_opt (parser, in_statement_expr);
8972 /* Finish the compound-statement. */
8973 finish_compound_stmt (compound_stmt);
8974 /* Consume the `}'. */
8975 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8977 return compound_stmt;
8980 /* Parse an (optional) statement-seq.
8982 statement-seq:
8983 statement
8984 statement-seq [opt] statement */
8986 static void
8987 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8989 /* Scan statements until there aren't any more. */
8990 while (true)
8992 cp_token *token = cp_lexer_peek_token (parser->lexer);
8994 /* If we are looking at a `}', then we have run out of
8995 statements; the same is true if we have reached the end
8996 of file, or have stumbled upon a stray '@end'. */
8997 if (token->type == CPP_CLOSE_BRACE
8998 || token->type == CPP_EOF
8999 || token->type == CPP_PRAGMA_EOL
9000 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9001 break;
9003 /* If we are in a compound statement and find 'else' then
9004 something went wrong. */
9005 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9007 if (parser->in_statement & IN_IF_STMT)
9008 break;
9009 else
9011 token = cp_lexer_consume_token (parser->lexer);
9012 error_at (token->location, "%<else%> without a previous %<if%>");
9016 /* Parse the statement. */
9017 cp_parser_statement (parser, in_statement_expr, true, NULL);
9021 /* Parse a selection-statement.
9023 selection-statement:
9024 if ( condition ) statement
9025 if ( condition ) statement else statement
9026 switch ( condition ) statement
9028 Returns the new IF_STMT or SWITCH_STMT.
9030 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9031 is a (possibly labeled) if statement which is not enclosed in
9032 braces and has an else clause. This is used to implement
9033 -Wparentheses. */
9035 static tree
9036 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9038 cp_token *token;
9039 enum rid keyword;
9041 if (if_p != NULL)
9042 *if_p = false;
9044 /* Peek at the next token. */
9045 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9047 /* See what kind of keyword it is. */
9048 keyword = token->keyword;
9049 switch (keyword)
9051 case RID_IF:
9052 case RID_SWITCH:
9054 tree statement;
9055 tree condition;
9057 /* Look for the `('. */
9058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9060 cp_parser_skip_to_end_of_statement (parser);
9061 return error_mark_node;
9064 /* Begin the selection-statement. */
9065 if (keyword == RID_IF)
9066 statement = begin_if_stmt ();
9067 else
9068 statement = begin_switch_stmt ();
9070 /* Parse the condition. */
9071 condition = cp_parser_condition (parser);
9072 /* Look for the `)'. */
9073 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9074 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9075 /*consume_paren=*/true);
9077 if (keyword == RID_IF)
9079 bool nested_if;
9080 unsigned char in_statement;
9082 /* Add the condition. */
9083 finish_if_stmt_cond (condition, statement);
9085 /* Parse the then-clause. */
9086 in_statement = parser->in_statement;
9087 parser->in_statement |= IN_IF_STMT;
9088 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9090 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9091 add_stmt (build_empty_stmt (loc));
9092 cp_lexer_consume_token (parser->lexer);
9093 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9094 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9095 "empty body in an %<if%> statement");
9096 nested_if = false;
9098 else
9099 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9100 parser->in_statement = in_statement;
9102 finish_then_clause (statement);
9104 /* If the next token is `else', parse the else-clause. */
9105 if (cp_lexer_next_token_is_keyword (parser->lexer,
9106 RID_ELSE))
9108 /* Consume the `else' keyword. */
9109 cp_lexer_consume_token (parser->lexer);
9110 begin_else_clause (statement);
9111 /* Parse the else-clause. */
9112 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9114 location_t loc;
9115 loc = cp_lexer_peek_token (parser->lexer)->location;
9116 warning_at (loc,
9117 OPT_Wempty_body, "suggest braces around "
9118 "empty body in an %<else%> statement");
9119 add_stmt (build_empty_stmt (loc));
9120 cp_lexer_consume_token (parser->lexer);
9122 else
9123 cp_parser_implicitly_scoped_statement (parser, NULL);
9125 finish_else_clause (statement);
9127 /* If we are currently parsing a then-clause, then
9128 IF_P will not be NULL. We set it to true to
9129 indicate that this if statement has an else clause.
9130 This may trigger the Wparentheses warning below
9131 when we get back up to the parent if statement. */
9132 if (if_p != NULL)
9133 *if_p = true;
9135 else
9137 /* This if statement does not have an else clause. If
9138 NESTED_IF is true, then the then-clause is an if
9139 statement which does have an else clause. We warn
9140 about the potential ambiguity. */
9141 if (nested_if)
9142 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9143 "suggest explicit braces to avoid ambiguous"
9144 " %<else%>");
9147 /* Now we're all done with the if-statement. */
9148 finish_if_stmt (statement);
9150 else
9152 bool in_switch_statement_p;
9153 unsigned char in_statement;
9155 /* Add the condition. */
9156 finish_switch_cond (condition, statement);
9158 /* Parse the body of the switch-statement. */
9159 in_switch_statement_p = parser->in_switch_statement_p;
9160 in_statement = parser->in_statement;
9161 parser->in_switch_statement_p = true;
9162 parser->in_statement |= IN_SWITCH_STMT;
9163 cp_parser_implicitly_scoped_statement (parser, NULL);
9164 parser->in_switch_statement_p = in_switch_statement_p;
9165 parser->in_statement = in_statement;
9167 /* Now we're all done with the switch-statement. */
9168 finish_switch_stmt (statement);
9171 return statement;
9173 break;
9175 default:
9176 cp_parser_error (parser, "expected selection-statement");
9177 return error_mark_node;
9181 /* Parse a condition.
9183 condition:
9184 expression
9185 type-specifier-seq declarator = initializer-clause
9186 type-specifier-seq declarator braced-init-list
9188 GNU Extension:
9190 condition:
9191 type-specifier-seq declarator asm-specification [opt]
9192 attributes [opt] = assignment-expression
9194 Returns the expression that should be tested. */
9196 static tree
9197 cp_parser_condition (cp_parser* parser)
9199 cp_decl_specifier_seq type_specifiers;
9200 const char *saved_message;
9201 int declares_class_or_enum;
9203 /* Try the declaration first. */
9204 cp_parser_parse_tentatively (parser);
9205 /* New types are not allowed in the type-specifier-seq for a
9206 condition. */
9207 saved_message = parser->type_definition_forbidden_message;
9208 parser->type_definition_forbidden_message
9209 = G_("types may not be defined in conditions");
9210 /* Parse the type-specifier-seq. */
9211 cp_parser_decl_specifier_seq (parser,
9212 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9213 &type_specifiers,
9214 &declares_class_or_enum);
9215 /* Restore the saved message. */
9216 parser->type_definition_forbidden_message = saved_message;
9217 /* If all is well, we might be looking at a declaration. */
9218 if (!cp_parser_error_occurred (parser))
9220 tree decl;
9221 tree asm_specification;
9222 tree attributes;
9223 cp_declarator *declarator;
9224 tree initializer = NULL_TREE;
9226 /* Parse the declarator. */
9227 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9228 /*ctor_dtor_or_conv_p=*/NULL,
9229 /*parenthesized_p=*/NULL,
9230 /*member_p=*/false);
9231 /* Parse the attributes. */
9232 attributes = cp_parser_attributes_opt (parser);
9233 /* Parse the asm-specification. */
9234 asm_specification = cp_parser_asm_specification_opt (parser);
9235 /* If the next token is not an `=' or '{', then we might still be
9236 looking at an expression. For example:
9238 if (A(a).x)
9240 looks like a decl-specifier-seq and a declarator -- but then
9241 there is no `=', so this is an expression. */
9242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9243 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9244 cp_parser_simulate_error (parser);
9246 /* If we did see an `=' or '{', then we are looking at a declaration
9247 for sure. */
9248 if (cp_parser_parse_definitely (parser))
9250 tree pushed_scope;
9251 bool non_constant_p;
9252 bool flags = LOOKUP_ONLYCONVERTING;
9254 /* Create the declaration. */
9255 decl = start_decl (declarator, &type_specifiers,
9256 /*initialized_p=*/true,
9257 attributes, /*prefix_attributes=*/NULL_TREE,
9258 &pushed_scope);
9260 /* Parse the initializer. */
9261 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9263 initializer = cp_parser_braced_list (parser, &non_constant_p);
9264 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9265 flags = 0;
9267 else
9269 /* Consume the `='. */
9270 cp_parser_require (parser, CPP_EQ, RT_EQ);
9271 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9273 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9274 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9276 /* Process the initializer. */
9277 cp_finish_decl (decl,
9278 initializer, !non_constant_p,
9279 asm_specification,
9280 flags);
9282 if (pushed_scope)
9283 pop_scope (pushed_scope);
9285 return convert_from_reference (decl);
9288 /* If we didn't even get past the declarator successfully, we are
9289 definitely not looking at a declaration. */
9290 else
9291 cp_parser_abort_tentative_parse (parser);
9293 /* Otherwise, we are looking at an expression. */
9294 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9297 /* Parses a for-statement or range-for-statement until the closing ')',
9298 not included. */
9300 static tree
9301 cp_parser_for (cp_parser *parser)
9303 tree init, scope, decl;
9304 bool is_range_for;
9306 /* Begin the for-statement. */
9307 scope = begin_for_scope (&init);
9309 /* Parse the initialization. */
9310 is_range_for = cp_parser_for_init_statement (parser, &decl);
9312 if (is_range_for)
9313 return cp_parser_range_for (parser, scope, init, decl);
9314 else
9315 return cp_parser_c_for (parser, scope, init);
9318 static tree
9319 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9321 /* Normal for loop */
9322 tree condition = NULL_TREE;
9323 tree expression = NULL_TREE;
9324 tree stmt;
9326 stmt = begin_for_stmt (scope, init);
9327 /* The for-init-statement has already been parsed in
9328 cp_parser_for_init_statement, so no work is needed here. */
9329 finish_for_init_stmt (stmt);
9331 /* If there's a condition, process it. */
9332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9333 condition = cp_parser_condition (parser);
9334 finish_for_cond (condition, stmt);
9335 /* Look for the `;'. */
9336 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9338 /* If there's an expression, process it. */
9339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9340 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9341 finish_for_expr (expression, stmt);
9343 return stmt;
9346 /* Tries to parse a range-based for-statement:
9348 range-based-for:
9349 decl-specifier-seq declarator : expression
9351 The decl-specifier-seq declarator and the `:' are already parsed by
9352 cp_parser_for_init_statement. If processing_template_decl it returns a
9353 newly created RANGE_FOR_STMT; if not, it is converted to a
9354 regular FOR_STMT. */
9356 static tree
9357 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9359 tree stmt, range_expr;
9361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9363 bool expr_non_constant_p;
9364 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9366 else
9367 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9369 /* If in template, STMT is converted to a normal for-statement
9370 at instantiation. If not, it is done just ahead. */
9371 if (processing_template_decl)
9373 if (check_for_bare_parameter_packs (range_expr))
9374 range_expr = error_mark_node;
9375 stmt = begin_range_for_stmt (scope, init);
9376 finish_range_for_decl (stmt, range_decl, range_expr);
9377 if (!type_dependent_expression_p (range_expr)
9378 /* do_auto_deduction doesn't mess with template init-lists. */
9379 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9380 do_range_for_auto_deduction (range_decl, range_expr);
9382 else
9384 stmt = begin_for_stmt (scope, init);
9385 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9387 return stmt;
9390 /* Subroutine of cp_convert_range_for: given the initializer expression,
9391 builds up the range temporary. */
9393 static tree
9394 build_range_temp (tree range_expr)
9396 tree range_type, range_temp;
9398 /* Find out the type deduced by the declaration
9399 `auto &&__range = range_expr'. */
9400 range_type = cp_build_reference_type (make_auto (), true);
9401 range_type = do_auto_deduction (range_type, range_expr,
9402 type_uses_auto (range_type));
9404 /* Create the __range variable. */
9405 range_temp = build_decl (input_location, VAR_DECL,
9406 get_identifier ("__for_range"), range_type);
9407 TREE_USED (range_temp) = 1;
9408 DECL_ARTIFICIAL (range_temp) = 1;
9410 return range_temp;
9413 /* Used by cp_parser_range_for in template context: we aren't going to
9414 do a full conversion yet, but we still need to resolve auto in the
9415 type of the for-range-declaration if present. This is basically
9416 a shortcut version of cp_convert_range_for. */
9418 static void
9419 do_range_for_auto_deduction (tree decl, tree range_expr)
9421 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9422 if (auto_node)
9424 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9425 range_temp = convert_from_reference (build_range_temp (range_expr));
9426 iter_type = (cp_parser_perform_range_for_lookup
9427 (range_temp, &begin_dummy, &end_dummy));
9428 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9429 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9430 tf_warning_or_error);
9431 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9432 iter_decl, auto_node);
9436 /* Converts a range-based for-statement into a normal
9437 for-statement, as per the definition.
9439 for (RANGE_DECL : RANGE_EXPR)
9440 BLOCK
9442 should be equivalent to:
9445 auto &&__range = RANGE_EXPR;
9446 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9447 __begin != __end;
9448 ++__begin)
9450 RANGE_DECL = *__begin;
9451 BLOCK
9455 If RANGE_EXPR is an array:
9456 BEGIN_EXPR = __range
9457 END_EXPR = __range + ARRAY_SIZE(__range)
9458 Else if RANGE_EXPR has a member 'begin' or 'end':
9459 BEGIN_EXPR = __range.begin()
9460 END_EXPR = __range.end()
9461 Else:
9462 BEGIN_EXPR = begin(__range)
9463 END_EXPR = end(__range);
9465 If __range has a member 'begin' but not 'end', or vice versa, we must
9466 still use the second alternative (it will surely fail, however).
9467 When calling begin()/end() in the third alternative we must use
9468 argument dependent lookup, but always considering 'std' as an associated
9469 namespace. */
9471 tree
9472 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9474 tree begin, end;
9475 tree iter_type, begin_expr, end_expr;
9476 tree condition, expression;
9478 if (range_decl == error_mark_node || range_expr == error_mark_node)
9479 /* If an error happened previously do nothing or else a lot of
9480 unhelpful errors would be issued. */
9481 begin_expr = end_expr = iter_type = error_mark_node;
9482 else
9484 tree range_temp = build_range_temp (range_expr);
9485 pushdecl (range_temp);
9486 cp_finish_decl (range_temp, range_expr,
9487 /*is_constant_init*/false, NULL_TREE,
9488 LOOKUP_ONLYCONVERTING);
9490 range_temp = convert_from_reference (range_temp);
9491 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9492 &begin_expr, &end_expr);
9495 /* The new for initialization statement. */
9496 begin = build_decl (input_location, VAR_DECL,
9497 get_identifier ("__for_begin"), iter_type);
9498 TREE_USED (begin) = 1;
9499 DECL_ARTIFICIAL (begin) = 1;
9500 pushdecl (begin);
9501 cp_finish_decl (begin, begin_expr,
9502 /*is_constant_init*/false, NULL_TREE,
9503 LOOKUP_ONLYCONVERTING);
9505 end = build_decl (input_location, VAR_DECL,
9506 get_identifier ("__for_end"), iter_type);
9507 TREE_USED (end) = 1;
9508 DECL_ARTIFICIAL (end) = 1;
9509 pushdecl (end);
9510 cp_finish_decl (end, end_expr,
9511 /*is_constant_init*/false, NULL_TREE,
9512 LOOKUP_ONLYCONVERTING);
9514 finish_for_init_stmt (statement);
9516 /* The new for condition. */
9517 condition = build_x_binary_op (input_location, NE_EXPR,
9518 begin, ERROR_MARK,
9519 end, ERROR_MARK,
9520 NULL, tf_warning_or_error);
9521 finish_for_cond (condition, statement);
9523 /* The new increment expression. */
9524 expression = finish_unary_op_expr (input_location,
9525 PREINCREMENT_EXPR, begin);
9526 finish_for_expr (expression, statement);
9528 /* The declaration is initialized with *__begin inside the loop body. */
9529 cp_finish_decl (range_decl,
9530 build_x_indirect_ref (input_location, begin, RO_NULL,
9531 tf_warning_or_error),
9532 /*is_constant_init*/false, NULL_TREE,
9533 LOOKUP_ONLYCONVERTING);
9535 return statement;
9538 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9539 We need to solve both at the same time because the method used
9540 depends on the existence of members begin or end.
9541 Returns the type deduced for the iterator expression. */
9543 static tree
9544 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9546 if (error_operand_p (range))
9548 *begin = *end = error_mark_node;
9549 return error_mark_node;
9552 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9554 error ("range-based %<for%> expression of type %qT "
9555 "has incomplete type", TREE_TYPE (range));
9556 *begin = *end = error_mark_node;
9557 return error_mark_node;
9559 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9561 /* If RANGE is an array, we will use pointer arithmetic. */
9562 *begin = range;
9563 *end = build_binary_op (input_location, PLUS_EXPR,
9564 range,
9565 array_type_nelts_top (TREE_TYPE (range)),
9567 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9569 else
9571 /* If it is not an array, we must do a bit of magic. */
9572 tree id_begin, id_end;
9573 tree member_begin, member_end;
9575 *begin = *end = error_mark_node;
9577 id_begin = get_identifier ("begin");
9578 id_end = get_identifier ("end");
9579 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9580 /*protect=*/2, /*want_type=*/false,
9581 tf_warning_or_error);
9582 member_end = lookup_member (TREE_TYPE (range), id_end,
9583 /*protect=*/2, /*want_type=*/false,
9584 tf_warning_or_error);
9586 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9588 /* Use the member functions. */
9589 if (member_begin != NULL_TREE)
9590 *begin = cp_parser_range_for_member_function (range, id_begin);
9591 else
9592 error ("range-based %<for%> expression of type %qT has an "
9593 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9595 if (member_end != NULL_TREE)
9596 *end = cp_parser_range_for_member_function (range, id_end);
9597 else
9598 error ("range-based %<for%> expression of type %qT has a "
9599 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9601 else
9603 /* Use global functions with ADL. */
9604 VEC(tree,gc) *vec;
9605 vec = make_tree_vector ();
9607 VEC_safe_push (tree, gc, vec, range);
9609 member_begin = perform_koenig_lookup (id_begin, vec,
9610 /*include_std=*/true,
9611 tf_warning_or_error);
9612 *begin = finish_call_expr (member_begin, &vec, false, true,
9613 tf_warning_or_error);
9614 member_end = perform_koenig_lookup (id_end, vec,
9615 /*include_std=*/true,
9616 tf_warning_or_error);
9617 *end = finish_call_expr (member_end, &vec, false, true,
9618 tf_warning_or_error);
9620 release_tree_vector (vec);
9623 /* Last common checks. */
9624 if (*begin == error_mark_node || *end == error_mark_node)
9626 /* If one of the expressions is an error do no more checks. */
9627 *begin = *end = error_mark_node;
9628 return error_mark_node;
9630 else
9632 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9633 /* The unqualified type of the __begin and __end temporaries should
9634 be the same, as required by the multiple auto declaration. */
9635 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9636 error ("inconsistent begin/end types in range-based %<for%> "
9637 "statement: %qT and %qT",
9638 TREE_TYPE (*begin), TREE_TYPE (*end));
9639 return iter_type;
9644 /* Helper function for cp_parser_perform_range_for_lookup.
9645 Builds a tree for RANGE.IDENTIFIER(). */
9647 static tree
9648 cp_parser_range_for_member_function (tree range, tree identifier)
9650 tree member, res;
9651 VEC(tree,gc) *vec;
9653 member = finish_class_member_access_expr (range, identifier,
9654 false, tf_warning_or_error);
9655 if (member == error_mark_node)
9656 return error_mark_node;
9658 vec = make_tree_vector ();
9659 res = finish_call_expr (member, &vec,
9660 /*disallow_virtual=*/false,
9661 /*koenig_p=*/false,
9662 tf_warning_or_error);
9663 release_tree_vector (vec);
9664 return res;
9667 /* Parse an iteration-statement.
9669 iteration-statement:
9670 while ( condition ) statement
9671 do statement while ( expression ) ;
9672 for ( for-init-statement condition [opt] ; expression [opt] )
9673 statement
9675 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9677 static tree
9678 cp_parser_iteration_statement (cp_parser* parser)
9680 cp_token *token;
9681 enum rid keyword;
9682 tree statement;
9683 unsigned char in_statement;
9685 /* Peek at the next token. */
9686 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9687 if (!token)
9688 return error_mark_node;
9690 /* Remember whether or not we are already within an iteration
9691 statement. */
9692 in_statement = parser->in_statement;
9694 /* See what kind of keyword it is. */
9695 keyword = token->keyword;
9696 switch (keyword)
9698 case RID_WHILE:
9700 tree condition;
9702 /* Begin the while-statement. */
9703 statement = begin_while_stmt ();
9704 /* Look for the `('. */
9705 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9706 /* Parse the condition. */
9707 condition = cp_parser_condition (parser);
9708 finish_while_stmt_cond (condition, statement);
9709 /* Look for the `)'. */
9710 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9711 /* Parse the dependent statement. */
9712 parser->in_statement = IN_ITERATION_STMT;
9713 cp_parser_already_scoped_statement (parser);
9714 parser->in_statement = in_statement;
9715 /* We're done with the while-statement. */
9716 finish_while_stmt (statement);
9718 break;
9720 case RID_DO:
9722 tree expression;
9724 /* Begin the do-statement. */
9725 statement = begin_do_stmt ();
9726 /* Parse the body of the do-statement. */
9727 parser->in_statement = IN_ITERATION_STMT;
9728 cp_parser_implicitly_scoped_statement (parser, NULL);
9729 parser->in_statement = in_statement;
9730 finish_do_body (statement);
9731 /* Look for the `while' keyword. */
9732 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9733 /* Look for the `('. */
9734 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9735 /* Parse the expression. */
9736 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9737 /* We're done with the do-statement. */
9738 finish_do_stmt (expression, statement);
9739 /* Look for the `)'. */
9740 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9741 /* Look for the `;'. */
9742 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9744 break;
9746 case RID_FOR:
9748 /* Look for the `('. */
9749 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9751 statement = cp_parser_for (parser);
9753 /* Look for the `)'. */
9754 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9756 /* Parse the body of the for-statement. */
9757 parser->in_statement = IN_ITERATION_STMT;
9758 cp_parser_already_scoped_statement (parser);
9759 parser->in_statement = in_statement;
9761 /* We're done with the for-statement. */
9762 finish_for_stmt (statement);
9764 break;
9766 default:
9767 cp_parser_error (parser, "expected iteration-statement");
9768 statement = error_mark_node;
9769 break;
9772 return statement;
9775 /* Parse a for-init-statement or the declarator of a range-based-for.
9776 Returns true if a range-based-for declaration is seen.
9778 for-init-statement:
9779 expression-statement
9780 simple-declaration */
9782 static bool
9783 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9785 /* If the next token is a `;', then we have an empty
9786 expression-statement. Grammatically, this is also a
9787 simple-declaration, but an invalid one, because it does not
9788 declare anything. Therefore, if we did not handle this case
9789 specially, we would issue an error message about an invalid
9790 declaration. */
9791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9793 bool is_range_for = false;
9794 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9796 parser->colon_corrects_to_scope_p = false;
9798 /* We're going to speculatively look for a declaration, falling back
9799 to an expression, if necessary. */
9800 cp_parser_parse_tentatively (parser);
9801 /* Parse the declaration. */
9802 cp_parser_simple_declaration (parser,
9803 /*function_definition_allowed_p=*/false,
9804 decl);
9805 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9806 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9808 /* It is a range-for, consume the ':' */
9809 cp_lexer_consume_token (parser->lexer);
9810 is_range_for = true;
9811 if (cxx_dialect < cxx0x)
9813 error_at (cp_lexer_peek_token (parser->lexer)->location,
9814 "range-based %<for%> loops are not allowed "
9815 "in C++98 mode");
9816 *decl = error_mark_node;
9819 else
9820 /* The ';' is not consumed yet because we told
9821 cp_parser_simple_declaration not to. */
9822 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9824 if (cp_parser_parse_definitely (parser))
9825 return is_range_for;
9826 /* If the tentative parse failed, then we shall need to look for an
9827 expression-statement. */
9829 /* If we are here, it is an expression-statement. */
9830 cp_parser_expression_statement (parser, NULL_TREE);
9831 return false;
9834 /* Parse a jump-statement.
9836 jump-statement:
9837 break ;
9838 continue ;
9839 return expression [opt] ;
9840 return braced-init-list ;
9841 goto identifier ;
9843 GNU extension:
9845 jump-statement:
9846 goto * expression ;
9848 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9850 static tree
9851 cp_parser_jump_statement (cp_parser* parser)
9853 tree statement = error_mark_node;
9854 cp_token *token;
9855 enum rid keyword;
9856 unsigned char in_statement;
9858 /* Peek at the next token. */
9859 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9860 if (!token)
9861 return error_mark_node;
9863 /* See what kind of keyword it is. */
9864 keyword = token->keyword;
9865 switch (keyword)
9867 case RID_BREAK:
9868 in_statement = parser->in_statement & ~IN_IF_STMT;
9869 switch (in_statement)
9871 case 0:
9872 error_at (token->location, "break statement not within loop or switch");
9873 break;
9874 default:
9875 gcc_assert ((in_statement & IN_SWITCH_STMT)
9876 || in_statement == IN_ITERATION_STMT);
9877 statement = finish_break_stmt ();
9878 break;
9879 case IN_OMP_BLOCK:
9880 error_at (token->location, "invalid exit from OpenMP structured block");
9881 break;
9882 case IN_OMP_FOR:
9883 error_at (token->location, "break statement used with OpenMP for loop");
9884 break;
9886 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9887 break;
9889 case RID_CONTINUE:
9890 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9892 case 0:
9893 error_at (token->location, "continue statement not within a loop");
9894 break;
9895 case IN_ITERATION_STMT:
9896 case IN_OMP_FOR:
9897 statement = finish_continue_stmt ();
9898 break;
9899 case IN_OMP_BLOCK:
9900 error_at (token->location, "invalid exit from OpenMP structured block");
9901 break;
9902 default:
9903 gcc_unreachable ();
9905 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9906 break;
9908 case RID_RETURN:
9910 tree expr;
9911 bool expr_non_constant_p;
9913 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9915 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9916 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9918 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9919 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9920 else
9921 /* If the next token is a `;', then there is no
9922 expression. */
9923 expr = NULL_TREE;
9924 /* Build the return-statement. */
9925 statement = finish_return_stmt (expr);
9926 /* Look for the final `;'. */
9927 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9929 break;
9931 case RID_GOTO:
9932 /* Create the goto-statement. */
9933 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9935 /* Issue a warning about this use of a GNU extension. */
9936 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
9937 /* Consume the '*' token. */
9938 cp_lexer_consume_token (parser->lexer);
9939 /* Parse the dependent expression. */
9940 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9942 else
9943 finish_goto_stmt (cp_parser_identifier (parser));
9944 /* Look for the final `;'. */
9945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9946 break;
9948 default:
9949 cp_parser_error (parser, "expected jump-statement");
9950 break;
9953 return statement;
9956 /* Parse a declaration-statement.
9958 declaration-statement:
9959 block-declaration */
9961 static void
9962 cp_parser_declaration_statement (cp_parser* parser)
9964 void *p;
9966 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9967 p = obstack_alloc (&declarator_obstack, 0);
9969 /* Parse the block-declaration. */
9970 cp_parser_block_declaration (parser, /*statement_p=*/true);
9972 /* Free any declarators allocated. */
9973 obstack_free (&declarator_obstack, p);
9975 /* Finish off the statement. */
9976 finish_stmt ();
9979 /* Some dependent statements (like `if (cond) statement'), are
9980 implicitly in their own scope. In other words, if the statement is
9981 a single statement (as opposed to a compound-statement), it is
9982 none-the-less treated as if it were enclosed in braces. Any
9983 declarations appearing in the dependent statement are out of scope
9984 after control passes that point. This function parses a statement,
9985 but ensures that is in its own scope, even if it is not a
9986 compound-statement.
9988 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9989 is a (possibly labeled) if statement which is not enclosed in
9990 braces and has an else clause. This is used to implement
9991 -Wparentheses.
9993 Returns the new statement. */
9995 static tree
9996 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9998 tree statement;
10000 if (if_p != NULL)
10001 *if_p = false;
10003 /* Mark if () ; with a special NOP_EXPR. */
10004 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10006 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10007 cp_lexer_consume_token (parser->lexer);
10008 statement = add_stmt (build_empty_stmt (loc));
10010 /* if a compound is opened, we simply parse the statement directly. */
10011 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10012 statement = cp_parser_compound_statement (parser, NULL, false, false);
10013 /* If the token is not a `{', then we must take special action. */
10014 else
10016 /* Create a compound-statement. */
10017 statement = begin_compound_stmt (0);
10018 /* Parse the dependent-statement. */
10019 cp_parser_statement (parser, NULL_TREE, false, if_p);
10020 /* Finish the dummy compound-statement. */
10021 finish_compound_stmt (statement);
10024 /* Return the statement. */
10025 return statement;
10028 /* For some dependent statements (like `while (cond) statement'), we
10029 have already created a scope. Therefore, even if the dependent
10030 statement is a compound-statement, we do not want to create another
10031 scope. */
10033 static void
10034 cp_parser_already_scoped_statement (cp_parser* parser)
10036 /* If the token is a `{', then we must take special action. */
10037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10038 cp_parser_statement (parser, NULL_TREE, false, NULL);
10039 else
10041 /* Avoid calling cp_parser_compound_statement, so that we
10042 don't create a new scope. Do everything else by hand. */
10043 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10044 /* If the next keyword is `__label__' we have a label declaration. */
10045 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10046 cp_parser_label_declaration (parser);
10047 /* Parse an (optional) statement-seq. */
10048 cp_parser_statement_seq_opt (parser, NULL_TREE);
10049 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10053 /* Declarations [gram.dcl.dcl] */
10055 /* Parse an optional declaration-sequence.
10057 declaration-seq:
10058 declaration
10059 declaration-seq declaration */
10061 static void
10062 cp_parser_declaration_seq_opt (cp_parser* parser)
10064 while (true)
10066 cp_token *token;
10068 token = cp_lexer_peek_token (parser->lexer);
10070 if (token->type == CPP_CLOSE_BRACE
10071 || token->type == CPP_EOF
10072 || token->type == CPP_PRAGMA_EOL)
10073 break;
10075 if (token->type == CPP_SEMICOLON)
10077 /* A declaration consisting of a single semicolon is
10078 invalid. Allow it unless we're being pedantic. */
10079 cp_lexer_consume_token (parser->lexer);
10080 if (!in_system_header)
10081 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10082 continue;
10085 /* If we're entering or exiting a region that's implicitly
10086 extern "C", modify the lang context appropriately. */
10087 if (!parser->implicit_extern_c && token->implicit_extern_c)
10089 push_lang_context (lang_name_c);
10090 parser->implicit_extern_c = true;
10092 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10094 pop_lang_context ();
10095 parser->implicit_extern_c = false;
10098 if (token->type == CPP_PRAGMA)
10100 /* A top-level declaration can consist solely of a #pragma.
10101 A nested declaration cannot, so this is done here and not
10102 in cp_parser_declaration. (A #pragma at block scope is
10103 handled in cp_parser_statement.) */
10104 cp_parser_pragma (parser, pragma_external);
10105 continue;
10108 /* Parse the declaration itself. */
10109 cp_parser_declaration (parser);
10113 /* Parse a declaration.
10115 declaration:
10116 block-declaration
10117 function-definition
10118 template-declaration
10119 explicit-instantiation
10120 explicit-specialization
10121 linkage-specification
10122 namespace-definition
10124 GNU extension:
10126 declaration:
10127 __extension__ declaration */
10129 static void
10130 cp_parser_declaration (cp_parser* parser)
10132 cp_token token1;
10133 cp_token token2;
10134 int saved_pedantic;
10135 void *p;
10136 tree attributes = NULL_TREE;
10138 /* Check for the `__extension__' keyword. */
10139 if (cp_parser_extension_opt (parser, &saved_pedantic))
10141 /* Parse the qualified declaration. */
10142 cp_parser_declaration (parser);
10143 /* Restore the PEDANTIC flag. */
10144 pedantic = saved_pedantic;
10146 return;
10149 /* Try to figure out what kind of declaration is present. */
10150 token1 = *cp_lexer_peek_token (parser->lexer);
10152 if (token1.type != CPP_EOF)
10153 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10154 else
10156 token2.type = CPP_EOF;
10157 token2.keyword = RID_MAX;
10160 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10161 p = obstack_alloc (&declarator_obstack, 0);
10163 /* If the next token is `extern' and the following token is a string
10164 literal, then we have a linkage specification. */
10165 if (token1.keyword == RID_EXTERN
10166 && cp_parser_is_pure_string_literal (&token2))
10167 cp_parser_linkage_specification (parser);
10168 /* If the next token is `template', then we have either a template
10169 declaration, an explicit instantiation, or an explicit
10170 specialization. */
10171 else if (token1.keyword == RID_TEMPLATE)
10173 /* `template <>' indicates a template specialization. */
10174 if (token2.type == CPP_LESS
10175 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10176 cp_parser_explicit_specialization (parser);
10177 /* `template <' indicates a template declaration. */
10178 else if (token2.type == CPP_LESS)
10179 cp_parser_template_declaration (parser, /*member_p=*/false);
10180 /* Anything else must be an explicit instantiation. */
10181 else
10182 cp_parser_explicit_instantiation (parser);
10184 /* If the next token is `export', then we have a template
10185 declaration. */
10186 else if (token1.keyword == RID_EXPORT)
10187 cp_parser_template_declaration (parser, /*member_p=*/false);
10188 /* If the next token is `extern', 'static' or 'inline' and the one
10189 after that is `template', we have a GNU extended explicit
10190 instantiation directive. */
10191 else if (cp_parser_allow_gnu_extensions_p (parser)
10192 && (token1.keyword == RID_EXTERN
10193 || token1.keyword == RID_STATIC
10194 || token1.keyword == RID_INLINE)
10195 && token2.keyword == RID_TEMPLATE)
10196 cp_parser_explicit_instantiation (parser);
10197 /* If the next token is `namespace', check for a named or unnamed
10198 namespace definition. */
10199 else if (token1.keyword == RID_NAMESPACE
10200 && (/* A named namespace definition. */
10201 (token2.type == CPP_NAME
10202 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10203 != CPP_EQ))
10204 /* An unnamed namespace definition. */
10205 || token2.type == CPP_OPEN_BRACE
10206 || token2.keyword == RID_ATTRIBUTE))
10207 cp_parser_namespace_definition (parser);
10208 /* An inline (associated) namespace definition. */
10209 else if (token1.keyword == RID_INLINE
10210 && token2.keyword == RID_NAMESPACE)
10211 cp_parser_namespace_definition (parser);
10212 /* Objective-C++ declaration/definition. */
10213 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10214 cp_parser_objc_declaration (parser, NULL_TREE);
10215 else if (c_dialect_objc ()
10216 && token1.keyword == RID_ATTRIBUTE
10217 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10218 cp_parser_objc_declaration (parser, attributes);
10219 /* We must have either a block declaration or a function
10220 definition. */
10221 else
10222 /* Try to parse a block-declaration, or a function-definition. */
10223 cp_parser_block_declaration (parser, /*statement_p=*/false);
10225 /* Free any declarators allocated. */
10226 obstack_free (&declarator_obstack, p);
10229 /* Parse a block-declaration.
10231 block-declaration:
10232 simple-declaration
10233 asm-definition
10234 namespace-alias-definition
10235 using-declaration
10236 using-directive
10238 GNU Extension:
10240 block-declaration:
10241 __extension__ block-declaration
10243 C++0x Extension:
10245 block-declaration:
10246 static_assert-declaration
10248 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10249 part of a declaration-statement. */
10251 static void
10252 cp_parser_block_declaration (cp_parser *parser,
10253 bool statement_p)
10255 cp_token *token1;
10256 int saved_pedantic;
10258 /* Check for the `__extension__' keyword. */
10259 if (cp_parser_extension_opt (parser, &saved_pedantic))
10261 /* Parse the qualified declaration. */
10262 cp_parser_block_declaration (parser, statement_p);
10263 /* Restore the PEDANTIC flag. */
10264 pedantic = saved_pedantic;
10266 return;
10269 /* Peek at the next token to figure out which kind of declaration is
10270 present. */
10271 token1 = cp_lexer_peek_token (parser->lexer);
10273 /* If the next keyword is `asm', we have an asm-definition. */
10274 if (token1->keyword == RID_ASM)
10276 if (statement_p)
10277 cp_parser_commit_to_tentative_parse (parser);
10278 cp_parser_asm_definition (parser);
10280 /* If the next keyword is `namespace', we have a
10281 namespace-alias-definition. */
10282 else if (token1->keyword == RID_NAMESPACE)
10283 cp_parser_namespace_alias_definition (parser);
10284 /* If the next keyword is `using', we have a
10285 using-declaration, a using-directive, or an alias-declaration. */
10286 else if (token1->keyword == RID_USING)
10288 cp_token *token2;
10290 if (statement_p)
10291 cp_parser_commit_to_tentative_parse (parser);
10292 /* If the token after `using' is `namespace', then we have a
10293 using-directive. */
10294 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10295 if (token2->keyword == RID_NAMESPACE)
10296 cp_parser_using_directive (parser);
10297 /* If the second token after 'using' is '=', then we have an
10298 alias-declaration. */
10299 else if (cxx_dialect >= cxx0x
10300 && token2->type == CPP_NAME
10301 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10302 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10303 == RID_ATTRIBUTE)))
10304 cp_parser_alias_declaration (parser);
10305 /* Otherwise, it's a using-declaration. */
10306 else
10307 cp_parser_using_declaration (parser,
10308 /*access_declaration_p=*/false);
10310 /* If the next keyword is `__label__' we have a misplaced label
10311 declaration. */
10312 else if (token1->keyword == RID_LABEL)
10314 cp_lexer_consume_token (parser->lexer);
10315 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10316 cp_parser_skip_to_end_of_statement (parser);
10317 /* If the next token is now a `;', consume it. */
10318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10319 cp_lexer_consume_token (parser->lexer);
10321 /* If the next token is `static_assert' we have a static assertion. */
10322 else if (token1->keyword == RID_STATIC_ASSERT)
10323 cp_parser_static_assert (parser, /*member_p=*/false);
10324 /* Anything else must be a simple-declaration. */
10325 else
10326 cp_parser_simple_declaration (parser, !statement_p,
10327 /*maybe_range_for_decl*/NULL);
10330 /* Parse a simple-declaration.
10332 simple-declaration:
10333 decl-specifier-seq [opt] init-declarator-list [opt] ;
10335 init-declarator-list:
10336 init-declarator
10337 init-declarator-list , init-declarator
10339 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10340 function-definition as a simple-declaration.
10342 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10343 parsed declaration if it is an uninitialized single declarator not followed
10344 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10345 if present, will not be consumed. */
10347 static void
10348 cp_parser_simple_declaration (cp_parser* parser,
10349 bool function_definition_allowed_p,
10350 tree *maybe_range_for_decl)
10352 cp_decl_specifier_seq decl_specifiers;
10353 int declares_class_or_enum;
10354 bool saw_declarator;
10356 if (maybe_range_for_decl)
10357 *maybe_range_for_decl = NULL_TREE;
10359 /* Defer access checks until we know what is being declared; the
10360 checks for names appearing in the decl-specifier-seq should be
10361 done as if we were in the scope of the thing being declared. */
10362 push_deferring_access_checks (dk_deferred);
10364 /* Parse the decl-specifier-seq. We have to keep track of whether
10365 or not the decl-specifier-seq declares a named class or
10366 enumeration type, since that is the only case in which the
10367 init-declarator-list is allowed to be empty.
10369 [dcl.dcl]
10371 In a simple-declaration, the optional init-declarator-list can be
10372 omitted only when declaring a class or enumeration, that is when
10373 the decl-specifier-seq contains either a class-specifier, an
10374 elaborated-type-specifier, or an enum-specifier. */
10375 cp_parser_decl_specifier_seq (parser,
10376 CP_PARSER_FLAGS_OPTIONAL,
10377 &decl_specifiers,
10378 &declares_class_or_enum);
10379 /* We no longer need to defer access checks. */
10380 stop_deferring_access_checks ();
10382 /* In a block scope, a valid declaration must always have a
10383 decl-specifier-seq. By not trying to parse declarators, we can
10384 resolve the declaration/expression ambiguity more quickly. */
10385 if (!function_definition_allowed_p
10386 && !decl_specifiers.any_specifiers_p)
10388 cp_parser_error (parser, "expected declaration");
10389 goto done;
10392 /* If the next two tokens are both identifiers, the code is
10393 erroneous. The usual cause of this situation is code like:
10395 T t;
10397 where "T" should name a type -- but does not. */
10398 if (!decl_specifiers.any_type_specifiers_p
10399 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10401 /* If parsing tentatively, we should commit; we really are
10402 looking at a declaration. */
10403 cp_parser_commit_to_tentative_parse (parser);
10404 /* Give up. */
10405 goto done;
10408 /* If we have seen at least one decl-specifier, and the next token
10409 is not a parenthesis, then we must be looking at a declaration.
10410 (After "int (" we might be looking at a functional cast.) */
10411 if (decl_specifiers.any_specifiers_p
10412 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10413 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10414 && !cp_parser_error_occurred (parser))
10415 cp_parser_commit_to_tentative_parse (parser);
10417 /* Keep going until we hit the `;' at the end of the simple
10418 declaration. */
10419 saw_declarator = false;
10420 while (cp_lexer_next_token_is_not (parser->lexer,
10421 CPP_SEMICOLON))
10423 cp_token *token;
10424 bool function_definition_p;
10425 tree decl;
10427 if (saw_declarator)
10429 /* If we are processing next declarator, coma is expected */
10430 token = cp_lexer_peek_token (parser->lexer);
10431 gcc_assert (token->type == CPP_COMMA);
10432 cp_lexer_consume_token (parser->lexer);
10433 if (maybe_range_for_decl)
10434 *maybe_range_for_decl = error_mark_node;
10436 else
10437 saw_declarator = true;
10439 /* Parse the init-declarator. */
10440 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10441 /*checks=*/NULL,
10442 function_definition_allowed_p,
10443 /*member_p=*/false,
10444 declares_class_or_enum,
10445 &function_definition_p,
10446 maybe_range_for_decl);
10447 /* If an error occurred while parsing tentatively, exit quickly.
10448 (That usually happens when in the body of a function; each
10449 statement is treated as a declaration-statement until proven
10450 otherwise.) */
10451 if (cp_parser_error_occurred (parser))
10452 goto done;
10453 /* Handle function definitions specially. */
10454 if (function_definition_p)
10456 /* If the next token is a `,', then we are probably
10457 processing something like:
10459 void f() {}, *p;
10461 which is erroneous. */
10462 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10464 cp_token *token = cp_lexer_peek_token (parser->lexer);
10465 error_at (token->location,
10466 "mixing"
10467 " declarations and function-definitions is forbidden");
10469 /* Otherwise, we're done with the list of declarators. */
10470 else
10472 pop_deferring_access_checks ();
10473 return;
10476 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10477 *maybe_range_for_decl = decl;
10478 /* The next token should be either a `,' or a `;'. */
10479 token = cp_lexer_peek_token (parser->lexer);
10480 /* If it's a `,', there are more declarators to come. */
10481 if (token->type == CPP_COMMA)
10482 /* will be consumed next time around */;
10483 /* If it's a `;', we are done. */
10484 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10485 break;
10486 /* Anything else is an error. */
10487 else
10489 /* If we have already issued an error message we don't need
10490 to issue another one. */
10491 if (decl != error_mark_node
10492 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10493 cp_parser_error (parser, "expected %<,%> or %<;%>");
10494 /* Skip tokens until we reach the end of the statement. */
10495 cp_parser_skip_to_end_of_statement (parser);
10496 /* If the next token is now a `;', consume it. */
10497 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10498 cp_lexer_consume_token (parser->lexer);
10499 goto done;
10501 /* After the first time around, a function-definition is not
10502 allowed -- even if it was OK at first. For example:
10504 int i, f() {}
10506 is not valid. */
10507 function_definition_allowed_p = false;
10510 /* Issue an error message if no declarators are present, and the
10511 decl-specifier-seq does not itself declare a class or
10512 enumeration. */
10513 if (!saw_declarator)
10515 if (cp_parser_declares_only_class_p (parser))
10516 shadow_tag (&decl_specifiers);
10517 /* Perform any deferred access checks. */
10518 perform_deferred_access_checks (tf_warning_or_error);
10521 /* Consume the `;'. */
10522 if (!maybe_range_for_decl)
10523 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10525 done:
10526 pop_deferring_access_checks ();
10529 /* Parse a decl-specifier-seq.
10531 decl-specifier-seq:
10532 decl-specifier-seq [opt] decl-specifier
10534 decl-specifier:
10535 storage-class-specifier
10536 type-specifier
10537 function-specifier
10538 friend
10539 typedef
10541 GNU Extension:
10543 decl-specifier:
10544 attributes
10546 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10548 The parser flags FLAGS is used to control type-specifier parsing.
10550 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10551 flags:
10553 1: one of the decl-specifiers is an elaborated-type-specifier
10554 (i.e., a type declaration)
10555 2: one of the decl-specifiers is an enum-specifier or a
10556 class-specifier (i.e., a type definition)
10560 static void
10561 cp_parser_decl_specifier_seq (cp_parser* parser,
10562 cp_parser_flags flags,
10563 cp_decl_specifier_seq *decl_specs,
10564 int* declares_class_or_enum)
10566 bool constructor_possible_p = !parser->in_declarator_p;
10567 cp_token *start_token = NULL;
10568 cp_decl_spec ds;
10570 /* Clear DECL_SPECS. */
10571 clear_decl_specs (decl_specs);
10573 /* Assume no class or enumeration type is declared. */
10574 *declares_class_or_enum = 0;
10576 /* Keep reading specifiers until there are no more to read. */
10577 while (true)
10579 bool constructor_p;
10580 bool found_decl_spec;
10581 cp_token *token;
10582 ds = ds_last;
10584 /* Peek at the next token. */
10585 token = cp_lexer_peek_token (parser->lexer);
10587 /* Save the first token of the decl spec list for error
10588 reporting. */
10589 if (!start_token)
10590 start_token = token;
10591 /* Handle attributes. */
10592 if (token->keyword == RID_ATTRIBUTE)
10594 /* Parse the attributes. */
10595 decl_specs->attributes
10596 = chainon (decl_specs->attributes,
10597 cp_parser_attributes_opt (parser));
10598 if (decl_specs->locations[ds_attribute] == 0)
10599 decl_specs->locations[ds_attribute] = token->location;
10600 continue;
10602 /* Assume we will find a decl-specifier keyword. */
10603 found_decl_spec = true;
10604 /* If the next token is an appropriate keyword, we can simply
10605 add it to the list. */
10606 switch (token->keyword)
10608 /* decl-specifier:
10609 friend
10610 constexpr */
10611 case RID_FRIEND:
10612 if (!at_class_scope_p ())
10614 error_at (token->location, "%<friend%> used outside of class");
10615 cp_lexer_purge_token (parser->lexer);
10617 else
10619 ds = ds_friend;
10620 /* Consume the token. */
10621 cp_lexer_consume_token (parser->lexer);
10623 break;
10625 case RID_CONSTEXPR:
10626 ds = ds_constexpr;
10627 cp_lexer_consume_token (parser->lexer);
10628 break;
10630 /* function-specifier:
10631 inline
10632 virtual
10633 explicit */
10634 case RID_INLINE:
10635 case RID_VIRTUAL:
10636 case RID_EXPLICIT:
10637 cp_parser_function_specifier_opt (parser, decl_specs);
10638 break;
10640 /* decl-specifier:
10641 typedef */
10642 case RID_TYPEDEF:
10643 ds = ds_typedef;
10644 /* Consume the token. */
10645 cp_lexer_consume_token (parser->lexer);
10646 /* A constructor declarator cannot appear in a typedef. */
10647 constructor_possible_p = false;
10648 /* The "typedef" keyword can only occur in a declaration; we
10649 may as well commit at this point. */
10650 cp_parser_commit_to_tentative_parse (parser);
10652 if (decl_specs->storage_class != sc_none)
10653 decl_specs->conflicting_specifiers_p = true;
10654 break;
10656 /* storage-class-specifier:
10657 auto
10658 register
10659 static
10660 extern
10661 mutable
10663 GNU Extension:
10664 thread */
10665 case RID_AUTO:
10666 if (cxx_dialect == cxx98)
10668 /* Consume the token. */
10669 cp_lexer_consume_token (parser->lexer);
10671 /* Complain about `auto' as a storage specifier, if
10672 we're complaining about C++0x compatibility. */
10673 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10674 " changes meaning in C++11; please remove it");
10676 /* Set the storage class anyway. */
10677 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10678 token->location);
10680 else
10681 /* C++0x auto type-specifier. */
10682 found_decl_spec = false;
10683 break;
10685 case RID_REGISTER:
10686 case RID_STATIC:
10687 case RID_EXTERN:
10688 case RID_MUTABLE:
10689 /* Consume the token. */
10690 cp_lexer_consume_token (parser->lexer);
10691 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10692 token->location);
10693 break;
10694 case RID_THREAD:
10695 /* Consume the token. */
10696 ds = ds_thread;
10697 cp_lexer_consume_token (parser->lexer);
10698 break;
10700 default:
10701 /* We did not yet find a decl-specifier yet. */
10702 found_decl_spec = false;
10703 break;
10706 if (found_decl_spec
10707 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10708 && token->keyword != RID_CONSTEXPR)
10709 error ("decl-specifier invalid in condition");
10711 if (ds != ds_last)
10712 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
10714 /* Constructors are a special case. The `S' in `S()' is not a
10715 decl-specifier; it is the beginning of the declarator. */
10716 constructor_p
10717 = (!found_decl_spec
10718 && constructor_possible_p
10719 && (cp_parser_constructor_declarator_p
10720 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10722 /* If we don't have a DECL_SPEC yet, then we must be looking at
10723 a type-specifier. */
10724 if (!found_decl_spec && !constructor_p)
10726 int decl_spec_declares_class_or_enum;
10727 bool is_cv_qualifier;
10728 tree type_spec;
10730 type_spec
10731 = cp_parser_type_specifier (parser, flags,
10732 decl_specs,
10733 /*is_declaration=*/true,
10734 &decl_spec_declares_class_or_enum,
10735 &is_cv_qualifier);
10736 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10738 /* If this type-specifier referenced a user-defined type
10739 (a typedef, class-name, etc.), then we can't allow any
10740 more such type-specifiers henceforth.
10742 [dcl.spec]
10744 The longest sequence of decl-specifiers that could
10745 possibly be a type name is taken as the
10746 decl-specifier-seq of a declaration. The sequence shall
10747 be self-consistent as described below.
10749 [dcl.type]
10751 As a general rule, at most one type-specifier is allowed
10752 in the complete decl-specifier-seq of a declaration. The
10753 only exceptions are the following:
10755 -- const or volatile can be combined with any other
10756 type-specifier.
10758 -- signed or unsigned can be combined with char, long,
10759 short, or int.
10761 -- ..
10763 Example:
10765 typedef char* Pc;
10766 void g (const int Pc);
10768 Here, Pc is *not* part of the decl-specifier seq; it's
10769 the declarator. Therefore, once we see a type-specifier
10770 (other than a cv-qualifier), we forbid any additional
10771 user-defined types. We *do* still allow things like `int
10772 int' to be considered a decl-specifier-seq, and issue the
10773 error message later. */
10774 if (type_spec && !is_cv_qualifier)
10775 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10776 /* A constructor declarator cannot follow a type-specifier. */
10777 if (type_spec)
10779 constructor_possible_p = false;
10780 found_decl_spec = true;
10781 if (!is_cv_qualifier)
10782 decl_specs->any_type_specifiers_p = true;
10786 /* If we still do not have a DECL_SPEC, then there are no more
10787 decl-specifiers. */
10788 if (!found_decl_spec)
10789 break;
10791 decl_specs->any_specifiers_p = true;
10792 /* After we see one decl-specifier, further decl-specifiers are
10793 always optional. */
10794 flags |= CP_PARSER_FLAGS_OPTIONAL;
10797 /* Don't allow a friend specifier with a class definition. */
10798 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10799 && (*declares_class_or_enum & 2))
10800 error_at (decl_specs->locations[ds_friend],
10801 "class definition may not be declared a friend");
10804 /* Parse an (optional) storage-class-specifier.
10806 storage-class-specifier:
10807 auto
10808 register
10809 static
10810 extern
10811 mutable
10813 GNU Extension:
10815 storage-class-specifier:
10816 thread
10818 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10820 static tree
10821 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10823 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10825 case RID_AUTO:
10826 if (cxx_dialect != cxx98)
10827 return NULL_TREE;
10828 /* Fall through for C++98. */
10830 case RID_REGISTER:
10831 case RID_STATIC:
10832 case RID_EXTERN:
10833 case RID_MUTABLE:
10834 case RID_THREAD:
10835 /* Consume the token. */
10836 return cp_lexer_consume_token (parser->lexer)->u.value;
10838 default:
10839 return NULL_TREE;
10843 /* Parse an (optional) function-specifier.
10845 function-specifier:
10846 inline
10847 virtual
10848 explicit
10850 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10851 Updates DECL_SPECS, if it is non-NULL. */
10853 static tree
10854 cp_parser_function_specifier_opt (cp_parser* parser,
10855 cp_decl_specifier_seq *decl_specs)
10857 cp_token *token = cp_lexer_peek_token (parser->lexer);
10858 switch (token->keyword)
10860 case RID_INLINE:
10861 set_and_check_decl_spec_loc (decl_specs, ds_inline, token->location);
10862 break;
10864 case RID_VIRTUAL:
10865 /* 14.5.2.3 [temp.mem]
10867 A member function template shall not be virtual. */
10868 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10869 error_at (token->location, "templates may not be %<virtual%>");
10870 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token->location);
10871 break;
10873 case RID_EXPLICIT:
10874 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token->location);
10875 break;
10877 default:
10878 return NULL_TREE;
10881 /* Consume the token. */
10882 return cp_lexer_consume_token (parser->lexer)->u.value;
10885 /* Parse a linkage-specification.
10887 linkage-specification:
10888 extern string-literal { declaration-seq [opt] }
10889 extern string-literal declaration */
10891 static void
10892 cp_parser_linkage_specification (cp_parser* parser)
10894 tree linkage;
10896 /* Look for the `extern' keyword. */
10897 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10899 /* Look for the string-literal. */
10900 linkage = cp_parser_string_literal (parser, false, false);
10902 /* Transform the literal into an identifier. If the literal is a
10903 wide-character string, or contains embedded NULs, then we can't
10904 handle it as the user wants. */
10905 if (strlen (TREE_STRING_POINTER (linkage))
10906 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10908 cp_parser_error (parser, "invalid linkage-specification");
10909 /* Assume C++ linkage. */
10910 linkage = lang_name_cplusplus;
10912 else
10913 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10915 /* We're now using the new linkage. */
10916 push_lang_context (linkage);
10918 /* If the next token is a `{', then we're using the first
10919 production. */
10920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10922 /* Consume the `{' token. */
10923 cp_lexer_consume_token (parser->lexer);
10924 /* Parse the declarations. */
10925 cp_parser_declaration_seq_opt (parser);
10926 /* Look for the closing `}'. */
10927 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10929 /* Otherwise, there's just one declaration. */
10930 else
10932 bool saved_in_unbraced_linkage_specification_p;
10934 saved_in_unbraced_linkage_specification_p
10935 = parser->in_unbraced_linkage_specification_p;
10936 parser->in_unbraced_linkage_specification_p = true;
10937 cp_parser_declaration (parser);
10938 parser->in_unbraced_linkage_specification_p
10939 = saved_in_unbraced_linkage_specification_p;
10942 /* We're done with the linkage-specification. */
10943 pop_lang_context ();
10946 /* Parse a static_assert-declaration.
10948 static_assert-declaration:
10949 static_assert ( constant-expression , string-literal ) ;
10951 If MEMBER_P, this static_assert is a class member. */
10953 static void
10954 cp_parser_static_assert(cp_parser *parser, bool member_p)
10956 tree condition;
10957 tree message;
10958 cp_token *token;
10959 location_t saved_loc;
10960 bool dummy;
10962 /* Peek at the `static_assert' token so we can keep track of exactly
10963 where the static assertion started. */
10964 token = cp_lexer_peek_token (parser->lexer);
10965 saved_loc = token->location;
10967 /* Look for the `static_assert' keyword. */
10968 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10969 RT_STATIC_ASSERT))
10970 return;
10972 /* We know we are in a static assertion; commit to any tentative
10973 parse. */
10974 if (cp_parser_parsing_tentatively (parser))
10975 cp_parser_commit_to_tentative_parse (parser);
10977 /* Parse the `(' starting the static assertion condition. */
10978 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10980 /* Parse the constant-expression. Allow a non-constant expression
10981 here in order to give better diagnostics in finish_static_assert. */
10982 condition =
10983 cp_parser_constant_expression (parser,
10984 /*allow_non_constant_p=*/true,
10985 /*non_constant_p=*/&dummy);
10987 /* Parse the separating `,'. */
10988 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10990 /* Parse the string-literal message. */
10991 message = cp_parser_string_literal (parser,
10992 /*translate=*/false,
10993 /*wide_ok=*/true);
10995 /* A `)' completes the static assertion. */
10996 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10997 cp_parser_skip_to_closing_parenthesis (parser,
10998 /*recovering=*/true,
10999 /*or_comma=*/false,
11000 /*consume_paren=*/true);
11002 /* A semicolon terminates the declaration. */
11003 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11005 /* Complete the static assertion, which may mean either processing
11006 the static assert now or saving it for template instantiation. */
11007 finish_static_assert (condition, message, saved_loc, member_p);
11010 /* Parse a `decltype' type. Returns the type.
11012 simple-type-specifier:
11013 decltype ( expression ) */
11015 static tree
11016 cp_parser_decltype (cp_parser *parser)
11018 tree expr;
11019 bool id_expression_or_member_access_p = false;
11020 const char *saved_message;
11021 bool saved_integral_constant_expression_p;
11022 bool saved_non_integral_constant_expression_p;
11023 cp_token *id_expr_start_token;
11024 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11026 if (start_token->type == CPP_DECLTYPE)
11028 /* Already parsed. */
11029 cp_lexer_consume_token (parser->lexer);
11030 return start_token->u.value;
11033 /* Look for the `decltype' token. */
11034 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11035 return error_mark_node;
11037 /* Types cannot be defined in a `decltype' expression. Save away the
11038 old message. */
11039 saved_message = parser->type_definition_forbidden_message;
11041 /* And create the new one. */
11042 parser->type_definition_forbidden_message
11043 = G_("types may not be defined in %<decltype%> expressions");
11045 /* The restrictions on constant-expressions do not apply inside
11046 decltype expressions. */
11047 saved_integral_constant_expression_p
11048 = parser->integral_constant_expression_p;
11049 saved_non_integral_constant_expression_p
11050 = parser->non_integral_constant_expression_p;
11051 parser->integral_constant_expression_p = false;
11053 /* Do not actually evaluate the expression. */
11054 ++cp_unevaluated_operand;
11056 /* Do not warn about problems with the expression. */
11057 ++c_inhibit_evaluation_warnings;
11059 /* Parse the opening `('. */
11060 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11061 return error_mark_node;
11063 /* First, try parsing an id-expression. */
11064 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11065 cp_parser_parse_tentatively (parser);
11066 expr = cp_parser_id_expression (parser,
11067 /*template_keyword_p=*/false,
11068 /*check_dependency_p=*/true,
11069 /*template_p=*/NULL,
11070 /*declarator_p=*/false,
11071 /*optional_p=*/false);
11073 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11075 bool non_integral_constant_expression_p = false;
11076 tree id_expression = expr;
11077 cp_id_kind idk;
11078 const char *error_msg;
11080 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11081 /* Lookup the name we got back from the id-expression. */
11082 expr = cp_parser_lookup_name (parser, expr,
11083 none_type,
11084 /*is_template=*/false,
11085 /*is_namespace=*/false,
11086 /*check_dependency=*/true,
11087 /*ambiguous_decls=*/NULL,
11088 id_expr_start_token->location);
11090 if (expr
11091 && expr != error_mark_node
11092 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11093 && TREE_CODE (expr) != TYPE_DECL
11094 && (TREE_CODE (expr) != BIT_NOT_EXPR
11095 || !TYPE_P (TREE_OPERAND (expr, 0)))
11096 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11098 /* Complete lookup of the id-expression. */
11099 expr = (finish_id_expression
11100 (id_expression, expr, parser->scope, &idk,
11101 /*integral_constant_expression_p=*/false,
11102 /*allow_non_integral_constant_expression_p=*/true,
11103 &non_integral_constant_expression_p,
11104 /*template_p=*/false,
11105 /*done=*/true,
11106 /*address_p=*/false,
11107 /*template_arg_p=*/false,
11108 &error_msg,
11109 id_expr_start_token->location));
11111 if (expr == error_mark_node)
11112 /* We found an id-expression, but it was something that we
11113 should not have found. This is an error, not something
11114 we can recover from, so note that we found an
11115 id-expression and we'll recover as gracefully as
11116 possible. */
11117 id_expression_or_member_access_p = true;
11120 if (expr
11121 && expr != error_mark_node
11122 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11123 /* We have an id-expression. */
11124 id_expression_or_member_access_p = true;
11127 if (!id_expression_or_member_access_p)
11129 /* Abort the id-expression parse. */
11130 cp_parser_abort_tentative_parse (parser);
11132 /* Parsing tentatively, again. */
11133 cp_parser_parse_tentatively (parser);
11135 /* Parse a class member access. */
11136 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11137 /*cast_p=*/false,
11138 /*member_access_only_p=*/true, NULL);
11140 if (expr
11141 && expr != error_mark_node
11142 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11143 /* We have an id-expression. */
11144 id_expression_or_member_access_p = true;
11147 if (id_expression_or_member_access_p)
11148 /* We have parsed the complete id-expression or member access. */
11149 cp_parser_parse_definitely (parser);
11150 else
11152 bool saved_greater_than_is_operator_p;
11154 /* Abort our attempt to parse an id-expression or member access
11155 expression. */
11156 cp_parser_abort_tentative_parse (parser);
11158 /* Within a parenthesized expression, a `>' token is always
11159 the greater-than operator. */
11160 saved_greater_than_is_operator_p
11161 = parser->greater_than_is_operator_p;
11162 parser->greater_than_is_operator_p = true;
11164 /* Parse a full expression. */
11165 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11167 /* The `>' token might be the end of a template-id or
11168 template-parameter-list now. */
11169 parser->greater_than_is_operator_p
11170 = saved_greater_than_is_operator_p;
11173 /* Go back to evaluating expressions. */
11174 --cp_unevaluated_operand;
11175 --c_inhibit_evaluation_warnings;
11177 /* Restore the old message and the integral constant expression
11178 flags. */
11179 parser->type_definition_forbidden_message = saved_message;
11180 parser->integral_constant_expression_p
11181 = saved_integral_constant_expression_p;
11182 parser->non_integral_constant_expression_p
11183 = saved_non_integral_constant_expression_p;
11185 /* Parse to the closing `)'. */
11186 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11188 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11189 /*consume_paren=*/true);
11190 return error_mark_node;
11193 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11194 tf_warning_or_error);
11196 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11197 it again. */
11198 start_token->type = CPP_DECLTYPE;
11199 start_token->u.value = expr;
11200 start_token->keyword = RID_MAX;
11201 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11203 return expr;
11206 /* Special member functions [gram.special] */
11208 /* Parse a conversion-function-id.
11210 conversion-function-id:
11211 operator conversion-type-id
11213 Returns an IDENTIFIER_NODE representing the operator. */
11215 static tree
11216 cp_parser_conversion_function_id (cp_parser* parser)
11218 tree type;
11219 tree saved_scope;
11220 tree saved_qualifying_scope;
11221 tree saved_object_scope;
11222 tree pushed_scope = NULL_TREE;
11224 /* Look for the `operator' token. */
11225 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11226 return error_mark_node;
11227 /* When we parse the conversion-type-id, the current scope will be
11228 reset. However, we need that information in able to look up the
11229 conversion function later, so we save it here. */
11230 saved_scope = parser->scope;
11231 saved_qualifying_scope = parser->qualifying_scope;
11232 saved_object_scope = parser->object_scope;
11233 /* We must enter the scope of the class so that the names of
11234 entities declared within the class are available in the
11235 conversion-type-id. For example, consider:
11237 struct S {
11238 typedef int I;
11239 operator I();
11242 S::operator I() { ... }
11244 In order to see that `I' is a type-name in the definition, we
11245 must be in the scope of `S'. */
11246 if (saved_scope)
11247 pushed_scope = push_scope (saved_scope);
11248 /* Parse the conversion-type-id. */
11249 type = cp_parser_conversion_type_id (parser);
11250 /* Leave the scope of the class, if any. */
11251 if (pushed_scope)
11252 pop_scope (pushed_scope);
11253 /* Restore the saved scope. */
11254 parser->scope = saved_scope;
11255 parser->qualifying_scope = saved_qualifying_scope;
11256 parser->object_scope = saved_object_scope;
11257 /* If the TYPE is invalid, indicate failure. */
11258 if (type == error_mark_node)
11259 return error_mark_node;
11260 return mangle_conv_op_name_for_type (type);
11263 /* Parse a conversion-type-id:
11265 conversion-type-id:
11266 type-specifier-seq conversion-declarator [opt]
11268 Returns the TYPE specified. */
11270 static tree
11271 cp_parser_conversion_type_id (cp_parser* parser)
11273 tree attributes;
11274 cp_decl_specifier_seq type_specifiers;
11275 cp_declarator *declarator;
11276 tree type_specified;
11278 /* Parse the attributes. */
11279 attributes = cp_parser_attributes_opt (parser);
11280 /* Parse the type-specifiers. */
11281 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11282 /*is_trailing_return=*/false,
11283 &type_specifiers);
11284 /* If that didn't work, stop. */
11285 if (type_specifiers.type == error_mark_node)
11286 return error_mark_node;
11287 /* Parse the conversion-declarator. */
11288 declarator = cp_parser_conversion_declarator_opt (parser);
11290 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11291 /*initialized=*/0, &attributes);
11292 if (attributes)
11293 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11295 /* Don't give this error when parsing tentatively. This happens to
11296 work because we always parse this definitively once. */
11297 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11298 && type_uses_auto (type_specified))
11300 if (cxx_dialect < cxx1y)
11302 error ("invalid use of %<auto%> in conversion operator");
11303 return error_mark_node;
11305 else if (template_parm_scope_p ())
11306 warning (0, "use of %<auto%> in member template "
11307 "conversion operator can never be deduced");
11310 return type_specified;
11313 /* Parse an (optional) conversion-declarator.
11315 conversion-declarator:
11316 ptr-operator conversion-declarator [opt]
11320 static cp_declarator *
11321 cp_parser_conversion_declarator_opt (cp_parser* parser)
11323 enum tree_code code;
11324 tree class_type;
11325 cp_cv_quals cv_quals;
11327 /* We don't know if there's a ptr-operator next, or not. */
11328 cp_parser_parse_tentatively (parser);
11329 /* Try the ptr-operator. */
11330 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11331 /* If it worked, look for more conversion-declarators. */
11332 if (cp_parser_parse_definitely (parser))
11334 cp_declarator *declarator;
11336 /* Parse another optional declarator. */
11337 declarator = cp_parser_conversion_declarator_opt (parser);
11339 return cp_parser_make_indirect_declarator
11340 (code, class_type, cv_quals, declarator);
11343 return NULL;
11346 /* Parse an (optional) ctor-initializer.
11348 ctor-initializer:
11349 : mem-initializer-list
11351 Returns TRUE iff the ctor-initializer was actually present. */
11353 static bool
11354 cp_parser_ctor_initializer_opt (cp_parser* parser)
11356 /* If the next token is not a `:', then there is no
11357 ctor-initializer. */
11358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11360 /* Do default initialization of any bases and members. */
11361 if (DECL_CONSTRUCTOR_P (current_function_decl))
11362 finish_mem_initializers (NULL_TREE);
11364 return false;
11367 /* Consume the `:' token. */
11368 cp_lexer_consume_token (parser->lexer);
11369 /* And the mem-initializer-list. */
11370 cp_parser_mem_initializer_list (parser);
11372 return true;
11375 /* Parse a mem-initializer-list.
11377 mem-initializer-list:
11378 mem-initializer ... [opt]
11379 mem-initializer ... [opt] , mem-initializer-list */
11381 static void
11382 cp_parser_mem_initializer_list (cp_parser* parser)
11384 tree mem_initializer_list = NULL_TREE;
11385 tree target_ctor = error_mark_node;
11386 cp_token *token = cp_lexer_peek_token (parser->lexer);
11388 /* Let the semantic analysis code know that we are starting the
11389 mem-initializer-list. */
11390 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11391 error_at (token->location,
11392 "only constructors take member initializers");
11394 /* Loop through the list. */
11395 while (true)
11397 tree mem_initializer;
11399 token = cp_lexer_peek_token (parser->lexer);
11400 /* Parse the mem-initializer. */
11401 mem_initializer = cp_parser_mem_initializer (parser);
11402 /* If the next token is a `...', we're expanding member initializers. */
11403 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11405 /* Consume the `...'. */
11406 cp_lexer_consume_token (parser->lexer);
11408 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11409 can be expanded but members cannot. */
11410 if (mem_initializer != error_mark_node
11411 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11413 error_at (token->location,
11414 "cannot expand initializer for member %<%D%>",
11415 TREE_PURPOSE (mem_initializer));
11416 mem_initializer = error_mark_node;
11419 /* Construct the pack expansion type. */
11420 if (mem_initializer != error_mark_node)
11421 mem_initializer = make_pack_expansion (mem_initializer);
11423 if (target_ctor != error_mark_node
11424 && mem_initializer != error_mark_node)
11426 error ("mem-initializer for %qD follows constructor delegation",
11427 TREE_PURPOSE (mem_initializer));
11428 mem_initializer = error_mark_node;
11430 /* Look for a target constructor. */
11431 if (mem_initializer != error_mark_node
11432 && TYPE_P (TREE_PURPOSE (mem_initializer))
11433 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11435 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11436 if (mem_initializer_list)
11438 error ("constructor delegation follows mem-initializer for %qD",
11439 TREE_PURPOSE (mem_initializer_list));
11440 mem_initializer = error_mark_node;
11442 target_ctor = mem_initializer;
11444 /* Add it to the list, unless it was erroneous. */
11445 if (mem_initializer != error_mark_node)
11447 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11448 mem_initializer_list = mem_initializer;
11450 /* If the next token is not a `,', we're done. */
11451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11452 break;
11453 /* Consume the `,' token. */
11454 cp_lexer_consume_token (parser->lexer);
11457 /* Perform semantic analysis. */
11458 if (DECL_CONSTRUCTOR_P (current_function_decl))
11459 finish_mem_initializers (mem_initializer_list);
11462 /* Parse a mem-initializer.
11464 mem-initializer:
11465 mem-initializer-id ( expression-list [opt] )
11466 mem-initializer-id braced-init-list
11468 GNU extension:
11470 mem-initializer:
11471 ( expression-list [opt] )
11473 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11474 class) or FIELD_DECL (for a non-static data member) to initialize;
11475 the TREE_VALUE is the expression-list. An empty initialization
11476 list is represented by void_list_node. */
11478 static tree
11479 cp_parser_mem_initializer (cp_parser* parser)
11481 tree mem_initializer_id;
11482 tree expression_list;
11483 tree member;
11484 cp_token *token = cp_lexer_peek_token (parser->lexer);
11486 /* Find out what is being initialized. */
11487 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11489 permerror (token->location,
11490 "anachronistic old-style base class initializer");
11491 mem_initializer_id = NULL_TREE;
11493 else
11495 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11496 if (mem_initializer_id == error_mark_node)
11497 return mem_initializer_id;
11499 member = expand_member_init (mem_initializer_id);
11500 if (member && !DECL_P (member))
11501 in_base_initializer = 1;
11503 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11505 bool expr_non_constant_p;
11506 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11507 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11508 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11509 expression_list = build_tree_list (NULL_TREE, expression_list);
11511 else
11513 VEC(tree,gc)* vec;
11514 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11515 /*cast_p=*/false,
11516 /*allow_expansion_p=*/true,
11517 /*non_constant_p=*/NULL);
11518 if (vec == NULL)
11519 return error_mark_node;
11520 expression_list = build_tree_list_vec (vec);
11521 release_tree_vector (vec);
11524 if (expression_list == error_mark_node)
11525 return error_mark_node;
11526 if (!expression_list)
11527 expression_list = void_type_node;
11529 in_base_initializer = 0;
11531 return member ? build_tree_list (member, expression_list) : error_mark_node;
11534 /* Parse a mem-initializer-id.
11536 mem-initializer-id:
11537 :: [opt] nested-name-specifier [opt] class-name
11538 identifier
11540 Returns a TYPE indicating the class to be initializer for the first
11541 production. Returns an IDENTIFIER_NODE indicating the data member
11542 to be initialized for the second production. */
11544 static tree
11545 cp_parser_mem_initializer_id (cp_parser* parser)
11547 bool global_scope_p;
11548 bool nested_name_specifier_p;
11549 bool template_p = false;
11550 tree id;
11552 cp_token *token = cp_lexer_peek_token (parser->lexer);
11554 /* `typename' is not allowed in this context ([temp.res]). */
11555 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11557 error_at (token->location,
11558 "keyword %<typename%> not allowed in this context (a qualified "
11559 "member initializer is implicitly a type)");
11560 cp_lexer_consume_token (parser->lexer);
11562 /* Look for the optional `::' operator. */
11563 global_scope_p
11564 = (cp_parser_global_scope_opt (parser,
11565 /*current_scope_valid_p=*/false)
11566 != NULL_TREE);
11567 /* Look for the optional nested-name-specifier. The simplest way to
11568 implement:
11570 [temp.res]
11572 The keyword `typename' is not permitted in a base-specifier or
11573 mem-initializer; in these contexts a qualified name that
11574 depends on a template-parameter is implicitly assumed to be a
11575 type name.
11577 is to assume that we have seen the `typename' keyword at this
11578 point. */
11579 nested_name_specifier_p
11580 = (cp_parser_nested_name_specifier_opt (parser,
11581 /*typename_keyword_p=*/true,
11582 /*check_dependency_p=*/true,
11583 /*type_p=*/true,
11584 /*is_declaration=*/true)
11585 != NULL_TREE);
11586 if (nested_name_specifier_p)
11587 template_p = cp_parser_optional_template_keyword (parser);
11588 /* If there is a `::' operator or a nested-name-specifier, then we
11589 are definitely looking for a class-name. */
11590 if (global_scope_p || nested_name_specifier_p)
11591 return cp_parser_class_name (parser,
11592 /*typename_keyword_p=*/true,
11593 /*template_keyword_p=*/template_p,
11594 typename_type,
11595 /*check_dependency_p=*/true,
11596 /*class_head_p=*/false,
11597 /*is_declaration=*/true);
11598 /* Otherwise, we could also be looking for an ordinary identifier. */
11599 cp_parser_parse_tentatively (parser);
11600 /* Try a class-name. */
11601 id = cp_parser_class_name (parser,
11602 /*typename_keyword_p=*/true,
11603 /*template_keyword_p=*/false,
11604 none_type,
11605 /*check_dependency_p=*/true,
11606 /*class_head_p=*/false,
11607 /*is_declaration=*/true);
11608 /* If we found one, we're done. */
11609 if (cp_parser_parse_definitely (parser))
11610 return id;
11611 /* Otherwise, look for an ordinary identifier. */
11612 return cp_parser_identifier (parser);
11615 /* Overloading [gram.over] */
11617 /* Parse an operator-function-id.
11619 operator-function-id:
11620 operator operator
11622 Returns an IDENTIFIER_NODE for the operator which is a
11623 human-readable spelling of the identifier, e.g., `operator +'. */
11625 static tree
11626 cp_parser_operator_function_id (cp_parser* parser)
11628 /* Look for the `operator' keyword. */
11629 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11630 return error_mark_node;
11631 /* And then the name of the operator itself. */
11632 return cp_parser_operator (parser);
11635 /* Return an identifier node for a user-defined literal operator.
11636 The suffix identifier is chained to the operator name identifier. */
11638 static tree
11639 cp_literal_operator_id (const char* name)
11641 tree identifier;
11642 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11643 + strlen (name) + 10);
11644 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11645 identifier = get_identifier (buffer);
11646 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11648 return identifier;
11651 /* Parse an operator.
11653 operator:
11654 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11655 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11656 || ++ -- , ->* -> () []
11658 GNU Extensions:
11660 operator:
11661 <? >? <?= >?=
11663 Returns an IDENTIFIER_NODE for the operator which is a
11664 human-readable spelling of the identifier, e.g., `operator +'. */
11666 static tree
11667 cp_parser_operator (cp_parser* parser)
11669 tree id = NULL_TREE;
11670 cp_token *token;
11672 /* Peek at the next token. */
11673 token = cp_lexer_peek_token (parser->lexer);
11674 /* Figure out which operator we have. */
11675 switch (token->type)
11677 case CPP_KEYWORD:
11679 enum tree_code op;
11681 /* The keyword should be either `new' or `delete'. */
11682 if (token->keyword == RID_NEW)
11683 op = NEW_EXPR;
11684 else if (token->keyword == RID_DELETE)
11685 op = DELETE_EXPR;
11686 else
11687 break;
11689 /* Consume the `new' or `delete' token. */
11690 cp_lexer_consume_token (parser->lexer);
11692 /* Peek at the next token. */
11693 token = cp_lexer_peek_token (parser->lexer);
11694 /* If it's a `[' token then this is the array variant of the
11695 operator. */
11696 if (token->type == CPP_OPEN_SQUARE)
11698 /* Consume the `[' token. */
11699 cp_lexer_consume_token (parser->lexer);
11700 /* Look for the `]' token. */
11701 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11702 id = ansi_opname (op == NEW_EXPR
11703 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11705 /* Otherwise, we have the non-array variant. */
11706 else
11707 id = ansi_opname (op);
11709 return id;
11712 case CPP_PLUS:
11713 id = ansi_opname (PLUS_EXPR);
11714 break;
11716 case CPP_MINUS:
11717 id = ansi_opname (MINUS_EXPR);
11718 break;
11720 case CPP_MULT:
11721 id = ansi_opname (MULT_EXPR);
11722 break;
11724 case CPP_DIV:
11725 id = ansi_opname (TRUNC_DIV_EXPR);
11726 break;
11728 case CPP_MOD:
11729 id = ansi_opname (TRUNC_MOD_EXPR);
11730 break;
11732 case CPP_XOR:
11733 id = ansi_opname (BIT_XOR_EXPR);
11734 break;
11736 case CPP_AND:
11737 id = ansi_opname (BIT_AND_EXPR);
11738 break;
11740 case CPP_OR:
11741 id = ansi_opname (BIT_IOR_EXPR);
11742 break;
11744 case CPP_COMPL:
11745 id = ansi_opname (BIT_NOT_EXPR);
11746 break;
11748 case CPP_NOT:
11749 id = ansi_opname (TRUTH_NOT_EXPR);
11750 break;
11752 case CPP_EQ:
11753 id = ansi_assopname (NOP_EXPR);
11754 break;
11756 case CPP_LESS:
11757 id = ansi_opname (LT_EXPR);
11758 break;
11760 case CPP_GREATER:
11761 id = ansi_opname (GT_EXPR);
11762 break;
11764 case CPP_PLUS_EQ:
11765 id = ansi_assopname (PLUS_EXPR);
11766 break;
11768 case CPP_MINUS_EQ:
11769 id = ansi_assopname (MINUS_EXPR);
11770 break;
11772 case CPP_MULT_EQ:
11773 id = ansi_assopname (MULT_EXPR);
11774 break;
11776 case CPP_DIV_EQ:
11777 id = ansi_assopname (TRUNC_DIV_EXPR);
11778 break;
11780 case CPP_MOD_EQ:
11781 id = ansi_assopname (TRUNC_MOD_EXPR);
11782 break;
11784 case CPP_XOR_EQ:
11785 id = ansi_assopname (BIT_XOR_EXPR);
11786 break;
11788 case CPP_AND_EQ:
11789 id = ansi_assopname (BIT_AND_EXPR);
11790 break;
11792 case CPP_OR_EQ:
11793 id = ansi_assopname (BIT_IOR_EXPR);
11794 break;
11796 case CPP_LSHIFT:
11797 id = ansi_opname (LSHIFT_EXPR);
11798 break;
11800 case CPP_RSHIFT:
11801 id = ansi_opname (RSHIFT_EXPR);
11802 break;
11804 case CPP_LSHIFT_EQ:
11805 id = ansi_assopname (LSHIFT_EXPR);
11806 break;
11808 case CPP_RSHIFT_EQ:
11809 id = ansi_assopname (RSHIFT_EXPR);
11810 break;
11812 case CPP_EQ_EQ:
11813 id = ansi_opname (EQ_EXPR);
11814 break;
11816 case CPP_NOT_EQ:
11817 id = ansi_opname (NE_EXPR);
11818 break;
11820 case CPP_LESS_EQ:
11821 id = ansi_opname (LE_EXPR);
11822 break;
11824 case CPP_GREATER_EQ:
11825 id = ansi_opname (GE_EXPR);
11826 break;
11828 case CPP_AND_AND:
11829 id = ansi_opname (TRUTH_ANDIF_EXPR);
11830 break;
11832 case CPP_OR_OR:
11833 id = ansi_opname (TRUTH_ORIF_EXPR);
11834 break;
11836 case CPP_PLUS_PLUS:
11837 id = ansi_opname (POSTINCREMENT_EXPR);
11838 break;
11840 case CPP_MINUS_MINUS:
11841 id = ansi_opname (PREDECREMENT_EXPR);
11842 break;
11844 case CPP_COMMA:
11845 id = ansi_opname (COMPOUND_EXPR);
11846 break;
11848 case CPP_DEREF_STAR:
11849 id = ansi_opname (MEMBER_REF);
11850 break;
11852 case CPP_DEREF:
11853 id = ansi_opname (COMPONENT_REF);
11854 break;
11856 case CPP_OPEN_PAREN:
11857 /* Consume the `('. */
11858 cp_lexer_consume_token (parser->lexer);
11859 /* Look for the matching `)'. */
11860 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11861 return ansi_opname (CALL_EXPR);
11863 case CPP_OPEN_SQUARE:
11864 /* Consume the `['. */
11865 cp_lexer_consume_token (parser->lexer);
11866 /* Look for the matching `]'. */
11867 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11868 return ansi_opname (ARRAY_REF);
11870 case CPP_STRING:
11871 if (cxx_dialect == cxx98)
11872 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11873 if (TREE_STRING_LENGTH (token->u.value) > 2)
11875 error ("expected empty string after %<operator%> keyword");
11876 return error_mark_node;
11878 /* Consume the string. */
11879 cp_lexer_consume_token (parser->lexer);
11880 /* Look for the suffix identifier. */
11881 token = cp_lexer_peek_token (parser->lexer);
11882 if (token->type == CPP_NAME)
11884 id = cp_parser_identifier (parser);
11885 if (id != error_mark_node)
11887 const char *name = IDENTIFIER_POINTER (id);
11888 return cp_literal_operator_id (name);
11891 else
11893 error ("expected suffix identifier");
11894 return error_mark_node;
11897 case CPP_STRING_USERDEF:
11898 error ("missing space between %<\"\"%> and suffix identifier");
11899 return error_mark_node;
11901 default:
11902 /* Anything else is an error. */
11903 break;
11906 /* If we have selected an identifier, we need to consume the
11907 operator token. */
11908 if (id)
11909 cp_lexer_consume_token (parser->lexer);
11910 /* Otherwise, no valid operator name was present. */
11911 else
11913 cp_parser_error (parser, "expected operator");
11914 id = error_mark_node;
11917 return id;
11920 /* Parse a template-declaration.
11922 template-declaration:
11923 export [opt] template < template-parameter-list > declaration
11925 If MEMBER_P is TRUE, this template-declaration occurs within a
11926 class-specifier.
11928 The grammar rule given by the standard isn't correct. What
11929 is really meant is:
11931 template-declaration:
11932 export [opt] template-parameter-list-seq
11933 decl-specifier-seq [opt] init-declarator [opt] ;
11934 export [opt] template-parameter-list-seq
11935 function-definition
11937 template-parameter-list-seq:
11938 template-parameter-list-seq [opt]
11939 template < template-parameter-list > */
11941 static void
11942 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11944 /* Check for `export'. */
11945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11947 /* Consume the `export' token. */
11948 cp_lexer_consume_token (parser->lexer);
11949 /* Warn that we do not support `export'. */
11950 warning (0, "keyword %<export%> not implemented, and will be ignored");
11953 cp_parser_template_declaration_after_export (parser, member_p);
11956 /* Parse a template-parameter-list.
11958 template-parameter-list:
11959 template-parameter
11960 template-parameter-list , template-parameter
11962 Returns a TREE_LIST. Each node represents a template parameter.
11963 The nodes are connected via their TREE_CHAINs. */
11965 static tree
11966 cp_parser_template_parameter_list (cp_parser* parser)
11968 tree parameter_list = NULL_TREE;
11970 begin_template_parm_list ();
11972 /* The loop below parses the template parms. We first need to know
11973 the total number of template parms to be able to compute proper
11974 canonical types of each dependent type. So after the loop, when
11975 we know the total number of template parms,
11976 end_template_parm_list computes the proper canonical types and
11977 fixes up the dependent types accordingly. */
11978 while (true)
11980 tree parameter;
11981 bool is_non_type;
11982 bool is_parameter_pack;
11983 location_t parm_loc;
11985 /* Parse the template-parameter. */
11986 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11987 parameter = cp_parser_template_parameter (parser,
11988 &is_non_type,
11989 &is_parameter_pack);
11990 /* Add it to the list. */
11991 if (parameter != error_mark_node)
11992 parameter_list = process_template_parm (parameter_list,
11993 parm_loc,
11994 parameter,
11995 is_non_type,
11996 is_parameter_pack);
11997 else
11999 tree err_parm = build_tree_list (parameter, parameter);
12000 parameter_list = chainon (parameter_list, err_parm);
12003 /* If the next token is not a `,', we're done. */
12004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12005 break;
12006 /* Otherwise, consume the `,' token. */
12007 cp_lexer_consume_token (parser->lexer);
12010 return end_template_parm_list (parameter_list);
12013 /* Parse a template-parameter.
12015 template-parameter:
12016 type-parameter
12017 parameter-declaration
12019 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12020 the parameter. The TREE_PURPOSE is the default value, if any.
12021 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12022 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12023 set to true iff this parameter is a parameter pack. */
12025 static tree
12026 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12027 bool *is_parameter_pack)
12029 cp_token *token;
12030 cp_parameter_declarator *parameter_declarator;
12031 cp_declarator *id_declarator;
12032 tree parm;
12034 /* Assume it is a type parameter or a template parameter. */
12035 *is_non_type = false;
12036 /* Assume it not a parameter pack. */
12037 *is_parameter_pack = false;
12038 /* Peek at the next token. */
12039 token = cp_lexer_peek_token (parser->lexer);
12040 /* If it is `class' or `template', we have a type-parameter. */
12041 if (token->keyword == RID_TEMPLATE)
12042 return cp_parser_type_parameter (parser, is_parameter_pack);
12043 /* If it is `class' or `typename' we do not know yet whether it is a
12044 type parameter or a non-type parameter. Consider:
12046 template <typename T, typename T::X X> ...
12050 template <class C, class D*> ...
12052 Here, the first parameter is a type parameter, and the second is
12053 a non-type parameter. We can tell by looking at the token after
12054 the identifier -- if it is a `,', `=', or `>' then we have a type
12055 parameter. */
12056 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12058 /* Peek at the token after `class' or `typename'. */
12059 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12060 /* If it's an ellipsis, we have a template type parameter
12061 pack. */
12062 if (token->type == CPP_ELLIPSIS)
12063 return cp_parser_type_parameter (parser, is_parameter_pack);
12064 /* If it's an identifier, skip it. */
12065 if (token->type == CPP_NAME)
12066 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12067 /* Now, see if the token looks like the end of a template
12068 parameter. */
12069 if (token->type == CPP_COMMA
12070 || token->type == CPP_EQ
12071 || token->type == CPP_GREATER)
12072 return cp_parser_type_parameter (parser, is_parameter_pack);
12075 /* Otherwise, it is a non-type parameter.
12077 [temp.param]
12079 When parsing a default template-argument for a non-type
12080 template-parameter, the first non-nested `>' is taken as the end
12081 of the template parameter-list rather than a greater-than
12082 operator. */
12083 *is_non_type = true;
12084 parameter_declarator
12085 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12086 /*parenthesized_p=*/NULL);
12088 /* If the parameter declaration is marked as a parameter pack, set
12089 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12090 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12091 grokdeclarator. */
12092 if (parameter_declarator
12093 && parameter_declarator->declarator
12094 && parameter_declarator->declarator->parameter_pack_p)
12096 *is_parameter_pack = true;
12097 parameter_declarator->declarator->parameter_pack_p = false;
12100 /* If the next token is an ellipsis, and we don't already have it
12101 marked as a parameter pack, then we have a parameter pack (that
12102 has no declarator). */
12103 if (!*is_parameter_pack
12104 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12105 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12107 /* Consume the `...'. */
12108 cp_lexer_consume_token (parser->lexer);
12109 maybe_warn_variadic_templates ();
12111 *is_parameter_pack = true;
12113 /* We might end up with a pack expansion as the type of the non-type
12114 template parameter, in which case this is a non-type template
12115 parameter pack. */
12116 else if (parameter_declarator
12117 && parameter_declarator->decl_specifiers.type
12118 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12120 *is_parameter_pack = true;
12121 parameter_declarator->decl_specifiers.type =
12122 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12125 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12127 /* Parameter packs cannot have default arguments. However, a
12128 user may try to do so, so we'll parse them and give an
12129 appropriate diagnostic here. */
12131 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12133 /* Find the name of the parameter pack. */
12134 id_declarator = parameter_declarator->declarator;
12135 while (id_declarator && id_declarator->kind != cdk_id)
12136 id_declarator = id_declarator->declarator;
12138 if (id_declarator && id_declarator->kind == cdk_id)
12139 error_at (start_token->location,
12140 "template parameter pack %qD cannot have a default argument",
12141 id_declarator->u.id.unqualified_name);
12142 else
12143 error_at (start_token->location,
12144 "template parameter pack cannot have a default argument");
12146 /* Parse the default argument, but throw away the result. */
12147 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12150 parm = grokdeclarator (parameter_declarator->declarator,
12151 &parameter_declarator->decl_specifiers,
12152 TPARM, /*initialized=*/0,
12153 /*attrlist=*/NULL);
12154 if (parm == error_mark_node)
12155 return error_mark_node;
12157 return build_tree_list (parameter_declarator->default_argument, parm);
12160 /* Parse a type-parameter.
12162 type-parameter:
12163 class identifier [opt]
12164 class identifier [opt] = type-id
12165 typename identifier [opt]
12166 typename identifier [opt] = type-id
12167 template < template-parameter-list > class identifier [opt]
12168 template < template-parameter-list > class identifier [opt]
12169 = id-expression
12171 GNU Extension (variadic templates):
12173 type-parameter:
12174 class ... identifier [opt]
12175 typename ... identifier [opt]
12177 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12178 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12179 the declaration of the parameter.
12181 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12183 static tree
12184 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12186 cp_token *token;
12187 tree parameter;
12189 /* Look for a keyword to tell us what kind of parameter this is. */
12190 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12191 if (!token)
12192 return error_mark_node;
12194 switch (token->keyword)
12196 case RID_CLASS:
12197 case RID_TYPENAME:
12199 tree identifier;
12200 tree default_argument;
12202 /* If the next token is an ellipsis, we have a template
12203 argument pack. */
12204 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12206 /* Consume the `...' token. */
12207 cp_lexer_consume_token (parser->lexer);
12208 maybe_warn_variadic_templates ();
12210 *is_parameter_pack = true;
12213 /* If the next token is an identifier, then it names the
12214 parameter. */
12215 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12216 identifier = cp_parser_identifier (parser);
12217 else
12218 identifier = NULL_TREE;
12220 /* Create the parameter. */
12221 parameter = finish_template_type_parm (class_type_node, identifier);
12223 /* If the next token is an `=', we have a default argument. */
12224 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12226 /* Consume the `=' token. */
12227 cp_lexer_consume_token (parser->lexer);
12228 /* Parse the default-argument. */
12229 push_deferring_access_checks (dk_no_deferred);
12230 default_argument = cp_parser_type_id (parser);
12232 /* Template parameter packs cannot have default
12233 arguments. */
12234 if (*is_parameter_pack)
12236 if (identifier)
12237 error_at (token->location,
12238 "template parameter pack %qD cannot have a "
12239 "default argument", identifier);
12240 else
12241 error_at (token->location,
12242 "template parameter packs cannot have "
12243 "default arguments");
12244 default_argument = NULL_TREE;
12246 pop_deferring_access_checks ();
12248 else
12249 default_argument = NULL_TREE;
12251 /* Create the combined representation of the parameter and the
12252 default argument. */
12253 parameter = build_tree_list (default_argument, parameter);
12255 break;
12257 case RID_TEMPLATE:
12259 tree identifier;
12260 tree default_argument;
12262 /* Look for the `<'. */
12263 cp_parser_require (parser, CPP_LESS, RT_LESS);
12264 /* Parse the template-parameter-list. */
12265 cp_parser_template_parameter_list (parser);
12266 /* Look for the `>'. */
12267 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12268 /* Look for the `class' keyword. */
12269 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12270 /* If the next token is an ellipsis, we have a template
12271 argument pack. */
12272 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12274 /* Consume the `...' token. */
12275 cp_lexer_consume_token (parser->lexer);
12276 maybe_warn_variadic_templates ();
12278 *is_parameter_pack = true;
12280 /* If the next token is an `=', then there is a
12281 default-argument. If the next token is a `>', we are at
12282 the end of the parameter-list. If the next token is a `,',
12283 then we are at the end of this parameter. */
12284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12285 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12286 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12288 identifier = cp_parser_identifier (parser);
12289 /* Treat invalid names as if the parameter were nameless. */
12290 if (identifier == error_mark_node)
12291 identifier = NULL_TREE;
12293 else
12294 identifier = NULL_TREE;
12296 /* Create the template parameter. */
12297 parameter = finish_template_template_parm (class_type_node,
12298 identifier);
12300 /* If the next token is an `=', then there is a
12301 default-argument. */
12302 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12304 bool is_template;
12306 /* Consume the `='. */
12307 cp_lexer_consume_token (parser->lexer);
12308 /* Parse the id-expression. */
12309 push_deferring_access_checks (dk_no_deferred);
12310 /* save token before parsing the id-expression, for error
12311 reporting */
12312 token = cp_lexer_peek_token (parser->lexer);
12313 default_argument
12314 = cp_parser_id_expression (parser,
12315 /*template_keyword_p=*/false,
12316 /*check_dependency_p=*/true,
12317 /*template_p=*/&is_template,
12318 /*declarator_p=*/false,
12319 /*optional_p=*/false);
12320 if (TREE_CODE (default_argument) == TYPE_DECL)
12321 /* If the id-expression was a template-id that refers to
12322 a template-class, we already have the declaration here,
12323 so no further lookup is needed. */
12325 else
12326 /* Look up the name. */
12327 default_argument
12328 = cp_parser_lookup_name (parser, default_argument,
12329 none_type,
12330 /*is_template=*/is_template,
12331 /*is_namespace=*/false,
12332 /*check_dependency=*/true,
12333 /*ambiguous_decls=*/NULL,
12334 token->location);
12335 /* See if the default argument is valid. */
12336 default_argument
12337 = check_template_template_default_arg (default_argument);
12339 /* Template parameter packs cannot have default
12340 arguments. */
12341 if (*is_parameter_pack)
12343 if (identifier)
12344 error_at (token->location,
12345 "template parameter pack %qD cannot "
12346 "have a default argument",
12347 identifier);
12348 else
12349 error_at (token->location, "template parameter packs cannot "
12350 "have default arguments");
12351 default_argument = NULL_TREE;
12353 pop_deferring_access_checks ();
12355 else
12356 default_argument = NULL_TREE;
12358 /* Create the combined representation of the parameter and the
12359 default argument. */
12360 parameter = build_tree_list (default_argument, parameter);
12362 break;
12364 default:
12365 gcc_unreachable ();
12366 break;
12369 return parameter;
12372 /* Parse a template-id.
12374 template-id:
12375 template-name < template-argument-list [opt] >
12377 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12378 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12379 returned. Otherwise, if the template-name names a function, or set
12380 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12381 names a class, returns a TYPE_DECL for the specialization.
12383 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12384 uninstantiated templates. */
12386 static tree
12387 cp_parser_template_id (cp_parser *parser,
12388 bool template_keyword_p,
12389 bool check_dependency_p,
12390 enum tag_types tag_type,
12391 bool is_declaration)
12393 int i;
12394 tree templ;
12395 tree arguments;
12396 tree template_id;
12397 cp_token_position start_of_id = 0;
12398 deferred_access_check *chk;
12399 VEC (deferred_access_check,gc) *access_check;
12400 cp_token *next_token = NULL, *next_token_2 = NULL;
12401 bool is_identifier;
12403 /* If the next token corresponds to a template-id, there is no need
12404 to reparse it. */
12405 next_token = cp_lexer_peek_token (parser->lexer);
12406 if (next_token->type == CPP_TEMPLATE_ID)
12408 struct tree_check *check_value;
12410 /* Get the stored value. */
12411 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12412 /* Perform any access checks that were deferred. */
12413 access_check = check_value->checks;
12414 if (access_check)
12416 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12417 perform_or_defer_access_check (chk->binfo,
12418 chk->decl,
12419 chk->diag_decl,
12420 tf_warning_or_error);
12422 /* Return the stored value. */
12423 return check_value->value;
12426 /* Avoid performing name lookup if there is no possibility of
12427 finding a template-id. */
12428 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12429 || (next_token->type == CPP_NAME
12430 && !cp_parser_nth_token_starts_template_argument_list_p
12431 (parser, 2)))
12433 cp_parser_error (parser, "expected template-id");
12434 return error_mark_node;
12437 /* Remember where the template-id starts. */
12438 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12439 start_of_id = cp_lexer_token_position (parser->lexer, false);
12441 push_deferring_access_checks (dk_deferred);
12443 /* Parse the template-name. */
12444 is_identifier = false;
12445 templ = cp_parser_template_name (parser, template_keyword_p,
12446 check_dependency_p,
12447 is_declaration,
12448 tag_type,
12449 &is_identifier);
12450 if (templ == error_mark_node || is_identifier)
12452 pop_deferring_access_checks ();
12453 return templ;
12456 /* If we find the sequence `[:' after a template-name, it's probably
12457 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12458 parse correctly the argument list. */
12459 next_token = cp_lexer_peek_token (parser->lexer);
12460 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12461 if (next_token->type == CPP_OPEN_SQUARE
12462 && next_token->flags & DIGRAPH
12463 && next_token_2->type == CPP_COLON
12464 && !(next_token_2->flags & PREV_WHITE))
12466 cp_parser_parse_tentatively (parser);
12467 /* Change `:' into `::'. */
12468 next_token_2->type = CPP_SCOPE;
12469 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12470 CPP_LESS. */
12471 cp_lexer_consume_token (parser->lexer);
12473 /* Parse the arguments. */
12474 arguments = cp_parser_enclosed_template_argument_list (parser);
12475 if (!cp_parser_parse_definitely (parser))
12477 /* If we couldn't parse an argument list, then we revert our changes
12478 and return simply an error. Maybe this is not a template-id
12479 after all. */
12480 next_token_2->type = CPP_COLON;
12481 cp_parser_error (parser, "expected %<<%>");
12482 pop_deferring_access_checks ();
12483 return error_mark_node;
12485 /* Otherwise, emit an error about the invalid digraph, but continue
12486 parsing because we got our argument list. */
12487 if (permerror (next_token->location,
12488 "%<<::%> cannot begin a template-argument list"))
12490 static bool hint = false;
12491 inform (next_token->location,
12492 "%<<:%> is an alternate spelling for %<[%>."
12493 " Insert whitespace between %<<%> and %<::%>");
12494 if (!hint && !flag_permissive)
12496 inform (next_token->location, "(if you use %<-fpermissive%>"
12497 " G++ will accept your code)");
12498 hint = true;
12502 else
12504 /* Look for the `<' that starts the template-argument-list. */
12505 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12507 pop_deferring_access_checks ();
12508 return error_mark_node;
12510 /* Parse the arguments. */
12511 arguments = cp_parser_enclosed_template_argument_list (parser);
12514 /* Build a representation of the specialization. */
12515 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12516 template_id = build_min_nt_loc (next_token->location,
12517 TEMPLATE_ID_EXPR,
12518 templ, arguments);
12519 else if (DECL_TYPE_TEMPLATE_P (templ)
12520 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12522 bool entering_scope;
12523 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12524 template (rather than some instantiation thereof) only if
12525 is not nested within some other construct. For example, in
12526 "template <typename T> void f(T) { A<T>::", A<T> is just an
12527 instantiation of A. */
12528 entering_scope = (template_parm_scope_p ()
12529 && cp_lexer_next_token_is (parser->lexer,
12530 CPP_SCOPE));
12531 template_id
12532 = finish_template_type (templ, arguments, entering_scope);
12534 else
12536 /* If it's not a class-template or a template-template, it should be
12537 a function-template. */
12538 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12539 || TREE_CODE (templ) == OVERLOAD
12540 || BASELINK_P (templ)));
12542 template_id = lookup_template_function (templ, arguments);
12545 /* If parsing tentatively, replace the sequence of tokens that makes
12546 up the template-id with a CPP_TEMPLATE_ID token. That way,
12547 should we re-parse the token stream, we will not have to repeat
12548 the effort required to do the parse, nor will we issue duplicate
12549 error messages about problems during instantiation of the
12550 template. */
12551 if (start_of_id)
12553 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12555 /* Reset the contents of the START_OF_ID token. */
12556 token->type = CPP_TEMPLATE_ID;
12557 /* Retrieve any deferred checks. Do not pop this access checks yet
12558 so the memory will not be reclaimed during token replacing below. */
12559 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12560 token->u.tree_check_value->value = template_id;
12561 token->u.tree_check_value->checks = get_deferred_access_checks ();
12562 token->keyword = RID_MAX;
12564 /* Purge all subsequent tokens. */
12565 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12567 /* ??? Can we actually assume that, if template_id ==
12568 error_mark_node, we will have issued a diagnostic to the
12569 user, as opposed to simply marking the tentative parse as
12570 failed? */
12571 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12572 error_at (token->location, "parse error in template argument list");
12575 pop_deferring_access_checks ();
12576 return template_id;
12579 /* Parse a template-name.
12581 template-name:
12582 identifier
12584 The standard should actually say:
12586 template-name:
12587 identifier
12588 operator-function-id
12590 A defect report has been filed about this issue.
12592 A conversion-function-id cannot be a template name because they cannot
12593 be part of a template-id. In fact, looking at this code:
12595 a.operator K<int>()
12597 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12598 It is impossible to call a templated conversion-function-id with an
12599 explicit argument list, since the only allowed template parameter is
12600 the type to which it is converting.
12602 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12603 `template' keyword, in a construction like:
12605 T::template f<3>()
12607 In that case `f' is taken to be a template-name, even though there
12608 is no way of knowing for sure.
12610 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12611 name refers to a set of overloaded functions, at least one of which
12612 is a template, or an IDENTIFIER_NODE with the name of the template,
12613 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12614 names are looked up inside uninstantiated templates. */
12616 static tree
12617 cp_parser_template_name (cp_parser* parser,
12618 bool template_keyword_p,
12619 bool check_dependency_p,
12620 bool is_declaration,
12621 enum tag_types tag_type,
12622 bool *is_identifier)
12624 tree identifier;
12625 tree decl;
12626 tree fns;
12627 cp_token *token = cp_lexer_peek_token (parser->lexer);
12629 /* If the next token is `operator', then we have either an
12630 operator-function-id or a conversion-function-id. */
12631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12633 /* We don't know whether we're looking at an
12634 operator-function-id or a conversion-function-id. */
12635 cp_parser_parse_tentatively (parser);
12636 /* Try an operator-function-id. */
12637 identifier = cp_parser_operator_function_id (parser);
12638 /* If that didn't work, try a conversion-function-id. */
12639 if (!cp_parser_parse_definitely (parser))
12641 cp_parser_error (parser, "expected template-name");
12642 return error_mark_node;
12645 /* Look for the identifier. */
12646 else
12647 identifier = cp_parser_identifier (parser);
12649 /* If we didn't find an identifier, we don't have a template-id. */
12650 if (identifier == error_mark_node)
12651 return error_mark_node;
12653 /* If the name immediately followed the `template' keyword, then it
12654 is a template-name. However, if the next token is not `<', then
12655 we do not treat it as a template-name, since it is not being used
12656 as part of a template-id. This enables us to handle constructs
12657 like:
12659 template <typename T> struct S { S(); };
12660 template <typename T> S<T>::S();
12662 correctly. We would treat `S' as a template -- if it were `S<T>'
12663 -- but we do not if there is no `<'. */
12665 if (processing_template_decl
12666 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12668 /* In a declaration, in a dependent context, we pretend that the
12669 "template" keyword was present in order to improve error
12670 recovery. For example, given:
12672 template <typename T> void f(T::X<int>);
12674 we want to treat "X<int>" as a template-id. */
12675 if (is_declaration
12676 && !template_keyword_p
12677 && parser->scope && TYPE_P (parser->scope)
12678 && check_dependency_p
12679 && dependent_scope_p (parser->scope)
12680 /* Do not do this for dtors (or ctors), since they never
12681 need the template keyword before their name. */
12682 && !constructor_name_p (identifier, parser->scope))
12684 cp_token_position start = 0;
12686 /* Explain what went wrong. */
12687 error_at (token->location, "non-template %qD used as template",
12688 identifier);
12689 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12690 parser->scope, identifier);
12691 /* If parsing tentatively, find the location of the "<" token. */
12692 if (cp_parser_simulate_error (parser))
12693 start = cp_lexer_token_position (parser->lexer, true);
12694 /* Parse the template arguments so that we can issue error
12695 messages about them. */
12696 cp_lexer_consume_token (parser->lexer);
12697 cp_parser_enclosed_template_argument_list (parser);
12698 /* Skip tokens until we find a good place from which to
12699 continue parsing. */
12700 cp_parser_skip_to_closing_parenthesis (parser,
12701 /*recovering=*/true,
12702 /*or_comma=*/true,
12703 /*consume_paren=*/false);
12704 /* If parsing tentatively, permanently remove the
12705 template argument list. That will prevent duplicate
12706 error messages from being issued about the missing
12707 "template" keyword. */
12708 if (start)
12709 cp_lexer_purge_tokens_after (parser->lexer, start);
12710 if (is_identifier)
12711 *is_identifier = true;
12712 return identifier;
12715 /* If the "template" keyword is present, then there is generally
12716 no point in doing name-lookup, so we just return IDENTIFIER.
12717 But, if the qualifying scope is non-dependent then we can
12718 (and must) do name-lookup normally. */
12719 if (template_keyword_p
12720 && (!parser->scope
12721 || (TYPE_P (parser->scope)
12722 && dependent_type_p (parser->scope))))
12723 return identifier;
12726 /* Look up the name. */
12727 decl = cp_parser_lookup_name (parser, identifier,
12728 tag_type,
12729 /*is_template=*/true,
12730 /*is_namespace=*/false,
12731 check_dependency_p,
12732 /*ambiguous_decls=*/NULL,
12733 token->location);
12735 /* If DECL is a template, then the name was a template-name. */
12736 if (TREE_CODE (decl) == TEMPLATE_DECL)
12738 else
12740 tree fn = NULL_TREE;
12742 /* The standard does not explicitly indicate whether a name that
12743 names a set of overloaded declarations, some of which are
12744 templates, is a template-name. However, such a name should
12745 be a template-name; otherwise, there is no way to form a
12746 template-id for the overloaded templates. */
12747 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12748 if (TREE_CODE (fns) == OVERLOAD)
12749 for (fn = fns; fn; fn = OVL_NEXT (fn))
12750 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12751 break;
12753 if (!fn)
12755 /* The name does not name a template. */
12756 cp_parser_error (parser, "expected template-name");
12757 return error_mark_node;
12761 /* If DECL is dependent, and refers to a function, then just return
12762 its name; we will look it up again during template instantiation. */
12763 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12765 tree scope = ovl_scope (decl);
12766 if (TYPE_P (scope) && dependent_type_p (scope))
12767 return identifier;
12770 return decl;
12773 /* Parse a template-argument-list.
12775 template-argument-list:
12776 template-argument ... [opt]
12777 template-argument-list , template-argument ... [opt]
12779 Returns a TREE_VEC containing the arguments. */
12781 static tree
12782 cp_parser_template_argument_list (cp_parser* parser)
12784 tree fixed_args[10];
12785 unsigned n_args = 0;
12786 unsigned alloced = 10;
12787 tree *arg_ary = fixed_args;
12788 tree vec;
12789 bool saved_in_template_argument_list_p;
12790 bool saved_ice_p;
12791 bool saved_non_ice_p;
12793 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12794 parser->in_template_argument_list_p = true;
12795 /* Even if the template-id appears in an integral
12796 constant-expression, the contents of the argument list do
12797 not. */
12798 saved_ice_p = parser->integral_constant_expression_p;
12799 parser->integral_constant_expression_p = false;
12800 saved_non_ice_p = parser->non_integral_constant_expression_p;
12801 parser->non_integral_constant_expression_p = false;
12803 /* Parse the arguments. */
12806 tree argument;
12808 if (n_args)
12809 /* Consume the comma. */
12810 cp_lexer_consume_token (parser->lexer);
12812 /* Parse the template-argument. */
12813 argument = cp_parser_template_argument (parser);
12815 /* If the next token is an ellipsis, we're expanding a template
12816 argument pack. */
12817 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12819 if (argument == error_mark_node)
12821 cp_token *token = cp_lexer_peek_token (parser->lexer);
12822 error_at (token->location,
12823 "expected parameter pack before %<...%>");
12825 /* Consume the `...' token. */
12826 cp_lexer_consume_token (parser->lexer);
12828 /* Make the argument into a TYPE_PACK_EXPANSION or
12829 EXPR_PACK_EXPANSION. */
12830 argument = make_pack_expansion (argument);
12833 if (n_args == alloced)
12835 alloced *= 2;
12837 if (arg_ary == fixed_args)
12839 arg_ary = XNEWVEC (tree, alloced);
12840 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12842 else
12843 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12845 arg_ary[n_args++] = argument;
12847 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12849 vec = make_tree_vec (n_args);
12851 while (n_args--)
12852 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12854 if (arg_ary != fixed_args)
12855 free (arg_ary);
12856 parser->non_integral_constant_expression_p = saved_non_ice_p;
12857 parser->integral_constant_expression_p = saved_ice_p;
12858 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12859 #ifdef ENABLE_CHECKING
12860 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12861 #endif
12862 return vec;
12865 /* Parse a template-argument.
12867 template-argument:
12868 assignment-expression
12869 type-id
12870 id-expression
12872 The representation is that of an assignment-expression, type-id, or
12873 id-expression -- except that the qualified id-expression is
12874 evaluated, so that the value returned is either a DECL or an
12875 OVERLOAD.
12877 Although the standard says "assignment-expression", it forbids
12878 throw-expressions or assignments in the template argument.
12879 Therefore, we use "conditional-expression" instead. */
12881 static tree
12882 cp_parser_template_argument (cp_parser* parser)
12884 tree argument;
12885 bool template_p;
12886 bool address_p;
12887 bool maybe_type_id = false;
12888 cp_token *token = NULL, *argument_start_token = NULL;
12889 location_t loc = 0;
12890 cp_id_kind idk;
12892 /* There's really no way to know what we're looking at, so we just
12893 try each alternative in order.
12895 [temp.arg]
12897 In a template-argument, an ambiguity between a type-id and an
12898 expression is resolved to a type-id, regardless of the form of
12899 the corresponding template-parameter.
12901 Therefore, we try a type-id first. */
12902 cp_parser_parse_tentatively (parser);
12903 argument = cp_parser_template_type_arg (parser);
12904 /* If there was no error parsing the type-id but the next token is a
12905 '>>', our behavior depends on which dialect of C++ we're
12906 parsing. In C++98, we probably found a typo for '> >'. But there
12907 are type-id which are also valid expressions. For instance:
12909 struct X { int operator >> (int); };
12910 template <int V> struct Foo {};
12911 Foo<X () >> 5> r;
12913 Here 'X()' is a valid type-id of a function type, but the user just
12914 wanted to write the expression "X() >> 5". Thus, we remember that we
12915 found a valid type-id, but we still try to parse the argument as an
12916 expression to see what happens.
12918 In C++0x, the '>>' will be considered two separate '>'
12919 tokens. */
12920 if (!cp_parser_error_occurred (parser)
12921 && cxx_dialect == cxx98
12922 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12924 maybe_type_id = true;
12925 cp_parser_abort_tentative_parse (parser);
12927 else
12929 /* If the next token isn't a `,' or a `>', then this argument wasn't
12930 really finished. This means that the argument is not a valid
12931 type-id. */
12932 if (!cp_parser_next_token_ends_template_argument_p (parser))
12933 cp_parser_error (parser, "expected template-argument");
12934 /* If that worked, we're done. */
12935 if (cp_parser_parse_definitely (parser))
12936 return argument;
12938 /* We're still not sure what the argument will be. */
12939 cp_parser_parse_tentatively (parser);
12940 /* Try a template. */
12941 argument_start_token = cp_lexer_peek_token (parser->lexer);
12942 argument = cp_parser_id_expression (parser,
12943 /*template_keyword_p=*/false,
12944 /*check_dependency_p=*/true,
12945 &template_p,
12946 /*declarator_p=*/false,
12947 /*optional_p=*/false);
12948 /* If the next token isn't a `,' or a `>', then this argument wasn't
12949 really finished. */
12950 if (!cp_parser_next_token_ends_template_argument_p (parser))
12951 cp_parser_error (parser, "expected template-argument");
12952 if (!cp_parser_error_occurred (parser))
12954 /* Figure out what is being referred to. If the id-expression
12955 was for a class template specialization, then we will have a
12956 TYPE_DECL at this point. There is no need to do name lookup
12957 at this point in that case. */
12958 if (TREE_CODE (argument) != TYPE_DECL)
12959 argument = cp_parser_lookup_name (parser, argument,
12960 none_type,
12961 /*is_template=*/template_p,
12962 /*is_namespace=*/false,
12963 /*check_dependency=*/true,
12964 /*ambiguous_decls=*/NULL,
12965 argument_start_token->location);
12966 if (TREE_CODE (argument) != TEMPLATE_DECL
12967 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12968 cp_parser_error (parser, "expected template-name");
12970 if (cp_parser_parse_definitely (parser))
12971 return argument;
12972 /* It must be a non-type argument. There permitted cases are given
12973 in [temp.arg.nontype]:
12975 -- an integral constant-expression of integral or enumeration
12976 type; or
12978 -- the name of a non-type template-parameter; or
12980 -- the name of an object or function with external linkage...
12982 -- the address of an object or function with external linkage...
12984 -- a pointer to member... */
12985 /* Look for a non-type template parameter. */
12986 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12988 cp_parser_parse_tentatively (parser);
12989 argument = cp_parser_primary_expression (parser,
12990 /*address_p=*/false,
12991 /*cast_p=*/false,
12992 /*template_arg_p=*/true,
12993 &idk);
12994 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12995 || !cp_parser_next_token_ends_template_argument_p (parser))
12996 cp_parser_simulate_error (parser);
12997 if (cp_parser_parse_definitely (parser))
12998 return argument;
13001 /* If the next token is "&", the argument must be the address of an
13002 object or function with external linkage. */
13003 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13004 if (address_p)
13006 loc = cp_lexer_peek_token (parser->lexer)->location;
13007 cp_lexer_consume_token (parser->lexer);
13009 /* See if we might have an id-expression. */
13010 token = cp_lexer_peek_token (parser->lexer);
13011 if (token->type == CPP_NAME
13012 || token->keyword == RID_OPERATOR
13013 || token->type == CPP_SCOPE
13014 || token->type == CPP_TEMPLATE_ID
13015 || token->type == CPP_NESTED_NAME_SPECIFIER)
13017 cp_parser_parse_tentatively (parser);
13018 argument = cp_parser_primary_expression (parser,
13019 address_p,
13020 /*cast_p=*/false,
13021 /*template_arg_p=*/true,
13022 &idk);
13023 if (cp_parser_error_occurred (parser)
13024 || !cp_parser_next_token_ends_template_argument_p (parser))
13025 cp_parser_abort_tentative_parse (parser);
13026 else
13028 tree probe;
13030 if (TREE_CODE (argument) == INDIRECT_REF)
13032 gcc_assert (REFERENCE_REF_P (argument));
13033 argument = TREE_OPERAND (argument, 0);
13036 /* If we're in a template, we represent a qualified-id referring
13037 to a static data member as a SCOPE_REF even if the scope isn't
13038 dependent so that we can check access control later. */
13039 probe = argument;
13040 if (TREE_CODE (probe) == SCOPE_REF)
13041 probe = TREE_OPERAND (probe, 1);
13042 if (TREE_CODE (probe) == VAR_DECL)
13044 /* A variable without external linkage might still be a
13045 valid constant-expression, so no error is issued here
13046 if the external-linkage check fails. */
13047 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13048 cp_parser_simulate_error (parser);
13050 else if (is_overloaded_fn (argument))
13051 /* All overloaded functions are allowed; if the external
13052 linkage test does not pass, an error will be issued
13053 later. */
13055 else if (address_p
13056 && (TREE_CODE (argument) == OFFSET_REF
13057 || TREE_CODE (argument) == SCOPE_REF))
13058 /* A pointer-to-member. */
13060 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13062 else
13063 cp_parser_simulate_error (parser);
13065 if (cp_parser_parse_definitely (parser))
13067 if (address_p)
13068 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13069 tf_warning_or_error);
13070 return argument;
13074 /* If the argument started with "&", there are no other valid
13075 alternatives at this point. */
13076 if (address_p)
13078 cp_parser_error (parser, "invalid non-type template argument");
13079 return error_mark_node;
13082 /* If the argument wasn't successfully parsed as a type-id followed
13083 by '>>', the argument can only be a constant expression now.
13084 Otherwise, we try parsing the constant-expression tentatively,
13085 because the argument could really be a type-id. */
13086 if (maybe_type_id)
13087 cp_parser_parse_tentatively (parser);
13088 argument = cp_parser_constant_expression (parser,
13089 /*allow_non_constant_p=*/false,
13090 /*non_constant_p=*/NULL);
13091 argument = fold_non_dependent_expr (argument);
13092 if (!maybe_type_id)
13093 return argument;
13094 if (!cp_parser_next_token_ends_template_argument_p (parser))
13095 cp_parser_error (parser, "expected template-argument");
13096 if (cp_parser_parse_definitely (parser))
13097 return argument;
13098 /* We did our best to parse the argument as a non type-id, but that
13099 was the only alternative that matched (albeit with a '>' after
13100 it). We can assume it's just a typo from the user, and a
13101 diagnostic will then be issued. */
13102 return cp_parser_template_type_arg (parser);
13105 /* Parse an explicit-instantiation.
13107 explicit-instantiation:
13108 template declaration
13110 Although the standard says `declaration', what it really means is:
13112 explicit-instantiation:
13113 template decl-specifier-seq [opt] declarator [opt] ;
13115 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13116 supposed to be allowed. A defect report has been filed about this
13117 issue.
13119 GNU Extension:
13121 explicit-instantiation:
13122 storage-class-specifier template
13123 decl-specifier-seq [opt] declarator [opt] ;
13124 function-specifier template
13125 decl-specifier-seq [opt] declarator [opt] ; */
13127 static void
13128 cp_parser_explicit_instantiation (cp_parser* parser)
13130 int declares_class_or_enum;
13131 cp_decl_specifier_seq decl_specifiers;
13132 tree extension_specifier = NULL_TREE;
13134 timevar_push (TV_TEMPLATE_INST);
13136 /* Look for an (optional) storage-class-specifier or
13137 function-specifier. */
13138 if (cp_parser_allow_gnu_extensions_p (parser))
13140 extension_specifier
13141 = cp_parser_storage_class_specifier_opt (parser);
13142 if (!extension_specifier)
13143 extension_specifier
13144 = cp_parser_function_specifier_opt (parser,
13145 /*decl_specs=*/NULL);
13148 /* Look for the `template' keyword. */
13149 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13150 /* Let the front end know that we are processing an explicit
13151 instantiation. */
13152 begin_explicit_instantiation ();
13153 /* [temp.explicit] says that we are supposed to ignore access
13154 control while processing explicit instantiation directives. */
13155 push_deferring_access_checks (dk_no_check);
13156 /* Parse a decl-specifier-seq. */
13157 cp_parser_decl_specifier_seq (parser,
13158 CP_PARSER_FLAGS_OPTIONAL,
13159 &decl_specifiers,
13160 &declares_class_or_enum);
13161 /* If there was exactly one decl-specifier, and it declared a class,
13162 and there's no declarator, then we have an explicit type
13163 instantiation. */
13164 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13166 tree type;
13168 type = check_tag_decl (&decl_specifiers);
13169 /* Turn access control back on for names used during
13170 template instantiation. */
13171 pop_deferring_access_checks ();
13172 if (type)
13173 do_type_instantiation (type, extension_specifier,
13174 /*complain=*/tf_error);
13176 else
13178 cp_declarator *declarator;
13179 tree decl;
13181 /* Parse the declarator. */
13182 declarator
13183 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13184 /*ctor_dtor_or_conv_p=*/NULL,
13185 /*parenthesized_p=*/NULL,
13186 /*member_p=*/false);
13187 if (declares_class_or_enum & 2)
13188 cp_parser_check_for_definition_in_return_type (declarator,
13189 decl_specifiers.type,
13190 decl_specifiers.locations[ds_type_spec]);
13191 if (declarator != cp_error_declarator)
13193 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13194 permerror (decl_specifiers.locations[ds_inline],
13195 "explicit instantiation shall not use"
13196 " %<inline%> specifier");
13197 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13198 permerror (decl_specifiers.locations[ds_constexpr],
13199 "explicit instantiation shall not use"
13200 " %<constexpr%> specifier");
13202 decl = grokdeclarator (declarator, &decl_specifiers,
13203 NORMAL, 0, &decl_specifiers.attributes);
13204 /* Turn access control back on for names used during
13205 template instantiation. */
13206 pop_deferring_access_checks ();
13207 /* Do the explicit instantiation. */
13208 do_decl_instantiation (decl, extension_specifier);
13210 else
13212 pop_deferring_access_checks ();
13213 /* Skip the body of the explicit instantiation. */
13214 cp_parser_skip_to_end_of_statement (parser);
13217 /* We're done with the instantiation. */
13218 end_explicit_instantiation ();
13220 cp_parser_consume_semicolon_at_end_of_statement (parser);
13222 timevar_pop (TV_TEMPLATE_INST);
13225 /* Parse an explicit-specialization.
13227 explicit-specialization:
13228 template < > declaration
13230 Although the standard says `declaration', what it really means is:
13232 explicit-specialization:
13233 template <> decl-specifier [opt] init-declarator [opt] ;
13234 template <> function-definition
13235 template <> explicit-specialization
13236 template <> template-declaration */
13238 static void
13239 cp_parser_explicit_specialization (cp_parser* parser)
13241 bool need_lang_pop;
13242 cp_token *token = cp_lexer_peek_token (parser->lexer);
13244 /* Look for the `template' keyword. */
13245 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13246 /* Look for the `<'. */
13247 cp_parser_require (parser, CPP_LESS, RT_LESS);
13248 /* Look for the `>'. */
13249 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13250 /* We have processed another parameter list. */
13251 ++parser->num_template_parameter_lists;
13252 /* [temp]
13254 A template ... explicit specialization ... shall not have C
13255 linkage. */
13256 if (current_lang_name == lang_name_c)
13258 error_at (token->location, "template specialization with C linkage");
13259 /* Give it C++ linkage to avoid confusing other parts of the
13260 front end. */
13261 push_lang_context (lang_name_cplusplus);
13262 need_lang_pop = true;
13264 else
13265 need_lang_pop = false;
13266 /* Let the front end know that we are beginning a specialization. */
13267 if (!begin_specialization ())
13269 end_specialization ();
13270 return;
13273 /* If the next keyword is `template', we need to figure out whether
13274 or not we're looking a template-declaration. */
13275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13277 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13278 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13279 cp_parser_template_declaration_after_export (parser,
13280 /*member_p=*/false);
13281 else
13282 cp_parser_explicit_specialization (parser);
13284 else
13285 /* Parse the dependent declaration. */
13286 cp_parser_single_declaration (parser,
13287 /*checks=*/NULL,
13288 /*member_p=*/false,
13289 /*explicit_specialization_p=*/true,
13290 /*friend_p=*/NULL);
13291 /* We're done with the specialization. */
13292 end_specialization ();
13293 /* For the erroneous case of a template with C linkage, we pushed an
13294 implicit C++ linkage scope; exit that scope now. */
13295 if (need_lang_pop)
13296 pop_lang_context ();
13297 /* We're done with this parameter list. */
13298 --parser->num_template_parameter_lists;
13301 /* Parse a type-specifier.
13303 type-specifier:
13304 simple-type-specifier
13305 class-specifier
13306 enum-specifier
13307 elaborated-type-specifier
13308 cv-qualifier
13310 GNU Extension:
13312 type-specifier:
13313 __complex__
13315 Returns a representation of the type-specifier. For a
13316 class-specifier, enum-specifier, or elaborated-type-specifier, a
13317 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13319 The parser flags FLAGS is used to control type-specifier parsing.
13321 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13322 in a decl-specifier-seq.
13324 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13325 class-specifier, enum-specifier, or elaborated-type-specifier, then
13326 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13327 if a type is declared; 2 if it is defined. Otherwise, it is set to
13328 zero.
13330 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13331 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13332 is set to FALSE. */
13334 static tree
13335 cp_parser_type_specifier (cp_parser* parser,
13336 cp_parser_flags flags,
13337 cp_decl_specifier_seq *decl_specs,
13338 bool is_declaration,
13339 int* declares_class_or_enum,
13340 bool* is_cv_qualifier)
13342 tree type_spec = NULL_TREE;
13343 cp_token *token;
13344 enum rid keyword;
13345 cp_decl_spec ds = ds_last;
13347 /* Assume this type-specifier does not declare a new type. */
13348 if (declares_class_or_enum)
13349 *declares_class_or_enum = 0;
13350 /* And that it does not specify a cv-qualifier. */
13351 if (is_cv_qualifier)
13352 *is_cv_qualifier = false;
13353 /* Peek at the next token. */
13354 token = cp_lexer_peek_token (parser->lexer);
13356 /* If we're looking at a keyword, we can use that to guide the
13357 production we choose. */
13358 keyword = token->keyword;
13359 switch (keyword)
13361 case RID_ENUM:
13362 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13363 goto elaborated_type_specifier;
13365 /* Look for the enum-specifier. */
13366 type_spec = cp_parser_enum_specifier (parser);
13367 /* If that worked, we're done. */
13368 if (type_spec)
13370 if (declares_class_or_enum)
13371 *declares_class_or_enum = 2;
13372 if (decl_specs)
13373 cp_parser_set_decl_spec_type (decl_specs,
13374 type_spec,
13375 token->location,
13376 /*type_definition_p=*/true);
13377 return type_spec;
13379 else
13380 goto elaborated_type_specifier;
13382 /* Any of these indicate either a class-specifier, or an
13383 elaborated-type-specifier. */
13384 case RID_CLASS:
13385 case RID_STRUCT:
13386 case RID_UNION:
13387 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13388 goto elaborated_type_specifier;
13390 /* Parse tentatively so that we can back up if we don't find a
13391 class-specifier. */
13392 cp_parser_parse_tentatively (parser);
13393 /* Look for the class-specifier. */
13394 type_spec = cp_parser_class_specifier (parser);
13395 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13396 /* If that worked, we're done. */
13397 if (cp_parser_parse_definitely (parser))
13399 if (declares_class_or_enum)
13400 *declares_class_or_enum = 2;
13401 if (decl_specs)
13402 cp_parser_set_decl_spec_type (decl_specs,
13403 type_spec,
13404 token->location,
13405 /*type_definition_p=*/true);
13406 return type_spec;
13409 /* Fall through. */
13410 elaborated_type_specifier:
13411 /* We're declaring (not defining) a class or enum. */
13412 if (declares_class_or_enum)
13413 *declares_class_or_enum = 1;
13415 /* Fall through. */
13416 case RID_TYPENAME:
13417 /* Look for an elaborated-type-specifier. */
13418 type_spec
13419 = (cp_parser_elaborated_type_specifier
13420 (parser,
13421 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13422 is_declaration));
13423 if (decl_specs)
13424 cp_parser_set_decl_spec_type (decl_specs,
13425 type_spec,
13426 token->location,
13427 /*type_definition_p=*/false);
13428 return type_spec;
13430 case RID_CONST:
13431 ds = ds_const;
13432 if (is_cv_qualifier)
13433 *is_cv_qualifier = true;
13434 break;
13436 case RID_VOLATILE:
13437 ds = ds_volatile;
13438 if (is_cv_qualifier)
13439 *is_cv_qualifier = true;
13440 break;
13442 case RID_RESTRICT:
13443 ds = ds_restrict;
13444 if (is_cv_qualifier)
13445 *is_cv_qualifier = true;
13446 break;
13448 case RID_COMPLEX:
13449 /* The `__complex__' keyword is a GNU extension. */
13450 ds = ds_complex;
13451 break;
13453 default:
13454 break;
13457 /* Handle simple keywords. */
13458 if (ds != ds_last)
13460 if (decl_specs)
13462 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
13463 decl_specs->any_specifiers_p = true;
13465 return cp_lexer_consume_token (parser->lexer)->u.value;
13468 /* If we do not already have a type-specifier, assume we are looking
13469 at a simple-type-specifier. */
13470 type_spec = cp_parser_simple_type_specifier (parser,
13471 decl_specs,
13472 flags);
13474 /* If we didn't find a type-specifier, and a type-specifier was not
13475 optional in this context, issue an error message. */
13476 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13478 cp_parser_error (parser, "expected type specifier");
13479 return error_mark_node;
13482 return type_spec;
13485 /* Parse a simple-type-specifier.
13487 simple-type-specifier:
13488 :: [opt] nested-name-specifier [opt] type-name
13489 :: [opt] nested-name-specifier template template-id
13490 char
13491 wchar_t
13492 bool
13493 short
13495 long
13496 signed
13497 unsigned
13498 float
13499 double
13500 void
13502 C++0x Extension:
13504 simple-type-specifier:
13505 auto
13506 decltype ( expression )
13507 char16_t
13508 char32_t
13509 __underlying_type ( type-id )
13511 GNU Extension:
13513 simple-type-specifier:
13514 __int128
13515 __typeof__ unary-expression
13516 __typeof__ ( type-id )
13518 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13519 appropriately updated. */
13521 static tree
13522 cp_parser_simple_type_specifier (cp_parser* parser,
13523 cp_decl_specifier_seq *decl_specs,
13524 cp_parser_flags flags)
13526 tree type = NULL_TREE;
13527 cp_token *token;
13529 /* Peek at the next token. */
13530 token = cp_lexer_peek_token (parser->lexer);
13532 /* If we're looking at a keyword, things are easy. */
13533 switch (token->keyword)
13535 case RID_CHAR:
13536 if (decl_specs)
13537 decl_specs->explicit_char_p = true;
13538 type = char_type_node;
13539 break;
13540 case RID_CHAR16:
13541 type = char16_type_node;
13542 break;
13543 case RID_CHAR32:
13544 type = char32_type_node;
13545 break;
13546 case RID_WCHAR:
13547 type = wchar_type_node;
13548 break;
13549 case RID_BOOL:
13550 type = boolean_type_node;
13551 break;
13552 case RID_SHORT:
13553 set_and_check_decl_spec_loc (decl_specs, ds_short, token->location);
13554 type = short_integer_type_node;
13555 break;
13556 case RID_INT:
13557 if (decl_specs)
13558 decl_specs->explicit_int_p = true;
13559 type = integer_type_node;
13560 break;
13561 case RID_INT128:
13562 if (!int128_integer_type_node)
13563 break;
13564 if (decl_specs)
13565 decl_specs->explicit_int128_p = true;
13566 type = int128_integer_type_node;
13567 break;
13568 case RID_LONG:
13569 if (decl_specs)
13570 set_and_check_decl_spec_loc (decl_specs, ds_long, token->location);
13571 type = long_integer_type_node;
13572 break;
13573 case RID_SIGNED:
13574 set_and_check_decl_spec_loc (decl_specs, ds_signed, token->location);
13575 type = integer_type_node;
13576 break;
13577 case RID_UNSIGNED:
13578 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token->location);
13579 type = unsigned_type_node;
13580 break;
13581 case RID_FLOAT:
13582 type = float_type_node;
13583 break;
13584 case RID_DOUBLE:
13585 type = double_type_node;
13586 break;
13587 case RID_VOID:
13588 type = void_type_node;
13589 break;
13591 case RID_AUTO:
13592 maybe_warn_cpp0x (CPP0X_AUTO);
13593 type = make_auto ();
13594 break;
13596 case RID_DECLTYPE:
13597 /* Since DR 743, decltype can either be a simple-type-specifier by
13598 itself or begin a nested-name-specifier. Parsing it will replace
13599 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13600 handling below decide what to do. */
13601 cp_parser_decltype (parser);
13602 cp_lexer_set_token_position (parser->lexer, token);
13603 break;
13605 case RID_TYPEOF:
13606 /* Consume the `typeof' token. */
13607 cp_lexer_consume_token (parser->lexer);
13608 /* Parse the operand to `typeof'. */
13609 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13610 /* If it is not already a TYPE, take its type. */
13611 if (!TYPE_P (type))
13612 type = finish_typeof (type);
13614 if (decl_specs)
13615 cp_parser_set_decl_spec_type (decl_specs, type,
13616 token->location,
13617 /*type_definition_p=*/false);
13619 return type;
13621 case RID_UNDERLYING_TYPE:
13622 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13623 if (decl_specs)
13624 cp_parser_set_decl_spec_type (decl_specs, type,
13625 token->location,
13626 /*type_definition_p=*/false);
13628 return type;
13630 case RID_BASES:
13631 case RID_DIRECT_BASES:
13632 type = cp_parser_trait_expr (parser, token->keyword);
13633 if (decl_specs)
13634 cp_parser_set_decl_spec_type (decl_specs, type,
13635 token->location,
13636 /*type_definition_p=*/false);
13637 return type;
13638 default:
13639 break;
13642 /* If token is an already-parsed decltype not followed by ::,
13643 it's a simple-type-specifier. */
13644 if (token->type == CPP_DECLTYPE
13645 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13647 type = token->u.value;
13648 if (decl_specs)
13649 cp_parser_set_decl_spec_type (decl_specs, type,
13650 token->location,
13651 /*type_definition_p=*/false);
13652 cp_lexer_consume_token (parser->lexer);
13653 return type;
13656 /* If the type-specifier was for a built-in type, we're done. */
13657 if (type)
13659 /* Record the type. */
13660 if (decl_specs
13661 && (token->keyword != RID_SIGNED
13662 && token->keyword != RID_UNSIGNED
13663 && token->keyword != RID_SHORT
13664 && token->keyword != RID_LONG))
13665 cp_parser_set_decl_spec_type (decl_specs,
13666 type,
13667 token->location,
13668 /*type_definition_p=*/false);
13669 if (decl_specs)
13670 decl_specs->any_specifiers_p = true;
13672 /* Consume the token. */
13673 cp_lexer_consume_token (parser->lexer);
13675 /* There is no valid C++ program where a non-template type is
13676 followed by a "<". That usually indicates that the user thought
13677 that the type was a template. */
13678 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13679 token->location);
13681 return TYPE_NAME (type);
13684 /* The type-specifier must be a user-defined type. */
13685 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13687 bool qualified_p;
13688 bool global_p;
13690 /* Don't gobble tokens or issue error messages if this is an
13691 optional type-specifier. */
13692 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13693 cp_parser_parse_tentatively (parser);
13695 /* Look for the optional `::' operator. */
13696 global_p
13697 = (cp_parser_global_scope_opt (parser,
13698 /*current_scope_valid_p=*/false)
13699 != NULL_TREE);
13700 /* Look for the nested-name specifier. */
13701 qualified_p
13702 = (cp_parser_nested_name_specifier_opt (parser,
13703 /*typename_keyword_p=*/false,
13704 /*check_dependency_p=*/true,
13705 /*type_p=*/false,
13706 /*is_declaration=*/false)
13707 != NULL_TREE);
13708 token = cp_lexer_peek_token (parser->lexer);
13709 /* If we have seen a nested-name-specifier, and the next token
13710 is `template', then we are using the template-id production. */
13711 if (parser->scope
13712 && cp_parser_optional_template_keyword (parser))
13714 /* Look for the template-id. */
13715 type = cp_parser_template_id (parser,
13716 /*template_keyword_p=*/true,
13717 /*check_dependency_p=*/true,
13718 none_type,
13719 /*is_declaration=*/false);
13720 /* If the template-id did not name a type, we are out of
13721 luck. */
13722 if (TREE_CODE (type) != TYPE_DECL)
13724 cp_parser_error (parser, "expected template-id for type");
13725 type = NULL_TREE;
13728 /* Otherwise, look for a type-name. */
13729 else
13730 type = cp_parser_type_name (parser);
13731 /* Keep track of all name-lookups performed in class scopes. */
13732 if (type
13733 && !global_p
13734 && !qualified_p
13735 && TREE_CODE (type) == TYPE_DECL
13736 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13737 maybe_note_name_used_in_class (DECL_NAME (type), type);
13738 /* If it didn't work out, we don't have a TYPE. */
13739 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13740 && !cp_parser_parse_definitely (parser))
13741 type = NULL_TREE;
13742 if (type && decl_specs)
13743 cp_parser_set_decl_spec_type (decl_specs, type,
13744 token->location,
13745 /*type_definition_p=*/false);
13748 /* If we didn't get a type-name, issue an error message. */
13749 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13751 cp_parser_error (parser, "expected type-name");
13752 return error_mark_node;
13755 if (type && type != error_mark_node)
13757 /* See if TYPE is an Objective-C type, and if so, parse and
13758 accept any protocol references following it. Do this before
13759 the cp_parser_check_for_invalid_template_id() call, because
13760 Objective-C types can be followed by '<...>' which would
13761 enclose protocol names rather than template arguments, and so
13762 everything is fine. */
13763 if (c_dialect_objc () && !parser->scope
13764 && (objc_is_id (type) || objc_is_class_name (type)))
13766 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13767 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13769 /* Clobber the "unqualified" type previously entered into
13770 DECL_SPECS with the new, improved protocol-qualified version. */
13771 if (decl_specs)
13772 decl_specs->type = qual_type;
13774 return qual_type;
13777 /* There is no valid C++ program where a non-template type is
13778 followed by a "<". That usually indicates that the user
13779 thought that the type was a template. */
13780 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13781 none_type,
13782 token->location);
13785 return type;
13788 /* Parse a type-name.
13790 type-name:
13791 class-name
13792 enum-name
13793 typedef-name
13794 simple-template-id [in c++0x]
13796 enum-name:
13797 identifier
13799 typedef-name:
13800 identifier
13802 Returns a TYPE_DECL for the type. */
13804 static tree
13805 cp_parser_type_name (cp_parser* parser)
13807 tree type_decl;
13809 /* We can't know yet whether it is a class-name or not. */
13810 cp_parser_parse_tentatively (parser);
13811 /* Try a class-name. */
13812 type_decl = cp_parser_class_name (parser,
13813 /*typename_keyword_p=*/false,
13814 /*template_keyword_p=*/false,
13815 none_type,
13816 /*check_dependency_p=*/true,
13817 /*class_head_p=*/false,
13818 /*is_declaration=*/false);
13819 /* If it's not a class-name, keep looking. */
13820 if (!cp_parser_parse_definitely (parser))
13822 if (cxx_dialect < cxx0x)
13823 /* It must be a typedef-name or an enum-name. */
13824 return cp_parser_nonclass_name (parser);
13826 cp_parser_parse_tentatively (parser);
13827 /* It is either a simple-template-id representing an
13828 instantiation of an alias template... */
13829 type_decl = cp_parser_template_id (parser,
13830 /*template_keyword_p=*/false,
13831 /*check_dependency_p=*/false,
13832 none_type,
13833 /*is_declaration=*/false);
13834 /* Note that this must be an instantiation of an alias template
13835 because [temp.names]/6 says:
13837 A template-id that names an alias template specialization
13838 is a type-name.
13840 Whereas [temp.names]/7 says:
13842 A simple-template-id that names a class template
13843 specialization is a class-name. */
13844 if (type_decl != NULL_TREE
13845 && TREE_CODE (type_decl) == TYPE_DECL
13846 && TYPE_DECL_ALIAS_P (type_decl))
13847 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13848 else
13849 cp_parser_simulate_error (parser);
13851 if (!cp_parser_parse_definitely (parser))
13852 /* ... Or a typedef-name or an enum-name. */
13853 return cp_parser_nonclass_name (parser);
13856 return type_decl;
13859 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13861 enum-name:
13862 identifier
13864 typedef-name:
13865 identifier
13867 Returns a TYPE_DECL for the type. */
13869 static tree
13870 cp_parser_nonclass_name (cp_parser* parser)
13872 tree type_decl;
13873 tree identifier;
13875 cp_token *token = cp_lexer_peek_token (parser->lexer);
13876 identifier = cp_parser_identifier (parser);
13877 if (identifier == error_mark_node)
13878 return error_mark_node;
13880 /* Look up the type-name. */
13881 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13883 if (TREE_CODE (type_decl) == USING_DECL)
13885 if (!DECL_DEPENDENT_P (type_decl))
13886 type_decl = strip_using_decl (type_decl);
13887 else if (USING_DECL_TYPENAME_P (type_decl))
13889 /* We have found a type introduced by a using
13890 declaration at class scope that refers to a dependent
13891 type.
13893 using typename :: [opt] nested-name-specifier unqualified-id ;
13895 type_decl = make_typename_type (TREE_TYPE (type_decl),
13896 DECL_NAME (type_decl),
13897 typename_type, tf_error);
13898 if (type_decl != error_mark_node)
13899 type_decl = TYPE_NAME (type_decl);
13903 if (TREE_CODE (type_decl) != TYPE_DECL
13904 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13906 /* See if this is an Objective-C type. */
13907 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13908 tree type = objc_get_protocol_qualified_type (identifier, protos);
13909 if (type)
13910 type_decl = TYPE_NAME (type);
13913 /* Issue an error if we did not find a type-name. */
13914 if (TREE_CODE (type_decl) != TYPE_DECL
13915 /* In Objective-C, we have the complication that class names are
13916 normally type names and start declarations (eg, the
13917 "NSObject" in "NSObject *object;"), but can be used in an
13918 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13919 is an expression. So, a classname followed by a dot is not a
13920 valid type-name. */
13921 || (objc_is_class_name (TREE_TYPE (type_decl))
13922 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13924 if (!cp_parser_simulate_error (parser))
13925 cp_parser_name_lookup_error (parser, identifier, type_decl,
13926 NLE_TYPE, token->location);
13927 return error_mark_node;
13929 /* Remember that the name was used in the definition of the
13930 current class so that we can check later to see if the
13931 meaning would have been different after the class was
13932 entirely defined. */
13933 else if (type_decl != error_mark_node
13934 && !parser->scope)
13935 maybe_note_name_used_in_class (identifier, type_decl);
13937 return type_decl;
13940 /* Parse an elaborated-type-specifier. Note that the grammar given
13941 here incorporates the resolution to DR68.
13943 elaborated-type-specifier:
13944 class-key :: [opt] nested-name-specifier [opt] identifier
13945 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13946 enum-key :: [opt] nested-name-specifier [opt] identifier
13947 typename :: [opt] nested-name-specifier identifier
13948 typename :: [opt] nested-name-specifier template [opt]
13949 template-id
13951 GNU extension:
13953 elaborated-type-specifier:
13954 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13955 class-key attributes :: [opt] nested-name-specifier [opt]
13956 template [opt] template-id
13957 enum attributes :: [opt] nested-name-specifier [opt] identifier
13959 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13960 declared `friend'. If IS_DECLARATION is TRUE, then this
13961 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13962 something is being declared.
13964 Returns the TYPE specified. */
13966 static tree
13967 cp_parser_elaborated_type_specifier (cp_parser* parser,
13968 bool is_friend,
13969 bool is_declaration)
13971 enum tag_types tag_type;
13972 tree identifier;
13973 tree type = NULL_TREE;
13974 tree attributes = NULL_TREE;
13975 tree globalscope;
13976 cp_token *token = NULL;
13978 /* See if we're looking at the `enum' keyword. */
13979 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13981 /* Consume the `enum' token. */
13982 cp_lexer_consume_token (parser->lexer);
13983 /* Remember that it's an enumeration type. */
13984 tag_type = enum_type;
13985 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13986 enums) is used here. */
13987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13988 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13990 pedwarn (input_location, 0, "elaborated-type-specifier "
13991 "for a scoped enum must not use the %<%D%> keyword",
13992 cp_lexer_peek_token (parser->lexer)->u.value);
13993 /* Consume the `struct' or `class' and parse it anyway. */
13994 cp_lexer_consume_token (parser->lexer);
13996 /* Parse the attributes. */
13997 attributes = cp_parser_attributes_opt (parser);
13999 /* Or, it might be `typename'. */
14000 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14001 RID_TYPENAME))
14003 /* Consume the `typename' token. */
14004 cp_lexer_consume_token (parser->lexer);
14005 /* Remember that it's a `typename' type. */
14006 tag_type = typename_type;
14008 /* Otherwise it must be a class-key. */
14009 else
14011 tag_type = cp_parser_class_key (parser);
14012 if (tag_type == none_type)
14013 return error_mark_node;
14014 /* Parse the attributes. */
14015 attributes = cp_parser_attributes_opt (parser);
14018 /* Look for the `::' operator. */
14019 globalscope = cp_parser_global_scope_opt (parser,
14020 /*current_scope_valid_p=*/false);
14021 /* Look for the nested-name-specifier. */
14022 if (tag_type == typename_type && !globalscope)
14024 if (!cp_parser_nested_name_specifier (parser,
14025 /*typename_keyword_p=*/true,
14026 /*check_dependency_p=*/true,
14027 /*type_p=*/true,
14028 is_declaration))
14029 return error_mark_node;
14031 else
14032 /* Even though `typename' is not present, the proposed resolution
14033 to Core Issue 180 says that in `class A<T>::B', `B' should be
14034 considered a type-name, even if `A<T>' is dependent. */
14035 cp_parser_nested_name_specifier_opt (parser,
14036 /*typename_keyword_p=*/true,
14037 /*check_dependency_p=*/true,
14038 /*type_p=*/true,
14039 is_declaration);
14040 /* For everything but enumeration types, consider a template-id.
14041 For an enumeration type, consider only a plain identifier. */
14042 if (tag_type != enum_type)
14044 bool template_p = false;
14045 tree decl;
14047 /* Allow the `template' keyword. */
14048 template_p = cp_parser_optional_template_keyword (parser);
14049 /* If we didn't see `template', we don't know if there's a
14050 template-id or not. */
14051 if (!template_p)
14052 cp_parser_parse_tentatively (parser);
14053 /* Parse the template-id. */
14054 token = cp_lexer_peek_token (parser->lexer);
14055 decl = cp_parser_template_id (parser, template_p,
14056 /*check_dependency_p=*/true,
14057 tag_type,
14058 is_declaration);
14059 /* If we didn't find a template-id, look for an ordinary
14060 identifier. */
14061 if (!template_p && !cp_parser_parse_definitely (parser))
14063 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14064 in effect, then we must assume that, upon instantiation, the
14065 template will correspond to a class. */
14066 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14067 && tag_type == typename_type)
14068 type = make_typename_type (parser->scope, decl,
14069 typename_type,
14070 /*complain=*/tf_error);
14071 /* If the `typename' keyword is in effect and DECL is not a type
14072 decl. Then type is non existant. */
14073 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14074 type = NULL_TREE;
14075 else
14076 type = check_elaborated_type_specifier (tag_type, decl,
14077 /*allow_template_p=*/true);
14080 if (!type)
14082 token = cp_lexer_peek_token (parser->lexer);
14083 identifier = cp_parser_identifier (parser);
14085 if (identifier == error_mark_node)
14087 parser->scope = NULL_TREE;
14088 return error_mark_node;
14091 /* For a `typename', we needn't call xref_tag. */
14092 if (tag_type == typename_type
14093 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14094 return cp_parser_make_typename_type (parser, parser->scope,
14095 identifier,
14096 token->location);
14097 /* Look up a qualified name in the usual way. */
14098 if (parser->scope)
14100 tree decl;
14101 tree ambiguous_decls;
14103 decl = cp_parser_lookup_name (parser, identifier,
14104 tag_type,
14105 /*is_template=*/false,
14106 /*is_namespace=*/false,
14107 /*check_dependency=*/true,
14108 &ambiguous_decls,
14109 token->location);
14111 /* If the lookup was ambiguous, an error will already have been
14112 issued. */
14113 if (ambiguous_decls)
14114 return error_mark_node;
14116 /* If we are parsing friend declaration, DECL may be a
14117 TEMPLATE_DECL tree node here. However, we need to check
14118 whether this TEMPLATE_DECL results in valid code. Consider
14119 the following example:
14121 namespace N {
14122 template <class T> class C {};
14124 class X {
14125 template <class T> friend class N::C; // #1, valid code
14127 template <class T> class Y {
14128 friend class N::C; // #2, invalid code
14131 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14132 name lookup of `N::C'. We see that friend declaration must
14133 be template for the code to be valid. Note that
14134 processing_template_decl does not work here since it is
14135 always 1 for the above two cases. */
14137 decl = (cp_parser_maybe_treat_template_as_class
14138 (decl, /*tag_name_p=*/is_friend
14139 && parser->num_template_parameter_lists));
14141 if (TREE_CODE (decl) != TYPE_DECL)
14143 cp_parser_diagnose_invalid_type_name (parser,
14144 parser->scope,
14145 identifier,
14146 token->location);
14147 return error_mark_node;
14150 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14152 bool allow_template = (parser->num_template_parameter_lists
14153 || DECL_SELF_REFERENCE_P (decl));
14154 type = check_elaborated_type_specifier (tag_type, decl,
14155 allow_template);
14157 if (type == error_mark_node)
14158 return error_mark_node;
14161 /* Forward declarations of nested types, such as
14163 class C1::C2;
14164 class C1::C2::C3;
14166 are invalid unless all components preceding the final '::'
14167 are complete. If all enclosing types are complete, these
14168 declarations become merely pointless.
14170 Invalid forward declarations of nested types are errors
14171 caught elsewhere in parsing. Those that are pointless arrive
14172 here. */
14174 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14175 && !is_friend && !processing_explicit_instantiation)
14176 warning (0, "declaration %qD does not declare anything", decl);
14178 type = TREE_TYPE (decl);
14180 else
14182 /* An elaborated-type-specifier sometimes introduces a new type and
14183 sometimes names an existing type. Normally, the rule is that it
14184 introduces a new type only if there is not an existing type of
14185 the same name already in scope. For example, given:
14187 struct S {};
14188 void f() { struct S s; }
14190 the `struct S' in the body of `f' is the same `struct S' as in
14191 the global scope; the existing definition is used. However, if
14192 there were no global declaration, this would introduce a new
14193 local class named `S'.
14195 An exception to this rule applies to the following code:
14197 namespace N { struct S; }
14199 Here, the elaborated-type-specifier names a new type
14200 unconditionally; even if there is already an `S' in the
14201 containing scope this declaration names a new type.
14202 This exception only applies if the elaborated-type-specifier
14203 forms the complete declaration:
14205 [class.name]
14207 A declaration consisting solely of `class-key identifier ;' is
14208 either a redeclaration of the name in the current scope or a
14209 forward declaration of the identifier as a class name. It
14210 introduces the name into the current scope.
14212 We are in this situation precisely when the next token is a `;'.
14214 An exception to the exception is that a `friend' declaration does
14215 *not* name a new type; i.e., given:
14217 struct S { friend struct T; };
14219 `T' is not a new type in the scope of `S'.
14221 Also, `new struct S' or `sizeof (struct S)' never results in the
14222 definition of a new type; a new type can only be declared in a
14223 declaration context. */
14225 tag_scope ts;
14226 bool template_p;
14228 if (is_friend)
14229 /* Friends have special name lookup rules. */
14230 ts = ts_within_enclosing_non_class;
14231 else if (is_declaration
14232 && cp_lexer_next_token_is (parser->lexer,
14233 CPP_SEMICOLON))
14234 /* This is a `class-key identifier ;' */
14235 ts = ts_current;
14236 else
14237 ts = ts_global;
14239 template_p =
14240 (parser->num_template_parameter_lists
14241 && (cp_parser_next_token_starts_class_definition_p (parser)
14242 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14243 /* An unqualified name was used to reference this type, so
14244 there were no qualifying templates. */
14245 if (!cp_parser_check_template_parameters (parser,
14246 /*num_templates=*/0,
14247 token->location,
14248 /*declarator=*/NULL))
14249 return error_mark_node;
14250 type = xref_tag (tag_type, identifier, ts, template_p);
14254 if (type == error_mark_node)
14255 return error_mark_node;
14257 /* Allow attributes on forward declarations of classes. */
14258 if (attributes)
14260 if (TREE_CODE (type) == TYPENAME_TYPE)
14261 warning (OPT_Wattributes,
14262 "attributes ignored on uninstantiated type");
14263 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14264 && ! processing_explicit_instantiation)
14265 warning (OPT_Wattributes,
14266 "attributes ignored on template instantiation");
14267 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14268 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14269 else
14270 warning (OPT_Wattributes,
14271 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14274 if (tag_type != enum_type)
14276 /* Indicate whether this class was declared as a `class' or as a
14277 `struct'. */
14278 if (TREE_CODE (type) == RECORD_TYPE)
14279 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14280 cp_parser_check_class_key (tag_type, type);
14283 /* A "<" cannot follow an elaborated type specifier. If that
14284 happens, the user was probably trying to form a template-id. */
14285 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14286 token->location);
14288 return type;
14291 /* Parse an enum-specifier.
14293 enum-specifier:
14294 enum-head { enumerator-list [opt] }
14295 enum-head { enumerator-list , } [C++0x]
14297 enum-head:
14298 enum-key identifier [opt] enum-base [opt]
14299 enum-key nested-name-specifier identifier enum-base [opt]
14301 enum-key:
14302 enum
14303 enum class [C++0x]
14304 enum struct [C++0x]
14306 enum-base: [C++0x]
14307 : type-specifier-seq
14309 opaque-enum-specifier:
14310 enum-key identifier enum-base [opt] ;
14312 GNU Extensions:
14313 enum-key attributes[opt] identifier [opt] enum-base [opt]
14314 { enumerator-list [opt] }attributes[opt]
14315 enum-key attributes[opt] identifier [opt] enum-base [opt]
14316 { enumerator-list, }attributes[opt] [C++0x]
14318 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14319 if the token stream isn't an enum-specifier after all. */
14321 static tree
14322 cp_parser_enum_specifier (cp_parser* parser)
14324 tree identifier;
14325 tree type = NULL_TREE;
14326 tree prev_scope;
14327 tree nested_name_specifier = NULL_TREE;
14328 tree attributes;
14329 bool scoped_enum_p = false;
14330 bool has_underlying_type = false;
14331 bool nested_being_defined = false;
14332 bool new_value_list = false;
14333 bool is_new_type = false;
14334 bool is_anonymous = false;
14335 tree underlying_type = NULL_TREE;
14336 cp_token *type_start_token = NULL;
14337 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14339 parser->colon_corrects_to_scope_p = false;
14341 /* Parse tentatively so that we can back up if we don't find a
14342 enum-specifier. */
14343 cp_parser_parse_tentatively (parser);
14345 /* Caller guarantees that the current token is 'enum', an identifier
14346 possibly follows, and the token after that is an opening brace.
14347 If we don't have an identifier, fabricate an anonymous name for
14348 the enumeration being defined. */
14349 cp_lexer_consume_token (parser->lexer);
14351 /* Parse the "class" or "struct", which indicates a scoped
14352 enumeration type in C++0x. */
14353 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14354 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14356 if (cxx_dialect < cxx0x)
14357 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14359 /* Consume the `struct' or `class' token. */
14360 cp_lexer_consume_token (parser->lexer);
14362 scoped_enum_p = true;
14365 attributes = cp_parser_attributes_opt (parser);
14367 /* Clear the qualification. */
14368 parser->scope = NULL_TREE;
14369 parser->qualifying_scope = NULL_TREE;
14370 parser->object_scope = NULL_TREE;
14372 /* Figure out in what scope the declaration is being placed. */
14373 prev_scope = current_scope ();
14375 type_start_token = cp_lexer_peek_token (parser->lexer);
14377 push_deferring_access_checks (dk_no_check);
14378 nested_name_specifier
14379 = cp_parser_nested_name_specifier_opt (parser,
14380 /*typename_keyword_p=*/true,
14381 /*check_dependency_p=*/false,
14382 /*type_p=*/false,
14383 /*is_declaration=*/false);
14385 if (nested_name_specifier)
14387 tree name;
14389 identifier = cp_parser_identifier (parser);
14390 name = cp_parser_lookup_name (parser, identifier,
14391 enum_type,
14392 /*is_template=*/false,
14393 /*is_namespace=*/false,
14394 /*check_dependency=*/true,
14395 /*ambiguous_decls=*/NULL,
14396 input_location);
14397 if (name)
14399 type = TREE_TYPE (name);
14400 if (TREE_CODE (type) == TYPENAME_TYPE)
14402 /* Are template enums allowed in ISO? */
14403 if (template_parm_scope_p ())
14404 pedwarn (type_start_token->location, OPT_Wpedantic,
14405 "%qD is an enumeration template", name);
14406 /* ignore a typename reference, for it will be solved by name
14407 in start_enum. */
14408 type = NULL_TREE;
14411 else
14412 error_at (type_start_token->location,
14413 "%qD is not an enumerator-name", identifier);
14415 else
14417 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14418 identifier = cp_parser_identifier (parser);
14419 else
14421 identifier = make_anon_name ();
14422 is_anonymous = true;
14425 pop_deferring_access_checks ();
14427 /* Check for the `:' that denotes a specified underlying type in C++0x.
14428 Note that a ':' could also indicate a bitfield width, however. */
14429 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14431 cp_decl_specifier_seq type_specifiers;
14433 /* Consume the `:'. */
14434 cp_lexer_consume_token (parser->lexer);
14436 /* Parse the type-specifier-seq. */
14437 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14438 /*is_trailing_return=*/false,
14439 &type_specifiers);
14441 /* At this point this is surely not elaborated type specifier. */
14442 if (!cp_parser_parse_definitely (parser))
14443 return NULL_TREE;
14445 if (cxx_dialect < cxx0x)
14446 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14448 has_underlying_type = true;
14450 /* If that didn't work, stop. */
14451 if (type_specifiers.type != error_mark_node)
14453 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14454 /*initialized=*/0, NULL);
14455 if (underlying_type == error_mark_node)
14456 underlying_type = NULL_TREE;
14460 /* Look for the `{' but don't consume it yet. */
14461 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14463 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14465 cp_parser_error (parser, "expected %<{%>");
14466 if (has_underlying_type)
14468 type = NULL_TREE;
14469 goto out;
14472 /* An opaque-enum-specifier must have a ';' here. */
14473 if ((scoped_enum_p || underlying_type)
14474 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14476 cp_parser_error (parser, "expected %<;%> or %<{%>");
14477 if (has_underlying_type)
14479 type = NULL_TREE;
14480 goto out;
14485 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14486 return NULL_TREE;
14488 if (nested_name_specifier)
14490 if (CLASS_TYPE_P (nested_name_specifier))
14492 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14493 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14494 push_scope (nested_name_specifier);
14496 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14498 push_nested_namespace (nested_name_specifier);
14502 /* Issue an error message if type-definitions are forbidden here. */
14503 if (!cp_parser_check_type_definition (parser))
14504 type = error_mark_node;
14505 else
14506 /* Create the new type. We do this before consuming the opening
14507 brace so the enum will be recorded as being on the line of its
14508 tag (or the 'enum' keyword, if there is no tag). */
14509 type = start_enum (identifier, type, underlying_type,
14510 scoped_enum_p, &is_new_type);
14512 /* If the next token is not '{' it is an opaque-enum-specifier or an
14513 elaborated-type-specifier. */
14514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14516 timevar_push (TV_PARSE_ENUM);
14517 if (nested_name_specifier)
14519 /* The following catches invalid code such as:
14520 enum class S<int>::E { A, B, C }; */
14521 if (!processing_specialization
14522 && CLASS_TYPE_P (nested_name_specifier)
14523 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14524 error_at (type_start_token->location, "cannot add an enumerator "
14525 "list to a template instantiation");
14527 /* If that scope does not contain the scope in which the
14528 class was originally declared, the program is invalid. */
14529 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14531 if (at_namespace_scope_p ())
14532 error_at (type_start_token->location,
14533 "declaration of %qD in namespace %qD which does not "
14534 "enclose %qD",
14535 type, prev_scope, nested_name_specifier);
14536 else
14537 error_at (type_start_token->location,
14538 "declaration of %qD in %qD which does not enclose %qD",
14539 type, prev_scope, nested_name_specifier);
14540 type = error_mark_node;
14544 if (scoped_enum_p)
14545 begin_scope (sk_scoped_enum, type);
14547 /* Consume the opening brace. */
14548 cp_lexer_consume_token (parser->lexer);
14550 if (type == error_mark_node)
14551 ; /* Nothing to add */
14552 else if (OPAQUE_ENUM_P (type)
14553 || (cxx_dialect > cxx98 && processing_specialization))
14555 new_value_list = true;
14556 SET_OPAQUE_ENUM_P (type, false);
14557 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14559 else
14561 error_at (type_start_token->location, "multiple definition of %q#T", type);
14562 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14563 "previous definition here");
14564 type = error_mark_node;
14567 if (type == error_mark_node)
14568 cp_parser_skip_to_end_of_block_or_statement (parser);
14569 /* If the next token is not '}', then there are some enumerators. */
14570 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14571 cp_parser_enumerator_list (parser, type);
14573 /* Consume the final '}'. */
14574 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14576 if (scoped_enum_p)
14577 finish_scope ();
14578 timevar_pop (TV_PARSE_ENUM);
14580 else
14582 /* If a ';' follows, then it is an opaque-enum-specifier
14583 and additional restrictions apply. */
14584 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14586 if (is_anonymous)
14587 error_at (type_start_token->location,
14588 "opaque-enum-specifier without name");
14589 else if (nested_name_specifier)
14590 error_at (type_start_token->location,
14591 "opaque-enum-specifier must use a simple identifier");
14595 /* Look for trailing attributes to apply to this enumeration, and
14596 apply them if appropriate. */
14597 if (cp_parser_allow_gnu_extensions_p (parser))
14599 tree trailing_attr = cp_parser_attributes_opt (parser);
14600 trailing_attr = chainon (trailing_attr, attributes);
14601 cplus_decl_attributes (&type,
14602 trailing_attr,
14603 (int) ATTR_FLAG_TYPE_IN_PLACE);
14606 /* Finish up the enumeration. */
14607 if (type != error_mark_node)
14609 if (new_value_list)
14610 finish_enum_value_list (type);
14611 if (is_new_type)
14612 finish_enum (type);
14615 if (nested_name_specifier)
14617 if (CLASS_TYPE_P (nested_name_specifier))
14619 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14620 pop_scope (nested_name_specifier);
14622 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14624 pop_nested_namespace (nested_name_specifier);
14627 out:
14628 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14629 return type;
14632 /* Parse an enumerator-list. The enumerators all have the indicated
14633 TYPE.
14635 enumerator-list:
14636 enumerator-definition
14637 enumerator-list , enumerator-definition */
14639 static void
14640 cp_parser_enumerator_list (cp_parser* parser, tree type)
14642 while (true)
14644 /* Parse an enumerator-definition. */
14645 cp_parser_enumerator_definition (parser, type);
14647 /* If the next token is not a ',', we've reached the end of
14648 the list. */
14649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14650 break;
14651 /* Otherwise, consume the `,' and keep going. */
14652 cp_lexer_consume_token (parser->lexer);
14653 /* If the next token is a `}', there is a trailing comma. */
14654 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14656 if (cxx_dialect < cxx0x && !in_system_header)
14657 pedwarn (input_location, OPT_Wpedantic,
14658 "comma at end of enumerator list");
14659 break;
14664 /* Parse an enumerator-definition. The enumerator has the indicated
14665 TYPE.
14667 enumerator-definition:
14668 enumerator
14669 enumerator = constant-expression
14671 enumerator:
14672 identifier */
14674 static void
14675 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14677 tree identifier;
14678 tree value;
14679 location_t loc;
14681 /* Save the input location because we are interested in the location
14682 of the identifier and not the location of the explicit value. */
14683 loc = cp_lexer_peek_token (parser->lexer)->location;
14685 /* Look for the identifier. */
14686 identifier = cp_parser_identifier (parser);
14687 if (identifier == error_mark_node)
14688 return;
14690 /* If the next token is an '=', then there is an explicit value. */
14691 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14693 /* Consume the `=' token. */
14694 cp_lexer_consume_token (parser->lexer);
14695 /* Parse the value. */
14696 value = cp_parser_constant_expression (parser,
14697 /*allow_non_constant_p=*/false,
14698 NULL);
14700 else
14701 value = NULL_TREE;
14703 /* If we are processing a template, make sure the initializer of the
14704 enumerator doesn't contain any bare template parameter pack. */
14705 if (check_for_bare_parameter_packs (value))
14706 value = error_mark_node;
14708 /* integral_constant_value will pull out this expression, so make sure
14709 it's folded as appropriate. */
14710 value = fold_non_dependent_expr (value);
14712 /* Create the enumerator. */
14713 build_enumerator (identifier, value, type, loc);
14716 /* Parse a namespace-name.
14718 namespace-name:
14719 original-namespace-name
14720 namespace-alias
14722 Returns the NAMESPACE_DECL for the namespace. */
14724 static tree
14725 cp_parser_namespace_name (cp_parser* parser)
14727 tree identifier;
14728 tree namespace_decl;
14730 cp_token *token = cp_lexer_peek_token (parser->lexer);
14732 /* Get the name of the namespace. */
14733 identifier = cp_parser_identifier (parser);
14734 if (identifier == error_mark_node)
14735 return error_mark_node;
14737 /* Look up the identifier in the currently active scope. Look only
14738 for namespaces, due to:
14740 [basic.lookup.udir]
14742 When looking up a namespace-name in a using-directive or alias
14743 definition, only namespace names are considered.
14745 And:
14747 [basic.lookup.qual]
14749 During the lookup of a name preceding the :: scope resolution
14750 operator, object, function, and enumerator names are ignored.
14752 (Note that cp_parser_qualifying_entity only calls this
14753 function if the token after the name is the scope resolution
14754 operator.) */
14755 namespace_decl = cp_parser_lookup_name (parser, identifier,
14756 none_type,
14757 /*is_template=*/false,
14758 /*is_namespace=*/true,
14759 /*check_dependency=*/true,
14760 /*ambiguous_decls=*/NULL,
14761 token->location);
14762 /* If it's not a namespace, issue an error. */
14763 if (namespace_decl == error_mark_node
14764 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14766 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14767 error_at (token->location, "%qD is not a namespace-name", identifier);
14768 cp_parser_error (parser, "expected namespace-name");
14769 namespace_decl = error_mark_node;
14772 return namespace_decl;
14775 /* Parse a namespace-definition.
14777 namespace-definition:
14778 named-namespace-definition
14779 unnamed-namespace-definition
14781 named-namespace-definition:
14782 original-namespace-definition
14783 extension-namespace-definition
14785 original-namespace-definition:
14786 namespace identifier { namespace-body }
14788 extension-namespace-definition:
14789 namespace original-namespace-name { namespace-body }
14791 unnamed-namespace-definition:
14792 namespace { namespace-body } */
14794 static void
14795 cp_parser_namespace_definition (cp_parser* parser)
14797 tree identifier, attribs;
14798 bool has_visibility;
14799 bool is_inline;
14801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14803 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14804 is_inline = true;
14805 cp_lexer_consume_token (parser->lexer);
14807 else
14808 is_inline = false;
14810 /* Look for the `namespace' keyword. */
14811 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14813 /* Get the name of the namespace. We do not attempt to distinguish
14814 between an original-namespace-definition and an
14815 extension-namespace-definition at this point. The semantic
14816 analysis routines are responsible for that. */
14817 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14818 identifier = cp_parser_identifier (parser);
14819 else
14820 identifier = NULL_TREE;
14822 /* Parse any specified attributes. */
14823 attribs = cp_parser_attributes_opt (parser);
14825 /* Look for the `{' to start the namespace. */
14826 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14827 /* Start the namespace. */
14828 push_namespace (identifier);
14830 /* "inline namespace" is equivalent to a stub namespace definition
14831 followed by a strong using directive. */
14832 if (is_inline)
14834 tree name_space = current_namespace;
14835 /* Set up namespace association. */
14836 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14837 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14838 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14839 /* Import the contents of the inline namespace. */
14840 pop_namespace ();
14841 do_using_directive (name_space);
14842 push_namespace (identifier);
14845 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14847 /* Parse the body of the namespace. */
14848 cp_parser_namespace_body (parser);
14850 if (has_visibility)
14851 pop_visibility (1);
14853 /* Finish the namespace. */
14854 pop_namespace ();
14855 /* Look for the final `}'. */
14856 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14859 /* Parse a namespace-body.
14861 namespace-body:
14862 declaration-seq [opt] */
14864 static void
14865 cp_parser_namespace_body (cp_parser* parser)
14867 cp_parser_declaration_seq_opt (parser);
14870 /* Parse a namespace-alias-definition.
14872 namespace-alias-definition:
14873 namespace identifier = qualified-namespace-specifier ; */
14875 static void
14876 cp_parser_namespace_alias_definition (cp_parser* parser)
14878 tree identifier;
14879 tree namespace_specifier;
14881 cp_token *token = cp_lexer_peek_token (parser->lexer);
14883 /* Look for the `namespace' keyword. */
14884 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14885 /* Look for the identifier. */
14886 identifier = cp_parser_identifier (parser);
14887 if (identifier == error_mark_node)
14888 return;
14889 /* Look for the `=' token. */
14890 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14891 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14893 error_at (token->location, "%<namespace%> definition is not allowed here");
14894 /* Skip the definition. */
14895 cp_lexer_consume_token (parser->lexer);
14896 if (cp_parser_skip_to_closing_brace (parser))
14897 cp_lexer_consume_token (parser->lexer);
14898 return;
14900 cp_parser_require (parser, CPP_EQ, RT_EQ);
14901 /* Look for the qualified-namespace-specifier. */
14902 namespace_specifier
14903 = cp_parser_qualified_namespace_specifier (parser);
14904 /* Look for the `;' token. */
14905 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14907 /* Register the alias in the symbol table. */
14908 do_namespace_alias (identifier, namespace_specifier);
14911 /* Parse a qualified-namespace-specifier.
14913 qualified-namespace-specifier:
14914 :: [opt] nested-name-specifier [opt] namespace-name
14916 Returns a NAMESPACE_DECL corresponding to the specified
14917 namespace. */
14919 static tree
14920 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14922 /* Look for the optional `::'. */
14923 cp_parser_global_scope_opt (parser,
14924 /*current_scope_valid_p=*/false);
14926 /* Look for the optional nested-name-specifier. */
14927 cp_parser_nested_name_specifier_opt (parser,
14928 /*typename_keyword_p=*/false,
14929 /*check_dependency_p=*/true,
14930 /*type_p=*/false,
14931 /*is_declaration=*/true);
14933 return cp_parser_namespace_name (parser);
14936 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14937 access declaration.
14939 using-declaration:
14940 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14941 using :: unqualified-id ;
14943 access-declaration:
14944 qualified-id ;
14948 static bool
14949 cp_parser_using_declaration (cp_parser* parser,
14950 bool access_declaration_p)
14952 cp_token *token;
14953 bool typename_p = false;
14954 bool global_scope_p;
14955 tree decl;
14956 tree identifier;
14957 tree qscope;
14958 int oldcount = errorcount;
14959 cp_token *diag_token = NULL;
14961 if (access_declaration_p)
14963 diag_token = cp_lexer_peek_token (parser->lexer);
14964 cp_parser_parse_tentatively (parser);
14966 else
14968 /* Look for the `using' keyword. */
14969 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14971 /* Peek at the next token. */
14972 token = cp_lexer_peek_token (parser->lexer);
14973 /* See if it's `typename'. */
14974 if (token->keyword == RID_TYPENAME)
14976 /* Remember that we've seen it. */
14977 typename_p = true;
14978 /* Consume the `typename' token. */
14979 cp_lexer_consume_token (parser->lexer);
14983 /* Look for the optional global scope qualification. */
14984 global_scope_p
14985 = (cp_parser_global_scope_opt (parser,
14986 /*current_scope_valid_p=*/false)
14987 != NULL_TREE);
14989 /* If we saw `typename', or didn't see `::', then there must be a
14990 nested-name-specifier present. */
14991 if (typename_p || !global_scope_p)
14992 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14993 /*check_dependency_p=*/true,
14994 /*type_p=*/false,
14995 /*is_declaration=*/true);
14996 /* Otherwise, we could be in either of the two productions. In that
14997 case, treat the nested-name-specifier as optional. */
14998 else
14999 qscope = cp_parser_nested_name_specifier_opt (parser,
15000 /*typename_keyword_p=*/false,
15001 /*check_dependency_p=*/true,
15002 /*type_p=*/false,
15003 /*is_declaration=*/true);
15004 if (!qscope)
15005 qscope = global_namespace;
15007 if (access_declaration_p && cp_parser_error_occurred (parser))
15008 /* Something has already gone wrong; there's no need to parse
15009 further. Since an error has occurred, the return value of
15010 cp_parser_parse_definitely will be false, as required. */
15011 return cp_parser_parse_definitely (parser);
15013 token = cp_lexer_peek_token (parser->lexer);
15014 /* Parse the unqualified-id. */
15015 identifier = cp_parser_unqualified_id (parser,
15016 /*template_keyword_p=*/false,
15017 /*check_dependency_p=*/true,
15018 /*declarator_p=*/true,
15019 /*optional_p=*/false);
15021 if (access_declaration_p)
15023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15024 cp_parser_simulate_error (parser);
15025 if (!cp_parser_parse_definitely (parser))
15026 return false;
15029 /* The function we call to handle a using-declaration is different
15030 depending on what scope we are in. */
15031 if (qscope == error_mark_node || identifier == error_mark_node)
15033 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15034 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15035 /* [namespace.udecl]
15037 A using declaration shall not name a template-id. */
15038 error_at (token->location,
15039 "a template-id may not appear in a using-declaration");
15040 else
15042 if (at_class_scope_p ())
15044 /* Create the USING_DECL. */
15045 decl = do_class_using_decl (parser->scope, identifier);
15047 if (decl && typename_p)
15048 USING_DECL_TYPENAME_P (decl) = 1;
15050 if (check_for_bare_parameter_packs (decl))
15051 return false;
15052 else
15053 /* Add it to the list of members in this class. */
15054 finish_member_declaration (decl);
15056 else
15058 decl = cp_parser_lookup_name_simple (parser,
15059 identifier,
15060 token->location);
15061 if (decl == error_mark_node)
15062 cp_parser_name_lookup_error (parser, identifier,
15063 decl, NLE_NULL,
15064 token->location);
15065 else if (check_for_bare_parameter_packs (decl))
15066 return false;
15067 else if (!at_namespace_scope_p ())
15068 do_local_using_decl (decl, qscope, identifier);
15069 else
15070 do_toplevel_using_decl (decl, qscope, identifier);
15074 /* Look for the final `;'. */
15075 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15077 if (access_declaration_p && errorcount == oldcount)
15078 warning_at (diag_token->location, OPT_Wdeprecated,
15079 "access declarations are deprecated "
15080 "in favour of using-declarations; "
15081 "suggestion: add the %<using%> keyword");
15083 return true;
15086 /* Parse an alias-declaration.
15088 alias-declaration:
15089 using identifier attribute-specifier-seq [opt] = type-id */
15091 static tree
15092 cp_parser_alias_declaration (cp_parser* parser)
15094 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15095 location_t id_location, using_location, attrs_location = 0;
15096 cp_declarator *declarator;
15097 cp_decl_specifier_seq decl_specs;
15098 bool member_p;
15099 const char *saved_message = NULL;
15101 /* Look for the `using' keyword. */
15102 using_location = cp_lexer_peek_token (parser->lexer)->location;
15103 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15104 id_location = cp_lexer_peek_token (parser->lexer)->location;
15105 id = cp_parser_identifier (parser);
15106 if (id == error_mark_node)
15107 return error_mark_node;
15109 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
15110 attributes = cp_parser_attributes_opt (parser);
15111 if (attributes == error_mark_node)
15112 return error_mark_node;
15114 cp_parser_require (parser, CPP_EQ, RT_EQ);
15116 if (cp_parser_error_occurred (parser))
15117 return error_mark_node;
15119 /* Now we are going to parse the type-id of the declaration. */
15122 [dcl.type]/3 says:
15124 "A type-specifier-seq shall not define a class or enumeration
15125 unless it appears in the type-id of an alias-declaration (7.1.3) that
15126 is not the declaration of a template-declaration."
15128 In other words, if we currently are in an alias template, the
15129 type-id should not define a type.
15131 So let's set parser->type_definition_forbidden_message in that
15132 case; cp_parser_check_type_definition (called by
15133 cp_parser_class_specifier) will then emit an error if a type is
15134 defined in the type-id. */
15135 if (parser->num_template_parameter_lists)
15137 saved_message = parser->type_definition_forbidden_message;
15138 parser->type_definition_forbidden_message =
15139 G_("types may not be defined in alias template declarations");
15142 type = cp_parser_type_id (parser);
15144 /* Restore the error message if need be. */
15145 if (parser->num_template_parameter_lists)
15146 parser->type_definition_forbidden_message = saved_message;
15148 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15150 if (cp_parser_error_occurred (parser))
15151 return error_mark_node;
15153 /* A typedef-name can also be introduced by an alias-declaration. The
15154 identifier following the using keyword becomes a typedef-name. It has
15155 the same semantics as if it were introduced by the typedef
15156 specifier. In particular, it does not define a new type and it shall
15157 not appear in the type-id. */
15159 clear_decl_specs (&decl_specs);
15160 decl_specs.type = type;
15161 if (attributes != NULL_TREE)
15163 decl_specs.attributes = attributes;
15164 set_and_check_decl_spec_loc (&decl_specs,
15165 ds_attribute,
15166 attrs_location);
15168 set_and_check_decl_spec_loc (&decl_specs,
15169 ds_typedef,
15170 using_location);
15171 set_and_check_decl_spec_loc (&decl_specs,
15172 ds_alias,
15173 using_location);
15175 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15176 declarator->id_loc = id_location;
15178 member_p = at_class_scope_p ();
15179 if (member_p)
15180 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15181 NULL_TREE, attributes);
15182 else
15183 decl = start_decl (declarator, &decl_specs, 0,
15184 attributes, NULL_TREE, &pushed_scope);
15185 if (decl == error_mark_node)
15186 return decl;
15188 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15190 if (pushed_scope)
15191 pop_scope (pushed_scope);
15193 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15194 added into the symbol table; otherwise, return the TYPE_DECL. */
15195 if (DECL_LANG_SPECIFIC (decl)
15196 && DECL_TEMPLATE_INFO (decl)
15197 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15199 decl = DECL_TI_TEMPLATE (decl);
15200 if (member_p)
15201 check_member_template (decl);
15204 return decl;
15207 /* Parse a using-directive.
15209 using-directive:
15210 using namespace :: [opt] nested-name-specifier [opt]
15211 namespace-name ; */
15213 static void
15214 cp_parser_using_directive (cp_parser* parser)
15216 tree namespace_decl;
15217 tree attribs;
15219 /* Look for the `using' keyword. */
15220 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15221 /* And the `namespace' keyword. */
15222 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15223 /* Look for the optional `::' operator. */
15224 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15225 /* And the optional nested-name-specifier. */
15226 cp_parser_nested_name_specifier_opt (parser,
15227 /*typename_keyword_p=*/false,
15228 /*check_dependency_p=*/true,
15229 /*type_p=*/false,
15230 /*is_declaration=*/true);
15231 /* Get the namespace being used. */
15232 namespace_decl = cp_parser_namespace_name (parser);
15233 /* And any specified attributes. */
15234 attribs = cp_parser_attributes_opt (parser);
15235 /* Update the symbol table. */
15236 parse_using_directive (namespace_decl, attribs);
15237 /* Look for the final `;'. */
15238 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15241 /* Parse an asm-definition.
15243 asm-definition:
15244 asm ( string-literal ) ;
15246 GNU Extension:
15248 asm-definition:
15249 asm volatile [opt] ( string-literal ) ;
15250 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15251 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15252 : asm-operand-list [opt] ) ;
15253 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15254 : asm-operand-list [opt]
15255 : asm-clobber-list [opt] ) ;
15256 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15257 : asm-clobber-list [opt]
15258 : asm-goto-list ) ; */
15260 static void
15261 cp_parser_asm_definition (cp_parser* parser)
15263 tree string;
15264 tree outputs = NULL_TREE;
15265 tree inputs = NULL_TREE;
15266 tree clobbers = NULL_TREE;
15267 tree labels = NULL_TREE;
15268 tree asm_stmt;
15269 bool volatile_p = false;
15270 bool extended_p = false;
15271 bool invalid_inputs_p = false;
15272 bool invalid_outputs_p = false;
15273 bool goto_p = false;
15274 required_token missing = RT_NONE;
15276 /* Look for the `asm' keyword. */
15277 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15278 /* See if the next token is `volatile'. */
15279 if (cp_parser_allow_gnu_extensions_p (parser)
15280 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15282 /* Remember that we saw the `volatile' keyword. */
15283 volatile_p = true;
15284 /* Consume the token. */
15285 cp_lexer_consume_token (parser->lexer);
15287 if (cp_parser_allow_gnu_extensions_p (parser)
15288 && parser->in_function_body
15289 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15291 /* Remember that we saw the `goto' keyword. */
15292 goto_p = true;
15293 /* Consume the token. */
15294 cp_lexer_consume_token (parser->lexer);
15296 /* Look for the opening `('. */
15297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15298 return;
15299 /* Look for the string. */
15300 string = cp_parser_string_literal (parser, false, false);
15301 if (string == error_mark_node)
15303 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15304 /*consume_paren=*/true);
15305 return;
15308 /* If we're allowing GNU extensions, check for the extended assembly
15309 syntax. Unfortunately, the `:' tokens need not be separated by
15310 a space in C, and so, for compatibility, we tolerate that here
15311 too. Doing that means that we have to treat the `::' operator as
15312 two `:' tokens. */
15313 if (cp_parser_allow_gnu_extensions_p (parser)
15314 && parser->in_function_body
15315 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15316 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15318 bool inputs_p = false;
15319 bool clobbers_p = false;
15320 bool labels_p = false;
15322 /* The extended syntax was used. */
15323 extended_p = true;
15325 /* Look for outputs. */
15326 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15328 /* Consume the `:'. */
15329 cp_lexer_consume_token (parser->lexer);
15330 /* Parse the output-operands. */
15331 if (cp_lexer_next_token_is_not (parser->lexer,
15332 CPP_COLON)
15333 && cp_lexer_next_token_is_not (parser->lexer,
15334 CPP_SCOPE)
15335 && cp_lexer_next_token_is_not (parser->lexer,
15336 CPP_CLOSE_PAREN)
15337 && !goto_p)
15338 outputs = cp_parser_asm_operand_list (parser);
15340 if (outputs == error_mark_node)
15341 invalid_outputs_p = true;
15343 /* If the next token is `::', there are no outputs, and the
15344 next token is the beginning of the inputs. */
15345 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15346 /* The inputs are coming next. */
15347 inputs_p = true;
15349 /* Look for inputs. */
15350 if (inputs_p
15351 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15353 /* Consume the `:' or `::'. */
15354 cp_lexer_consume_token (parser->lexer);
15355 /* Parse the output-operands. */
15356 if (cp_lexer_next_token_is_not (parser->lexer,
15357 CPP_COLON)
15358 && cp_lexer_next_token_is_not (parser->lexer,
15359 CPP_SCOPE)
15360 && cp_lexer_next_token_is_not (parser->lexer,
15361 CPP_CLOSE_PAREN))
15362 inputs = cp_parser_asm_operand_list (parser);
15364 if (inputs == error_mark_node)
15365 invalid_inputs_p = true;
15367 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15368 /* The clobbers are coming next. */
15369 clobbers_p = true;
15371 /* Look for clobbers. */
15372 if (clobbers_p
15373 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15375 clobbers_p = true;
15376 /* Consume the `:' or `::'. */
15377 cp_lexer_consume_token (parser->lexer);
15378 /* Parse the clobbers. */
15379 if (cp_lexer_next_token_is_not (parser->lexer,
15380 CPP_COLON)
15381 && cp_lexer_next_token_is_not (parser->lexer,
15382 CPP_CLOSE_PAREN))
15383 clobbers = cp_parser_asm_clobber_list (parser);
15385 else if (goto_p
15386 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15387 /* The labels are coming next. */
15388 labels_p = true;
15390 /* Look for labels. */
15391 if (labels_p
15392 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15394 labels_p = true;
15395 /* Consume the `:' or `::'. */
15396 cp_lexer_consume_token (parser->lexer);
15397 /* Parse the labels. */
15398 labels = cp_parser_asm_label_list (parser);
15401 if (goto_p && !labels_p)
15402 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15404 else if (goto_p)
15405 missing = RT_COLON_SCOPE;
15407 /* Look for the closing `)'. */
15408 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15409 missing ? missing : RT_CLOSE_PAREN))
15410 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15411 /*consume_paren=*/true);
15412 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15414 if (!invalid_inputs_p && !invalid_outputs_p)
15416 /* Create the ASM_EXPR. */
15417 if (parser->in_function_body)
15419 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15420 inputs, clobbers, labels);
15421 /* If the extended syntax was not used, mark the ASM_EXPR. */
15422 if (!extended_p)
15424 tree temp = asm_stmt;
15425 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15426 temp = TREE_OPERAND (temp, 0);
15428 ASM_INPUT_P (temp) = 1;
15431 else
15432 add_asm_node (string);
15436 /* Declarators [gram.dcl.decl] */
15438 /* Parse an init-declarator.
15440 init-declarator:
15441 declarator initializer [opt]
15443 GNU Extension:
15445 init-declarator:
15446 declarator asm-specification [opt] attributes [opt] initializer [opt]
15448 function-definition:
15449 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15450 function-body
15451 decl-specifier-seq [opt] declarator function-try-block
15453 GNU Extension:
15455 function-definition:
15456 __extension__ function-definition
15458 TM Extension:
15460 function-definition:
15461 decl-specifier-seq [opt] declarator function-transaction-block
15463 The DECL_SPECIFIERS apply to this declarator. Returns a
15464 representation of the entity declared. If MEMBER_P is TRUE, then
15465 this declarator appears in a class scope. The new DECL created by
15466 this declarator is returned.
15468 The CHECKS are access checks that should be performed once we know
15469 what entity is being declared (and, therefore, what classes have
15470 befriended it).
15472 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15473 for a function-definition here as well. If the declarator is a
15474 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15475 be TRUE upon return. By that point, the function-definition will
15476 have been completely parsed.
15478 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15479 is FALSE.
15481 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15482 parsed declaration if it is an uninitialized single declarator not followed
15483 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15484 if present, will not be consumed. If returned, this declarator will be
15485 created with SD_INITIALIZED but will not call cp_finish_decl. */
15487 static tree
15488 cp_parser_init_declarator (cp_parser* parser,
15489 cp_decl_specifier_seq *decl_specifiers,
15490 VEC (deferred_access_check,gc)* checks,
15491 bool function_definition_allowed_p,
15492 bool member_p,
15493 int declares_class_or_enum,
15494 bool* function_definition_p,
15495 tree* maybe_range_for_decl)
15497 cp_token *token = NULL, *asm_spec_start_token = NULL,
15498 *attributes_start_token = NULL;
15499 cp_declarator *declarator;
15500 tree prefix_attributes;
15501 tree attributes;
15502 tree asm_specification;
15503 tree initializer;
15504 tree decl = NULL_TREE;
15505 tree scope;
15506 int is_initialized;
15507 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15508 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15509 "(...)". */
15510 enum cpp_ttype initialization_kind;
15511 bool is_direct_init = false;
15512 bool is_non_constant_init;
15513 int ctor_dtor_or_conv_p;
15514 bool friend_p;
15515 tree pushed_scope = NULL_TREE;
15516 bool range_for_decl_p = false;
15518 /* Gather the attributes that were provided with the
15519 decl-specifiers. */
15520 prefix_attributes = decl_specifiers->attributes;
15522 /* Assume that this is not the declarator for a function
15523 definition. */
15524 if (function_definition_p)
15525 *function_definition_p = false;
15527 /* Defer access checks while parsing the declarator; we cannot know
15528 what names are accessible until we know what is being
15529 declared. */
15530 resume_deferring_access_checks ();
15532 /* Parse the declarator. */
15533 token = cp_lexer_peek_token (parser->lexer);
15534 declarator
15535 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15536 &ctor_dtor_or_conv_p,
15537 /*parenthesized_p=*/NULL,
15538 member_p);
15539 /* Gather up the deferred checks. */
15540 stop_deferring_access_checks ();
15542 /* If the DECLARATOR was erroneous, there's no need to go
15543 further. */
15544 if (declarator == cp_error_declarator)
15545 return error_mark_node;
15547 /* Check that the number of template-parameter-lists is OK. */
15548 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15549 token->location))
15550 return error_mark_node;
15552 if (declares_class_or_enum & 2)
15553 cp_parser_check_for_definition_in_return_type (declarator,
15554 decl_specifiers->type,
15555 decl_specifiers->locations[ds_type_spec]);
15557 /* Figure out what scope the entity declared by the DECLARATOR is
15558 located in. `grokdeclarator' sometimes changes the scope, so
15559 we compute it now. */
15560 scope = get_scope_of_declarator (declarator);
15562 /* Perform any lookups in the declared type which were thought to be
15563 dependent, but are not in the scope of the declarator. */
15564 decl_specifiers->type
15565 = maybe_update_decl_type (decl_specifiers->type, scope);
15567 /* If we're allowing GNU extensions, look for an asm-specification
15568 and attributes. */
15569 if (cp_parser_allow_gnu_extensions_p (parser))
15571 /* Look for an asm-specification. */
15572 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15573 asm_specification = cp_parser_asm_specification_opt (parser);
15574 /* And attributes. */
15575 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15576 attributes = cp_parser_attributes_opt (parser);
15578 else
15580 asm_specification = NULL_TREE;
15581 attributes = NULL_TREE;
15584 /* Peek at the next token. */
15585 token = cp_lexer_peek_token (parser->lexer);
15586 /* Check to see if the token indicates the start of a
15587 function-definition. */
15588 if (function_declarator_p (declarator)
15589 && cp_parser_token_starts_function_definition_p (token))
15591 if (!function_definition_allowed_p)
15593 /* If a function-definition should not appear here, issue an
15594 error message. */
15595 cp_parser_error (parser,
15596 "a function-definition is not allowed here");
15597 return error_mark_node;
15599 else
15601 location_t func_brace_location
15602 = cp_lexer_peek_token (parser->lexer)->location;
15604 /* Neither attributes nor an asm-specification are allowed
15605 on a function-definition. */
15606 if (asm_specification)
15607 error_at (asm_spec_start_token->location,
15608 "an asm-specification is not allowed "
15609 "on a function-definition");
15610 if (attributes)
15611 error_at (attributes_start_token->location,
15612 "attributes are not allowed on a function-definition");
15613 /* This is a function-definition. */
15614 *function_definition_p = true;
15616 /* Parse the function definition. */
15617 if (member_p)
15618 decl = cp_parser_save_member_function_body (parser,
15619 decl_specifiers,
15620 declarator,
15621 prefix_attributes);
15622 else
15623 decl
15624 = (cp_parser_function_definition_from_specifiers_and_declarator
15625 (parser, decl_specifiers, prefix_attributes, declarator));
15627 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15629 /* This is where the prologue starts... */
15630 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15631 = func_brace_location;
15634 return decl;
15638 /* [dcl.dcl]
15640 Only in function declarations for constructors, destructors, and
15641 type conversions can the decl-specifier-seq be omitted.
15643 We explicitly postpone this check past the point where we handle
15644 function-definitions because we tolerate function-definitions
15645 that are missing their return types in some modes. */
15646 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15648 cp_parser_error (parser,
15649 "expected constructor, destructor, or type conversion");
15650 return error_mark_node;
15653 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15654 if (token->type == CPP_EQ
15655 || token->type == CPP_OPEN_PAREN
15656 || token->type == CPP_OPEN_BRACE)
15658 is_initialized = SD_INITIALIZED;
15659 initialization_kind = token->type;
15660 if (maybe_range_for_decl)
15661 *maybe_range_for_decl = error_mark_node;
15663 if (token->type == CPP_EQ
15664 && function_declarator_p (declarator))
15666 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15667 if (t2->keyword == RID_DEFAULT)
15668 is_initialized = SD_DEFAULTED;
15669 else if (t2->keyword == RID_DELETE)
15670 is_initialized = SD_DELETED;
15673 else
15675 /* If the init-declarator isn't initialized and isn't followed by a
15676 `,' or `;', it's not a valid init-declarator. */
15677 if (token->type != CPP_COMMA
15678 && token->type != CPP_SEMICOLON)
15680 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15681 range_for_decl_p = true;
15682 else
15684 cp_parser_error (parser, "expected initializer");
15685 return error_mark_node;
15688 is_initialized = SD_UNINITIALIZED;
15689 initialization_kind = CPP_EOF;
15692 /* Because start_decl has side-effects, we should only call it if we
15693 know we're going ahead. By this point, we know that we cannot
15694 possibly be looking at any other construct. */
15695 cp_parser_commit_to_tentative_parse (parser);
15697 /* If the decl specifiers were bad, issue an error now that we're
15698 sure this was intended to be a declarator. Then continue
15699 declaring the variable(s), as int, to try to cut down on further
15700 errors. */
15701 if (decl_specifiers->any_specifiers_p
15702 && decl_specifiers->type == error_mark_node)
15704 cp_parser_error (parser, "invalid type in declaration");
15705 decl_specifiers->type = integer_type_node;
15708 /* Check to see whether or not this declaration is a friend. */
15709 friend_p = cp_parser_friend_p (decl_specifiers);
15711 /* Enter the newly declared entry in the symbol table. If we're
15712 processing a declaration in a class-specifier, we wait until
15713 after processing the initializer. */
15714 if (!member_p)
15716 if (parser->in_unbraced_linkage_specification_p)
15717 decl_specifiers->storage_class = sc_extern;
15718 decl = start_decl (declarator, decl_specifiers,
15719 range_for_decl_p? SD_INITIALIZED : is_initialized,
15720 attributes, prefix_attributes,
15721 &pushed_scope);
15722 /* Adjust location of decl if declarator->id_loc is more appropriate:
15723 set, and decl wasn't merged with another decl, in which case its
15724 location would be different from input_location, and more accurate. */
15725 if (DECL_P (decl)
15726 && declarator->id_loc != UNKNOWN_LOCATION
15727 && DECL_SOURCE_LOCATION (decl) == input_location)
15728 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15730 else if (scope)
15731 /* Enter the SCOPE. That way unqualified names appearing in the
15732 initializer will be looked up in SCOPE. */
15733 pushed_scope = push_scope (scope);
15735 /* Perform deferred access control checks, now that we know in which
15736 SCOPE the declared entity resides. */
15737 if (!member_p && decl)
15739 tree saved_current_function_decl = NULL_TREE;
15741 /* If the entity being declared is a function, pretend that we
15742 are in its scope. If it is a `friend', it may have access to
15743 things that would not otherwise be accessible. */
15744 if (TREE_CODE (decl) == FUNCTION_DECL)
15746 saved_current_function_decl = current_function_decl;
15747 current_function_decl = decl;
15750 /* Perform access checks for template parameters. */
15751 cp_parser_perform_template_parameter_access_checks (checks);
15753 /* Perform the access control checks for the declarator and the
15754 decl-specifiers. */
15755 perform_deferred_access_checks (tf_warning_or_error);
15757 /* Restore the saved value. */
15758 if (TREE_CODE (decl) == FUNCTION_DECL)
15759 current_function_decl = saved_current_function_decl;
15762 /* Parse the initializer. */
15763 initializer = NULL_TREE;
15764 is_direct_init = false;
15765 is_non_constant_init = true;
15766 if (is_initialized)
15768 if (function_declarator_p (declarator))
15770 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15771 if (initialization_kind == CPP_EQ)
15772 initializer = cp_parser_pure_specifier (parser);
15773 else
15775 /* If the declaration was erroneous, we don't really
15776 know what the user intended, so just silently
15777 consume the initializer. */
15778 if (decl != error_mark_node)
15779 error_at (initializer_start_token->location,
15780 "initializer provided for function");
15781 cp_parser_skip_to_closing_parenthesis (parser,
15782 /*recovering=*/true,
15783 /*or_comma=*/false,
15784 /*consume_paren=*/true);
15787 else
15789 /* We want to record the extra mangling scope for in-class
15790 initializers of class members and initializers of static data
15791 member templates. The former involves deferring
15792 parsing of the initializer until end of class as with default
15793 arguments. So right here we only handle the latter. */
15794 if (!member_p && processing_template_decl)
15795 start_lambda_scope (decl);
15796 initializer = cp_parser_initializer (parser,
15797 &is_direct_init,
15798 &is_non_constant_init);
15799 if (!member_p && processing_template_decl)
15800 finish_lambda_scope ();
15801 if (initializer == error_mark_node)
15802 cp_parser_skip_to_end_of_statement (parser);
15806 /* The old parser allows attributes to appear after a parenthesized
15807 initializer. Mark Mitchell proposed removing this functionality
15808 on the GCC mailing lists on 2002-08-13. This parser accepts the
15809 attributes -- but ignores them. */
15810 if (cp_parser_allow_gnu_extensions_p (parser)
15811 && initialization_kind == CPP_OPEN_PAREN)
15812 if (cp_parser_attributes_opt (parser))
15813 warning (OPT_Wattributes,
15814 "attributes after parenthesized initializer ignored");
15816 /* For an in-class declaration, use `grokfield' to create the
15817 declaration. */
15818 if (member_p)
15820 if (pushed_scope)
15822 pop_scope (pushed_scope);
15823 pushed_scope = NULL_TREE;
15825 decl = grokfield (declarator, decl_specifiers,
15826 initializer, !is_non_constant_init,
15827 /*asmspec=*/NULL_TREE,
15828 prefix_attributes);
15829 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15830 cp_parser_save_default_args (parser, decl);
15833 /* Finish processing the declaration. But, skip member
15834 declarations. */
15835 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15837 cp_finish_decl (decl,
15838 initializer, !is_non_constant_init,
15839 asm_specification,
15840 /* If the initializer is in parentheses, then this is
15841 a direct-initialization, which means that an
15842 `explicit' constructor is OK. Otherwise, an
15843 `explicit' constructor cannot be used. */
15844 ((is_direct_init || !is_initialized)
15845 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15847 else if ((cxx_dialect != cxx98) && friend_p
15848 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15849 /* Core issue #226 (C++0x only): A default template-argument
15850 shall not be specified in a friend class template
15851 declaration. */
15852 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
15853 /*is_partial=*/false, /*is_friend_decl=*/1);
15855 if (!friend_p && pushed_scope)
15856 pop_scope (pushed_scope);
15858 return decl;
15861 /* Parse a declarator.
15863 declarator:
15864 direct-declarator
15865 ptr-operator declarator
15867 abstract-declarator:
15868 ptr-operator abstract-declarator [opt]
15869 direct-abstract-declarator
15871 GNU Extensions:
15873 declarator:
15874 attributes [opt] direct-declarator
15875 attributes [opt] ptr-operator declarator
15877 abstract-declarator:
15878 attributes [opt] ptr-operator abstract-declarator [opt]
15879 attributes [opt] direct-abstract-declarator
15881 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15882 detect constructor, destructor or conversion operators. It is set
15883 to -1 if the declarator is a name, and +1 if it is a
15884 function. Otherwise it is set to zero. Usually you just want to
15885 test for >0, but internally the negative value is used.
15887 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15888 a decl-specifier-seq unless it declares a constructor, destructor,
15889 or conversion. It might seem that we could check this condition in
15890 semantic analysis, rather than parsing, but that makes it difficult
15891 to handle something like `f()'. We want to notice that there are
15892 no decl-specifiers, and therefore realize that this is an
15893 expression, not a declaration.)
15895 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15896 the declarator is a direct-declarator of the form "(...)".
15898 MEMBER_P is true iff this declarator is a member-declarator. */
15900 static cp_declarator *
15901 cp_parser_declarator (cp_parser* parser,
15902 cp_parser_declarator_kind dcl_kind,
15903 int* ctor_dtor_or_conv_p,
15904 bool* parenthesized_p,
15905 bool member_p)
15907 cp_declarator *declarator;
15908 enum tree_code code;
15909 cp_cv_quals cv_quals;
15910 tree class_type;
15911 tree attributes = NULL_TREE;
15913 /* Assume this is not a constructor, destructor, or type-conversion
15914 operator. */
15915 if (ctor_dtor_or_conv_p)
15916 *ctor_dtor_or_conv_p = 0;
15918 if (cp_parser_allow_gnu_extensions_p (parser))
15919 attributes = cp_parser_attributes_opt (parser);
15921 /* Check for the ptr-operator production. */
15922 cp_parser_parse_tentatively (parser);
15923 /* Parse the ptr-operator. */
15924 code = cp_parser_ptr_operator (parser,
15925 &class_type,
15926 &cv_quals);
15927 /* If that worked, then we have a ptr-operator. */
15928 if (cp_parser_parse_definitely (parser))
15930 /* If a ptr-operator was found, then this declarator was not
15931 parenthesized. */
15932 if (parenthesized_p)
15933 *parenthesized_p = true;
15934 /* The dependent declarator is optional if we are parsing an
15935 abstract-declarator. */
15936 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15937 cp_parser_parse_tentatively (parser);
15939 /* Parse the dependent declarator. */
15940 declarator = cp_parser_declarator (parser, dcl_kind,
15941 /*ctor_dtor_or_conv_p=*/NULL,
15942 /*parenthesized_p=*/NULL,
15943 /*member_p=*/false);
15945 /* If we are parsing an abstract-declarator, we must handle the
15946 case where the dependent declarator is absent. */
15947 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15948 && !cp_parser_parse_definitely (parser))
15949 declarator = NULL;
15951 declarator = cp_parser_make_indirect_declarator
15952 (code, class_type, cv_quals, declarator);
15954 /* Everything else is a direct-declarator. */
15955 else
15957 if (parenthesized_p)
15958 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15959 CPP_OPEN_PAREN);
15960 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15961 ctor_dtor_or_conv_p,
15962 member_p);
15965 if (attributes && declarator && declarator != cp_error_declarator)
15966 declarator->attributes = attributes;
15968 return declarator;
15971 /* Parse a direct-declarator or direct-abstract-declarator.
15973 direct-declarator:
15974 declarator-id
15975 direct-declarator ( parameter-declaration-clause )
15976 cv-qualifier-seq [opt]
15977 exception-specification [opt]
15978 direct-declarator [ constant-expression [opt] ]
15979 ( declarator )
15981 direct-abstract-declarator:
15982 direct-abstract-declarator [opt]
15983 ( parameter-declaration-clause )
15984 cv-qualifier-seq [opt]
15985 exception-specification [opt]
15986 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15987 ( abstract-declarator )
15989 Returns a representation of the declarator. DCL_KIND is
15990 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15991 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15992 we are parsing a direct-declarator. It is
15993 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15994 of ambiguity we prefer an abstract declarator, as per
15995 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15996 cp_parser_declarator. */
15998 static cp_declarator *
15999 cp_parser_direct_declarator (cp_parser* parser,
16000 cp_parser_declarator_kind dcl_kind,
16001 int* ctor_dtor_or_conv_p,
16002 bool member_p)
16004 cp_token *token;
16005 cp_declarator *declarator = NULL;
16006 tree scope = NULL_TREE;
16007 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16008 bool saved_in_declarator_p = parser->in_declarator_p;
16009 bool first = true;
16010 tree pushed_scope = NULL_TREE;
16012 while (true)
16014 /* Peek at the next token. */
16015 token = cp_lexer_peek_token (parser->lexer);
16016 if (token->type == CPP_OPEN_PAREN)
16018 /* This is either a parameter-declaration-clause, or a
16019 parenthesized declarator. When we know we are parsing a
16020 named declarator, it must be a parenthesized declarator
16021 if FIRST is true. For instance, `(int)' is a
16022 parameter-declaration-clause, with an omitted
16023 direct-abstract-declarator. But `((*))', is a
16024 parenthesized abstract declarator. Finally, when T is a
16025 template parameter `(T)' is a
16026 parameter-declaration-clause, and not a parenthesized
16027 named declarator.
16029 We first try and parse a parameter-declaration-clause,
16030 and then try a nested declarator (if FIRST is true).
16032 It is not an error for it not to be a
16033 parameter-declaration-clause, even when FIRST is
16034 false. Consider,
16036 int i (int);
16037 int i (3);
16039 The first is the declaration of a function while the
16040 second is the definition of a variable, including its
16041 initializer.
16043 Having seen only the parenthesis, we cannot know which of
16044 these two alternatives should be selected. Even more
16045 complex are examples like:
16047 int i (int (a));
16048 int i (int (3));
16050 The former is a function-declaration; the latter is a
16051 variable initialization.
16053 Thus again, we try a parameter-declaration-clause, and if
16054 that fails, we back out and return. */
16056 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16058 tree params;
16059 unsigned saved_num_template_parameter_lists;
16060 bool is_declarator = false;
16061 tree t;
16063 /* In a member-declarator, the only valid interpretation
16064 of a parenthesis is the start of a
16065 parameter-declaration-clause. (It is invalid to
16066 initialize a static data member with a parenthesized
16067 initializer; only the "=" form of initialization is
16068 permitted.) */
16069 if (!member_p)
16070 cp_parser_parse_tentatively (parser);
16072 /* Consume the `('. */
16073 cp_lexer_consume_token (parser->lexer);
16074 if (first)
16076 /* If this is going to be an abstract declarator, we're
16077 in a declarator and we can't have default args. */
16078 parser->default_arg_ok_p = false;
16079 parser->in_declarator_p = true;
16082 /* Inside the function parameter list, surrounding
16083 template-parameter-lists do not apply. */
16084 saved_num_template_parameter_lists
16085 = parser->num_template_parameter_lists;
16086 parser->num_template_parameter_lists = 0;
16088 begin_scope (sk_function_parms, NULL_TREE);
16090 /* Parse the parameter-declaration-clause. */
16091 params = cp_parser_parameter_declaration_clause (parser);
16093 parser->num_template_parameter_lists
16094 = saved_num_template_parameter_lists;
16096 /* Consume the `)'. */
16097 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16099 /* If all went well, parse the cv-qualifier-seq and the
16100 exception-specification. */
16101 if (member_p || cp_parser_parse_definitely (parser))
16103 cp_cv_quals cv_quals;
16104 cp_virt_specifiers virt_specifiers;
16105 tree exception_specification;
16106 tree late_return;
16108 is_declarator = true;
16110 if (ctor_dtor_or_conv_p)
16111 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16112 first = false;
16114 /* Parse the cv-qualifier-seq. */
16115 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16116 /* And the exception-specification. */
16117 exception_specification
16118 = cp_parser_exception_specification_opt (parser);
16120 late_return = (cp_parser_late_return_type_opt
16121 (parser, member_p ? cv_quals : -1));
16123 /* Parse the virt-specifier-seq. */
16124 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16126 /* Create the function-declarator. */
16127 declarator = make_call_declarator (declarator,
16128 params,
16129 cv_quals,
16130 virt_specifiers,
16131 exception_specification,
16132 late_return);
16133 /* Any subsequent parameter lists are to do with
16134 return type, so are not those of the declared
16135 function. */
16136 parser->default_arg_ok_p = false;
16139 /* Remove the function parms from scope. */
16140 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16141 pop_binding (DECL_NAME (t), t);
16142 leave_scope();
16144 if (is_declarator)
16145 /* Repeat the main loop. */
16146 continue;
16149 /* If this is the first, we can try a parenthesized
16150 declarator. */
16151 if (first)
16153 bool saved_in_type_id_in_expr_p;
16155 parser->default_arg_ok_p = saved_default_arg_ok_p;
16156 parser->in_declarator_p = saved_in_declarator_p;
16158 /* Consume the `('. */
16159 cp_lexer_consume_token (parser->lexer);
16160 /* Parse the nested declarator. */
16161 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16162 parser->in_type_id_in_expr_p = true;
16163 declarator
16164 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16165 /*parenthesized_p=*/NULL,
16166 member_p);
16167 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16168 first = false;
16169 /* Expect a `)'. */
16170 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16171 declarator = cp_error_declarator;
16172 if (declarator == cp_error_declarator)
16173 break;
16175 goto handle_declarator;
16177 /* Otherwise, we must be done. */
16178 else
16179 break;
16181 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16182 && token->type == CPP_OPEN_SQUARE)
16184 /* Parse an array-declarator. */
16185 tree bounds;
16187 if (ctor_dtor_or_conv_p)
16188 *ctor_dtor_or_conv_p = 0;
16190 first = false;
16191 parser->default_arg_ok_p = false;
16192 parser->in_declarator_p = true;
16193 /* Consume the `['. */
16194 cp_lexer_consume_token (parser->lexer);
16195 /* Peek at the next token. */
16196 token = cp_lexer_peek_token (parser->lexer);
16197 /* If the next token is `]', then there is no
16198 constant-expression. */
16199 if (token->type != CPP_CLOSE_SQUARE)
16201 bool non_constant_p;
16203 bounds
16204 = cp_parser_constant_expression (parser,
16205 /*allow_non_constant=*/true,
16206 &non_constant_p);
16207 if (!non_constant_p)
16208 /* OK */;
16209 else if (error_operand_p (bounds))
16210 /* Already gave an error. */;
16211 else if (!parser->in_function_body
16212 || current_binding_level->kind == sk_function_parms)
16214 /* Normally, the array bound must be an integral constant
16215 expression. However, as an extension, we allow VLAs
16216 in function scopes as long as they aren't part of a
16217 parameter declaration. */
16218 cp_parser_error (parser,
16219 "array bound is not an integer constant");
16220 bounds = error_mark_node;
16222 else if (processing_template_decl)
16224 /* Remember this wasn't a constant-expression. */
16225 bounds = build_nop (TREE_TYPE (bounds), bounds);
16226 TREE_SIDE_EFFECTS (bounds) = 1;
16229 else
16230 bounds = NULL_TREE;
16231 /* Look for the closing `]'. */
16232 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16234 declarator = cp_error_declarator;
16235 break;
16238 declarator = make_array_declarator (declarator, bounds);
16240 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16243 tree qualifying_scope;
16244 tree unqualified_name;
16245 special_function_kind sfk;
16246 bool abstract_ok;
16247 bool pack_expansion_p = false;
16248 cp_token *declarator_id_start_token;
16250 /* Parse a declarator-id */
16251 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16252 if (abstract_ok)
16254 cp_parser_parse_tentatively (parser);
16256 /* If we see an ellipsis, we should be looking at a
16257 parameter pack. */
16258 if (token->type == CPP_ELLIPSIS)
16260 /* Consume the `...' */
16261 cp_lexer_consume_token (parser->lexer);
16263 pack_expansion_p = true;
16267 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16268 unqualified_name
16269 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16270 qualifying_scope = parser->scope;
16271 if (abstract_ok)
16273 bool okay = false;
16275 if (!unqualified_name && pack_expansion_p)
16277 /* Check whether an error occurred. */
16278 okay = !cp_parser_error_occurred (parser);
16280 /* We already consumed the ellipsis to mark a
16281 parameter pack, but we have no way to report it,
16282 so abort the tentative parse. We will be exiting
16283 immediately anyway. */
16284 cp_parser_abort_tentative_parse (parser);
16286 else
16287 okay = cp_parser_parse_definitely (parser);
16289 if (!okay)
16290 unqualified_name = error_mark_node;
16291 else if (unqualified_name
16292 && (qualifying_scope
16293 || (TREE_CODE (unqualified_name)
16294 != IDENTIFIER_NODE)))
16296 cp_parser_error (parser, "expected unqualified-id");
16297 unqualified_name = error_mark_node;
16301 if (!unqualified_name)
16302 return NULL;
16303 if (unqualified_name == error_mark_node)
16305 declarator = cp_error_declarator;
16306 pack_expansion_p = false;
16307 declarator->parameter_pack_p = false;
16308 break;
16311 if (qualifying_scope && at_namespace_scope_p ()
16312 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16314 /* In the declaration of a member of a template class
16315 outside of the class itself, the SCOPE will sometimes
16316 be a TYPENAME_TYPE. For example, given:
16318 template <typename T>
16319 int S<T>::R::i = 3;
16321 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16322 this context, we must resolve S<T>::R to an ordinary
16323 type, rather than a typename type.
16325 The reason we normally avoid resolving TYPENAME_TYPEs
16326 is that a specialization of `S' might render
16327 `S<T>::R' not a type. However, if `S' is
16328 specialized, then this `i' will not be used, so there
16329 is no harm in resolving the types here. */
16330 tree type;
16332 /* Resolve the TYPENAME_TYPE. */
16333 type = resolve_typename_type (qualifying_scope,
16334 /*only_current_p=*/false);
16335 /* If that failed, the declarator is invalid. */
16336 if (TREE_CODE (type) == TYPENAME_TYPE)
16338 if (typedef_variant_p (type))
16339 error_at (declarator_id_start_token->location,
16340 "cannot define member of dependent typedef "
16341 "%qT", type);
16342 else
16343 error_at (declarator_id_start_token->location,
16344 "%<%T::%E%> is not a type",
16345 TYPE_CONTEXT (qualifying_scope),
16346 TYPE_IDENTIFIER (qualifying_scope));
16348 qualifying_scope = type;
16351 sfk = sfk_none;
16353 if (unqualified_name)
16355 tree class_type;
16357 if (qualifying_scope
16358 && CLASS_TYPE_P (qualifying_scope))
16359 class_type = qualifying_scope;
16360 else
16361 class_type = current_class_type;
16363 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16365 tree name_type = TREE_TYPE (unqualified_name);
16366 if (class_type && same_type_p (name_type, class_type))
16368 if (qualifying_scope
16369 && CLASSTYPE_USE_TEMPLATE (name_type))
16371 error_at (declarator_id_start_token->location,
16372 "invalid use of constructor as a template");
16373 inform (declarator_id_start_token->location,
16374 "use %<%T::%D%> instead of %<%T::%D%> to "
16375 "name the constructor in a qualified name",
16376 class_type,
16377 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16378 class_type, name_type);
16379 declarator = cp_error_declarator;
16380 break;
16382 else
16383 unqualified_name = constructor_name (class_type);
16385 else
16387 /* We do not attempt to print the declarator
16388 here because we do not have enough
16389 information about its original syntactic
16390 form. */
16391 cp_parser_error (parser, "invalid declarator");
16392 declarator = cp_error_declarator;
16393 break;
16397 if (class_type)
16399 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16400 sfk = sfk_destructor;
16401 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16402 sfk = sfk_conversion;
16403 else if (/* There's no way to declare a constructor
16404 for an anonymous type, even if the type
16405 got a name for linkage purposes. */
16406 !TYPE_WAS_ANONYMOUS (class_type)
16407 && constructor_name_p (unqualified_name,
16408 class_type))
16410 unqualified_name = constructor_name (class_type);
16411 sfk = sfk_constructor;
16413 else if (is_overloaded_fn (unqualified_name)
16414 && DECL_CONSTRUCTOR_P (get_first_fn
16415 (unqualified_name)))
16416 sfk = sfk_constructor;
16418 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16419 *ctor_dtor_or_conv_p = -1;
16422 declarator = make_id_declarator (qualifying_scope,
16423 unqualified_name,
16424 sfk);
16425 declarator->id_loc = token->location;
16426 declarator->parameter_pack_p = pack_expansion_p;
16428 if (pack_expansion_p)
16429 maybe_warn_variadic_templates ();
16432 handle_declarator:;
16433 scope = get_scope_of_declarator (declarator);
16434 if (scope)
16435 /* Any names that appear after the declarator-id for a
16436 member are looked up in the containing scope. */
16437 pushed_scope = push_scope (scope);
16438 parser->in_declarator_p = true;
16439 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16440 || (declarator && declarator->kind == cdk_id))
16441 /* Default args are only allowed on function
16442 declarations. */
16443 parser->default_arg_ok_p = saved_default_arg_ok_p;
16444 else
16445 parser->default_arg_ok_p = false;
16447 first = false;
16449 /* We're done. */
16450 else
16451 break;
16454 /* For an abstract declarator, we might wind up with nothing at this
16455 point. That's an error; the declarator is not optional. */
16456 if (!declarator)
16457 cp_parser_error (parser, "expected declarator");
16459 /* If we entered a scope, we must exit it now. */
16460 if (pushed_scope)
16461 pop_scope (pushed_scope);
16463 parser->default_arg_ok_p = saved_default_arg_ok_p;
16464 parser->in_declarator_p = saved_in_declarator_p;
16466 return declarator;
16469 /* Parse a ptr-operator.
16471 ptr-operator:
16472 * cv-qualifier-seq [opt]
16474 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16476 GNU Extension:
16478 ptr-operator:
16479 & cv-qualifier-seq [opt]
16481 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16482 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16483 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16484 filled in with the TYPE containing the member. *CV_QUALS is
16485 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16486 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16487 Note that the tree codes returned by this function have nothing
16488 to do with the types of trees that will be eventually be created
16489 to represent the pointer or reference type being parsed. They are
16490 just constants with suggestive names. */
16491 static enum tree_code
16492 cp_parser_ptr_operator (cp_parser* parser,
16493 tree* type,
16494 cp_cv_quals *cv_quals)
16496 enum tree_code code = ERROR_MARK;
16497 cp_token *token;
16499 /* Assume that it's not a pointer-to-member. */
16500 *type = NULL_TREE;
16501 /* And that there are no cv-qualifiers. */
16502 *cv_quals = TYPE_UNQUALIFIED;
16504 /* Peek at the next token. */
16505 token = cp_lexer_peek_token (parser->lexer);
16507 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16508 if (token->type == CPP_MULT)
16509 code = INDIRECT_REF;
16510 else if (token->type == CPP_AND)
16511 code = ADDR_EXPR;
16512 else if ((cxx_dialect != cxx98) &&
16513 token->type == CPP_AND_AND) /* C++0x only */
16514 code = NON_LVALUE_EXPR;
16516 if (code != ERROR_MARK)
16518 /* Consume the `*', `&' or `&&'. */
16519 cp_lexer_consume_token (parser->lexer);
16521 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16522 `&', if we are allowing GNU extensions. (The only qualifier
16523 that can legally appear after `&' is `restrict', but that is
16524 enforced during semantic analysis. */
16525 if (code == INDIRECT_REF
16526 || cp_parser_allow_gnu_extensions_p (parser))
16527 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16529 else
16531 /* Try the pointer-to-member case. */
16532 cp_parser_parse_tentatively (parser);
16533 /* Look for the optional `::' operator. */
16534 cp_parser_global_scope_opt (parser,
16535 /*current_scope_valid_p=*/false);
16536 /* Look for the nested-name specifier. */
16537 token = cp_lexer_peek_token (parser->lexer);
16538 cp_parser_nested_name_specifier (parser,
16539 /*typename_keyword_p=*/false,
16540 /*check_dependency_p=*/true,
16541 /*type_p=*/false,
16542 /*is_declaration=*/false);
16543 /* If we found it, and the next token is a `*', then we are
16544 indeed looking at a pointer-to-member operator. */
16545 if (!cp_parser_error_occurred (parser)
16546 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16548 /* Indicate that the `*' operator was used. */
16549 code = INDIRECT_REF;
16551 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16552 error_at (token->location, "%qD is a namespace", parser->scope);
16553 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16554 error_at (token->location, "cannot form pointer to member of "
16555 "non-class %q#T", parser->scope);
16556 else
16558 /* The type of which the member is a member is given by the
16559 current SCOPE. */
16560 *type = parser->scope;
16561 /* The next name will not be qualified. */
16562 parser->scope = NULL_TREE;
16563 parser->qualifying_scope = NULL_TREE;
16564 parser->object_scope = NULL_TREE;
16565 /* Look for the optional cv-qualifier-seq. */
16566 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16569 /* If that didn't work we don't have a ptr-operator. */
16570 if (!cp_parser_parse_definitely (parser))
16571 cp_parser_error (parser, "expected ptr-operator");
16574 return code;
16577 /* Parse an (optional) cv-qualifier-seq.
16579 cv-qualifier-seq:
16580 cv-qualifier cv-qualifier-seq [opt]
16582 cv-qualifier:
16583 const
16584 volatile
16586 GNU Extension:
16588 cv-qualifier:
16589 __restrict__
16591 Returns a bitmask representing the cv-qualifiers. */
16593 static cp_cv_quals
16594 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16596 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16598 while (true)
16600 cp_token *token;
16601 cp_cv_quals cv_qualifier;
16603 /* Peek at the next token. */
16604 token = cp_lexer_peek_token (parser->lexer);
16605 /* See if it's a cv-qualifier. */
16606 switch (token->keyword)
16608 case RID_CONST:
16609 cv_qualifier = TYPE_QUAL_CONST;
16610 break;
16612 case RID_VOLATILE:
16613 cv_qualifier = TYPE_QUAL_VOLATILE;
16614 break;
16616 case RID_RESTRICT:
16617 cv_qualifier = TYPE_QUAL_RESTRICT;
16618 break;
16620 default:
16621 cv_qualifier = TYPE_UNQUALIFIED;
16622 break;
16625 if (!cv_qualifier)
16626 break;
16628 if (cv_quals & cv_qualifier)
16630 error_at (token->location, "duplicate cv-qualifier");
16631 cp_lexer_purge_token (parser->lexer);
16633 else
16635 cp_lexer_consume_token (parser->lexer);
16636 cv_quals |= cv_qualifier;
16640 return cv_quals;
16643 /* Parse an (optional) virt-specifier-seq.
16645 virt-specifier-seq:
16646 virt-specifier virt-specifier-seq [opt]
16648 virt-specifier:
16649 override
16650 final
16652 Returns a bitmask representing the virt-specifiers. */
16654 static cp_virt_specifiers
16655 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16657 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16659 while (true)
16661 cp_token *token;
16662 cp_virt_specifiers virt_specifier;
16664 /* Peek at the next token. */
16665 token = cp_lexer_peek_token (parser->lexer);
16666 /* See if it's a virt-specifier-qualifier. */
16667 if (token->type != CPP_NAME)
16668 break;
16669 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16671 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16672 virt_specifier = VIRT_SPEC_OVERRIDE;
16674 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16676 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16677 virt_specifier = VIRT_SPEC_FINAL;
16679 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16681 virt_specifier = VIRT_SPEC_FINAL;
16683 else
16684 break;
16686 if (virt_specifiers & virt_specifier)
16688 error_at (token->location, "duplicate virt-specifier");
16689 cp_lexer_purge_token (parser->lexer);
16691 else
16693 cp_lexer_consume_token (parser->lexer);
16694 virt_specifiers |= virt_specifier;
16697 return virt_specifiers;
16700 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16701 is in scope even though it isn't real. */
16703 static void
16704 inject_this_parameter (tree ctype, cp_cv_quals quals)
16706 tree this_parm;
16708 if (current_class_ptr)
16710 /* We don't clear this between NSDMIs. Is it already what we want? */
16711 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16712 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16713 && cp_type_quals (type) == quals)
16714 return;
16717 this_parm = build_this_parm (ctype, quals);
16718 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16719 current_class_ptr = NULL_TREE;
16720 current_class_ref
16721 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16722 current_class_ptr = this_parm;
16725 /* Parse a late-specified return type, if any. This is not a separate
16726 non-terminal, but part of a function declarator, which looks like
16728 -> trailing-type-specifier-seq abstract-declarator(opt)
16730 Returns the type indicated by the type-id.
16732 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16733 function. */
16735 static tree
16736 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16738 cp_token *token;
16739 tree type;
16741 /* Peek at the next token. */
16742 token = cp_lexer_peek_token (parser->lexer);
16743 /* A late-specified return type is indicated by an initial '->'. */
16744 if (token->type != CPP_DEREF)
16745 return NULL_TREE;
16747 /* Consume the ->. */
16748 cp_lexer_consume_token (parser->lexer);
16750 if (quals >= 0)
16752 /* DR 1207: 'this' is in scope in the trailing return type. */
16753 gcc_assert (current_class_ptr == NULL_TREE);
16754 inject_this_parameter (current_class_type, quals);
16757 type = cp_parser_trailing_type_id (parser);
16759 if (quals >= 0)
16760 current_class_ptr = current_class_ref = NULL_TREE;
16762 return type;
16765 /* Parse a declarator-id.
16767 declarator-id:
16768 id-expression
16769 :: [opt] nested-name-specifier [opt] type-name
16771 In the `id-expression' case, the value returned is as for
16772 cp_parser_id_expression if the id-expression was an unqualified-id.
16773 If the id-expression was a qualified-id, then a SCOPE_REF is
16774 returned. The first operand is the scope (either a NAMESPACE_DECL
16775 or TREE_TYPE), but the second is still just a representation of an
16776 unqualified-id. */
16778 static tree
16779 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16781 tree id;
16782 /* The expression must be an id-expression. Assume that qualified
16783 names are the names of types so that:
16785 template <class T>
16786 int S<T>::R::i = 3;
16788 will work; we must treat `S<T>::R' as the name of a type.
16789 Similarly, assume that qualified names are templates, where
16790 required, so that:
16792 template <class T>
16793 int S<T>::R<T>::i = 3;
16795 will work, too. */
16796 id = cp_parser_id_expression (parser,
16797 /*template_keyword_p=*/false,
16798 /*check_dependency_p=*/false,
16799 /*template_p=*/NULL,
16800 /*declarator_p=*/true,
16801 optional_p);
16802 if (id && BASELINK_P (id))
16803 id = BASELINK_FUNCTIONS (id);
16804 return id;
16807 /* Parse a type-id.
16809 type-id:
16810 type-specifier-seq abstract-declarator [opt]
16812 Returns the TYPE specified. */
16814 static tree
16815 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16816 bool is_trailing_return)
16818 cp_decl_specifier_seq type_specifier_seq;
16819 cp_declarator *abstract_declarator;
16821 /* Parse the type-specifier-seq. */
16822 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16823 is_trailing_return,
16824 &type_specifier_seq);
16825 if (type_specifier_seq.type == error_mark_node)
16826 return error_mark_node;
16828 /* There might or might not be an abstract declarator. */
16829 cp_parser_parse_tentatively (parser);
16830 /* Look for the declarator. */
16831 abstract_declarator
16832 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16833 /*parenthesized_p=*/NULL,
16834 /*member_p=*/false);
16835 /* Check to see if there really was a declarator. */
16836 if (!cp_parser_parse_definitely (parser))
16837 abstract_declarator = NULL;
16839 if (type_specifier_seq.type
16840 && type_uses_auto (type_specifier_seq.type))
16842 /* A type-id with type 'auto' is only ok if the abstract declarator
16843 is a function declarator with a late-specified return type. */
16844 if (abstract_declarator
16845 && abstract_declarator->kind == cdk_function
16846 && abstract_declarator->u.function.late_return_type)
16847 /* OK */;
16848 else
16850 error ("invalid use of %<auto%>");
16851 return error_mark_node;
16855 return groktypename (&type_specifier_seq, abstract_declarator,
16856 is_template_arg);
16859 static tree cp_parser_type_id (cp_parser *parser)
16861 return cp_parser_type_id_1 (parser, false, false);
16864 static tree cp_parser_template_type_arg (cp_parser *parser)
16866 tree r;
16867 const char *saved_message = parser->type_definition_forbidden_message;
16868 parser->type_definition_forbidden_message
16869 = G_("types may not be defined in template arguments");
16870 r = cp_parser_type_id_1 (parser, true, false);
16871 parser->type_definition_forbidden_message = saved_message;
16872 return r;
16875 static tree cp_parser_trailing_type_id (cp_parser *parser)
16877 return cp_parser_type_id_1 (parser, false, true);
16880 /* Parse a type-specifier-seq.
16882 type-specifier-seq:
16883 type-specifier type-specifier-seq [opt]
16885 GNU extension:
16887 type-specifier-seq:
16888 attributes type-specifier-seq [opt]
16890 If IS_DECLARATION is true, we are at the start of a "condition" or
16891 exception-declaration, so we might be followed by a declarator-id.
16893 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16894 i.e. we've just seen "->".
16896 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16898 static void
16899 cp_parser_type_specifier_seq (cp_parser* parser,
16900 bool is_declaration,
16901 bool is_trailing_return,
16902 cp_decl_specifier_seq *type_specifier_seq)
16904 bool seen_type_specifier = false;
16905 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16906 cp_token *start_token = NULL;
16908 /* Clear the TYPE_SPECIFIER_SEQ. */
16909 clear_decl_specs (type_specifier_seq);
16911 /* In the context of a trailing return type, enum E { } is an
16912 elaborated-type-specifier followed by a function-body, not an
16913 enum-specifier. */
16914 if (is_trailing_return)
16915 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16917 /* Parse the type-specifiers and attributes. */
16918 while (true)
16920 tree type_specifier;
16921 bool is_cv_qualifier;
16923 /* Check for attributes first. */
16924 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16926 type_specifier_seq->attributes =
16927 chainon (type_specifier_seq->attributes,
16928 cp_parser_attributes_opt (parser));
16929 continue;
16932 /* record the token of the beginning of the type specifier seq,
16933 for error reporting purposes*/
16934 if (!start_token)
16935 start_token = cp_lexer_peek_token (parser->lexer);
16937 /* Look for the type-specifier. */
16938 type_specifier = cp_parser_type_specifier (parser,
16939 flags,
16940 type_specifier_seq,
16941 /*is_declaration=*/false,
16942 NULL,
16943 &is_cv_qualifier);
16944 if (!type_specifier)
16946 /* If the first type-specifier could not be found, this is not a
16947 type-specifier-seq at all. */
16948 if (!seen_type_specifier)
16950 cp_parser_error (parser, "expected type-specifier");
16951 type_specifier_seq->type = error_mark_node;
16952 return;
16954 /* If subsequent type-specifiers could not be found, the
16955 type-specifier-seq is complete. */
16956 break;
16959 seen_type_specifier = true;
16960 /* The standard says that a condition can be:
16962 type-specifier-seq declarator = assignment-expression
16964 However, given:
16966 struct S {};
16967 if (int S = ...)
16969 we should treat the "S" as a declarator, not as a
16970 type-specifier. The standard doesn't say that explicitly for
16971 type-specifier-seq, but it does say that for
16972 decl-specifier-seq in an ordinary declaration. Perhaps it
16973 would be clearer just to allow a decl-specifier-seq here, and
16974 then add a semantic restriction that if any decl-specifiers
16975 that are not type-specifiers appear, the program is invalid. */
16976 if (is_declaration && !is_cv_qualifier)
16977 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16981 /* Parse a parameter-declaration-clause.
16983 parameter-declaration-clause:
16984 parameter-declaration-list [opt] ... [opt]
16985 parameter-declaration-list , ...
16987 Returns a representation for the parameter declarations. A return
16988 value of NULL indicates a parameter-declaration-clause consisting
16989 only of an ellipsis. */
16991 static tree
16992 cp_parser_parameter_declaration_clause (cp_parser* parser)
16994 tree parameters;
16995 cp_token *token;
16996 bool ellipsis_p;
16997 bool is_error;
16999 /* Peek at the next token. */
17000 token = cp_lexer_peek_token (parser->lexer);
17001 /* Check for trivial parameter-declaration-clauses. */
17002 if (token->type == CPP_ELLIPSIS)
17004 /* Consume the `...' token. */
17005 cp_lexer_consume_token (parser->lexer);
17006 return NULL_TREE;
17008 else if (token->type == CPP_CLOSE_PAREN)
17009 /* There are no parameters. */
17011 #ifndef NO_IMPLICIT_EXTERN_C
17012 if (in_system_header && current_class_type == NULL
17013 && current_lang_name == lang_name_c)
17014 return NULL_TREE;
17015 else
17016 #endif
17017 return void_list_node;
17019 /* Check for `(void)', too, which is a special case. */
17020 else if (token->keyword == RID_VOID
17021 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17022 == CPP_CLOSE_PAREN))
17024 /* Consume the `void' token. */
17025 cp_lexer_consume_token (parser->lexer);
17026 /* There are no parameters. */
17027 return void_list_node;
17030 /* Parse the parameter-declaration-list. */
17031 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17032 /* If a parse error occurred while parsing the
17033 parameter-declaration-list, then the entire
17034 parameter-declaration-clause is erroneous. */
17035 if (is_error)
17036 return NULL;
17038 /* Peek at the next token. */
17039 token = cp_lexer_peek_token (parser->lexer);
17040 /* If it's a `,', the clause should terminate with an ellipsis. */
17041 if (token->type == CPP_COMMA)
17043 /* Consume the `,'. */
17044 cp_lexer_consume_token (parser->lexer);
17045 /* Expect an ellipsis. */
17046 ellipsis_p
17047 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17049 /* It might also be `...' if the optional trailing `,' was
17050 omitted. */
17051 else if (token->type == CPP_ELLIPSIS)
17053 /* Consume the `...' token. */
17054 cp_lexer_consume_token (parser->lexer);
17055 /* And remember that we saw it. */
17056 ellipsis_p = true;
17058 else
17059 ellipsis_p = false;
17061 /* Finish the parameter list. */
17062 if (!ellipsis_p)
17063 parameters = chainon (parameters, void_list_node);
17065 return parameters;
17068 /* Parse a parameter-declaration-list.
17070 parameter-declaration-list:
17071 parameter-declaration
17072 parameter-declaration-list , parameter-declaration
17074 Returns a representation of the parameter-declaration-list, as for
17075 cp_parser_parameter_declaration_clause. However, the
17076 `void_list_node' is never appended to the list. Upon return,
17077 *IS_ERROR will be true iff an error occurred. */
17079 static tree
17080 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17082 tree parameters = NULL_TREE;
17083 tree *tail = &parameters;
17084 bool saved_in_unbraced_linkage_specification_p;
17085 int index = 0;
17087 /* Assume all will go well. */
17088 *is_error = false;
17089 /* The special considerations that apply to a function within an
17090 unbraced linkage specifications do not apply to the parameters
17091 to the function. */
17092 saved_in_unbraced_linkage_specification_p
17093 = parser->in_unbraced_linkage_specification_p;
17094 parser->in_unbraced_linkage_specification_p = false;
17096 /* Look for more parameters. */
17097 while (true)
17099 cp_parameter_declarator *parameter;
17100 tree decl = error_mark_node;
17101 bool parenthesized_p = false;
17102 /* Parse the parameter. */
17103 parameter
17104 = cp_parser_parameter_declaration (parser,
17105 /*template_parm_p=*/false,
17106 &parenthesized_p);
17108 /* We don't know yet if the enclosing context is deprecated, so wait
17109 and warn in grokparms if appropriate. */
17110 deprecated_state = DEPRECATED_SUPPRESS;
17112 if (parameter)
17113 decl = grokdeclarator (parameter->declarator,
17114 &parameter->decl_specifiers,
17115 PARM,
17116 parameter->default_argument != NULL_TREE,
17117 &parameter->decl_specifiers.attributes);
17119 deprecated_state = DEPRECATED_NORMAL;
17121 /* If a parse error occurred parsing the parameter declaration,
17122 then the entire parameter-declaration-list is erroneous. */
17123 if (decl == error_mark_node)
17125 *is_error = true;
17126 parameters = error_mark_node;
17127 break;
17130 if (parameter->decl_specifiers.attributes)
17131 cplus_decl_attributes (&decl,
17132 parameter->decl_specifiers.attributes,
17134 if (DECL_NAME (decl))
17135 decl = pushdecl (decl);
17137 if (decl != error_mark_node)
17139 retrofit_lang_decl (decl);
17140 DECL_PARM_INDEX (decl) = ++index;
17141 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17144 /* Add the new parameter to the list. */
17145 *tail = build_tree_list (parameter->default_argument, decl);
17146 tail = &TREE_CHAIN (*tail);
17148 /* Peek at the next token. */
17149 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17150 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17151 /* These are for Objective-C++ */
17152 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17153 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17154 /* The parameter-declaration-list is complete. */
17155 break;
17156 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17158 cp_token *token;
17160 /* Peek at the next token. */
17161 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17162 /* If it's an ellipsis, then the list is complete. */
17163 if (token->type == CPP_ELLIPSIS)
17164 break;
17165 /* Otherwise, there must be more parameters. Consume the
17166 `,'. */
17167 cp_lexer_consume_token (parser->lexer);
17168 /* When parsing something like:
17170 int i(float f, double d)
17172 we can tell after seeing the declaration for "f" that we
17173 are not looking at an initialization of a variable "i",
17174 but rather at the declaration of a function "i".
17176 Due to the fact that the parsing of template arguments
17177 (as specified to a template-id) requires backtracking we
17178 cannot use this technique when inside a template argument
17179 list. */
17180 if (!parser->in_template_argument_list_p
17181 && !parser->in_type_id_in_expr_p
17182 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17183 /* However, a parameter-declaration of the form
17184 "foat(f)" (which is a valid declaration of a
17185 parameter "f") can also be interpreted as an
17186 expression (the conversion of "f" to "float"). */
17187 && !parenthesized_p)
17188 cp_parser_commit_to_tentative_parse (parser);
17190 else
17192 cp_parser_error (parser, "expected %<,%> or %<...%>");
17193 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17194 cp_parser_skip_to_closing_parenthesis (parser,
17195 /*recovering=*/true,
17196 /*or_comma=*/false,
17197 /*consume_paren=*/false);
17198 break;
17202 parser->in_unbraced_linkage_specification_p
17203 = saved_in_unbraced_linkage_specification_p;
17205 return parameters;
17208 /* Parse a parameter declaration.
17210 parameter-declaration:
17211 decl-specifier-seq ... [opt] declarator
17212 decl-specifier-seq declarator = assignment-expression
17213 decl-specifier-seq ... [opt] abstract-declarator [opt]
17214 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17216 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17217 declares a template parameter. (In that case, a non-nested `>'
17218 token encountered during the parsing of the assignment-expression
17219 is not interpreted as a greater-than operator.)
17221 Returns a representation of the parameter, or NULL if an error
17222 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17223 true iff the declarator is of the form "(p)". */
17225 static cp_parameter_declarator *
17226 cp_parser_parameter_declaration (cp_parser *parser,
17227 bool template_parm_p,
17228 bool *parenthesized_p)
17230 int declares_class_or_enum;
17231 cp_decl_specifier_seq decl_specifiers;
17232 cp_declarator *declarator;
17233 tree default_argument;
17234 cp_token *token = NULL, *declarator_token_start = NULL;
17235 const char *saved_message;
17237 /* In a template parameter, `>' is not an operator.
17239 [temp.param]
17241 When parsing a default template-argument for a non-type
17242 template-parameter, the first non-nested `>' is taken as the end
17243 of the template parameter-list rather than a greater-than
17244 operator. */
17246 /* Type definitions may not appear in parameter types. */
17247 saved_message = parser->type_definition_forbidden_message;
17248 parser->type_definition_forbidden_message
17249 = G_("types may not be defined in parameter types");
17251 /* Parse the declaration-specifiers. */
17252 cp_parser_decl_specifier_seq (parser,
17253 CP_PARSER_FLAGS_NONE,
17254 &decl_specifiers,
17255 &declares_class_or_enum);
17257 /* Complain about missing 'typename' or other invalid type names. */
17258 if (!decl_specifiers.any_type_specifiers_p)
17259 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17261 /* If an error occurred, there's no reason to attempt to parse the
17262 rest of the declaration. */
17263 if (cp_parser_error_occurred (parser))
17265 parser->type_definition_forbidden_message = saved_message;
17266 return NULL;
17269 /* Peek at the next token. */
17270 token = cp_lexer_peek_token (parser->lexer);
17272 /* If the next token is a `)', `,', `=', `>', or `...', then there
17273 is no declarator. However, when variadic templates are enabled,
17274 there may be a declarator following `...'. */
17275 if (token->type == CPP_CLOSE_PAREN
17276 || token->type == CPP_COMMA
17277 || token->type == CPP_EQ
17278 || token->type == CPP_GREATER)
17280 declarator = NULL;
17281 if (parenthesized_p)
17282 *parenthesized_p = false;
17284 /* Otherwise, there should be a declarator. */
17285 else
17287 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17288 parser->default_arg_ok_p = false;
17290 /* After seeing a decl-specifier-seq, if the next token is not a
17291 "(", there is no possibility that the code is a valid
17292 expression. Therefore, if parsing tentatively, we commit at
17293 this point. */
17294 if (!parser->in_template_argument_list_p
17295 /* In an expression context, having seen:
17297 (int((char ...
17299 we cannot be sure whether we are looking at a
17300 function-type (taking a "char" as a parameter) or a cast
17301 of some object of type "char" to "int". */
17302 && !parser->in_type_id_in_expr_p
17303 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17304 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17305 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17306 cp_parser_commit_to_tentative_parse (parser);
17307 /* Parse the declarator. */
17308 declarator_token_start = token;
17309 declarator = cp_parser_declarator (parser,
17310 CP_PARSER_DECLARATOR_EITHER,
17311 /*ctor_dtor_or_conv_p=*/NULL,
17312 parenthesized_p,
17313 /*member_p=*/false);
17314 parser->default_arg_ok_p = saved_default_arg_ok_p;
17315 /* After the declarator, allow more attributes. */
17316 decl_specifiers.attributes
17317 = chainon (decl_specifiers.attributes,
17318 cp_parser_attributes_opt (parser));
17321 /* If the next token is an ellipsis, and we have not seen a
17322 declarator name, and the type of the declarator contains parameter
17323 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17324 a parameter pack expansion expression. Otherwise, leave the
17325 ellipsis for a C-style variadic function. */
17326 token = cp_lexer_peek_token (parser->lexer);
17327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17329 tree type = decl_specifiers.type;
17331 if (type && DECL_P (type))
17332 type = TREE_TYPE (type);
17334 if (type
17335 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17336 && declarator_can_be_parameter_pack (declarator)
17337 && (!declarator || !declarator->parameter_pack_p)
17338 && uses_parameter_packs (type))
17340 /* Consume the `...'. */
17341 cp_lexer_consume_token (parser->lexer);
17342 maybe_warn_variadic_templates ();
17344 /* Build a pack expansion type */
17345 if (declarator)
17346 declarator->parameter_pack_p = true;
17347 else
17348 decl_specifiers.type = make_pack_expansion (type);
17352 /* The restriction on defining new types applies only to the type
17353 of the parameter, not to the default argument. */
17354 parser->type_definition_forbidden_message = saved_message;
17356 /* If the next token is `=', then process a default argument. */
17357 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17359 token = cp_lexer_peek_token (parser->lexer);
17360 /* If we are defining a class, then the tokens that make up the
17361 default argument must be saved and processed later. */
17362 if (!template_parm_p && at_class_scope_p ()
17363 && TYPE_BEING_DEFINED (current_class_type)
17364 && !LAMBDA_TYPE_P (current_class_type))
17365 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17366 /* Outside of a class definition, we can just parse the
17367 assignment-expression. */
17368 else
17369 default_argument
17370 = cp_parser_default_argument (parser, template_parm_p);
17372 if (!parser->default_arg_ok_p)
17374 if (flag_permissive)
17375 warning (0, "deprecated use of default argument for parameter of non-function");
17376 else
17378 error_at (token->location,
17379 "default arguments are only "
17380 "permitted for function parameters");
17381 default_argument = NULL_TREE;
17384 else if ((declarator && declarator->parameter_pack_p)
17385 || (decl_specifiers.type
17386 && PACK_EXPANSION_P (decl_specifiers.type)))
17388 /* Find the name of the parameter pack. */
17389 cp_declarator *id_declarator = declarator;
17390 while (id_declarator && id_declarator->kind != cdk_id)
17391 id_declarator = id_declarator->declarator;
17393 if (id_declarator && id_declarator->kind == cdk_id)
17394 error_at (declarator_token_start->location,
17395 template_parm_p
17396 ? G_("template parameter pack %qD "
17397 "cannot have a default argument")
17398 : G_("parameter pack %qD cannot have "
17399 "a default argument"),
17400 id_declarator->u.id.unqualified_name);
17401 else
17402 error_at (declarator_token_start->location,
17403 template_parm_p
17404 ? G_("template parameter pack cannot have "
17405 "a default argument")
17406 : G_("parameter pack cannot have a "
17407 "default argument"));
17409 default_argument = NULL_TREE;
17412 else
17413 default_argument = NULL_TREE;
17415 return make_parameter_declarator (&decl_specifiers,
17416 declarator,
17417 default_argument);
17420 /* Parse a default argument and return it.
17422 TEMPLATE_PARM_P is true if this is a default argument for a
17423 non-type template parameter. */
17424 static tree
17425 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17427 tree default_argument = NULL_TREE;
17428 bool saved_greater_than_is_operator_p;
17429 bool saved_local_variables_forbidden_p;
17430 bool non_constant_p, is_direct_init;
17432 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17433 set correctly. */
17434 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17435 parser->greater_than_is_operator_p = !template_parm_p;
17436 /* Local variable names (and the `this' keyword) may not
17437 appear in a default argument. */
17438 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17439 parser->local_variables_forbidden_p = true;
17440 /* Parse the assignment-expression. */
17441 if (template_parm_p)
17442 push_deferring_access_checks (dk_no_deferred);
17443 default_argument
17444 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17445 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17446 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17447 if (template_parm_p)
17448 pop_deferring_access_checks ();
17449 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17450 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17452 return default_argument;
17455 /* Parse a function-body.
17457 function-body:
17458 compound_statement */
17460 static void
17461 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17463 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17466 /* Parse a ctor-initializer-opt followed by a function-body. Return
17467 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17468 is true we are parsing a function-try-block. */
17470 static bool
17471 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17472 bool in_function_try_block)
17474 tree body, list;
17475 bool ctor_initializer_p;
17476 const bool check_body_p =
17477 DECL_CONSTRUCTOR_P (current_function_decl)
17478 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17479 tree last = NULL;
17481 /* Begin the function body. */
17482 body = begin_function_body ();
17483 /* Parse the optional ctor-initializer. */
17484 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17486 /* If we're parsing a constexpr constructor definition, we need
17487 to check that the constructor body is indeed empty. However,
17488 before we get to cp_parser_function_body lot of junk has been
17489 generated, so we can't just check that we have an empty block.
17490 Rather we take a snapshot of the outermost block, and check whether
17491 cp_parser_function_body changed its state. */
17492 if (check_body_p)
17494 list = cur_stmt_list;
17495 if (STATEMENT_LIST_TAIL (list))
17496 last = STATEMENT_LIST_TAIL (list)->stmt;
17498 /* Parse the function-body. */
17499 cp_parser_function_body (parser, in_function_try_block);
17500 if (check_body_p)
17501 check_constexpr_ctor_body (last, list);
17502 /* Finish the function body. */
17503 finish_function_body (body);
17505 return ctor_initializer_p;
17508 /* Parse an initializer.
17510 initializer:
17511 = initializer-clause
17512 ( expression-list )
17514 Returns an expression representing the initializer. If no
17515 initializer is present, NULL_TREE is returned.
17517 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17518 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17519 set to TRUE if there is no initializer present. If there is an
17520 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17521 is set to true; otherwise it is set to false. */
17523 static tree
17524 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17525 bool* non_constant_p)
17527 cp_token *token;
17528 tree init;
17530 /* Peek at the next token. */
17531 token = cp_lexer_peek_token (parser->lexer);
17533 /* Let our caller know whether or not this initializer was
17534 parenthesized. */
17535 *is_direct_init = (token->type != CPP_EQ);
17536 /* Assume that the initializer is constant. */
17537 *non_constant_p = false;
17539 if (token->type == CPP_EQ)
17541 /* Consume the `='. */
17542 cp_lexer_consume_token (parser->lexer);
17543 /* Parse the initializer-clause. */
17544 init = cp_parser_initializer_clause (parser, non_constant_p);
17546 else if (token->type == CPP_OPEN_PAREN)
17548 VEC(tree,gc) *vec;
17549 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17550 /*cast_p=*/false,
17551 /*allow_expansion_p=*/true,
17552 non_constant_p);
17553 if (vec == NULL)
17554 return error_mark_node;
17555 init = build_tree_list_vec (vec);
17556 release_tree_vector (vec);
17558 else if (token->type == CPP_OPEN_BRACE)
17560 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17561 init = cp_parser_braced_list (parser, non_constant_p);
17562 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17564 else
17566 /* Anything else is an error. */
17567 cp_parser_error (parser, "expected initializer");
17568 init = error_mark_node;
17571 return init;
17574 /* Parse an initializer-clause.
17576 initializer-clause:
17577 assignment-expression
17578 braced-init-list
17580 Returns an expression representing the initializer.
17582 If the `assignment-expression' production is used the value
17583 returned is simply a representation for the expression.
17585 Otherwise, calls cp_parser_braced_list. */
17587 static tree
17588 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17590 tree initializer;
17592 /* Assume the expression is constant. */
17593 *non_constant_p = false;
17595 /* If it is not a `{', then we are looking at an
17596 assignment-expression. */
17597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17599 initializer
17600 = cp_parser_constant_expression (parser,
17601 /*allow_non_constant_p=*/true,
17602 non_constant_p);
17604 else
17605 initializer = cp_parser_braced_list (parser, non_constant_p);
17607 return initializer;
17610 /* Parse a brace-enclosed initializer list.
17612 braced-init-list:
17613 { initializer-list , [opt] }
17616 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17617 the elements of the initializer-list (or NULL, if the last
17618 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17619 NULL_TREE. There is no way to detect whether or not the optional
17620 trailing `,' was provided. NON_CONSTANT_P is as for
17621 cp_parser_initializer. */
17623 static tree
17624 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17626 tree initializer;
17628 /* Consume the `{' token. */
17629 cp_lexer_consume_token (parser->lexer);
17630 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17631 initializer = make_node (CONSTRUCTOR);
17632 /* If it's not a `}', then there is a non-trivial initializer. */
17633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17635 /* Parse the initializer list. */
17636 CONSTRUCTOR_ELTS (initializer)
17637 = cp_parser_initializer_list (parser, non_constant_p);
17638 /* A trailing `,' token is allowed. */
17639 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17640 cp_lexer_consume_token (parser->lexer);
17642 /* Now, there should be a trailing `}'. */
17643 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17644 TREE_TYPE (initializer) = init_list_type_node;
17645 return initializer;
17648 /* Parse an initializer-list.
17650 initializer-list:
17651 initializer-clause ... [opt]
17652 initializer-list , initializer-clause ... [opt]
17654 GNU Extension:
17656 initializer-list:
17657 designation initializer-clause ...[opt]
17658 initializer-list , designation initializer-clause ...[opt]
17660 designation:
17661 . identifier =
17662 identifier :
17663 [ constant-expression ] =
17665 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17666 for the initializer. If the INDEX of the elt is non-NULL, it is the
17667 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17668 as for cp_parser_initializer. */
17670 static VEC(constructor_elt,gc) *
17671 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17673 VEC(constructor_elt,gc) *v = NULL;
17675 /* Assume all of the expressions are constant. */
17676 *non_constant_p = false;
17678 /* Parse the rest of the list. */
17679 while (true)
17681 cp_token *token;
17682 tree designator;
17683 tree initializer;
17684 bool clause_non_constant_p;
17686 /* If the next token is an identifier and the following one is a
17687 colon, we are looking at the GNU designated-initializer
17688 syntax. */
17689 if (cp_parser_allow_gnu_extensions_p (parser)
17690 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17691 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17693 /* Warn the user that they are using an extension. */
17694 pedwarn (input_location, OPT_Wpedantic,
17695 "ISO C++ does not allow designated initializers");
17696 /* Consume the identifier. */
17697 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17698 /* Consume the `:'. */
17699 cp_lexer_consume_token (parser->lexer);
17701 /* Also handle the C99 syntax, '. id ='. */
17702 else if (cp_parser_allow_gnu_extensions_p (parser)
17703 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17704 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17705 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17707 /* Warn the user that they are using an extension. */
17708 pedwarn (input_location, OPT_Wpedantic,
17709 "ISO C++ does not allow C99 designated initializers");
17710 /* Consume the `.'. */
17711 cp_lexer_consume_token (parser->lexer);
17712 /* Consume the identifier. */
17713 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17714 /* Consume the `='. */
17715 cp_lexer_consume_token (parser->lexer);
17717 /* Also handle C99 array designators, '[ const ] ='. */
17718 else if (cp_parser_allow_gnu_extensions_p (parser)
17719 && !c_dialect_objc ()
17720 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17722 /* In C++11, [ could start a lambda-introducer. */
17723 cp_parser_parse_tentatively (parser);
17724 cp_lexer_consume_token (parser->lexer);
17725 designator = cp_parser_constant_expression (parser, false, NULL);
17726 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17727 cp_parser_require (parser, CPP_EQ, RT_EQ);
17728 if (!cp_parser_parse_definitely (parser))
17729 designator = NULL_TREE;
17731 else
17732 designator = NULL_TREE;
17734 /* Parse the initializer. */
17735 initializer = cp_parser_initializer_clause (parser,
17736 &clause_non_constant_p);
17737 /* If any clause is non-constant, so is the entire initializer. */
17738 if (clause_non_constant_p)
17739 *non_constant_p = true;
17741 /* If we have an ellipsis, this is an initializer pack
17742 expansion. */
17743 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17745 /* Consume the `...'. */
17746 cp_lexer_consume_token (parser->lexer);
17748 /* Turn the initializer into an initializer expansion. */
17749 initializer = make_pack_expansion (initializer);
17752 /* Add it to the vector. */
17753 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17755 /* If the next token is not a comma, we have reached the end of
17756 the list. */
17757 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17758 break;
17760 /* Peek at the next token. */
17761 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17762 /* If the next token is a `}', then we're still done. An
17763 initializer-clause can have a trailing `,' after the
17764 initializer-list and before the closing `}'. */
17765 if (token->type == CPP_CLOSE_BRACE)
17766 break;
17768 /* Consume the `,' token. */
17769 cp_lexer_consume_token (parser->lexer);
17772 return v;
17775 /* Classes [gram.class] */
17777 /* Parse a class-name.
17779 class-name:
17780 identifier
17781 template-id
17783 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17784 to indicate that names looked up in dependent types should be
17785 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17786 keyword has been used to indicate that the name that appears next
17787 is a template. TAG_TYPE indicates the explicit tag given before
17788 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17789 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17790 is the class being defined in a class-head.
17792 Returns the TYPE_DECL representing the class. */
17794 static tree
17795 cp_parser_class_name (cp_parser *parser,
17796 bool typename_keyword_p,
17797 bool template_keyword_p,
17798 enum tag_types tag_type,
17799 bool check_dependency_p,
17800 bool class_head_p,
17801 bool is_declaration)
17803 tree decl;
17804 tree scope;
17805 bool typename_p;
17806 cp_token *token;
17807 tree identifier = NULL_TREE;
17809 /* All class-names start with an identifier. */
17810 token = cp_lexer_peek_token (parser->lexer);
17811 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17813 cp_parser_error (parser, "expected class-name");
17814 return error_mark_node;
17817 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17818 to a template-id, so we save it here. */
17819 scope = parser->scope;
17820 if (scope == error_mark_node)
17821 return error_mark_node;
17823 /* Any name names a type if we're following the `typename' keyword
17824 in a qualified name where the enclosing scope is type-dependent. */
17825 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17826 && dependent_type_p (scope));
17827 /* Handle the common case (an identifier, but not a template-id)
17828 efficiently. */
17829 if (token->type == CPP_NAME
17830 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17832 cp_token *identifier_token;
17833 bool ambiguous_p;
17835 /* Look for the identifier. */
17836 identifier_token = cp_lexer_peek_token (parser->lexer);
17837 ambiguous_p = identifier_token->ambiguous_p;
17838 identifier = cp_parser_identifier (parser);
17839 /* If the next token isn't an identifier, we are certainly not
17840 looking at a class-name. */
17841 if (identifier == error_mark_node)
17842 decl = error_mark_node;
17843 /* If we know this is a type-name, there's no need to look it
17844 up. */
17845 else if (typename_p)
17846 decl = identifier;
17847 else
17849 tree ambiguous_decls;
17850 /* If we already know that this lookup is ambiguous, then
17851 we've already issued an error message; there's no reason
17852 to check again. */
17853 if (ambiguous_p)
17855 cp_parser_simulate_error (parser);
17856 return error_mark_node;
17858 /* If the next token is a `::', then the name must be a type
17859 name.
17861 [basic.lookup.qual]
17863 During the lookup for a name preceding the :: scope
17864 resolution operator, object, function, and enumerator
17865 names are ignored. */
17866 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17867 tag_type = typename_type;
17868 /* Look up the name. */
17869 decl = cp_parser_lookup_name (parser, identifier,
17870 tag_type,
17871 /*is_template=*/false,
17872 /*is_namespace=*/false,
17873 check_dependency_p,
17874 &ambiguous_decls,
17875 identifier_token->location);
17876 if (ambiguous_decls)
17878 if (cp_parser_parsing_tentatively (parser))
17879 cp_parser_simulate_error (parser);
17880 return error_mark_node;
17884 else
17886 /* Try a template-id. */
17887 decl = cp_parser_template_id (parser, template_keyword_p,
17888 check_dependency_p,
17889 tag_type,
17890 is_declaration);
17891 if (decl == error_mark_node)
17892 return error_mark_node;
17895 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17897 /* If this is a typename, create a TYPENAME_TYPE. */
17898 if (typename_p && decl != error_mark_node)
17900 decl = make_typename_type (scope, decl, typename_type,
17901 /*complain=*/tf_error);
17902 if (decl != error_mark_node)
17903 decl = TYPE_NAME (decl);
17906 decl = strip_using_decl (decl);
17908 /* Check to see that it is really the name of a class. */
17909 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17910 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17911 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17912 /* Situations like this:
17914 template <typename T> struct A {
17915 typename T::template X<int>::I i;
17918 are problematic. Is `T::template X<int>' a class-name? The
17919 standard does not seem to be definitive, but there is no other
17920 valid interpretation of the following `::'. Therefore, those
17921 names are considered class-names. */
17923 decl = make_typename_type (scope, decl, tag_type, tf_error);
17924 if (decl != error_mark_node)
17925 decl = TYPE_NAME (decl);
17927 else if (TREE_CODE (decl) != TYPE_DECL
17928 || TREE_TYPE (decl) == error_mark_node
17929 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17930 /* In Objective-C 2.0, a classname followed by '.' starts a
17931 dot-syntax expression, and it's not a type-name. */
17932 || (c_dialect_objc ()
17933 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17934 && objc_is_class_name (decl)))
17935 decl = error_mark_node;
17937 if (decl == error_mark_node)
17938 cp_parser_error (parser, "expected class-name");
17939 else if (identifier && !parser->scope)
17940 maybe_note_name_used_in_class (identifier, decl);
17942 return decl;
17945 /* Parse a class-specifier.
17947 class-specifier:
17948 class-head { member-specification [opt] }
17950 Returns the TREE_TYPE representing the class. */
17952 static tree
17953 cp_parser_class_specifier_1 (cp_parser* parser)
17955 tree type;
17956 tree attributes = NULL_TREE;
17957 bool nested_name_specifier_p;
17958 unsigned saved_num_template_parameter_lists;
17959 bool saved_in_function_body;
17960 unsigned char in_statement;
17961 bool in_switch_statement_p;
17962 bool saved_in_unbraced_linkage_specification_p;
17963 tree old_scope = NULL_TREE;
17964 tree scope = NULL_TREE;
17965 cp_token *closing_brace;
17967 push_deferring_access_checks (dk_no_deferred);
17969 /* Parse the class-head. */
17970 type = cp_parser_class_head (parser,
17971 &nested_name_specifier_p);
17972 /* If the class-head was a semantic disaster, skip the entire body
17973 of the class. */
17974 if (!type)
17976 cp_parser_skip_to_end_of_block_or_statement (parser);
17977 pop_deferring_access_checks ();
17978 return error_mark_node;
17981 /* Look for the `{'. */
17982 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17984 pop_deferring_access_checks ();
17985 return error_mark_node;
17988 /* Issue an error message if type-definitions are forbidden here. */
17989 cp_parser_check_type_definition (parser);
17990 /* Remember that we are defining one more class. */
17991 ++parser->num_classes_being_defined;
17992 /* Inside the class, surrounding template-parameter-lists do not
17993 apply. */
17994 saved_num_template_parameter_lists
17995 = parser->num_template_parameter_lists;
17996 parser->num_template_parameter_lists = 0;
17997 /* We are not in a function body. */
17998 saved_in_function_body = parser->in_function_body;
17999 parser->in_function_body = false;
18000 /* Or in a loop. */
18001 in_statement = parser->in_statement;
18002 parser->in_statement = 0;
18003 /* Or in a switch. */
18004 in_switch_statement_p = parser->in_switch_statement_p;
18005 parser->in_switch_statement_p = false;
18006 /* We are not immediately inside an extern "lang" block. */
18007 saved_in_unbraced_linkage_specification_p
18008 = parser->in_unbraced_linkage_specification_p;
18009 parser->in_unbraced_linkage_specification_p = false;
18011 /* Start the class. */
18012 if (nested_name_specifier_p)
18014 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18015 old_scope = push_inner_scope (scope);
18017 type = begin_class_definition (type);
18019 if (type == error_mark_node)
18020 /* If the type is erroneous, skip the entire body of the class. */
18021 cp_parser_skip_to_closing_brace (parser);
18022 else
18023 /* Parse the member-specification. */
18024 cp_parser_member_specification_opt (parser);
18026 /* Look for the trailing `}'. */
18027 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18028 /* Look for trailing attributes to apply to this class. */
18029 if (cp_parser_allow_gnu_extensions_p (parser))
18030 attributes = cp_parser_attributes_opt (parser);
18031 if (type != error_mark_node)
18032 type = finish_struct (type, attributes);
18033 if (nested_name_specifier_p)
18034 pop_inner_scope (old_scope, scope);
18036 /* We've finished a type definition. Check for the common syntax
18037 error of forgetting a semicolon after the definition. We need to
18038 be careful, as we can't just check for not-a-semicolon and be done
18039 with it; the user might have typed:
18041 class X { } c = ...;
18042 class X { } *p = ...;
18044 and so forth. Instead, enumerate all the possible tokens that
18045 might follow this production; if we don't see one of them, then
18046 complain and silently insert the semicolon. */
18048 cp_token *token = cp_lexer_peek_token (parser->lexer);
18049 bool want_semicolon = true;
18051 switch (token->type)
18053 case CPP_NAME:
18054 case CPP_SEMICOLON:
18055 case CPP_MULT:
18056 case CPP_AND:
18057 case CPP_OPEN_PAREN:
18058 case CPP_CLOSE_PAREN:
18059 case CPP_COMMA:
18060 want_semicolon = false;
18061 break;
18063 /* While it's legal for type qualifiers and storage class
18064 specifiers to follow type definitions in the grammar, only
18065 compiler testsuites contain code like that. Assume that if
18066 we see such code, then what we're really seeing is a case
18067 like:
18069 class X { }
18070 const <type> var = ...;
18074 class Y { }
18075 static <type> func (...) ...
18077 i.e. the qualifier or specifier applies to the next
18078 declaration. To do so, however, we need to look ahead one
18079 more token to see if *that* token is a type specifier.
18081 This code could be improved to handle:
18083 class Z { }
18084 static const <type> var = ...; */
18085 case CPP_KEYWORD:
18086 if (keyword_is_decl_specifier (token->keyword))
18088 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18090 /* Handling user-defined types here would be nice, but very
18091 tricky. */
18092 want_semicolon
18093 = (lookahead->type == CPP_KEYWORD
18094 && keyword_begins_type_specifier (lookahead->keyword));
18096 break;
18097 default:
18098 break;
18101 /* If we don't have a type, then something is very wrong and we
18102 shouldn't try to do anything clever. Likewise for not seeing the
18103 closing brace. */
18104 if (closing_brace && TYPE_P (type) && want_semicolon)
18106 cp_token_position prev
18107 = cp_lexer_previous_token_position (parser->lexer);
18108 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18109 location_t loc = prev_token->location;
18111 if (CLASSTYPE_DECLARED_CLASS (type))
18112 error_at (loc, "expected %<;%> after class definition");
18113 else if (TREE_CODE (type) == RECORD_TYPE)
18114 error_at (loc, "expected %<;%> after struct definition");
18115 else if (TREE_CODE (type) == UNION_TYPE)
18116 error_at (loc, "expected %<;%> after union definition");
18117 else
18118 gcc_unreachable ();
18120 /* Unget one token and smash it to look as though we encountered
18121 a semicolon in the input stream. */
18122 cp_lexer_set_token_position (parser->lexer, prev);
18123 token = cp_lexer_peek_token (parser->lexer);
18124 token->type = CPP_SEMICOLON;
18125 token->keyword = RID_MAX;
18129 /* If this class is not itself within the scope of another class,
18130 then we need to parse the bodies of all of the queued function
18131 definitions. Note that the queued functions defined in a class
18132 are not always processed immediately following the
18133 class-specifier for that class. Consider:
18135 struct A {
18136 struct B { void f() { sizeof (A); } };
18139 If `f' were processed before the processing of `A' were
18140 completed, there would be no way to compute the size of `A'.
18141 Note that the nesting we are interested in here is lexical --
18142 not the semantic nesting given by TYPE_CONTEXT. In particular,
18143 for:
18145 struct A { struct B; };
18146 struct A::B { void f() { } };
18148 there is no need to delay the parsing of `A::B::f'. */
18149 if (--parser->num_classes_being_defined == 0)
18151 tree decl;
18152 tree class_type = NULL_TREE;
18153 tree pushed_scope = NULL_TREE;
18154 unsigned ix;
18155 cp_default_arg_entry *e;
18156 tree save_ccp, save_ccr;
18158 /* In a first pass, parse default arguments to the functions.
18159 Then, in a second pass, parse the bodies of the functions.
18160 This two-phased approach handles cases like:
18162 struct S {
18163 void f() { g(); }
18164 void g(int i = 3);
18168 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18169 ix, e)
18171 decl = e->decl;
18172 /* If there are default arguments that have not yet been processed,
18173 take care of them now. */
18174 if (class_type != e->class_type)
18176 if (pushed_scope)
18177 pop_scope (pushed_scope);
18178 class_type = e->class_type;
18179 pushed_scope = push_scope (class_type);
18181 /* Make sure that any template parameters are in scope. */
18182 maybe_begin_member_template_processing (decl);
18183 /* Parse the default argument expressions. */
18184 cp_parser_late_parsing_default_args (parser, decl);
18185 /* Remove any template parameters from the symbol table. */
18186 maybe_end_member_template_processing ();
18188 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18189 /* Now parse any NSDMIs. */
18190 save_ccp = current_class_ptr;
18191 save_ccr = current_class_ref;
18192 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18194 if (class_type != DECL_CONTEXT (decl))
18196 if (pushed_scope)
18197 pop_scope (pushed_scope);
18198 class_type = DECL_CONTEXT (decl);
18199 pushed_scope = push_scope (class_type);
18201 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18202 cp_parser_late_parsing_nsdmi (parser, decl);
18204 VEC_truncate (tree, unparsed_nsdmis, 0);
18205 current_class_ptr = save_ccp;
18206 current_class_ref = save_ccr;
18207 if (pushed_scope)
18208 pop_scope (pushed_scope);
18209 /* Now parse the body of the functions. */
18210 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18211 cp_parser_late_parsing_for_member (parser, decl);
18212 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18215 /* Put back any saved access checks. */
18216 pop_deferring_access_checks ();
18218 /* Restore saved state. */
18219 parser->in_switch_statement_p = in_switch_statement_p;
18220 parser->in_statement = in_statement;
18221 parser->in_function_body = saved_in_function_body;
18222 parser->num_template_parameter_lists
18223 = saved_num_template_parameter_lists;
18224 parser->in_unbraced_linkage_specification_p
18225 = saved_in_unbraced_linkage_specification_p;
18227 return type;
18230 static tree
18231 cp_parser_class_specifier (cp_parser* parser)
18233 tree ret;
18234 timevar_push (TV_PARSE_STRUCT);
18235 ret = cp_parser_class_specifier_1 (parser);
18236 timevar_pop (TV_PARSE_STRUCT);
18237 return ret;
18240 /* Parse a class-head.
18242 class-head:
18243 class-key identifier [opt] base-clause [opt]
18244 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18245 class-key nested-name-specifier [opt] template-id
18246 base-clause [opt]
18248 class-virt-specifier:
18249 final
18251 GNU Extensions:
18252 class-key attributes identifier [opt] base-clause [opt]
18253 class-key attributes nested-name-specifier identifier base-clause [opt]
18254 class-key attributes nested-name-specifier [opt] template-id
18255 base-clause [opt]
18257 Upon return BASES is initialized to the list of base classes (or
18258 NULL, if there are none) in the same form returned by
18259 cp_parser_base_clause.
18261 Returns the TYPE of the indicated class. Sets
18262 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18263 involving a nested-name-specifier was used, and FALSE otherwise.
18265 Returns error_mark_node if this is not a class-head.
18267 Returns NULL_TREE if the class-head is syntactically valid, but
18268 semantically invalid in a way that means we should skip the entire
18269 body of the class. */
18271 static tree
18272 cp_parser_class_head (cp_parser* parser,
18273 bool* nested_name_specifier_p)
18275 tree nested_name_specifier;
18276 enum tag_types class_key;
18277 tree id = NULL_TREE;
18278 tree type = NULL_TREE;
18279 tree attributes;
18280 tree bases;
18281 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18282 bool template_id_p = false;
18283 bool qualified_p = false;
18284 bool invalid_nested_name_p = false;
18285 bool invalid_explicit_specialization_p = false;
18286 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18287 tree pushed_scope = NULL_TREE;
18288 unsigned num_templates;
18289 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18290 /* Assume no nested-name-specifier will be present. */
18291 *nested_name_specifier_p = false;
18292 /* Assume no template parameter lists will be used in defining the
18293 type. */
18294 num_templates = 0;
18295 parser->colon_corrects_to_scope_p = false;
18297 /* Look for the class-key. */
18298 class_key = cp_parser_class_key (parser);
18299 if (class_key == none_type)
18300 return error_mark_node;
18302 /* Parse the attributes. */
18303 attributes = cp_parser_attributes_opt (parser);
18305 /* If the next token is `::', that is invalid -- but sometimes
18306 people do try to write:
18308 struct ::S {};
18310 Handle this gracefully by accepting the extra qualifier, and then
18311 issuing an error about it later if this really is a
18312 class-head. If it turns out just to be an elaborated type
18313 specifier, remain silent. */
18314 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18315 qualified_p = true;
18317 push_deferring_access_checks (dk_no_check);
18319 /* Determine the name of the class. Begin by looking for an
18320 optional nested-name-specifier. */
18321 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18322 nested_name_specifier
18323 = cp_parser_nested_name_specifier_opt (parser,
18324 /*typename_keyword_p=*/false,
18325 /*check_dependency_p=*/false,
18326 /*type_p=*/true,
18327 /*is_declaration=*/false);
18328 /* If there was a nested-name-specifier, then there *must* be an
18329 identifier. */
18330 if (nested_name_specifier)
18332 type_start_token = cp_lexer_peek_token (parser->lexer);
18333 /* Although the grammar says `identifier', it really means
18334 `class-name' or `template-name'. You are only allowed to
18335 define a class that has already been declared with this
18336 syntax.
18338 The proposed resolution for Core Issue 180 says that wherever
18339 you see `class T::X' you should treat `X' as a type-name.
18341 It is OK to define an inaccessible class; for example:
18343 class A { class B; };
18344 class A::B {};
18346 We do not know if we will see a class-name, or a
18347 template-name. We look for a class-name first, in case the
18348 class-name is a template-id; if we looked for the
18349 template-name first we would stop after the template-name. */
18350 cp_parser_parse_tentatively (parser);
18351 type = cp_parser_class_name (parser,
18352 /*typename_keyword_p=*/false,
18353 /*template_keyword_p=*/false,
18354 class_type,
18355 /*check_dependency_p=*/false,
18356 /*class_head_p=*/true,
18357 /*is_declaration=*/false);
18358 /* If that didn't work, ignore the nested-name-specifier. */
18359 if (!cp_parser_parse_definitely (parser))
18361 invalid_nested_name_p = true;
18362 type_start_token = cp_lexer_peek_token (parser->lexer);
18363 id = cp_parser_identifier (parser);
18364 if (id == error_mark_node)
18365 id = NULL_TREE;
18367 /* If we could not find a corresponding TYPE, treat this
18368 declaration like an unqualified declaration. */
18369 if (type == error_mark_node)
18370 nested_name_specifier = NULL_TREE;
18371 /* Otherwise, count the number of templates used in TYPE and its
18372 containing scopes. */
18373 else
18375 tree scope;
18377 for (scope = TREE_TYPE (type);
18378 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18379 scope = (TYPE_P (scope)
18380 ? TYPE_CONTEXT (scope)
18381 : DECL_CONTEXT (scope)))
18382 if (TYPE_P (scope)
18383 && CLASS_TYPE_P (scope)
18384 && CLASSTYPE_TEMPLATE_INFO (scope)
18385 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18386 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18387 ++num_templates;
18390 /* Otherwise, the identifier is optional. */
18391 else
18393 /* We don't know whether what comes next is a template-id,
18394 an identifier, or nothing at all. */
18395 cp_parser_parse_tentatively (parser);
18396 /* Check for a template-id. */
18397 type_start_token = cp_lexer_peek_token (parser->lexer);
18398 id = cp_parser_template_id (parser,
18399 /*template_keyword_p=*/false,
18400 /*check_dependency_p=*/true,
18401 class_key,
18402 /*is_declaration=*/true);
18403 /* If that didn't work, it could still be an identifier. */
18404 if (!cp_parser_parse_definitely (parser))
18406 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18408 type_start_token = cp_lexer_peek_token (parser->lexer);
18409 id = cp_parser_identifier (parser);
18411 else
18412 id = NULL_TREE;
18414 else
18416 template_id_p = true;
18417 ++num_templates;
18421 pop_deferring_access_checks ();
18423 if (id)
18425 cp_parser_check_for_invalid_template_id (parser, id,
18426 class_key,
18427 type_start_token->location);
18429 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18431 /* If it's not a `:' or a `{' then we can't really be looking at a
18432 class-head, since a class-head only appears as part of a
18433 class-specifier. We have to detect this situation before calling
18434 xref_tag, since that has irreversible side-effects. */
18435 if (!cp_parser_next_token_starts_class_definition_p (parser))
18437 cp_parser_error (parser, "expected %<{%> or %<:%>");
18438 type = error_mark_node;
18439 goto out;
18442 /* At this point, we're going ahead with the class-specifier, even
18443 if some other problem occurs. */
18444 cp_parser_commit_to_tentative_parse (parser);
18445 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18447 cp_parser_error (parser,
18448 "cannot specify %<override%> for a class");
18449 type = error_mark_node;
18450 goto out;
18452 /* Issue the error about the overly-qualified name now. */
18453 if (qualified_p)
18455 cp_parser_error (parser,
18456 "global qualification of class name is invalid");
18457 type = error_mark_node;
18458 goto out;
18460 else if (invalid_nested_name_p)
18462 cp_parser_error (parser,
18463 "qualified name does not name a class");
18464 type = error_mark_node;
18465 goto out;
18467 else if (nested_name_specifier)
18469 tree scope;
18471 /* Reject typedef-names in class heads. */
18472 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18474 error_at (type_start_token->location,
18475 "invalid class name in declaration of %qD",
18476 type);
18477 type = NULL_TREE;
18478 goto done;
18481 /* Figure out in what scope the declaration is being placed. */
18482 scope = current_scope ();
18483 /* If that scope does not contain the scope in which the
18484 class was originally declared, the program is invalid. */
18485 if (scope && !is_ancestor (scope, nested_name_specifier))
18487 if (at_namespace_scope_p ())
18488 error_at (type_start_token->location,
18489 "declaration of %qD in namespace %qD which does not "
18490 "enclose %qD",
18491 type, scope, nested_name_specifier);
18492 else
18493 error_at (type_start_token->location,
18494 "declaration of %qD in %qD which does not enclose %qD",
18495 type, scope, nested_name_specifier);
18496 type = NULL_TREE;
18497 goto done;
18499 /* [dcl.meaning]
18501 A declarator-id shall not be qualified except for the
18502 definition of a ... nested class outside of its class
18503 ... [or] the definition or explicit instantiation of a
18504 class member of a namespace outside of its namespace. */
18505 if (scope == nested_name_specifier)
18507 permerror (nested_name_specifier_token_start->location,
18508 "extra qualification not allowed");
18509 nested_name_specifier = NULL_TREE;
18510 num_templates = 0;
18513 /* An explicit-specialization must be preceded by "template <>". If
18514 it is not, try to recover gracefully. */
18515 if (at_namespace_scope_p ()
18516 && parser->num_template_parameter_lists == 0
18517 && template_id_p)
18519 error_at (type_start_token->location,
18520 "an explicit specialization must be preceded by %<template <>%>");
18521 invalid_explicit_specialization_p = true;
18522 /* Take the same action that would have been taken by
18523 cp_parser_explicit_specialization. */
18524 ++parser->num_template_parameter_lists;
18525 begin_specialization ();
18527 /* There must be no "return" statements between this point and the
18528 end of this function; set "type "to the correct return value and
18529 use "goto done;" to return. */
18530 /* Make sure that the right number of template parameters were
18531 present. */
18532 if (!cp_parser_check_template_parameters (parser, num_templates,
18533 type_start_token->location,
18534 /*declarator=*/NULL))
18536 /* If something went wrong, there is no point in even trying to
18537 process the class-definition. */
18538 type = NULL_TREE;
18539 goto done;
18542 /* Look up the type. */
18543 if (template_id_p)
18545 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18546 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18547 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18549 error_at (type_start_token->location,
18550 "function template %qD redeclared as a class template", id);
18551 type = error_mark_node;
18553 else
18555 type = TREE_TYPE (id);
18556 type = maybe_process_partial_specialization (type);
18558 if (nested_name_specifier)
18559 pushed_scope = push_scope (nested_name_specifier);
18561 else if (nested_name_specifier)
18563 tree class_type;
18565 /* Given:
18567 template <typename T> struct S { struct T };
18568 template <typename T> struct S<T>::T { };
18570 we will get a TYPENAME_TYPE when processing the definition of
18571 `S::T'. We need to resolve it to the actual type before we
18572 try to define it. */
18573 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18575 class_type = resolve_typename_type (TREE_TYPE (type),
18576 /*only_current_p=*/false);
18577 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18578 type = TYPE_NAME (class_type);
18579 else
18581 cp_parser_error (parser, "could not resolve typename type");
18582 type = error_mark_node;
18586 if (maybe_process_partial_specialization (TREE_TYPE (type))
18587 == error_mark_node)
18589 type = NULL_TREE;
18590 goto done;
18593 class_type = current_class_type;
18594 /* Enter the scope indicated by the nested-name-specifier. */
18595 pushed_scope = push_scope (nested_name_specifier);
18596 /* Get the canonical version of this type. */
18597 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18598 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18599 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18601 type = push_template_decl (type);
18602 if (type == error_mark_node)
18604 type = NULL_TREE;
18605 goto done;
18609 type = TREE_TYPE (type);
18610 *nested_name_specifier_p = true;
18612 else /* The name is not a nested name. */
18614 /* If the class was unnamed, create a dummy name. */
18615 if (!id)
18616 id = make_anon_name ();
18617 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18618 parser->num_template_parameter_lists);
18621 /* Indicate whether this class was declared as a `class' or as a
18622 `struct'. */
18623 if (TREE_CODE (type) == RECORD_TYPE)
18624 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18625 cp_parser_check_class_key (class_key, type);
18627 /* If this type was already complete, and we see another definition,
18628 that's an error. */
18629 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18631 error_at (type_start_token->location, "redefinition of %q#T",
18632 type);
18633 error_at (type_start_token->location, "previous definition of %q+#T",
18634 type);
18635 type = NULL_TREE;
18636 goto done;
18638 else if (type == error_mark_node)
18639 type = NULL_TREE;
18641 if (type)
18643 /* Apply attributes now, before any use of the class as a template
18644 argument in its base list. */
18645 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18646 fixup_attribute_variants (type);
18649 /* We will have entered the scope containing the class; the names of
18650 base classes should be looked up in that context. For example:
18652 struct A { struct B {}; struct C; };
18653 struct A::C : B {};
18655 is valid. */
18657 /* Get the list of base-classes, if there is one. */
18658 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18659 bases = cp_parser_base_clause (parser);
18660 else
18661 bases = NULL_TREE;
18663 /* If we're really defining a class, process the base classes.
18664 If they're invalid, fail. */
18665 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18666 && !xref_basetypes (type, bases))
18667 type = NULL_TREE;
18669 done:
18670 /* Leave the scope given by the nested-name-specifier. We will
18671 enter the class scope itself while processing the members. */
18672 if (pushed_scope)
18673 pop_scope (pushed_scope);
18675 if (invalid_explicit_specialization_p)
18677 end_specialization ();
18678 --parser->num_template_parameter_lists;
18681 if (type)
18682 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18683 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18684 CLASSTYPE_FINAL (type) = 1;
18685 out:
18686 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18687 return type;
18690 /* Parse a class-key.
18692 class-key:
18693 class
18694 struct
18695 union
18697 Returns the kind of class-key specified, or none_type to indicate
18698 error. */
18700 static enum tag_types
18701 cp_parser_class_key (cp_parser* parser)
18703 cp_token *token;
18704 enum tag_types tag_type;
18706 /* Look for the class-key. */
18707 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18708 if (!token)
18709 return none_type;
18711 /* Check to see if the TOKEN is a class-key. */
18712 tag_type = cp_parser_token_is_class_key (token);
18713 if (!tag_type)
18714 cp_parser_error (parser, "expected class-key");
18715 return tag_type;
18718 /* Parse an (optional) member-specification.
18720 member-specification:
18721 member-declaration member-specification [opt]
18722 access-specifier : member-specification [opt] */
18724 static void
18725 cp_parser_member_specification_opt (cp_parser* parser)
18727 while (true)
18729 cp_token *token;
18730 enum rid keyword;
18732 /* Peek at the next token. */
18733 token = cp_lexer_peek_token (parser->lexer);
18734 /* If it's a `}', or EOF then we've seen all the members. */
18735 if (token->type == CPP_CLOSE_BRACE
18736 || token->type == CPP_EOF
18737 || token->type == CPP_PRAGMA_EOL)
18738 break;
18740 /* See if this token is a keyword. */
18741 keyword = token->keyword;
18742 switch (keyword)
18744 case RID_PUBLIC:
18745 case RID_PROTECTED:
18746 case RID_PRIVATE:
18747 /* Consume the access-specifier. */
18748 cp_lexer_consume_token (parser->lexer);
18749 /* Remember which access-specifier is active. */
18750 current_access_specifier = token->u.value;
18751 /* Look for the `:'. */
18752 cp_parser_require (parser, CPP_COLON, RT_COLON);
18753 break;
18755 default:
18756 /* Accept #pragmas at class scope. */
18757 if (token->type == CPP_PRAGMA)
18759 cp_parser_pragma (parser, pragma_external);
18760 break;
18763 /* Otherwise, the next construction must be a
18764 member-declaration. */
18765 cp_parser_member_declaration (parser);
18770 /* Parse a member-declaration.
18772 member-declaration:
18773 decl-specifier-seq [opt] member-declarator-list [opt] ;
18774 function-definition ; [opt]
18775 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18776 using-declaration
18777 template-declaration
18778 alias-declaration
18780 member-declarator-list:
18781 member-declarator
18782 member-declarator-list , member-declarator
18784 member-declarator:
18785 declarator pure-specifier [opt]
18786 declarator constant-initializer [opt]
18787 identifier [opt] : constant-expression
18789 GNU Extensions:
18791 member-declaration:
18792 __extension__ member-declaration
18794 member-declarator:
18795 declarator attributes [opt] pure-specifier [opt]
18796 declarator attributes [opt] constant-initializer [opt]
18797 identifier [opt] attributes [opt] : constant-expression
18799 C++0x Extensions:
18801 member-declaration:
18802 static_assert-declaration */
18804 static void
18805 cp_parser_member_declaration (cp_parser* parser)
18807 cp_decl_specifier_seq decl_specifiers;
18808 tree prefix_attributes;
18809 tree decl;
18810 int declares_class_or_enum;
18811 bool friend_p;
18812 cp_token *token = NULL;
18813 cp_token *decl_spec_token_start = NULL;
18814 cp_token *initializer_token_start = NULL;
18815 int saved_pedantic;
18816 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18818 /* Check for the `__extension__' keyword. */
18819 if (cp_parser_extension_opt (parser, &saved_pedantic))
18821 /* Recurse. */
18822 cp_parser_member_declaration (parser);
18823 /* Restore the old value of the PEDANTIC flag. */
18824 pedantic = saved_pedantic;
18826 return;
18829 /* Check for a template-declaration. */
18830 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18832 /* An explicit specialization here is an error condition, and we
18833 expect the specialization handler to detect and report this. */
18834 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18835 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18836 cp_parser_explicit_specialization (parser);
18837 else
18838 cp_parser_template_declaration (parser, /*member_p=*/true);
18840 return;
18843 /* Check for a using-declaration. */
18844 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18846 if (cxx_dialect < cxx0x)
18848 /* Parse the using-declaration. */
18849 cp_parser_using_declaration (parser,
18850 /*access_declaration_p=*/false);
18851 return;
18853 else
18855 tree decl;
18856 cp_parser_parse_tentatively (parser);
18857 decl = cp_parser_alias_declaration (parser);
18858 if (cp_parser_parse_definitely (parser))
18859 finish_member_declaration (decl);
18860 else
18861 cp_parser_using_declaration (parser,
18862 /*access_declaration_p=*/false);
18863 return;
18867 /* Check for @defs. */
18868 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18870 tree ivar, member;
18871 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18872 ivar = ivar_chains;
18873 while (ivar)
18875 member = ivar;
18876 ivar = TREE_CHAIN (member);
18877 TREE_CHAIN (member) = NULL_TREE;
18878 finish_member_declaration (member);
18880 return;
18883 /* If the next token is `static_assert' we have a static assertion. */
18884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18886 cp_parser_static_assert (parser, /*member_p=*/true);
18887 return;
18890 parser->colon_corrects_to_scope_p = false;
18892 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18893 goto out;
18895 /* Parse the decl-specifier-seq. */
18896 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18897 cp_parser_decl_specifier_seq (parser,
18898 CP_PARSER_FLAGS_OPTIONAL,
18899 &decl_specifiers,
18900 &declares_class_or_enum);
18901 prefix_attributes = decl_specifiers.attributes;
18902 decl_specifiers.attributes = NULL_TREE;
18903 /* Check for an invalid type-name. */
18904 if (!decl_specifiers.any_type_specifiers_p
18905 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18906 goto out;
18907 /* If there is no declarator, then the decl-specifier-seq should
18908 specify a type. */
18909 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18911 /* If there was no decl-specifier-seq, and the next token is a
18912 `;', then we have something like:
18914 struct S { ; };
18916 [class.mem]
18918 Each member-declaration shall declare at least one member
18919 name of the class. */
18920 if (!decl_specifiers.any_specifiers_p)
18922 cp_token *token = cp_lexer_peek_token (parser->lexer);
18923 if (!in_system_header_at (token->location))
18924 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
18926 else
18928 tree type;
18930 /* See if this declaration is a friend. */
18931 friend_p = cp_parser_friend_p (&decl_specifiers);
18932 /* If there were decl-specifiers, check to see if there was
18933 a class-declaration. */
18934 type = check_tag_decl (&decl_specifiers);
18935 /* Nested classes have already been added to the class, but
18936 a `friend' needs to be explicitly registered. */
18937 if (friend_p)
18939 /* If the `friend' keyword was present, the friend must
18940 be introduced with a class-key. */
18941 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18942 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
18943 "in C++03 a class-key must be used "
18944 "when declaring a friend");
18945 /* In this case:
18947 template <typename T> struct A {
18948 friend struct A<T>::B;
18951 A<T>::B will be represented by a TYPENAME_TYPE, and
18952 therefore not recognized by check_tag_decl. */
18953 if (!type)
18955 type = decl_specifiers.type;
18956 if (type && TREE_CODE (type) == TYPE_DECL)
18957 type = TREE_TYPE (type);
18959 if (!type || !TYPE_P (type))
18960 error_at (decl_spec_token_start->location,
18961 "friend declaration does not name a class or "
18962 "function");
18963 else
18964 make_friend_class (current_class_type, type,
18965 /*complain=*/true);
18967 /* If there is no TYPE, an error message will already have
18968 been issued. */
18969 else if (!type || type == error_mark_node)
18971 /* An anonymous aggregate has to be handled specially; such
18972 a declaration really declares a data member (with a
18973 particular type), as opposed to a nested class. */
18974 else if (ANON_AGGR_TYPE_P (type))
18976 /* C++11 9.5/6. */
18977 if (decl_specifiers.storage_class != sc_none)
18978 error_at (decl_spec_token_start->location,
18979 "a storage class on an anonymous aggregate "
18980 "in class scope is not allowed");
18982 /* Remove constructors and such from TYPE, now that we
18983 know it is an anonymous aggregate. */
18984 fixup_anonymous_aggr (type);
18985 /* And make the corresponding data member. */
18986 decl = build_decl (decl_spec_token_start->location,
18987 FIELD_DECL, NULL_TREE, type);
18988 /* Add it to the class. */
18989 finish_member_declaration (decl);
18991 else
18992 cp_parser_check_access_in_redeclaration
18993 (TYPE_NAME (type),
18994 decl_spec_token_start->location);
18997 else
18999 bool assume_semicolon = false;
19001 /* See if these declarations will be friends. */
19002 friend_p = cp_parser_friend_p (&decl_specifiers);
19004 /* Keep going until we hit the `;' at the end of the
19005 declaration. */
19006 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19008 tree attributes = NULL_TREE;
19009 tree first_attribute;
19011 /* Peek at the next token. */
19012 token = cp_lexer_peek_token (parser->lexer);
19014 /* Check for a bitfield declaration. */
19015 if (token->type == CPP_COLON
19016 || (token->type == CPP_NAME
19017 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19018 == CPP_COLON))
19020 tree identifier;
19021 tree width;
19023 /* Get the name of the bitfield. Note that we cannot just
19024 check TOKEN here because it may have been invalidated by
19025 the call to cp_lexer_peek_nth_token above. */
19026 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19027 identifier = cp_parser_identifier (parser);
19028 else
19029 identifier = NULL_TREE;
19031 /* Consume the `:' token. */
19032 cp_lexer_consume_token (parser->lexer);
19033 /* Get the width of the bitfield. */
19034 width
19035 = cp_parser_constant_expression (parser,
19036 /*allow_non_constant=*/false,
19037 NULL);
19039 /* Look for attributes that apply to the bitfield. */
19040 attributes = cp_parser_attributes_opt (parser);
19041 /* Remember which attributes are prefix attributes and
19042 which are not. */
19043 first_attribute = attributes;
19044 /* Combine the attributes. */
19045 attributes = chainon (prefix_attributes, attributes);
19047 /* Create the bitfield declaration. */
19048 decl = grokbitfield (identifier
19049 ? make_id_declarator (NULL_TREE,
19050 identifier,
19051 sfk_none)
19052 : NULL,
19053 &decl_specifiers,
19054 width,
19055 attributes);
19057 else
19059 cp_declarator *declarator;
19060 tree initializer;
19061 tree asm_specification;
19062 int ctor_dtor_or_conv_p;
19064 /* Parse the declarator. */
19065 declarator
19066 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19067 &ctor_dtor_or_conv_p,
19068 /*parenthesized_p=*/NULL,
19069 /*member_p=*/true);
19071 /* If something went wrong parsing the declarator, make sure
19072 that we at least consume some tokens. */
19073 if (declarator == cp_error_declarator)
19075 /* Skip to the end of the statement. */
19076 cp_parser_skip_to_end_of_statement (parser);
19077 /* If the next token is not a semicolon, that is
19078 probably because we just skipped over the body of
19079 a function. So, we consume a semicolon if
19080 present, but do not issue an error message if it
19081 is not present. */
19082 if (cp_lexer_next_token_is (parser->lexer,
19083 CPP_SEMICOLON))
19084 cp_lexer_consume_token (parser->lexer);
19085 goto out;
19088 if (declares_class_or_enum & 2)
19089 cp_parser_check_for_definition_in_return_type
19090 (declarator, decl_specifiers.type,
19091 decl_specifiers.locations[ds_type_spec]);
19093 /* Look for an asm-specification. */
19094 asm_specification = cp_parser_asm_specification_opt (parser);
19095 /* Look for attributes that apply to the declaration. */
19096 attributes = cp_parser_attributes_opt (parser);
19097 /* Remember which attributes are prefix attributes and
19098 which are not. */
19099 first_attribute = attributes;
19100 /* Combine the attributes. */
19101 attributes = chainon (prefix_attributes, attributes);
19103 /* If it's an `=', then we have a constant-initializer or a
19104 pure-specifier. It is not correct to parse the
19105 initializer before registering the member declaration
19106 since the member declaration should be in scope while
19107 its initializer is processed. However, the rest of the
19108 front end does not yet provide an interface that allows
19109 us to handle this correctly. */
19110 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19112 /* In [class.mem]:
19114 A pure-specifier shall be used only in the declaration of
19115 a virtual function.
19117 A member-declarator can contain a constant-initializer
19118 only if it declares a static member of integral or
19119 enumeration type.
19121 Therefore, if the DECLARATOR is for a function, we look
19122 for a pure-specifier; otherwise, we look for a
19123 constant-initializer. When we call `grokfield', it will
19124 perform more stringent semantics checks. */
19125 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19126 if (function_declarator_p (declarator)
19127 || (decl_specifiers.type
19128 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19129 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19130 == FUNCTION_TYPE)))
19131 initializer = cp_parser_pure_specifier (parser);
19132 else if (decl_specifiers.storage_class != sc_static)
19133 initializer = cp_parser_save_nsdmi (parser);
19134 else if (cxx_dialect >= cxx0x)
19136 bool nonconst;
19137 /* Don't require a constant rvalue in C++11, since we
19138 might want a reference constant. We'll enforce
19139 constancy later. */
19140 cp_lexer_consume_token (parser->lexer);
19141 /* Parse the initializer. */
19142 initializer = cp_parser_initializer_clause (parser,
19143 &nonconst);
19145 else
19146 /* Parse the initializer. */
19147 initializer = cp_parser_constant_initializer (parser);
19149 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19150 && !function_declarator_p (declarator))
19152 bool x;
19153 if (decl_specifiers.storage_class != sc_static)
19154 initializer = cp_parser_save_nsdmi (parser);
19155 else
19156 initializer = cp_parser_initializer (parser, &x, &x);
19158 /* Otherwise, there is no initializer. */
19159 else
19160 initializer = NULL_TREE;
19162 /* See if we are probably looking at a function
19163 definition. We are certainly not looking at a
19164 member-declarator. Calling `grokfield' has
19165 side-effects, so we must not do it unless we are sure
19166 that we are looking at a member-declarator. */
19167 if (cp_parser_token_starts_function_definition_p
19168 (cp_lexer_peek_token (parser->lexer)))
19170 /* The grammar does not allow a pure-specifier to be
19171 used when a member function is defined. (It is
19172 possible that this fact is an oversight in the
19173 standard, since a pure function may be defined
19174 outside of the class-specifier. */
19175 if (initializer && initializer_token_start)
19176 error_at (initializer_token_start->location,
19177 "pure-specifier on function-definition");
19178 decl = cp_parser_save_member_function_body (parser,
19179 &decl_specifiers,
19180 declarator,
19181 attributes);
19182 /* If the member was not a friend, declare it here. */
19183 if (!friend_p)
19184 finish_member_declaration (decl);
19185 /* Peek at the next token. */
19186 token = cp_lexer_peek_token (parser->lexer);
19187 /* If the next token is a semicolon, consume it. */
19188 if (token->type == CPP_SEMICOLON)
19189 cp_lexer_consume_token (parser->lexer);
19190 goto out;
19192 else
19193 if (declarator->kind == cdk_function)
19194 declarator->id_loc = token->location;
19195 /* Create the declaration. */
19196 decl = grokfield (declarator, &decl_specifiers,
19197 initializer, /*init_const_expr_p=*/true,
19198 asm_specification,
19199 attributes);
19202 /* Reset PREFIX_ATTRIBUTES. */
19203 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19204 attributes = TREE_CHAIN (attributes);
19205 if (attributes)
19206 TREE_CHAIN (attributes) = NULL_TREE;
19208 /* If there is any qualification still in effect, clear it
19209 now; we will be starting fresh with the next declarator. */
19210 parser->scope = NULL_TREE;
19211 parser->qualifying_scope = NULL_TREE;
19212 parser->object_scope = NULL_TREE;
19213 /* If it's a `,', then there are more declarators. */
19214 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19215 cp_lexer_consume_token (parser->lexer);
19216 /* If the next token isn't a `;', then we have a parse error. */
19217 else if (cp_lexer_next_token_is_not (parser->lexer,
19218 CPP_SEMICOLON))
19220 /* The next token might be a ways away from where the
19221 actual semicolon is missing. Find the previous token
19222 and use that for our error position. */
19223 cp_token *token = cp_lexer_previous_token (parser->lexer);
19224 error_at (token->location,
19225 "expected %<;%> at end of member declaration");
19227 /* Assume that the user meant to provide a semicolon. If
19228 we were to cp_parser_skip_to_end_of_statement, we might
19229 skip to a semicolon inside a member function definition
19230 and issue nonsensical error messages. */
19231 assume_semicolon = true;
19234 if (decl)
19236 /* Add DECL to the list of members. */
19237 if (!friend_p)
19238 finish_member_declaration (decl);
19240 if (TREE_CODE (decl) == FUNCTION_DECL)
19241 cp_parser_save_default_args (parser, decl);
19242 else if (TREE_CODE (decl) == FIELD_DECL
19243 && !DECL_C_BIT_FIELD (decl)
19244 && DECL_INITIAL (decl))
19245 /* Add DECL to the queue of NSDMI to be parsed later. */
19246 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19249 if (assume_semicolon)
19250 goto out;
19254 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19255 out:
19256 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19259 /* Parse a pure-specifier.
19261 pure-specifier:
19264 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19265 Otherwise, ERROR_MARK_NODE is returned. */
19267 static tree
19268 cp_parser_pure_specifier (cp_parser* parser)
19270 cp_token *token;
19272 /* Look for the `=' token. */
19273 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19274 return error_mark_node;
19275 /* Look for the `0' token. */
19276 token = cp_lexer_peek_token (parser->lexer);
19278 if (token->type == CPP_EOF
19279 || token->type == CPP_PRAGMA_EOL)
19280 return error_mark_node;
19282 cp_lexer_consume_token (parser->lexer);
19284 /* Accept = default or = delete in c++0x mode. */
19285 if (token->keyword == RID_DEFAULT
19286 || token->keyword == RID_DELETE)
19288 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19289 return token->u.value;
19292 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19293 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19295 cp_parser_error (parser,
19296 "invalid pure specifier (only %<= 0%> is allowed)");
19297 cp_parser_skip_to_end_of_statement (parser);
19298 return error_mark_node;
19300 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19302 error_at (token->location, "templates may not be %<virtual%>");
19303 return error_mark_node;
19306 return integer_zero_node;
19309 /* Parse a constant-initializer.
19311 constant-initializer:
19312 = constant-expression
19314 Returns a representation of the constant-expression. */
19316 static tree
19317 cp_parser_constant_initializer (cp_parser* parser)
19319 /* Look for the `=' token. */
19320 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19321 return error_mark_node;
19323 /* It is invalid to write:
19325 struct S { static const int i = { 7 }; };
19328 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19330 cp_parser_error (parser,
19331 "a brace-enclosed initializer is not allowed here");
19332 /* Consume the opening brace. */
19333 cp_lexer_consume_token (parser->lexer);
19334 /* Skip the initializer. */
19335 cp_parser_skip_to_closing_brace (parser);
19336 /* Look for the trailing `}'. */
19337 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19339 return error_mark_node;
19342 return cp_parser_constant_expression (parser,
19343 /*allow_non_constant=*/false,
19344 NULL);
19347 /* Derived classes [gram.class.derived] */
19349 /* Parse a base-clause.
19351 base-clause:
19352 : base-specifier-list
19354 base-specifier-list:
19355 base-specifier ... [opt]
19356 base-specifier-list , base-specifier ... [opt]
19358 Returns a TREE_LIST representing the base-classes, in the order in
19359 which they were declared. The representation of each node is as
19360 described by cp_parser_base_specifier.
19362 In the case that no bases are specified, this function will return
19363 NULL_TREE, not ERROR_MARK_NODE. */
19365 static tree
19366 cp_parser_base_clause (cp_parser* parser)
19368 tree bases = NULL_TREE;
19370 /* Look for the `:' that begins the list. */
19371 cp_parser_require (parser, CPP_COLON, RT_COLON);
19373 /* Scan the base-specifier-list. */
19374 while (true)
19376 cp_token *token;
19377 tree base;
19378 bool pack_expansion_p = false;
19380 /* Look for the base-specifier. */
19381 base = cp_parser_base_specifier (parser);
19382 /* Look for the (optional) ellipsis. */
19383 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19385 /* Consume the `...'. */
19386 cp_lexer_consume_token (parser->lexer);
19388 pack_expansion_p = true;
19391 /* Add BASE to the front of the list. */
19392 if (base && base != error_mark_node)
19394 if (pack_expansion_p)
19395 /* Make this a pack expansion type. */
19396 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19398 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19400 TREE_CHAIN (base) = bases;
19401 bases = base;
19404 /* Peek at the next token. */
19405 token = cp_lexer_peek_token (parser->lexer);
19406 /* If it's not a comma, then the list is complete. */
19407 if (token->type != CPP_COMMA)
19408 break;
19409 /* Consume the `,'. */
19410 cp_lexer_consume_token (parser->lexer);
19413 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19414 base class had a qualified name. However, the next name that
19415 appears is certainly not qualified. */
19416 parser->scope = NULL_TREE;
19417 parser->qualifying_scope = NULL_TREE;
19418 parser->object_scope = NULL_TREE;
19420 return nreverse (bases);
19423 /* Parse a base-specifier.
19425 base-specifier:
19426 :: [opt] nested-name-specifier [opt] class-name
19427 virtual access-specifier [opt] :: [opt] nested-name-specifier
19428 [opt] class-name
19429 access-specifier virtual [opt] :: [opt] nested-name-specifier
19430 [opt] class-name
19432 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19433 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19434 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19435 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19437 static tree
19438 cp_parser_base_specifier (cp_parser* parser)
19440 cp_token *token;
19441 bool done = false;
19442 bool virtual_p = false;
19443 bool duplicate_virtual_error_issued_p = false;
19444 bool duplicate_access_error_issued_p = false;
19445 bool class_scope_p, template_p;
19446 tree access = access_default_node;
19447 tree type;
19449 /* Process the optional `virtual' and `access-specifier'. */
19450 while (!done)
19452 /* Peek at the next token. */
19453 token = cp_lexer_peek_token (parser->lexer);
19454 /* Process `virtual'. */
19455 switch (token->keyword)
19457 case RID_VIRTUAL:
19458 /* If `virtual' appears more than once, issue an error. */
19459 if (virtual_p && !duplicate_virtual_error_issued_p)
19461 cp_parser_error (parser,
19462 "%<virtual%> specified more than once in base-specified");
19463 duplicate_virtual_error_issued_p = true;
19466 virtual_p = true;
19468 /* Consume the `virtual' token. */
19469 cp_lexer_consume_token (parser->lexer);
19471 break;
19473 case RID_PUBLIC:
19474 case RID_PROTECTED:
19475 case RID_PRIVATE:
19476 /* If more than one access specifier appears, issue an
19477 error. */
19478 if (access != access_default_node
19479 && !duplicate_access_error_issued_p)
19481 cp_parser_error (parser,
19482 "more than one access specifier in base-specified");
19483 duplicate_access_error_issued_p = true;
19486 access = ridpointers[(int) token->keyword];
19488 /* Consume the access-specifier. */
19489 cp_lexer_consume_token (parser->lexer);
19491 break;
19493 default:
19494 done = true;
19495 break;
19498 /* It is not uncommon to see programs mechanically, erroneously, use
19499 the 'typename' keyword to denote (dependent) qualified types
19500 as base classes. */
19501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19503 token = cp_lexer_peek_token (parser->lexer);
19504 if (!processing_template_decl)
19505 error_at (token->location,
19506 "keyword %<typename%> not allowed outside of templates");
19507 else
19508 error_at (token->location,
19509 "keyword %<typename%> not allowed in this context "
19510 "(the base class is implicitly a type)");
19511 cp_lexer_consume_token (parser->lexer);
19514 /* Look for the optional `::' operator. */
19515 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19516 /* Look for the nested-name-specifier. The simplest way to
19517 implement:
19519 [temp.res]
19521 The keyword `typename' is not permitted in a base-specifier or
19522 mem-initializer; in these contexts a qualified name that
19523 depends on a template-parameter is implicitly assumed to be a
19524 type name.
19526 is to pretend that we have seen the `typename' keyword at this
19527 point. */
19528 cp_parser_nested_name_specifier_opt (parser,
19529 /*typename_keyword_p=*/true,
19530 /*check_dependency_p=*/true,
19531 typename_type,
19532 /*is_declaration=*/true);
19533 /* If the base class is given by a qualified name, assume that names
19534 we see are type names or templates, as appropriate. */
19535 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19536 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19538 if (!parser->scope
19539 && cp_lexer_next_token_is_decltype (parser->lexer))
19540 /* DR 950 allows decltype as a base-specifier. */
19541 type = cp_parser_decltype (parser);
19542 else
19544 /* Otherwise, look for the class-name. */
19545 type = cp_parser_class_name (parser,
19546 class_scope_p,
19547 template_p,
19548 typename_type,
19549 /*check_dependency_p=*/true,
19550 /*class_head_p=*/false,
19551 /*is_declaration=*/true);
19552 type = TREE_TYPE (type);
19555 if (type == error_mark_node)
19556 return error_mark_node;
19558 return finish_base_specifier (type, access, virtual_p);
19561 /* Exception handling [gram.exception] */
19563 /* Parse an (optional) noexcept-specification.
19565 noexcept-specification:
19566 noexcept ( constant-expression ) [opt]
19568 If no noexcept-specification is present, returns NULL_TREE.
19569 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19570 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19571 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19572 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19573 in which case a boolean condition is returned instead. */
19575 static tree
19576 cp_parser_noexcept_specification_opt (cp_parser* parser,
19577 bool require_constexpr,
19578 bool* consumed_expr,
19579 bool return_cond)
19581 cp_token *token;
19582 const char *saved_message;
19584 /* Peek at the next token. */
19585 token = cp_lexer_peek_token (parser->lexer);
19587 /* Is it a noexcept-specification? */
19588 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19590 tree expr;
19591 cp_lexer_consume_token (parser->lexer);
19593 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19595 cp_lexer_consume_token (parser->lexer);
19597 if (require_constexpr)
19599 /* Types may not be defined in an exception-specification. */
19600 saved_message = parser->type_definition_forbidden_message;
19601 parser->type_definition_forbidden_message
19602 = G_("types may not be defined in an exception-specification");
19604 expr = cp_parser_constant_expression (parser, false, NULL);
19606 /* Restore the saved message. */
19607 parser->type_definition_forbidden_message = saved_message;
19609 else
19611 expr = cp_parser_expression (parser, false, NULL);
19612 *consumed_expr = true;
19615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19617 else
19619 expr = boolean_true_node;
19620 if (!require_constexpr)
19621 *consumed_expr = false;
19624 /* We cannot build a noexcept-spec right away because this will check
19625 that expr is a constexpr. */
19626 if (!return_cond)
19627 return build_noexcept_spec (expr, tf_warning_or_error);
19628 else
19629 return expr;
19631 else
19632 return NULL_TREE;
19635 /* Parse an (optional) exception-specification.
19637 exception-specification:
19638 throw ( type-id-list [opt] )
19640 Returns a TREE_LIST representing the exception-specification. The
19641 TREE_VALUE of each node is a type. */
19643 static tree
19644 cp_parser_exception_specification_opt (cp_parser* parser)
19646 cp_token *token;
19647 tree type_id_list;
19648 const char *saved_message;
19650 /* Peek at the next token. */
19651 token = cp_lexer_peek_token (parser->lexer);
19653 /* Is it a noexcept-specification? */
19654 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19655 false);
19656 if (type_id_list != NULL_TREE)
19657 return type_id_list;
19659 /* If it's not `throw', then there's no exception-specification. */
19660 if (!cp_parser_is_keyword (token, RID_THROW))
19661 return NULL_TREE;
19663 #if 0
19664 /* Enable this once a lot of code has transitioned to noexcept? */
19665 if (cxx_dialect >= cxx0x && !in_system_header)
19666 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19667 "deprecated in C++0x; use %<noexcept%> instead");
19668 #endif
19670 /* Consume the `throw'. */
19671 cp_lexer_consume_token (parser->lexer);
19673 /* Look for the `('. */
19674 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19676 /* Peek at the next token. */
19677 token = cp_lexer_peek_token (parser->lexer);
19678 /* If it's not a `)', then there is a type-id-list. */
19679 if (token->type != CPP_CLOSE_PAREN)
19681 /* Types may not be defined in an exception-specification. */
19682 saved_message = parser->type_definition_forbidden_message;
19683 parser->type_definition_forbidden_message
19684 = G_("types may not be defined in an exception-specification");
19685 /* Parse the type-id-list. */
19686 type_id_list = cp_parser_type_id_list (parser);
19687 /* Restore the saved message. */
19688 parser->type_definition_forbidden_message = saved_message;
19690 else
19691 type_id_list = empty_except_spec;
19693 /* Look for the `)'. */
19694 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19696 return type_id_list;
19699 /* Parse an (optional) type-id-list.
19701 type-id-list:
19702 type-id ... [opt]
19703 type-id-list , type-id ... [opt]
19705 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19706 in the order that the types were presented. */
19708 static tree
19709 cp_parser_type_id_list (cp_parser* parser)
19711 tree types = NULL_TREE;
19713 while (true)
19715 cp_token *token;
19716 tree type;
19718 /* Get the next type-id. */
19719 type = cp_parser_type_id (parser);
19720 /* Parse the optional ellipsis. */
19721 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19723 /* Consume the `...'. */
19724 cp_lexer_consume_token (parser->lexer);
19726 /* Turn the type into a pack expansion expression. */
19727 type = make_pack_expansion (type);
19729 /* Add it to the list. */
19730 types = add_exception_specifier (types, type, /*complain=*/1);
19731 /* Peek at the next token. */
19732 token = cp_lexer_peek_token (parser->lexer);
19733 /* If it is not a `,', we are done. */
19734 if (token->type != CPP_COMMA)
19735 break;
19736 /* Consume the `,'. */
19737 cp_lexer_consume_token (parser->lexer);
19740 return nreverse (types);
19743 /* Parse a try-block.
19745 try-block:
19746 try compound-statement handler-seq */
19748 static tree
19749 cp_parser_try_block (cp_parser* parser)
19751 tree try_block;
19753 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19754 try_block = begin_try_block ();
19755 cp_parser_compound_statement (parser, NULL, true, false);
19756 finish_try_block (try_block);
19757 cp_parser_handler_seq (parser);
19758 finish_handler_sequence (try_block);
19760 return try_block;
19763 /* Parse a function-try-block.
19765 function-try-block:
19766 try ctor-initializer [opt] function-body handler-seq */
19768 static bool
19769 cp_parser_function_try_block (cp_parser* parser)
19771 tree compound_stmt;
19772 tree try_block;
19773 bool ctor_initializer_p;
19775 /* Look for the `try' keyword. */
19776 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19777 return false;
19778 /* Let the rest of the front end know where we are. */
19779 try_block = begin_function_try_block (&compound_stmt);
19780 /* Parse the function-body. */
19781 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19782 (parser, /*in_function_try_block=*/true);
19783 /* We're done with the `try' part. */
19784 finish_function_try_block (try_block);
19785 /* Parse the handlers. */
19786 cp_parser_handler_seq (parser);
19787 /* We're done with the handlers. */
19788 finish_function_handler_sequence (try_block, compound_stmt);
19790 return ctor_initializer_p;
19793 /* Parse a handler-seq.
19795 handler-seq:
19796 handler handler-seq [opt] */
19798 static void
19799 cp_parser_handler_seq (cp_parser* parser)
19801 while (true)
19803 cp_token *token;
19805 /* Parse the handler. */
19806 cp_parser_handler (parser);
19807 /* Peek at the next token. */
19808 token = cp_lexer_peek_token (parser->lexer);
19809 /* If it's not `catch' then there are no more handlers. */
19810 if (!cp_parser_is_keyword (token, RID_CATCH))
19811 break;
19815 /* Parse a handler.
19817 handler:
19818 catch ( exception-declaration ) compound-statement */
19820 static void
19821 cp_parser_handler (cp_parser* parser)
19823 tree handler;
19824 tree declaration;
19826 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19827 handler = begin_handler ();
19828 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19829 declaration = cp_parser_exception_declaration (parser);
19830 finish_handler_parms (declaration, handler);
19831 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19832 cp_parser_compound_statement (parser, NULL, false, false);
19833 finish_handler (handler);
19836 /* Parse an exception-declaration.
19838 exception-declaration:
19839 type-specifier-seq declarator
19840 type-specifier-seq abstract-declarator
19841 type-specifier-seq
19844 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19845 ellipsis variant is used. */
19847 static tree
19848 cp_parser_exception_declaration (cp_parser* parser)
19850 cp_decl_specifier_seq type_specifiers;
19851 cp_declarator *declarator;
19852 const char *saved_message;
19854 /* If it's an ellipsis, it's easy to handle. */
19855 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19857 /* Consume the `...' token. */
19858 cp_lexer_consume_token (parser->lexer);
19859 return NULL_TREE;
19862 /* Types may not be defined in exception-declarations. */
19863 saved_message = parser->type_definition_forbidden_message;
19864 parser->type_definition_forbidden_message
19865 = G_("types may not be defined in exception-declarations");
19867 /* Parse the type-specifier-seq. */
19868 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19869 /*is_trailing_return=*/false,
19870 &type_specifiers);
19871 /* If it's a `)', then there is no declarator. */
19872 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19873 declarator = NULL;
19874 else
19875 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19876 /*ctor_dtor_or_conv_p=*/NULL,
19877 /*parenthesized_p=*/NULL,
19878 /*member_p=*/false);
19880 /* Restore the saved message. */
19881 parser->type_definition_forbidden_message = saved_message;
19883 if (!type_specifiers.any_specifiers_p)
19884 return error_mark_node;
19886 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19889 /* Parse a throw-expression.
19891 throw-expression:
19892 throw assignment-expression [opt]
19894 Returns a THROW_EXPR representing the throw-expression. */
19896 static tree
19897 cp_parser_throw_expression (cp_parser* parser)
19899 tree expression;
19900 cp_token* token;
19902 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19903 token = cp_lexer_peek_token (parser->lexer);
19904 /* Figure out whether or not there is an assignment-expression
19905 following the "throw" keyword. */
19906 if (token->type == CPP_COMMA
19907 || token->type == CPP_SEMICOLON
19908 || token->type == CPP_CLOSE_PAREN
19909 || token->type == CPP_CLOSE_SQUARE
19910 || token->type == CPP_CLOSE_BRACE
19911 || token->type == CPP_COLON)
19912 expression = NULL_TREE;
19913 else
19914 expression = cp_parser_assignment_expression (parser,
19915 /*cast_p=*/false, NULL);
19917 return build_throw (expression);
19920 /* GNU Extensions */
19922 /* Parse an (optional) asm-specification.
19924 asm-specification:
19925 asm ( string-literal )
19927 If the asm-specification is present, returns a STRING_CST
19928 corresponding to the string-literal. Otherwise, returns
19929 NULL_TREE. */
19931 static tree
19932 cp_parser_asm_specification_opt (cp_parser* parser)
19934 cp_token *token;
19935 tree asm_specification;
19937 /* Peek at the next token. */
19938 token = cp_lexer_peek_token (parser->lexer);
19939 /* If the next token isn't the `asm' keyword, then there's no
19940 asm-specification. */
19941 if (!cp_parser_is_keyword (token, RID_ASM))
19942 return NULL_TREE;
19944 /* Consume the `asm' token. */
19945 cp_lexer_consume_token (parser->lexer);
19946 /* Look for the `('. */
19947 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19949 /* Look for the string-literal. */
19950 asm_specification = cp_parser_string_literal (parser, false, false);
19952 /* Look for the `)'. */
19953 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19955 return asm_specification;
19958 /* Parse an asm-operand-list.
19960 asm-operand-list:
19961 asm-operand
19962 asm-operand-list , asm-operand
19964 asm-operand:
19965 string-literal ( expression )
19966 [ string-literal ] string-literal ( expression )
19968 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19969 each node is the expression. The TREE_PURPOSE is itself a
19970 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19971 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19972 is a STRING_CST for the string literal before the parenthesis. Returns
19973 ERROR_MARK_NODE if any of the operands are invalid. */
19975 static tree
19976 cp_parser_asm_operand_list (cp_parser* parser)
19978 tree asm_operands = NULL_TREE;
19979 bool invalid_operands = false;
19981 while (true)
19983 tree string_literal;
19984 tree expression;
19985 tree name;
19987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19989 /* Consume the `[' token. */
19990 cp_lexer_consume_token (parser->lexer);
19991 /* Read the operand name. */
19992 name = cp_parser_identifier (parser);
19993 if (name != error_mark_node)
19994 name = build_string (IDENTIFIER_LENGTH (name),
19995 IDENTIFIER_POINTER (name));
19996 /* Look for the closing `]'. */
19997 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19999 else
20000 name = NULL_TREE;
20001 /* Look for the string-literal. */
20002 string_literal = cp_parser_string_literal (parser, false, false);
20004 /* Look for the `('. */
20005 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20006 /* Parse the expression. */
20007 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20008 /* Look for the `)'. */
20009 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20011 if (name == error_mark_node
20012 || string_literal == error_mark_node
20013 || expression == error_mark_node)
20014 invalid_operands = true;
20016 /* Add this operand to the list. */
20017 asm_operands = tree_cons (build_tree_list (name, string_literal),
20018 expression,
20019 asm_operands);
20020 /* If the next token is not a `,', there are no more
20021 operands. */
20022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20023 break;
20024 /* Consume the `,'. */
20025 cp_lexer_consume_token (parser->lexer);
20028 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20031 /* Parse an asm-clobber-list.
20033 asm-clobber-list:
20034 string-literal
20035 asm-clobber-list , string-literal
20037 Returns a TREE_LIST, indicating the clobbers in the order that they
20038 appeared. The TREE_VALUE of each node is a STRING_CST. */
20040 static tree
20041 cp_parser_asm_clobber_list (cp_parser* parser)
20043 tree clobbers = NULL_TREE;
20045 while (true)
20047 tree string_literal;
20049 /* Look for the string literal. */
20050 string_literal = cp_parser_string_literal (parser, false, false);
20051 /* Add it to the list. */
20052 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20053 /* If the next token is not a `,', then the list is
20054 complete. */
20055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20056 break;
20057 /* Consume the `,' token. */
20058 cp_lexer_consume_token (parser->lexer);
20061 return clobbers;
20064 /* Parse an asm-label-list.
20066 asm-label-list:
20067 identifier
20068 asm-label-list , identifier
20070 Returns a TREE_LIST, indicating the labels in the order that they
20071 appeared. The TREE_VALUE of each node is a label. */
20073 static tree
20074 cp_parser_asm_label_list (cp_parser* parser)
20076 tree labels = NULL_TREE;
20078 while (true)
20080 tree identifier, label, name;
20082 /* Look for the identifier. */
20083 identifier = cp_parser_identifier (parser);
20084 if (!error_operand_p (identifier))
20086 label = lookup_label (identifier);
20087 if (TREE_CODE (label) == LABEL_DECL)
20089 TREE_USED (label) = 1;
20090 check_goto (label);
20091 name = build_string (IDENTIFIER_LENGTH (identifier),
20092 IDENTIFIER_POINTER (identifier));
20093 labels = tree_cons (name, label, labels);
20096 /* If the next token is not a `,', then the list is
20097 complete. */
20098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20099 break;
20100 /* Consume the `,' token. */
20101 cp_lexer_consume_token (parser->lexer);
20104 return nreverse (labels);
20107 /* Parse an (optional) series of attributes.
20109 attributes:
20110 attributes attribute
20112 attribute:
20113 __attribute__ (( attribute-list [opt] ))
20115 The return value is as for cp_parser_attribute_list. */
20117 static tree
20118 cp_parser_attributes_opt (cp_parser* parser)
20120 tree attributes = NULL_TREE;
20122 while (true)
20124 cp_token *token;
20125 tree attribute_list;
20126 bool ok = true;
20128 /* Peek at the next token. */
20129 token = cp_lexer_peek_token (parser->lexer);
20130 /* If it's not `__attribute__', then we're done. */
20131 if (token->keyword != RID_ATTRIBUTE)
20132 break;
20134 /* Consume the `__attribute__' keyword. */
20135 cp_lexer_consume_token (parser->lexer);
20136 /* Look for the two `(' tokens. */
20137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20138 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20140 /* Peek at the next token. */
20141 token = cp_lexer_peek_token (parser->lexer);
20142 if (token->type != CPP_CLOSE_PAREN)
20143 /* Parse the attribute-list. */
20144 attribute_list = cp_parser_attribute_list (parser);
20145 else
20146 /* If the next token is a `)', then there is no attribute
20147 list. */
20148 attribute_list = NULL;
20150 /* Look for the two `)' tokens. */
20151 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20152 ok = false;
20153 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20154 ok = false;
20155 if (!ok)
20156 cp_parser_skip_to_end_of_statement (parser);
20158 /* Add these new attributes to the list. */
20159 attributes = chainon (attributes, attribute_list);
20162 return attributes;
20165 /* Parse an attribute-list.
20167 attribute-list:
20168 attribute
20169 attribute-list , attribute
20171 attribute:
20172 identifier
20173 identifier ( identifier )
20174 identifier ( identifier , expression-list )
20175 identifier ( expression-list )
20177 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20178 to an attribute. The TREE_PURPOSE of each node is the identifier
20179 indicating which attribute is in use. The TREE_VALUE represents
20180 the arguments, if any. */
20182 static tree
20183 cp_parser_attribute_list (cp_parser* parser)
20185 tree attribute_list = NULL_TREE;
20186 bool save_translate_strings_p = parser->translate_strings_p;
20188 parser->translate_strings_p = false;
20189 while (true)
20191 cp_token *token;
20192 tree identifier;
20193 tree attribute;
20195 /* Look for the identifier. We also allow keywords here; for
20196 example `__attribute__ ((const))' is legal. */
20197 token = cp_lexer_peek_token (parser->lexer);
20198 if (token->type == CPP_NAME
20199 || token->type == CPP_KEYWORD)
20201 tree arguments = NULL_TREE;
20203 /* Consume the token. */
20204 token = cp_lexer_consume_token (parser->lexer);
20206 /* Save away the identifier that indicates which attribute
20207 this is. */
20208 identifier = (token->type == CPP_KEYWORD)
20209 /* For keywords, use the canonical spelling, not the
20210 parsed identifier. */
20211 ? ridpointers[(int) token->keyword]
20212 : token->u.value;
20214 attribute = build_tree_list (identifier, NULL_TREE);
20216 /* Peek at the next token. */
20217 token = cp_lexer_peek_token (parser->lexer);
20218 /* If it's an `(', then parse the attribute arguments. */
20219 if (token->type == CPP_OPEN_PAREN)
20221 VEC(tree,gc) *vec;
20222 int attr_flag = (attribute_takes_identifier_p (identifier)
20223 ? id_attr : normal_attr);
20224 vec = cp_parser_parenthesized_expression_list
20225 (parser, attr_flag, /*cast_p=*/false,
20226 /*allow_expansion_p=*/false,
20227 /*non_constant_p=*/NULL);
20228 if (vec == NULL)
20229 arguments = error_mark_node;
20230 else
20232 arguments = build_tree_list_vec (vec);
20233 release_tree_vector (vec);
20235 /* Save the arguments away. */
20236 TREE_VALUE (attribute) = arguments;
20239 if (arguments != error_mark_node)
20241 /* Add this attribute to the list. */
20242 TREE_CHAIN (attribute) = attribute_list;
20243 attribute_list = attribute;
20246 token = cp_lexer_peek_token (parser->lexer);
20248 /* Now, look for more attributes. If the next token isn't a
20249 `,', we're done. */
20250 if (token->type != CPP_COMMA)
20251 break;
20253 /* Consume the comma and keep going. */
20254 cp_lexer_consume_token (parser->lexer);
20256 parser->translate_strings_p = save_translate_strings_p;
20258 /* We built up the list in reverse order. */
20259 return nreverse (attribute_list);
20262 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20263 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20264 current value of the PEDANTIC flag, regardless of whether or not
20265 the `__extension__' keyword is present. The caller is responsible
20266 for restoring the value of the PEDANTIC flag. */
20268 static bool
20269 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20271 /* Save the old value of the PEDANTIC flag. */
20272 *saved_pedantic = pedantic;
20274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20276 /* Consume the `__extension__' token. */
20277 cp_lexer_consume_token (parser->lexer);
20278 /* We're not being pedantic while the `__extension__' keyword is
20279 in effect. */
20280 pedantic = 0;
20282 return true;
20285 return false;
20288 /* Parse a label declaration.
20290 label-declaration:
20291 __label__ label-declarator-seq ;
20293 label-declarator-seq:
20294 identifier , label-declarator-seq
20295 identifier */
20297 static void
20298 cp_parser_label_declaration (cp_parser* parser)
20300 /* Look for the `__label__' keyword. */
20301 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20303 while (true)
20305 tree identifier;
20307 /* Look for an identifier. */
20308 identifier = cp_parser_identifier (parser);
20309 /* If we failed, stop. */
20310 if (identifier == error_mark_node)
20311 break;
20312 /* Declare it as a label. */
20313 finish_label_decl (identifier);
20314 /* If the next token is a `;', stop. */
20315 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20316 break;
20317 /* Look for the `,' separating the label declarations. */
20318 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20321 /* Look for the final `;'. */
20322 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20325 /* Support Functions */
20327 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20328 NAME should have one of the representations used for an
20329 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20330 is returned. If PARSER->SCOPE is a dependent type, then a
20331 SCOPE_REF is returned.
20333 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20334 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20335 was formed. Abstractly, such entities should not be passed to this
20336 function, because they do not need to be looked up, but it is
20337 simpler to check for this special case here, rather than at the
20338 call-sites.
20340 In cases not explicitly covered above, this function returns a
20341 DECL, OVERLOAD, or baselink representing the result of the lookup.
20342 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20343 is returned.
20345 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20346 (e.g., "struct") that was used. In that case bindings that do not
20347 refer to types are ignored.
20349 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20350 ignored.
20352 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20353 are ignored.
20355 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20356 types.
20358 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20359 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20360 NULL_TREE otherwise. */
20362 static tree
20363 cp_parser_lookup_name (cp_parser *parser, tree name,
20364 enum tag_types tag_type,
20365 bool is_template,
20366 bool is_namespace,
20367 bool check_dependency,
20368 tree *ambiguous_decls,
20369 location_t name_location)
20371 tree decl;
20372 tree object_type = parser->context->object_type;
20374 /* Assume that the lookup will be unambiguous. */
20375 if (ambiguous_decls)
20376 *ambiguous_decls = NULL_TREE;
20378 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20379 no longer valid. Note that if we are parsing tentatively, and
20380 the parse fails, OBJECT_TYPE will be automatically restored. */
20381 parser->context->object_type = NULL_TREE;
20383 if (name == error_mark_node)
20384 return error_mark_node;
20386 /* A template-id has already been resolved; there is no lookup to
20387 do. */
20388 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20389 return name;
20390 if (BASELINK_P (name))
20392 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20393 == TEMPLATE_ID_EXPR);
20394 return name;
20397 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20398 it should already have been checked to make sure that the name
20399 used matches the type being destroyed. */
20400 if (TREE_CODE (name) == BIT_NOT_EXPR)
20402 tree type;
20404 /* Figure out to which type this destructor applies. */
20405 if (parser->scope)
20406 type = parser->scope;
20407 else if (object_type)
20408 type = object_type;
20409 else
20410 type = current_class_type;
20411 /* If that's not a class type, there is no destructor. */
20412 if (!type || !CLASS_TYPE_P (type))
20413 return error_mark_node;
20414 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20415 lazily_declare_fn (sfk_destructor, type);
20416 if (!CLASSTYPE_DESTRUCTORS (type))
20417 return error_mark_node;
20418 /* If it was a class type, return the destructor. */
20419 return CLASSTYPE_DESTRUCTORS (type);
20422 /* By this point, the NAME should be an ordinary identifier. If
20423 the id-expression was a qualified name, the qualifying scope is
20424 stored in PARSER->SCOPE at this point. */
20425 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20427 /* Perform the lookup. */
20428 if (parser->scope)
20430 bool dependent_p;
20432 if (parser->scope == error_mark_node)
20433 return error_mark_node;
20435 /* If the SCOPE is dependent, the lookup must be deferred until
20436 the template is instantiated -- unless we are explicitly
20437 looking up names in uninstantiated templates. Even then, we
20438 cannot look up the name if the scope is not a class type; it
20439 might, for example, be a template type parameter. */
20440 dependent_p = (TYPE_P (parser->scope)
20441 && dependent_scope_p (parser->scope));
20442 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20443 && dependent_p)
20444 /* Defer lookup. */
20445 decl = error_mark_node;
20446 else
20448 tree pushed_scope = NULL_TREE;
20450 /* If PARSER->SCOPE is a dependent type, then it must be a
20451 class type, and we must not be checking dependencies;
20452 otherwise, we would have processed this lookup above. So
20453 that PARSER->SCOPE is not considered a dependent base by
20454 lookup_member, we must enter the scope here. */
20455 if (dependent_p)
20456 pushed_scope = push_scope (parser->scope);
20458 /* If the PARSER->SCOPE is a template specialization, it
20459 may be instantiated during name lookup. In that case,
20460 errors may be issued. Even if we rollback the current
20461 tentative parse, those errors are valid. */
20462 decl = lookup_qualified_name (parser->scope, name,
20463 tag_type != none_type,
20464 /*complain=*/true);
20466 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20467 lookup result and the nested-name-specifier nominates a class C:
20468 * if the name specified after the nested-name-specifier, when
20469 looked up in C, is the injected-class-name of C (Clause 9), or
20470 * if the name specified after the nested-name-specifier is the
20471 same as the identifier or the simple-template-id's template-
20472 name in the last component of the nested-name-specifier,
20473 the name is instead considered to name the constructor of
20474 class C. [ Note: for example, the constructor is not an
20475 acceptable lookup result in an elaborated-type-specifier so
20476 the constructor would not be used in place of the
20477 injected-class-name. --end note ] Such a constructor name
20478 shall be used only in the declarator-id of a declaration that
20479 names a constructor or in a using-declaration. */
20480 if (tag_type == none_type
20481 && DECL_SELF_REFERENCE_P (decl)
20482 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20483 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20484 tag_type != none_type,
20485 /*complain=*/true);
20487 /* If we have a single function from a using decl, pull it out. */
20488 if (TREE_CODE (decl) == OVERLOAD
20489 && !really_overloaded_fn (decl))
20490 decl = OVL_FUNCTION (decl);
20492 if (pushed_scope)
20493 pop_scope (pushed_scope);
20496 /* If the scope is a dependent type and either we deferred lookup or
20497 we did lookup but didn't find the name, rememeber the name. */
20498 if (decl == error_mark_node && TYPE_P (parser->scope)
20499 && dependent_type_p (parser->scope))
20501 if (tag_type)
20503 tree type;
20505 /* The resolution to Core Issue 180 says that `struct
20506 A::B' should be considered a type-name, even if `A'
20507 is dependent. */
20508 type = make_typename_type (parser->scope, name, tag_type,
20509 /*complain=*/tf_error);
20510 decl = TYPE_NAME (type);
20512 else if (is_template
20513 && (cp_parser_next_token_ends_template_argument_p (parser)
20514 || cp_lexer_next_token_is (parser->lexer,
20515 CPP_CLOSE_PAREN)))
20516 decl = make_unbound_class_template (parser->scope,
20517 name, NULL_TREE,
20518 /*complain=*/tf_error);
20519 else
20520 decl = build_qualified_name (/*type=*/NULL_TREE,
20521 parser->scope, name,
20522 is_template);
20524 parser->qualifying_scope = parser->scope;
20525 parser->object_scope = NULL_TREE;
20527 else if (object_type)
20529 tree object_decl = NULL_TREE;
20530 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20531 OBJECT_TYPE is not a class. */
20532 if (CLASS_TYPE_P (object_type))
20533 /* If the OBJECT_TYPE is a template specialization, it may
20534 be instantiated during name lookup. In that case, errors
20535 may be issued. Even if we rollback the current tentative
20536 parse, those errors are valid. */
20537 object_decl = lookup_member (object_type,
20538 name,
20539 /*protect=*/0,
20540 tag_type != none_type,
20541 tf_warning_or_error);
20542 /* Look it up in the enclosing context, too. */
20543 decl = lookup_name_real (name, tag_type != none_type,
20544 /*nonclass=*/0,
20545 /*block_p=*/true, is_namespace, 0);
20546 parser->object_scope = object_type;
20547 parser->qualifying_scope = NULL_TREE;
20548 if (object_decl)
20549 decl = object_decl;
20551 else
20553 decl = lookup_name_real (name, tag_type != none_type,
20554 /*nonclass=*/0,
20555 /*block_p=*/true, is_namespace, 0);
20556 parser->qualifying_scope = NULL_TREE;
20557 parser->object_scope = NULL_TREE;
20560 /* If the lookup failed, let our caller know. */
20561 if (!decl || decl == error_mark_node)
20562 return error_mark_node;
20564 /* Pull out the template from an injected-class-name (or multiple). */
20565 if (is_template)
20566 decl = maybe_get_template_decl_from_type_decl (decl);
20568 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20569 if (TREE_CODE (decl) == TREE_LIST)
20571 if (ambiguous_decls)
20572 *ambiguous_decls = decl;
20573 /* The error message we have to print is too complicated for
20574 cp_parser_error, so we incorporate its actions directly. */
20575 if (!cp_parser_simulate_error (parser))
20577 error_at (name_location, "reference to %qD is ambiguous",
20578 name);
20579 print_candidates (decl);
20581 return error_mark_node;
20584 gcc_assert (DECL_P (decl)
20585 || TREE_CODE (decl) == OVERLOAD
20586 || TREE_CODE (decl) == SCOPE_REF
20587 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20588 || BASELINK_P (decl));
20590 /* If we have resolved the name of a member declaration, check to
20591 see if the declaration is accessible. When the name resolves to
20592 set of overloaded functions, accessibility is checked when
20593 overload resolution is done.
20595 During an explicit instantiation, access is not checked at all,
20596 as per [temp.explicit]. */
20597 if (DECL_P (decl))
20598 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20600 maybe_record_typedef_use (decl);
20602 return decl;
20605 /* Like cp_parser_lookup_name, but for use in the typical case where
20606 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20607 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20609 static tree
20610 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20612 return cp_parser_lookup_name (parser, name,
20613 none_type,
20614 /*is_template=*/false,
20615 /*is_namespace=*/false,
20616 /*check_dependency=*/true,
20617 /*ambiguous_decls=*/NULL,
20618 location);
20621 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20622 the current context, return the TYPE_DECL. If TAG_NAME_P is
20623 true, the DECL indicates the class being defined in a class-head,
20624 or declared in an elaborated-type-specifier.
20626 Otherwise, return DECL. */
20628 static tree
20629 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20631 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20632 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20634 struct A {
20635 template <typename T> struct B;
20638 template <typename T> struct A::B {};
20640 Similarly, in an elaborated-type-specifier:
20642 namespace N { struct X{}; }
20644 struct A {
20645 template <typename T> friend struct N::X;
20648 However, if the DECL refers to a class type, and we are in
20649 the scope of the class, then the name lookup automatically
20650 finds the TYPE_DECL created by build_self_reference rather
20651 than a TEMPLATE_DECL. For example, in:
20653 template <class T> struct S {
20654 S s;
20657 there is no need to handle such case. */
20659 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20660 return DECL_TEMPLATE_RESULT (decl);
20662 return decl;
20665 /* If too many, or too few, template-parameter lists apply to the
20666 declarator, issue an error message. Returns TRUE if all went well,
20667 and FALSE otherwise. */
20669 static bool
20670 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20671 cp_declarator *declarator,
20672 location_t declarator_location)
20674 unsigned num_templates;
20676 /* We haven't seen any classes that involve template parameters yet. */
20677 num_templates = 0;
20679 switch (declarator->kind)
20681 case cdk_id:
20682 if (declarator->u.id.qualifying_scope)
20684 tree scope;
20686 scope = declarator->u.id.qualifying_scope;
20688 while (scope && CLASS_TYPE_P (scope))
20690 /* You're supposed to have one `template <...>'
20691 for every template class, but you don't need one
20692 for a full specialization. For example:
20694 template <class T> struct S{};
20695 template <> struct S<int> { void f(); };
20696 void S<int>::f () {}
20698 is correct; there shouldn't be a `template <>' for
20699 the definition of `S<int>::f'. */
20700 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20701 /* If SCOPE does not have template information of any
20702 kind, then it is not a template, nor is it nested
20703 within a template. */
20704 break;
20705 if (explicit_class_specialization_p (scope))
20706 break;
20707 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20708 ++num_templates;
20710 scope = TYPE_CONTEXT (scope);
20713 else if (TREE_CODE (declarator->u.id.unqualified_name)
20714 == TEMPLATE_ID_EXPR)
20715 /* If the DECLARATOR has the form `X<y>' then it uses one
20716 additional level of template parameters. */
20717 ++num_templates;
20719 return cp_parser_check_template_parameters
20720 (parser, num_templates, declarator_location, declarator);
20723 case cdk_function:
20724 case cdk_array:
20725 case cdk_pointer:
20726 case cdk_reference:
20727 case cdk_ptrmem:
20728 return (cp_parser_check_declarator_template_parameters
20729 (parser, declarator->declarator, declarator_location));
20731 case cdk_error:
20732 return true;
20734 default:
20735 gcc_unreachable ();
20737 return false;
20740 /* NUM_TEMPLATES were used in the current declaration. If that is
20741 invalid, return FALSE and issue an error messages. Otherwise,
20742 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20743 declarator and we can print more accurate diagnostics. */
20745 static bool
20746 cp_parser_check_template_parameters (cp_parser* parser,
20747 unsigned num_templates,
20748 location_t location,
20749 cp_declarator *declarator)
20751 /* If there are the same number of template classes and parameter
20752 lists, that's OK. */
20753 if (parser->num_template_parameter_lists == num_templates)
20754 return true;
20755 /* If there are more, but only one more, then we are referring to a
20756 member template. That's OK too. */
20757 if (parser->num_template_parameter_lists == num_templates + 1)
20758 return true;
20759 /* If there are more template classes than parameter lists, we have
20760 something like:
20762 template <class T> void S<T>::R<T>::f (); */
20763 if (parser->num_template_parameter_lists < num_templates)
20765 if (declarator && !current_function_decl)
20766 error_at (location, "specializing member %<%T::%E%> "
20767 "requires %<template<>%> syntax",
20768 declarator->u.id.qualifying_scope,
20769 declarator->u.id.unqualified_name);
20770 else if (declarator)
20771 error_at (location, "invalid declaration of %<%T::%E%>",
20772 declarator->u.id.qualifying_scope,
20773 declarator->u.id.unqualified_name);
20774 else
20775 error_at (location, "too few template-parameter-lists");
20776 return false;
20778 /* Otherwise, there are too many template parameter lists. We have
20779 something like:
20781 template <class T> template <class U> void S::f(); */
20782 error_at (location, "too many template-parameter-lists");
20783 return false;
20786 /* Parse an optional `::' token indicating that the following name is
20787 from the global namespace. If so, PARSER->SCOPE is set to the
20788 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20789 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20790 Returns the new value of PARSER->SCOPE, if the `::' token is
20791 present, and NULL_TREE otherwise. */
20793 static tree
20794 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20796 cp_token *token;
20798 /* Peek at the next token. */
20799 token = cp_lexer_peek_token (parser->lexer);
20800 /* If we're looking at a `::' token then we're starting from the
20801 global namespace, not our current location. */
20802 if (token->type == CPP_SCOPE)
20804 /* Consume the `::' token. */
20805 cp_lexer_consume_token (parser->lexer);
20806 /* Set the SCOPE so that we know where to start the lookup. */
20807 parser->scope = global_namespace;
20808 parser->qualifying_scope = global_namespace;
20809 parser->object_scope = NULL_TREE;
20811 return parser->scope;
20813 else if (!current_scope_valid_p)
20815 parser->scope = NULL_TREE;
20816 parser->qualifying_scope = NULL_TREE;
20817 parser->object_scope = NULL_TREE;
20820 return NULL_TREE;
20823 /* Returns TRUE if the upcoming token sequence is the start of a
20824 constructor declarator. If FRIEND_P is true, the declarator is
20825 preceded by the `friend' specifier. */
20827 static bool
20828 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20830 bool constructor_p;
20831 tree nested_name_specifier;
20832 cp_token *next_token;
20834 /* The common case is that this is not a constructor declarator, so
20835 try to avoid doing lots of work if at all possible. It's not
20836 valid declare a constructor at function scope. */
20837 if (parser->in_function_body)
20838 return false;
20839 /* And only certain tokens can begin a constructor declarator. */
20840 next_token = cp_lexer_peek_token (parser->lexer);
20841 if (next_token->type != CPP_NAME
20842 && next_token->type != CPP_SCOPE
20843 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20844 && next_token->type != CPP_TEMPLATE_ID)
20845 return false;
20847 /* Parse tentatively; we are going to roll back all of the tokens
20848 consumed here. */
20849 cp_parser_parse_tentatively (parser);
20850 /* Assume that we are looking at a constructor declarator. */
20851 constructor_p = true;
20853 /* Look for the optional `::' operator. */
20854 cp_parser_global_scope_opt (parser,
20855 /*current_scope_valid_p=*/false);
20856 /* Look for the nested-name-specifier. */
20857 nested_name_specifier
20858 = (cp_parser_nested_name_specifier_opt (parser,
20859 /*typename_keyword_p=*/false,
20860 /*check_dependency_p=*/false,
20861 /*type_p=*/false,
20862 /*is_declaration=*/false));
20863 /* Outside of a class-specifier, there must be a
20864 nested-name-specifier. */
20865 if (!nested_name_specifier &&
20866 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20867 || friend_p))
20868 constructor_p = false;
20869 else if (nested_name_specifier == error_mark_node)
20870 constructor_p = false;
20872 /* If we have a class scope, this is easy; DR 147 says that S::S always
20873 names the constructor, and no other qualified name could. */
20874 if (constructor_p && nested_name_specifier
20875 && CLASS_TYPE_P (nested_name_specifier))
20877 tree id = cp_parser_unqualified_id (parser,
20878 /*template_keyword_p=*/false,
20879 /*check_dependency_p=*/false,
20880 /*declarator_p=*/true,
20881 /*optional_p=*/false);
20882 if (is_overloaded_fn (id))
20883 id = DECL_NAME (get_first_fn (id));
20884 if (!constructor_name_p (id, nested_name_specifier))
20885 constructor_p = false;
20887 /* If we still think that this might be a constructor-declarator,
20888 look for a class-name. */
20889 else if (constructor_p)
20891 /* If we have:
20893 template <typename T> struct S {
20894 S();
20897 we must recognize that the nested `S' names a class. */
20898 tree type_decl;
20899 type_decl = cp_parser_class_name (parser,
20900 /*typename_keyword_p=*/false,
20901 /*template_keyword_p=*/false,
20902 none_type,
20903 /*check_dependency_p=*/false,
20904 /*class_head_p=*/false,
20905 /*is_declaration=*/false);
20906 /* If there was no class-name, then this is not a constructor. */
20907 constructor_p = !cp_parser_error_occurred (parser);
20909 /* If we're still considering a constructor, we have to see a `(',
20910 to begin the parameter-declaration-clause, followed by either a
20911 `)', an `...', or a decl-specifier. We need to check for a
20912 type-specifier to avoid being fooled into thinking that:
20914 S (f) (int);
20916 is a constructor. (It is actually a function named `f' that
20917 takes one parameter (of type `int') and returns a value of type
20918 `S'. */
20919 if (constructor_p
20920 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20921 constructor_p = false;
20923 if (constructor_p
20924 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20925 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20926 /* A parameter declaration begins with a decl-specifier,
20927 which is either the "attribute" keyword, a storage class
20928 specifier, or (usually) a type-specifier. */
20929 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20931 tree type;
20932 tree pushed_scope = NULL_TREE;
20933 unsigned saved_num_template_parameter_lists;
20935 /* Names appearing in the type-specifier should be looked up
20936 in the scope of the class. */
20937 if (current_class_type)
20938 type = NULL_TREE;
20939 else
20941 type = TREE_TYPE (type_decl);
20942 if (TREE_CODE (type) == TYPENAME_TYPE)
20944 type = resolve_typename_type (type,
20945 /*only_current_p=*/false);
20946 if (TREE_CODE (type) == TYPENAME_TYPE)
20948 cp_parser_abort_tentative_parse (parser);
20949 return false;
20952 pushed_scope = push_scope (type);
20955 /* Inside the constructor parameter list, surrounding
20956 template-parameter-lists do not apply. */
20957 saved_num_template_parameter_lists
20958 = parser->num_template_parameter_lists;
20959 parser->num_template_parameter_lists = 0;
20961 /* Look for the type-specifier. */
20962 cp_parser_type_specifier (parser,
20963 CP_PARSER_FLAGS_NONE,
20964 /*decl_specs=*/NULL,
20965 /*is_declarator=*/true,
20966 /*declares_class_or_enum=*/NULL,
20967 /*is_cv_qualifier=*/NULL);
20969 parser->num_template_parameter_lists
20970 = saved_num_template_parameter_lists;
20972 /* Leave the scope of the class. */
20973 if (pushed_scope)
20974 pop_scope (pushed_scope);
20976 constructor_p = !cp_parser_error_occurred (parser);
20980 /* We did not really want to consume any tokens. */
20981 cp_parser_abort_tentative_parse (parser);
20983 return constructor_p;
20986 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20987 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20988 they must be performed once we are in the scope of the function.
20990 Returns the function defined. */
20992 static tree
20993 cp_parser_function_definition_from_specifiers_and_declarator
20994 (cp_parser* parser,
20995 cp_decl_specifier_seq *decl_specifiers,
20996 tree attributes,
20997 const cp_declarator *declarator)
20999 tree fn;
21000 bool success_p;
21002 /* Begin the function-definition. */
21003 success_p = start_function (decl_specifiers, declarator, attributes);
21005 /* The things we're about to see are not directly qualified by any
21006 template headers we've seen thus far. */
21007 reset_specialization ();
21009 /* If there were names looked up in the decl-specifier-seq that we
21010 did not check, check them now. We must wait until we are in the
21011 scope of the function to perform the checks, since the function
21012 might be a friend. */
21013 perform_deferred_access_checks (tf_warning_or_error);
21015 if (!success_p)
21017 /* Skip the entire function. */
21018 cp_parser_skip_to_end_of_block_or_statement (parser);
21019 fn = error_mark_node;
21021 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21023 /* Seen already, skip it. An error message has already been output. */
21024 cp_parser_skip_to_end_of_block_or_statement (parser);
21025 fn = current_function_decl;
21026 current_function_decl = NULL_TREE;
21027 /* If this is a function from a class, pop the nested class. */
21028 if (current_class_name)
21029 pop_nested_class ();
21031 else
21033 timevar_id_t tv;
21034 if (DECL_DECLARED_INLINE_P (current_function_decl))
21035 tv = TV_PARSE_INLINE;
21036 else
21037 tv = TV_PARSE_FUNC;
21038 timevar_push (tv);
21039 fn = cp_parser_function_definition_after_declarator (parser,
21040 /*inline_p=*/false);
21041 timevar_pop (tv);
21044 return fn;
21047 /* Parse the part of a function-definition that follows the
21048 declarator. INLINE_P is TRUE iff this function is an inline
21049 function defined within a class-specifier.
21051 Returns the function defined. */
21053 static tree
21054 cp_parser_function_definition_after_declarator (cp_parser* parser,
21055 bool inline_p)
21057 tree fn;
21058 bool ctor_initializer_p = false;
21059 bool saved_in_unbraced_linkage_specification_p;
21060 bool saved_in_function_body;
21061 unsigned saved_num_template_parameter_lists;
21062 cp_token *token;
21064 saved_in_function_body = parser->in_function_body;
21065 parser->in_function_body = true;
21066 /* If the next token is `return', then the code may be trying to
21067 make use of the "named return value" extension that G++ used to
21068 support. */
21069 token = cp_lexer_peek_token (parser->lexer);
21070 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21072 /* Consume the `return' keyword. */
21073 cp_lexer_consume_token (parser->lexer);
21074 /* Look for the identifier that indicates what value is to be
21075 returned. */
21076 cp_parser_identifier (parser);
21077 /* Issue an error message. */
21078 error_at (token->location,
21079 "named return values are no longer supported");
21080 /* Skip tokens until we reach the start of the function body. */
21081 while (true)
21083 cp_token *token = cp_lexer_peek_token (parser->lexer);
21084 if (token->type == CPP_OPEN_BRACE
21085 || token->type == CPP_EOF
21086 || token->type == CPP_PRAGMA_EOL)
21087 break;
21088 cp_lexer_consume_token (parser->lexer);
21091 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21092 anything declared inside `f'. */
21093 saved_in_unbraced_linkage_specification_p
21094 = parser->in_unbraced_linkage_specification_p;
21095 parser->in_unbraced_linkage_specification_p = false;
21096 /* Inside the function, surrounding template-parameter-lists do not
21097 apply. */
21098 saved_num_template_parameter_lists
21099 = parser->num_template_parameter_lists;
21100 parser->num_template_parameter_lists = 0;
21102 start_lambda_scope (current_function_decl);
21104 /* If the next token is `try', `__transaction_atomic', or
21105 `__transaction_relaxed`, then we are looking at either function-try-block
21106 or function-transaction-block. Note that all of these include the
21107 function-body. */
21108 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21109 ctor_initializer_p = cp_parser_function_transaction (parser,
21110 RID_TRANSACTION_ATOMIC);
21111 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21112 RID_TRANSACTION_RELAXED))
21113 ctor_initializer_p = cp_parser_function_transaction (parser,
21114 RID_TRANSACTION_RELAXED);
21115 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21116 ctor_initializer_p = cp_parser_function_try_block (parser);
21117 else
21118 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21119 (parser, /*in_function_try_block=*/false);
21121 finish_lambda_scope ();
21123 /* Finish the function. */
21124 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21125 (inline_p ? 2 : 0));
21126 /* Generate code for it, if necessary. */
21127 expand_or_defer_fn (fn);
21128 /* Restore the saved values. */
21129 parser->in_unbraced_linkage_specification_p
21130 = saved_in_unbraced_linkage_specification_p;
21131 parser->num_template_parameter_lists
21132 = saved_num_template_parameter_lists;
21133 parser->in_function_body = saved_in_function_body;
21135 return fn;
21138 /* Parse a template-declaration, assuming that the `export' (and
21139 `extern') keywords, if present, has already been scanned. MEMBER_P
21140 is as for cp_parser_template_declaration. */
21142 static void
21143 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21145 tree decl = NULL_TREE;
21146 VEC (deferred_access_check,gc) *checks;
21147 tree parameter_list;
21148 bool friend_p = false;
21149 bool need_lang_pop;
21150 cp_token *token;
21152 /* Look for the `template' keyword. */
21153 token = cp_lexer_peek_token (parser->lexer);
21154 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21155 return;
21157 /* And the `<'. */
21158 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21159 return;
21160 if (at_class_scope_p () && current_function_decl)
21162 /* 14.5.2.2 [temp.mem]
21164 A local class shall not have member templates. */
21165 error_at (token->location,
21166 "invalid declaration of member template in local class");
21167 cp_parser_skip_to_end_of_block_or_statement (parser);
21168 return;
21170 /* [temp]
21172 A template ... shall not have C linkage. */
21173 if (current_lang_name == lang_name_c)
21175 error_at (token->location, "template with C linkage");
21176 /* Give it C++ linkage to avoid confusing other parts of the
21177 front end. */
21178 push_lang_context (lang_name_cplusplus);
21179 need_lang_pop = true;
21181 else
21182 need_lang_pop = false;
21184 /* We cannot perform access checks on the template parameter
21185 declarations until we know what is being declared, just as we
21186 cannot check the decl-specifier list. */
21187 push_deferring_access_checks (dk_deferred);
21189 /* If the next token is `>', then we have an invalid
21190 specialization. Rather than complain about an invalid template
21191 parameter, issue an error message here. */
21192 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21194 cp_parser_error (parser, "invalid explicit specialization");
21195 begin_specialization ();
21196 parameter_list = NULL_TREE;
21198 else
21200 /* Parse the template parameters. */
21201 parameter_list = cp_parser_template_parameter_list (parser);
21204 /* Get the deferred access checks from the parameter list. These
21205 will be checked once we know what is being declared, as for a
21206 member template the checks must be performed in the scope of the
21207 class containing the member. */
21208 checks = get_deferred_access_checks ();
21210 /* Look for the `>'. */
21211 cp_parser_skip_to_end_of_template_parameter_list (parser);
21212 /* We just processed one more parameter list. */
21213 ++parser->num_template_parameter_lists;
21214 /* If the next token is `template', there are more template
21215 parameters. */
21216 if (cp_lexer_next_token_is_keyword (parser->lexer,
21217 RID_TEMPLATE))
21218 cp_parser_template_declaration_after_export (parser, member_p);
21219 else if (cxx_dialect >= cxx0x
21220 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21221 decl = cp_parser_alias_declaration (parser);
21222 else
21224 /* There are no access checks when parsing a template, as we do not
21225 know if a specialization will be a friend. */
21226 push_deferring_access_checks (dk_no_check);
21227 token = cp_lexer_peek_token (parser->lexer);
21228 decl = cp_parser_single_declaration (parser,
21229 checks,
21230 member_p,
21231 /*explicit_specialization_p=*/false,
21232 &friend_p);
21233 pop_deferring_access_checks ();
21235 /* If this is a member template declaration, let the front
21236 end know. */
21237 if (member_p && !friend_p && decl)
21239 if (TREE_CODE (decl) == TYPE_DECL)
21240 cp_parser_check_access_in_redeclaration (decl, token->location);
21242 decl = finish_member_template_decl (decl);
21244 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21245 make_friend_class (current_class_type, TREE_TYPE (decl),
21246 /*complain=*/true);
21248 /* We are done with the current parameter list. */
21249 --parser->num_template_parameter_lists;
21251 pop_deferring_access_checks ();
21253 /* Finish up. */
21254 finish_template_decl (parameter_list);
21256 /* Check the template arguments for a literal operator template. */
21257 if (decl
21258 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21259 && UDLIT_OPER_P (DECL_NAME (decl)))
21261 bool ok = true;
21262 if (parameter_list == NULL_TREE)
21263 ok = false;
21264 else
21266 int num_parms = TREE_VEC_LENGTH (parameter_list);
21267 if (num_parms != 1)
21268 ok = false;
21269 else
21271 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21272 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21273 if (TREE_TYPE (parm) != char_type_node
21274 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21275 ok = false;
21278 if (!ok)
21279 error ("literal operator template %qD has invalid parameter list."
21280 " Expected non-type template argument pack <char...>",
21281 decl);
21283 /* Register member declarations. */
21284 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21285 finish_member_declaration (decl);
21286 /* For the erroneous case of a template with C linkage, we pushed an
21287 implicit C++ linkage scope; exit that scope now. */
21288 if (need_lang_pop)
21289 pop_lang_context ();
21290 /* If DECL is a function template, we must return to parse it later.
21291 (Even though there is no definition, there might be default
21292 arguments that need handling.) */
21293 if (member_p && decl
21294 && (TREE_CODE (decl) == FUNCTION_DECL
21295 || DECL_FUNCTION_TEMPLATE_P (decl)))
21296 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21299 /* Perform the deferred access checks from a template-parameter-list.
21300 CHECKS is a TREE_LIST of access checks, as returned by
21301 get_deferred_access_checks. */
21303 static void
21304 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21306 ++processing_template_parmlist;
21307 perform_access_checks (checks, tf_warning_or_error);
21308 --processing_template_parmlist;
21311 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21312 `function-definition' sequence. MEMBER_P is true, this declaration
21313 appears in a class scope.
21315 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21316 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21318 static tree
21319 cp_parser_single_declaration (cp_parser* parser,
21320 VEC (deferred_access_check,gc)* checks,
21321 bool member_p,
21322 bool explicit_specialization_p,
21323 bool* friend_p)
21325 int declares_class_or_enum;
21326 tree decl = NULL_TREE;
21327 cp_decl_specifier_seq decl_specifiers;
21328 bool function_definition_p = false;
21329 cp_token *decl_spec_token_start;
21331 /* This function is only used when processing a template
21332 declaration. */
21333 gcc_assert (innermost_scope_kind () == sk_template_parms
21334 || innermost_scope_kind () == sk_template_spec);
21336 /* Defer access checks until we know what is being declared. */
21337 push_deferring_access_checks (dk_deferred);
21339 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21340 alternative. */
21341 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21342 cp_parser_decl_specifier_seq (parser,
21343 CP_PARSER_FLAGS_OPTIONAL,
21344 &decl_specifiers,
21345 &declares_class_or_enum);
21346 if (friend_p)
21347 *friend_p = cp_parser_friend_p (&decl_specifiers);
21349 /* There are no template typedefs. */
21350 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21352 error_at (decl_spec_token_start->location,
21353 "template declaration of %<typedef%>");
21354 decl = error_mark_node;
21357 /* Gather up the access checks that occurred the
21358 decl-specifier-seq. */
21359 stop_deferring_access_checks ();
21361 /* Check for the declaration of a template class. */
21362 if (declares_class_or_enum)
21364 if (cp_parser_declares_only_class_p (parser))
21366 decl = shadow_tag (&decl_specifiers);
21368 /* In this case:
21370 struct C {
21371 friend template <typename T> struct A<T>::B;
21374 A<T>::B will be represented by a TYPENAME_TYPE, and
21375 therefore not recognized by shadow_tag. */
21376 if (friend_p && *friend_p
21377 && !decl
21378 && decl_specifiers.type
21379 && TYPE_P (decl_specifiers.type))
21380 decl = decl_specifiers.type;
21382 if (decl && decl != error_mark_node)
21383 decl = TYPE_NAME (decl);
21384 else
21385 decl = error_mark_node;
21387 /* Perform access checks for template parameters. */
21388 cp_parser_perform_template_parameter_access_checks (checks);
21392 /* Complain about missing 'typename' or other invalid type names. */
21393 if (!decl_specifiers.any_type_specifiers_p
21394 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21396 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21397 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21398 the rest of this declaration. */
21399 decl = error_mark_node;
21400 goto out;
21403 /* If it's not a template class, try for a template function. If
21404 the next token is a `;', then this declaration does not declare
21405 anything. But, if there were errors in the decl-specifiers, then
21406 the error might well have come from an attempted class-specifier.
21407 In that case, there's no need to warn about a missing declarator. */
21408 if (!decl
21409 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21410 || decl_specifiers.type != error_mark_node))
21412 decl = cp_parser_init_declarator (parser,
21413 &decl_specifiers,
21414 checks,
21415 /*function_definition_allowed_p=*/true,
21416 member_p,
21417 declares_class_or_enum,
21418 &function_definition_p,
21419 NULL);
21421 /* 7.1.1-1 [dcl.stc]
21423 A storage-class-specifier shall not be specified in an explicit
21424 specialization... */
21425 if (decl
21426 && explicit_specialization_p
21427 && decl_specifiers.storage_class != sc_none)
21429 error_at (decl_spec_token_start->location,
21430 "explicit template specialization cannot have a storage class");
21431 decl = error_mark_node;
21435 /* Look for a trailing `;' after the declaration. */
21436 if (!function_definition_p
21437 && (decl == error_mark_node
21438 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21439 cp_parser_skip_to_end_of_block_or_statement (parser);
21441 out:
21442 pop_deferring_access_checks ();
21444 /* Clear any current qualification; whatever comes next is the start
21445 of something new. */
21446 parser->scope = NULL_TREE;
21447 parser->qualifying_scope = NULL_TREE;
21448 parser->object_scope = NULL_TREE;
21450 return decl;
21453 /* Parse a cast-expression that is not the operand of a unary "&". */
21455 static tree
21456 cp_parser_simple_cast_expression (cp_parser *parser)
21458 return cp_parser_cast_expression (parser, /*address_p=*/false,
21459 /*cast_p=*/false, NULL);
21462 /* Parse a functional cast to TYPE. Returns an expression
21463 representing the cast. */
21465 static tree
21466 cp_parser_functional_cast (cp_parser* parser, tree type)
21468 VEC(tree,gc) *vec;
21469 tree expression_list;
21470 tree cast;
21471 bool nonconst_p;
21473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21475 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21476 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21477 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21478 if (TREE_CODE (type) == TYPE_DECL)
21479 type = TREE_TYPE (type);
21480 return finish_compound_literal (type, expression_list,
21481 tf_warning_or_error);
21485 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21486 /*cast_p=*/true,
21487 /*allow_expansion_p=*/true,
21488 /*non_constant_p=*/NULL);
21489 if (vec == NULL)
21490 expression_list = error_mark_node;
21491 else
21493 expression_list = build_tree_list_vec (vec);
21494 release_tree_vector (vec);
21497 cast = build_functional_cast (type, expression_list,
21498 tf_warning_or_error);
21499 /* [expr.const]/1: In an integral constant expression "only type
21500 conversions to integral or enumeration type can be used". */
21501 if (TREE_CODE (type) == TYPE_DECL)
21502 type = TREE_TYPE (type);
21503 if (cast != error_mark_node
21504 && !cast_valid_in_integral_constant_expression_p (type)
21505 && cp_parser_non_integral_constant_expression (parser,
21506 NIC_CONSTRUCTOR))
21507 return error_mark_node;
21508 return cast;
21511 /* Save the tokens that make up the body of a member function defined
21512 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21513 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21514 specifiers applied to the declaration. Returns the FUNCTION_DECL
21515 for the member function. */
21517 static tree
21518 cp_parser_save_member_function_body (cp_parser* parser,
21519 cp_decl_specifier_seq *decl_specifiers,
21520 cp_declarator *declarator,
21521 tree attributes)
21523 cp_token *first;
21524 cp_token *last;
21525 tree fn;
21527 /* Create the FUNCTION_DECL. */
21528 fn = grokmethod (decl_specifiers, declarator, attributes);
21529 /* If something went badly wrong, bail out now. */
21530 if (fn == error_mark_node)
21532 /* If there's a function-body, skip it. */
21533 if (cp_parser_token_starts_function_definition_p
21534 (cp_lexer_peek_token (parser->lexer)))
21535 cp_parser_skip_to_end_of_block_or_statement (parser);
21536 return error_mark_node;
21539 /* Remember it, if there default args to post process. */
21540 cp_parser_save_default_args (parser, fn);
21542 /* Save away the tokens that make up the body of the
21543 function. */
21544 first = parser->lexer->next_token;
21545 /* We can have braced-init-list mem-initializers before the fn body. */
21546 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21548 cp_lexer_consume_token (parser->lexer);
21549 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21550 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21552 /* cache_group will stop after an un-nested { } pair, too. */
21553 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21554 break;
21556 /* variadic mem-inits have ... after the ')'. */
21557 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21558 cp_lexer_consume_token (parser->lexer);
21561 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21562 /* Handle function try blocks. */
21563 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21564 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21565 last = parser->lexer->next_token;
21567 /* Save away the inline definition; we will process it when the
21568 class is complete. */
21569 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21570 DECL_PENDING_INLINE_P (fn) = 1;
21572 /* We need to know that this was defined in the class, so that
21573 friend templates are handled correctly. */
21574 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21576 /* Add FN to the queue of functions to be parsed later. */
21577 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21579 return fn;
21582 /* Save the tokens that make up the in-class initializer for a non-static
21583 data member. Returns a DEFAULT_ARG. */
21585 static tree
21586 cp_parser_save_nsdmi (cp_parser* parser)
21588 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21591 /* Parse a template-argument-list, as well as the trailing ">" (but
21592 not the opening "<"). See cp_parser_template_argument_list for the
21593 return value. */
21595 static tree
21596 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21598 tree arguments;
21599 tree saved_scope;
21600 tree saved_qualifying_scope;
21601 tree saved_object_scope;
21602 bool saved_greater_than_is_operator_p;
21603 int saved_unevaluated_operand;
21604 int saved_inhibit_evaluation_warnings;
21606 /* [temp.names]
21608 When parsing a template-id, the first non-nested `>' is taken as
21609 the end of the template-argument-list rather than a greater-than
21610 operator. */
21611 saved_greater_than_is_operator_p
21612 = parser->greater_than_is_operator_p;
21613 parser->greater_than_is_operator_p = false;
21614 /* Parsing the argument list may modify SCOPE, so we save it
21615 here. */
21616 saved_scope = parser->scope;
21617 saved_qualifying_scope = parser->qualifying_scope;
21618 saved_object_scope = parser->object_scope;
21619 /* We need to evaluate the template arguments, even though this
21620 template-id may be nested within a "sizeof". */
21621 saved_unevaluated_operand = cp_unevaluated_operand;
21622 cp_unevaluated_operand = 0;
21623 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21624 c_inhibit_evaluation_warnings = 0;
21625 /* Parse the template-argument-list itself. */
21626 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21627 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21628 arguments = NULL_TREE;
21629 else
21630 arguments = cp_parser_template_argument_list (parser);
21631 /* Look for the `>' that ends the template-argument-list. If we find
21632 a '>>' instead, it's probably just a typo. */
21633 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21635 if (cxx_dialect != cxx98)
21637 /* In C++0x, a `>>' in a template argument list or cast
21638 expression is considered to be two separate `>'
21639 tokens. So, change the current token to a `>', but don't
21640 consume it: it will be consumed later when the outer
21641 template argument list (or cast expression) is parsed.
21642 Note that this replacement of `>' for `>>' is necessary
21643 even if we are parsing tentatively: in the tentative
21644 case, after calling
21645 cp_parser_enclosed_template_argument_list we will always
21646 throw away all of the template arguments and the first
21647 closing `>', either because the template argument list
21648 was erroneous or because we are replacing those tokens
21649 with a CPP_TEMPLATE_ID token. The second `>' (which will
21650 not have been thrown away) is needed either to close an
21651 outer template argument list or to complete a new-style
21652 cast. */
21653 cp_token *token = cp_lexer_peek_token (parser->lexer);
21654 token->type = CPP_GREATER;
21656 else if (!saved_greater_than_is_operator_p)
21658 /* If we're in a nested template argument list, the '>>' has
21659 to be a typo for '> >'. We emit the error message, but we
21660 continue parsing and we push a '>' as next token, so that
21661 the argument list will be parsed correctly. Note that the
21662 global source location is still on the token before the
21663 '>>', so we need to say explicitly where we want it. */
21664 cp_token *token = cp_lexer_peek_token (parser->lexer);
21665 error_at (token->location, "%<>>%> should be %<> >%> "
21666 "within a nested template argument list");
21668 token->type = CPP_GREATER;
21670 else
21672 /* If this is not a nested template argument list, the '>>'
21673 is a typo for '>'. Emit an error message and continue.
21674 Same deal about the token location, but here we can get it
21675 right by consuming the '>>' before issuing the diagnostic. */
21676 cp_token *token = cp_lexer_consume_token (parser->lexer);
21677 error_at (token->location,
21678 "spurious %<>>%>, use %<>%> to terminate "
21679 "a template argument list");
21682 else
21683 cp_parser_skip_to_end_of_template_parameter_list (parser);
21684 /* The `>' token might be a greater-than operator again now. */
21685 parser->greater_than_is_operator_p
21686 = saved_greater_than_is_operator_p;
21687 /* Restore the SAVED_SCOPE. */
21688 parser->scope = saved_scope;
21689 parser->qualifying_scope = saved_qualifying_scope;
21690 parser->object_scope = saved_object_scope;
21691 cp_unevaluated_operand = saved_unevaluated_operand;
21692 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21694 return arguments;
21697 /* MEMBER_FUNCTION is a member function, or a friend. If default
21698 arguments, or the body of the function have not yet been parsed,
21699 parse them now. */
21701 static void
21702 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21704 timevar_push (TV_PARSE_INMETH);
21705 /* If this member is a template, get the underlying
21706 FUNCTION_DECL. */
21707 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21708 member_function = DECL_TEMPLATE_RESULT (member_function);
21710 /* There should not be any class definitions in progress at this
21711 point; the bodies of members are only parsed outside of all class
21712 definitions. */
21713 gcc_assert (parser->num_classes_being_defined == 0);
21714 /* While we're parsing the member functions we might encounter more
21715 classes. We want to handle them right away, but we don't want
21716 them getting mixed up with functions that are currently in the
21717 queue. */
21718 push_unparsed_function_queues (parser);
21720 /* Make sure that any template parameters are in scope. */
21721 maybe_begin_member_template_processing (member_function);
21723 /* If the body of the function has not yet been parsed, parse it
21724 now. */
21725 if (DECL_PENDING_INLINE_P (member_function))
21727 tree function_scope;
21728 cp_token_cache *tokens;
21730 /* The function is no longer pending; we are processing it. */
21731 tokens = DECL_PENDING_INLINE_INFO (member_function);
21732 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21733 DECL_PENDING_INLINE_P (member_function) = 0;
21735 /* If this is a local class, enter the scope of the containing
21736 function. */
21737 function_scope = current_function_decl;
21738 if (function_scope)
21739 push_function_context ();
21741 /* Push the body of the function onto the lexer stack. */
21742 cp_parser_push_lexer_for_tokens (parser, tokens);
21744 /* Let the front end know that we going to be defining this
21745 function. */
21746 start_preparsed_function (member_function, NULL_TREE,
21747 SF_PRE_PARSED | SF_INCLASS_INLINE);
21749 /* Don't do access checking if it is a templated function. */
21750 if (processing_template_decl)
21751 push_deferring_access_checks (dk_no_check);
21753 /* Now, parse the body of the function. */
21754 cp_parser_function_definition_after_declarator (parser,
21755 /*inline_p=*/true);
21757 if (processing_template_decl)
21758 pop_deferring_access_checks ();
21760 /* Leave the scope of the containing function. */
21761 if (function_scope)
21762 pop_function_context ();
21763 cp_parser_pop_lexer (parser);
21766 /* Remove any template parameters from the symbol table. */
21767 maybe_end_member_template_processing ();
21769 /* Restore the queue. */
21770 pop_unparsed_function_queues (parser);
21771 timevar_pop (TV_PARSE_INMETH);
21774 /* If DECL contains any default args, remember it on the unparsed
21775 functions queue. */
21777 static void
21778 cp_parser_save_default_args (cp_parser* parser, tree decl)
21780 tree probe;
21782 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21783 probe;
21784 probe = TREE_CHAIN (probe))
21785 if (TREE_PURPOSE (probe))
21787 cp_default_arg_entry *entry
21788 = VEC_safe_push (cp_default_arg_entry, gc,
21789 unparsed_funs_with_default_args, NULL);
21790 entry->class_type = current_class_type;
21791 entry->decl = decl;
21792 break;
21796 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21797 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21798 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21799 from the parameter-type-list. */
21801 static tree
21802 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21803 tree default_arg, tree parmtype)
21805 cp_token_cache *tokens;
21806 tree parsed_arg;
21807 bool dummy;
21809 if (default_arg == error_mark_node)
21810 return error_mark_node;
21812 /* Push the saved tokens for the default argument onto the parser's
21813 lexer stack. */
21814 tokens = DEFARG_TOKENS (default_arg);
21815 cp_parser_push_lexer_for_tokens (parser, tokens);
21817 start_lambda_scope (decl);
21819 /* Parse the default argument. */
21820 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21821 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21822 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21824 finish_lambda_scope ();
21826 if (parsed_arg == error_mark_node)
21827 cp_parser_skip_to_end_of_statement (parser);
21829 if (!processing_template_decl)
21831 /* In a non-template class, check conversions now. In a template,
21832 we'll wait and instantiate these as needed. */
21833 if (TREE_CODE (decl) == PARM_DECL)
21834 parsed_arg = check_default_argument (parmtype, parsed_arg);
21835 else
21837 int flags = LOOKUP_IMPLICIT;
21838 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21839 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21840 flags = LOOKUP_NORMAL;
21841 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21845 /* If the token stream has not been completely used up, then
21846 there was extra junk after the end of the default
21847 argument. */
21848 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21850 if (TREE_CODE (decl) == PARM_DECL)
21851 cp_parser_error (parser, "expected %<,%>");
21852 else
21853 cp_parser_error (parser, "expected %<;%>");
21856 /* Revert to the main lexer. */
21857 cp_parser_pop_lexer (parser);
21859 return parsed_arg;
21862 /* FIELD is a non-static data member with an initializer which we saved for
21863 later; parse it now. */
21865 static void
21866 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21868 tree def;
21870 push_unparsed_function_queues (parser);
21871 def = cp_parser_late_parse_one_default_arg (parser, field,
21872 DECL_INITIAL (field),
21873 NULL_TREE);
21874 pop_unparsed_function_queues (parser);
21876 DECL_INITIAL (field) = def;
21879 /* FN is a FUNCTION_DECL which may contains a parameter with an
21880 unparsed DEFAULT_ARG. Parse the default args now. This function
21881 assumes that the current scope is the scope in which the default
21882 argument should be processed. */
21884 static void
21885 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21887 bool saved_local_variables_forbidden_p;
21888 tree parm, parmdecl;
21890 /* While we're parsing the default args, we might (due to the
21891 statement expression extension) encounter more classes. We want
21892 to handle them right away, but we don't want them getting mixed
21893 up with default args that are currently in the queue. */
21894 push_unparsed_function_queues (parser);
21896 /* Local variable names (and the `this' keyword) may not appear
21897 in a default argument. */
21898 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21899 parser->local_variables_forbidden_p = true;
21901 push_defarg_context (fn);
21903 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21904 parmdecl = DECL_ARGUMENTS (fn);
21905 parm && parm != void_list_node;
21906 parm = TREE_CHAIN (parm),
21907 parmdecl = DECL_CHAIN (parmdecl))
21909 tree default_arg = TREE_PURPOSE (parm);
21910 tree parsed_arg;
21911 VEC(tree,gc) *insts;
21912 tree copy;
21913 unsigned ix;
21915 if (!default_arg)
21916 continue;
21918 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21919 /* This can happen for a friend declaration for a function
21920 already declared with default arguments. */
21921 continue;
21923 parsed_arg
21924 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21925 default_arg,
21926 TREE_VALUE (parm));
21927 if (parsed_arg == error_mark_node)
21929 continue;
21932 TREE_PURPOSE (parm) = parsed_arg;
21934 /* Update any instantiations we've already created. */
21935 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21936 VEC_iterate (tree, insts, ix, copy); ix++)
21937 TREE_PURPOSE (copy) = parsed_arg;
21940 pop_defarg_context ();
21942 /* Make sure no default arg is missing. */
21943 check_default_args (fn);
21945 /* Restore the state of local_variables_forbidden_p. */
21946 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21948 /* Restore the queue. */
21949 pop_unparsed_function_queues (parser);
21952 /* Parse the operand of `sizeof' (or a similar operator). Returns
21953 either a TYPE or an expression, depending on the form of the
21954 input. The KEYWORD indicates which kind of expression we have
21955 encountered. */
21957 static tree
21958 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21960 tree expr = NULL_TREE;
21961 const char *saved_message;
21962 char *tmp;
21963 bool saved_integral_constant_expression_p;
21964 bool saved_non_integral_constant_expression_p;
21965 bool pack_expansion_p = false;
21967 /* Types cannot be defined in a `sizeof' expression. Save away the
21968 old message. */
21969 saved_message = parser->type_definition_forbidden_message;
21970 /* And create the new one. */
21971 tmp = concat ("types may not be defined in %<",
21972 IDENTIFIER_POINTER (ridpointers[keyword]),
21973 "%> expressions", NULL);
21974 parser->type_definition_forbidden_message = tmp;
21976 /* The restrictions on constant-expressions do not apply inside
21977 sizeof expressions. */
21978 saved_integral_constant_expression_p
21979 = parser->integral_constant_expression_p;
21980 saved_non_integral_constant_expression_p
21981 = parser->non_integral_constant_expression_p;
21982 parser->integral_constant_expression_p = false;
21984 /* If it's a `...', then we are computing the length of a parameter
21985 pack. */
21986 if (keyword == RID_SIZEOF
21987 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21989 /* Consume the `...'. */
21990 cp_lexer_consume_token (parser->lexer);
21991 maybe_warn_variadic_templates ();
21993 /* Note that this is an expansion. */
21994 pack_expansion_p = true;
21997 /* Do not actually evaluate the expression. */
21998 ++cp_unevaluated_operand;
21999 ++c_inhibit_evaluation_warnings;
22000 /* If it's a `(', then we might be looking at the type-id
22001 construction. */
22002 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22004 tree type;
22005 bool saved_in_type_id_in_expr_p;
22007 /* We can't be sure yet whether we're looking at a type-id or an
22008 expression. */
22009 cp_parser_parse_tentatively (parser);
22010 /* Consume the `('. */
22011 cp_lexer_consume_token (parser->lexer);
22012 /* Parse the type-id. */
22013 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22014 parser->in_type_id_in_expr_p = true;
22015 type = cp_parser_type_id (parser);
22016 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22017 /* Now, look for the trailing `)'. */
22018 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22019 /* If all went well, then we're done. */
22020 if (cp_parser_parse_definitely (parser))
22022 cp_decl_specifier_seq decl_specs;
22024 /* Build a trivial decl-specifier-seq. */
22025 clear_decl_specs (&decl_specs);
22026 decl_specs.type = type;
22028 /* Call grokdeclarator to figure out what type this is. */
22029 expr = grokdeclarator (NULL,
22030 &decl_specs,
22031 TYPENAME,
22032 /*initialized=*/0,
22033 /*attrlist=*/NULL);
22036 else if (pack_expansion_p)
22037 permerror (cp_lexer_peek_token (parser->lexer)->location,
22038 "%<sizeof...%> argument must be surrounded by parentheses");
22040 /* If the type-id production did not work out, then we must be
22041 looking at the unary-expression production. */
22042 if (!expr)
22043 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22044 /*cast_p=*/false, NULL);
22046 if (pack_expansion_p)
22047 /* Build a pack expansion. */
22048 expr = make_pack_expansion (expr);
22050 /* Go back to evaluating expressions. */
22051 --cp_unevaluated_operand;
22052 --c_inhibit_evaluation_warnings;
22054 /* Free the message we created. */
22055 free (tmp);
22056 /* And restore the old one. */
22057 parser->type_definition_forbidden_message = saved_message;
22058 parser->integral_constant_expression_p
22059 = saved_integral_constant_expression_p;
22060 parser->non_integral_constant_expression_p
22061 = saved_non_integral_constant_expression_p;
22063 return expr;
22066 /* If the current declaration has no declarator, return true. */
22068 static bool
22069 cp_parser_declares_only_class_p (cp_parser *parser)
22071 /* If the next token is a `;' or a `,' then there is no
22072 declarator. */
22073 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22074 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22077 /* Update the DECL_SPECS to reflect the storage class indicated by
22078 KEYWORD. */
22080 static void
22081 cp_parser_set_storage_class (cp_parser *parser,
22082 cp_decl_specifier_seq *decl_specs,
22083 enum rid keyword,
22084 location_t location)
22086 cp_storage_class storage_class;
22088 if (parser->in_unbraced_linkage_specification_p)
22090 error_at (location, "invalid use of %qD in linkage specification",
22091 ridpointers[keyword]);
22092 return;
22094 else if (decl_specs->storage_class != sc_none)
22096 decl_specs->conflicting_specifiers_p = true;
22097 return;
22100 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22101 && decl_spec_seq_has_spec_p (decl_specs, ds_thread))
22103 error_at (decl_specs->locations[ds_thread],
22104 "%<__thread%> before %qD", ridpointers[keyword]);
22105 decl_specs->locations[ds_thread] = 0;
22108 switch (keyword)
22110 case RID_AUTO:
22111 storage_class = sc_auto;
22112 break;
22113 case RID_REGISTER:
22114 storage_class = sc_register;
22115 break;
22116 case RID_STATIC:
22117 storage_class = sc_static;
22118 break;
22119 case RID_EXTERN:
22120 storage_class = sc_extern;
22121 break;
22122 case RID_MUTABLE:
22123 storage_class = sc_mutable;
22124 break;
22125 default:
22126 gcc_unreachable ();
22128 decl_specs->storage_class = storage_class;
22129 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, location);
22131 /* A storage class specifier cannot be applied alongside a typedef
22132 specifier. If there is a typedef specifier present then set
22133 conflicting_specifiers_p which will trigger an error later
22134 on in grokdeclarator. */
22135 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22136 decl_specs->conflicting_specifiers_p = true;
22139 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22140 is true, the type is a class or enum definition. */
22142 static void
22143 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22144 tree type_spec,
22145 location_t location,
22146 bool type_definition_p)
22148 decl_specs->any_specifiers_p = true;
22150 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22151 (with, for example, in "typedef int wchar_t;") we remember that
22152 this is what happened. In system headers, we ignore these
22153 declarations so that G++ can work with system headers that are not
22154 C++-safe. */
22155 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22156 && !type_definition_p
22157 && (type_spec == boolean_type_node
22158 || type_spec == char16_type_node
22159 || type_spec == char32_type_node
22160 || type_spec == wchar_type_node)
22161 && (decl_specs->type
22162 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22163 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22164 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22165 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22167 decl_specs->redefined_builtin_type = type_spec;
22168 set_and_check_decl_spec_loc (decl_specs,
22169 ds_redefined_builtin_type_spec,
22170 location);
22171 if (!decl_specs->type)
22173 decl_specs->type = type_spec;
22174 decl_specs->type_definition_p = false;
22175 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, location);
22178 else if (decl_specs->type)
22179 decl_specs->multiple_types_p = true;
22180 else
22182 decl_specs->type = type_spec;
22183 decl_specs->type_definition_p = type_definition_p;
22184 decl_specs->redefined_builtin_type = NULL_TREE;
22185 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, location);
22189 /* Set the location for a declarator specifier and check if it is
22190 duplicated.
22192 DECL_SPECS is the sequence of declarator specifiers onto which to
22193 set the location.
22195 DS is the single declarator specifier to set which location is to
22196 be set onto the existing sequence of declarators.
22198 LOCATION is the location for the declarator specifier to
22199 consider. */
22201 static void
22202 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22203 cp_decl_spec ds, source_location location)
22205 gcc_assert (ds < ds_last);
22207 if (decl_specs == NULL)
22208 return;
22210 if (decl_specs->locations[ds] == 0)
22211 decl_specs->locations[ds] = location;
22212 else
22214 if (ds == ds_long)
22216 if (decl_specs->locations[ds_long_long] != 0)
22217 error_at (location,
22218 "%<long long long%> is too long for GCC");
22219 else
22221 decl_specs->locations[ds_long_long] = location;
22222 pedwarn_cxx98 (location,
22223 OPT_Wlong_long,
22224 "ISO C++ 1998 does not support %<long long%>");
22227 else
22229 static const char *const decl_spec_names[] = {
22230 "signed",
22231 "unsigned",
22232 "short",
22233 "long",
22234 "const",
22235 "volatile",
22236 "restrict",
22237 "inline",
22238 "virtual",
22239 "explicit",
22240 "friend",
22241 "typedef",
22242 "using",
22243 "constexpr",
22244 "__complex",
22245 "__thread"
22247 error_at (location,
22248 "duplicate %qs", decl_spec_names[ds]);
22253 /* Return true iff the declarator specifier DS is present in the
22254 sequence of declarator specifiers DECL_SPECS. */
22256 bool
22257 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22258 cp_decl_spec ds)
22260 gcc_assert (ds < ds_last);
22262 if (decl_specs == NULL)
22263 return false;
22265 return decl_specs->locations[ds] != 0;
22268 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22269 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22271 static bool
22272 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22274 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22277 /* Issue an error message indicating that TOKEN_DESC was expected.
22278 If KEYWORD is true, it indicated this function is called by
22279 cp_parser_require_keword and the required token can only be
22280 a indicated keyword. */
22282 static void
22283 cp_parser_required_error (cp_parser *parser,
22284 required_token token_desc,
22285 bool keyword)
22287 switch (token_desc)
22289 case RT_NEW:
22290 cp_parser_error (parser, "expected %<new%>");
22291 return;
22292 case RT_DELETE:
22293 cp_parser_error (parser, "expected %<delete%>");
22294 return;
22295 case RT_RETURN:
22296 cp_parser_error (parser, "expected %<return%>");
22297 return;
22298 case RT_WHILE:
22299 cp_parser_error (parser, "expected %<while%>");
22300 return;
22301 case RT_EXTERN:
22302 cp_parser_error (parser, "expected %<extern%>");
22303 return;
22304 case RT_STATIC_ASSERT:
22305 cp_parser_error (parser, "expected %<static_assert%>");
22306 return;
22307 case RT_DECLTYPE:
22308 cp_parser_error (parser, "expected %<decltype%>");
22309 return;
22310 case RT_OPERATOR:
22311 cp_parser_error (parser, "expected %<operator%>");
22312 return;
22313 case RT_CLASS:
22314 cp_parser_error (parser, "expected %<class%>");
22315 return;
22316 case RT_TEMPLATE:
22317 cp_parser_error (parser, "expected %<template%>");
22318 return;
22319 case RT_NAMESPACE:
22320 cp_parser_error (parser, "expected %<namespace%>");
22321 return;
22322 case RT_USING:
22323 cp_parser_error (parser, "expected %<using%>");
22324 return;
22325 case RT_ASM:
22326 cp_parser_error (parser, "expected %<asm%>");
22327 return;
22328 case RT_TRY:
22329 cp_parser_error (parser, "expected %<try%>");
22330 return;
22331 case RT_CATCH:
22332 cp_parser_error (parser, "expected %<catch%>");
22333 return;
22334 case RT_THROW:
22335 cp_parser_error (parser, "expected %<throw%>");
22336 return;
22337 case RT_LABEL:
22338 cp_parser_error (parser, "expected %<__label__%>");
22339 return;
22340 case RT_AT_TRY:
22341 cp_parser_error (parser, "expected %<@try%>");
22342 return;
22343 case RT_AT_SYNCHRONIZED:
22344 cp_parser_error (parser, "expected %<@synchronized%>");
22345 return;
22346 case RT_AT_THROW:
22347 cp_parser_error (parser, "expected %<@throw%>");
22348 return;
22349 case RT_TRANSACTION_ATOMIC:
22350 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22351 return;
22352 case RT_TRANSACTION_RELAXED:
22353 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22354 return;
22355 default:
22356 break;
22358 if (!keyword)
22360 switch (token_desc)
22362 case RT_SEMICOLON:
22363 cp_parser_error (parser, "expected %<;%>");
22364 return;
22365 case RT_OPEN_PAREN:
22366 cp_parser_error (parser, "expected %<(%>");
22367 return;
22368 case RT_CLOSE_BRACE:
22369 cp_parser_error (parser, "expected %<}%>");
22370 return;
22371 case RT_OPEN_BRACE:
22372 cp_parser_error (parser, "expected %<{%>");
22373 return;
22374 case RT_CLOSE_SQUARE:
22375 cp_parser_error (parser, "expected %<]%>");
22376 return;
22377 case RT_OPEN_SQUARE:
22378 cp_parser_error (parser, "expected %<[%>");
22379 return;
22380 case RT_COMMA:
22381 cp_parser_error (parser, "expected %<,%>");
22382 return;
22383 case RT_SCOPE:
22384 cp_parser_error (parser, "expected %<::%>");
22385 return;
22386 case RT_LESS:
22387 cp_parser_error (parser, "expected %<<%>");
22388 return;
22389 case RT_GREATER:
22390 cp_parser_error (parser, "expected %<>%>");
22391 return;
22392 case RT_EQ:
22393 cp_parser_error (parser, "expected %<=%>");
22394 return;
22395 case RT_ELLIPSIS:
22396 cp_parser_error (parser, "expected %<...%>");
22397 return;
22398 case RT_MULT:
22399 cp_parser_error (parser, "expected %<*%>");
22400 return;
22401 case RT_COMPL:
22402 cp_parser_error (parser, "expected %<~%>");
22403 return;
22404 case RT_COLON:
22405 cp_parser_error (parser, "expected %<:%>");
22406 return;
22407 case RT_COLON_SCOPE:
22408 cp_parser_error (parser, "expected %<:%> or %<::%>");
22409 return;
22410 case RT_CLOSE_PAREN:
22411 cp_parser_error (parser, "expected %<)%>");
22412 return;
22413 case RT_COMMA_CLOSE_PAREN:
22414 cp_parser_error (parser, "expected %<,%> or %<)%>");
22415 return;
22416 case RT_PRAGMA_EOL:
22417 cp_parser_error (parser, "expected end of line");
22418 return;
22419 case RT_NAME:
22420 cp_parser_error (parser, "expected identifier");
22421 return;
22422 case RT_SELECT:
22423 cp_parser_error (parser, "expected selection-statement");
22424 return;
22425 case RT_INTERATION:
22426 cp_parser_error (parser, "expected iteration-statement");
22427 return;
22428 case RT_JUMP:
22429 cp_parser_error (parser, "expected jump-statement");
22430 return;
22431 case RT_CLASS_KEY:
22432 cp_parser_error (parser, "expected class-key");
22433 return;
22434 case RT_CLASS_TYPENAME_TEMPLATE:
22435 cp_parser_error (parser,
22436 "expected %<class%>, %<typename%>, or %<template%>");
22437 return;
22438 default:
22439 gcc_unreachable ();
22442 else
22443 gcc_unreachable ();
22448 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22449 issue an error message indicating that TOKEN_DESC was expected.
22451 Returns the token consumed, if the token had the appropriate type.
22452 Otherwise, returns NULL. */
22454 static cp_token *
22455 cp_parser_require (cp_parser* parser,
22456 enum cpp_ttype type,
22457 required_token token_desc)
22459 if (cp_lexer_next_token_is (parser->lexer, type))
22460 return cp_lexer_consume_token (parser->lexer);
22461 else
22463 /* Output the MESSAGE -- unless we're parsing tentatively. */
22464 if (!cp_parser_simulate_error (parser))
22465 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22466 return NULL;
22470 /* An error message is produced if the next token is not '>'.
22471 All further tokens are skipped until the desired token is
22472 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22474 static void
22475 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22477 /* Current level of '< ... >'. */
22478 unsigned level = 0;
22479 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22480 unsigned nesting_depth = 0;
22482 /* Are we ready, yet? If not, issue error message. */
22483 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22484 return;
22486 /* Skip tokens until the desired token is found. */
22487 while (true)
22489 /* Peek at the next token. */
22490 switch (cp_lexer_peek_token (parser->lexer)->type)
22492 case CPP_LESS:
22493 if (!nesting_depth)
22494 ++level;
22495 break;
22497 case CPP_RSHIFT:
22498 if (cxx_dialect == cxx98)
22499 /* C++0x views the `>>' operator as two `>' tokens, but
22500 C++98 does not. */
22501 break;
22502 else if (!nesting_depth && level-- == 0)
22504 /* We've hit a `>>' where the first `>' closes the
22505 template argument list, and the second `>' is
22506 spurious. Just consume the `>>' and stop; we've
22507 already produced at least one error. */
22508 cp_lexer_consume_token (parser->lexer);
22509 return;
22511 /* Fall through for C++0x, so we handle the second `>' in
22512 the `>>'. */
22514 case CPP_GREATER:
22515 if (!nesting_depth && level-- == 0)
22517 /* We've reached the token we want, consume it and stop. */
22518 cp_lexer_consume_token (parser->lexer);
22519 return;
22521 break;
22523 case CPP_OPEN_PAREN:
22524 case CPP_OPEN_SQUARE:
22525 ++nesting_depth;
22526 break;
22528 case CPP_CLOSE_PAREN:
22529 case CPP_CLOSE_SQUARE:
22530 if (nesting_depth-- == 0)
22531 return;
22532 break;
22534 case CPP_EOF:
22535 case CPP_PRAGMA_EOL:
22536 case CPP_SEMICOLON:
22537 case CPP_OPEN_BRACE:
22538 case CPP_CLOSE_BRACE:
22539 /* The '>' was probably forgotten, don't look further. */
22540 return;
22542 default:
22543 break;
22546 /* Consume this token. */
22547 cp_lexer_consume_token (parser->lexer);
22551 /* If the next token is the indicated keyword, consume it. Otherwise,
22552 issue an error message indicating that TOKEN_DESC was expected.
22554 Returns the token consumed, if the token had the appropriate type.
22555 Otherwise, returns NULL. */
22557 static cp_token *
22558 cp_parser_require_keyword (cp_parser* parser,
22559 enum rid keyword,
22560 required_token token_desc)
22562 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22564 if (token && token->keyword != keyword)
22566 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22567 return NULL;
22570 return token;
22573 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22574 function-definition. */
22576 static bool
22577 cp_parser_token_starts_function_definition_p (cp_token* token)
22579 return (/* An ordinary function-body begins with an `{'. */
22580 token->type == CPP_OPEN_BRACE
22581 /* A ctor-initializer begins with a `:'. */
22582 || token->type == CPP_COLON
22583 /* A function-try-block begins with `try'. */
22584 || token->keyword == RID_TRY
22585 /* A function-transaction-block begins with `__transaction_atomic'
22586 or `__transaction_relaxed'. */
22587 || token->keyword == RID_TRANSACTION_ATOMIC
22588 || token->keyword == RID_TRANSACTION_RELAXED
22589 /* The named return value extension begins with `return'. */
22590 || token->keyword == RID_RETURN);
22593 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22594 definition. */
22596 static bool
22597 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22599 cp_token *token;
22601 token = cp_lexer_peek_token (parser->lexer);
22602 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22605 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22606 C++0x) ending a template-argument. */
22608 static bool
22609 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22611 cp_token *token;
22613 token = cp_lexer_peek_token (parser->lexer);
22614 return (token->type == CPP_COMMA
22615 || token->type == CPP_GREATER
22616 || token->type == CPP_ELLIPSIS
22617 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22620 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22621 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22623 static bool
22624 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22625 size_t n)
22627 cp_token *token;
22629 token = cp_lexer_peek_nth_token (parser->lexer, n);
22630 if (token->type == CPP_LESS)
22631 return true;
22632 /* Check for the sequence `<::' in the original code. It would be lexed as
22633 `[:', where `[' is a digraph, and there is no whitespace before
22634 `:'. */
22635 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22637 cp_token *token2;
22638 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22639 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22640 return true;
22642 return false;
22645 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22646 or none_type otherwise. */
22648 static enum tag_types
22649 cp_parser_token_is_class_key (cp_token* token)
22651 switch (token->keyword)
22653 case RID_CLASS:
22654 return class_type;
22655 case RID_STRUCT:
22656 return record_type;
22657 case RID_UNION:
22658 return union_type;
22660 default:
22661 return none_type;
22665 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22667 static void
22668 cp_parser_check_class_key (enum tag_types class_key, tree type)
22670 if (type == error_mark_node)
22671 return;
22672 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22674 permerror (input_location, "%qs tag used in naming %q#T",
22675 class_key == union_type ? "union"
22676 : class_key == record_type ? "struct" : "class",
22677 type);
22678 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22679 "%q#T was previously declared here", type);
22683 /* Issue an error message if DECL is redeclared with different
22684 access than its original declaration [class.access.spec/3].
22685 This applies to nested classes and nested class templates.
22686 [class.mem/1]. */
22688 static void
22689 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22691 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22692 return;
22694 if ((TREE_PRIVATE (decl)
22695 != (current_access_specifier == access_private_node))
22696 || (TREE_PROTECTED (decl)
22697 != (current_access_specifier == access_protected_node)))
22698 error_at (location, "%qD redeclared with different access", decl);
22701 /* Look for the `template' keyword, as a syntactic disambiguator.
22702 Return TRUE iff it is present, in which case it will be
22703 consumed. */
22705 static bool
22706 cp_parser_optional_template_keyword (cp_parser *parser)
22708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22710 /* The `template' keyword can only be used within templates;
22711 outside templates the parser can always figure out what is a
22712 template and what is not. */
22713 if (!processing_template_decl)
22715 cp_token *token = cp_lexer_peek_token (parser->lexer);
22716 error_at (token->location,
22717 "%<template%> (as a disambiguator) is only allowed "
22718 "within templates");
22719 /* If this part of the token stream is rescanned, the same
22720 error message would be generated. So, we purge the token
22721 from the stream. */
22722 cp_lexer_purge_token (parser->lexer);
22723 return false;
22725 else
22727 /* Consume the `template' keyword. */
22728 cp_lexer_consume_token (parser->lexer);
22729 return true;
22733 return false;
22736 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22737 set PARSER->SCOPE, and perform other related actions. */
22739 static void
22740 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22742 int i;
22743 struct tree_check *check_value;
22744 deferred_access_check *chk;
22745 VEC (deferred_access_check,gc) *checks;
22747 /* Get the stored value. */
22748 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22749 /* Perform any access checks that were deferred. */
22750 checks = check_value->checks;
22751 if (checks)
22753 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22754 perform_or_defer_access_check (chk->binfo,
22755 chk->decl,
22756 chk->diag_decl, tf_warning_or_error);
22758 /* Set the scope from the stored value. */
22759 parser->scope = check_value->value;
22760 parser->qualifying_scope = check_value->qualifying_scope;
22761 parser->object_scope = NULL_TREE;
22764 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22765 encounter the end of a block before what we were looking for. */
22767 static bool
22768 cp_parser_cache_group (cp_parser *parser,
22769 enum cpp_ttype end,
22770 unsigned depth)
22772 while (true)
22774 cp_token *token = cp_lexer_peek_token (parser->lexer);
22776 /* Abort a parenthesized expression if we encounter a semicolon. */
22777 if ((end == CPP_CLOSE_PAREN || depth == 0)
22778 && token->type == CPP_SEMICOLON)
22779 return true;
22780 /* If we've reached the end of the file, stop. */
22781 if (token->type == CPP_EOF
22782 || (end != CPP_PRAGMA_EOL
22783 && token->type == CPP_PRAGMA_EOL))
22784 return true;
22785 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22786 /* We've hit the end of an enclosing block, so there's been some
22787 kind of syntax error. */
22788 return true;
22790 /* Consume the token. */
22791 cp_lexer_consume_token (parser->lexer);
22792 /* See if it starts a new group. */
22793 if (token->type == CPP_OPEN_BRACE)
22795 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22796 /* In theory this should probably check end == '}', but
22797 cp_parser_save_member_function_body needs it to exit
22798 after either '}' or ')' when called with ')'. */
22799 if (depth == 0)
22800 return false;
22802 else if (token->type == CPP_OPEN_PAREN)
22804 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22805 if (depth == 0 && end == CPP_CLOSE_PAREN)
22806 return false;
22808 else if (token->type == CPP_PRAGMA)
22809 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22810 else if (token->type == end)
22811 return false;
22815 /* Like above, for caching a default argument or NSDMI. Both of these are
22816 terminated by a non-nested comma, but it can be unclear whether or not a
22817 comma is nested in a template argument list unless we do more parsing.
22818 In order to handle this ambiguity, when we encounter a ',' after a '<'
22819 we try to parse what follows as a parameter-declaration-list (in the
22820 case of a default argument) or a member-declarator (in the case of an
22821 NSDMI). If that succeeds, then we stop caching. */
22823 static tree
22824 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22826 unsigned depth = 0;
22827 int maybe_template_id = 0;
22828 cp_token *first_token;
22829 cp_token *token;
22830 tree default_argument;
22832 /* Add tokens until we have processed the entire default
22833 argument. We add the range [first_token, token). */
22834 first_token = cp_lexer_peek_token (parser->lexer);
22835 if (first_token->type == CPP_OPEN_BRACE)
22837 /* For list-initialization, this is straightforward. */
22838 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22839 token = cp_lexer_peek_token (parser->lexer);
22841 else while (true)
22843 bool done = false;
22845 /* Peek at the next token. */
22846 token = cp_lexer_peek_token (parser->lexer);
22847 /* What we do depends on what token we have. */
22848 switch (token->type)
22850 /* In valid code, a default argument must be
22851 immediately followed by a `,' `)', or `...'. */
22852 case CPP_COMMA:
22853 if (depth == 0 && maybe_template_id)
22855 /* If we've seen a '<', we might be in a
22856 template-argument-list. Until Core issue 325 is
22857 resolved, we don't know how this situation ought
22858 to be handled, so try to DTRT. We check whether
22859 what comes after the comma is a valid parameter
22860 declaration list. If it is, then the comma ends
22861 the default argument; otherwise the default
22862 argument continues. */
22863 bool error = false;
22864 tree t;
22866 /* Set ITALP so cp_parser_parameter_declaration_list
22867 doesn't decide to commit to this parse. */
22868 bool saved_italp = parser->in_template_argument_list_p;
22869 parser->in_template_argument_list_p = true;
22871 cp_parser_parse_tentatively (parser);
22872 cp_lexer_consume_token (parser->lexer);
22874 if (nsdmi)
22876 int ctor_dtor_or_conv_p;
22877 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22878 &ctor_dtor_or_conv_p,
22879 /*parenthesized_p=*/NULL,
22880 /*member_p=*/true);
22882 else
22884 begin_scope (sk_function_parms, NULL_TREE);
22885 cp_parser_parameter_declaration_list (parser, &error);
22886 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22887 pop_binding (DECL_NAME (t), t);
22888 leave_scope ();
22890 if (!cp_parser_error_occurred (parser) && !error)
22891 done = true;
22892 cp_parser_abort_tentative_parse (parser);
22894 parser->in_template_argument_list_p = saved_italp;
22895 break;
22897 case CPP_CLOSE_PAREN:
22898 case CPP_ELLIPSIS:
22899 /* If we run into a non-nested `;', `}', or `]',
22900 then the code is invalid -- but the default
22901 argument is certainly over. */
22902 case CPP_SEMICOLON:
22903 case CPP_CLOSE_BRACE:
22904 case CPP_CLOSE_SQUARE:
22905 if (depth == 0)
22906 done = true;
22907 /* Update DEPTH, if necessary. */
22908 else if (token->type == CPP_CLOSE_PAREN
22909 || token->type == CPP_CLOSE_BRACE
22910 || token->type == CPP_CLOSE_SQUARE)
22911 --depth;
22912 break;
22914 case CPP_OPEN_PAREN:
22915 case CPP_OPEN_SQUARE:
22916 case CPP_OPEN_BRACE:
22917 ++depth;
22918 break;
22920 case CPP_LESS:
22921 if (depth == 0)
22922 /* This might be the comparison operator, or it might
22923 start a template argument list. */
22924 ++maybe_template_id;
22925 break;
22927 case CPP_RSHIFT:
22928 if (cxx_dialect == cxx98)
22929 break;
22930 /* Fall through for C++0x, which treats the `>>'
22931 operator like two `>' tokens in certain
22932 cases. */
22934 case CPP_GREATER:
22935 if (depth == 0)
22937 /* This might be an operator, or it might close a
22938 template argument list. But if a previous '<'
22939 started a template argument list, this will have
22940 closed it, so we can't be in one anymore. */
22941 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22942 if (maybe_template_id < 0)
22943 maybe_template_id = 0;
22945 break;
22947 /* If we run out of tokens, issue an error message. */
22948 case CPP_EOF:
22949 case CPP_PRAGMA_EOL:
22950 error_at (token->location, "file ends in default argument");
22951 done = true;
22952 break;
22954 case CPP_NAME:
22955 case CPP_SCOPE:
22956 /* In these cases, we should look for template-ids.
22957 For example, if the default argument is
22958 `X<int, double>()', we need to do name lookup to
22959 figure out whether or not `X' is a template; if
22960 so, the `,' does not end the default argument.
22962 That is not yet done. */
22963 break;
22965 default:
22966 break;
22969 /* If we've reached the end, stop. */
22970 if (done)
22971 break;
22973 /* Add the token to the token block. */
22974 token = cp_lexer_consume_token (parser->lexer);
22977 /* Create a DEFAULT_ARG to represent the unparsed default
22978 argument. */
22979 default_argument = make_node (DEFAULT_ARG);
22980 DEFARG_TOKENS (default_argument)
22981 = cp_token_cache_new (first_token, token);
22982 DEFARG_INSTANTIATIONS (default_argument) = NULL;
22984 return default_argument;
22987 /* Begin parsing tentatively. We always save tokens while parsing
22988 tentatively so that if the tentative parsing fails we can restore the
22989 tokens. */
22991 static void
22992 cp_parser_parse_tentatively (cp_parser* parser)
22994 /* Enter a new parsing context. */
22995 parser->context = cp_parser_context_new (parser->context);
22996 /* Begin saving tokens. */
22997 cp_lexer_save_tokens (parser->lexer);
22998 /* In order to avoid repetitive access control error messages,
22999 access checks are queued up until we are no longer parsing
23000 tentatively. */
23001 push_deferring_access_checks (dk_deferred);
23004 /* Commit to the currently active tentative parse. */
23006 static void
23007 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23009 cp_parser_context *context;
23010 cp_lexer *lexer;
23012 /* Mark all of the levels as committed. */
23013 lexer = parser->lexer;
23014 for (context = parser->context; context->next; context = context->next)
23016 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23017 break;
23018 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23019 while (!cp_lexer_saving_tokens (lexer))
23020 lexer = lexer->next;
23021 cp_lexer_commit_tokens (lexer);
23025 /* Abort the currently active tentative parse. All consumed tokens
23026 will be rolled back, and no diagnostics will be issued. */
23028 static void
23029 cp_parser_abort_tentative_parse (cp_parser* parser)
23031 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23032 || errorcount > 0);
23033 cp_parser_simulate_error (parser);
23034 /* Now, pretend that we want to see if the construct was
23035 successfully parsed. */
23036 cp_parser_parse_definitely (parser);
23039 /* Stop parsing tentatively. If a parse error has occurred, restore the
23040 token stream. Otherwise, commit to the tokens we have consumed.
23041 Returns true if no error occurred; false otherwise. */
23043 static bool
23044 cp_parser_parse_definitely (cp_parser* parser)
23046 bool error_occurred;
23047 cp_parser_context *context;
23049 /* Remember whether or not an error occurred, since we are about to
23050 destroy that information. */
23051 error_occurred = cp_parser_error_occurred (parser);
23052 /* Remove the topmost context from the stack. */
23053 context = parser->context;
23054 parser->context = context->next;
23055 /* If no parse errors occurred, commit to the tentative parse. */
23056 if (!error_occurred)
23058 /* Commit to the tokens read tentatively, unless that was
23059 already done. */
23060 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23061 cp_lexer_commit_tokens (parser->lexer);
23063 pop_to_parent_deferring_access_checks ();
23065 /* Otherwise, if errors occurred, roll back our state so that things
23066 are just as they were before we began the tentative parse. */
23067 else
23069 cp_lexer_rollback_tokens (parser->lexer);
23070 pop_deferring_access_checks ();
23072 /* Add the context to the front of the free list. */
23073 context->next = cp_parser_context_free_list;
23074 cp_parser_context_free_list = context;
23076 return !error_occurred;
23079 /* Returns true if we are parsing tentatively and are not committed to
23080 this tentative parse. */
23082 static bool
23083 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23085 return (cp_parser_parsing_tentatively (parser)
23086 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23089 /* Returns nonzero iff an error has occurred during the most recent
23090 tentative parse. */
23092 static bool
23093 cp_parser_error_occurred (cp_parser* parser)
23095 return (cp_parser_parsing_tentatively (parser)
23096 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23099 /* Returns nonzero if GNU extensions are allowed. */
23101 static bool
23102 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23104 return parser->allow_gnu_extensions_p;
23107 /* Objective-C++ Productions */
23110 /* Parse an Objective-C expression, which feeds into a primary-expression
23111 above.
23113 objc-expression:
23114 objc-message-expression
23115 objc-string-literal
23116 objc-encode-expression
23117 objc-protocol-expression
23118 objc-selector-expression
23120 Returns a tree representation of the expression. */
23122 static tree
23123 cp_parser_objc_expression (cp_parser* parser)
23125 /* Try to figure out what kind of declaration is present. */
23126 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23128 switch (kwd->type)
23130 case CPP_OPEN_SQUARE:
23131 return cp_parser_objc_message_expression (parser);
23133 case CPP_OBJC_STRING:
23134 kwd = cp_lexer_consume_token (parser->lexer);
23135 return objc_build_string_object (kwd->u.value);
23137 case CPP_KEYWORD:
23138 switch (kwd->keyword)
23140 case RID_AT_ENCODE:
23141 return cp_parser_objc_encode_expression (parser);
23143 case RID_AT_PROTOCOL:
23144 return cp_parser_objc_protocol_expression (parser);
23146 case RID_AT_SELECTOR:
23147 return cp_parser_objc_selector_expression (parser);
23149 default:
23150 break;
23152 default:
23153 error_at (kwd->location,
23154 "misplaced %<@%D%> Objective-C++ construct",
23155 kwd->u.value);
23156 cp_parser_skip_to_end_of_block_or_statement (parser);
23159 return error_mark_node;
23162 /* Parse an Objective-C message expression.
23164 objc-message-expression:
23165 [ objc-message-receiver objc-message-args ]
23167 Returns a representation of an Objective-C message. */
23169 static tree
23170 cp_parser_objc_message_expression (cp_parser* parser)
23172 tree receiver, messageargs;
23174 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23175 receiver = cp_parser_objc_message_receiver (parser);
23176 messageargs = cp_parser_objc_message_args (parser);
23177 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23179 return objc_build_message_expr (receiver, messageargs);
23182 /* Parse an objc-message-receiver.
23184 objc-message-receiver:
23185 expression
23186 simple-type-specifier
23188 Returns a representation of the type or expression. */
23190 static tree
23191 cp_parser_objc_message_receiver (cp_parser* parser)
23193 tree rcv;
23195 /* An Objective-C message receiver may be either (1) a type
23196 or (2) an expression. */
23197 cp_parser_parse_tentatively (parser);
23198 rcv = cp_parser_expression (parser, false, NULL);
23200 if (cp_parser_parse_definitely (parser))
23201 return rcv;
23203 rcv = cp_parser_simple_type_specifier (parser,
23204 /*decl_specs=*/NULL,
23205 CP_PARSER_FLAGS_NONE);
23207 return objc_get_class_reference (rcv);
23210 /* Parse the arguments and selectors comprising an Objective-C message.
23212 objc-message-args:
23213 objc-selector
23214 objc-selector-args
23215 objc-selector-args , objc-comma-args
23217 objc-selector-args:
23218 objc-selector [opt] : assignment-expression
23219 objc-selector-args objc-selector [opt] : assignment-expression
23221 objc-comma-args:
23222 assignment-expression
23223 objc-comma-args , assignment-expression
23225 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23226 selector arguments and TREE_VALUE containing a list of comma
23227 arguments. */
23229 static tree
23230 cp_parser_objc_message_args (cp_parser* parser)
23232 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23233 bool maybe_unary_selector_p = true;
23234 cp_token *token = cp_lexer_peek_token (parser->lexer);
23236 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23238 tree selector = NULL_TREE, arg;
23240 if (token->type != CPP_COLON)
23241 selector = cp_parser_objc_selector (parser);
23243 /* Detect if we have a unary selector. */
23244 if (maybe_unary_selector_p
23245 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23246 return build_tree_list (selector, NULL_TREE);
23248 maybe_unary_selector_p = false;
23249 cp_parser_require (parser, CPP_COLON, RT_COLON);
23250 arg = cp_parser_assignment_expression (parser, false, NULL);
23252 sel_args
23253 = chainon (sel_args,
23254 build_tree_list (selector, arg));
23256 token = cp_lexer_peek_token (parser->lexer);
23259 /* Handle non-selector arguments, if any. */
23260 while (token->type == CPP_COMMA)
23262 tree arg;
23264 cp_lexer_consume_token (parser->lexer);
23265 arg = cp_parser_assignment_expression (parser, false, NULL);
23267 addl_args
23268 = chainon (addl_args,
23269 build_tree_list (NULL_TREE, arg));
23271 token = cp_lexer_peek_token (parser->lexer);
23274 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23276 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23277 return build_tree_list (error_mark_node, error_mark_node);
23280 return build_tree_list (sel_args, addl_args);
23283 /* Parse an Objective-C encode expression.
23285 objc-encode-expression:
23286 @encode objc-typename
23288 Returns an encoded representation of the type argument. */
23290 static tree
23291 cp_parser_objc_encode_expression (cp_parser* parser)
23293 tree type;
23294 cp_token *token;
23296 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23297 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23298 token = cp_lexer_peek_token (parser->lexer);
23299 type = complete_type (cp_parser_type_id (parser));
23300 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23302 if (!type)
23304 error_at (token->location,
23305 "%<@encode%> must specify a type as an argument");
23306 return error_mark_node;
23309 /* This happens if we find @encode(T) (where T is a template
23310 typename or something dependent on a template typename) when
23311 parsing a template. In that case, we can't compile it
23312 immediately, but we rather create an AT_ENCODE_EXPR which will
23313 need to be instantiated when the template is used.
23315 if (dependent_type_p (type))
23317 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23318 TREE_READONLY (value) = 1;
23319 return value;
23322 return objc_build_encode_expr (type);
23325 /* Parse an Objective-C @defs expression. */
23327 static tree
23328 cp_parser_objc_defs_expression (cp_parser *parser)
23330 tree name;
23332 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23333 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23334 name = cp_parser_identifier (parser);
23335 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23337 return objc_get_class_ivars (name);
23340 /* Parse an Objective-C protocol expression.
23342 objc-protocol-expression:
23343 @protocol ( identifier )
23345 Returns a representation of the protocol expression. */
23347 static tree
23348 cp_parser_objc_protocol_expression (cp_parser* parser)
23350 tree proto;
23352 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23353 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23354 proto = cp_parser_identifier (parser);
23355 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23357 return objc_build_protocol_expr (proto);
23360 /* Parse an Objective-C selector expression.
23362 objc-selector-expression:
23363 @selector ( objc-method-signature )
23365 objc-method-signature:
23366 objc-selector
23367 objc-selector-seq
23369 objc-selector-seq:
23370 objc-selector :
23371 objc-selector-seq objc-selector :
23373 Returns a representation of the method selector. */
23375 static tree
23376 cp_parser_objc_selector_expression (cp_parser* parser)
23378 tree sel_seq = NULL_TREE;
23379 bool maybe_unary_selector_p = true;
23380 cp_token *token;
23381 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23383 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23384 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23385 token = cp_lexer_peek_token (parser->lexer);
23387 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23388 || token->type == CPP_SCOPE)
23390 tree selector = NULL_TREE;
23392 if (token->type != CPP_COLON
23393 || token->type == CPP_SCOPE)
23394 selector = cp_parser_objc_selector (parser);
23396 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23397 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23399 /* Detect if we have a unary selector. */
23400 if (maybe_unary_selector_p)
23402 sel_seq = selector;
23403 goto finish_selector;
23405 else
23407 cp_parser_error (parser, "expected %<:%>");
23410 maybe_unary_selector_p = false;
23411 token = cp_lexer_consume_token (parser->lexer);
23413 if (token->type == CPP_SCOPE)
23415 sel_seq
23416 = chainon (sel_seq,
23417 build_tree_list (selector, NULL_TREE));
23418 sel_seq
23419 = chainon (sel_seq,
23420 build_tree_list (NULL_TREE, NULL_TREE));
23422 else
23423 sel_seq
23424 = chainon (sel_seq,
23425 build_tree_list (selector, NULL_TREE));
23427 token = cp_lexer_peek_token (parser->lexer);
23430 finish_selector:
23431 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23433 return objc_build_selector_expr (loc, sel_seq);
23436 /* Parse a list of identifiers.
23438 objc-identifier-list:
23439 identifier
23440 objc-identifier-list , identifier
23442 Returns a TREE_LIST of identifier nodes. */
23444 static tree
23445 cp_parser_objc_identifier_list (cp_parser* parser)
23447 tree identifier;
23448 tree list;
23449 cp_token *sep;
23451 identifier = cp_parser_identifier (parser);
23452 if (identifier == error_mark_node)
23453 return error_mark_node;
23455 list = build_tree_list (NULL_TREE, identifier);
23456 sep = cp_lexer_peek_token (parser->lexer);
23458 while (sep->type == CPP_COMMA)
23460 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23461 identifier = cp_parser_identifier (parser);
23462 if (identifier == error_mark_node)
23463 return list;
23465 list = chainon (list, build_tree_list (NULL_TREE,
23466 identifier));
23467 sep = cp_lexer_peek_token (parser->lexer);
23470 return list;
23473 /* Parse an Objective-C alias declaration.
23475 objc-alias-declaration:
23476 @compatibility_alias identifier identifier ;
23478 This function registers the alias mapping with the Objective-C front end.
23479 It returns nothing. */
23481 static void
23482 cp_parser_objc_alias_declaration (cp_parser* parser)
23484 tree alias, orig;
23486 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23487 alias = cp_parser_identifier (parser);
23488 orig = cp_parser_identifier (parser);
23489 objc_declare_alias (alias, orig);
23490 cp_parser_consume_semicolon_at_end_of_statement (parser);
23493 /* Parse an Objective-C class forward-declaration.
23495 objc-class-declaration:
23496 @class objc-identifier-list ;
23498 The function registers the forward declarations with the Objective-C
23499 front end. It returns nothing. */
23501 static void
23502 cp_parser_objc_class_declaration (cp_parser* parser)
23504 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23505 while (true)
23507 tree id;
23509 id = cp_parser_identifier (parser);
23510 if (id == error_mark_node)
23511 break;
23513 objc_declare_class (id);
23515 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23516 cp_lexer_consume_token (parser->lexer);
23517 else
23518 break;
23520 cp_parser_consume_semicolon_at_end_of_statement (parser);
23523 /* Parse a list of Objective-C protocol references.
23525 objc-protocol-refs-opt:
23526 objc-protocol-refs [opt]
23528 objc-protocol-refs:
23529 < objc-identifier-list >
23531 Returns a TREE_LIST of identifiers, if any. */
23533 static tree
23534 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23536 tree protorefs = NULL_TREE;
23538 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23540 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23541 protorefs = cp_parser_objc_identifier_list (parser);
23542 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23545 return protorefs;
23548 /* Parse a Objective-C visibility specification. */
23550 static void
23551 cp_parser_objc_visibility_spec (cp_parser* parser)
23553 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23555 switch (vis->keyword)
23557 case RID_AT_PRIVATE:
23558 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23559 break;
23560 case RID_AT_PROTECTED:
23561 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23562 break;
23563 case RID_AT_PUBLIC:
23564 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23565 break;
23566 case RID_AT_PACKAGE:
23567 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23568 break;
23569 default:
23570 return;
23573 /* Eat '@private'/'@protected'/'@public'. */
23574 cp_lexer_consume_token (parser->lexer);
23577 /* Parse an Objective-C method type. Return 'true' if it is a class
23578 (+) method, and 'false' if it is an instance (-) method. */
23580 static inline bool
23581 cp_parser_objc_method_type (cp_parser* parser)
23583 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23584 return true;
23585 else
23586 return false;
23589 /* Parse an Objective-C protocol qualifier. */
23591 static tree
23592 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23594 tree quals = NULL_TREE, node;
23595 cp_token *token = cp_lexer_peek_token (parser->lexer);
23597 node = token->u.value;
23599 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23600 && (node == ridpointers [(int) RID_IN]
23601 || node == ridpointers [(int) RID_OUT]
23602 || node == ridpointers [(int) RID_INOUT]
23603 || node == ridpointers [(int) RID_BYCOPY]
23604 || node == ridpointers [(int) RID_BYREF]
23605 || node == ridpointers [(int) RID_ONEWAY]))
23607 quals = tree_cons (NULL_TREE, node, quals);
23608 cp_lexer_consume_token (parser->lexer);
23609 token = cp_lexer_peek_token (parser->lexer);
23610 node = token->u.value;
23613 return quals;
23616 /* Parse an Objective-C typename. */
23618 static tree
23619 cp_parser_objc_typename (cp_parser* parser)
23621 tree type_name = NULL_TREE;
23623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23625 tree proto_quals, cp_type = NULL_TREE;
23627 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23628 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23630 /* An ObjC type name may consist of just protocol qualifiers, in which
23631 case the type shall default to 'id'. */
23632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23634 cp_type = cp_parser_type_id (parser);
23636 /* If the type could not be parsed, an error has already
23637 been produced. For error recovery, behave as if it had
23638 not been specified, which will use the default type
23639 'id'. */
23640 if (cp_type == error_mark_node)
23642 cp_type = NULL_TREE;
23643 /* We need to skip to the closing parenthesis as
23644 cp_parser_type_id() does not seem to do it for
23645 us. */
23646 cp_parser_skip_to_closing_parenthesis (parser,
23647 /*recovering=*/true,
23648 /*or_comma=*/false,
23649 /*consume_paren=*/false);
23653 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23654 type_name = build_tree_list (proto_quals, cp_type);
23657 return type_name;
23660 /* Check to see if TYPE refers to an Objective-C selector name. */
23662 static bool
23663 cp_parser_objc_selector_p (enum cpp_ttype type)
23665 return (type == CPP_NAME || type == CPP_KEYWORD
23666 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23667 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23668 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23669 || type == CPP_XOR || type == CPP_XOR_EQ);
23672 /* Parse an Objective-C selector. */
23674 static tree
23675 cp_parser_objc_selector (cp_parser* parser)
23677 cp_token *token = cp_lexer_consume_token (parser->lexer);
23679 if (!cp_parser_objc_selector_p (token->type))
23681 error_at (token->location, "invalid Objective-C++ selector name");
23682 return error_mark_node;
23685 /* C++ operator names are allowed to appear in ObjC selectors. */
23686 switch (token->type)
23688 case CPP_AND_AND: return get_identifier ("and");
23689 case CPP_AND_EQ: return get_identifier ("and_eq");
23690 case CPP_AND: return get_identifier ("bitand");
23691 case CPP_OR: return get_identifier ("bitor");
23692 case CPP_COMPL: return get_identifier ("compl");
23693 case CPP_NOT: return get_identifier ("not");
23694 case CPP_NOT_EQ: return get_identifier ("not_eq");
23695 case CPP_OR_OR: return get_identifier ("or");
23696 case CPP_OR_EQ: return get_identifier ("or_eq");
23697 case CPP_XOR: return get_identifier ("xor");
23698 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23699 default: return token->u.value;
23703 /* Parse an Objective-C params list. */
23705 static tree
23706 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23708 tree params = NULL_TREE;
23709 bool maybe_unary_selector_p = true;
23710 cp_token *token = cp_lexer_peek_token (parser->lexer);
23712 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23714 tree selector = NULL_TREE, type_name, identifier;
23715 tree parm_attr = NULL_TREE;
23717 if (token->keyword == RID_ATTRIBUTE)
23718 break;
23720 if (token->type != CPP_COLON)
23721 selector = cp_parser_objc_selector (parser);
23723 /* Detect if we have a unary selector. */
23724 if (maybe_unary_selector_p
23725 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23727 params = selector; /* Might be followed by attributes. */
23728 break;
23731 maybe_unary_selector_p = false;
23732 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23734 /* Something went quite wrong. There should be a colon
23735 here, but there is not. Stop parsing parameters. */
23736 break;
23738 type_name = cp_parser_objc_typename (parser);
23739 /* New ObjC allows attributes on parameters too. */
23740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23741 parm_attr = cp_parser_attributes_opt (parser);
23742 identifier = cp_parser_identifier (parser);
23744 params
23745 = chainon (params,
23746 objc_build_keyword_decl (selector,
23747 type_name,
23748 identifier,
23749 parm_attr));
23751 token = cp_lexer_peek_token (parser->lexer);
23754 if (params == NULL_TREE)
23756 cp_parser_error (parser, "objective-c++ method declaration is expected");
23757 return error_mark_node;
23760 /* We allow tail attributes for the method. */
23761 if (token->keyword == RID_ATTRIBUTE)
23763 *attributes = cp_parser_attributes_opt (parser);
23764 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23765 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23766 return params;
23767 cp_parser_error (parser,
23768 "method attributes must be specified at the end");
23769 return error_mark_node;
23772 if (params == NULL_TREE)
23774 cp_parser_error (parser, "objective-c++ method declaration is expected");
23775 return error_mark_node;
23777 return params;
23780 /* Parse the non-keyword Objective-C params. */
23782 static tree
23783 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23784 tree* attributes)
23786 tree params = make_node (TREE_LIST);
23787 cp_token *token = cp_lexer_peek_token (parser->lexer);
23788 *ellipsisp = false; /* Initially, assume no ellipsis. */
23790 while (token->type == CPP_COMMA)
23792 cp_parameter_declarator *parmdecl;
23793 tree parm;
23795 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23796 token = cp_lexer_peek_token (parser->lexer);
23798 if (token->type == CPP_ELLIPSIS)
23800 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23801 *ellipsisp = true;
23802 token = cp_lexer_peek_token (parser->lexer);
23803 break;
23806 /* TODO: parse attributes for tail parameters. */
23807 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23808 parm = grokdeclarator (parmdecl->declarator,
23809 &parmdecl->decl_specifiers,
23810 PARM, /*initialized=*/0,
23811 /*attrlist=*/NULL);
23813 chainon (params, build_tree_list (NULL_TREE, parm));
23814 token = cp_lexer_peek_token (parser->lexer);
23817 /* We allow tail attributes for the method. */
23818 if (token->keyword == RID_ATTRIBUTE)
23820 if (*attributes == NULL_TREE)
23822 *attributes = cp_parser_attributes_opt (parser);
23823 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23824 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23825 return params;
23827 else
23828 /* We have an error, but parse the attributes, so that we can
23829 carry on. */
23830 *attributes = cp_parser_attributes_opt (parser);
23832 cp_parser_error (parser,
23833 "method attributes must be specified at the end");
23834 return error_mark_node;
23837 return params;
23840 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23842 static void
23843 cp_parser_objc_interstitial_code (cp_parser* parser)
23845 cp_token *token = cp_lexer_peek_token (parser->lexer);
23847 /* If the next token is `extern' and the following token is a string
23848 literal, then we have a linkage specification. */
23849 if (token->keyword == RID_EXTERN
23850 && cp_parser_is_pure_string_literal
23851 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23852 cp_parser_linkage_specification (parser);
23853 /* Handle #pragma, if any. */
23854 else if (token->type == CPP_PRAGMA)
23855 cp_parser_pragma (parser, pragma_external);
23856 /* Allow stray semicolons. */
23857 else if (token->type == CPP_SEMICOLON)
23858 cp_lexer_consume_token (parser->lexer);
23859 /* Mark methods as optional or required, when building protocols. */
23860 else if (token->keyword == RID_AT_OPTIONAL)
23862 cp_lexer_consume_token (parser->lexer);
23863 objc_set_method_opt (true);
23865 else if (token->keyword == RID_AT_REQUIRED)
23867 cp_lexer_consume_token (parser->lexer);
23868 objc_set_method_opt (false);
23870 else if (token->keyword == RID_NAMESPACE)
23871 cp_parser_namespace_definition (parser);
23872 /* Other stray characters must generate errors. */
23873 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23875 cp_lexer_consume_token (parser->lexer);
23876 error ("stray %qs between Objective-C++ methods",
23877 token->type == CPP_OPEN_BRACE ? "{" : "}");
23879 /* Finally, try to parse a block-declaration, or a function-definition. */
23880 else
23881 cp_parser_block_declaration (parser, /*statement_p=*/false);
23884 /* Parse a method signature. */
23886 static tree
23887 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23889 tree rettype, kwdparms, optparms;
23890 bool ellipsis = false;
23891 bool is_class_method;
23893 is_class_method = cp_parser_objc_method_type (parser);
23894 rettype = cp_parser_objc_typename (parser);
23895 *attributes = NULL_TREE;
23896 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23897 if (kwdparms == error_mark_node)
23898 return error_mark_node;
23899 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23900 if (optparms == error_mark_node)
23901 return error_mark_node;
23903 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23906 static bool
23907 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23909 tree tattr;
23910 cp_lexer_save_tokens (parser->lexer);
23911 tattr = cp_parser_attributes_opt (parser);
23912 gcc_assert (tattr) ;
23914 /* If the attributes are followed by a method introducer, this is not allowed.
23915 Dump the attributes and flag the situation. */
23916 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23917 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23918 return true;
23920 /* Otherwise, the attributes introduce some interstitial code, possibly so
23921 rewind to allow that check. */
23922 cp_lexer_rollback_tokens (parser->lexer);
23923 return false;
23926 /* Parse an Objective-C method prototype list. */
23928 static void
23929 cp_parser_objc_method_prototype_list (cp_parser* parser)
23931 cp_token *token = cp_lexer_peek_token (parser->lexer);
23933 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23935 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23937 tree attributes, sig;
23938 bool is_class_method;
23939 if (token->type == CPP_PLUS)
23940 is_class_method = true;
23941 else
23942 is_class_method = false;
23943 sig = cp_parser_objc_method_signature (parser, &attributes);
23944 if (sig == error_mark_node)
23946 cp_parser_skip_to_end_of_block_or_statement (parser);
23947 token = cp_lexer_peek_token (parser->lexer);
23948 continue;
23950 objc_add_method_declaration (is_class_method, sig, attributes);
23951 cp_parser_consume_semicolon_at_end_of_statement (parser);
23953 else if (token->keyword == RID_AT_PROPERTY)
23954 cp_parser_objc_at_property_declaration (parser);
23955 else if (token->keyword == RID_ATTRIBUTE
23956 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23957 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23958 OPT_Wattributes,
23959 "prefix attributes are ignored for methods");
23960 else
23961 /* Allow for interspersed non-ObjC++ code. */
23962 cp_parser_objc_interstitial_code (parser);
23964 token = cp_lexer_peek_token (parser->lexer);
23967 if (token->type != CPP_EOF)
23968 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23969 else
23970 cp_parser_error (parser, "expected %<@end%>");
23972 objc_finish_interface ();
23975 /* Parse an Objective-C method definition list. */
23977 static void
23978 cp_parser_objc_method_definition_list (cp_parser* parser)
23980 cp_token *token = cp_lexer_peek_token (parser->lexer);
23982 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23984 tree meth;
23986 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23988 cp_token *ptk;
23989 tree sig, attribute;
23990 bool is_class_method;
23991 if (token->type == CPP_PLUS)
23992 is_class_method = true;
23993 else
23994 is_class_method = false;
23995 push_deferring_access_checks (dk_deferred);
23996 sig = cp_parser_objc_method_signature (parser, &attribute);
23997 if (sig == error_mark_node)
23999 cp_parser_skip_to_end_of_block_or_statement (parser);
24000 token = cp_lexer_peek_token (parser->lexer);
24001 continue;
24003 objc_start_method_definition (is_class_method, sig, attribute,
24004 NULL_TREE);
24006 /* For historical reasons, we accept an optional semicolon. */
24007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24008 cp_lexer_consume_token (parser->lexer);
24010 ptk = cp_lexer_peek_token (parser->lexer);
24011 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24012 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24014 perform_deferred_access_checks (tf_warning_or_error);
24015 stop_deferring_access_checks ();
24016 meth = cp_parser_function_definition_after_declarator (parser,
24017 false);
24018 pop_deferring_access_checks ();
24019 objc_finish_method_definition (meth);
24022 /* The following case will be removed once @synthesize is
24023 completely implemented. */
24024 else if (token->keyword == RID_AT_PROPERTY)
24025 cp_parser_objc_at_property_declaration (parser);
24026 else if (token->keyword == RID_AT_SYNTHESIZE)
24027 cp_parser_objc_at_synthesize_declaration (parser);
24028 else if (token->keyword == RID_AT_DYNAMIC)
24029 cp_parser_objc_at_dynamic_declaration (parser);
24030 else if (token->keyword == RID_ATTRIBUTE
24031 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24032 warning_at (token->location, OPT_Wattributes,
24033 "prefix attributes are ignored for methods");
24034 else
24035 /* Allow for interspersed non-ObjC++ code. */
24036 cp_parser_objc_interstitial_code (parser);
24038 token = cp_lexer_peek_token (parser->lexer);
24041 if (token->type != CPP_EOF)
24042 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24043 else
24044 cp_parser_error (parser, "expected %<@end%>");
24046 objc_finish_implementation ();
24049 /* Parse Objective-C ivars. */
24051 static void
24052 cp_parser_objc_class_ivars (cp_parser* parser)
24054 cp_token *token = cp_lexer_peek_token (parser->lexer);
24056 if (token->type != CPP_OPEN_BRACE)
24057 return; /* No ivars specified. */
24059 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24060 token = cp_lexer_peek_token (parser->lexer);
24062 while (token->type != CPP_CLOSE_BRACE
24063 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24065 cp_decl_specifier_seq declspecs;
24066 int decl_class_or_enum_p;
24067 tree prefix_attributes;
24069 cp_parser_objc_visibility_spec (parser);
24071 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24072 break;
24074 cp_parser_decl_specifier_seq (parser,
24075 CP_PARSER_FLAGS_OPTIONAL,
24076 &declspecs,
24077 &decl_class_or_enum_p);
24079 /* auto, register, static, extern, mutable. */
24080 if (declspecs.storage_class != sc_none)
24082 cp_parser_error (parser, "invalid type for instance variable");
24083 declspecs.storage_class = sc_none;
24086 /* __thread. */
24087 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24089 cp_parser_error (parser, "invalid type for instance variable");
24090 declspecs.locations[ds_thread] = 0;
24093 /* typedef. */
24094 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24096 cp_parser_error (parser, "invalid type for instance variable");
24097 declspecs.locations[ds_thread] = 0;
24100 prefix_attributes = declspecs.attributes;
24101 declspecs.attributes = NULL_TREE;
24103 /* Keep going until we hit the `;' at the end of the
24104 declaration. */
24105 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24107 tree width = NULL_TREE, attributes, first_attribute, decl;
24108 cp_declarator *declarator = NULL;
24109 int ctor_dtor_or_conv_p;
24111 /* Check for a (possibly unnamed) bitfield declaration. */
24112 token = cp_lexer_peek_token (parser->lexer);
24113 if (token->type == CPP_COLON)
24114 goto eat_colon;
24116 if (token->type == CPP_NAME
24117 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24118 == CPP_COLON))
24120 /* Get the name of the bitfield. */
24121 declarator = make_id_declarator (NULL_TREE,
24122 cp_parser_identifier (parser),
24123 sfk_none);
24125 eat_colon:
24126 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24127 /* Get the width of the bitfield. */
24128 width
24129 = cp_parser_constant_expression (parser,
24130 /*allow_non_constant=*/false,
24131 NULL);
24133 else
24135 /* Parse the declarator. */
24136 declarator
24137 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24138 &ctor_dtor_or_conv_p,
24139 /*parenthesized_p=*/NULL,
24140 /*member_p=*/false);
24143 /* Look for attributes that apply to the ivar. */
24144 attributes = cp_parser_attributes_opt (parser);
24145 /* Remember which attributes are prefix attributes and
24146 which are not. */
24147 first_attribute = attributes;
24148 /* Combine the attributes. */
24149 attributes = chainon (prefix_attributes, attributes);
24151 if (width)
24152 /* Create the bitfield declaration. */
24153 decl = grokbitfield (declarator, &declspecs,
24154 width,
24155 attributes);
24156 else
24157 decl = grokfield (declarator, &declspecs,
24158 NULL_TREE, /*init_const_expr_p=*/false,
24159 NULL_TREE, attributes);
24161 /* Add the instance variable. */
24162 if (decl != error_mark_node && decl != NULL_TREE)
24163 objc_add_instance_variable (decl);
24165 /* Reset PREFIX_ATTRIBUTES. */
24166 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24167 attributes = TREE_CHAIN (attributes);
24168 if (attributes)
24169 TREE_CHAIN (attributes) = NULL_TREE;
24171 token = cp_lexer_peek_token (parser->lexer);
24173 if (token->type == CPP_COMMA)
24175 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24176 continue;
24178 break;
24181 cp_parser_consume_semicolon_at_end_of_statement (parser);
24182 token = cp_lexer_peek_token (parser->lexer);
24185 if (token->keyword == RID_AT_END)
24186 cp_parser_error (parser, "expected %<}%>");
24188 /* Do not consume the RID_AT_END, so it will be read again as terminating
24189 the @interface of @implementation. */
24190 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24191 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24193 /* For historical reasons, we accept an optional semicolon. */
24194 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24195 cp_lexer_consume_token (parser->lexer);
24198 /* Parse an Objective-C protocol declaration. */
24200 static void
24201 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24203 tree proto, protorefs;
24204 cp_token *tok;
24206 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24209 tok = cp_lexer_peek_token (parser->lexer);
24210 error_at (tok->location, "identifier expected after %<@protocol%>");
24211 cp_parser_consume_semicolon_at_end_of_statement (parser);
24212 return;
24215 /* See if we have a forward declaration or a definition. */
24216 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24218 /* Try a forward declaration first. */
24219 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24221 while (true)
24223 tree id;
24225 id = cp_parser_identifier (parser);
24226 if (id == error_mark_node)
24227 break;
24229 objc_declare_protocol (id, attributes);
24231 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24232 cp_lexer_consume_token (parser->lexer);
24233 else
24234 break;
24236 cp_parser_consume_semicolon_at_end_of_statement (parser);
24239 /* Ok, we got a full-fledged definition (or at least should). */
24240 else
24242 proto = cp_parser_identifier (parser);
24243 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24244 objc_start_protocol (proto, protorefs, attributes);
24245 cp_parser_objc_method_prototype_list (parser);
24249 /* Parse an Objective-C superclass or category. */
24251 static void
24252 cp_parser_objc_superclass_or_category (cp_parser *parser,
24253 bool iface_p,
24254 tree *super,
24255 tree *categ, bool *is_class_extension)
24257 cp_token *next = cp_lexer_peek_token (parser->lexer);
24259 *super = *categ = NULL_TREE;
24260 *is_class_extension = false;
24261 if (next->type == CPP_COLON)
24263 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24264 *super = cp_parser_identifier (parser);
24266 else if (next->type == CPP_OPEN_PAREN)
24268 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24270 /* If there is no category name, and this is an @interface, we
24271 have a class extension. */
24272 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24274 *categ = NULL_TREE;
24275 *is_class_extension = true;
24277 else
24278 *categ = cp_parser_identifier (parser);
24280 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24284 /* Parse an Objective-C class interface. */
24286 static void
24287 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24289 tree name, super, categ, protos;
24290 bool is_class_extension;
24292 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24293 name = cp_parser_identifier (parser);
24294 if (name == error_mark_node)
24296 /* It's hard to recover because even if valid @interface stuff
24297 is to follow, we can't compile it (or validate it) if we
24298 don't even know which class it refers to. Let's assume this
24299 was a stray '@interface' token in the stream and skip it.
24301 return;
24303 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24304 &is_class_extension);
24305 protos = cp_parser_objc_protocol_refs_opt (parser);
24307 /* We have either a class or a category on our hands. */
24308 if (categ || is_class_extension)
24309 objc_start_category_interface (name, categ, protos, attributes);
24310 else
24312 objc_start_class_interface (name, super, protos, attributes);
24313 /* Handle instance variable declarations, if any. */
24314 cp_parser_objc_class_ivars (parser);
24315 objc_continue_interface ();
24318 cp_parser_objc_method_prototype_list (parser);
24321 /* Parse an Objective-C class implementation. */
24323 static void
24324 cp_parser_objc_class_implementation (cp_parser* parser)
24326 tree name, super, categ;
24327 bool is_class_extension;
24329 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24330 name = cp_parser_identifier (parser);
24331 if (name == error_mark_node)
24333 /* It's hard to recover because even if valid @implementation
24334 stuff is to follow, we can't compile it (or validate it) if
24335 we don't even know which class it refers to. Let's assume
24336 this was a stray '@implementation' token in the stream and
24337 skip it.
24339 return;
24341 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24342 &is_class_extension);
24344 /* We have either a class or a category on our hands. */
24345 if (categ)
24346 objc_start_category_implementation (name, categ);
24347 else
24349 objc_start_class_implementation (name, super);
24350 /* Handle instance variable declarations, if any. */
24351 cp_parser_objc_class_ivars (parser);
24352 objc_continue_implementation ();
24355 cp_parser_objc_method_definition_list (parser);
24358 /* Consume the @end token and finish off the implementation. */
24360 static void
24361 cp_parser_objc_end_implementation (cp_parser* parser)
24363 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24364 objc_finish_implementation ();
24367 /* Parse an Objective-C declaration. */
24369 static void
24370 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24372 /* Try to figure out what kind of declaration is present. */
24373 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24375 if (attributes)
24376 switch (kwd->keyword)
24378 case RID_AT_ALIAS:
24379 case RID_AT_CLASS:
24380 case RID_AT_END:
24381 error_at (kwd->location, "attributes may not be specified before"
24382 " the %<@%D%> Objective-C++ keyword",
24383 kwd->u.value);
24384 attributes = NULL;
24385 break;
24386 case RID_AT_IMPLEMENTATION:
24387 warning_at (kwd->location, OPT_Wattributes,
24388 "prefix attributes are ignored before %<@%D%>",
24389 kwd->u.value);
24390 attributes = NULL;
24391 default:
24392 break;
24395 switch (kwd->keyword)
24397 case RID_AT_ALIAS:
24398 cp_parser_objc_alias_declaration (parser);
24399 break;
24400 case RID_AT_CLASS:
24401 cp_parser_objc_class_declaration (parser);
24402 break;
24403 case RID_AT_PROTOCOL:
24404 cp_parser_objc_protocol_declaration (parser, attributes);
24405 break;
24406 case RID_AT_INTERFACE:
24407 cp_parser_objc_class_interface (parser, attributes);
24408 break;
24409 case RID_AT_IMPLEMENTATION:
24410 cp_parser_objc_class_implementation (parser);
24411 break;
24412 case RID_AT_END:
24413 cp_parser_objc_end_implementation (parser);
24414 break;
24415 default:
24416 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24417 kwd->u.value);
24418 cp_parser_skip_to_end_of_block_or_statement (parser);
24422 /* Parse an Objective-C try-catch-finally statement.
24424 objc-try-catch-finally-stmt:
24425 @try compound-statement objc-catch-clause-seq [opt]
24426 objc-finally-clause [opt]
24428 objc-catch-clause-seq:
24429 objc-catch-clause objc-catch-clause-seq [opt]
24431 objc-catch-clause:
24432 @catch ( objc-exception-declaration ) compound-statement
24434 objc-finally-clause:
24435 @finally compound-statement
24437 objc-exception-declaration:
24438 parameter-declaration
24439 '...'
24441 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24443 Returns NULL_TREE.
24445 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24446 for C. Keep them in sync. */
24448 static tree
24449 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24451 location_t location;
24452 tree stmt;
24454 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24455 location = cp_lexer_peek_token (parser->lexer)->location;
24456 objc_maybe_warn_exceptions (location);
24457 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24458 node, lest it get absorbed into the surrounding block. */
24459 stmt = push_stmt_list ();
24460 cp_parser_compound_statement (parser, NULL, false, false);
24461 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24463 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24465 cp_parameter_declarator *parm;
24466 tree parameter_declaration = error_mark_node;
24467 bool seen_open_paren = false;
24469 cp_lexer_consume_token (parser->lexer);
24470 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24471 seen_open_paren = true;
24472 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24474 /* We have "@catch (...)" (where the '...' are literally
24475 what is in the code). Skip the '...'.
24476 parameter_declaration is set to NULL_TREE, and
24477 objc_being_catch_clauses() knows that that means
24478 '...'. */
24479 cp_lexer_consume_token (parser->lexer);
24480 parameter_declaration = NULL_TREE;
24482 else
24484 /* We have "@catch (NSException *exception)" or something
24485 like that. Parse the parameter declaration. */
24486 parm = cp_parser_parameter_declaration (parser, false, NULL);
24487 if (parm == NULL)
24488 parameter_declaration = error_mark_node;
24489 else
24490 parameter_declaration = grokdeclarator (parm->declarator,
24491 &parm->decl_specifiers,
24492 PARM, /*initialized=*/0,
24493 /*attrlist=*/NULL);
24495 if (seen_open_paren)
24496 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24497 else
24499 /* If there was no open parenthesis, we are recovering from
24500 an error, and we are trying to figure out what mistake
24501 the user has made. */
24503 /* If there is an immediate closing parenthesis, the user
24504 probably forgot the opening one (ie, they typed "@catch
24505 NSException *e)". Parse the closing parenthesis and keep
24506 going. */
24507 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24508 cp_lexer_consume_token (parser->lexer);
24510 /* If these is no immediate closing parenthesis, the user
24511 probably doesn't know that parenthesis are required at
24512 all (ie, they typed "@catch NSException *e"). So, just
24513 forget about the closing parenthesis and keep going. */
24515 objc_begin_catch_clause (parameter_declaration);
24516 cp_parser_compound_statement (parser, NULL, false, false);
24517 objc_finish_catch_clause ();
24519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24521 cp_lexer_consume_token (parser->lexer);
24522 location = cp_lexer_peek_token (parser->lexer)->location;
24523 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24524 node, lest it get absorbed into the surrounding block. */
24525 stmt = push_stmt_list ();
24526 cp_parser_compound_statement (parser, NULL, false, false);
24527 objc_build_finally_clause (location, pop_stmt_list (stmt));
24530 return objc_finish_try_stmt ();
24533 /* Parse an Objective-C synchronized statement.
24535 objc-synchronized-stmt:
24536 @synchronized ( expression ) compound-statement
24538 Returns NULL_TREE. */
24540 static tree
24541 cp_parser_objc_synchronized_statement (cp_parser *parser)
24543 location_t location;
24544 tree lock, stmt;
24546 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24548 location = cp_lexer_peek_token (parser->lexer)->location;
24549 objc_maybe_warn_exceptions (location);
24550 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24551 lock = cp_parser_expression (parser, false, NULL);
24552 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24554 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24555 node, lest it get absorbed into the surrounding block. */
24556 stmt = push_stmt_list ();
24557 cp_parser_compound_statement (parser, NULL, false, false);
24559 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24562 /* Parse an Objective-C throw statement.
24564 objc-throw-stmt:
24565 @throw assignment-expression [opt] ;
24567 Returns a constructed '@throw' statement. */
24569 static tree
24570 cp_parser_objc_throw_statement (cp_parser *parser)
24572 tree expr = NULL_TREE;
24573 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24575 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24578 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24580 cp_parser_consume_semicolon_at_end_of_statement (parser);
24582 return objc_build_throw_stmt (loc, expr);
24585 /* Parse an Objective-C statement. */
24587 static tree
24588 cp_parser_objc_statement (cp_parser * parser)
24590 /* Try to figure out what kind of declaration is present. */
24591 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24593 switch (kwd->keyword)
24595 case RID_AT_TRY:
24596 return cp_parser_objc_try_catch_finally_statement (parser);
24597 case RID_AT_SYNCHRONIZED:
24598 return cp_parser_objc_synchronized_statement (parser);
24599 case RID_AT_THROW:
24600 return cp_parser_objc_throw_statement (parser);
24601 default:
24602 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24603 kwd->u.value);
24604 cp_parser_skip_to_end_of_block_or_statement (parser);
24607 return error_mark_node;
24610 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24611 look ahead to see if an objc keyword follows the attributes. This
24612 is to detect the use of prefix attributes on ObjC @interface and
24613 @protocol. */
24615 static bool
24616 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24618 cp_lexer_save_tokens (parser->lexer);
24619 *attrib = cp_parser_attributes_opt (parser);
24620 gcc_assert (*attrib);
24621 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24623 cp_lexer_commit_tokens (parser->lexer);
24624 return true;
24626 cp_lexer_rollback_tokens (parser->lexer);
24627 return false;
24630 /* This routine is a minimal replacement for
24631 c_parser_struct_declaration () used when parsing the list of
24632 types/names or ObjC++ properties. For example, when parsing the
24633 code
24635 @property (readonly) int a, b, c;
24637 this function is responsible for parsing "int a, int b, int c" and
24638 returning the declarations as CHAIN of DECLs.
24640 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24641 similar parsing. */
24642 static tree
24643 cp_parser_objc_struct_declaration (cp_parser *parser)
24645 tree decls = NULL_TREE;
24646 cp_decl_specifier_seq declspecs;
24647 int decl_class_or_enum_p;
24648 tree prefix_attributes;
24650 cp_parser_decl_specifier_seq (parser,
24651 CP_PARSER_FLAGS_NONE,
24652 &declspecs,
24653 &decl_class_or_enum_p);
24655 if (declspecs.type == error_mark_node)
24656 return error_mark_node;
24658 /* auto, register, static, extern, mutable. */
24659 if (declspecs.storage_class != sc_none)
24661 cp_parser_error (parser, "invalid type for property");
24662 declspecs.storage_class = sc_none;
24665 /* __thread. */
24666 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24668 cp_parser_error (parser, "invalid type for property");
24669 declspecs.locations[ds_thread] = 0;
24672 /* typedef. */
24673 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24675 cp_parser_error (parser, "invalid type for property");
24676 declspecs.locations[ds_typedef] = 0;
24679 prefix_attributes = declspecs.attributes;
24680 declspecs.attributes = NULL_TREE;
24682 /* Keep going until we hit the `;' at the end of the declaration. */
24683 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24685 tree attributes, first_attribute, decl;
24686 cp_declarator *declarator;
24687 cp_token *token;
24689 /* Parse the declarator. */
24690 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24691 NULL, NULL, false);
24693 /* Look for attributes that apply to the ivar. */
24694 attributes = cp_parser_attributes_opt (parser);
24695 /* Remember which attributes are prefix attributes and
24696 which are not. */
24697 first_attribute = attributes;
24698 /* Combine the attributes. */
24699 attributes = chainon (prefix_attributes, attributes);
24701 decl = grokfield (declarator, &declspecs,
24702 NULL_TREE, /*init_const_expr_p=*/false,
24703 NULL_TREE, attributes);
24705 if (decl == error_mark_node || decl == NULL_TREE)
24706 return error_mark_node;
24708 /* Reset PREFIX_ATTRIBUTES. */
24709 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24710 attributes = TREE_CHAIN (attributes);
24711 if (attributes)
24712 TREE_CHAIN (attributes) = NULL_TREE;
24714 DECL_CHAIN (decl) = decls;
24715 decls = decl;
24717 token = cp_lexer_peek_token (parser->lexer);
24718 if (token->type == CPP_COMMA)
24720 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24721 continue;
24723 else
24724 break;
24726 return decls;
24729 /* Parse an Objective-C @property declaration. The syntax is:
24731 objc-property-declaration:
24732 '@property' objc-property-attributes[opt] struct-declaration ;
24734 objc-property-attributes:
24735 '(' objc-property-attribute-list ')'
24737 objc-property-attribute-list:
24738 objc-property-attribute
24739 objc-property-attribute-list, objc-property-attribute
24741 objc-property-attribute
24742 'getter' = identifier
24743 'setter' = identifier
24744 'readonly'
24745 'readwrite'
24746 'assign'
24747 'retain'
24748 'copy'
24749 'nonatomic'
24751 For example:
24752 @property NSString *name;
24753 @property (readonly) id object;
24754 @property (retain, nonatomic, getter=getTheName) id name;
24755 @property int a, b, c;
24757 PS: This function is identical to
24758 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24759 static void
24760 cp_parser_objc_at_property_declaration (cp_parser *parser)
24762 /* The following variables hold the attributes of the properties as
24763 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24764 seen. When we see an attribute, we set them to 'true' (if they
24765 are boolean properties) or to the identifier (if they have an
24766 argument, ie, for getter and setter). Note that here we only
24767 parse the list of attributes, check the syntax and accumulate the
24768 attributes that we find. objc_add_property_declaration() will
24769 then process the information. */
24770 bool property_assign = false;
24771 bool property_copy = false;
24772 tree property_getter_ident = NULL_TREE;
24773 bool property_nonatomic = false;
24774 bool property_readonly = false;
24775 bool property_readwrite = false;
24776 bool property_retain = false;
24777 tree property_setter_ident = NULL_TREE;
24779 /* 'properties' is the list of properties that we read. Usually a
24780 single one, but maybe more (eg, in "@property int a, b, c;" there
24781 are three). */
24782 tree properties;
24783 location_t loc;
24785 loc = cp_lexer_peek_token (parser->lexer)->location;
24787 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24789 /* Parse the optional attribute list... */
24790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24792 /* Eat the '('. */
24793 cp_lexer_consume_token (parser->lexer);
24795 while (true)
24797 bool syntax_error = false;
24798 cp_token *token = cp_lexer_peek_token (parser->lexer);
24799 enum rid keyword;
24801 if (token->type != CPP_NAME)
24803 cp_parser_error (parser, "expected identifier");
24804 break;
24806 keyword = C_RID_CODE (token->u.value);
24807 cp_lexer_consume_token (parser->lexer);
24808 switch (keyword)
24810 case RID_ASSIGN: property_assign = true; break;
24811 case RID_COPY: property_copy = true; break;
24812 case RID_NONATOMIC: property_nonatomic = true; break;
24813 case RID_READONLY: property_readonly = true; break;
24814 case RID_READWRITE: property_readwrite = true; break;
24815 case RID_RETAIN: property_retain = true; break;
24817 case RID_GETTER:
24818 case RID_SETTER:
24819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24821 if (keyword == RID_GETTER)
24822 cp_parser_error (parser,
24823 "missing %<=%> (after %<getter%> attribute)");
24824 else
24825 cp_parser_error (parser,
24826 "missing %<=%> (after %<setter%> attribute)");
24827 syntax_error = true;
24828 break;
24830 cp_lexer_consume_token (parser->lexer); /* eat the = */
24831 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24833 cp_parser_error (parser, "expected identifier");
24834 syntax_error = true;
24835 break;
24837 if (keyword == RID_SETTER)
24839 if (property_setter_ident != NULL_TREE)
24841 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24842 cp_lexer_consume_token (parser->lexer);
24844 else
24845 property_setter_ident = cp_parser_objc_selector (parser);
24846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24847 cp_parser_error (parser, "setter name must terminate with %<:%>");
24848 else
24849 cp_lexer_consume_token (parser->lexer);
24851 else
24853 if (property_getter_ident != NULL_TREE)
24855 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24856 cp_lexer_consume_token (parser->lexer);
24858 else
24859 property_getter_ident = cp_parser_objc_selector (parser);
24861 break;
24862 default:
24863 cp_parser_error (parser, "unknown property attribute");
24864 syntax_error = true;
24865 break;
24868 if (syntax_error)
24869 break;
24871 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24872 cp_lexer_consume_token (parser->lexer);
24873 else
24874 break;
24877 /* FIXME: "@property (setter, assign);" will generate a spurious
24878 "error: expected ‘)’ before ‘,’ token". This is because
24879 cp_parser_require, unlike the C counterpart, will produce an
24880 error even if we are in error recovery. */
24881 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24883 cp_parser_skip_to_closing_parenthesis (parser,
24884 /*recovering=*/true,
24885 /*or_comma=*/false,
24886 /*consume_paren=*/true);
24890 /* ... and the property declaration(s). */
24891 properties = cp_parser_objc_struct_declaration (parser);
24893 if (properties == error_mark_node)
24895 cp_parser_skip_to_end_of_statement (parser);
24896 /* If the next token is now a `;', consume it. */
24897 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24898 cp_lexer_consume_token (parser->lexer);
24899 return;
24902 if (properties == NULL_TREE)
24903 cp_parser_error (parser, "expected identifier");
24904 else
24906 /* Comma-separated properties are chained together in
24907 reverse order; add them one by one. */
24908 properties = nreverse (properties);
24910 for (; properties; properties = TREE_CHAIN (properties))
24911 objc_add_property_declaration (loc, copy_node (properties),
24912 property_readonly, property_readwrite,
24913 property_assign, property_retain,
24914 property_copy, property_nonatomic,
24915 property_getter_ident, property_setter_ident);
24918 cp_parser_consume_semicolon_at_end_of_statement (parser);
24921 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24923 objc-synthesize-declaration:
24924 @synthesize objc-synthesize-identifier-list ;
24926 objc-synthesize-identifier-list:
24927 objc-synthesize-identifier
24928 objc-synthesize-identifier-list, objc-synthesize-identifier
24930 objc-synthesize-identifier
24931 identifier
24932 identifier = identifier
24934 For example:
24935 @synthesize MyProperty;
24936 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24938 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24939 for C. Keep them in sync.
24941 static void
24942 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24944 tree list = NULL_TREE;
24945 location_t loc;
24946 loc = cp_lexer_peek_token (parser->lexer)->location;
24948 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24949 while (true)
24951 tree property, ivar;
24952 property = cp_parser_identifier (parser);
24953 if (property == error_mark_node)
24955 cp_parser_consume_semicolon_at_end_of_statement (parser);
24956 return;
24958 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24960 cp_lexer_consume_token (parser->lexer);
24961 ivar = cp_parser_identifier (parser);
24962 if (ivar == error_mark_node)
24964 cp_parser_consume_semicolon_at_end_of_statement (parser);
24965 return;
24968 else
24969 ivar = NULL_TREE;
24970 list = chainon (list, build_tree_list (ivar, property));
24971 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24972 cp_lexer_consume_token (parser->lexer);
24973 else
24974 break;
24976 cp_parser_consume_semicolon_at_end_of_statement (parser);
24977 objc_add_synthesize_declaration (loc, list);
24980 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24982 objc-dynamic-declaration:
24983 @dynamic identifier-list ;
24985 For example:
24986 @dynamic MyProperty;
24987 @dynamic MyProperty, AnotherProperty;
24989 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24990 for C. Keep them in sync.
24992 static void
24993 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24995 tree list = NULL_TREE;
24996 location_t loc;
24997 loc = cp_lexer_peek_token (parser->lexer)->location;
24999 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25000 while (true)
25002 tree property;
25003 property = cp_parser_identifier (parser);
25004 if (property == error_mark_node)
25006 cp_parser_consume_semicolon_at_end_of_statement (parser);
25007 return;
25009 list = chainon (list, build_tree_list (NULL, property));
25010 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25011 cp_lexer_consume_token (parser->lexer);
25012 else
25013 break;
25015 cp_parser_consume_semicolon_at_end_of_statement (parser);
25016 objc_add_dynamic_declaration (loc, list);
25020 /* OpenMP 2.5 parsing routines. */
25022 /* Returns name of the next clause.
25023 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25024 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25025 returned and the token is consumed. */
25027 static pragma_omp_clause
25028 cp_parser_omp_clause_name (cp_parser *parser)
25030 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25032 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25033 result = PRAGMA_OMP_CLAUSE_IF;
25034 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25035 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25036 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25037 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25038 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25040 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25041 const char *p = IDENTIFIER_POINTER (id);
25043 switch (p[0])
25045 case 'c':
25046 if (!strcmp ("collapse", p))
25047 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25048 else if (!strcmp ("copyin", p))
25049 result = PRAGMA_OMP_CLAUSE_COPYIN;
25050 else if (!strcmp ("copyprivate", p))
25051 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25052 break;
25053 case 'f':
25054 if (!strcmp ("final", p))
25055 result = PRAGMA_OMP_CLAUSE_FINAL;
25056 else if (!strcmp ("firstprivate", p))
25057 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25058 break;
25059 case 'l':
25060 if (!strcmp ("lastprivate", p))
25061 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25062 break;
25063 case 'm':
25064 if (!strcmp ("mergeable", p))
25065 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25066 break;
25067 case 'n':
25068 if (!strcmp ("nowait", p))
25069 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25070 else if (!strcmp ("num_threads", p))
25071 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25072 break;
25073 case 'o':
25074 if (!strcmp ("ordered", p))
25075 result = PRAGMA_OMP_CLAUSE_ORDERED;
25076 break;
25077 case 'r':
25078 if (!strcmp ("reduction", p))
25079 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25080 break;
25081 case 's':
25082 if (!strcmp ("schedule", p))
25083 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25084 else if (!strcmp ("shared", p))
25085 result = PRAGMA_OMP_CLAUSE_SHARED;
25086 break;
25087 case 'u':
25088 if (!strcmp ("untied", p))
25089 result = PRAGMA_OMP_CLAUSE_UNTIED;
25090 break;
25094 if (result != PRAGMA_OMP_CLAUSE_NONE)
25095 cp_lexer_consume_token (parser->lexer);
25097 return result;
25100 /* Validate that a clause of the given type does not already exist. */
25102 static void
25103 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25104 const char *name, location_t location)
25106 tree c;
25108 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25109 if (OMP_CLAUSE_CODE (c) == code)
25111 error_at (location, "too many %qs clauses", name);
25112 break;
25116 /* OpenMP 2.5:
25117 variable-list:
25118 identifier
25119 variable-list , identifier
25121 In addition, we match a closing parenthesis. An opening parenthesis
25122 will have been consumed by the caller.
25124 If KIND is nonzero, create the appropriate node and install the decl
25125 in OMP_CLAUSE_DECL and add the node to the head of the list.
25127 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25128 return the list created. */
25130 static tree
25131 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25132 tree list)
25134 cp_token *token;
25135 while (1)
25137 tree name, decl;
25139 token = cp_lexer_peek_token (parser->lexer);
25140 name = cp_parser_id_expression (parser, /*template_p=*/false,
25141 /*check_dependency_p=*/true,
25142 /*template_p=*/NULL,
25143 /*declarator_p=*/false,
25144 /*optional_p=*/false);
25145 if (name == error_mark_node)
25146 goto skip_comma;
25148 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25149 if (decl == error_mark_node)
25150 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25151 token->location);
25152 else if (kind != 0)
25154 tree u = build_omp_clause (token->location, kind);
25155 OMP_CLAUSE_DECL (u) = decl;
25156 OMP_CLAUSE_CHAIN (u) = list;
25157 list = u;
25159 else
25160 list = tree_cons (decl, NULL_TREE, list);
25162 get_comma:
25163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25164 break;
25165 cp_lexer_consume_token (parser->lexer);
25168 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25170 int ending;
25172 /* Try to resync to an unnested comma. Copied from
25173 cp_parser_parenthesized_expression_list. */
25174 skip_comma:
25175 ending = cp_parser_skip_to_closing_parenthesis (parser,
25176 /*recovering=*/true,
25177 /*or_comma=*/true,
25178 /*consume_paren=*/true);
25179 if (ending < 0)
25180 goto get_comma;
25183 return list;
25186 /* Similarly, but expect leading and trailing parenthesis. This is a very
25187 common case for omp clauses. */
25189 static tree
25190 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25192 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25193 return cp_parser_omp_var_list_no_open (parser, kind, list);
25194 return list;
25197 /* OpenMP 3.0:
25198 collapse ( constant-expression ) */
25200 static tree
25201 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25203 tree c, num;
25204 location_t loc;
25205 HOST_WIDE_INT n;
25207 loc = cp_lexer_peek_token (parser->lexer)->location;
25208 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25209 return list;
25211 num = cp_parser_constant_expression (parser, false, NULL);
25213 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25214 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25215 /*or_comma=*/false,
25216 /*consume_paren=*/true);
25218 if (num == error_mark_node)
25219 return list;
25220 num = fold_non_dependent_expr (num);
25221 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25222 || !host_integerp (num, 0)
25223 || (n = tree_low_cst (num, 0)) <= 0
25224 || (int) n != n)
25226 error_at (loc, "collapse argument needs positive constant integer expression");
25227 return list;
25230 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25231 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25232 OMP_CLAUSE_CHAIN (c) = list;
25233 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25235 return c;
25238 /* OpenMP 2.5:
25239 default ( shared | none ) */
25241 static tree
25242 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25244 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25245 tree c;
25247 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25248 return list;
25249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25252 const char *p = IDENTIFIER_POINTER (id);
25254 switch (p[0])
25256 case 'n':
25257 if (strcmp ("none", p) != 0)
25258 goto invalid_kind;
25259 kind = OMP_CLAUSE_DEFAULT_NONE;
25260 break;
25262 case 's':
25263 if (strcmp ("shared", p) != 0)
25264 goto invalid_kind;
25265 kind = OMP_CLAUSE_DEFAULT_SHARED;
25266 break;
25268 default:
25269 goto invalid_kind;
25272 cp_lexer_consume_token (parser->lexer);
25274 else
25276 invalid_kind:
25277 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25280 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25281 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25282 /*or_comma=*/false,
25283 /*consume_paren=*/true);
25285 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25286 return list;
25288 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25289 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25290 OMP_CLAUSE_CHAIN (c) = list;
25291 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25293 return c;
25296 /* OpenMP 3.1:
25297 final ( expression ) */
25299 static tree
25300 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25302 tree t, c;
25304 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25305 return list;
25307 t = cp_parser_condition (parser);
25309 if (t == error_mark_node
25310 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25311 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25312 /*or_comma=*/false,
25313 /*consume_paren=*/true);
25315 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25317 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25318 OMP_CLAUSE_FINAL_EXPR (c) = t;
25319 OMP_CLAUSE_CHAIN (c) = list;
25321 return c;
25324 /* OpenMP 2.5:
25325 if ( expression ) */
25327 static tree
25328 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25330 tree t, c;
25332 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25333 return list;
25335 t = cp_parser_condition (parser);
25337 if (t == error_mark_node
25338 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25339 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25340 /*or_comma=*/false,
25341 /*consume_paren=*/true);
25343 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25345 c = build_omp_clause (location, OMP_CLAUSE_IF);
25346 OMP_CLAUSE_IF_EXPR (c) = t;
25347 OMP_CLAUSE_CHAIN (c) = list;
25349 return c;
25352 /* OpenMP 3.1:
25353 mergeable */
25355 static tree
25356 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25357 tree list, location_t location)
25359 tree c;
25361 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25362 location);
25364 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25365 OMP_CLAUSE_CHAIN (c) = list;
25366 return c;
25369 /* OpenMP 2.5:
25370 nowait */
25372 static tree
25373 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25374 tree list, location_t location)
25376 tree c;
25378 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25380 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25381 OMP_CLAUSE_CHAIN (c) = list;
25382 return c;
25385 /* OpenMP 2.5:
25386 num_threads ( expression ) */
25388 static tree
25389 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25390 location_t location)
25392 tree t, c;
25394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25395 return list;
25397 t = cp_parser_expression (parser, false, NULL);
25399 if (t == error_mark_node
25400 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25401 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25402 /*or_comma=*/false,
25403 /*consume_paren=*/true);
25405 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25406 "num_threads", location);
25408 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25409 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25410 OMP_CLAUSE_CHAIN (c) = list;
25412 return c;
25415 /* OpenMP 2.5:
25416 ordered */
25418 static tree
25419 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25420 tree list, location_t location)
25422 tree c;
25424 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25425 "ordered", location);
25427 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25428 OMP_CLAUSE_CHAIN (c) = list;
25429 return c;
25432 /* OpenMP 2.5:
25433 reduction ( reduction-operator : variable-list )
25435 reduction-operator:
25436 One of: + * - & ^ | && ||
25438 OpenMP 3.1:
25440 reduction-operator:
25441 One of: + * - & ^ | && || min max */
25443 static tree
25444 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25446 enum tree_code code;
25447 tree nlist, c;
25449 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25450 return list;
25452 switch (cp_lexer_peek_token (parser->lexer)->type)
25454 case CPP_PLUS:
25455 code = PLUS_EXPR;
25456 break;
25457 case CPP_MULT:
25458 code = MULT_EXPR;
25459 break;
25460 case CPP_MINUS:
25461 code = MINUS_EXPR;
25462 break;
25463 case CPP_AND:
25464 code = BIT_AND_EXPR;
25465 break;
25466 case CPP_XOR:
25467 code = BIT_XOR_EXPR;
25468 break;
25469 case CPP_OR:
25470 code = BIT_IOR_EXPR;
25471 break;
25472 case CPP_AND_AND:
25473 code = TRUTH_ANDIF_EXPR;
25474 break;
25475 case CPP_OR_OR:
25476 code = TRUTH_ORIF_EXPR;
25477 break;
25478 case CPP_NAME:
25480 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25481 const char *p = IDENTIFIER_POINTER (id);
25483 if (strcmp (p, "min") == 0)
25485 code = MIN_EXPR;
25486 break;
25488 if (strcmp (p, "max") == 0)
25490 code = MAX_EXPR;
25491 break;
25494 /* FALLTHROUGH */
25495 default:
25496 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25497 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25498 resync_fail:
25499 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25500 /*or_comma=*/false,
25501 /*consume_paren=*/true);
25502 return list;
25504 cp_lexer_consume_token (parser->lexer);
25506 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25507 goto resync_fail;
25509 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25510 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25511 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25513 return nlist;
25516 /* OpenMP 2.5:
25517 schedule ( schedule-kind )
25518 schedule ( schedule-kind , expression )
25520 schedule-kind:
25521 static | dynamic | guided | runtime | auto */
25523 static tree
25524 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25526 tree c, t;
25528 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25529 return list;
25531 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25535 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25536 const char *p = IDENTIFIER_POINTER (id);
25538 switch (p[0])
25540 case 'd':
25541 if (strcmp ("dynamic", p) != 0)
25542 goto invalid_kind;
25543 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25544 break;
25546 case 'g':
25547 if (strcmp ("guided", p) != 0)
25548 goto invalid_kind;
25549 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25550 break;
25552 case 'r':
25553 if (strcmp ("runtime", p) != 0)
25554 goto invalid_kind;
25555 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25556 break;
25558 default:
25559 goto invalid_kind;
25562 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25563 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25564 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25565 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25566 else
25567 goto invalid_kind;
25568 cp_lexer_consume_token (parser->lexer);
25570 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25572 cp_token *token;
25573 cp_lexer_consume_token (parser->lexer);
25575 token = cp_lexer_peek_token (parser->lexer);
25576 t = cp_parser_assignment_expression (parser, false, NULL);
25578 if (t == error_mark_node)
25579 goto resync_fail;
25580 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25581 error_at (token->location, "schedule %<runtime%> does not take "
25582 "a %<chunk_size%> parameter");
25583 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25584 error_at (token->location, "schedule %<auto%> does not take "
25585 "a %<chunk_size%> parameter");
25586 else
25587 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25589 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25590 goto resync_fail;
25592 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25593 goto resync_fail;
25595 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25596 OMP_CLAUSE_CHAIN (c) = list;
25597 return c;
25599 invalid_kind:
25600 cp_parser_error (parser, "invalid schedule kind");
25601 resync_fail:
25602 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25603 /*or_comma=*/false,
25604 /*consume_paren=*/true);
25605 return list;
25608 /* OpenMP 3.0:
25609 untied */
25611 static tree
25612 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25613 tree list, location_t location)
25615 tree c;
25617 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25619 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25620 OMP_CLAUSE_CHAIN (c) = list;
25621 return c;
25624 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25625 is a bitmask in MASK. Return the list of clauses found; the result
25626 of clause default goes in *pdefault. */
25628 static tree
25629 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25630 const char *where, cp_token *pragma_tok)
25632 tree clauses = NULL;
25633 bool first = true;
25634 cp_token *token = NULL;
25636 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25638 pragma_omp_clause c_kind;
25639 const char *c_name;
25640 tree prev = clauses;
25642 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25643 cp_lexer_consume_token (parser->lexer);
25645 token = cp_lexer_peek_token (parser->lexer);
25646 c_kind = cp_parser_omp_clause_name (parser);
25647 first = false;
25649 switch (c_kind)
25651 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25652 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25653 token->location);
25654 c_name = "collapse";
25655 break;
25656 case PRAGMA_OMP_CLAUSE_COPYIN:
25657 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25658 c_name = "copyin";
25659 break;
25660 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25661 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25662 clauses);
25663 c_name = "copyprivate";
25664 break;
25665 case PRAGMA_OMP_CLAUSE_DEFAULT:
25666 clauses = cp_parser_omp_clause_default (parser, clauses,
25667 token->location);
25668 c_name = "default";
25669 break;
25670 case PRAGMA_OMP_CLAUSE_FINAL:
25671 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25672 c_name = "final";
25673 break;
25674 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25675 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25676 clauses);
25677 c_name = "firstprivate";
25678 break;
25679 case PRAGMA_OMP_CLAUSE_IF:
25680 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25681 c_name = "if";
25682 break;
25683 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25684 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25685 clauses);
25686 c_name = "lastprivate";
25687 break;
25688 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25689 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25690 token->location);
25691 c_name = "mergeable";
25692 break;
25693 case PRAGMA_OMP_CLAUSE_NOWAIT:
25694 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25695 c_name = "nowait";
25696 break;
25697 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25698 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25699 token->location);
25700 c_name = "num_threads";
25701 break;
25702 case PRAGMA_OMP_CLAUSE_ORDERED:
25703 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25704 token->location);
25705 c_name = "ordered";
25706 break;
25707 case PRAGMA_OMP_CLAUSE_PRIVATE:
25708 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25709 clauses);
25710 c_name = "private";
25711 break;
25712 case PRAGMA_OMP_CLAUSE_REDUCTION:
25713 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25714 c_name = "reduction";
25715 break;
25716 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25717 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25718 token->location);
25719 c_name = "schedule";
25720 break;
25721 case PRAGMA_OMP_CLAUSE_SHARED:
25722 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25723 clauses);
25724 c_name = "shared";
25725 break;
25726 case PRAGMA_OMP_CLAUSE_UNTIED:
25727 clauses = cp_parser_omp_clause_untied (parser, clauses,
25728 token->location);
25729 c_name = "nowait";
25730 break;
25731 default:
25732 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25733 goto saw_error;
25736 if (((mask >> c_kind) & 1) == 0)
25738 /* Remove the invalid clause(s) from the list to avoid
25739 confusing the rest of the compiler. */
25740 clauses = prev;
25741 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25744 saw_error:
25745 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25746 return finish_omp_clauses (clauses);
25749 /* OpenMP 2.5:
25750 structured-block:
25751 statement
25753 In practice, we're also interested in adding the statement to an
25754 outer node. So it is convenient if we work around the fact that
25755 cp_parser_statement calls add_stmt. */
25757 static unsigned
25758 cp_parser_begin_omp_structured_block (cp_parser *parser)
25760 unsigned save = parser->in_statement;
25762 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25763 This preserves the "not within loop or switch" style error messages
25764 for nonsense cases like
25765 void foo() {
25766 #pragma omp single
25767 break;
25770 if (parser->in_statement)
25771 parser->in_statement = IN_OMP_BLOCK;
25773 return save;
25776 static void
25777 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25779 parser->in_statement = save;
25782 static tree
25783 cp_parser_omp_structured_block (cp_parser *parser)
25785 tree stmt = begin_omp_structured_block ();
25786 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25788 cp_parser_statement (parser, NULL_TREE, false, NULL);
25790 cp_parser_end_omp_structured_block (parser, save);
25791 return finish_omp_structured_block (stmt);
25794 /* OpenMP 2.5:
25795 # pragma omp atomic new-line
25796 expression-stmt
25798 expression-stmt:
25799 x binop= expr | x++ | ++x | x-- | --x
25800 binop:
25801 +, *, -, /, &, ^, |, <<, >>
25803 where x is an lvalue expression with scalar type.
25805 OpenMP 3.1:
25806 # pragma omp atomic new-line
25807 update-stmt
25809 # pragma omp atomic read new-line
25810 read-stmt
25812 # pragma omp atomic write new-line
25813 write-stmt
25815 # pragma omp atomic update new-line
25816 update-stmt
25818 # pragma omp atomic capture new-line
25819 capture-stmt
25821 # pragma omp atomic capture new-line
25822 capture-block
25824 read-stmt:
25825 v = x
25826 write-stmt:
25827 x = expr
25828 update-stmt:
25829 expression-stmt | x = x binop expr
25830 capture-stmt:
25831 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25832 capture-block:
25833 { v = x; update-stmt; } | { update-stmt; v = x; }
25835 where x and v are lvalue expressions with scalar type. */
25837 static void
25838 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25840 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25841 tree rhs1 = NULL_TREE, orig_lhs;
25842 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25843 bool structured_block = false;
25845 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25848 const char *p = IDENTIFIER_POINTER (id);
25850 if (!strcmp (p, "read"))
25851 code = OMP_ATOMIC_READ;
25852 else if (!strcmp (p, "write"))
25853 code = NOP_EXPR;
25854 else if (!strcmp (p, "update"))
25855 code = OMP_ATOMIC;
25856 else if (!strcmp (p, "capture"))
25857 code = OMP_ATOMIC_CAPTURE_NEW;
25858 else
25859 p = NULL;
25860 if (p)
25861 cp_lexer_consume_token (parser->lexer);
25863 cp_parser_require_pragma_eol (parser, pragma_tok);
25865 switch (code)
25867 case OMP_ATOMIC_READ:
25868 case NOP_EXPR: /* atomic write */
25869 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25870 /*cast_p=*/false, NULL);
25871 if (v == error_mark_node)
25872 goto saw_error;
25873 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25874 goto saw_error;
25875 if (code == NOP_EXPR)
25876 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25877 else
25878 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25879 /*cast_p=*/false, NULL);
25880 if (lhs == error_mark_node)
25881 goto saw_error;
25882 if (code == NOP_EXPR)
25884 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25885 opcode. */
25886 code = OMP_ATOMIC;
25887 rhs = lhs;
25888 lhs = v;
25889 v = NULL_TREE;
25891 goto done;
25892 case OMP_ATOMIC_CAPTURE_NEW:
25893 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25895 cp_lexer_consume_token (parser->lexer);
25896 structured_block = true;
25898 else
25900 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25901 /*cast_p=*/false, NULL);
25902 if (v == error_mark_node)
25903 goto saw_error;
25904 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25905 goto saw_error;
25907 default:
25908 break;
25911 restart:
25912 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25913 /*cast_p=*/false, NULL);
25914 orig_lhs = lhs;
25915 switch (TREE_CODE (lhs))
25917 case ERROR_MARK:
25918 goto saw_error;
25920 case POSTINCREMENT_EXPR:
25921 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25922 code = OMP_ATOMIC_CAPTURE_OLD;
25923 /* FALLTHROUGH */
25924 case PREINCREMENT_EXPR:
25925 lhs = TREE_OPERAND (lhs, 0);
25926 opcode = PLUS_EXPR;
25927 rhs = integer_one_node;
25928 break;
25930 case POSTDECREMENT_EXPR:
25931 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25932 code = OMP_ATOMIC_CAPTURE_OLD;
25933 /* FALLTHROUGH */
25934 case PREDECREMENT_EXPR:
25935 lhs = TREE_OPERAND (lhs, 0);
25936 opcode = MINUS_EXPR;
25937 rhs = integer_one_node;
25938 break;
25940 case COMPOUND_EXPR:
25941 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25942 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25943 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25944 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25945 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25946 (TREE_OPERAND (lhs, 1), 0), 0)))
25947 == BOOLEAN_TYPE)
25948 /* Undo effects of boolean_increment for post {in,de}crement. */
25949 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25950 /* FALLTHRU */
25951 case MODIFY_EXPR:
25952 if (TREE_CODE (lhs) == MODIFY_EXPR
25953 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25955 /* Undo effects of boolean_increment. */
25956 if (integer_onep (TREE_OPERAND (lhs, 1)))
25958 /* This is pre or post increment. */
25959 rhs = TREE_OPERAND (lhs, 1);
25960 lhs = TREE_OPERAND (lhs, 0);
25961 opcode = NOP_EXPR;
25962 if (code == OMP_ATOMIC_CAPTURE_NEW
25963 && !structured_block
25964 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25965 code = OMP_ATOMIC_CAPTURE_OLD;
25966 break;
25969 /* FALLTHRU */
25970 default:
25971 switch (cp_lexer_peek_token (parser->lexer)->type)
25973 case CPP_MULT_EQ:
25974 opcode = MULT_EXPR;
25975 break;
25976 case CPP_DIV_EQ:
25977 opcode = TRUNC_DIV_EXPR;
25978 break;
25979 case CPP_PLUS_EQ:
25980 opcode = PLUS_EXPR;
25981 break;
25982 case CPP_MINUS_EQ:
25983 opcode = MINUS_EXPR;
25984 break;
25985 case CPP_LSHIFT_EQ:
25986 opcode = LSHIFT_EXPR;
25987 break;
25988 case CPP_RSHIFT_EQ:
25989 opcode = RSHIFT_EXPR;
25990 break;
25991 case CPP_AND_EQ:
25992 opcode = BIT_AND_EXPR;
25993 break;
25994 case CPP_OR_EQ:
25995 opcode = BIT_IOR_EXPR;
25996 break;
25997 case CPP_XOR_EQ:
25998 opcode = BIT_XOR_EXPR;
25999 break;
26000 case CPP_EQ:
26001 if (structured_block || code == OMP_ATOMIC)
26003 enum cp_parser_prec oprec;
26004 cp_token *token;
26005 cp_lexer_consume_token (parser->lexer);
26006 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26007 /*cast_p=*/false, NULL);
26008 if (rhs1 == error_mark_node)
26009 goto saw_error;
26010 token = cp_lexer_peek_token (parser->lexer);
26011 switch (token->type)
26013 case CPP_SEMICOLON:
26014 if (code == OMP_ATOMIC_CAPTURE_NEW)
26016 code = OMP_ATOMIC_CAPTURE_OLD;
26017 v = lhs;
26018 lhs = NULL_TREE;
26019 lhs1 = rhs1;
26020 rhs1 = NULL_TREE;
26021 cp_lexer_consume_token (parser->lexer);
26022 goto restart;
26024 cp_parser_error (parser,
26025 "invalid form of %<#pragma omp atomic%>");
26026 goto saw_error;
26027 case CPP_MULT:
26028 opcode = MULT_EXPR;
26029 break;
26030 case CPP_DIV:
26031 opcode = TRUNC_DIV_EXPR;
26032 break;
26033 case CPP_PLUS:
26034 opcode = PLUS_EXPR;
26035 break;
26036 case CPP_MINUS:
26037 opcode = MINUS_EXPR;
26038 break;
26039 case CPP_LSHIFT:
26040 opcode = LSHIFT_EXPR;
26041 break;
26042 case CPP_RSHIFT:
26043 opcode = RSHIFT_EXPR;
26044 break;
26045 case CPP_AND:
26046 opcode = BIT_AND_EXPR;
26047 break;
26048 case CPP_OR:
26049 opcode = BIT_IOR_EXPR;
26050 break;
26051 case CPP_XOR:
26052 opcode = BIT_XOR_EXPR;
26053 break;
26054 default:
26055 cp_parser_error (parser,
26056 "invalid operator for %<#pragma omp atomic%>");
26057 goto saw_error;
26059 oprec = TOKEN_PRECEDENCE (token);
26060 gcc_assert (oprec != PREC_NOT_OPERATOR);
26061 if (commutative_tree_code (opcode))
26062 oprec = (enum cp_parser_prec) (oprec - 1);
26063 cp_lexer_consume_token (parser->lexer);
26064 rhs = cp_parser_binary_expression (parser, false, false,
26065 oprec, NULL);
26066 if (rhs == error_mark_node)
26067 goto saw_error;
26068 goto stmt_done;
26070 /* FALLTHROUGH */
26071 default:
26072 cp_parser_error (parser,
26073 "invalid operator for %<#pragma omp atomic%>");
26074 goto saw_error;
26076 cp_lexer_consume_token (parser->lexer);
26078 rhs = cp_parser_expression (parser, false, NULL);
26079 if (rhs == error_mark_node)
26080 goto saw_error;
26081 break;
26083 stmt_done:
26084 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26086 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26087 goto saw_error;
26088 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26089 /*cast_p=*/false, NULL);
26090 if (v == error_mark_node)
26091 goto saw_error;
26092 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26093 goto saw_error;
26094 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26095 /*cast_p=*/false, NULL);
26096 if (lhs1 == error_mark_node)
26097 goto saw_error;
26099 if (structured_block)
26101 cp_parser_consume_semicolon_at_end_of_statement (parser);
26102 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26104 done:
26105 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26106 if (!structured_block)
26107 cp_parser_consume_semicolon_at_end_of_statement (parser);
26108 return;
26110 saw_error:
26111 cp_parser_skip_to_end_of_block_or_statement (parser);
26112 if (structured_block)
26114 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26115 cp_lexer_consume_token (parser->lexer);
26116 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26118 cp_parser_skip_to_end_of_block_or_statement (parser);
26119 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26120 cp_lexer_consume_token (parser->lexer);
26126 /* OpenMP 2.5:
26127 # pragma omp barrier new-line */
26129 static void
26130 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26132 cp_parser_require_pragma_eol (parser, pragma_tok);
26133 finish_omp_barrier ();
26136 /* OpenMP 2.5:
26137 # pragma omp critical [(name)] new-line
26138 structured-block */
26140 static tree
26141 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26143 tree stmt, name = NULL;
26145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26147 cp_lexer_consume_token (parser->lexer);
26149 name = cp_parser_identifier (parser);
26151 if (name == error_mark_node
26152 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26154 /*or_comma=*/false,
26155 /*consume_paren=*/true);
26156 if (name == error_mark_node)
26157 name = NULL;
26159 cp_parser_require_pragma_eol (parser, pragma_tok);
26161 stmt = cp_parser_omp_structured_block (parser);
26162 return c_finish_omp_critical (input_location, stmt, name);
26165 /* OpenMP 2.5:
26166 # pragma omp flush flush-vars[opt] new-line
26168 flush-vars:
26169 ( variable-list ) */
26171 static void
26172 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26174 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26175 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26176 cp_parser_require_pragma_eol (parser, pragma_tok);
26178 finish_omp_flush ();
26181 /* Helper function, to parse omp for increment expression. */
26183 static tree
26184 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26186 tree cond = cp_parser_binary_expression (parser, false, true,
26187 PREC_NOT_OPERATOR, NULL);
26188 if (cond == error_mark_node
26189 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26191 cp_parser_skip_to_end_of_statement (parser);
26192 return error_mark_node;
26195 switch (TREE_CODE (cond))
26197 case GT_EXPR:
26198 case GE_EXPR:
26199 case LT_EXPR:
26200 case LE_EXPR:
26201 break;
26202 default:
26203 return error_mark_node;
26206 /* If decl is an iterator, preserve LHS and RHS of the relational
26207 expr until finish_omp_for. */
26208 if (decl
26209 && (type_dependent_expression_p (decl)
26210 || CLASS_TYPE_P (TREE_TYPE (decl))))
26211 return cond;
26213 return build_x_binary_op (input_location, TREE_CODE (cond),
26214 TREE_OPERAND (cond, 0), ERROR_MARK,
26215 TREE_OPERAND (cond, 1), ERROR_MARK,
26216 /*overload=*/NULL, tf_warning_or_error);
26219 /* Helper function, to parse omp for increment expression. */
26221 static tree
26222 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26224 cp_token *token = cp_lexer_peek_token (parser->lexer);
26225 enum tree_code op;
26226 tree lhs, rhs;
26227 cp_id_kind idk;
26228 bool decl_first;
26230 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26232 op = (token->type == CPP_PLUS_PLUS
26233 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26234 cp_lexer_consume_token (parser->lexer);
26235 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26236 if (lhs != decl)
26237 return error_mark_node;
26238 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26241 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26242 if (lhs != decl)
26243 return error_mark_node;
26245 token = cp_lexer_peek_token (parser->lexer);
26246 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26248 op = (token->type == CPP_PLUS_PLUS
26249 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26250 cp_lexer_consume_token (parser->lexer);
26251 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26254 op = cp_parser_assignment_operator_opt (parser);
26255 if (op == ERROR_MARK)
26256 return error_mark_node;
26258 if (op != NOP_EXPR)
26260 rhs = cp_parser_assignment_expression (parser, false, NULL);
26261 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26262 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26265 lhs = cp_parser_binary_expression (parser, false, false,
26266 PREC_ADDITIVE_EXPRESSION, NULL);
26267 token = cp_lexer_peek_token (parser->lexer);
26268 decl_first = lhs == decl;
26269 if (decl_first)
26270 lhs = NULL_TREE;
26271 if (token->type != CPP_PLUS
26272 && token->type != CPP_MINUS)
26273 return error_mark_node;
26277 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26278 cp_lexer_consume_token (parser->lexer);
26279 rhs = cp_parser_binary_expression (parser, false, false,
26280 PREC_ADDITIVE_EXPRESSION, NULL);
26281 token = cp_lexer_peek_token (parser->lexer);
26282 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26284 if (lhs == NULL_TREE)
26286 if (op == PLUS_EXPR)
26287 lhs = rhs;
26288 else
26289 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26290 tf_warning_or_error);
26292 else
26293 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26294 ERROR_MARK, NULL, tf_warning_or_error);
26297 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26299 if (!decl_first)
26301 if (rhs != decl || op == MINUS_EXPR)
26302 return error_mark_node;
26303 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26305 else
26306 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26308 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26311 /* Parse the restricted form of the for statement allowed by OpenMP. */
26313 static tree
26314 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26316 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26317 tree real_decl, initv, condv, incrv, declv;
26318 tree this_pre_body, cl;
26319 location_t loc_first;
26320 bool collapse_err = false;
26321 int i, collapse = 1, nbraces = 0;
26322 VEC(tree,gc) *for_block = make_tree_vector ();
26324 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26325 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26326 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26328 gcc_assert (collapse >= 1);
26330 declv = make_tree_vec (collapse);
26331 initv = make_tree_vec (collapse);
26332 condv = make_tree_vec (collapse);
26333 incrv = make_tree_vec (collapse);
26335 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26337 for (i = 0; i < collapse; i++)
26339 int bracecount = 0;
26340 bool add_private_clause = false;
26341 location_t loc;
26343 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26345 cp_parser_error (parser, "for statement expected");
26346 return NULL;
26348 loc = cp_lexer_consume_token (parser->lexer)->location;
26350 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26351 return NULL;
26353 init = decl = real_decl = NULL;
26354 this_pre_body = push_stmt_list ();
26355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26357 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26359 init-expr:
26360 var = lb
26361 integer-type var = lb
26362 random-access-iterator-type var = lb
26363 pointer-type var = lb
26365 cp_decl_specifier_seq type_specifiers;
26367 /* First, try to parse as an initialized declaration. See
26368 cp_parser_condition, from whence the bulk of this is copied. */
26370 cp_parser_parse_tentatively (parser);
26371 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26372 /*is_trailing_return=*/false,
26373 &type_specifiers);
26374 if (cp_parser_parse_definitely (parser))
26376 /* If parsing a type specifier seq succeeded, then this
26377 MUST be a initialized declaration. */
26378 tree asm_specification, attributes;
26379 cp_declarator *declarator;
26381 declarator = cp_parser_declarator (parser,
26382 CP_PARSER_DECLARATOR_NAMED,
26383 /*ctor_dtor_or_conv_p=*/NULL,
26384 /*parenthesized_p=*/NULL,
26385 /*member_p=*/false);
26386 attributes = cp_parser_attributes_opt (parser);
26387 asm_specification = cp_parser_asm_specification_opt (parser);
26389 if (declarator == cp_error_declarator)
26390 cp_parser_skip_to_end_of_statement (parser);
26392 else
26394 tree pushed_scope, auto_node;
26396 decl = start_decl (declarator, &type_specifiers,
26397 SD_INITIALIZED, attributes,
26398 /*prefix_attributes=*/NULL_TREE,
26399 &pushed_scope);
26401 auto_node = type_uses_auto (TREE_TYPE (decl));
26402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26404 if (cp_lexer_next_token_is (parser->lexer,
26405 CPP_OPEN_PAREN))
26406 error ("parenthesized initialization is not allowed in "
26407 "OpenMP %<for%> loop");
26408 else
26409 /* Trigger an error. */
26410 cp_parser_require (parser, CPP_EQ, RT_EQ);
26412 init = error_mark_node;
26413 cp_parser_skip_to_end_of_statement (parser);
26415 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26416 || type_dependent_expression_p (decl)
26417 || auto_node)
26419 bool is_direct_init, is_non_constant_init;
26421 init = cp_parser_initializer (parser,
26422 &is_direct_init,
26423 &is_non_constant_init);
26425 if (auto_node)
26427 TREE_TYPE (decl)
26428 = do_auto_deduction (TREE_TYPE (decl), init,
26429 auto_node);
26431 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26432 && !type_dependent_expression_p (decl))
26433 goto non_class;
26436 cp_finish_decl (decl, init, !is_non_constant_init,
26437 asm_specification,
26438 LOOKUP_ONLYCONVERTING);
26439 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26441 VEC_safe_push (tree, gc, for_block, this_pre_body);
26442 init = NULL_TREE;
26444 else
26445 init = pop_stmt_list (this_pre_body);
26446 this_pre_body = NULL_TREE;
26448 else
26450 /* Consume '='. */
26451 cp_lexer_consume_token (parser->lexer);
26452 init = cp_parser_assignment_expression (parser, false, NULL);
26454 non_class:
26455 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26456 init = error_mark_node;
26457 else
26458 cp_finish_decl (decl, NULL_TREE,
26459 /*init_const_expr_p=*/false,
26460 asm_specification,
26461 LOOKUP_ONLYCONVERTING);
26464 if (pushed_scope)
26465 pop_scope (pushed_scope);
26468 else
26470 cp_id_kind idk;
26471 /* If parsing a type specifier sequence failed, then
26472 this MUST be a simple expression. */
26473 cp_parser_parse_tentatively (parser);
26474 decl = cp_parser_primary_expression (parser, false, false,
26475 false, &idk);
26476 if (!cp_parser_error_occurred (parser)
26477 && decl
26478 && DECL_P (decl)
26479 && CLASS_TYPE_P (TREE_TYPE (decl)))
26481 tree rhs;
26483 cp_parser_parse_definitely (parser);
26484 cp_parser_require (parser, CPP_EQ, RT_EQ);
26485 rhs = cp_parser_assignment_expression (parser, false, NULL);
26486 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
26487 decl, NOP_EXPR,
26488 rhs,
26489 tf_warning_or_error));
26490 add_private_clause = true;
26492 else
26494 decl = NULL;
26495 cp_parser_abort_tentative_parse (parser);
26496 init = cp_parser_expression (parser, false, NULL);
26497 if (init)
26499 if (TREE_CODE (init) == MODIFY_EXPR
26500 || TREE_CODE (init) == MODOP_EXPR)
26501 real_decl = TREE_OPERAND (init, 0);
26506 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26507 if (this_pre_body)
26509 this_pre_body = pop_stmt_list (this_pre_body);
26510 if (pre_body)
26512 tree t = pre_body;
26513 pre_body = push_stmt_list ();
26514 add_stmt (t);
26515 add_stmt (this_pre_body);
26516 pre_body = pop_stmt_list (pre_body);
26518 else
26519 pre_body = this_pre_body;
26522 if (decl)
26523 real_decl = decl;
26524 if (par_clauses != NULL && real_decl != NULL_TREE)
26526 tree *c;
26527 for (c = par_clauses; *c ; )
26528 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26529 && OMP_CLAUSE_DECL (*c) == real_decl)
26531 error_at (loc, "iteration variable %qD"
26532 " should not be firstprivate", real_decl);
26533 *c = OMP_CLAUSE_CHAIN (*c);
26535 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26536 && OMP_CLAUSE_DECL (*c) == real_decl)
26538 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26539 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26540 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26541 OMP_CLAUSE_DECL (l) = real_decl;
26542 OMP_CLAUSE_CHAIN (l) = clauses;
26543 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26544 clauses = l;
26545 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26546 CP_OMP_CLAUSE_INFO (*c) = NULL;
26547 add_private_clause = false;
26549 else
26551 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26552 && OMP_CLAUSE_DECL (*c) == real_decl)
26553 add_private_clause = false;
26554 c = &OMP_CLAUSE_CHAIN (*c);
26558 if (add_private_clause)
26560 tree c;
26561 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26563 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26564 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26565 && OMP_CLAUSE_DECL (c) == decl)
26566 break;
26567 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26568 && OMP_CLAUSE_DECL (c) == decl)
26569 error_at (loc, "iteration variable %qD "
26570 "should not be firstprivate",
26571 decl);
26572 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26573 && OMP_CLAUSE_DECL (c) == decl)
26574 error_at (loc, "iteration variable %qD should not be reduction",
26575 decl);
26577 if (c == NULL)
26579 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26580 OMP_CLAUSE_DECL (c) = decl;
26581 c = finish_omp_clauses (c);
26582 if (c)
26584 OMP_CLAUSE_CHAIN (c) = clauses;
26585 clauses = c;
26590 cond = NULL;
26591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26592 cond = cp_parser_omp_for_cond (parser, decl);
26593 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26595 incr = NULL;
26596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26598 /* If decl is an iterator, preserve the operator on decl
26599 until finish_omp_for. */
26600 if (real_decl
26601 && ((processing_template_decl
26602 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26603 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26604 incr = cp_parser_omp_for_incr (parser, real_decl);
26605 else
26606 incr = cp_parser_expression (parser, false, NULL);
26609 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26610 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26611 /*or_comma=*/false,
26612 /*consume_paren=*/true);
26614 TREE_VEC_ELT (declv, i) = decl;
26615 TREE_VEC_ELT (initv, i) = init;
26616 TREE_VEC_ELT (condv, i) = cond;
26617 TREE_VEC_ELT (incrv, i) = incr;
26619 if (i == collapse - 1)
26620 break;
26622 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26623 in between the collapsed for loops to be still considered perfectly
26624 nested. Hopefully the final version clarifies this.
26625 For now handle (multiple) {'s and empty statements. */
26626 cp_parser_parse_tentatively (parser);
26629 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26630 break;
26631 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26633 cp_lexer_consume_token (parser->lexer);
26634 bracecount++;
26636 else if (bracecount
26637 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26638 cp_lexer_consume_token (parser->lexer);
26639 else
26641 loc = cp_lexer_peek_token (parser->lexer)->location;
26642 error_at (loc, "not enough collapsed for loops");
26643 collapse_err = true;
26644 cp_parser_abort_tentative_parse (parser);
26645 declv = NULL_TREE;
26646 break;
26649 while (1);
26651 if (declv)
26653 cp_parser_parse_definitely (parser);
26654 nbraces += bracecount;
26658 /* Note that we saved the original contents of this flag when we entered
26659 the structured block, and so we don't need to re-save it here. */
26660 parser->in_statement = IN_OMP_FOR;
26662 /* Note that the grammar doesn't call for a structured block here,
26663 though the loop as a whole is a structured block. */
26664 body = push_stmt_list ();
26665 cp_parser_statement (parser, NULL_TREE, false, NULL);
26666 body = pop_stmt_list (body);
26668 if (declv == NULL_TREE)
26669 ret = NULL_TREE;
26670 else
26671 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26672 pre_body, clauses);
26674 while (nbraces)
26676 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26678 cp_lexer_consume_token (parser->lexer);
26679 nbraces--;
26681 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26682 cp_lexer_consume_token (parser->lexer);
26683 else
26685 if (!collapse_err)
26687 error_at (cp_lexer_peek_token (parser->lexer)->location,
26688 "collapsed loops not perfectly nested");
26690 collapse_err = true;
26691 cp_parser_statement_seq_opt (parser, NULL);
26692 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26693 break;
26697 while (!VEC_empty (tree, for_block))
26698 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26699 release_tree_vector (for_block);
26701 return ret;
26704 /* OpenMP 2.5:
26705 #pragma omp for for-clause[optseq] new-line
26706 for-loop */
26708 #define OMP_FOR_CLAUSE_MASK \
26709 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26710 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26711 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26712 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26713 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26714 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26715 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26716 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26718 static tree
26719 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26721 tree clauses, sb, ret;
26722 unsigned int save;
26724 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26725 "#pragma omp for", pragma_tok);
26727 sb = begin_omp_structured_block ();
26728 save = cp_parser_begin_omp_structured_block (parser);
26730 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26732 cp_parser_end_omp_structured_block (parser, save);
26733 add_stmt (finish_omp_structured_block (sb));
26735 return ret;
26738 /* OpenMP 2.5:
26739 # pragma omp master new-line
26740 structured-block */
26742 static tree
26743 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26745 cp_parser_require_pragma_eol (parser, pragma_tok);
26746 return c_finish_omp_master (input_location,
26747 cp_parser_omp_structured_block (parser));
26750 /* OpenMP 2.5:
26751 # pragma omp ordered new-line
26752 structured-block */
26754 static tree
26755 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26757 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26758 cp_parser_require_pragma_eol (parser, pragma_tok);
26759 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26762 /* OpenMP 2.5:
26764 section-scope:
26765 { section-sequence }
26767 section-sequence:
26768 section-directive[opt] structured-block
26769 section-sequence section-directive structured-block */
26771 static tree
26772 cp_parser_omp_sections_scope (cp_parser *parser)
26774 tree stmt, substmt;
26775 bool error_suppress = false;
26776 cp_token *tok;
26778 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26779 return NULL_TREE;
26781 stmt = push_stmt_list ();
26783 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26785 unsigned save;
26787 substmt = begin_omp_structured_block ();
26788 save = cp_parser_begin_omp_structured_block (parser);
26790 while (1)
26792 cp_parser_statement (parser, NULL_TREE, false, NULL);
26794 tok = cp_lexer_peek_token (parser->lexer);
26795 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26796 break;
26797 if (tok->type == CPP_CLOSE_BRACE)
26798 break;
26799 if (tok->type == CPP_EOF)
26800 break;
26803 cp_parser_end_omp_structured_block (parser, save);
26804 substmt = finish_omp_structured_block (substmt);
26805 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26806 add_stmt (substmt);
26809 while (1)
26811 tok = cp_lexer_peek_token (parser->lexer);
26812 if (tok->type == CPP_CLOSE_BRACE)
26813 break;
26814 if (tok->type == CPP_EOF)
26815 break;
26817 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26819 cp_lexer_consume_token (parser->lexer);
26820 cp_parser_require_pragma_eol (parser, tok);
26821 error_suppress = false;
26823 else if (!error_suppress)
26825 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26826 error_suppress = true;
26829 substmt = cp_parser_omp_structured_block (parser);
26830 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26831 add_stmt (substmt);
26833 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26835 substmt = pop_stmt_list (stmt);
26837 stmt = make_node (OMP_SECTIONS);
26838 TREE_TYPE (stmt) = void_type_node;
26839 OMP_SECTIONS_BODY (stmt) = substmt;
26841 add_stmt (stmt);
26842 return stmt;
26845 /* OpenMP 2.5:
26846 # pragma omp sections sections-clause[optseq] newline
26847 sections-scope */
26849 #define OMP_SECTIONS_CLAUSE_MASK \
26850 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26851 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26852 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26853 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26854 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26856 static tree
26857 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26859 tree clauses, ret;
26861 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26862 "#pragma omp sections", pragma_tok);
26864 ret = cp_parser_omp_sections_scope (parser);
26865 if (ret)
26866 OMP_SECTIONS_CLAUSES (ret) = clauses;
26868 return ret;
26871 /* OpenMP 2.5:
26872 # pragma parallel parallel-clause new-line
26873 # pragma parallel for parallel-for-clause new-line
26874 # pragma parallel sections parallel-sections-clause new-line */
26876 #define OMP_PARALLEL_CLAUSE_MASK \
26877 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26878 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26879 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26880 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26881 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26882 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26883 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26884 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26886 static tree
26887 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26889 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26890 const char *p_name = "#pragma omp parallel";
26891 tree stmt, clauses, par_clause, ws_clause, block;
26892 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26893 unsigned int save;
26894 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26898 cp_lexer_consume_token (parser->lexer);
26899 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26900 p_name = "#pragma omp parallel for";
26901 mask |= OMP_FOR_CLAUSE_MASK;
26902 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26904 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26906 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26907 const char *p = IDENTIFIER_POINTER (id);
26908 if (strcmp (p, "sections") == 0)
26910 cp_lexer_consume_token (parser->lexer);
26911 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26912 p_name = "#pragma omp parallel sections";
26913 mask |= OMP_SECTIONS_CLAUSE_MASK;
26914 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26918 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26919 block = begin_omp_parallel ();
26920 save = cp_parser_begin_omp_structured_block (parser);
26922 switch (p_kind)
26924 case PRAGMA_OMP_PARALLEL:
26925 cp_parser_statement (parser, NULL_TREE, false, NULL);
26926 par_clause = clauses;
26927 break;
26929 case PRAGMA_OMP_PARALLEL_FOR:
26930 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26931 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26932 break;
26934 case PRAGMA_OMP_PARALLEL_SECTIONS:
26935 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26936 stmt = cp_parser_omp_sections_scope (parser);
26937 if (stmt)
26938 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26939 break;
26941 default:
26942 gcc_unreachable ();
26945 cp_parser_end_omp_structured_block (parser, save);
26946 stmt = finish_omp_parallel (par_clause, block);
26947 if (p_kind != PRAGMA_OMP_PARALLEL)
26948 OMP_PARALLEL_COMBINED (stmt) = 1;
26949 return stmt;
26952 /* OpenMP 2.5:
26953 # pragma omp single single-clause[optseq] new-line
26954 structured-block */
26956 #define OMP_SINGLE_CLAUSE_MASK \
26957 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26958 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26959 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26960 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26962 static tree
26963 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26965 tree stmt = make_node (OMP_SINGLE);
26966 TREE_TYPE (stmt) = void_type_node;
26968 OMP_SINGLE_CLAUSES (stmt)
26969 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26970 "#pragma omp single", pragma_tok);
26971 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26973 return add_stmt (stmt);
26976 /* OpenMP 3.0:
26977 # pragma omp task task-clause[optseq] new-line
26978 structured-block */
26980 #define OMP_TASK_CLAUSE_MASK \
26981 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26982 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26983 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26984 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26985 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26986 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26987 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26988 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26990 static tree
26991 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26993 tree clauses, block;
26994 unsigned int save;
26996 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26997 "#pragma omp task", pragma_tok);
26998 block = begin_omp_task ();
26999 save = cp_parser_begin_omp_structured_block (parser);
27000 cp_parser_statement (parser, NULL_TREE, false, NULL);
27001 cp_parser_end_omp_structured_block (parser, save);
27002 return finish_omp_task (clauses, block);
27005 /* OpenMP 3.0:
27006 # pragma omp taskwait new-line */
27008 static void
27009 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27011 cp_parser_require_pragma_eol (parser, pragma_tok);
27012 finish_omp_taskwait ();
27015 /* OpenMP 3.1:
27016 # pragma omp taskyield new-line */
27018 static void
27019 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27021 cp_parser_require_pragma_eol (parser, pragma_tok);
27022 finish_omp_taskyield ();
27025 /* OpenMP 2.5:
27026 # pragma omp threadprivate (variable-list) */
27028 static void
27029 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27031 tree vars;
27033 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27034 cp_parser_require_pragma_eol (parser, pragma_tok);
27036 finish_omp_threadprivate (vars);
27039 /* Main entry point to OpenMP statement pragmas. */
27041 static void
27042 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27044 tree stmt;
27046 switch (pragma_tok->pragma_kind)
27048 case PRAGMA_OMP_ATOMIC:
27049 cp_parser_omp_atomic (parser, pragma_tok);
27050 return;
27051 case PRAGMA_OMP_CRITICAL:
27052 stmt = cp_parser_omp_critical (parser, pragma_tok);
27053 break;
27054 case PRAGMA_OMP_FOR:
27055 stmt = cp_parser_omp_for (parser, pragma_tok);
27056 break;
27057 case PRAGMA_OMP_MASTER:
27058 stmt = cp_parser_omp_master (parser, pragma_tok);
27059 break;
27060 case PRAGMA_OMP_ORDERED:
27061 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27062 break;
27063 case PRAGMA_OMP_PARALLEL:
27064 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27065 break;
27066 case PRAGMA_OMP_SECTIONS:
27067 stmt = cp_parser_omp_sections (parser, pragma_tok);
27068 break;
27069 case PRAGMA_OMP_SINGLE:
27070 stmt = cp_parser_omp_single (parser, pragma_tok);
27071 break;
27072 case PRAGMA_OMP_TASK:
27073 stmt = cp_parser_omp_task (parser, pragma_tok);
27074 break;
27075 default:
27076 gcc_unreachable ();
27079 if (stmt)
27080 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27083 /* Transactional Memory parsing routines. */
27085 /* Parse a transaction attribute.
27087 txn-attribute:
27088 attribute
27089 [ [ identifier ] ]
27091 ??? Simplify this when C++0x bracket attributes are
27092 implemented properly. */
27094 static tree
27095 cp_parser_txn_attribute_opt (cp_parser *parser)
27097 cp_token *token;
27098 tree attr_name, attr = NULL;
27100 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27101 return cp_parser_attributes_opt (parser);
27103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27104 return NULL_TREE;
27105 cp_lexer_consume_token (parser->lexer);
27106 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27107 goto error1;
27109 token = cp_lexer_peek_token (parser->lexer);
27110 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27112 token = cp_lexer_consume_token (parser->lexer);
27114 attr_name = (token->type == CPP_KEYWORD
27115 /* For keywords, use the canonical spelling,
27116 not the parsed identifier. */
27117 ? ridpointers[(int) token->keyword]
27118 : token->u.value);
27119 attr = build_tree_list (attr_name, NULL_TREE);
27121 else
27122 cp_parser_error (parser, "expected identifier");
27124 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27125 error1:
27126 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27127 return attr;
27130 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27132 transaction-statement:
27133 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27134 compound-statement
27135 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27138 static tree
27139 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27141 unsigned char old_in = parser->in_transaction;
27142 unsigned char this_in = 1, new_in;
27143 cp_token *token;
27144 tree stmt, attrs, noex;
27146 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27147 || keyword == RID_TRANSACTION_RELAXED);
27148 token = cp_parser_require_keyword (parser, keyword,
27149 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27150 : RT_TRANSACTION_RELAXED));
27151 gcc_assert (token != NULL);
27153 if (keyword == RID_TRANSACTION_RELAXED)
27154 this_in |= TM_STMT_ATTR_RELAXED;
27155 else
27157 attrs = cp_parser_txn_attribute_opt (parser);
27158 if (attrs)
27159 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27162 /* Parse a noexcept specification. */
27163 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27165 /* Keep track if we're in the lexical scope of an outer transaction. */
27166 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27168 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27170 parser->in_transaction = new_in;
27171 cp_parser_compound_statement (parser, NULL, false, false);
27172 parser->in_transaction = old_in;
27174 finish_transaction_stmt (stmt, NULL, this_in, noex);
27176 return stmt;
27179 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27181 transaction-expression:
27182 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27183 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27186 static tree
27187 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27189 unsigned char old_in = parser->in_transaction;
27190 unsigned char this_in = 1;
27191 cp_token *token;
27192 tree expr, noex;
27193 bool noex_expr;
27195 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27196 || keyword == RID_TRANSACTION_RELAXED);
27198 if (!flag_tm)
27199 error (keyword == RID_TRANSACTION_RELAXED
27200 ? G_("%<__transaction_relaxed%> without transactional memory "
27201 "support enabled")
27202 : G_("%<__transaction_atomic%> without transactional memory "
27203 "support enabled"));
27205 token = cp_parser_require_keyword (parser, keyword,
27206 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27207 : RT_TRANSACTION_RELAXED));
27208 gcc_assert (token != NULL);
27210 if (keyword == RID_TRANSACTION_RELAXED)
27211 this_in |= TM_STMT_ATTR_RELAXED;
27213 /* Set this early. This might mean that we allow transaction_cancel in
27214 an expression that we find out later actually has to be a constexpr.
27215 However, we expect that cxx_constant_value will be able to deal with
27216 this; also, if the noexcept has no constexpr, then what we parse next
27217 really is a transaction's body. */
27218 parser->in_transaction = this_in;
27220 /* Parse a noexcept specification. */
27221 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27222 true);
27224 if (!noex || !noex_expr
27225 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27227 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27229 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27230 finish_parenthesized_expr (expr);
27232 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27234 else
27236 /* The only expression that is available got parsed for the noexcept
27237 already. noexcept is true then. */
27238 expr = noex;
27239 noex = boolean_true_node;
27242 expr = build_transaction_expr (token->location, expr, this_in, noex);
27243 parser->in_transaction = old_in;
27245 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27246 return error_mark_node;
27248 return (flag_tm ? expr : error_mark_node);
27251 /* Parse a function-transaction-block.
27253 function-transaction-block:
27254 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27255 function-body
27256 __transaction_atomic txn-attribute[opt] function-try-block
27257 __transaction_relaxed ctor-initializer[opt] function-body
27258 __transaction_relaxed function-try-block
27261 static bool
27262 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27264 unsigned char old_in = parser->in_transaction;
27265 unsigned char new_in = 1;
27266 tree compound_stmt, stmt, attrs;
27267 bool ctor_initializer_p;
27268 cp_token *token;
27270 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27271 || keyword == RID_TRANSACTION_RELAXED);
27272 token = cp_parser_require_keyword (parser, keyword,
27273 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27274 : RT_TRANSACTION_RELAXED));
27275 gcc_assert (token != NULL);
27277 if (keyword == RID_TRANSACTION_RELAXED)
27278 new_in |= TM_STMT_ATTR_RELAXED;
27279 else
27281 attrs = cp_parser_txn_attribute_opt (parser);
27282 if (attrs)
27283 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27286 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27288 parser->in_transaction = new_in;
27290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27291 ctor_initializer_p = cp_parser_function_try_block (parser);
27292 else
27293 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27294 (parser, /*in_function_try_block=*/false);
27296 parser->in_transaction = old_in;
27298 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27300 return ctor_initializer_p;
27303 /* Parse a __transaction_cancel statement.
27305 cancel-statement:
27306 __transaction_cancel txn-attribute[opt] ;
27307 __transaction_cancel txn-attribute[opt] throw-expression ;
27309 ??? Cancel and throw is not yet implemented. */
27311 static tree
27312 cp_parser_transaction_cancel (cp_parser *parser)
27314 cp_token *token;
27315 bool is_outer = false;
27316 tree stmt, attrs;
27318 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27319 RT_TRANSACTION_CANCEL);
27320 gcc_assert (token != NULL);
27322 attrs = cp_parser_txn_attribute_opt (parser);
27323 if (attrs)
27324 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27326 /* ??? Parse cancel-and-throw here. */
27328 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27330 if (!flag_tm)
27332 error_at (token->location, "%<__transaction_cancel%> without "
27333 "transactional memory support enabled");
27334 return error_mark_node;
27336 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27338 error_at (token->location, "%<__transaction_cancel%> within a "
27339 "%<__transaction_relaxed%>");
27340 return error_mark_node;
27342 else if (is_outer)
27344 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27345 && !is_tm_may_cancel_outer (current_function_decl))
27347 error_at (token->location, "outer %<__transaction_cancel%> not "
27348 "within outer %<__transaction_atomic%>");
27349 error_at (token->location,
27350 " or a %<transaction_may_cancel_outer%> function");
27351 return error_mark_node;
27354 else if (parser->in_transaction == 0)
27356 error_at (token->location, "%<__transaction_cancel%> not within "
27357 "%<__transaction_atomic%>");
27358 return error_mark_node;
27361 stmt = build_tm_abort_call (token->location, is_outer);
27362 add_stmt (stmt);
27363 finish_stmt ();
27365 return stmt;
27368 /* The parser. */
27370 static GTY (()) cp_parser *the_parser;
27373 /* Special handling for the first token or line in the file. The first
27374 thing in the file might be #pragma GCC pch_preprocess, which loads a
27375 PCH file, which is a GC collection point. So we need to handle this
27376 first pragma without benefit of an existing lexer structure.
27378 Always returns one token to the caller in *FIRST_TOKEN. This is
27379 either the true first token of the file, or the first token after
27380 the initial pragma. */
27382 static void
27383 cp_parser_initial_pragma (cp_token *first_token)
27385 tree name = NULL;
27387 cp_lexer_get_preprocessor_token (NULL, first_token);
27388 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27389 return;
27391 cp_lexer_get_preprocessor_token (NULL, first_token);
27392 if (first_token->type == CPP_STRING)
27394 name = first_token->u.value;
27396 cp_lexer_get_preprocessor_token (NULL, first_token);
27397 if (first_token->type != CPP_PRAGMA_EOL)
27398 error_at (first_token->location,
27399 "junk at end of %<#pragma GCC pch_preprocess%>");
27401 else
27402 error_at (first_token->location, "expected string literal");
27404 /* Skip to the end of the pragma. */
27405 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27406 cp_lexer_get_preprocessor_token (NULL, first_token);
27408 /* Now actually load the PCH file. */
27409 if (name)
27410 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27412 /* Read one more token to return to our caller. We have to do this
27413 after reading the PCH file in, since its pointers have to be
27414 live. */
27415 cp_lexer_get_preprocessor_token (NULL, first_token);
27418 /* Normal parsing of a pragma token. Here we can (and must) use the
27419 regular lexer. */
27421 static bool
27422 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27424 cp_token *pragma_tok;
27425 unsigned int id;
27427 pragma_tok = cp_lexer_consume_token (parser->lexer);
27428 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27429 parser->lexer->in_pragma = true;
27431 id = pragma_tok->pragma_kind;
27432 switch (id)
27434 case PRAGMA_GCC_PCH_PREPROCESS:
27435 error_at (pragma_tok->location,
27436 "%<#pragma GCC pch_preprocess%> must be first");
27437 break;
27439 case PRAGMA_OMP_BARRIER:
27440 switch (context)
27442 case pragma_compound:
27443 cp_parser_omp_barrier (parser, pragma_tok);
27444 return false;
27445 case pragma_stmt:
27446 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27447 "used in compound statements");
27448 break;
27449 default:
27450 goto bad_stmt;
27452 break;
27454 case PRAGMA_OMP_FLUSH:
27455 switch (context)
27457 case pragma_compound:
27458 cp_parser_omp_flush (parser, pragma_tok);
27459 return false;
27460 case pragma_stmt:
27461 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27462 "used in compound statements");
27463 break;
27464 default:
27465 goto bad_stmt;
27467 break;
27469 case PRAGMA_OMP_TASKWAIT:
27470 switch (context)
27472 case pragma_compound:
27473 cp_parser_omp_taskwait (parser, pragma_tok);
27474 return false;
27475 case pragma_stmt:
27476 error_at (pragma_tok->location,
27477 "%<#pragma omp taskwait%> may only be "
27478 "used in compound statements");
27479 break;
27480 default:
27481 goto bad_stmt;
27483 break;
27485 case PRAGMA_OMP_TASKYIELD:
27486 switch (context)
27488 case pragma_compound:
27489 cp_parser_omp_taskyield (parser, pragma_tok);
27490 return false;
27491 case pragma_stmt:
27492 error_at (pragma_tok->location,
27493 "%<#pragma omp taskyield%> may only be "
27494 "used in compound statements");
27495 break;
27496 default:
27497 goto bad_stmt;
27499 break;
27501 case PRAGMA_OMP_THREADPRIVATE:
27502 cp_parser_omp_threadprivate (parser, pragma_tok);
27503 return false;
27505 case PRAGMA_OMP_ATOMIC:
27506 case PRAGMA_OMP_CRITICAL:
27507 case PRAGMA_OMP_FOR:
27508 case PRAGMA_OMP_MASTER:
27509 case PRAGMA_OMP_ORDERED:
27510 case PRAGMA_OMP_PARALLEL:
27511 case PRAGMA_OMP_SECTIONS:
27512 case PRAGMA_OMP_SINGLE:
27513 case PRAGMA_OMP_TASK:
27514 if (context == pragma_external)
27515 goto bad_stmt;
27516 cp_parser_omp_construct (parser, pragma_tok);
27517 return true;
27519 case PRAGMA_OMP_SECTION:
27520 error_at (pragma_tok->location,
27521 "%<#pragma omp section%> may only be used in "
27522 "%<#pragma omp sections%> construct");
27523 break;
27525 default:
27526 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27527 c_invoke_pragma_handler (id);
27528 break;
27530 bad_stmt:
27531 cp_parser_error (parser, "expected declaration specifiers");
27532 break;
27535 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27536 return false;
27539 /* The interface the pragma parsers have to the lexer. */
27541 enum cpp_ttype
27542 pragma_lex (tree *value)
27544 cp_token *tok;
27545 enum cpp_ttype ret;
27547 tok = cp_lexer_peek_token (the_parser->lexer);
27549 ret = tok->type;
27550 *value = tok->u.value;
27552 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27553 ret = CPP_EOF;
27554 else if (ret == CPP_STRING)
27555 *value = cp_parser_string_literal (the_parser, false, false);
27556 else
27558 cp_lexer_consume_token (the_parser->lexer);
27559 if (ret == CPP_KEYWORD)
27560 ret = CPP_NAME;
27563 return ret;
27567 /* External interface. */
27569 /* Parse one entire translation unit. */
27571 void
27572 c_parse_file (void)
27574 static bool already_called = false;
27576 if (already_called)
27578 sorry ("inter-module optimizations not implemented for C++");
27579 return;
27581 already_called = true;
27583 the_parser = cp_parser_new ();
27584 push_deferring_access_checks (flag_access_control
27585 ? dk_no_deferred : dk_no_check);
27586 cp_parser_translation_unit (the_parser);
27587 the_parser = NULL;
27590 #include "gt-cp-parser.h"