Merge branches/gcc-4_8-branch rev 216856
[official-gcc.git] / gcc-4_8-branch / gcc / cp / parser.c
bloba84bc59cef2273de8d045468858f3382e84fdac5
1 /* C++ Parser.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "target.h"
35 #include "cgraph.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "plugin.h"
39 #include "tree-pretty-print.h"
40 #include "parser.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 static cp_token eof_token =
50 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 /* The various kinds of non integral constant we encounter. */
54 typedef enum non_integral_constant {
55 NIC_NONE,
56 /* floating-point literal */
57 NIC_FLOAT,
58 /* %<this%> */
59 NIC_THIS,
60 /* %<__FUNCTION__%> */
61 NIC_FUNC_NAME,
62 /* %<__PRETTY_FUNCTION__%> */
63 NIC_PRETTY_FUNC,
64 /* %<__func__%> */
65 NIC_C99_FUNC,
66 /* "%<va_arg%> */
67 NIC_VA_ARG,
68 /* a cast */
69 NIC_CAST,
70 /* %<typeid%> operator */
71 NIC_TYPEID,
72 /* non-constant compound literals */
73 NIC_NCC,
74 /* a function call */
75 NIC_FUNC_CALL,
76 /* an increment */
77 NIC_INC,
78 /* an decrement */
79 NIC_DEC,
80 /* an array reference */
81 NIC_ARRAY_REF,
82 /* %<->%> */
83 NIC_ARROW,
84 /* %<.%> */
85 NIC_POINT,
86 /* the address of a label */
87 NIC_ADDR_LABEL,
88 /* %<*%> */
89 NIC_STAR,
90 /* %<&%> */
91 NIC_ADDR,
92 /* %<++%> */
93 NIC_PREINCREMENT,
94 /* %<--%> */
95 NIC_PREDECREMENT,
96 /* %<new%> */
97 NIC_NEW,
98 /* %<delete%> */
99 NIC_DEL,
100 /* calls to overloaded operators */
101 NIC_OVERLOADED,
102 /* an assignment */
103 NIC_ASSIGNMENT,
104 /* a comma operator */
105 NIC_COMMA,
106 /* a call to a constructor */
107 NIC_CONSTRUCTOR,
108 /* a transaction expression */
109 NIC_TRANSACTION
110 } non_integral_constant;
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114 /* NULL */
115 NLE_NULL,
116 /* is not a type */
117 NLE_TYPE,
118 /* is not a class or namespace */
119 NLE_CXX98,
120 /* is not a class, namespace, or enumeration */
121 NLE_NOT_CXX98
122 } name_lookup_error;
124 /* The various kinds of required token */
125 typedef enum required_token {
126 RT_NONE,
127 RT_SEMICOLON, /* ';' */
128 RT_OPEN_PAREN, /* '(' */
129 RT_CLOSE_BRACE, /* '}' */
130 RT_OPEN_BRACE, /* '{' */
131 RT_CLOSE_SQUARE, /* ']' */
132 RT_OPEN_SQUARE, /* '[' */
133 RT_COMMA, /* ',' */
134 RT_SCOPE, /* '::' */
135 RT_LESS, /* '<' */
136 RT_GREATER, /* '>' */
137 RT_EQ, /* '=' */
138 RT_ELLIPSIS, /* '...' */
139 RT_MULT, /* '*' */
140 RT_COMPL, /* '~' */
141 RT_COLON, /* ':' */
142 RT_COLON_SCOPE, /* ':' or '::' */
143 RT_CLOSE_PAREN, /* ')' */
144 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145 RT_PRAGMA_EOL, /* end of line */
146 RT_NAME, /* identifier */
148 /* The type is CPP_KEYWORD */
149 RT_NEW, /* new */
150 RT_DELETE, /* delete */
151 RT_RETURN, /* return */
152 RT_WHILE, /* while */
153 RT_EXTERN, /* extern */
154 RT_STATIC_ASSERT, /* static_assert */
155 RT_DECLTYPE, /* decltype */
156 RT_OPERATOR, /* operator */
157 RT_CLASS, /* class */
158 RT_TEMPLATE, /* template */
159 RT_NAMESPACE, /* namespace */
160 RT_USING, /* using */
161 RT_ASM, /* asm */
162 RT_TRY, /* try */
163 RT_CATCH, /* catch */
164 RT_THROW, /* throw */
165 RT_LABEL, /* __label__ */
166 RT_AT_TRY, /* @try */
167 RT_AT_SYNCHRONIZED, /* @synchronized */
168 RT_AT_THROW, /* @throw */
170 RT_SELECT, /* selection-statement */
171 RT_INTERATION, /* iteration-statement */
172 RT_JUMP, /* jump-statement */
173 RT_CLASS_KEY, /* class-key */
174 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
175 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
176 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
177 RT_TRANSACTION_CANCEL /* __transaction_cancel */
178 } required_token;
180 /* Prototypes. */
182 static cp_lexer *cp_lexer_new_main
183 (void);
184 static cp_lexer *cp_lexer_new_from_tokens
185 (cp_token_cache *tokens);
186 static void cp_lexer_destroy
187 (cp_lexer *);
188 static int cp_lexer_saving_tokens
189 (const cp_lexer *);
190 static cp_token *cp_lexer_token_at
191 (cp_lexer *, cp_token_position);
192 static void cp_lexer_get_preprocessor_token
193 (cp_lexer *, cp_token *);
194 static inline cp_token *cp_lexer_peek_token
195 (cp_lexer *);
196 static cp_token *cp_lexer_peek_nth_token
197 (cp_lexer *, size_t);
198 static inline bool cp_lexer_next_token_is
199 (cp_lexer *, enum cpp_ttype);
200 static bool cp_lexer_next_token_is_not
201 (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_keyword
203 (cp_lexer *, enum rid);
204 static cp_token *cp_lexer_consume_token
205 (cp_lexer *);
206 static void cp_lexer_purge_token
207 (cp_lexer *);
208 static void cp_lexer_purge_tokens_after
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_save_tokens
211 (cp_lexer *);
212 static void cp_lexer_commit_tokens
213 (cp_lexer *);
214 static void cp_lexer_rollback_tokens
215 (cp_lexer *);
216 static void cp_lexer_print_token
217 (FILE *, cp_token *);
218 static inline bool cp_lexer_debugging_p
219 (cp_lexer *);
220 static void cp_lexer_start_debugging
221 (cp_lexer *) ATTRIBUTE_UNUSED;
222 static void cp_lexer_stop_debugging
223 (cp_lexer *) ATTRIBUTE_UNUSED;
225 static cp_token_cache *cp_token_cache_new
226 (cp_token *, cp_token *);
228 static void cp_parser_initial_pragma
229 (cp_token *);
231 static tree cp_literal_operator_id
232 (const char *);
234 /* Manifest constants. */
235 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
236 #define CP_SAVED_TOKEN_STACK 5
238 /* Variables. */
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
243 /* Nonzero if we are parsing an unevaluated operand: an operand to
244 sizeof, typeof, or alignof. */
245 int cp_unevaluated_operand;
247 /* Dump up to NUM tokens in BUFFER to FILE starting with token
248 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
249 first token in BUFFER. If NUM is 0, dump all the tokens. If
250 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
251 highlighted by surrounding it in [[ ]]. */
253 static void
254 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
255 cp_token *start_token, unsigned num,
256 cp_token *curr_token)
258 unsigned i, nprinted;
259 cp_token *token;
260 bool do_print;
262 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
264 if (buffer == NULL)
265 return;
267 if (num == 0)
268 num = buffer->length ();
270 if (start_token == NULL)
271 start_token = buffer->address ();
273 if (start_token > buffer->address ())
275 cp_lexer_print_token (file, &(*buffer)[0]);
276 fprintf (file, " ... ");
279 do_print = false;
280 nprinted = 0;
281 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
283 if (token == start_token)
284 do_print = true;
286 if (!do_print)
287 continue;
289 nprinted++;
290 if (token == curr_token)
291 fprintf (file, "[[");
293 cp_lexer_print_token (file, token);
295 if (token == curr_token)
296 fprintf (file, "]]");
298 switch (token->type)
300 case CPP_SEMICOLON:
301 case CPP_OPEN_BRACE:
302 case CPP_CLOSE_BRACE:
303 case CPP_EOF:
304 fputc ('\n', file);
305 break;
307 default:
308 fputc (' ', file);
312 if (i == num && i < buffer->length ())
314 fprintf (file, " ... ");
315 cp_lexer_print_token (file, &buffer->last ());
318 fprintf (file, "\n");
322 /* Dump all tokens in BUFFER to stderr. */
324 void
325 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
327 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
332 description for T. */
334 static void
335 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
337 if (t)
339 fprintf (file, "%s: ", desc);
340 print_node_brief (file, "", t, 0);
345 /* Dump parser context C to FILE. */
347 static void
348 cp_debug_print_context (FILE *file, cp_parser_context *c)
350 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
351 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
352 print_node_brief (file, "", c->object_type, 0);
353 fprintf (file, "}\n");
357 /* Print the stack of parsing contexts to FILE starting with FIRST. */
359 static void
360 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
362 unsigned i;
363 cp_parser_context *c;
365 fprintf (file, "Parsing context stack:\n");
366 for (i = 0, c = first; c; c = c->next, i++)
368 fprintf (file, "\t#%u: ", i);
369 cp_debug_print_context (file, c);
374 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
376 static void
377 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
379 if (flag)
380 fprintf (file, "%s: true\n", desc);
384 /* Print an unparsed function entry UF to FILE. */
386 static void
387 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
389 unsigned i;
390 cp_default_arg_entry *default_arg_fn;
391 tree fn;
393 fprintf (file, "\tFunctions with default args:\n");
394 for (i = 0;
395 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
396 i++)
398 fprintf (file, "\t\tClass type: ");
399 print_node_brief (file, "", default_arg_fn->class_type, 0);
400 fprintf (file, "\t\tDeclaration: ");
401 print_node_brief (file, "", default_arg_fn->decl, 0);
402 fprintf (file, "\n");
405 fprintf (file, "\n\tFunctions with definitions that require "
406 "post-processing\n\t\t");
407 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
409 print_node_brief (file, "", fn, 0);
410 fprintf (file, " ");
412 fprintf (file, "\n");
414 fprintf (file, "\n\tNon-static data members with initializers that require "
415 "post-processing\n\t\t");
416 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
418 print_node_brief (file, "", fn, 0);
419 fprintf (file, " ");
421 fprintf (file, "\n");
425 /* Print the stack of unparsed member functions S to FILE. */
427 static void
428 cp_debug_print_unparsed_queues (FILE *file,
429 vec<cp_unparsed_functions_entry, va_gc> *s)
431 unsigned i;
432 cp_unparsed_functions_entry *uf;
434 fprintf (file, "Unparsed functions\n");
435 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
437 fprintf (file, "#%u:\n", i);
438 cp_debug_print_unparsed_function (file, uf);
443 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
444 the given PARSER. If FILE is NULL, the output is printed on stderr. */
446 static void
447 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
449 cp_token *next_token, *first_token, *start_token;
451 if (file == NULL)
452 file = stderr;
454 next_token = parser->lexer->next_token;
455 first_token = parser->lexer->buffer->address ();
456 start_token = (next_token > first_token + window_size / 2)
457 ? next_token - window_size / 2
458 : first_token;
459 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
460 next_token);
464 /* Dump debugging information for the given PARSER. If FILE is NULL,
465 the output is printed on stderr. */
467 void
468 cp_debug_parser (FILE *file, cp_parser *parser)
470 const size_t window_size = 20;
471 cp_token *token;
472 expanded_location eloc;
474 if (file == NULL)
475 file = stderr;
477 fprintf (file, "Parser state\n\n");
478 fprintf (file, "Number of tokens: %u\n",
479 vec_safe_length (parser->lexer->buffer));
480 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
481 cp_debug_print_tree_if_set (file, "Object scope",
482 parser->object_scope);
483 cp_debug_print_tree_if_set (file, "Qualifying scope",
484 parser->qualifying_scope);
485 cp_debug_print_context_stack (file, parser->context);
486 cp_debug_print_flag (file, "Allow GNU extensions",
487 parser->allow_gnu_extensions_p);
488 cp_debug_print_flag (file, "'>' token is greater-than",
489 parser->greater_than_is_operator_p);
490 cp_debug_print_flag (file, "Default args allowed in current "
491 "parameter list", parser->default_arg_ok_p);
492 cp_debug_print_flag (file, "Parsing integral constant-expression",
493 parser->integral_constant_expression_p);
494 cp_debug_print_flag (file, "Allow non-constant expression in current "
495 "constant-expression",
496 parser->allow_non_integral_constant_expression_p);
497 cp_debug_print_flag (file, "Seen non-constant expression",
498 parser->non_integral_constant_expression_p);
499 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
500 "current context",
501 parser->local_variables_forbidden_p);
502 cp_debug_print_flag (file, "In unbraced linkage specification",
503 parser->in_unbraced_linkage_specification_p);
504 cp_debug_print_flag (file, "Parsing a declarator",
505 parser->in_declarator_p);
506 cp_debug_print_flag (file, "In template argument list",
507 parser->in_template_argument_list_p);
508 cp_debug_print_flag (file, "Parsing an iteration statement",
509 parser->in_statement & IN_ITERATION_STMT);
510 cp_debug_print_flag (file, "Parsing a switch statement",
511 parser->in_statement & IN_SWITCH_STMT);
512 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
513 parser->in_statement & IN_OMP_BLOCK);
514 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
515 parser->in_statement & IN_OMP_FOR);
516 cp_debug_print_flag (file, "Parsing an if statement",
517 parser->in_statement & IN_IF_STMT);
518 cp_debug_print_flag (file, "Parsing a type-id in an expression "
519 "context", parser->in_type_id_in_expr_p);
520 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
521 parser->implicit_extern_c);
522 cp_debug_print_flag (file, "String expressions should be translated "
523 "to execution character set",
524 parser->translate_strings_p);
525 cp_debug_print_flag (file, "Parsing function body outside of a "
526 "local class", parser->in_function_body);
527 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
528 parser->colon_corrects_to_scope_p);
529 if (parser->type_definition_forbidden_message)
530 fprintf (file, "Error message for forbidden type definitions: %s\n",
531 parser->type_definition_forbidden_message);
532 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
533 fprintf (file, "Number of class definitions in progress: %u\n",
534 parser->num_classes_being_defined);
535 fprintf (file, "Number of template parameter lists for the current "
536 "declaration: %u\n", parser->num_template_parameter_lists);
537 cp_debug_parser_tokens (file, parser, window_size);
538 token = parser->lexer->next_token;
539 fprintf (file, "Next token to parse:\n");
540 fprintf (file, "\tToken: ");
541 cp_lexer_print_token (file, token);
542 eloc = expand_location (token->location);
543 fprintf (file, "\n\tFile: %s\n", eloc.file);
544 fprintf (file, "\tLine: %d\n", eloc.line);
545 fprintf (file, "\tColumn: %d\n", eloc.column);
549 /* Allocate memory for a new lexer object and return it. */
551 static cp_lexer *
552 cp_lexer_alloc (void)
554 cp_lexer *lexer;
556 c_common_no_more_pch ();
558 /* Allocate the memory. */
559 lexer = ggc_alloc_cleared_cp_lexer ();
561 /* Initially we are not debugging. */
562 lexer->debugging_p = false;
564 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
566 /* Create the buffer. */
567 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
569 return lexer;
573 /* Create a new main C++ lexer, the lexer that gets tokens from the
574 preprocessor. */
576 static cp_lexer *
577 cp_lexer_new_main (void)
579 cp_lexer *lexer;
580 cp_token token;
582 /* It's possible that parsing the first pragma will load a PCH file,
583 which is a GC collection point. So we have to do that before
584 allocating any memory. */
585 cp_parser_initial_pragma (&token);
587 lexer = cp_lexer_alloc ();
589 /* Put the first token in the buffer. */
590 lexer->buffer->quick_push (token);
592 /* Get the remaining tokens from the preprocessor. */
593 while (token.type != CPP_EOF)
595 cp_lexer_get_preprocessor_token (lexer, &token);
596 vec_safe_push (lexer->buffer, token);
599 lexer->last_token = lexer->buffer->address ()
600 + lexer->buffer->length ()
601 - 1;
602 lexer->next_token = lexer->buffer->length ()
603 ? lexer->buffer->address ()
604 : &eof_token;
606 /* Subsequent preprocessor diagnostics should use compiler
607 diagnostic functions to get the compiler source location. */
608 done_lexing = true;
610 gcc_assert (!lexer->next_token->purged_p);
611 return lexer;
614 /* Create a new lexer whose token stream is primed with the tokens in
615 CACHE. When these tokens are exhausted, no new tokens will be read. */
617 static cp_lexer *
618 cp_lexer_new_from_tokens (cp_token_cache *cache)
620 cp_token *first = cache->first;
621 cp_token *last = cache->last;
622 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
624 /* We do not own the buffer. */
625 lexer->buffer = NULL;
626 lexer->next_token = first == last ? &eof_token : first;
627 lexer->last_token = last;
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
631 /* Initially we are not debugging. */
632 lexer->debugging_p = false;
634 gcc_assert (!lexer->next_token->purged_p);
635 return lexer;
638 /* Frees all resources associated with LEXER. */
640 static void
641 cp_lexer_destroy (cp_lexer *lexer)
643 vec_free (lexer->buffer);
644 lexer->saved_tokens.release ();
645 ggc_free (lexer);
648 /* Returns nonzero if debugging information should be output. */
650 static inline bool
651 cp_lexer_debugging_p (cp_lexer *lexer)
653 return lexer->debugging_p;
657 static inline cp_token_position
658 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
660 gcc_assert (!previous_p || lexer->next_token != &eof_token);
662 return lexer->next_token - previous_p;
665 static inline cp_token *
666 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
668 return pos;
671 static inline void
672 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
674 lexer->next_token = cp_lexer_token_at (lexer, pos);
677 static inline cp_token_position
678 cp_lexer_previous_token_position (cp_lexer *lexer)
680 if (lexer->next_token == &eof_token)
681 return lexer->last_token - 1;
682 else
683 return cp_lexer_token_position (lexer, true);
686 static inline cp_token *
687 cp_lexer_previous_token (cp_lexer *lexer)
689 cp_token_position tp = cp_lexer_previous_token_position (lexer);
691 return cp_lexer_token_at (lexer, tp);
694 /* nonzero if we are presently saving tokens. */
696 static inline int
697 cp_lexer_saving_tokens (const cp_lexer* lexer)
699 return lexer->saved_tokens.length () != 0;
702 /* Store the next token from the preprocessor in *TOKEN. Return true
703 if we reach EOF. If LEXER is NULL, assume we are handling an
704 initial #pragma pch_preprocess, and thus want the lexer to return
705 processed strings. */
707 static void
708 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
710 static int is_extern_c = 0;
712 /* Get a new token from the preprocessor. */
713 token->type
714 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
715 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
716 token->keyword = RID_MAX;
717 token->pragma_kind = PRAGMA_NONE;
718 token->purged_p = false;
720 /* On some systems, some header files are surrounded by an
721 implicit extern "C" block. Set a flag in the token if it
722 comes from such a header. */
723 is_extern_c += pending_lang_change;
724 pending_lang_change = 0;
725 token->implicit_extern_c = is_extern_c > 0;
727 /* Check to see if this token is a keyword. */
728 if (token->type == CPP_NAME)
730 if (C_IS_RESERVED_WORD (token->u.value))
732 /* Mark this token as a keyword. */
733 token->type = CPP_KEYWORD;
734 /* Record which keyword. */
735 token->keyword = C_RID_CODE (token->u.value);
737 else
739 if (warn_cxx0x_compat
740 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
741 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
743 /* Warn about the C++0x keyword (but still treat it as
744 an identifier). */
745 warning (OPT_Wc__0x_compat,
746 "identifier %qE is a keyword in C++11",
747 token->u.value);
749 /* Clear out the C_RID_CODE so we don't warn about this
750 particular identifier-turned-keyword again. */
751 C_SET_RID_CODE (token->u.value, RID_MAX);
754 token->ambiguous_p = false;
755 token->keyword = RID_MAX;
758 else if (token->type == CPP_AT_NAME)
760 /* This only happens in Objective-C++; it must be a keyword. */
761 token->type = CPP_KEYWORD;
762 switch (C_RID_CODE (token->u.value))
764 /* Replace 'class' with '@class', 'private' with '@private',
765 etc. This prevents confusion with the C++ keyword
766 'class', and makes the tokens consistent with other
767 Objective-C 'AT' keywords. For example '@class' is
768 reported as RID_AT_CLASS which is consistent with
769 '@synchronized', which is reported as
770 RID_AT_SYNCHRONIZED.
772 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
773 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
774 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
775 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
776 case RID_THROW: token->keyword = RID_AT_THROW; break;
777 case RID_TRY: token->keyword = RID_AT_TRY; break;
778 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
779 default: token->keyword = C_RID_CODE (token->u.value);
782 else if (token->type == CPP_PRAGMA)
784 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
785 token->pragma_kind = ((enum pragma_kind)
786 TREE_INT_CST_LOW (token->u.value));
787 token->u.value = NULL_TREE;
791 /* Update the globals input_location and the input file stack from TOKEN. */
792 static inline void
793 cp_lexer_set_source_position_from_token (cp_token *token)
795 if (token->type != CPP_EOF)
797 input_location = token->location;
801 /* Return a pointer to the next token in the token stream, but do not
802 consume it. */
804 static inline cp_token *
805 cp_lexer_peek_token (cp_lexer *lexer)
807 if (cp_lexer_debugging_p (lexer))
809 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
810 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
811 putc ('\n', cp_lexer_debug_stream);
813 return lexer->next_token;
816 /* Return true if the next token has the indicated TYPE. */
818 static inline bool
819 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
821 return cp_lexer_peek_token (lexer)->type == type;
824 /* Return true if the next token does not have the indicated TYPE. */
826 static inline bool
827 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
829 return !cp_lexer_next_token_is (lexer, type);
832 /* Return true if the next token is the indicated KEYWORD. */
834 static inline bool
835 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
837 return cp_lexer_peek_token (lexer)->keyword == keyword;
840 /* Return true if the next token is not the indicated KEYWORD. */
842 static inline bool
843 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
845 return cp_lexer_peek_token (lexer)->keyword != keyword;
848 /* Return true if the next token is a keyword for a decl-specifier. */
850 static bool
851 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
853 cp_token *token;
855 token = cp_lexer_peek_token (lexer);
856 switch (token->keyword)
858 /* auto specifier: storage-class-specifier in C++,
859 simple-type-specifier in C++0x. */
860 case RID_AUTO:
861 /* Storage classes. */
862 case RID_REGISTER:
863 case RID_STATIC:
864 case RID_EXTERN:
865 case RID_MUTABLE:
866 case RID_THREAD:
867 /* Elaborated type specifiers. */
868 case RID_ENUM:
869 case RID_CLASS:
870 case RID_STRUCT:
871 case RID_UNION:
872 case RID_TYPENAME:
873 /* Simple type specifiers. */
874 case RID_CHAR:
875 case RID_CHAR16:
876 case RID_CHAR32:
877 case RID_WCHAR:
878 case RID_BOOL:
879 case RID_SHORT:
880 case RID_INT:
881 case RID_LONG:
882 case RID_INT128:
883 case RID_SIGNED:
884 case RID_UNSIGNED:
885 case RID_FLOAT:
886 case RID_DOUBLE:
887 case RID_VOID:
888 /* GNU extensions. */
889 case RID_ATTRIBUTE:
890 case RID_TYPEOF:
891 /* C++0x extensions. */
892 case RID_DECLTYPE:
893 case RID_UNDERLYING_TYPE:
894 return true;
896 default:
897 return false;
901 /* Returns TRUE iff the token T begins a decltype type. */
903 static bool
904 token_is_decltype (cp_token *t)
906 return (t->keyword == RID_DECLTYPE
907 || t->type == CPP_DECLTYPE);
910 /* Returns TRUE iff the next token begins a decltype type. */
912 static bool
913 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
915 cp_token *t = cp_lexer_peek_token (lexer);
916 return token_is_decltype (t);
919 /* Return a pointer to the Nth token in the token stream. If N is 1,
920 then this is precisely equivalent to cp_lexer_peek_token (except
921 that it is not inline). One would like to disallow that case, but
922 there is one case (cp_parser_nth_token_starts_template_id) where
923 the caller passes a variable for N and it might be 1. */
925 static cp_token *
926 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
928 cp_token *token;
930 /* N is 1-based, not zero-based. */
931 gcc_assert (n > 0);
933 if (cp_lexer_debugging_p (lexer))
934 fprintf (cp_lexer_debug_stream,
935 "cp_lexer: peeking ahead %ld at token: ", (long)n);
937 --n;
938 token = lexer->next_token;
939 gcc_assert (!n || token != &eof_token);
940 while (n != 0)
942 ++token;
943 if (token == lexer->last_token)
945 token = &eof_token;
946 break;
949 if (!token->purged_p)
950 --n;
953 if (cp_lexer_debugging_p (lexer))
955 cp_lexer_print_token (cp_lexer_debug_stream, token);
956 putc ('\n', cp_lexer_debug_stream);
959 return token;
962 /* Return the next token, and advance the lexer's next_token pointer
963 to point to the next non-purged token. */
965 static cp_token *
966 cp_lexer_consume_token (cp_lexer* lexer)
968 cp_token *token = lexer->next_token;
970 gcc_assert (token != &eof_token);
971 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
975 lexer->next_token++;
976 if (lexer->next_token == lexer->last_token)
978 lexer->next_token = &eof_token;
979 break;
983 while (lexer->next_token->purged_p);
985 cp_lexer_set_source_position_from_token (token);
987 /* Provide debugging output. */
988 if (cp_lexer_debugging_p (lexer))
990 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
991 cp_lexer_print_token (cp_lexer_debug_stream, token);
992 putc ('\n', cp_lexer_debug_stream);
995 return token;
998 /* Permanently remove the next token from the token stream, and
999 advance the next_token pointer to refer to the next non-purged
1000 token. */
1002 static void
1003 cp_lexer_purge_token (cp_lexer *lexer)
1005 cp_token *tok = lexer->next_token;
1007 gcc_assert (tok != &eof_token);
1008 tok->purged_p = true;
1009 tok->location = UNKNOWN_LOCATION;
1010 tok->u.value = NULL_TREE;
1011 tok->keyword = RID_MAX;
1015 tok++;
1016 if (tok == lexer->last_token)
1018 tok = &eof_token;
1019 break;
1022 while (tok->purged_p);
1023 lexer->next_token = tok;
1026 /* Permanently remove all tokens after TOK, up to, but not
1027 including, the token that will be returned next by
1028 cp_lexer_peek_token. */
1030 static void
1031 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1033 cp_token *peek = lexer->next_token;
1035 if (peek == &eof_token)
1036 peek = lexer->last_token;
1038 gcc_assert (tok < peek);
1040 for ( tok += 1; tok != peek; tok += 1)
1042 tok->purged_p = true;
1043 tok->location = UNKNOWN_LOCATION;
1044 tok->u.value = NULL_TREE;
1045 tok->keyword = RID_MAX;
1049 /* Begin saving tokens. All tokens consumed after this point will be
1050 preserved. */
1052 static void
1053 cp_lexer_save_tokens (cp_lexer* lexer)
1055 /* Provide debugging output. */
1056 if (cp_lexer_debugging_p (lexer))
1057 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1059 lexer->saved_tokens.safe_push (lexer->next_token);
1062 /* Commit to the portion of the token stream most recently saved. */
1064 static void
1065 cp_lexer_commit_tokens (cp_lexer* lexer)
1067 /* Provide debugging output. */
1068 if (cp_lexer_debugging_p (lexer))
1069 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1071 lexer->saved_tokens.pop ();
1074 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1075 to the token stream. Stop saving tokens. */
1077 static void
1078 cp_lexer_rollback_tokens (cp_lexer* lexer)
1080 /* Provide debugging output. */
1081 if (cp_lexer_debugging_p (lexer))
1082 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1084 lexer->next_token = lexer->saved_tokens.pop ();
1087 /* Print a representation of the TOKEN on the STREAM. */
1089 static void
1090 cp_lexer_print_token (FILE * stream, cp_token *token)
1092 /* We don't use cpp_type2name here because the parser defines
1093 a few tokens of its own. */
1094 static const char *const token_names[] = {
1095 /* cpplib-defined token types */
1096 #define OP(e, s) #e,
1097 #define TK(e, s) #e,
1098 TTYPE_TABLE
1099 #undef OP
1100 #undef TK
1101 /* C++ parser token types - see "Manifest constants", above. */
1102 "KEYWORD",
1103 "TEMPLATE_ID",
1104 "NESTED_NAME_SPECIFIER",
1107 /* For some tokens, print the associated data. */
1108 switch (token->type)
1110 case CPP_KEYWORD:
1111 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1112 For example, `struct' is mapped to an INTEGER_CST. */
1113 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1114 break;
1115 /* else fall through */
1116 case CPP_NAME:
1117 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1118 break;
1120 case CPP_STRING:
1121 case CPP_STRING16:
1122 case CPP_STRING32:
1123 case CPP_WSTRING:
1124 case CPP_UTF8STRING:
1125 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1126 break;
1128 case CPP_NUMBER:
1129 print_generic_expr (stream, token->u.value, 0);
1130 break;
1132 default:
1133 /* If we have a name for the token, print it out. Otherwise, we
1134 simply give the numeric code. */
1135 if (token->type < ARRAY_SIZE(token_names))
1136 fputs (token_names[token->type], stream);
1137 else
1138 fprintf (stream, "[%d]", token->type);
1139 break;
1143 /* Start emitting debugging information. */
1145 static void
1146 cp_lexer_start_debugging (cp_lexer* lexer)
1148 lexer->debugging_p = true;
1149 cp_lexer_debug_stream = stderr;
1152 /* Stop emitting debugging information. */
1154 static void
1155 cp_lexer_stop_debugging (cp_lexer* lexer)
1157 lexer->debugging_p = false;
1158 cp_lexer_debug_stream = NULL;
1161 /* Create a new cp_token_cache, representing a range of tokens. */
1163 static cp_token_cache *
1164 cp_token_cache_new (cp_token *first, cp_token *last)
1166 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1167 cache->first = first;
1168 cache->last = last;
1169 return cache;
1173 /* Decl-specifiers. */
1175 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1177 static void
1178 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1180 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1183 /* Declarators. */
1185 /* Nothing other than the parser should be creating declarators;
1186 declarators are a semi-syntactic representation of C++ entities.
1187 Other parts of the front end that need to create entities (like
1188 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1190 static cp_declarator *make_call_declarator
1191 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1192 static cp_declarator *make_array_declarator
1193 (cp_declarator *, tree);
1194 static cp_declarator *make_pointer_declarator
1195 (cp_cv_quals, cp_declarator *, tree);
1196 static cp_declarator *make_reference_declarator
1197 (cp_cv_quals, cp_declarator *, bool, tree);
1198 static cp_parameter_declarator *make_parameter_declarator
1199 (cp_decl_specifier_seq *, cp_declarator *, tree);
1200 static cp_declarator *make_ptrmem_declarator
1201 (cp_cv_quals, tree, cp_declarator *, tree);
1203 /* An erroneous declarator. */
1204 static cp_declarator *cp_error_declarator;
1206 /* The obstack on which declarators and related data structures are
1207 allocated. */
1208 static struct obstack declarator_obstack;
1210 /* Alloc BYTES from the declarator memory pool. */
1212 static inline void *
1213 alloc_declarator (size_t bytes)
1215 return obstack_alloc (&declarator_obstack, bytes);
1218 /* Allocate a declarator of the indicated KIND. Clear fields that are
1219 common to all declarators. */
1221 static cp_declarator *
1222 make_declarator (cp_declarator_kind kind)
1224 cp_declarator *declarator;
1226 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1227 declarator->kind = kind;
1228 declarator->attributes = NULL_TREE;
1229 declarator->std_attributes = NULL_TREE;
1230 declarator->declarator = NULL;
1231 declarator->parameter_pack_p = false;
1232 declarator->id_loc = UNKNOWN_LOCATION;
1234 return declarator;
1237 /* Make a declarator for a generalized identifier. If
1238 QUALIFYING_SCOPE is non-NULL, the identifier is
1239 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1240 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1241 is, if any. */
1243 static cp_declarator *
1244 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1245 special_function_kind sfk)
1247 cp_declarator *declarator;
1249 /* It is valid to write:
1251 class C { void f(); };
1252 typedef C D;
1253 void D::f();
1255 The standard is not clear about whether `typedef const C D' is
1256 legal; as of 2002-09-15 the committee is considering that
1257 question. EDG 3.0 allows that syntax. Therefore, we do as
1258 well. */
1259 if (qualifying_scope && TYPE_P (qualifying_scope))
1260 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1262 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1263 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1264 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1266 declarator = make_declarator (cdk_id);
1267 declarator->u.id.qualifying_scope = qualifying_scope;
1268 declarator->u.id.unqualified_name = unqualified_name;
1269 declarator->u.id.sfk = sfk;
1271 return declarator;
1274 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1275 of modifiers such as const or volatile to apply to the pointer
1276 type, represented as identifiers. ATTRIBUTES represent the attributes that
1277 appertain to the pointer or reference. */
1279 cp_declarator *
1280 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1281 tree attributes)
1283 cp_declarator *declarator;
1285 declarator = make_declarator (cdk_pointer);
1286 declarator->declarator = target;
1287 declarator->u.pointer.qualifiers = cv_qualifiers;
1288 declarator->u.pointer.class_type = NULL_TREE;
1289 if (target)
1291 declarator->id_loc = target->id_loc;
1292 declarator->parameter_pack_p = target->parameter_pack_p;
1293 target->parameter_pack_p = false;
1295 else
1296 declarator->parameter_pack_p = false;
1298 declarator->std_attributes = attributes;
1300 return declarator;
1303 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1304 represent the attributes that appertain to the pointer or
1305 reference. */
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309 bool rvalue_ref, tree attributes)
1311 cp_declarator *declarator;
1313 declarator = make_declarator (cdk_reference);
1314 declarator->declarator = target;
1315 declarator->u.reference.qualifiers = cv_qualifiers;
1316 declarator->u.reference.rvalue_ref = rvalue_ref;
1317 if (target)
1319 declarator->id_loc = target->id_loc;
1320 declarator->parameter_pack_p = target->parameter_pack_p;
1321 target->parameter_pack_p = false;
1323 else
1324 declarator->parameter_pack_p = false;
1326 declarator->std_attributes = attributes;
1328 return declarator;
1331 /* Like make_pointer_declarator -- but for a pointer to a non-static
1332 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1333 appertain to the pointer or reference. */
1335 cp_declarator *
1336 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1337 cp_declarator *pointee,
1338 tree attributes)
1340 cp_declarator *declarator;
1342 declarator = make_declarator (cdk_ptrmem);
1343 declarator->declarator = pointee;
1344 declarator->u.pointer.qualifiers = cv_qualifiers;
1345 declarator->u.pointer.class_type = class_type;
1347 if (pointee)
1349 declarator->parameter_pack_p = pointee->parameter_pack_p;
1350 pointee->parameter_pack_p = false;
1352 else
1353 declarator->parameter_pack_p = false;
1355 declarator->std_attributes = attributes;
1357 return declarator;
1360 /* Make a declarator for the function given by TARGET, with the
1361 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1362 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1363 indicates what exceptions can be thrown. */
1365 cp_declarator *
1366 make_call_declarator (cp_declarator *target,
1367 tree parms,
1368 cp_cv_quals cv_qualifiers,
1369 cp_virt_specifiers virt_specifiers,
1370 cp_ref_qualifier ref_qualifier,
1371 tree exception_specification,
1372 tree late_return_type)
1374 cp_declarator *declarator;
1376 declarator = make_declarator (cdk_function);
1377 declarator->declarator = target;
1378 declarator->u.function.parameters = parms;
1379 declarator->u.function.qualifiers = cv_qualifiers;
1380 declarator->u.function.virt_specifiers = virt_specifiers;
1381 declarator->u.function.ref_qualifier = ref_qualifier;
1382 declarator->u.function.exception_specification = exception_specification;
1383 declarator->u.function.late_return_type = late_return_type;
1384 if (target)
1386 declarator->id_loc = target->id_loc;
1387 declarator->parameter_pack_p = target->parameter_pack_p;
1388 target->parameter_pack_p = false;
1390 else
1391 declarator->parameter_pack_p = false;
1393 return declarator;
1396 /* Make a declarator for an array of BOUNDS elements, each of which is
1397 defined by ELEMENT. */
1399 cp_declarator *
1400 make_array_declarator (cp_declarator *element, tree bounds)
1402 cp_declarator *declarator;
1404 declarator = make_declarator (cdk_array);
1405 declarator->declarator = element;
1406 declarator->u.array.bounds = bounds;
1407 if (element)
1409 declarator->id_loc = element->id_loc;
1410 declarator->parameter_pack_p = element->parameter_pack_p;
1411 element->parameter_pack_p = false;
1413 else
1414 declarator->parameter_pack_p = false;
1416 return declarator;
1419 /* Determine whether the declarator we've seen so far can be a
1420 parameter pack, when followed by an ellipsis. */
1421 static bool
1422 declarator_can_be_parameter_pack (cp_declarator *declarator)
1424 /* Search for a declarator name, or any other declarator that goes
1425 after the point where the ellipsis could appear in a parameter
1426 pack. If we find any of these, then this declarator can not be
1427 made into a parameter pack. */
1428 bool found = false;
1429 while (declarator && !found)
1431 switch ((int)declarator->kind)
1433 case cdk_id:
1434 case cdk_array:
1435 found = true;
1436 break;
1438 case cdk_error:
1439 return true;
1441 default:
1442 declarator = declarator->declarator;
1443 break;
1447 return !found;
1450 cp_parameter_declarator *no_parameters;
1452 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1453 DECLARATOR and DEFAULT_ARGUMENT. */
1455 cp_parameter_declarator *
1456 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1457 cp_declarator *declarator,
1458 tree default_argument)
1460 cp_parameter_declarator *parameter;
1462 parameter = ((cp_parameter_declarator *)
1463 alloc_declarator (sizeof (cp_parameter_declarator)));
1464 parameter->next = NULL;
1465 if (decl_specifiers)
1466 parameter->decl_specifiers = *decl_specifiers;
1467 else
1468 clear_decl_specs (&parameter->decl_specifiers);
1469 parameter->declarator = declarator;
1470 parameter->default_argument = default_argument;
1471 parameter->ellipsis_p = false;
1473 return parameter;
1476 /* Returns true iff DECLARATOR is a declaration for a function. */
1478 static bool
1479 function_declarator_p (const cp_declarator *declarator)
1481 while (declarator)
1483 if (declarator->kind == cdk_function
1484 && declarator->declarator->kind == cdk_id)
1485 return true;
1486 if (declarator->kind == cdk_id
1487 || declarator->kind == cdk_error)
1488 return false;
1489 declarator = declarator->declarator;
1491 return false;
1494 /* The parser. */
1496 /* Overview
1497 --------
1499 A cp_parser parses the token stream as specified by the C++
1500 grammar. Its job is purely parsing, not semantic analysis. For
1501 example, the parser breaks the token stream into declarators,
1502 expressions, statements, and other similar syntactic constructs.
1503 It does not check that the types of the expressions on either side
1504 of an assignment-statement are compatible, or that a function is
1505 not declared with a parameter of type `void'.
1507 The parser invokes routines elsewhere in the compiler to perform
1508 semantic analysis and to build up the abstract syntax tree for the
1509 code processed.
1511 The parser (and the template instantiation code, which is, in a
1512 way, a close relative of parsing) are the only parts of the
1513 compiler that should be calling push_scope and pop_scope, or
1514 related functions. The parser (and template instantiation code)
1515 keeps track of what scope is presently active; everything else
1516 should simply honor that. (The code that generates static
1517 initializers may also need to set the scope, in order to check
1518 access control correctly when emitting the initializers.)
1520 Methodology
1521 -----------
1523 The parser is of the standard recursive-descent variety. Upcoming
1524 tokens in the token stream are examined in order to determine which
1525 production to use when parsing a non-terminal. Some C++ constructs
1526 require arbitrary look ahead to disambiguate. For example, it is
1527 impossible, in the general case, to tell whether a statement is an
1528 expression or declaration without scanning the entire statement.
1529 Therefore, the parser is capable of "parsing tentatively." When the
1530 parser is not sure what construct comes next, it enters this mode.
1531 Then, while we attempt to parse the construct, the parser queues up
1532 error messages, rather than issuing them immediately, and saves the
1533 tokens it consumes. If the construct is parsed successfully, the
1534 parser "commits", i.e., it issues any queued error messages and
1535 the tokens that were being preserved are permanently discarded.
1536 If, however, the construct is not parsed successfully, the parser
1537 rolls back its state completely so that it can resume parsing using
1538 a different alternative.
1540 Future Improvements
1541 -------------------
1543 The performance of the parser could probably be improved substantially.
1544 We could often eliminate the need to parse tentatively by looking ahead
1545 a little bit. In some places, this approach might not entirely eliminate
1546 the need to parse tentatively, but it might still speed up the average
1547 case. */
1549 /* Flags that are passed to some parsing functions. These values can
1550 be bitwise-ored together. */
1552 enum
1554 /* No flags. */
1555 CP_PARSER_FLAGS_NONE = 0x0,
1556 /* The construct is optional. If it is not present, then no error
1557 should be issued. */
1558 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1559 /* When parsing a type-specifier, treat user-defined type-names
1560 as non-type identifiers. */
1561 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1562 /* When parsing a type-specifier, do not try to parse a class-specifier
1563 or enum-specifier. */
1564 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1565 /* When parsing a decl-specifier-seq, only allow type-specifier or
1566 constexpr. */
1567 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1570 /* This type is used for parameters and variables which hold
1571 combinations of the above flags. */
1572 typedef int cp_parser_flags;
1574 /* The different kinds of declarators we want to parse. */
1576 typedef enum cp_parser_declarator_kind
1578 /* We want an abstract declarator. */
1579 CP_PARSER_DECLARATOR_ABSTRACT,
1580 /* We want a named declarator. */
1581 CP_PARSER_DECLARATOR_NAMED,
1582 /* We don't mind, but the name must be an unqualified-id. */
1583 CP_PARSER_DECLARATOR_EITHER
1584 } cp_parser_declarator_kind;
1586 /* The precedence values used to parse binary expressions. The minimum value
1587 of PREC must be 1, because zero is reserved to quickly discriminate
1588 binary operators from other tokens. */
1590 enum cp_parser_prec
1592 PREC_NOT_OPERATOR,
1593 PREC_LOGICAL_OR_EXPRESSION,
1594 PREC_LOGICAL_AND_EXPRESSION,
1595 PREC_INCLUSIVE_OR_EXPRESSION,
1596 PREC_EXCLUSIVE_OR_EXPRESSION,
1597 PREC_AND_EXPRESSION,
1598 PREC_EQUALITY_EXPRESSION,
1599 PREC_RELATIONAL_EXPRESSION,
1600 PREC_SHIFT_EXPRESSION,
1601 PREC_ADDITIVE_EXPRESSION,
1602 PREC_MULTIPLICATIVE_EXPRESSION,
1603 PREC_PM_EXPRESSION,
1604 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1607 /* A mapping from a token type to a corresponding tree node type, with a
1608 precedence value. */
1610 typedef struct cp_parser_binary_operations_map_node
1612 /* The token type. */
1613 enum cpp_ttype token_type;
1614 /* The corresponding tree code. */
1615 enum tree_code tree_type;
1616 /* The precedence of this operator. */
1617 enum cp_parser_prec prec;
1618 } cp_parser_binary_operations_map_node;
1620 typedef struct cp_parser_expression_stack_entry
1622 /* Left hand side of the binary operation we are currently
1623 parsing. */
1624 tree lhs;
1625 /* Original tree code for left hand side, if it was a binary
1626 expression itself (used for -Wparentheses). */
1627 enum tree_code lhs_type;
1628 /* Tree code for the binary operation we are parsing. */
1629 enum tree_code tree_type;
1630 /* Precedence of the binary operation we are parsing. */
1631 enum cp_parser_prec prec;
1632 /* Location of the binary operation we are parsing. */
1633 location_t loc;
1634 } cp_parser_expression_stack_entry;
1636 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1637 entries because precedence levels on the stack are monotonically
1638 increasing. */
1639 typedef struct cp_parser_expression_stack_entry
1640 cp_parser_expression_stack[NUM_PREC_VALUES];
1642 /* Prototypes. */
1644 /* Constructors and destructors. */
1646 static cp_parser_context *cp_parser_context_new
1647 (cp_parser_context *);
1649 /* Class variables. */
1651 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1653 /* The operator-precedence table used by cp_parser_binary_expression.
1654 Transformed into an associative array (binops_by_token) by
1655 cp_parser_new. */
1657 static const cp_parser_binary_operations_map_node binops[] = {
1658 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1659 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1661 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1662 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1663 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1665 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1666 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1668 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1669 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1671 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1672 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1673 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1674 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1676 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1677 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1679 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1681 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1683 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1685 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1687 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1690 /* The same as binops, but initialized by cp_parser_new so that
1691 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1692 for speed. */
1693 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1695 /* Constructors and destructors. */
1697 /* Construct a new context. The context below this one on the stack
1698 is given by NEXT. */
1700 static cp_parser_context *
1701 cp_parser_context_new (cp_parser_context* next)
1703 cp_parser_context *context;
1705 /* Allocate the storage. */
1706 if (cp_parser_context_free_list != NULL)
1708 /* Pull the first entry from the free list. */
1709 context = cp_parser_context_free_list;
1710 cp_parser_context_free_list = context->next;
1711 memset (context, 0, sizeof (*context));
1713 else
1714 context = ggc_alloc_cleared_cp_parser_context ();
1716 /* No errors have occurred yet in this context. */
1717 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1718 /* If this is not the bottommost context, copy information that we
1719 need from the previous context. */
1720 if (next)
1722 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1723 expression, then we are parsing one in this context, too. */
1724 context->object_type = next->object_type;
1725 /* Thread the stack. */
1726 context->next = next;
1729 return context;
1732 /* Managing the unparsed function queues. */
1734 #define unparsed_funs_with_default_args \
1735 parser->unparsed_queues->last ().funs_with_default_args
1736 #define unparsed_funs_with_definitions \
1737 parser->unparsed_queues->last ().funs_with_definitions
1738 #define unparsed_nsdmis \
1739 parser->unparsed_queues->last ().nsdmis
1741 static void
1742 push_unparsed_function_queues (cp_parser *parser)
1744 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1745 vec_safe_push (parser->unparsed_queues, e);
1748 static void
1749 pop_unparsed_function_queues (cp_parser *parser)
1751 release_tree_vector (unparsed_funs_with_definitions);
1752 parser->unparsed_queues->pop ();
1755 /* Prototypes. */
1757 /* Constructors and destructors. */
1759 static cp_parser *cp_parser_new
1760 (void);
1762 /* Routines to parse various constructs.
1764 Those that return `tree' will return the error_mark_node (rather
1765 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1766 Sometimes, they will return an ordinary node if error-recovery was
1767 attempted, even though a parse error occurred. So, to check
1768 whether or not a parse error occurred, you should always use
1769 cp_parser_error_occurred. If the construct is optional (indicated
1770 either by an `_opt' in the name of the function that does the
1771 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1772 the construct is not present. */
1774 /* Lexical conventions [gram.lex] */
1776 static tree cp_parser_identifier
1777 (cp_parser *);
1778 static tree cp_parser_string_literal
1779 (cp_parser *, bool, bool);
1780 static tree cp_parser_userdef_char_literal
1781 (cp_parser *);
1782 static tree cp_parser_userdef_string_literal
1783 (cp_token *);
1784 static tree cp_parser_userdef_numeric_literal
1785 (cp_parser *);
1787 /* Basic concepts [gram.basic] */
1789 static bool cp_parser_translation_unit
1790 (cp_parser *);
1792 /* Expressions [gram.expr] */
1794 static tree cp_parser_primary_expression
1795 (cp_parser *, bool, bool, bool, cp_id_kind *);
1796 static tree cp_parser_id_expression
1797 (cp_parser *, bool, bool, bool *, bool, bool);
1798 static tree cp_parser_unqualified_id
1799 (cp_parser *, bool, bool, bool, bool);
1800 static tree cp_parser_nested_name_specifier_opt
1801 (cp_parser *, bool, bool, bool, bool);
1802 static tree cp_parser_nested_name_specifier
1803 (cp_parser *, bool, bool, bool, bool);
1804 static tree cp_parser_qualifying_entity
1805 (cp_parser *, bool, bool, bool, bool, bool);
1806 static tree cp_parser_postfix_expression
1807 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1808 static tree cp_parser_postfix_open_square_expression
1809 (cp_parser *, tree, bool, bool);
1810 static tree cp_parser_postfix_dot_deref_expression
1811 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1812 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1813 (cp_parser *, int, bool, bool, bool *);
1814 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1815 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1816 static void cp_parser_pseudo_destructor_name
1817 (cp_parser *, tree *, tree *);
1818 static tree cp_parser_unary_expression
1819 (cp_parser *, bool, bool, cp_id_kind *);
1820 static enum tree_code cp_parser_unary_operator
1821 (cp_token *);
1822 static tree cp_parser_new_expression
1823 (cp_parser *);
1824 static vec<tree, va_gc> *cp_parser_new_placement
1825 (cp_parser *);
1826 static tree cp_parser_new_type_id
1827 (cp_parser *, tree *);
1828 static cp_declarator *cp_parser_new_declarator_opt
1829 (cp_parser *);
1830 static cp_declarator *cp_parser_direct_new_declarator
1831 (cp_parser *);
1832 static vec<tree, va_gc> *cp_parser_new_initializer
1833 (cp_parser *);
1834 static tree cp_parser_delete_expression
1835 (cp_parser *);
1836 static tree cp_parser_cast_expression
1837 (cp_parser *, bool, bool, bool, cp_id_kind *);
1838 static tree cp_parser_binary_expression
1839 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1840 static tree cp_parser_question_colon_clause
1841 (cp_parser *, tree);
1842 static tree cp_parser_assignment_expression
1843 (cp_parser *, bool, cp_id_kind *);
1844 static enum tree_code cp_parser_assignment_operator_opt
1845 (cp_parser *);
1846 static tree cp_parser_expression
1847 (cp_parser *, bool, cp_id_kind *);
1848 static tree cp_parser_expression
1849 (cp_parser *, bool, bool, cp_id_kind *);
1850 static tree cp_parser_constant_expression
1851 (cp_parser *, bool, bool *);
1852 static tree cp_parser_builtin_offsetof
1853 (cp_parser *);
1854 static tree cp_parser_lambda_expression
1855 (cp_parser *);
1856 static void cp_parser_lambda_introducer
1857 (cp_parser *, tree);
1858 static bool cp_parser_lambda_declarator_opt
1859 (cp_parser *, tree);
1860 static void cp_parser_lambda_body
1861 (cp_parser *, tree);
1863 /* Statements [gram.stmt.stmt] */
1865 static void cp_parser_statement
1866 (cp_parser *, tree, bool, bool *);
1867 static void cp_parser_label_for_labeled_statement
1868 (cp_parser *, tree);
1869 static tree cp_parser_expression_statement
1870 (cp_parser *, tree);
1871 static tree cp_parser_compound_statement
1872 (cp_parser *, tree, bool, bool);
1873 static void cp_parser_statement_seq_opt
1874 (cp_parser *, tree);
1875 static tree cp_parser_selection_statement
1876 (cp_parser *, bool *);
1877 static tree cp_parser_condition
1878 (cp_parser *);
1879 static tree cp_parser_iteration_statement
1880 (cp_parser *);
1881 static bool cp_parser_for_init_statement
1882 (cp_parser *, tree *decl);
1883 static tree cp_parser_for
1884 (cp_parser *);
1885 static tree cp_parser_c_for
1886 (cp_parser *, tree, tree);
1887 static tree cp_parser_range_for
1888 (cp_parser *, tree, tree, tree);
1889 static void do_range_for_auto_deduction
1890 (tree, tree);
1891 static tree cp_parser_perform_range_for_lookup
1892 (tree, tree *, tree *);
1893 static tree cp_parser_range_for_member_function
1894 (tree, tree);
1895 static tree cp_parser_jump_statement
1896 (cp_parser *);
1897 static void cp_parser_declaration_statement
1898 (cp_parser *);
1900 static tree cp_parser_implicitly_scoped_statement
1901 (cp_parser *, bool *);
1902 static void cp_parser_already_scoped_statement
1903 (cp_parser *);
1905 /* Declarations [gram.dcl.dcl] */
1907 static void cp_parser_declaration_seq_opt
1908 (cp_parser *);
1909 static void cp_parser_declaration
1910 (cp_parser *);
1911 static void cp_parser_block_declaration
1912 (cp_parser *, bool);
1913 static void cp_parser_simple_declaration
1914 (cp_parser *, bool, tree *);
1915 static void cp_parser_decl_specifier_seq
1916 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1917 static tree cp_parser_storage_class_specifier_opt
1918 (cp_parser *);
1919 static tree cp_parser_function_specifier_opt
1920 (cp_parser *, cp_decl_specifier_seq *);
1921 static tree cp_parser_type_specifier
1922 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1923 int *, bool *);
1924 static tree cp_parser_simple_type_specifier
1925 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1926 static tree cp_parser_type_name
1927 (cp_parser *);
1928 static tree cp_parser_nonclass_name
1929 (cp_parser* parser);
1930 static tree cp_parser_elaborated_type_specifier
1931 (cp_parser *, bool, bool);
1932 static tree cp_parser_enum_specifier
1933 (cp_parser *);
1934 static void cp_parser_enumerator_list
1935 (cp_parser *, tree);
1936 static void cp_parser_enumerator_definition
1937 (cp_parser *, tree);
1938 static tree cp_parser_namespace_name
1939 (cp_parser *);
1940 static void cp_parser_namespace_definition
1941 (cp_parser *);
1942 static void cp_parser_namespace_body
1943 (cp_parser *);
1944 static tree cp_parser_qualified_namespace_specifier
1945 (cp_parser *);
1946 static void cp_parser_namespace_alias_definition
1947 (cp_parser *);
1948 static bool cp_parser_using_declaration
1949 (cp_parser *, bool);
1950 static void cp_parser_using_directive
1951 (cp_parser *);
1952 static tree cp_parser_alias_declaration
1953 (cp_parser *);
1954 static void cp_parser_asm_definition
1955 (cp_parser *);
1956 static void cp_parser_linkage_specification
1957 (cp_parser *);
1958 static void cp_parser_static_assert
1959 (cp_parser *, bool);
1960 static tree cp_parser_decltype
1961 (cp_parser *);
1963 /* Declarators [gram.dcl.decl] */
1965 static tree cp_parser_init_declarator
1966 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
1967 static cp_declarator *cp_parser_declarator
1968 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1969 static cp_declarator *cp_parser_direct_declarator
1970 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1971 static enum tree_code cp_parser_ptr_operator
1972 (cp_parser *, tree *, cp_cv_quals *, tree *);
1973 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1974 (cp_parser *);
1975 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1976 (cp_parser *);
1977 static cp_ref_qualifier cp_parser_ref_qualifier_seq_opt
1978 (cp_parser *);
1979 static tree cp_parser_late_return_type_opt
1980 (cp_parser *, cp_cv_quals);
1981 static tree cp_parser_declarator_id
1982 (cp_parser *, bool);
1983 static tree cp_parser_type_id
1984 (cp_parser *);
1985 static tree cp_parser_template_type_arg
1986 (cp_parser *);
1987 static tree cp_parser_trailing_type_id (cp_parser *);
1988 static tree cp_parser_type_id_1
1989 (cp_parser *, bool, bool);
1990 static void cp_parser_type_specifier_seq
1991 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1992 static tree cp_parser_parameter_declaration_clause
1993 (cp_parser *);
1994 static tree cp_parser_parameter_declaration_list
1995 (cp_parser *, bool *);
1996 static cp_parameter_declarator *cp_parser_parameter_declaration
1997 (cp_parser *, bool, bool *);
1998 static tree cp_parser_default_argument
1999 (cp_parser *, bool);
2000 static void cp_parser_function_body
2001 (cp_parser *, bool);
2002 static tree cp_parser_initializer
2003 (cp_parser *, bool *, bool *);
2004 static tree cp_parser_initializer_clause
2005 (cp_parser *, bool *);
2006 static tree cp_parser_braced_list
2007 (cp_parser*, bool*);
2008 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2009 (cp_parser *, bool *);
2011 static bool cp_parser_ctor_initializer_opt_and_function_body
2012 (cp_parser *, bool);
2014 /* Classes [gram.class] */
2016 static tree cp_parser_class_name
2017 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2018 static tree cp_parser_class_specifier
2019 (cp_parser *);
2020 static tree cp_parser_class_head
2021 (cp_parser *, bool *);
2022 static enum tag_types cp_parser_class_key
2023 (cp_parser *);
2024 static void cp_parser_member_specification_opt
2025 (cp_parser *);
2026 static void cp_parser_member_declaration
2027 (cp_parser *);
2028 static tree cp_parser_pure_specifier
2029 (cp_parser *);
2030 static tree cp_parser_constant_initializer
2031 (cp_parser *);
2033 /* Derived classes [gram.class.derived] */
2035 static tree cp_parser_base_clause
2036 (cp_parser *);
2037 static tree cp_parser_base_specifier
2038 (cp_parser *);
2040 /* Special member functions [gram.special] */
2042 static tree cp_parser_conversion_function_id
2043 (cp_parser *);
2044 static tree cp_parser_conversion_type_id
2045 (cp_parser *);
2046 static cp_declarator *cp_parser_conversion_declarator_opt
2047 (cp_parser *);
2048 static bool cp_parser_ctor_initializer_opt
2049 (cp_parser *);
2050 static void cp_parser_mem_initializer_list
2051 (cp_parser *);
2052 static tree cp_parser_mem_initializer
2053 (cp_parser *);
2054 static tree cp_parser_mem_initializer_id
2055 (cp_parser *);
2057 /* Overloading [gram.over] */
2059 static tree cp_parser_operator_function_id
2060 (cp_parser *);
2061 static tree cp_parser_operator
2062 (cp_parser *);
2064 /* Templates [gram.temp] */
2066 static void cp_parser_template_declaration
2067 (cp_parser *, bool);
2068 static tree cp_parser_template_parameter_list
2069 (cp_parser *);
2070 static tree cp_parser_template_parameter
2071 (cp_parser *, bool *, bool *);
2072 static tree cp_parser_type_parameter
2073 (cp_parser *, bool *);
2074 static tree cp_parser_template_id
2075 (cp_parser *, bool, bool, enum tag_types, bool);
2076 static tree cp_parser_template_name
2077 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2078 static tree cp_parser_template_argument_list
2079 (cp_parser *);
2080 static tree cp_parser_template_argument
2081 (cp_parser *);
2082 static void cp_parser_explicit_instantiation
2083 (cp_parser *);
2084 static void cp_parser_explicit_specialization
2085 (cp_parser *);
2087 /* Exception handling [gram.exception] */
2089 static tree cp_parser_try_block
2090 (cp_parser *);
2091 static bool cp_parser_function_try_block
2092 (cp_parser *);
2093 static void cp_parser_handler_seq
2094 (cp_parser *);
2095 static void cp_parser_handler
2096 (cp_parser *);
2097 static tree cp_parser_exception_declaration
2098 (cp_parser *);
2099 static tree cp_parser_throw_expression
2100 (cp_parser *);
2101 static tree cp_parser_exception_specification_opt
2102 (cp_parser *);
2103 static tree cp_parser_type_id_list
2104 (cp_parser *);
2106 /* GNU Extensions */
2108 static tree cp_parser_asm_specification_opt
2109 (cp_parser *);
2110 static tree cp_parser_asm_operand_list
2111 (cp_parser *);
2112 static tree cp_parser_asm_clobber_list
2113 (cp_parser *);
2114 static tree cp_parser_asm_label_list
2115 (cp_parser *);
2116 static bool cp_next_tokens_can_be_attribute_p
2117 (cp_parser *);
2118 static bool cp_next_tokens_can_be_gnu_attribute_p
2119 (cp_parser *);
2120 static bool cp_next_tokens_can_be_std_attribute_p
2121 (cp_parser *);
2122 static bool cp_nth_tokens_can_be_std_attribute_p
2123 (cp_parser *, size_t);
2124 static bool cp_nth_tokens_can_be_gnu_attribute_p
2125 (cp_parser *, size_t);
2126 static bool cp_nth_tokens_can_be_attribute_p
2127 (cp_parser *, size_t);
2128 static tree cp_parser_attributes_opt
2129 (cp_parser *);
2130 static tree cp_parser_gnu_attributes_opt
2131 (cp_parser *);
2132 static tree cp_parser_gnu_attribute_list
2133 (cp_parser *);
2134 static tree cp_parser_std_attribute
2135 (cp_parser *);
2136 static tree cp_parser_std_attribute_spec
2137 (cp_parser *);
2138 static tree cp_parser_std_attribute_spec_seq
2139 (cp_parser *);
2140 static bool cp_parser_extension_opt
2141 (cp_parser *, int *);
2142 static void cp_parser_label_declaration
2143 (cp_parser *);
2145 /* Transactional Memory Extensions */
2147 static tree cp_parser_transaction
2148 (cp_parser *, enum rid);
2149 static tree cp_parser_transaction_expression
2150 (cp_parser *, enum rid);
2151 static bool cp_parser_function_transaction
2152 (cp_parser *, enum rid);
2153 static tree cp_parser_transaction_cancel
2154 (cp_parser *);
2156 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2157 static bool cp_parser_pragma
2158 (cp_parser *, enum pragma_context);
2160 /* Objective-C++ Productions */
2162 static tree cp_parser_objc_message_receiver
2163 (cp_parser *);
2164 static tree cp_parser_objc_message_args
2165 (cp_parser *);
2166 static tree cp_parser_objc_message_expression
2167 (cp_parser *);
2168 static tree cp_parser_objc_encode_expression
2169 (cp_parser *);
2170 static tree cp_parser_objc_defs_expression
2171 (cp_parser *);
2172 static tree cp_parser_objc_protocol_expression
2173 (cp_parser *);
2174 static tree cp_parser_objc_selector_expression
2175 (cp_parser *);
2176 static tree cp_parser_objc_expression
2177 (cp_parser *);
2178 static bool cp_parser_objc_selector_p
2179 (enum cpp_ttype);
2180 static tree cp_parser_objc_selector
2181 (cp_parser *);
2182 static tree cp_parser_objc_protocol_refs_opt
2183 (cp_parser *);
2184 static void cp_parser_objc_declaration
2185 (cp_parser *, tree);
2186 static tree cp_parser_objc_statement
2187 (cp_parser *);
2188 static bool cp_parser_objc_valid_prefix_attributes
2189 (cp_parser *, tree *);
2190 static void cp_parser_objc_at_property_declaration
2191 (cp_parser *) ;
2192 static void cp_parser_objc_at_synthesize_declaration
2193 (cp_parser *) ;
2194 static void cp_parser_objc_at_dynamic_declaration
2195 (cp_parser *) ;
2196 static tree cp_parser_objc_struct_declaration
2197 (cp_parser *) ;
2199 /* Utility Routines */
2201 static tree cp_parser_lookup_name
2202 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2203 static tree cp_parser_lookup_name_simple
2204 (cp_parser *, tree, location_t);
2205 static tree cp_parser_maybe_treat_template_as_class
2206 (tree, bool);
2207 static bool cp_parser_check_declarator_template_parameters
2208 (cp_parser *, cp_declarator *, location_t);
2209 static bool cp_parser_check_template_parameters
2210 (cp_parser *, unsigned, location_t, cp_declarator *);
2211 static tree cp_parser_simple_cast_expression
2212 (cp_parser *);
2213 static tree cp_parser_global_scope_opt
2214 (cp_parser *, bool);
2215 static bool cp_parser_constructor_declarator_p
2216 (cp_parser *, bool);
2217 static tree cp_parser_function_definition_from_specifiers_and_declarator
2218 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2219 static tree cp_parser_function_definition_after_declarator
2220 (cp_parser *, bool);
2221 static void cp_parser_template_declaration_after_export
2222 (cp_parser *, bool);
2223 static void cp_parser_perform_template_parameter_access_checks
2224 (vec<deferred_access_check, va_gc> *);
2225 static tree cp_parser_single_declaration
2226 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2227 static tree cp_parser_functional_cast
2228 (cp_parser *, tree);
2229 static tree cp_parser_save_member_function_body
2230 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2231 static tree cp_parser_save_nsdmi
2232 (cp_parser *);
2233 static tree cp_parser_enclosed_template_argument_list
2234 (cp_parser *);
2235 static void cp_parser_save_default_args
2236 (cp_parser *, tree);
2237 static void cp_parser_late_parsing_for_member
2238 (cp_parser *, tree);
2239 static tree cp_parser_late_parse_one_default_arg
2240 (cp_parser *, tree, tree, tree);
2241 static void cp_parser_late_parsing_nsdmi
2242 (cp_parser *, tree);
2243 static void cp_parser_late_parsing_default_args
2244 (cp_parser *, tree);
2245 static tree cp_parser_sizeof_operand
2246 (cp_parser *, enum rid);
2247 static tree cp_parser_trait_expr
2248 (cp_parser *, enum rid);
2249 static bool cp_parser_declares_only_class_p
2250 (cp_parser *);
2251 static void cp_parser_set_storage_class
2252 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2253 static void cp_parser_set_decl_spec_type
2254 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2255 static void set_and_check_decl_spec_loc
2256 (cp_decl_specifier_seq *decl_specs,
2257 cp_decl_spec ds, cp_token *);
2258 static bool cp_parser_friend_p
2259 (const cp_decl_specifier_seq *);
2260 static void cp_parser_required_error
2261 (cp_parser *, required_token, bool);
2262 static cp_token *cp_parser_require
2263 (cp_parser *, enum cpp_ttype, required_token);
2264 static cp_token *cp_parser_require_keyword
2265 (cp_parser *, enum rid, required_token);
2266 static bool cp_parser_token_starts_function_definition_p
2267 (cp_token *);
2268 static bool cp_parser_next_token_starts_class_definition_p
2269 (cp_parser *);
2270 static bool cp_parser_next_token_ends_template_argument_p
2271 (cp_parser *);
2272 static bool cp_parser_nth_token_starts_template_argument_list_p
2273 (cp_parser *, size_t);
2274 static enum tag_types cp_parser_token_is_class_key
2275 (cp_token *);
2276 static void cp_parser_check_class_key
2277 (enum tag_types, tree type);
2278 static void cp_parser_check_access_in_redeclaration
2279 (tree type, location_t location);
2280 static bool cp_parser_optional_template_keyword
2281 (cp_parser *);
2282 static void cp_parser_pre_parsed_nested_name_specifier
2283 (cp_parser *);
2284 static bool cp_parser_cache_group
2285 (cp_parser *, enum cpp_ttype, unsigned);
2286 static tree cp_parser_cache_defarg
2287 (cp_parser *parser, bool nsdmi);
2288 static void cp_parser_parse_tentatively
2289 (cp_parser *);
2290 static void cp_parser_commit_to_tentative_parse
2291 (cp_parser *);
2292 static void cp_parser_abort_tentative_parse
2293 (cp_parser *);
2294 static bool cp_parser_parse_definitely
2295 (cp_parser *);
2296 static inline bool cp_parser_parsing_tentatively
2297 (cp_parser *);
2298 static bool cp_parser_uncommitted_to_tentative_parse_p
2299 (cp_parser *);
2300 static void cp_parser_error
2301 (cp_parser *, const char *);
2302 static void cp_parser_name_lookup_error
2303 (cp_parser *, tree, tree, name_lookup_error, location_t);
2304 static bool cp_parser_simulate_error
2305 (cp_parser *);
2306 static bool cp_parser_check_type_definition
2307 (cp_parser *);
2308 static void cp_parser_check_for_definition_in_return_type
2309 (cp_declarator *, tree, location_t type_location);
2310 static void cp_parser_check_for_invalid_template_id
2311 (cp_parser *, tree, enum tag_types, location_t location);
2312 static bool cp_parser_non_integral_constant_expression
2313 (cp_parser *, non_integral_constant);
2314 static void cp_parser_diagnose_invalid_type_name
2315 (cp_parser *, tree, tree, location_t);
2316 static bool cp_parser_parse_and_diagnose_invalid_type_name
2317 (cp_parser *);
2318 static int cp_parser_skip_to_closing_parenthesis
2319 (cp_parser *, bool, bool, bool);
2320 static void cp_parser_skip_to_end_of_statement
2321 (cp_parser *);
2322 static void cp_parser_consume_semicolon_at_end_of_statement
2323 (cp_parser *);
2324 static void cp_parser_skip_to_end_of_block_or_statement
2325 (cp_parser *);
2326 static bool cp_parser_skip_to_closing_brace
2327 (cp_parser *);
2328 static void cp_parser_skip_to_end_of_template_parameter_list
2329 (cp_parser *);
2330 static void cp_parser_skip_to_pragma_eol
2331 (cp_parser*, cp_token *);
2332 static bool cp_parser_error_occurred
2333 (cp_parser *);
2334 static bool cp_parser_allow_gnu_extensions_p
2335 (cp_parser *);
2336 static bool cp_parser_is_pure_string_literal
2337 (cp_token *);
2338 static bool cp_parser_is_string_literal
2339 (cp_token *);
2340 static bool cp_parser_is_keyword
2341 (cp_token *, enum rid);
2342 static tree cp_parser_make_typename_type
2343 (cp_parser *, tree, tree, location_t location);
2344 static cp_declarator * cp_parser_make_indirect_declarator
2345 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2347 /* Returns nonzero if we are parsing tentatively. */
2349 static inline bool
2350 cp_parser_parsing_tentatively (cp_parser* parser)
2352 return parser->context->next != NULL;
2355 /* Returns nonzero if TOKEN is a string literal. */
2357 static bool
2358 cp_parser_is_pure_string_literal (cp_token* token)
2360 return (token->type == CPP_STRING ||
2361 token->type == CPP_STRING16 ||
2362 token->type == CPP_STRING32 ||
2363 token->type == CPP_WSTRING ||
2364 token->type == CPP_UTF8STRING);
2367 /* Returns nonzero if TOKEN is a string literal
2368 of a user-defined string literal. */
2370 static bool
2371 cp_parser_is_string_literal (cp_token* token)
2373 return (cp_parser_is_pure_string_literal (token) ||
2374 token->type == CPP_STRING_USERDEF ||
2375 token->type == CPP_STRING16_USERDEF ||
2376 token->type == CPP_STRING32_USERDEF ||
2377 token->type == CPP_WSTRING_USERDEF ||
2378 token->type == CPP_UTF8STRING_USERDEF);
2381 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2383 static bool
2384 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2386 return token->keyword == keyword;
2389 /* If not parsing tentatively, issue a diagnostic of the form
2390 FILE:LINE: MESSAGE before TOKEN
2391 where TOKEN is the next token in the input stream. MESSAGE
2392 (specified by the caller) is usually of the form "expected
2393 OTHER-TOKEN". */
2395 static void
2396 cp_parser_error (cp_parser* parser, const char* gmsgid)
2398 if (!cp_parser_simulate_error (parser))
2400 cp_token *token = cp_lexer_peek_token (parser->lexer);
2401 /* This diagnostic makes more sense if it is tagged to the line
2402 of the token we just peeked at. */
2403 cp_lexer_set_source_position_from_token (token);
2405 if (token->type == CPP_PRAGMA)
2407 error_at (token->location,
2408 "%<#pragma%> is not allowed here");
2409 cp_parser_skip_to_pragma_eol (parser, token);
2410 return;
2413 c_parse_error (gmsgid,
2414 /* Because c_parser_error does not understand
2415 CPP_KEYWORD, keywords are treated like
2416 identifiers. */
2417 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2418 token->u.value, token->flags);
2422 /* Issue an error about name-lookup failing. NAME is the
2423 IDENTIFIER_NODE DECL is the result of
2424 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2425 the thing that we hoped to find. */
2427 static void
2428 cp_parser_name_lookup_error (cp_parser* parser,
2429 tree name,
2430 tree decl,
2431 name_lookup_error desired,
2432 location_t location)
2434 /* If name lookup completely failed, tell the user that NAME was not
2435 declared. */
2436 if (decl == error_mark_node)
2438 if (parser->scope && parser->scope != global_namespace)
2439 error_at (location, "%<%E::%E%> has not been declared",
2440 parser->scope, name);
2441 else if (parser->scope == global_namespace)
2442 error_at (location, "%<::%E%> has not been declared", name);
2443 else if (parser->object_scope
2444 && !CLASS_TYPE_P (parser->object_scope))
2445 error_at (location, "request for member %qE in non-class type %qT",
2446 name, parser->object_scope);
2447 else if (parser->object_scope)
2448 error_at (location, "%<%T::%E%> has not been declared",
2449 parser->object_scope, name);
2450 else
2451 error_at (location, "%qE has not been declared", name);
2453 else if (parser->scope && parser->scope != global_namespace)
2455 switch (desired)
2457 case NLE_TYPE:
2458 error_at (location, "%<%E::%E%> is not a type",
2459 parser->scope, name);
2460 break;
2461 case NLE_CXX98:
2462 error_at (location, "%<%E::%E%> is not a class or namespace",
2463 parser->scope, name);
2464 break;
2465 case NLE_NOT_CXX98:
2466 error_at (location,
2467 "%<%E::%E%> is not a class, namespace, or enumeration",
2468 parser->scope, name);
2469 break;
2470 default:
2471 gcc_unreachable ();
2475 else if (parser->scope == global_namespace)
2477 switch (desired)
2479 case NLE_TYPE:
2480 error_at (location, "%<::%E%> is not a type", name);
2481 break;
2482 case NLE_CXX98:
2483 error_at (location, "%<::%E%> is not a class or namespace", name);
2484 break;
2485 case NLE_NOT_CXX98:
2486 error_at (location,
2487 "%<::%E%> is not a class, namespace, or enumeration",
2488 name);
2489 break;
2490 default:
2491 gcc_unreachable ();
2494 else
2496 switch (desired)
2498 case NLE_TYPE:
2499 error_at (location, "%qE is not a type", name);
2500 break;
2501 case NLE_CXX98:
2502 error_at (location, "%qE is not a class or namespace", name);
2503 break;
2504 case NLE_NOT_CXX98:
2505 error_at (location,
2506 "%qE is not a class, namespace, or enumeration", name);
2507 break;
2508 default:
2509 gcc_unreachable ();
2514 /* If we are parsing tentatively, remember that an error has occurred
2515 during this tentative parse. Returns true if the error was
2516 simulated; false if a message should be issued by the caller. */
2518 static bool
2519 cp_parser_simulate_error (cp_parser* parser)
2521 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2523 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2524 return true;
2526 return false;
2529 /* This function is called when a type is defined. If type
2530 definitions are forbidden at this point, an error message is
2531 issued. */
2533 static bool
2534 cp_parser_check_type_definition (cp_parser* parser)
2536 /* If types are forbidden here, issue a message. */
2537 if (parser->type_definition_forbidden_message)
2539 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2540 in the message need to be interpreted. */
2541 error (parser->type_definition_forbidden_message);
2542 return false;
2544 return true;
2547 /* This function is called when the DECLARATOR is processed. The TYPE
2548 was a type defined in the decl-specifiers. If it is invalid to
2549 define a type in the decl-specifiers for DECLARATOR, an error is
2550 issued. TYPE_LOCATION is the location of TYPE and is used
2551 for error reporting. */
2553 static void
2554 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2555 tree type, location_t type_location)
2557 /* [dcl.fct] forbids type definitions in return types.
2558 Unfortunately, it's not easy to know whether or not we are
2559 processing a return type until after the fact. */
2560 while (declarator
2561 && (declarator->kind == cdk_pointer
2562 || declarator->kind == cdk_reference
2563 || declarator->kind == cdk_ptrmem))
2564 declarator = declarator->declarator;
2565 if (declarator
2566 && declarator->kind == cdk_function)
2568 error_at (type_location,
2569 "new types may not be defined in a return type");
2570 inform (type_location,
2571 "(perhaps a semicolon is missing after the definition of %qT)",
2572 type);
2576 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2577 "<" in any valid C++ program. If the next token is indeed "<",
2578 issue a message warning the user about what appears to be an
2579 invalid attempt to form a template-id. LOCATION is the location
2580 of the type-specifier (TYPE) */
2582 static void
2583 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2584 tree type,
2585 enum tag_types tag_type,
2586 location_t location)
2588 cp_token_position start = 0;
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2592 if (TYPE_P (type))
2593 error_at (location, "%qT is not a template", type);
2594 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2596 if (tag_type != none_type)
2597 error_at (location, "%qE is not a class template", type);
2598 else
2599 error_at (location, "%qE is not a template", type);
2601 else
2602 error_at (location, "invalid template-id");
2603 /* Remember the location of the invalid "<". */
2604 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2605 start = cp_lexer_token_position (parser->lexer, true);
2606 /* Consume the "<". */
2607 cp_lexer_consume_token (parser->lexer);
2608 /* Parse the template arguments. */
2609 cp_parser_enclosed_template_argument_list (parser);
2610 /* Permanently remove the invalid template arguments so that
2611 this error message is not issued again. */
2612 if (start)
2613 cp_lexer_purge_tokens_after (parser->lexer, start);
2617 /* If parsing an integral constant-expression, issue an error message
2618 about the fact that THING appeared and return true. Otherwise,
2619 return false. In either case, set
2620 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2622 static bool
2623 cp_parser_non_integral_constant_expression (cp_parser *parser,
2624 non_integral_constant thing)
2626 parser->non_integral_constant_expression_p = true;
2627 if (parser->integral_constant_expression_p)
2629 if (!parser->allow_non_integral_constant_expression_p)
2631 const char *msg = NULL;
2632 switch (thing)
2634 case NIC_FLOAT:
2635 error ("floating-point literal "
2636 "cannot appear in a constant-expression");
2637 return true;
2638 case NIC_CAST:
2639 error ("a cast to a type other than an integral or "
2640 "enumeration type cannot appear in a "
2641 "constant-expression");
2642 return true;
2643 case NIC_TYPEID:
2644 error ("%<typeid%> operator "
2645 "cannot appear in a constant-expression");
2646 return true;
2647 case NIC_NCC:
2648 error ("non-constant compound literals "
2649 "cannot appear in a constant-expression");
2650 return true;
2651 case NIC_FUNC_CALL:
2652 error ("a function call "
2653 "cannot appear in a constant-expression");
2654 return true;
2655 case NIC_INC:
2656 error ("an increment "
2657 "cannot appear in a constant-expression");
2658 return true;
2659 case NIC_DEC:
2660 error ("an decrement "
2661 "cannot appear in a constant-expression");
2662 return true;
2663 case NIC_ARRAY_REF:
2664 error ("an array reference "
2665 "cannot appear in a constant-expression");
2666 return true;
2667 case NIC_ADDR_LABEL:
2668 error ("the address of a label "
2669 "cannot appear in a constant-expression");
2670 return true;
2671 case NIC_OVERLOADED:
2672 error ("calls to overloaded operators "
2673 "cannot appear in a constant-expression");
2674 return true;
2675 case NIC_ASSIGNMENT:
2676 error ("an assignment cannot appear in a constant-expression");
2677 return true;
2678 case NIC_COMMA:
2679 error ("a comma operator "
2680 "cannot appear in a constant-expression");
2681 return true;
2682 case NIC_CONSTRUCTOR:
2683 error ("a call to a constructor "
2684 "cannot appear in a constant-expression");
2685 return true;
2686 case NIC_TRANSACTION:
2687 error ("a transaction expression "
2688 "cannot appear in a constant-expression");
2689 return true;
2690 case NIC_THIS:
2691 msg = "this";
2692 break;
2693 case NIC_FUNC_NAME:
2694 msg = "__FUNCTION__";
2695 break;
2696 case NIC_PRETTY_FUNC:
2697 msg = "__PRETTY_FUNCTION__";
2698 break;
2699 case NIC_C99_FUNC:
2700 msg = "__func__";
2701 break;
2702 case NIC_VA_ARG:
2703 msg = "va_arg";
2704 break;
2705 case NIC_ARROW:
2706 msg = "->";
2707 break;
2708 case NIC_POINT:
2709 msg = ".";
2710 break;
2711 case NIC_STAR:
2712 msg = "*";
2713 break;
2714 case NIC_ADDR:
2715 msg = "&";
2716 break;
2717 case NIC_PREINCREMENT:
2718 msg = "++";
2719 break;
2720 case NIC_PREDECREMENT:
2721 msg = "--";
2722 break;
2723 case NIC_NEW:
2724 msg = "new";
2725 break;
2726 case NIC_DEL:
2727 msg = "delete";
2728 break;
2729 default:
2730 gcc_unreachable ();
2732 if (msg)
2733 error ("%qs cannot appear in a constant-expression", msg);
2734 return true;
2737 return false;
2740 /* Emit a diagnostic for an invalid type name. SCOPE is the
2741 qualifying scope (or NULL, if none) for ID. This function commits
2742 to the current active tentative parse, if any. (Otherwise, the
2743 problematic construct might be encountered again later, resulting
2744 in duplicate error messages.) LOCATION is the location of ID. */
2746 static void
2747 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2748 tree scope, tree id,
2749 location_t location)
2751 tree decl, old_scope;
2752 cp_parser_commit_to_tentative_parse (parser);
2753 /* Try to lookup the identifier. */
2754 old_scope = parser->scope;
2755 parser->scope = scope;
2756 decl = cp_parser_lookup_name_simple (parser, id, location);
2757 parser->scope = old_scope;
2758 /* If the lookup found a template-name, it means that the user forgot
2759 to specify an argument list. Emit a useful error message. */
2760 if (TREE_CODE (decl) == TEMPLATE_DECL)
2761 error_at (location,
2762 "invalid use of template-name %qE without an argument list",
2763 decl);
2764 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2765 error_at (location, "invalid use of destructor %qD as a type", id);
2766 else if (TREE_CODE (decl) == TYPE_DECL)
2767 /* Something like 'unsigned A a;' */
2768 error_at (location, "invalid combination of multiple type-specifiers");
2769 else if (!parser->scope)
2771 /* Issue an error message. */
2772 error_at (location, "%qE does not name a type", id);
2773 /* If we're in a template class, it's possible that the user was
2774 referring to a type from a base class. For example:
2776 template <typename T> struct A { typedef T X; };
2777 template <typename T> struct B : public A<T> { X x; };
2779 The user should have said "typename A<T>::X". */
2780 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2781 inform (location, "C++11 %<constexpr%> only available with "
2782 "-std=c++11 or -std=gnu++11");
2783 else if (processing_template_decl && current_class_type
2784 && TYPE_BINFO (current_class_type))
2786 tree b;
2788 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2790 b = TREE_CHAIN (b))
2792 tree base_type = BINFO_TYPE (b);
2793 if (CLASS_TYPE_P (base_type)
2794 && dependent_type_p (base_type))
2796 tree field;
2797 /* Go from a particular instantiation of the
2798 template (which will have an empty TYPE_FIELDs),
2799 to the main version. */
2800 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2801 for (field = TYPE_FIELDS (base_type);
2802 field;
2803 field = DECL_CHAIN (field))
2804 if (TREE_CODE (field) == TYPE_DECL
2805 && DECL_NAME (field) == id)
2807 inform (location,
2808 "(perhaps %<typename %T::%E%> was intended)",
2809 BINFO_TYPE (b), id);
2810 break;
2812 if (field)
2813 break;
2818 /* Here we diagnose qualified-ids where the scope is actually correct,
2819 but the identifier does not resolve to a valid type name. */
2820 else if (parser->scope != error_mark_node)
2822 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2823 error_at (location, "%qE in namespace %qE does not name a type",
2824 id, parser->scope);
2825 else if (CLASS_TYPE_P (parser->scope)
2826 && constructor_name_p (id, parser->scope))
2828 /* A<T>::A<T>() */
2829 error_at (location, "%<%T::%E%> names the constructor, not"
2830 " the type", parser->scope, id);
2831 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2832 error_at (location, "and %qT has no template constructors",
2833 parser->scope);
2835 else if (TYPE_P (parser->scope)
2836 && dependent_scope_p (parser->scope))
2837 error_at (location, "need %<typename%> before %<%T::%E%> because "
2838 "%qT is a dependent scope",
2839 parser->scope, id, parser->scope);
2840 else if (TYPE_P (parser->scope))
2841 error_at (location, "%qE in %q#T does not name a type",
2842 id, parser->scope);
2843 else
2844 gcc_unreachable ();
2848 /* Check for a common situation where a type-name should be present,
2849 but is not, and issue a sensible error message. Returns true if an
2850 invalid type-name was detected.
2852 The situation handled by this function are variable declarations of the
2853 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2854 Usually, `ID' should name a type, but if we got here it means that it
2855 does not. We try to emit the best possible error message depending on
2856 how exactly the id-expression looks like. */
2858 static bool
2859 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2861 tree id;
2862 cp_token *token = cp_lexer_peek_token (parser->lexer);
2864 /* Avoid duplicate error about ambiguous lookup. */
2865 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2867 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2868 if (next->type == CPP_NAME && next->ambiguous_p)
2869 goto out;
2872 cp_parser_parse_tentatively (parser);
2873 id = cp_parser_id_expression (parser,
2874 /*template_keyword_p=*/false,
2875 /*check_dependency_p=*/true,
2876 /*template_p=*/NULL,
2877 /*declarator_p=*/true,
2878 /*optional_p=*/false);
2879 /* If the next token is a (, this is a function with no explicit return
2880 type, i.e. constructor, destructor or conversion op. */
2881 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2882 || TREE_CODE (id) == TYPE_DECL)
2884 cp_parser_abort_tentative_parse (parser);
2885 return false;
2887 if (!cp_parser_parse_definitely (parser))
2888 return false;
2890 /* Emit a diagnostic for the invalid type. */
2891 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2892 id, token->location);
2893 out:
2894 /* If we aren't in the middle of a declarator (i.e. in a
2895 parameter-declaration-clause), skip to the end of the declaration;
2896 there's no point in trying to process it. */
2897 if (!parser->in_declarator_p)
2898 cp_parser_skip_to_end_of_block_or_statement (parser);
2899 return true;
2902 /* Consume tokens up to, and including, the next non-nested closing `)'.
2903 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2904 are doing error recovery. Returns -1 if OR_COMMA is true and we
2905 found an unnested comma. */
2907 static int
2908 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2909 bool recovering,
2910 bool or_comma,
2911 bool consume_paren)
2913 unsigned paren_depth = 0;
2914 unsigned brace_depth = 0;
2915 unsigned square_depth = 0;
2917 if (recovering && !or_comma
2918 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2919 return 0;
2921 while (true)
2923 cp_token * token = cp_lexer_peek_token (parser->lexer);
2925 switch (token->type)
2927 case CPP_EOF:
2928 case CPP_PRAGMA_EOL:
2929 /* If we've run out of tokens, then there is no closing `)'. */
2930 return 0;
2932 /* This is good for lambda expression capture-lists. */
2933 case CPP_OPEN_SQUARE:
2934 ++square_depth;
2935 break;
2936 case CPP_CLOSE_SQUARE:
2937 if (!square_depth--)
2938 return 0;
2939 break;
2941 case CPP_SEMICOLON:
2942 /* This matches the processing in skip_to_end_of_statement. */
2943 if (!brace_depth)
2944 return 0;
2945 break;
2947 case CPP_OPEN_BRACE:
2948 ++brace_depth;
2949 break;
2950 case CPP_CLOSE_BRACE:
2951 if (!brace_depth--)
2952 return 0;
2953 break;
2955 case CPP_COMMA:
2956 if (recovering && or_comma && !brace_depth && !paren_depth
2957 && !square_depth)
2958 return -1;
2959 break;
2961 case CPP_OPEN_PAREN:
2962 if (!brace_depth)
2963 ++paren_depth;
2964 break;
2966 case CPP_CLOSE_PAREN:
2967 if (!brace_depth && !paren_depth--)
2969 if (consume_paren)
2970 cp_lexer_consume_token (parser->lexer);
2971 return 1;
2973 break;
2975 default:
2976 break;
2979 /* Consume the token. */
2980 cp_lexer_consume_token (parser->lexer);
2984 /* Consume tokens until we reach the end of the current statement.
2985 Normally, that will be just before consuming a `;'. However, if a
2986 non-nested `}' comes first, then we stop before consuming that. */
2988 static void
2989 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2991 unsigned nesting_depth = 0;
2993 while (true)
2995 cp_token *token = cp_lexer_peek_token (parser->lexer);
2997 switch (token->type)
2999 case CPP_EOF:
3000 case CPP_PRAGMA_EOL:
3001 /* If we've run out of tokens, stop. */
3002 return;
3004 case CPP_SEMICOLON:
3005 /* If the next token is a `;', we have reached the end of the
3006 statement. */
3007 if (!nesting_depth)
3008 return;
3009 break;
3011 case CPP_CLOSE_BRACE:
3012 /* If this is a non-nested '}', stop before consuming it.
3013 That way, when confronted with something like:
3015 { 3 + }
3017 we stop before consuming the closing '}', even though we
3018 have not yet reached a `;'. */
3019 if (nesting_depth == 0)
3020 return;
3022 /* If it is the closing '}' for a block that we have
3023 scanned, stop -- but only after consuming the token.
3024 That way given:
3026 void f g () { ... }
3027 typedef int I;
3029 we will stop after the body of the erroneously declared
3030 function, but before consuming the following `typedef'
3031 declaration. */
3032 if (--nesting_depth == 0)
3034 cp_lexer_consume_token (parser->lexer);
3035 return;
3038 case CPP_OPEN_BRACE:
3039 ++nesting_depth;
3040 break;
3042 default:
3043 break;
3046 /* Consume the token. */
3047 cp_lexer_consume_token (parser->lexer);
3051 /* This function is called at the end of a statement or declaration.
3052 If the next token is a semicolon, it is consumed; otherwise, error
3053 recovery is attempted. */
3055 static void
3056 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3058 /* Look for the trailing `;'. */
3059 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3061 /* If there is additional (erroneous) input, skip to the end of
3062 the statement. */
3063 cp_parser_skip_to_end_of_statement (parser);
3064 /* If the next token is now a `;', consume it. */
3065 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3066 cp_lexer_consume_token (parser->lexer);
3070 /* Skip tokens until we have consumed an entire block, or until we
3071 have consumed a non-nested `;'. */
3073 static void
3074 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3076 int nesting_depth = 0;
3078 while (nesting_depth >= 0)
3080 cp_token *token = cp_lexer_peek_token (parser->lexer);
3082 switch (token->type)
3084 case CPP_EOF:
3085 case CPP_PRAGMA_EOL:
3086 /* If we've run out of tokens, stop. */
3087 return;
3089 case CPP_SEMICOLON:
3090 /* Stop if this is an unnested ';'. */
3091 if (!nesting_depth)
3092 nesting_depth = -1;
3093 break;
3095 case CPP_CLOSE_BRACE:
3096 /* Stop if this is an unnested '}', or closes the outermost
3097 nesting level. */
3098 nesting_depth--;
3099 if (nesting_depth < 0)
3100 return;
3101 if (!nesting_depth)
3102 nesting_depth = -1;
3103 break;
3105 case CPP_OPEN_BRACE:
3106 /* Nest. */
3107 nesting_depth++;
3108 break;
3110 default:
3111 break;
3114 /* Consume the token. */
3115 cp_lexer_consume_token (parser->lexer);
3119 /* Skip tokens until a non-nested closing curly brace is the next
3120 token, or there are no more tokens. Return true in the first case,
3121 false otherwise. */
3123 static bool
3124 cp_parser_skip_to_closing_brace (cp_parser *parser)
3126 unsigned nesting_depth = 0;
3128 while (true)
3130 cp_token *token = cp_lexer_peek_token (parser->lexer);
3132 switch (token->type)
3134 case CPP_EOF:
3135 case CPP_PRAGMA_EOL:
3136 /* If we've run out of tokens, stop. */
3137 return false;
3139 case CPP_CLOSE_BRACE:
3140 /* If the next token is a non-nested `}', then we have reached
3141 the end of the current block. */
3142 if (nesting_depth-- == 0)
3143 return true;
3144 break;
3146 case CPP_OPEN_BRACE:
3147 /* If it the next token is a `{', then we are entering a new
3148 block. Consume the entire block. */
3149 ++nesting_depth;
3150 break;
3152 default:
3153 break;
3156 /* Consume the token. */
3157 cp_lexer_consume_token (parser->lexer);
3161 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3162 parameter is the PRAGMA token, allowing us to purge the entire pragma
3163 sequence. */
3165 static void
3166 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3168 cp_token *token;
3170 parser->lexer->in_pragma = false;
3173 token = cp_lexer_consume_token (parser->lexer);
3174 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3176 /* Ensure that the pragma is not parsed again. */
3177 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3180 /* Require pragma end of line, resyncing with it as necessary. The
3181 arguments are as for cp_parser_skip_to_pragma_eol. */
3183 static void
3184 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3186 parser->lexer->in_pragma = false;
3187 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3188 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3191 /* This is a simple wrapper around make_typename_type. When the id is
3192 an unresolved identifier node, we can provide a superior diagnostic
3193 using cp_parser_diagnose_invalid_type_name. */
3195 static tree
3196 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3197 tree id, location_t id_location)
3199 tree result;
3200 if (TREE_CODE (id) == IDENTIFIER_NODE)
3202 result = make_typename_type (scope, id, typename_type,
3203 /*complain=*/tf_none);
3204 if (result == error_mark_node)
3205 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3206 return result;
3208 return make_typename_type (scope, id, typename_type, tf_error);
3211 /* This is a wrapper around the
3212 make_{pointer,ptrmem,reference}_declarator functions that decides
3213 which one to call based on the CODE and CLASS_TYPE arguments. The
3214 CODE argument should be one of the values returned by
3215 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3216 appertain to the pointer or reference. */
3218 static cp_declarator *
3219 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3220 cp_cv_quals cv_qualifiers,
3221 cp_declarator *target,
3222 tree attributes)
3224 if (code == ERROR_MARK)
3225 return cp_error_declarator;
3227 if (code == INDIRECT_REF)
3228 if (class_type == NULL_TREE)
3229 return make_pointer_declarator (cv_qualifiers, target, attributes);
3230 else
3231 return make_ptrmem_declarator (cv_qualifiers, class_type,
3232 target, attributes);
3233 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3234 return make_reference_declarator (cv_qualifiers, target,
3235 false, attributes);
3236 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3237 return make_reference_declarator (cv_qualifiers, target,
3238 true, attributes);
3239 gcc_unreachable ();
3242 /* Create a new C++ parser. */
3244 static cp_parser *
3245 cp_parser_new (void)
3247 cp_parser *parser;
3248 cp_lexer *lexer;
3249 unsigned i;
3251 /* cp_lexer_new_main is called before doing GC allocation because
3252 cp_lexer_new_main might load a PCH file. */
3253 lexer = cp_lexer_new_main ();
3255 /* Initialize the binops_by_token so that we can get the tree
3256 directly from the token. */
3257 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3258 binops_by_token[binops[i].token_type] = binops[i];
3260 parser = ggc_alloc_cleared_cp_parser ();
3261 parser->lexer = lexer;
3262 parser->context = cp_parser_context_new (NULL);
3264 /* For now, we always accept GNU extensions. */
3265 parser->allow_gnu_extensions_p = 1;
3267 /* The `>' token is a greater-than operator, not the end of a
3268 template-id. */
3269 parser->greater_than_is_operator_p = true;
3271 parser->default_arg_ok_p = true;
3273 /* We are not parsing a constant-expression. */
3274 parser->integral_constant_expression_p = false;
3275 parser->allow_non_integral_constant_expression_p = false;
3276 parser->non_integral_constant_expression_p = false;
3278 /* Local variable names are not forbidden. */
3279 parser->local_variables_forbidden_p = false;
3281 /* We are not processing an `extern "C"' declaration. */
3282 parser->in_unbraced_linkage_specification_p = false;
3284 /* We are not processing a declarator. */
3285 parser->in_declarator_p = false;
3287 /* We are not processing a template-argument-list. */
3288 parser->in_template_argument_list_p = false;
3290 /* We are not in an iteration statement. */
3291 parser->in_statement = 0;
3293 /* We are not in a switch statement. */
3294 parser->in_switch_statement_p = false;
3296 /* We are not parsing a type-id inside an expression. */
3297 parser->in_type_id_in_expr_p = false;
3299 /* Declarations aren't implicitly extern "C". */
3300 parser->implicit_extern_c = false;
3302 /* String literals should be translated to the execution character set. */
3303 parser->translate_strings_p = true;
3305 /* We are not parsing a function body. */
3306 parser->in_function_body = false;
3308 /* We can correct until told otherwise. */
3309 parser->colon_corrects_to_scope_p = true;
3311 /* The unparsed function queue is empty. */
3312 push_unparsed_function_queues (parser);
3314 /* There are no classes being defined. */
3315 parser->num_classes_being_defined = 0;
3317 /* No template parameters apply. */
3318 parser->num_template_parameter_lists = 0;
3320 return parser;
3323 /* Create a cp_lexer structure which will emit the tokens in CACHE
3324 and push it onto the parser's lexer stack. This is used for delayed
3325 parsing of in-class method bodies and default arguments, and should
3326 not be confused with tentative parsing. */
3327 static void
3328 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3330 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3331 lexer->next = parser->lexer;
3332 parser->lexer = lexer;
3334 /* Move the current source position to that of the first token in the
3335 new lexer. */
3336 cp_lexer_set_source_position_from_token (lexer->next_token);
3339 /* Pop the top lexer off the parser stack. This is never used for the
3340 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3341 static void
3342 cp_parser_pop_lexer (cp_parser *parser)
3344 cp_lexer *lexer = parser->lexer;
3345 parser->lexer = lexer->next;
3346 cp_lexer_destroy (lexer);
3348 /* Put the current source position back where it was before this
3349 lexer was pushed. */
3350 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3353 /* Lexical conventions [gram.lex] */
3355 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3356 identifier. */
3358 static tree
3359 cp_parser_identifier (cp_parser* parser)
3361 cp_token *token;
3363 /* Look for the identifier. */
3364 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3365 /* Return the value. */
3366 return token ? token->u.value : error_mark_node;
3369 /* Parse a sequence of adjacent string constants. Returns a
3370 TREE_STRING representing the combined, nul-terminated string
3371 constant. If TRANSLATE is true, translate the string to the
3372 execution character set. If WIDE_OK is true, a wide string is
3373 invalid here.
3375 C++98 [lex.string] says that if a narrow string literal token is
3376 adjacent to a wide string literal token, the behavior is undefined.
3377 However, C99 6.4.5p4 says that this results in a wide string literal.
3378 We follow C99 here, for consistency with the C front end.
3380 This code is largely lifted from lex_string() in c-lex.c.
3382 FUTURE: ObjC++ will need to handle @-strings here. */
3383 static tree
3384 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3386 tree value;
3387 size_t count;
3388 struct obstack str_ob;
3389 cpp_string str, istr, *strs;
3390 cp_token *tok;
3391 enum cpp_ttype type, curr_type;
3392 int have_suffix_p = 0;
3393 tree string_tree;
3394 tree suffix_id = NULL_TREE;
3395 bool curr_tok_is_userdef_p = false;
3397 tok = cp_lexer_peek_token (parser->lexer);
3398 if (!cp_parser_is_string_literal (tok))
3400 cp_parser_error (parser, "expected string-literal");
3401 return error_mark_node;
3404 if (cpp_userdef_string_p (tok->type))
3406 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3407 curr_type = cpp_userdef_string_remove_type (tok->type);
3408 curr_tok_is_userdef_p = true;
3410 else
3412 string_tree = tok->u.value;
3413 curr_type = tok->type;
3415 type = curr_type;
3417 /* Try to avoid the overhead of creating and destroying an obstack
3418 for the common case of just one string. */
3419 if (!cp_parser_is_string_literal
3420 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3422 cp_lexer_consume_token (parser->lexer);
3424 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3425 str.len = TREE_STRING_LENGTH (string_tree);
3426 count = 1;
3428 if (curr_tok_is_userdef_p)
3430 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3431 have_suffix_p = 1;
3432 curr_type = cpp_userdef_string_remove_type (tok->type);
3434 else
3435 curr_type = tok->type;
3437 strs = &str;
3439 else
3441 gcc_obstack_init (&str_ob);
3442 count = 0;
3446 cp_lexer_consume_token (parser->lexer);
3447 count++;
3448 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3449 str.len = TREE_STRING_LENGTH (string_tree);
3451 if (curr_tok_is_userdef_p)
3453 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3454 if (have_suffix_p == 0)
3456 suffix_id = curr_suffix_id;
3457 have_suffix_p = 1;
3459 else if (have_suffix_p == 1
3460 && curr_suffix_id != suffix_id)
3462 error ("inconsistent user-defined literal suffixes"
3463 " %qD and %qD in string literal",
3464 suffix_id, curr_suffix_id);
3465 have_suffix_p = -1;
3467 curr_type = cpp_userdef_string_remove_type (tok->type);
3469 else
3470 curr_type = tok->type;
3472 if (type != curr_type)
3474 if (type == CPP_STRING)
3475 type = curr_type;
3476 else if (curr_type != CPP_STRING)
3477 error_at (tok->location,
3478 "unsupported non-standard concatenation "
3479 "of string literals");
3482 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3484 tok = cp_lexer_peek_token (parser->lexer);
3485 if (cpp_userdef_string_p (tok->type))
3487 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3488 curr_type = cpp_userdef_string_remove_type (tok->type);
3489 curr_tok_is_userdef_p = true;
3491 else
3493 string_tree = tok->u.value;
3494 curr_type = tok->type;
3495 curr_tok_is_userdef_p = false;
3498 while (cp_parser_is_string_literal (tok));
3500 strs = (cpp_string *) obstack_finish (&str_ob);
3503 if (type != CPP_STRING && !wide_ok)
3505 cp_parser_error (parser, "a wide string is invalid in this context");
3506 type = CPP_STRING;
3509 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3510 (parse_in, strs, count, &istr, type))
3512 value = build_string (istr.len, (const char *)istr.text);
3513 free (CONST_CAST (unsigned char *, istr.text));
3515 switch (type)
3517 default:
3518 case CPP_STRING:
3519 case CPP_UTF8STRING:
3520 TREE_TYPE (value) = char_array_type_node;
3521 break;
3522 case CPP_STRING16:
3523 TREE_TYPE (value) = char16_array_type_node;
3524 break;
3525 case CPP_STRING32:
3526 TREE_TYPE (value) = char32_array_type_node;
3527 break;
3528 case CPP_WSTRING:
3529 TREE_TYPE (value) = wchar_array_type_node;
3530 break;
3533 value = fix_string_type (value);
3535 if (have_suffix_p)
3537 tree literal = build_userdef_literal (suffix_id, value,
3538 OT_NONE, NULL_TREE);
3539 tok->u.value = literal;
3540 return cp_parser_userdef_string_literal (tok);
3543 else
3544 /* cpp_interpret_string has issued an error. */
3545 value = error_mark_node;
3547 if (count > 1)
3548 obstack_free (&str_ob, 0);
3550 return value;
3553 /* Look up a literal operator with the name and the exact arguments. */
3555 static tree
3556 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3558 tree decl, fns;
3559 decl = lookup_name (name);
3560 if (!decl || !is_overloaded_fn (decl))
3561 return error_mark_node;
3563 for (fns = decl; fns; fns = OVL_NEXT (fns))
3565 unsigned int ix;
3566 bool found = true;
3567 tree fn = OVL_CURRENT (fns);
3568 tree argtypes = NULL_TREE;
3569 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3570 if (argtypes != NULL_TREE)
3572 for (ix = 0; ix < vec_safe_length (args) && argtypes != NULL_TREE;
3573 ++ix, argtypes = TREE_CHAIN (argtypes))
3575 tree targ = TREE_VALUE (argtypes);
3576 tree tparm = TREE_TYPE ((*args)[ix]);
3577 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3578 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3579 if ((ptr || arr || !same_type_p (targ, tparm))
3580 && (!ptr || !arr
3581 || !same_type_p (TREE_TYPE (targ),
3582 TREE_TYPE (tparm))))
3583 found = false;
3585 if (found
3586 && ix == vec_safe_length (args)
3587 /* May be this should be sufficient_parms_p instead,
3588 depending on how exactly should user-defined literals
3589 work in presence of default arguments on the literal
3590 operator parameters. */
3591 && argtypes == void_list_node)
3592 return fn;
3596 return error_mark_node;
3599 /* Parse a user-defined char constant. Returns a call to a user-defined
3600 literal operator taking the character as an argument. */
3602 static tree
3603 cp_parser_userdef_char_literal (cp_parser *parser)
3605 cp_token *token = cp_lexer_consume_token (parser->lexer);
3606 tree literal = token->u.value;
3607 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3608 tree value = USERDEF_LITERAL_VALUE (literal);
3609 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3610 tree decl, result;
3612 /* Build up a call to the user-defined operator */
3613 /* Lookup the name we got back from the id-expression. */
3614 vec<tree, va_gc> *args = make_tree_vector ();
3615 vec_safe_push (args, value);
3616 decl = lookup_literal_operator (name, args);
3617 if (!decl || decl == error_mark_node)
3619 error ("unable to find character literal operator %qD with %qT argument",
3620 name, TREE_TYPE (value));
3621 release_tree_vector (args);
3622 return error_mark_node;
3624 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3625 release_tree_vector (args);
3626 if (result != error_mark_node)
3627 return result;
3629 error ("unable to find character literal operator %qD with %qT argument",
3630 name, TREE_TYPE (value));
3631 return error_mark_node;
3634 /* A subroutine of cp_parser_userdef_numeric_literal to
3635 create a char... template parameter pack from a string node. */
3637 static tree
3638 make_char_string_pack (tree value)
3640 tree charvec;
3641 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3642 const char *str = TREE_STRING_POINTER (value);
3643 int i, len = TREE_STRING_LENGTH (value) - 1;
3644 tree argvec = make_tree_vec (1);
3646 /* Fill in CHARVEC with all of the parameters. */
3647 charvec = make_tree_vec (len);
3648 for (i = 0; i < len; ++i)
3649 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3651 /* Build the argument packs. */
3652 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3653 TREE_TYPE (argpack) = char_type_node;
3655 TREE_VEC_ELT (argvec, 0) = argpack;
3657 return argvec;
3660 /* Parse a user-defined numeric constant. returns a call to a user-defined
3661 literal operator. */
3663 static tree
3664 cp_parser_userdef_numeric_literal (cp_parser *parser)
3666 cp_token *token = cp_lexer_consume_token (parser->lexer);
3667 tree literal = token->u.value;
3668 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3669 tree value = USERDEF_LITERAL_VALUE (literal);
3670 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3671 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3672 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3673 tree decl, result;
3674 vec<tree, va_gc> *args;
3676 /* Look for a literal operator taking the exact type of numeric argument
3677 as the literal value. */
3678 args = make_tree_vector ();
3679 vec_safe_push (args, value);
3680 decl = lookup_literal_operator (name, args);
3681 if (decl && decl != error_mark_node)
3683 result = finish_call_expr (decl, &args, false, true, tf_none);
3684 if (result != error_mark_node)
3686 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3687 warning_at (token->location, OPT_Woverflow,
3688 "integer literal exceeds range of %qT type",
3689 long_long_unsigned_type_node);
3690 else
3692 if (overflow > 0)
3693 warning_at (token->location, OPT_Woverflow,
3694 "floating literal exceeds range of %qT type",
3695 long_double_type_node);
3696 else if (overflow < 0)
3697 warning_at (token->location, OPT_Woverflow,
3698 "floating literal truncated to zero");
3700 release_tree_vector (args);
3701 return result;
3704 release_tree_vector (args);
3706 /* If the numeric argument didn't work, look for a raw literal
3707 operator taking a const char* argument consisting of the number
3708 in string format. */
3709 args = make_tree_vector ();
3710 vec_safe_push (args, num_string);
3711 decl = lookup_literal_operator (name, args);
3712 if (decl && decl != error_mark_node)
3714 result = finish_call_expr (decl, &args, false, true, tf_none);
3715 if (result != error_mark_node)
3717 release_tree_vector (args);
3718 return result;
3721 release_tree_vector (args);
3723 /* If the raw literal didn't work, look for a non-type template
3724 function with parameter pack char.... Call the function with
3725 template parameter characters representing the number. */
3726 args = make_tree_vector ();
3727 decl = lookup_literal_operator (name, args);
3728 if (decl && decl != error_mark_node)
3730 tree tmpl_args = make_char_string_pack (num_string);
3731 decl = lookup_template_function (decl, tmpl_args);
3732 result = finish_call_expr (decl, &args, false, true, tf_none);
3733 if (result != error_mark_node)
3735 release_tree_vector (args);
3736 return result;
3739 release_tree_vector (args);
3741 error ("unable to find numeric literal operator %qD", name);
3742 return error_mark_node;
3745 /* Parse a user-defined string constant. Returns a call to a user-defined
3746 literal operator taking a character pointer and the length of the string
3747 as arguments. */
3749 static tree
3750 cp_parser_userdef_string_literal (cp_token *token)
3752 tree literal = token->u.value;
3753 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3754 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3755 tree value = USERDEF_LITERAL_VALUE (literal);
3756 int len = TREE_STRING_LENGTH (value)
3757 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3758 tree decl, result;
3760 /* Build up a call to the user-defined operator */
3761 /* Lookup the name we got back from the id-expression. */
3762 vec<tree, va_gc> *args = make_tree_vector ();
3763 vec_safe_push (args, value);
3764 vec_safe_push (args, build_int_cst (size_type_node, len));
3765 decl = lookup_name (name);
3766 if (!decl || decl == error_mark_node)
3768 error ("unable to find string literal operator %qD", name);
3769 release_tree_vector (args);
3770 return error_mark_node;
3772 result = finish_call_expr (decl, &args, false, true, tf_none);
3773 release_tree_vector (args);
3774 if (result != error_mark_node)
3775 return result;
3777 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3778 name, TREE_TYPE (value), size_type_node);
3779 return error_mark_node;
3783 /* Basic concepts [gram.basic] */
3785 /* Parse a translation-unit.
3787 translation-unit:
3788 declaration-seq [opt]
3790 Returns TRUE if all went well. */
3792 static bool
3793 cp_parser_translation_unit (cp_parser* parser)
3795 /* The address of the first non-permanent object on the declarator
3796 obstack. */
3797 static void *declarator_obstack_base;
3799 bool success;
3801 /* Create the declarator obstack, if necessary. */
3802 if (!cp_error_declarator)
3804 gcc_obstack_init (&declarator_obstack);
3805 /* Create the error declarator. */
3806 cp_error_declarator = make_declarator (cdk_error);
3807 /* Create the empty parameter list. */
3808 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3809 /* Remember where the base of the declarator obstack lies. */
3810 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3813 cp_parser_declaration_seq_opt (parser);
3815 /* If there are no tokens left then all went well. */
3816 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3818 /* Get rid of the token array; we don't need it any more. */
3819 cp_lexer_destroy (parser->lexer);
3820 parser->lexer = NULL;
3822 /* This file might have been a context that's implicitly extern
3823 "C". If so, pop the lang context. (Only relevant for PCH.) */
3824 if (parser->implicit_extern_c)
3826 pop_lang_context ();
3827 parser->implicit_extern_c = false;
3830 /* Finish up. */
3831 finish_translation_unit ();
3833 success = true;
3835 else
3837 cp_parser_error (parser, "expected declaration");
3838 success = false;
3841 /* Make sure the declarator obstack was fully cleaned up. */
3842 gcc_assert (obstack_next_free (&declarator_obstack)
3843 == declarator_obstack_base);
3845 /* All went well. */
3846 return success;
3849 /* Return the appropriate tsubst flags for parsing, possibly in N3276
3850 decltype context. */
3852 static inline tsubst_flags_t
3853 complain_flags (bool decltype_p)
3855 tsubst_flags_t complain = tf_warning_or_error;
3856 if (decltype_p)
3857 complain |= tf_decltype;
3858 return complain;
3861 /* Expressions [gram.expr] */
3863 /* Parse a primary-expression.
3865 primary-expression:
3866 literal
3867 this
3868 ( expression )
3869 id-expression
3871 GNU Extensions:
3873 primary-expression:
3874 ( compound-statement )
3875 __builtin_va_arg ( assignment-expression , type-id )
3876 __builtin_offsetof ( type-id , offsetof-expression )
3878 C++ Extensions:
3879 __has_nothrow_assign ( type-id )
3880 __has_nothrow_constructor ( type-id )
3881 __has_nothrow_copy ( type-id )
3882 __has_trivial_assign ( type-id )
3883 __has_trivial_constructor ( type-id )
3884 __has_trivial_copy ( type-id )
3885 __has_trivial_destructor ( type-id )
3886 __has_virtual_destructor ( type-id )
3887 __is_abstract ( type-id )
3888 __is_base_of ( type-id , type-id )
3889 __is_class ( type-id )
3890 __is_convertible_to ( type-id , type-id )
3891 __is_empty ( type-id )
3892 __is_enum ( type-id )
3893 __is_final ( type-id )
3894 __is_literal_type ( type-id )
3895 __is_pod ( type-id )
3896 __is_polymorphic ( type-id )
3897 __is_std_layout ( type-id )
3898 __is_trivial ( type-id )
3899 __is_union ( type-id )
3901 Objective-C++ Extension:
3903 primary-expression:
3904 objc-expression
3906 literal:
3907 __null
3909 ADDRESS_P is true iff this expression was immediately preceded by
3910 "&" and therefore might denote a pointer-to-member. CAST_P is true
3911 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3912 true iff this expression is a template argument.
3914 Returns a representation of the expression. Upon return, *IDK
3915 indicates what kind of id-expression (if any) was present. */
3917 static tree
3918 cp_parser_primary_expression (cp_parser *parser,
3919 bool address_p,
3920 bool cast_p,
3921 bool template_arg_p,
3922 bool decltype_p,
3923 cp_id_kind *idk)
3925 cp_token *token = NULL;
3927 /* Assume the primary expression is not an id-expression. */
3928 *idk = CP_ID_KIND_NONE;
3930 /* Peek at the next token. */
3931 token = cp_lexer_peek_token (parser->lexer);
3932 switch (token->type)
3934 /* literal:
3935 integer-literal
3936 character-literal
3937 floating-literal
3938 string-literal
3939 boolean-literal
3940 pointer-literal
3941 user-defined-literal */
3942 case CPP_CHAR:
3943 case CPP_CHAR16:
3944 case CPP_CHAR32:
3945 case CPP_WCHAR:
3946 case CPP_NUMBER:
3947 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3948 return cp_parser_userdef_numeric_literal (parser);
3949 token = cp_lexer_consume_token (parser->lexer);
3950 if (TREE_CODE (token->u.value) == FIXED_CST)
3952 error_at (token->location,
3953 "fixed-point types not supported in C++");
3954 return error_mark_node;
3956 /* Floating-point literals are only allowed in an integral
3957 constant expression if they are cast to an integral or
3958 enumeration type. */
3959 if (TREE_CODE (token->u.value) == REAL_CST
3960 && parser->integral_constant_expression_p
3961 && pedantic)
3963 /* CAST_P will be set even in invalid code like "int(2.7 +
3964 ...)". Therefore, we have to check that the next token
3965 is sure to end the cast. */
3966 if (cast_p)
3968 cp_token *next_token;
3970 next_token = cp_lexer_peek_token (parser->lexer);
3971 if (/* The comma at the end of an
3972 enumerator-definition. */
3973 next_token->type != CPP_COMMA
3974 /* The curly brace at the end of an enum-specifier. */
3975 && next_token->type != CPP_CLOSE_BRACE
3976 /* The end of a statement. */
3977 && next_token->type != CPP_SEMICOLON
3978 /* The end of the cast-expression. */
3979 && next_token->type != CPP_CLOSE_PAREN
3980 /* The end of an array bound. */
3981 && next_token->type != CPP_CLOSE_SQUARE
3982 /* The closing ">" in a template-argument-list. */
3983 && (next_token->type != CPP_GREATER
3984 || parser->greater_than_is_operator_p)
3985 /* C++0x only: A ">>" treated like two ">" tokens,
3986 in a template-argument-list. */
3987 && (next_token->type != CPP_RSHIFT
3988 || (cxx_dialect == cxx98)
3989 || parser->greater_than_is_operator_p))
3990 cast_p = false;
3993 /* If we are within a cast, then the constraint that the
3994 cast is to an integral or enumeration type will be
3995 checked at that point. If we are not within a cast, then
3996 this code is invalid. */
3997 if (!cast_p)
3998 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4000 return token->u.value;
4002 case CPP_CHAR_USERDEF:
4003 case CPP_CHAR16_USERDEF:
4004 case CPP_CHAR32_USERDEF:
4005 case CPP_WCHAR_USERDEF:
4006 return cp_parser_userdef_char_literal (parser);
4008 case CPP_STRING:
4009 case CPP_STRING16:
4010 case CPP_STRING32:
4011 case CPP_WSTRING:
4012 case CPP_UTF8STRING:
4013 case CPP_STRING_USERDEF:
4014 case CPP_STRING16_USERDEF:
4015 case CPP_STRING32_USERDEF:
4016 case CPP_WSTRING_USERDEF:
4017 case CPP_UTF8STRING_USERDEF:
4018 /* ??? Should wide strings be allowed when parser->translate_strings_p
4019 is false (i.e. in attributes)? If not, we can kill the third
4020 argument to cp_parser_string_literal. */
4021 return cp_parser_string_literal (parser,
4022 parser->translate_strings_p,
4023 true);
4025 case CPP_OPEN_PAREN:
4027 tree expr;
4028 bool saved_greater_than_is_operator_p;
4030 /* Consume the `('. */
4031 cp_lexer_consume_token (parser->lexer);
4032 /* Within a parenthesized expression, a `>' token is always
4033 the greater-than operator. */
4034 saved_greater_than_is_operator_p
4035 = parser->greater_than_is_operator_p;
4036 parser->greater_than_is_operator_p = true;
4037 /* If we see `( { ' then we are looking at the beginning of
4038 a GNU statement-expression. */
4039 if (cp_parser_allow_gnu_extensions_p (parser)
4040 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4042 /* Statement-expressions are not allowed by the standard. */
4043 pedwarn (token->location, OPT_Wpedantic,
4044 "ISO C++ forbids braced-groups within expressions");
4046 /* And they're not allowed outside of a function-body; you
4047 cannot, for example, write:
4049 int i = ({ int j = 3; j + 1; });
4051 at class or namespace scope. */
4052 if (!parser->in_function_body
4053 || parser->in_template_argument_list_p)
4055 error_at (token->location,
4056 "statement-expressions are not allowed outside "
4057 "functions nor in template-argument lists");
4058 cp_parser_skip_to_end_of_block_or_statement (parser);
4059 expr = error_mark_node;
4061 else
4063 /* Start the statement-expression. */
4064 expr = begin_stmt_expr ();
4065 /* Parse the compound-statement. */
4066 cp_parser_compound_statement (parser, expr, false, false);
4067 /* Finish up. */
4068 expr = finish_stmt_expr (expr, false);
4071 else
4073 /* Parse the parenthesized expression. */
4074 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4075 /* Let the front end know that this expression was
4076 enclosed in parentheses. This matters in case, for
4077 example, the expression is of the form `A::B', since
4078 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4079 not. */
4080 finish_parenthesized_expr (expr);
4081 /* DR 705: Wrapping an unqualified name in parentheses
4082 suppresses arg-dependent lookup. We want to pass back
4083 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4084 (c++/37862), but none of the others. */
4085 if (*idk != CP_ID_KIND_QUALIFIED)
4086 *idk = CP_ID_KIND_NONE;
4088 /* The `>' token might be the end of a template-id or
4089 template-parameter-list now. */
4090 parser->greater_than_is_operator_p
4091 = saved_greater_than_is_operator_p;
4092 /* Consume the `)'. */
4093 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4094 cp_parser_skip_to_end_of_statement (parser);
4096 return expr;
4099 case CPP_OPEN_SQUARE:
4100 if (c_dialect_objc ())
4101 /* We have an Objective-C++ message. */
4102 return cp_parser_objc_expression (parser);
4104 tree lam = cp_parser_lambda_expression (parser);
4105 /* Don't warn about a failed tentative parse. */
4106 if (cp_parser_error_occurred (parser))
4107 return error_mark_node;
4108 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4109 return lam;
4112 case CPP_OBJC_STRING:
4113 if (c_dialect_objc ())
4114 /* We have an Objective-C++ string literal. */
4115 return cp_parser_objc_expression (parser);
4116 cp_parser_error (parser, "expected primary-expression");
4117 return error_mark_node;
4119 case CPP_KEYWORD:
4120 switch (token->keyword)
4122 /* These two are the boolean literals. */
4123 case RID_TRUE:
4124 cp_lexer_consume_token (parser->lexer);
4125 return boolean_true_node;
4126 case RID_FALSE:
4127 cp_lexer_consume_token (parser->lexer);
4128 return boolean_false_node;
4130 /* The `__null' literal. */
4131 case RID_NULL:
4132 cp_lexer_consume_token (parser->lexer);
4133 return null_node;
4135 /* The `nullptr' literal. */
4136 case RID_NULLPTR:
4137 cp_lexer_consume_token (parser->lexer);
4138 return nullptr_node;
4140 /* Recognize the `this' keyword. */
4141 case RID_THIS:
4142 cp_lexer_consume_token (parser->lexer);
4143 if (parser->local_variables_forbidden_p)
4145 error_at (token->location,
4146 "%<this%> may not be used in this context");
4147 return error_mark_node;
4149 /* Pointers cannot appear in constant-expressions. */
4150 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4151 return error_mark_node;
4152 return finish_this_expr ();
4154 /* The `operator' keyword can be the beginning of an
4155 id-expression. */
4156 case RID_OPERATOR:
4157 goto id_expression;
4159 case RID_FUNCTION_NAME:
4160 case RID_PRETTY_FUNCTION_NAME:
4161 case RID_C99_FUNCTION_NAME:
4163 non_integral_constant name;
4165 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4166 __func__ are the names of variables -- but they are
4167 treated specially. Therefore, they are handled here,
4168 rather than relying on the generic id-expression logic
4169 below. Grammatically, these names are id-expressions.
4171 Consume the token. */
4172 token = cp_lexer_consume_token (parser->lexer);
4174 switch (token->keyword)
4176 case RID_FUNCTION_NAME:
4177 name = NIC_FUNC_NAME;
4178 break;
4179 case RID_PRETTY_FUNCTION_NAME:
4180 name = NIC_PRETTY_FUNC;
4181 break;
4182 case RID_C99_FUNCTION_NAME:
4183 name = NIC_C99_FUNC;
4184 break;
4185 default:
4186 gcc_unreachable ();
4189 if (cp_parser_non_integral_constant_expression (parser, name))
4190 return error_mark_node;
4192 /* Look up the name. */
4193 return finish_fname (token->u.value);
4196 case RID_VA_ARG:
4198 tree expression;
4199 tree type;
4200 source_location type_location;
4202 /* The `__builtin_va_arg' construct is used to handle
4203 `va_arg'. Consume the `__builtin_va_arg' token. */
4204 cp_lexer_consume_token (parser->lexer);
4205 /* Look for the opening `('. */
4206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4207 /* Now, parse the assignment-expression. */
4208 expression = cp_parser_assignment_expression (parser,
4209 /*cast_p=*/false, NULL);
4210 /* Look for the `,'. */
4211 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4212 type_location = cp_lexer_peek_token (parser->lexer)->location;
4213 /* Parse the type-id. */
4214 type = cp_parser_type_id (parser);
4215 /* Look for the closing `)'. */
4216 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4217 /* Using `va_arg' in a constant-expression is not
4218 allowed. */
4219 if (cp_parser_non_integral_constant_expression (parser,
4220 NIC_VA_ARG))
4221 return error_mark_node;
4222 return build_x_va_arg (type_location, expression, type);
4225 case RID_OFFSETOF:
4226 return cp_parser_builtin_offsetof (parser);
4228 case RID_HAS_NOTHROW_ASSIGN:
4229 case RID_HAS_NOTHROW_CONSTRUCTOR:
4230 case RID_HAS_NOTHROW_COPY:
4231 case RID_HAS_TRIVIAL_ASSIGN:
4232 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4233 case RID_HAS_TRIVIAL_COPY:
4234 case RID_HAS_TRIVIAL_DESTRUCTOR:
4235 case RID_HAS_VIRTUAL_DESTRUCTOR:
4236 case RID_IS_ABSTRACT:
4237 case RID_IS_BASE_OF:
4238 case RID_IS_CLASS:
4239 case RID_IS_CONVERTIBLE_TO:
4240 case RID_IS_EMPTY:
4241 case RID_IS_ENUM:
4242 case RID_IS_FINAL:
4243 case RID_IS_LITERAL_TYPE:
4244 case RID_IS_POD:
4245 case RID_IS_POLYMORPHIC:
4246 case RID_IS_STD_LAYOUT:
4247 case RID_IS_TRIVIAL:
4248 case RID_IS_UNION:
4249 return cp_parser_trait_expr (parser, token->keyword);
4251 /* Objective-C++ expressions. */
4252 case RID_AT_ENCODE:
4253 case RID_AT_PROTOCOL:
4254 case RID_AT_SELECTOR:
4255 return cp_parser_objc_expression (parser);
4257 case RID_TEMPLATE:
4258 if (parser->in_function_body
4259 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4260 == CPP_LESS))
4262 error_at (token->location,
4263 "a template declaration cannot appear at block scope");
4264 cp_parser_skip_to_end_of_block_or_statement (parser);
4265 return error_mark_node;
4267 default:
4268 cp_parser_error (parser, "expected primary-expression");
4269 return error_mark_node;
4272 /* An id-expression can start with either an identifier, a
4273 `::' as the beginning of a qualified-id, or the "operator"
4274 keyword. */
4275 case CPP_NAME:
4276 case CPP_SCOPE:
4277 case CPP_TEMPLATE_ID:
4278 case CPP_NESTED_NAME_SPECIFIER:
4280 tree id_expression;
4281 tree decl;
4282 const char *error_msg;
4283 bool template_p;
4284 bool done;
4285 cp_token *id_expr_token;
4287 id_expression:
4288 /* Parse the id-expression. */
4289 id_expression
4290 = cp_parser_id_expression (parser,
4291 /*template_keyword_p=*/false,
4292 /*check_dependency_p=*/true,
4293 &template_p,
4294 /*declarator_p=*/false,
4295 /*optional_p=*/false);
4296 if (id_expression == error_mark_node)
4297 return error_mark_node;
4298 id_expr_token = token;
4299 token = cp_lexer_peek_token (parser->lexer);
4300 done = (token->type != CPP_OPEN_SQUARE
4301 && token->type != CPP_OPEN_PAREN
4302 && token->type != CPP_DOT
4303 && token->type != CPP_DEREF
4304 && token->type != CPP_PLUS_PLUS
4305 && token->type != CPP_MINUS_MINUS);
4306 /* If we have a template-id, then no further lookup is
4307 required. If the template-id was for a template-class, we
4308 will sometimes have a TYPE_DECL at this point. */
4309 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4310 || TREE_CODE (id_expression) == TYPE_DECL)
4311 decl = id_expression;
4312 /* Look up the name. */
4313 else
4315 tree ambiguous_decls;
4317 /* If we already know that this lookup is ambiguous, then
4318 we've already issued an error message; there's no reason
4319 to check again. */
4320 if (id_expr_token->type == CPP_NAME
4321 && id_expr_token->ambiguous_p)
4323 cp_parser_simulate_error (parser);
4324 return error_mark_node;
4327 decl = cp_parser_lookup_name (parser, id_expression,
4328 none_type,
4329 template_p,
4330 /*is_namespace=*/false,
4331 /*check_dependency=*/true,
4332 &ambiguous_decls,
4333 id_expr_token->location);
4334 /* If the lookup was ambiguous, an error will already have
4335 been issued. */
4336 if (ambiguous_decls)
4337 return error_mark_node;
4339 /* In Objective-C++, we may have an Objective-C 2.0
4340 dot-syntax for classes here. */
4341 if (c_dialect_objc ()
4342 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4343 && TREE_CODE (decl) == TYPE_DECL
4344 && objc_is_class_name (decl))
4346 tree component;
4347 cp_lexer_consume_token (parser->lexer);
4348 component = cp_parser_identifier (parser);
4349 if (component == error_mark_node)
4350 return error_mark_node;
4352 return objc_build_class_component_ref (id_expression, component);
4355 /* In Objective-C++, an instance variable (ivar) may be preferred
4356 to whatever cp_parser_lookup_name() found. */
4357 decl = objc_lookup_ivar (decl, id_expression);
4359 /* If name lookup gives us a SCOPE_REF, then the
4360 qualifying scope was dependent. */
4361 if (TREE_CODE (decl) == SCOPE_REF)
4363 /* At this point, we do not know if DECL is a valid
4364 integral constant expression. We assume that it is
4365 in fact such an expression, so that code like:
4367 template <int N> struct A {
4368 int a[B<N>::i];
4371 is accepted. At template-instantiation time, we
4372 will check that B<N>::i is actually a constant. */
4373 return decl;
4375 /* Check to see if DECL is a local variable in a context
4376 where that is forbidden. */
4377 if (parser->local_variables_forbidden_p
4378 && local_variable_p (decl))
4380 /* It might be that we only found DECL because we are
4381 trying to be generous with pre-ISO scoping rules.
4382 For example, consider:
4384 int i;
4385 void g() {
4386 for (int i = 0; i < 10; ++i) {}
4387 extern void f(int j = i);
4390 Here, name look up will originally find the out
4391 of scope `i'. We need to issue a warning message,
4392 but then use the global `i'. */
4393 decl = check_for_out_of_scope_variable (decl);
4394 if (local_variable_p (decl))
4396 error_at (id_expr_token->location,
4397 "local variable %qD may not appear in this context",
4398 decl);
4399 return error_mark_node;
4404 decl = (finish_id_expression
4405 (id_expression, decl, parser->scope,
4406 idk,
4407 parser->integral_constant_expression_p,
4408 parser->allow_non_integral_constant_expression_p,
4409 &parser->non_integral_constant_expression_p,
4410 template_p, done, address_p,
4411 template_arg_p,
4412 &error_msg,
4413 id_expr_token->location));
4414 if (error_msg)
4415 cp_parser_error (parser, error_msg);
4416 return decl;
4419 /* Anything else is an error. */
4420 default:
4421 cp_parser_error (parser, "expected primary-expression");
4422 return error_mark_node;
4426 static inline tree
4427 cp_parser_primary_expression (cp_parser *parser,
4428 bool address_p,
4429 bool cast_p,
4430 bool template_arg_p,
4431 cp_id_kind *idk)
4433 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4434 /*decltype*/false, idk);
4437 /* Parse an id-expression.
4439 id-expression:
4440 unqualified-id
4441 qualified-id
4443 qualified-id:
4444 :: [opt] nested-name-specifier template [opt] unqualified-id
4445 :: identifier
4446 :: operator-function-id
4447 :: template-id
4449 Return a representation of the unqualified portion of the
4450 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4451 a `::' or nested-name-specifier.
4453 Often, if the id-expression was a qualified-id, the caller will
4454 want to make a SCOPE_REF to represent the qualified-id. This
4455 function does not do this in order to avoid wastefully creating
4456 SCOPE_REFs when they are not required.
4458 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4459 `template' keyword.
4461 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4462 uninstantiated templates.
4464 If *TEMPLATE_P is non-NULL, it is set to true iff the
4465 `template' keyword is used to explicitly indicate that the entity
4466 named is a template.
4468 If DECLARATOR_P is true, the id-expression is appearing as part of
4469 a declarator, rather than as part of an expression. */
4471 static tree
4472 cp_parser_id_expression (cp_parser *parser,
4473 bool template_keyword_p,
4474 bool check_dependency_p,
4475 bool *template_p,
4476 bool declarator_p,
4477 bool optional_p)
4479 bool global_scope_p;
4480 bool nested_name_specifier_p;
4482 /* Assume the `template' keyword was not used. */
4483 if (template_p)
4484 *template_p = template_keyword_p;
4486 /* Look for the optional `::' operator. */
4487 global_scope_p
4488 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4489 != NULL_TREE);
4490 /* Look for the optional nested-name-specifier. */
4491 nested_name_specifier_p
4492 = (cp_parser_nested_name_specifier_opt (parser,
4493 /*typename_keyword_p=*/false,
4494 check_dependency_p,
4495 /*type_p=*/false,
4496 declarator_p)
4497 != NULL_TREE);
4498 /* If there is a nested-name-specifier, then we are looking at
4499 the first qualified-id production. */
4500 if (nested_name_specifier_p)
4502 tree saved_scope;
4503 tree saved_object_scope;
4504 tree saved_qualifying_scope;
4505 tree unqualified_id;
4506 bool is_template;
4508 /* See if the next token is the `template' keyword. */
4509 if (!template_p)
4510 template_p = &is_template;
4511 *template_p = cp_parser_optional_template_keyword (parser);
4512 /* Name lookup we do during the processing of the
4513 unqualified-id might obliterate SCOPE. */
4514 saved_scope = parser->scope;
4515 saved_object_scope = parser->object_scope;
4516 saved_qualifying_scope = parser->qualifying_scope;
4517 /* Process the final unqualified-id. */
4518 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4519 check_dependency_p,
4520 declarator_p,
4521 /*optional_p=*/false);
4522 /* Restore the SAVED_SCOPE for our caller. */
4523 parser->scope = saved_scope;
4524 parser->object_scope = saved_object_scope;
4525 parser->qualifying_scope = saved_qualifying_scope;
4527 return unqualified_id;
4529 /* Otherwise, if we are in global scope, then we are looking at one
4530 of the other qualified-id productions. */
4531 else if (global_scope_p)
4533 cp_token *token;
4534 tree id;
4536 /* Peek at the next token. */
4537 token = cp_lexer_peek_token (parser->lexer);
4539 /* If it's an identifier, and the next token is not a "<", then
4540 we can avoid the template-id case. This is an optimization
4541 for this common case. */
4542 if (token->type == CPP_NAME
4543 && !cp_parser_nth_token_starts_template_argument_list_p
4544 (parser, 2))
4545 return cp_parser_identifier (parser);
4547 cp_parser_parse_tentatively (parser);
4548 /* Try a template-id. */
4549 id = cp_parser_template_id (parser,
4550 /*template_keyword_p=*/false,
4551 /*check_dependency_p=*/true,
4552 none_type,
4553 declarator_p);
4554 /* If that worked, we're done. */
4555 if (cp_parser_parse_definitely (parser))
4556 return id;
4558 /* Peek at the next token. (Changes in the token buffer may
4559 have invalidated the pointer obtained above.) */
4560 token = cp_lexer_peek_token (parser->lexer);
4562 switch (token->type)
4564 case CPP_NAME:
4565 return cp_parser_identifier (parser);
4567 case CPP_KEYWORD:
4568 if (token->keyword == RID_OPERATOR)
4569 return cp_parser_operator_function_id (parser);
4570 /* Fall through. */
4572 default:
4573 cp_parser_error (parser, "expected id-expression");
4574 return error_mark_node;
4577 else
4578 return cp_parser_unqualified_id (parser, template_keyword_p,
4579 /*check_dependency_p=*/true,
4580 declarator_p,
4581 optional_p);
4584 /* Parse an unqualified-id.
4586 unqualified-id:
4587 identifier
4588 operator-function-id
4589 conversion-function-id
4590 ~ class-name
4591 template-id
4593 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4594 keyword, in a construct like `A::template ...'.
4596 Returns a representation of unqualified-id. For the `identifier'
4597 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4598 production a BIT_NOT_EXPR is returned; the operand of the
4599 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4600 other productions, see the documentation accompanying the
4601 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4602 names are looked up in uninstantiated templates. If DECLARATOR_P
4603 is true, the unqualified-id is appearing as part of a declarator,
4604 rather than as part of an expression. */
4606 static tree
4607 cp_parser_unqualified_id (cp_parser* parser,
4608 bool template_keyword_p,
4609 bool check_dependency_p,
4610 bool declarator_p,
4611 bool optional_p)
4613 cp_token *token;
4615 /* Peek at the next token. */
4616 token = cp_lexer_peek_token (parser->lexer);
4618 switch (token->type)
4620 case CPP_NAME:
4622 tree id;
4624 /* We don't know yet whether or not this will be a
4625 template-id. */
4626 cp_parser_parse_tentatively (parser);
4627 /* Try a template-id. */
4628 id = cp_parser_template_id (parser, template_keyword_p,
4629 check_dependency_p,
4630 none_type,
4631 declarator_p);
4632 /* If it worked, we're done. */
4633 if (cp_parser_parse_definitely (parser))
4634 return id;
4635 /* Otherwise, it's an ordinary identifier. */
4636 return cp_parser_identifier (parser);
4639 case CPP_TEMPLATE_ID:
4640 return cp_parser_template_id (parser, template_keyword_p,
4641 check_dependency_p,
4642 none_type,
4643 declarator_p);
4645 case CPP_COMPL:
4647 tree type_decl;
4648 tree qualifying_scope;
4649 tree object_scope;
4650 tree scope;
4651 bool done;
4653 /* Consume the `~' token. */
4654 cp_lexer_consume_token (parser->lexer);
4655 /* Parse the class-name. The standard, as written, seems to
4656 say that:
4658 template <typename T> struct S { ~S (); };
4659 template <typename T> S<T>::~S() {}
4661 is invalid, since `~' must be followed by a class-name, but
4662 `S<T>' is dependent, and so not known to be a class.
4663 That's not right; we need to look in uninstantiated
4664 templates. A further complication arises from:
4666 template <typename T> void f(T t) {
4667 t.T::~T();
4670 Here, it is not possible to look up `T' in the scope of `T'
4671 itself. We must look in both the current scope, and the
4672 scope of the containing complete expression.
4674 Yet another issue is:
4676 struct S {
4677 int S;
4678 ~S();
4681 S::~S() {}
4683 The standard does not seem to say that the `S' in `~S'
4684 should refer to the type `S' and not the data member
4685 `S::S'. */
4687 /* DR 244 says that we look up the name after the "~" in the
4688 same scope as we looked up the qualifying name. That idea
4689 isn't fully worked out; it's more complicated than that. */
4690 scope = parser->scope;
4691 object_scope = parser->object_scope;
4692 qualifying_scope = parser->qualifying_scope;
4694 /* Check for invalid scopes. */
4695 if (scope == error_mark_node)
4697 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4698 cp_lexer_consume_token (parser->lexer);
4699 return error_mark_node;
4701 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4703 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4704 error_at (token->location,
4705 "scope %qT before %<~%> is not a class-name",
4706 scope);
4707 cp_parser_simulate_error (parser);
4708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4709 cp_lexer_consume_token (parser->lexer);
4710 return error_mark_node;
4712 gcc_assert (!scope || TYPE_P (scope));
4714 /* If the name is of the form "X::~X" it's OK even if X is a
4715 typedef. */
4716 token = cp_lexer_peek_token (parser->lexer);
4717 if (scope
4718 && token->type == CPP_NAME
4719 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4720 != CPP_LESS)
4721 && (token->u.value == TYPE_IDENTIFIER (scope)
4722 || (CLASS_TYPE_P (scope)
4723 && constructor_name_p (token->u.value, scope))))
4725 cp_lexer_consume_token (parser->lexer);
4726 return build_nt (BIT_NOT_EXPR, scope);
4729 /* If there was an explicit qualification (S::~T), first look
4730 in the scope given by the qualification (i.e., S).
4732 Note: in the calls to cp_parser_class_name below we pass
4733 typename_type so that lookup finds the injected-class-name
4734 rather than the constructor. */
4735 done = false;
4736 type_decl = NULL_TREE;
4737 if (scope)
4739 cp_parser_parse_tentatively (parser);
4740 type_decl = cp_parser_class_name (parser,
4741 /*typename_keyword_p=*/false,
4742 /*template_keyword_p=*/false,
4743 typename_type,
4744 /*check_dependency=*/false,
4745 /*class_head_p=*/false,
4746 declarator_p);
4747 if (cp_parser_parse_definitely (parser))
4748 done = true;
4750 /* In "N::S::~S", look in "N" as well. */
4751 if (!done && scope && qualifying_scope)
4753 cp_parser_parse_tentatively (parser);
4754 parser->scope = qualifying_scope;
4755 parser->object_scope = NULL_TREE;
4756 parser->qualifying_scope = NULL_TREE;
4757 type_decl
4758 = cp_parser_class_name (parser,
4759 /*typename_keyword_p=*/false,
4760 /*template_keyword_p=*/false,
4761 typename_type,
4762 /*check_dependency=*/false,
4763 /*class_head_p=*/false,
4764 declarator_p);
4765 if (cp_parser_parse_definitely (parser))
4766 done = true;
4768 /* In "p->S::~T", look in the scope given by "*p" as well. */
4769 else if (!done && object_scope)
4771 cp_parser_parse_tentatively (parser);
4772 parser->scope = object_scope;
4773 parser->object_scope = NULL_TREE;
4774 parser->qualifying_scope = NULL_TREE;
4775 type_decl
4776 = cp_parser_class_name (parser,
4777 /*typename_keyword_p=*/false,
4778 /*template_keyword_p=*/false,
4779 typename_type,
4780 /*check_dependency=*/false,
4781 /*class_head_p=*/false,
4782 declarator_p);
4783 if (cp_parser_parse_definitely (parser))
4784 done = true;
4786 /* Look in the surrounding context. */
4787 if (!done)
4789 parser->scope = NULL_TREE;
4790 parser->object_scope = NULL_TREE;
4791 parser->qualifying_scope = NULL_TREE;
4792 if (processing_template_decl)
4793 cp_parser_parse_tentatively (parser);
4794 type_decl
4795 = cp_parser_class_name (parser,
4796 /*typename_keyword_p=*/false,
4797 /*template_keyword_p=*/false,
4798 typename_type,
4799 /*check_dependency=*/false,
4800 /*class_head_p=*/false,
4801 declarator_p);
4802 if (processing_template_decl
4803 && ! cp_parser_parse_definitely (parser))
4805 /* We couldn't find a type with this name, so just accept
4806 it and check for a match at instantiation time. */
4807 type_decl = cp_parser_identifier (parser);
4808 if (type_decl != error_mark_node)
4809 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4810 return type_decl;
4813 /* If an error occurred, assume that the name of the
4814 destructor is the same as the name of the qualifying
4815 class. That allows us to keep parsing after running
4816 into ill-formed destructor names. */
4817 if (type_decl == error_mark_node && scope)
4818 return build_nt (BIT_NOT_EXPR, scope);
4819 else if (type_decl == error_mark_node)
4820 return error_mark_node;
4822 /* Check that destructor name and scope match. */
4823 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4825 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4826 error_at (token->location,
4827 "declaration of %<~%T%> as member of %qT",
4828 type_decl, scope);
4829 cp_parser_simulate_error (parser);
4830 return error_mark_node;
4833 /* [class.dtor]
4835 A typedef-name that names a class shall not be used as the
4836 identifier in the declarator for a destructor declaration. */
4837 if (declarator_p
4838 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4839 && !DECL_SELF_REFERENCE_P (type_decl)
4840 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4841 error_at (token->location,
4842 "typedef-name %qD used as destructor declarator",
4843 type_decl);
4845 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4848 case CPP_KEYWORD:
4849 if (token->keyword == RID_OPERATOR)
4851 tree id;
4853 /* This could be a template-id, so we try that first. */
4854 cp_parser_parse_tentatively (parser);
4855 /* Try a template-id. */
4856 id = cp_parser_template_id (parser, template_keyword_p,
4857 /*check_dependency_p=*/true,
4858 none_type,
4859 declarator_p);
4860 /* If that worked, we're done. */
4861 if (cp_parser_parse_definitely (parser))
4862 return id;
4863 /* We still don't know whether we're looking at an
4864 operator-function-id or a conversion-function-id. */
4865 cp_parser_parse_tentatively (parser);
4866 /* Try an operator-function-id. */
4867 id = cp_parser_operator_function_id (parser);
4868 /* If that didn't work, try a conversion-function-id. */
4869 if (!cp_parser_parse_definitely (parser))
4870 id = cp_parser_conversion_function_id (parser);
4871 else if (UDLIT_OPER_P (id))
4873 /* 17.6.3.3.5 */
4874 const char *name = UDLIT_OP_SUFFIX (id);
4875 if (name[0] != '_' && !in_system_header)
4876 warning (0, "literal operator suffixes not preceded by %<_%>"
4877 " are reserved for future standardization");
4880 return id;
4882 /* Fall through. */
4884 default:
4885 if (optional_p)
4886 return NULL_TREE;
4887 cp_parser_error (parser, "expected unqualified-id");
4888 return error_mark_node;
4892 /* Parse an (optional) nested-name-specifier.
4894 nested-name-specifier: [C++98]
4895 class-or-namespace-name :: nested-name-specifier [opt]
4896 class-or-namespace-name :: template nested-name-specifier [opt]
4898 nested-name-specifier: [C++0x]
4899 type-name ::
4900 namespace-name ::
4901 nested-name-specifier identifier ::
4902 nested-name-specifier template [opt] simple-template-id ::
4904 PARSER->SCOPE should be set appropriately before this function is
4905 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4906 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4907 in name lookups.
4909 Sets PARSER->SCOPE to the class (TYPE) or namespace
4910 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4911 it unchanged if there is no nested-name-specifier. Returns the new
4912 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4914 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4915 part of a declaration and/or decl-specifier. */
4917 static tree
4918 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4919 bool typename_keyword_p,
4920 bool check_dependency_p,
4921 bool type_p,
4922 bool is_declaration)
4924 bool success = false;
4925 cp_token_position start = 0;
4926 cp_token *token;
4928 /* Remember where the nested-name-specifier starts. */
4929 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4931 start = cp_lexer_token_position (parser->lexer, false);
4932 push_deferring_access_checks (dk_deferred);
4935 while (true)
4937 tree new_scope;
4938 tree old_scope;
4939 tree saved_qualifying_scope;
4940 bool template_keyword_p;
4942 /* Spot cases that cannot be the beginning of a
4943 nested-name-specifier. */
4944 token = cp_lexer_peek_token (parser->lexer);
4946 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4947 the already parsed nested-name-specifier. */
4948 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4950 /* Grab the nested-name-specifier and continue the loop. */
4951 cp_parser_pre_parsed_nested_name_specifier (parser);
4952 /* If we originally encountered this nested-name-specifier
4953 with IS_DECLARATION set to false, we will not have
4954 resolved TYPENAME_TYPEs, so we must do so here. */
4955 if (is_declaration
4956 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4958 new_scope = resolve_typename_type (parser->scope,
4959 /*only_current_p=*/false);
4960 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4961 parser->scope = new_scope;
4963 success = true;
4964 continue;
4967 /* Spot cases that cannot be the beginning of a
4968 nested-name-specifier. On the second and subsequent times
4969 through the loop, we look for the `template' keyword. */
4970 if (success && token->keyword == RID_TEMPLATE)
4972 /* A template-id can start a nested-name-specifier. */
4973 else if (token->type == CPP_TEMPLATE_ID)
4975 /* DR 743: decltype can be used in a nested-name-specifier. */
4976 else if (token_is_decltype (token))
4978 else
4980 /* If the next token is not an identifier, then it is
4981 definitely not a type-name or namespace-name. */
4982 if (token->type != CPP_NAME)
4983 break;
4984 /* If the following token is neither a `<' (to begin a
4985 template-id), nor a `::', then we are not looking at a
4986 nested-name-specifier. */
4987 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4989 if (token->type == CPP_COLON
4990 && parser->colon_corrects_to_scope_p
4991 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4993 error_at (token->location,
4994 "found %<:%> in nested-name-specifier, expected %<::%>");
4995 token->type = CPP_SCOPE;
4998 if (token->type != CPP_SCOPE
4999 && !cp_parser_nth_token_starts_template_argument_list_p
5000 (parser, 2))
5001 break;
5004 /* The nested-name-specifier is optional, so we parse
5005 tentatively. */
5006 cp_parser_parse_tentatively (parser);
5008 /* Look for the optional `template' keyword, if this isn't the
5009 first time through the loop. */
5010 if (success)
5011 template_keyword_p = cp_parser_optional_template_keyword (parser);
5012 else
5013 template_keyword_p = false;
5015 /* Save the old scope since the name lookup we are about to do
5016 might destroy it. */
5017 old_scope = parser->scope;
5018 saved_qualifying_scope = parser->qualifying_scope;
5019 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5020 look up names in "X<T>::I" in order to determine that "Y" is
5021 a template. So, if we have a typename at this point, we make
5022 an effort to look through it. */
5023 if (is_declaration
5024 && !typename_keyword_p
5025 && parser->scope
5026 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5027 parser->scope = resolve_typename_type (parser->scope,
5028 /*only_current_p=*/false);
5029 /* Parse the qualifying entity. */
5030 new_scope
5031 = cp_parser_qualifying_entity (parser,
5032 typename_keyword_p,
5033 template_keyword_p,
5034 check_dependency_p,
5035 type_p,
5036 is_declaration);
5037 /* Look for the `::' token. */
5038 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5040 /* If we found what we wanted, we keep going; otherwise, we're
5041 done. */
5042 if (!cp_parser_parse_definitely (parser))
5044 bool error_p = false;
5046 /* Restore the OLD_SCOPE since it was valid before the
5047 failed attempt at finding the last
5048 class-or-namespace-name. */
5049 parser->scope = old_scope;
5050 parser->qualifying_scope = saved_qualifying_scope;
5052 /* If the next token is a decltype, and the one after that is a
5053 `::', then the decltype has failed to resolve to a class or
5054 enumeration type. Give this error even when parsing
5055 tentatively since it can't possibly be valid--and we're going
5056 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5057 won't get another chance.*/
5058 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5059 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5060 == CPP_SCOPE))
5062 token = cp_lexer_consume_token (parser->lexer);
5063 error_at (token->location, "decltype evaluates to %qT, "
5064 "which is not a class or enumeration type",
5065 token->u.value);
5066 parser->scope = error_mark_node;
5067 error_p = true;
5068 /* As below. */
5069 success = true;
5070 cp_lexer_consume_token (parser->lexer);
5073 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5074 break;
5075 /* If the next token is an identifier, and the one after
5076 that is a `::', then any valid interpretation would have
5077 found a class-or-namespace-name. */
5078 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5079 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5080 == CPP_SCOPE)
5081 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5082 != CPP_COMPL))
5084 token = cp_lexer_consume_token (parser->lexer);
5085 if (!error_p)
5087 if (!token->ambiguous_p)
5089 tree decl;
5090 tree ambiguous_decls;
5092 decl = cp_parser_lookup_name (parser, token->u.value,
5093 none_type,
5094 /*is_template=*/false,
5095 /*is_namespace=*/false,
5096 /*check_dependency=*/true,
5097 &ambiguous_decls,
5098 token->location);
5099 if (TREE_CODE (decl) == TEMPLATE_DECL)
5100 error_at (token->location,
5101 "%qD used without template parameters",
5102 decl);
5103 else if (ambiguous_decls)
5105 error_at (token->location,
5106 "reference to %qD is ambiguous",
5107 token->u.value);
5108 print_candidates (ambiguous_decls);
5109 decl = error_mark_node;
5111 else
5113 if (cxx_dialect != cxx98)
5114 cp_parser_name_lookup_error
5115 (parser, token->u.value, decl, NLE_NOT_CXX98,
5116 token->location);
5117 else
5118 cp_parser_name_lookup_error
5119 (parser, token->u.value, decl, NLE_CXX98,
5120 token->location);
5123 parser->scope = error_mark_node;
5124 error_p = true;
5125 /* Treat this as a successful nested-name-specifier
5126 due to:
5128 [basic.lookup.qual]
5130 If the name found is not a class-name (clause
5131 _class_) or namespace-name (_namespace.def_), the
5132 program is ill-formed. */
5133 success = true;
5135 cp_lexer_consume_token (parser->lexer);
5137 break;
5139 /* We've found one valid nested-name-specifier. */
5140 success = true;
5141 /* Name lookup always gives us a DECL. */
5142 if (TREE_CODE (new_scope) == TYPE_DECL)
5143 new_scope = TREE_TYPE (new_scope);
5144 /* Uses of "template" must be followed by actual templates. */
5145 if (template_keyword_p
5146 && !(CLASS_TYPE_P (new_scope)
5147 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5148 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5149 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5150 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5151 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5152 == TEMPLATE_ID_EXPR)))
5153 permerror (input_location, TYPE_P (new_scope)
5154 ? G_("%qT is not a template")
5155 : G_("%qD is not a template"),
5156 new_scope);
5157 /* If it is a class scope, try to complete it; we are about to
5158 be looking up names inside the class. */
5159 if (TYPE_P (new_scope)
5160 /* Since checking types for dependency can be expensive,
5161 avoid doing it if the type is already complete. */
5162 && !COMPLETE_TYPE_P (new_scope)
5163 /* Do not try to complete dependent types. */
5164 && !dependent_type_p (new_scope))
5166 new_scope = complete_type (new_scope);
5167 /* If it is a typedef to current class, use the current
5168 class instead, as the typedef won't have any names inside
5169 it yet. */
5170 if (!COMPLETE_TYPE_P (new_scope)
5171 && currently_open_class (new_scope))
5172 new_scope = TYPE_MAIN_VARIANT (new_scope);
5174 /* Make sure we look in the right scope the next time through
5175 the loop. */
5176 parser->scope = new_scope;
5179 /* If parsing tentatively, replace the sequence of tokens that makes
5180 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5181 token. That way, should we re-parse the token stream, we will
5182 not have to repeat the effort required to do the parse, nor will
5183 we issue duplicate error messages. */
5184 if (success && start)
5186 cp_token *token;
5188 token = cp_lexer_token_at (parser->lexer, start);
5189 /* Reset the contents of the START token. */
5190 token->type = CPP_NESTED_NAME_SPECIFIER;
5191 /* Retrieve any deferred checks. Do not pop this access checks yet
5192 so the memory will not be reclaimed during token replacing below. */
5193 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5194 token->u.tree_check_value->value = parser->scope;
5195 token->u.tree_check_value->checks = get_deferred_access_checks ();
5196 token->u.tree_check_value->qualifying_scope =
5197 parser->qualifying_scope;
5198 token->keyword = RID_MAX;
5200 /* Purge all subsequent tokens. */
5201 cp_lexer_purge_tokens_after (parser->lexer, start);
5204 if (start)
5205 pop_to_parent_deferring_access_checks ();
5207 return success ? parser->scope : NULL_TREE;
5210 /* Parse a nested-name-specifier. See
5211 cp_parser_nested_name_specifier_opt for details. This function
5212 behaves identically, except that it will an issue an error if no
5213 nested-name-specifier is present. */
5215 static tree
5216 cp_parser_nested_name_specifier (cp_parser *parser,
5217 bool typename_keyword_p,
5218 bool check_dependency_p,
5219 bool type_p,
5220 bool is_declaration)
5222 tree scope;
5224 /* Look for the nested-name-specifier. */
5225 scope = cp_parser_nested_name_specifier_opt (parser,
5226 typename_keyword_p,
5227 check_dependency_p,
5228 type_p,
5229 is_declaration);
5230 /* If it was not present, issue an error message. */
5231 if (!scope)
5233 cp_parser_error (parser, "expected nested-name-specifier");
5234 parser->scope = NULL_TREE;
5237 return scope;
5240 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5241 this is either a class-name or a namespace-name (which corresponds
5242 to the class-or-namespace-name production in the grammar). For
5243 C++0x, it can also be a type-name that refers to an enumeration
5244 type or a simple-template-id.
5246 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5247 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5248 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5249 TYPE_P is TRUE iff the next name should be taken as a class-name,
5250 even the same name is declared to be another entity in the same
5251 scope.
5253 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5254 specified by the class-or-namespace-name. If neither is found the
5255 ERROR_MARK_NODE is returned. */
5257 static tree
5258 cp_parser_qualifying_entity (cp_parser *parser,
5259 bool typename_keyword_p,
5260 bool template_keyword_p,
5261 bool check_dependency_p,
5262 bool type_p,
5263 bool is_declaration)
5265 tree saved_scope;
5266 tree saved_qualifying_scope;
5267 tree saved_object_scope;
5268 tree scope;
5269 bool only_class_p;
5270 bool successful_parse_p;
5272 /* DR 743: decltype can appear in a nested-name-specifier. */
5273 if (cp_lexer_next_token_is_decltype (parser->lexer))
5275 scope = cp_parser_decltype (parser);
5276 if (TREE_CODE (scope) != ENUMERAL_TYPE
5277 && !MAYBE_CLASS_TYPE_P (scope))
5279 cp_parser_simulate_error (parser);
5280 return error_mark_node;
5282 if (TYPE_NAME (scope))
5283 scope = TYPE_NAME (scope);
5284 return scope;
5287 /* Before we try to parse the class-name, we must save away the
5288 current PARSER->SCOPE since cp_parser_class_name will destroy
5289 it. */
5290 saved_scope = parser->scope;
5291 saved_qualifying_scope = parser->qualifying_scope;
5292 saved_object_scope = parser->object_scope;
5293 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5294 there is no need to look for a namespace-name. */
5295 only_class_p = template_keyword_p
5296 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5297 if (!only_class_p)
5298 cp_parser_parse_tentatively (parser);
5299 scope = cp_parser_class_name (parser,
5300 typename_keyword_p,
5301 template_keyword_p,
5302 type_p ? class_type : none_type,
5303 check_dependency_p,
5304 /*class_head_p=*/false,
5305 is_declaration);
5306 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5307 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5308 if (!only_class_p
5309 && cxx_dialect != cxx98
5310 && !successful_parse_p)
5312 /* Restore the saved scope. */
5313 parser->scope = saved_scope;
5314 parser->qualifying_scope = saved_qualifying_scope;
5315 parser->object_scope = saved_object_scope;
5317 /* Parse tentatively. */
5318 cp_parser_parse_tentatively (parser);
5320 /* Parse a type-name */
5321 scope = cp_parser_type_name (parser);
5323 /* "If the name found does not designate a namespace or a class,
5324 enumeration, or dependent type, the program is ill-formed."
5326 We cover classes and dependent types above and namespaces below,
5327 so this code is only looking for enums. */
5328 if (!scope || TREE_CODE (scope) != TYPE_DECL
5329 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5330 cp_parser_simulate_error (parser);
5332 successful_parse_p = cp_parser_parse_definitely (parser);
5334 /* If that didn't work, try for a namespace-name. */
5335 if (!only_class_p && !successful_parse_p)
5337 /* Restore the saved scope. */
5338 parser->scope = saved_scope;
5339 parser->qualifying_scope = saved_qualifying_scope;
5340 parser->object_scope = saved_object_scope;
5341 /* If we are not looking at an identifier followed by the scope
5342 resolution operator, then this is not part of a
5343 nested-name-specifier. (Note that this function is only used
5344 to parse the components of a nested-name-specifier.) */
5345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5346 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5347 return error_mark_node;
5348 scope = cp_parser_namespace_name (parser);
5351 return scope;
5354 /* Parse a postfix-expression.
5356 postfix-expression:
5357 primary-expression
5358 postfix-expression [ expression ]
5359 postfix-expression ( expression-list [opt] )
5360 simple-type-specifier ( expression-list [opt] )
5361 typename :: [opt] nested-name-specifier identifier
5362 ( expression-list [opt] )
5363 typename :: [opt] nested-name-specifier template [opt] template-id
5364 ( expression-list [opt] )
5365 postfix-expression . template [opt] id-expression
5366 postfix-expression -> template [opt] id-expression
5367 postfix-expression . pseudo-destructor-name
5368 postfix-expression -> pseudo-destructor-name
5369 postfix-expression ++
5370 postfix-expression --
5371 dynamic_cast < type-id > ( expression )
5372 static_cast < type-id > ( expression )
5373 reinterpret_cast < type-id > ( expression )
5374 const_cast < type-id > ( expression )
5375 typeid ( expression )
5376 typeid ( type-id )
5378 GNU Extension:
5380 postfix-expression:
5381 ( type-id ) { initializer-list , [opt] }
5383 This extension is a GNU version of the C99 compound-literal
5384 construct. (The C99 grammar uses `type-name' instead of `type-id',
5385 but they are essentially the same concept.)
5387 If ADDRESS_P is true, the postfix expression is the operand of the
5388 `&' operator. CAST_P is true if this expression is the target of a
5389 cast.
5391 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5392 class member access expressions [expr.ref].
5394 Returns a representation of the expression. */
5396 static tree
5397 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5398 bool member_access_only_p, bool decltype_p,
5399 cp_id_kind * pidk_return)
5401 cp_token *token;
5402 enum rid keyword;
5403 cp_id_kind idk = CP_ID_KIND_NONE;
5404 tree postfix_expression = NULL_TREE;
5405 bool is_member_access = false;
5407 /* Peek at the next token. */
5408 token = cp_lexer_peek_token (parser->lexer);
5409 /* Some of the productions are determined by keywords. */
5410 keyword = token->keyword;
5411 switch (keyword)
5413 case RID_DYNCAST:
5414 case RID_STATCAST:
5415 case RID_REINTCAST:
5416 case RID_CONSTCAST:
5418 tree type;
5419 tree expression;
5420 const char *saved_message;
5422 /* All of these can be handled in the same way from the point
5423 of view of parsing. Begin by consuming the token
5424 identifying the cast. */
5425 cp_lexer_consume_token (parser->lexer);
5427 /* New types cannot be defined in the cast. */
5428 saved_message = parser->type_definition_forbidden_message;
5429 parser->type_definition_forbidden_message
5430 = G_("types may not be defined in casts");
5432 /* Look for the opening `<'. */
5433 cp_parser_require (parser, CPP_LESS, RT_LESS);
5434 /* Parse the type to which we are casting. */
5435 type = cp_parser_type_id (parser);
5436 /* Look for the closing `>'. */
5437 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5438 /* Restore the old message. */
5439 parser->type_definition_forbidden_message = saved_message;
5441 bool saved_greater_than_is_operator_p
5442 = parser->greater_than_is_operator_p;
5443 parser->greater_than_is_operator_p = true;
5445 /* And the expression which is being cast. */
5446 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5447 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5448 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5450 parser->greater_than_is_operator_p
5451 = saved_greater_than_is_operator_p;
5453 /* Only type conversions to integral or enumeration types
5454 can be used in constant-expressions. */
5455 if (!cast_valid_in_integral_constant_expression_p (type)
5456 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5457 return error_mark_node;
5459 switch (keyword)
5461 case RID_DYNCAST:
5462 postfix_expression
5463 = build_dynamic_cast (type, expression, tf_warning_or_error);
5464 break;
5465 case RID_STATCAST:
5466 postfix_expression
5467 = build_static_cast (type, expression, tf_warning_or_error);
5468 break;
5469 case RID_REINTCAST:
5470 postfix_expression
5471 = build_reinterpret_cast (type, expression,
5472 tf_warning_or_error);
5473 break;
5474 case RID_CONSTCAST:
5475 postfix_expression
5476 = build_const_cast (type, expression, tf_warning_or_error);
5477 break;
5478 default:
5479 gcc_unreachable ();
5482 break;
5484 case RID_TYPEID:
5486 tree type;
5487 const char *saved_message;
5488 bool saved_in_type_id_in_expr_p;
5490 /* Consume the `typeid' token. */
5491 cp_lexer_consume_token (parser->lexer);
5492 /* Look for the `(' token. */
5493 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5494 /* Types cannot be defined in a `typeid' expression. */
5495 saved_message = parser->type_definition_forbidden_message;
5496 parser->type_definition_forbidden_message
5497 = G_("types may not be defined in a %<typeid%> expression");
5498 /* We can't be sure yet whether we're looking at a type-id or an
5499 expression. */
5500 cp_parser_parse_tentatively (parser);
5501 /* Try a type-id first. */
5502 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5503 parser->in_type_id_in_expr_p = true;
5504 type = cp_parser_type_id (parser);
5505 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5506 /* Look for the `)' token. Otherwise, we can't be sure that
5507 we're not looking at an expression: consider `typeid (int
5508 (3))', for example. */
5509 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5510 /* If all went well, simply lookup the type-id. */
5511 if (cp_parser_parse_definitely (parser))
5512 postfix_expression = get_typeid (type, tf_warning_or_error);
5513 /* Otherwise, fall back to the expression variant. */
5514 else
5516 tree expression;
5518 /* Look for an expression. */
5519 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5520 /* Compute its typeid. */
5521 postfix_expression = build_typeid (expression, tf_warning_or_error);
5522 /* Look for the `)' token. */
5523 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5525 /* Restore the saved message. */
5526 parser->type_definition_forbidden_message = saved_message;
5527 /* `typeid' may not appear in an integral constant expression. */
5528 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5529 return error_mark_node;
5531 break;
5533 case RID_TYPENAME:
5535 tree type;
5536 /* The syntax permitted here is the same permitted for an
5537 elaborated-type-specifier. */
5538 type = cp_parser_elaborated_type_specifier (parser,
5539 /*is_friend=*/false,
5540 /*is_declaration=*/false);
5541 postfix_expression = cp_parser_functional_cast (parser, type);
5543 break;
5545 case RID_BUILTIN_SHUFFLE:
5547 vec<tree, va_gc> *vec;
5548 unsigned int i;
5549 tree p;
5550 location_t loc = token->location;
5552 cp_lexer_consume_token (parser->lexer);
5553 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5554 /*cast_p=*/false, /*allow_expansion_p=*/true,
5555 /*non_constant_p=*/NULL);
5556 if (vec == NULL)
5557 return error_mark_node;
5559 FOR_EACH_VEC_ELT (*vec, i, p)
5560 mark_exp_read (p);
5562 if (vec->length () == 2)
5563 return c_build_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1]);
5564 else if (vec->length () == 3)
5565 return c_build_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2]);
5566 else
5568 error_at (loc, "wrong number of arguments to "
5569 "%<__builtin_shuffle%>");
5570 return error_mark_node;
5572 break;
5575 default:
5577 tree type;
5579 /* If the next thing is a simple-type-specifier, we may be
5580 looking at a functional cast. We could also be looking at
5581 an id-expression. So, we try the functional cast, and if
5582 that doesn't work we fall back to the primary-expression. */
5583 cp_parser_parse_tentatively (parser);
5584 /* Look for the simple-type-specifier. */
5585 type = cp_parser_simple_type_specifier (parser,
5586 /*decl_specs=*/NULL,
5587 CP_PARSER_FLAGS_NONE);
5588 /* Parse the cast itself. */
5589 if (!cp_parser_error_occurred (parser))
5590 postfix_expression
5591 = cp_parser_functional_cast (parser, type);
5592 /* If that worked, we're done. */
5593 if (cp_parser_parse_definitely (parser))
5594 break;
5596 /* If the functional-cast didn't work out, try a
5597 compound-literal. */
5598 if (cp_parser_allow_gnu_extensions_p (parser)
5599 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5601 vec<constructor_elt, va_gc> *initializer_list = NULL;
5602 bool saved_in_type_id_in_expr_p;
5604 cp_parser_parse_tentatively (parser);
5605 /* Consume the `('. */
5606 cp_lexer_consume_token (parser->lexer);
5607 /* Parse the type. */
5608 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5609 parser->in_type_id_in_expr_p = true;
5610 type = cp_parser_type_id (parser);
5611 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5612 /* Look for the `)'. */
5613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5614 /* Look for the `{'. */
5615 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5616 /* If things aren't going well, there's no need to
5617 keep going. */
5618 if (!cp_parser_error_occurred (parser))
5620 bool non_constant_p;
5621 /* Parse the initializer-list. */
5622 initializer_list
5623 = cp_parser_initializer_list (parser, &non_constant_p);
5624 /* Allow a trailing `,'. */
5625 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5626 cp_lexer_consume_token (parser->lexer);
5627 /* Look for the final `}'. */
5628 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5630 /* If that worked, we're definitely looking at a
5631 compound-literal expression. */
5632 if (cp_parser_parse_definitely (parser))
5634 /* Warn the user that a compound literal is not
5635 allowed in standard C++. */
5636 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5637 /* For simplicity, we disallow compound literals in
5638 constant-expressions. We could
5639 allow compound literals of integer type, whose
5640 initializer was a constant, in constant
5641 expressions. Permitting that usage, as a further
5642 extension, would not change the meaning of any
5643 currently accepted programs. (Of course, as
5644 compound literals are not part of ISO C++, the
5645 standard has nothing to say.) */
5646 if (cp_parser_non_integral_constant_expression (parser,
5647 NIC_NCC))
5649 postfix_expression = error_mark_node;
5650 break;
5652 /* Form the representation of the compound-literal. */
5653 postfix_expression
5654 = (finish_compound_literal
5655 (type, build_constructor (init_list_type_node,
5656 initializer_list),
5657 tf_warning_or_error));
5658 break;
5662 /* It must be a primary-expression. */
5663 postfix_expression
5664 = cp_parser_primary_expression (parser, address_p, cast_p,
5665 /*template_arg_p=*/false,
5666 decltype_p,
5667 &idk);
5669 break;
5672 /* Note that we don't need to worry about calling build_cplus_new on a
5673 class-valued CALL_EXPR in decltype when it isn't the end of the
5674 postfix-expression; unary_complex_lvalue will take care of that for
5675 all these cases. */
5677 /* Keep looping until the postfix-expression is complete. */
5678 while (true)
5680 if (idk == CP_ID_KIND_UNQUALIFIED
5681 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5682 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5683 /* It is not a Koenig lookup function call. */
5684 postfix_expression
5685 = unqualified_name_lookup_error (postfix_expression);
5687 /* Peek at the next token. */
5688 token = cp_lexer_peek_token (parser->lexer);
5690 switch (token->type)
5692 case CPP_OPEN_SQUARE:
5693 if (cp_next_tokens_can_be_std_attribute_p (parser))
5695 cp_parser_error (parser,
5696 "two consecutive %<[%> shall "
5697 "only introduce an attribute");
5698 return error_mark_node;
5700 postfix_expression
5701 = cp_parser_postfix_open_square_expression (parser,
5702 postfix_expression,
5703 false,
5704 decltype_p);
5705 idk = CP_ID_KIND_NONE;
5706 is_member_access = false;
5707 break;
5709 case CPP_OPEN_PAREN:
5710 /* postfix-expression ( expression-list [opt] ) */
5712 bool koenig_p;
5713 bool is_builtin_constant_p;
5714 bool saved_integral_constant_expression_p = false;
5715 bool saved_non_integral_constant_expression_p = false;
5716 tsubst_flags_t complain = complain_flags (decltype_p);
5717 vec<tree, va_gc> *args;
5719 is_member_access = false;
5721 is_builtin_constant_p
5722 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5723 if (is_builtin_constant_p)
5725 /* The whole point of __builtin_constant_p is to allow
5726 non-constant expressions to appear as arguments. */
5727 saved_integral_constant_expression_p
5728 = parser->integral_constant_expression_p;
5729 saved_non_integral_constant_expression_p
5730 = parser->non_integral_constant_expression_p;
5731 parser->integral_constant_expression_p = false;
5733 args = (cp_parser_parenthesized_expression_list
5734 (parser, non_attr,
5735 /*cast_p=*/false, /*allow_expansion_p=*/true,
5736 /*non_constant_p=*/NULL));
5737 if (is_builtin_constant_p)
5739 parser->integral_constant_expression_p
5740 = saved_integral_constant_expression_p;
5741 parser->non_integral_constant_expression_p
5742 = saved_non_integral_constant_expression_p;
5745 if (args == NULL)
5747 postfix_expression = error_mark_node;
5748 break;
5751 /* Function calls are not permitted in
5752 constant-expressions. */
5753 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5754 && cp_parser_non_integral_constant_expression (parser,
5755 NIC_FUNC_CALL))
5757 postfix_expression = error_mark_node;
5758 release_tree_vector (args);
5759 break;
5762 koenig_p = false;
5763 if (idk == CP_ID_KIND_UNQUALIFIED
5764 || idk == CP_ID_KIND_TEMPLATE_ID)
5766 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5768 if (!args->is_empty ())
5770 koenig_p = true;
5771 if (!any_type_dependent_arguments_p (args))
5772 postfix_expression
5773 = perform_koenig_lookup (postfix_expression, args,
5774 /*include_std=*/false,
5775 complain);
5777 else
5778 postfix_expression
5779 = unqualified_fn_lookup_error (postfix_expression);
5781 /* We do not perform argument-dependent lookup if
5782 normal lookup finds a non-function, in accordance
5783 with the expected resolution of DR 218. */
5784 else if (!args->is_empty ()
5785 && is_overloaded_fn (postfix_expression))
5787 tree fn = get_first_fn (postfix_expression);
5788 fn = STRIP_TEMPLATE (fn);
5790 /* Do not do argument dependent lookup if regular
5791 lookup finds a member function or a block-scope
5792 function declaration. [basic.lookup.argdep]/3 */
5793 if (!DECL_FUNCTION_MEMBER_P (fn)
5794 && !DECL_LOCAL_FUNCTION_P (fn))
5796 koenig_p = true;
5797 if (!any_type_dependent_arguments_p (args))
5798 postfix_expression
5799 = perform_koenig_lookup (postfix_expression, args,
5800 /*include_std=*/false,
5801 complain);
5806 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5808 tree instance = TREE_OPERAND (postfix_expression, 0);
5809 tree fn = TREE_OPERAND (postfix_expression, 1);
5811 if (processing_template_decl
5812 && (type_dependent_expression_p (instance)
5813 || (!BASELINK_P (fn)
5814 && TREE_CODE (fn) != FIELD_DECL)
5815 || type_dependent_expression_p (fn)
5816 || any_type_dependent_arguments_p (args)))
5818 postfix_expression
5819 = build_nt_call_vec (postfix_expression, args);
5820 release_tree_vector (args);
5821 break;
5824 if (BASELINK_P (fn))
5826 postfix_expression
5827 = (build_new_method_call
5828 (instance, fn, &args, NULL_TREE,
5829 (idk == CP_ID_KIND_QUALIFIED
5830 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5831 : LOOKUP_NORMAL),
5832 /*fn_p=*/NULL,
5833 complain));
5835 else
5836 postfix_expression
5837 = finish_call_expr (postfix_expression, &args,
5838 /*disallow_virtual=*/false,
5839 /*koenig_p=*/false,
5840 complain);
5842 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5843 || TREE_CODE (postfix_expression) == MEMBER_REF
5844 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5845 postfix_expression = (build_offset_ref_call_from_tree
5846 (postfix_expression, &args,
5847 complain));
5848 else if (idk == CP_ID_KIND_QUALIFIED)
5849 /* A call to a static class member, or a namespace-scope
5850 function. */
5851 postfix_expression
5852 = finish_call_expr (postfix_expression, &args,
5853 /*disallow_virtual=*/true,
5854 koenig_p,
5855 complain);
5856 else
5857 /* All other function calls. */
5858 postfix_expression
5859 = finish_call_expr (postfix_expression, &args,
5860 /*disallow_virtual=*/false,
5861 koenig_p,
5862 complain);
5864 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5865 idk = CP_ID_KIND_NONE;
5867 release_tree_vector (args);
5869 break;
5871 case CPP_DOT:
5872 case CPP_DEREF:
5873 /* postfix-expression . template [opt] id-expression
5874 postfix-expression . pseudo-destructor-name
5875 postfix-expression -> template [opt] id-expression
5876 postfix-expression -> pseudo-destructor-name */
5878 /* Consume the `.' or `->' operator. */
5879 cp_lexer_consume_token (parser->lexer);
5881 postfix_expression
5882 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5883 postfix_expression,
5884 false, &idk,
5885 token->location);
5887 is_member_access = true;
5888 break;
5890 case CPP_PLUS_PLUS:
5891 /* postfix-expression ++ */
5892 /* Consume the `++' token. */
5893 cp_lexer_consume_token (parser->lexer);
5894 /* Generate a representation for the complete expression. */
5895 postfix_expression
5896 = finish_increment_expr (postfix_expression,
5897 POSTINCREMENT_EXPR);
5898 /* Increments may not appear in constant-expressions. */
5899 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5900 postfix_expression = error_mark_node;
5901 idk = CP_ID_KIND_NONE;
5902 is_member_access = false;
5903 break;
5905 case CPP_MINUS_MINUS:
5906 /* postfix-expression -- */
5907 /* Consume the `--' token. */
5908 cp_lexer_consume_token (parser->lexer);
5909 /* Generate a representation for the complete expression. */
5910 postfix_expression
5911 = finish_increment_expr (postfix_expression,
5912 POSTDECREMENT_EXPR);
5913 /* Decrements may not appear in constant-expressions. */
5914 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5915 postfix_expression = error_mark_node;
5916 idk = CP_ID_KIND_NONE;
5917 is_member_access = false;
5918 break;
5920 default:
5921 if (pidk_return != NULL)
5922 * pidk_return = idk;
5923 if (member_access_only_p)
5924 return is_member_access? postfix_expression : error_mark_node;
5925 else
5926 return postfix_expression;
5930 /* We should never get here. */
5931 gcc_unreachable ();
5932 return error_mark_node;
5935 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5936 by cp_parser_builtin_offsetof. We're looking for
5938 postfix-expression [ expression ]
5939 postfix-expression [ braced-init-list ] (C++11)
5941 FOR_OFFSETOF is set if we're being called in that context, which
5942 changes how we deal with integer constant expressions. */
5944 static tree
5945 cp_parser_postfix_open_square_expression (cp_parser *parser,
5946 tree postfix_expression,
5947 bool for_offsetof,
5948 bool decltype_p)
5950 tree index;
5951 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5953 /* Consume the `[' token. */
5954 cp_lexer_consume_token (parser->lexer);
5956 /* Parse the index expression. */
5957 /* ??? For offsetof, there is a question of what to allow here. If
5958 offsetof is not being used in an integral constant expression context,
5959 then we *could* get the right answer by computing the value at runtime.
5960 If we are in an integral constant expression context, then we might
5961 could accept any constant expression; hard to say without analysis.
5962 Rather than open the barn door too wide right away, allow only integer
5963 constant expressions here. */
5964 if (for_offsetof)
5965 index = cp_parser_constant_expression (parser, false, NULL);
5966 else
5968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5970 bool expr_nonconst_p;
5971 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5972 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5974 else
5975 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5978 /* Look for the closing `]'. */
5979 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5981 /* Build the ARRAY_REF. */
5982 postfix_expression = grok_array_decl (loc, postfix_expression,
5983 index, decltype_p);
5985 /* When not doing offsetof, array references are not permitted in
5986 constant-expressions. */
5987 if (!for_offsetof
5988 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5989 postfix_expression = error_mark_node;
5991 return postfix_expression;
5994 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5995 by cp_parser_builtin_offsetof. We're looking for
5997 postfix-expression . template [opt] id-expression
5998 postfix-expression . pseudo-destructor-name
5999 postfix-expression -> template [opt] id-expression
6000 postfix-expression -> pseudo-destructor-name
6002 FOR_OFFSETOF is set if we're being called in that context. That sorta
6003 limits what of the above we'll actually accept, but nevermind.
6004 TOKEN_TYPE is the "." or "->" token, which will already have been
6005 removed from the stream. */
6007 static tree
6008 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6009 enum cpp_ttype token_type,
6010 tree postfix_expression,
6011 bool for_offsetof, cp_id_kind *idk,
6012 location_t location)
6014 tree name;
6015 bool dependent_p;
6016 bool pseudo_destructor_p;
6017 tree scope = NULL_TREE;
6019 /* If this is a `->' operator, dereference the pointer. */
6020 if (token_type == CPP_DEREF)
6021 postfix_expression = build_x_arrow (location, postfix_expression,
6022 tf_warning_or_error);
6023 /* Check to see whether or not the expression is type-dependent. */
6024 dependent_p = type_dependent_expression_p (postfix_expression);
6025 /* The identifier following the `->' or `.' is not qualified. */
6026 parser->scope = NULL_TREE;
6027 parser->qualifying_scope = NULL_TREE;
6028 parser->object_scope = NULL_TREE;
6029 *idk = CP_ID_KIND_NONE;
6031 /* Enter the scope corresponding to the type of the object
6032 given by the POSTFIX_EXPRESSION. */
6033 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6035 scope = TREE_TYPE (postfix_expression);
6036 /* According to the standard, no expression should ever have
6037 reference type. Unfortunately, we do not currently match
6038 the standard in this respect in that our internal representation
6039 of an expression may have reference type even when the standard
6040 says it does not. Therefore, we have to manually obtain the
6041 underlying type here. */
6042 scope = non_reference (scope);
6043 /* The type of the POSTFIX_EXPRESSION must be complete. */
6044 if (scope == unknown_type_node)
6046 error_at (location, "%qE does not have class type",
6047 postfix_expression);
6048 scope = NULL_TREE;
6050 /* Unlike the object expression in other contexts, *this is not
6051 required to be of complete type for purposes of class member
6052 access (5.2.5) outside the member function body. */
6053 else if (scope != current_class_ref
6054 && !(processing_template_decl && scope == current_class_type))
6055 scope = complete_type_or_else (scope, NULL_TREE);
6056 /* Let the name lookup machinery know that we are processing a
6057 class member access expression. */
6058 parser->context->object_type = scope;
6059 /* If something went wrong, we want to be able to discern that case,
6060 as opposed to the case where there was no SCOPE due to the type
6061 of expression being dependent. */
6062 if (!scope)
6063 scope = error_mark_node;
6064 /* If the SCOPE was erroneous, make the various semantic analysis
6065 functions exit quickly -- and without issuing additional error
6066 messages. */
6067 if (scope == error_mark_node)
6068 postfix_expression = error_mark_node;
6071 /* Assume this expression is not a pseudo-destructor access. */
6072 pseudo_destructor_p = false;
6074 /* If the SCOPE is a scalar type, then, if this is a valid program,
6075 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6076 is type dependent, it can be pseudo-destructor-name or something else.
6077 Try to parse it as pseudo-destructor-name first. */
6078 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6080 tree s;
6081 tree type;
6083 cp_parser_parse_tentatively (parser);
6084 /* Parse the pseudo-destructor-name. */
6085 s = NULL_TREE;
6086 cp_parser_pseudo_destructor_name (parser, &s, &type);
6087 if (dependent_p
6088 && (cp_parser_error_occurred (parser)
6089 || TREE_CODE (type) != TYPE_DECL
6090 || !SCALAR_TYPE_P (TREE_TYPE (type))))
6091 cp_parser_abort_tentative_parse (parser);
6092 else if (cp_parser_parse_definitely (parser))
6094 pseudo_destructor_p = true;
6095 postfix_expression
6096 = finish_pseudo_destructor_expr (postfix_expression,
6097 s, TREE_TYPE (type));
6101 if (!pseudo_destructor_p)
6103 /* If the SCOPE is not a scalar type, we are looking at an
6104 ordinary class member access expression, rather than a
6105 pseudo-destructor-name. */
6106 bool template_p;
6107 cp_token *token = cp_lexer_peek_token (parser->lexer);
6108 /* Parse the id-expression. */
6109 name = (cp_parser_id_expression
6110 (parser,
6111 cp_parser_optional_template_keyword (parser),
6112 /*check_dependency_p=*/true,
6113 &template_p,
6114 /*declarator_p=*/false,
6115 /*optional_p=*/false));
6116 /* In general, build a SCOPE_REF if the member name is qualified.
6117 However, if the name was not dependent and has already been
6118 resolved; there is no need to build the SCOPE_REF. For example;
6120 struct X { void f(); };
6121 template <typename T> void f(T* t) { t->X::f(); }
6123 Even though "t" is dependent, "X::f" is not and has been resolved
6124 to a BASELINK; there is no need to include scope information. */
6126 /* But we do need to remember that there was an explicit scope for
6127 virtual function calls. */
6128 if (parser->scope)
6129 *idk = CP_ID_KIND_QUALIFIED;
6131 /* If the name is a template-id that names a type, we will get a
6132 TYPE_DECL here. That is invalid code. */
6133 if (TREE_CODE (name) == TYPE_DECL)
6135 error_at (token->location, "invalid use of %qD", name);
6136 postfix_expression = error_mark_node;
6138 else
6140 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6142 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6144 error_at (token->location, "%<%D::%D%> is not a class member",
6145 parser->scope, name);
6146 postfix_expression = error_mark_node;
6148 else
6149 name = build_qualified_name (/*type=*/NULL_TREE,
6150 parser->scope,
6151 name,
6152 template_p);
6153 parser->scope = NULL_TREE;
6154 parser->qualifying_scope = NULL_TREE;
6155 parser->object_scope = NULL_TREE;
6157 if (parser->scope && name && BASELINK_P (name))
6158 adjust_result_of_qualified_name_lookup
6159 (name, parser->scope, scope);
6160 postfix_expression
6161 = finish_class_member_access_expr (postfix_expression, name,
6162 template_p,
6163 tf_warning_or_error);
6167 /* We no longer need to look up names in the scope of the object on
6168 the left-hand side of the `.' or `->' operator. */
6169 parser->context->object_type = NULL_TREE;
6171 /* Outside of offsetof, these operators may not appear in
6172 constant-expressions. */
6173 if (!for_offsetof
6174 && (cp_parser_non_integral_constant_expression
6175 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6176 postfix_expression = error_mark_node;
6178 return postfix_expression;
6181 /* Parse a parenthesized expression-list.
6183 expression-list:
6184 assignment-expression
6185 expression-list, assignment-expression
6187 attribute-list:
6188 expression-list
6189 identifier
6190 identifier, expression-list
6192 CAST_P is true if this expression is the target of a cast.
6194 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6195 argument pack.
6197 Returns a vector of trees. Each element is a representation of an
6198 assignment-expression. NULL is returned if the ( and or ) are
6199 missing. An empty, but allocated, vector is returned on no
6200 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6201 if we are parsing an attribute list for an attribute that wants a
6202 plain identifier argument, normal_attr for an attribute that wants
6203 an expression, or non_attr if we aren't parsing an attribute list. If
6204 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6205 not all of the expressions in the list were constant. */
6207 static vec<tree, va_gc> *
6208 cp_parser_parenthesized_expression_list (cp_parser* parser,
6209 int is_attribute_list,
6210 bool cast_p,
6211 bool allow_expansion_p,
6212 bool *non_constant_p)
6214 vec<tree, va_gc> *expression_list;
6215 bool fold_expr_p = is_attribute_list != non_attr;
6216 tree identifier = NULL_TREE;
6217 bool saved_greater_than_is_operator_p;
6219 /* Assume all the expressions will be constant. */
6220 if (non_constant_p)
6221 *non_constant_p = false;
6223 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6224 return NULL;
6226 expression_list = make_tree_vector ();
6228 /* Within a parenthesized expression, a `>' token is always
6229 the greater-than operator. */
6230 saved_greater_than_is_operator_p
6231 = parser->greater_than_is_operator_p;
6232 parser->greater_than_is_operator_p = true;
6234 /* Consume expressions until there are no more. */
6235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6236 while (true)
6238 tree expr;
6240 /* At the beginning of attribute lists, check to see if the
6241 next token is an identifier. */
6242 if (is_attribute_list == id_attr
6243 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6245 cp_token *token;
6247 /* Consume the identifier. */
6248 token = cp_lexer_consume_token (parser->lexer);
6249 /* Save the identifier. */
6250 identifier = token->u.value;
6252 else
6254 bool expr_non_constant_p;
6256 /* Parse the next assignment-expression. */
6257 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6259 /* A braced-init-list. */
6260 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6261 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6262 if (non_constant_p && expr_non_constant_p)
6263 *non_constant_p = true;
6265 else if (non_constant_p)
6267 expr = (cp_parser_constant_expression
6268 (parser, /*allow_non_constant_p=*/true,
6269 &expr_non_constant_p));
6270 if (expr_non_constant_p)
6271 *non_constant_p = true;
6273 else
6274 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6276 if (fold_expr_p)
6277 expr = fold_non_dependent_expr (expr);
6279 /* If we have an ellipsis, then this is an expression
6280 expansion. */
6281 if (allow_expansion_p
6282 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6284 /* Consume the `...'. */
6285 cp_lexer_consume_token (parser->lexer);
6287 /* Build the argument pack. */
6288 expr = make_pack_expansion (expr);
6291 /* Add it to the list. We add error_mark_node
6292 expressions to the list, so that we can still tell if
6293 the correct form for a parenthesized expression-list
6294 is found. That gives better errors. */
6295 vec_safe_push (expression_list, expr);
6297 if (expr == error_mark_node)
6298 goto skip_comma;
6301 /* After the first item, attribute lists look the same as
6302 expression lists. */
6303 is_attribute_list = non_attr;
6305 get_comma:;
6306 /* If the next token isn't a `,', then we are done. */
6307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6308 break;
6310 /* Otherwise, consume the `,' and keep going. */
6311 cp_lexer_consume_token (parser->lexer);
6314 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6316 int ending;
6318 skip_comma:;
6319 /* We try and resync to an unnested comma, as that will give the
6320 user better diagnostics. */
6321 ending = cp_parser_skip_to_closing_parenthesis (parser,
6322 /*recovering=*/true,
6323 /*or_comma=*/true,
6324 /*consume_paren=*/true);
6325 if (ending < 0)
6326 goto get_comma;
6327 if (!ending)
6329 parser->greater_than_is_operator_p
6330 = saved_greater_than_is_operator_p;
6331 return NULL;
6335 parser->greater_than_is_operator_p
6336 = saved_greater_than_is_operator_p;
6338 if (identifier)
6339 vec_safe_insert (expression_list, 0, identifier);
6341 return expression_list;
6344 /* Parse a pseudo-destructor-name.
6346 pseudo-destructor-name:
6347 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6348 :: [opt] nested-name-specifier template template-id :: ~ type-name
6349 :: [opt] nested-name-specifier [opt] ~ type-name
6351 If either of the first two productions is used, sets *SCOPE to the
6352 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6353 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6354 or ERROR_MARK_NODE if the parse fails. */
6356 static void
6357 cp_parser_pseudo_destructor_name (cp_parser* parser,
6358 tree* scope,
6359 tree* type)
6361 bool nested_name_specifier_p;
6363 /* Assume that things will not work out. */
6364 *type = error_mark_node;
6366 /* Look for the optional `::' operator. */
6367 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6368 /* Look for the optional nested-name-specifier. */
6369 nested_name_specifier_p
6370 = (cp_parser_nested_name_specifier_opt (parser,
6371 /*typename_keyword_p=*/false,
6372 /*check_dependency_p=*/true,
6373 /*type_p=*/false,
6374 /*is_declaration=*/false)
6375 != NULL_TREE);
6376 /* Now, if we saw a nested-name-specifier, we might be doing the
6377 second production. */
6378 if (nested_name_specifier_p
6379 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6381 /* Consume the `template' keyword. */
6382 cp_lexer_consume_token (parser->lexer);
6383 /* Parse the template-id. */
6384 cp_parser_template_id (parser,
6385 /*template_keyword_p=*/true,
6386 /*check_dependency_p=*/false,
6387 class_type,
6388 /*is_declaration=*/true);
6389 /* Look for the `::' token. */
6390 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6392 /* If the next token is not a `~', then there might be some
6393 additional qualification. */
6394 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6396 /* At this point, we're looking for "type-name :: ~". The type-name
6397 must not be a class-name, since this is a pseudo-destructor. So,
6398 it must be either an enum-name, or a typedef-name -- both of which
6399 are just identifiers. So, we peek ahead to check that the "::"
6400 and "~" tokens are present; if they are not, then we can avoid
6401 calling type_name. */
6402 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6403 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6404 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6406 cp_parser_error (parser, "non-scalar type");
6407 return;
6410 /* Look for the type-name. */
6411 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6412 if (*scope == error_mark_node)
6413 return;
6415 /* Look for the `::' token. */
6416 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6418 else
6419 *scope = NULL_TREE;
6421 /* Look for the `~'. */
6422 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6424 /* Look for the type-name again. We are not responsible for
6425 checking that it matches the first type-name. */
6426 *type = cp_parser_nonclass_name (parser);
6429 /* Parse a unary-expression.
6431 unary-expression:
6432 postfix-expression
6433 ++ cast-expression
6434 -- cast-expression
6435 unary-operator cast-expression
6436 sizeof unary-expression
6437 sizeof ( type-id )
6438 alignof ( type-id ) [C++0x]
6439 new-expression
6440 delete-expression
6442 GNU Extensions:
6444 unary-expression:
6445 __extension__ cast-expression
6446 __alignof__ unary-expression
6447 __alignof__ ( type-id )
6448 alignof unary-expression [C++0x]
6449 __real__ cast-expression
6450 __imag__ cast-expression
6451 && identifier
6453 ADDRESS_P is true iff the unary-expression is appearing as the
6454 operand of the `&' operator. CAST_P is true if this expression is
6455 the target of a cast.
6457 Returns a representation of the expression. */
6459 static tree
6460 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6461 bool decltype_p, cp_id_kind * pidk)
6463 cp_token *token;
6464 enum tree_code unary_operator;
6466 /* Peek at the next token. */
6467 token = cp_lexer_peek_token (parser->lexer);
6468 /* Some keywords give away the kind of expression. */
6469 if (token->type == CPP_KEYWORD)
6471 enum rid keyword = token->keyword;
6473 switch (keyword)
6475 case RID_ALIGNOF:
6476 case RID_SIZEOF:
6478 tree operand, ret;
6479 enum tree_code op;
6480 location_t first_loc;
6482 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6483 /* Consume the token. */
6484 cp_lexer_consume_token (parser->lexer);
6485 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6486 /* Parse the operand. */
6487 operand = cp_parser_sizeof_operand (parser, keyword);
6489 if (TYPE_P (operand))
6490 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6491 else
6493 /* ISO C++ defines alignof only with types, not with
6494 expressions. So pedwarn if alignof is used with a non-
6495 type expression. However, __alignof__ is ok. */
6496 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6497 pedwarn (token->location, OPT_Wpedantic,
6498 "ISO C++ does not allow %<alignof%> "
6499 "with a non-type");
6501 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6503 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6504 SIZEOF_EXPR with the original operand. */
6505 if (op == SIZEOF_EXPR && ret != error_mark_node)
6507 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6509 if (!processing_template_decl && TYPE_P (operand))
6511 ret = build_min (SIZEOF_EXPR, size_type_node,
6512 build1 (NOP_EXPR, operand,
6513 error_mark_node));
6514 SIZEOF_EXPR_TYPE_P (ret) = 1;
6516 else
6517 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6518 TREE_SIDE_EFFECTS (ret) = 0;
6519 TREE_READONLY (ret) = 1;
6521 SET_EXPR_LOCATION (ret, first_loc);
6523 return ret;
6526 case RID_NEW:
6527 return cp_parser_new_expression (parser);
6529 case RID_DELETE:
6530 return cp_parser_delete_expression (parser);
6532 case RID_EXTENSION:
6534 /* The saved value of the PEDANTIC flag. */
6535 int saved_pedantic;
6536 tree expr;
6538 /* Save away the PEDANTIC flag. */
6539 cp_parser_extension_opt (parser, &saved_pedantic);
6540 /* Parse the cast-expression. */
6541 expr = cp_parser_simple_cast_expression (parser);
6542 /* Restore the PEDANTIC flag. */
6543 pedantic = saved_pedantic;
6545 return expr;
6548 case RID_REALPART:
6549 case RID_IMAGPART:
6551 tree expression;
6553 /* Consume the `__real__' or `__imag__' token. */
6554 cp_lexer_consume_token (parser->lexer);
6555 /* Parse the cast-expression. */
6556 expression = cp_parser_simple_cast_expression (parser);
6557 /* Create the complete representation. */
6558 return build_x_unary_op (token->location,
6559 (keyword == RID_REALPART
6560 ? REALPART_EXPR : IMAGPART_EXPR),
6561 expression,
6562 tf_warning_or_error);
6564 break;
6566 case RID_TRANSACTION_ATOMIC:
6567 case RID_TRANSACTION_RELAXED:
6568 return cp_parser_transaction_expression (parser, keyword);
6570 case RID_NOEXCEPT:
6572 tree expr;
6573 const char *saved_message;
6574 bool saved_integral_constant_expression_p;
6575 bool saved_non_integral_constant_expression_p;
6576 bool saved_greater_than_is_operator_p;
6578 cp_lexer_consume_token (parser->lexer);
6579 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6581 saved_message = parser->type_definition_forbidden_message;
6582 parser->type_definition_forbidden_message
6583 = G_("types may not be defined in %<noexcept%> expressions");
6585 saved_integral_constant_expression_p
6586 = parser->integral_constant_expression_p;
6587 saved_non_integral_constant_expression_p
6588 = parser->non_integral_constant_expression_p;
6589 parser->integral_constant_expression_p = false;
6591 saved_greater_than_is_operator_p
6592 = parser->greater_than_is_operator_p;
6593 parser->greater_than_is_operator_p = true;
6595 ++cp_unevaluated_operand;
6596 ++c_inhibit_evaluation_warnings;
6597 expr = cp_parser_expression (parser, false, NULL);
6598 --c_inhibit_evaluation_warnings;
6599 --cp_unevaluated_operand;
6601 parser->greater_than_is_operator_p
6602 = saved_greater_than_is_operator_p;
6604 parser->integral_constant_expression_p
6605 = saved_integral_constant_expression_p;
6606 parser->non_integral_constant_expression_p
6607 = saved_non_integral_constant_expression_p;
6609 parser->type_definition_forbidden_message = saved_message;
6611 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6612 return finish_noexcept_expr (expr, tf_warning_or_error);
6615 default:
6616 break;
6620 /* Look for the `:: new' and `:: delete', which also signal the
6621 beginning of a new-expression, or delete-expression,
6622 respectively. If the next token is `::', then it might be one of
6623 these. */
6624 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6626 enum rid keyword;
6628 /* See if the token after the `::' is one of the keywords in
6629 which we're interested. */
6630 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6631 /* If it's `new', we have a new-expression. */
6632 if (keyword == RID_NEW)
6633 return cp_parser_new_expression (parser);
6634 /* Similarly, for `delete'. */
6635 else if (keyword == RID_DELETE)
6636 return cp_parser_delete_expression (parser);
6639 /* Look for a unary operator. */
6640 unary_operator = cp_parser_unary_operator (token);
6641 /* The `++' and `--' operators can be handled similarly, even though
6642 they are not technically unary-operators in the grammar. */
6643 if (unary_operator == ERROR_MARK)
6645 if (token->type == CPP_PLUS_PLUS)
6646 unary_operator = PREINCREMENT_EXPR;
6647 else if (token->type == CPP_MINUS_MINUS)
6648 unary_operator = PREDECREMENT_EXPR;
6649 /* Handle the GNU address-of-label extension. */
6650 else if (cp_parser_allow_gnu_extensions_p (parser)
6651 && token->type == CPP_AND_AND)
6653 tree identifier;
6654 tree expression;
6655 location_t loc = token->location;
6657 /* Consume the '&&' token. */
6658 cp_lexer_consume_token (parser->lexer);
6659 /* Look for the identifier. */
6660 identifier = cp_parser_identifier (parser);
6661 /* Create an expression representing the address. */
6662 expression = finish_label_address_expr (identifier, loc);
6663 if (cp_parser_non_integral_constant_expression (parser,
6664 NIC_ADDR_LABEL))
6665 expression = error_mark_node;
6666 return expression;
6669 if (unary_operator != ERROR_MARK)
6671 tree cast_expression;
6672 tree expression = error_mark_node;
6673 non_integral_constant non_constant_p = NIC_NONE;
6674 location_t loc = token->location;
6675 tsubst_flags_t complain = complain_flags (decltype_p);
6677 /* Consume the operator token. */
6678 token = cp_lexer_consume_token (parser->lexer);
6679 /* Parse the cast-expression. */
6680 cast_expression
6681 = cp_parser_cast_expression (parser,
6682 unary_operator == ADDR_EXPR,
6683 /*cast_p=*/false,
6684 /*decltype*/false,
6685 pidk);
6686 /* Now, build an appropriate representation. */
6687 switch (unary_operator)
6689 case INDIRECT_REF:
6690 non_constant_p = NIC_STAR;
6691 expression = build_x_indirect_ref (loc, cast_expression,
6692 RO_UNARY_STAR,
6693 complain);
6694 break;
6696 case ADDR_EXPR:
6697 non_constant_p = NIC_ADDR;
6698 /* Fall through. */
6699 case BIT_NOT_EXPR:
6700 expression = build_x_unary_op (loc, unary_operator,
6701 cast_expression,
6702 complain);
6703 break;
6705 case PREINCREMENT_EXPR:
6706 case PREDECREMENT_EXPR:
6707 non_constant_p = unary_operator == PREINCREMENT_EXPR
6708 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6709 /* Fall through. */
6710 case UNARY_PLUS_EXPR:
6711 case NEGATE_EXPR:
6712 case TRUTH_NOT_EXPR:
6713 expression = finish_unary_op_expr (loc, unary_operator,
6714 cast_expression, complain);
6715 break;
6717 default:
6718 gcc_unreachable ();
6721 if (non_constant_p != NIC_NONE
6722 && cp_parser_non_integral_constant_expression (parser,
6723 non_constant_p))
6724 expression = error_mark_node;
6726 return expression;
6729 return cp_parser_postfix_expression (parser, address_p, cast_p,
6730 /*member_access_only_p=*/false,
6731 decltype_p,
6732 pidk);
6735 static inline tree
6736 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6737 cp_id_kind * pidk)
6739 return cp_parser_unary_expression (parser, address_p, cast_p,
6740 /*decltype*/false, pidk);
6743 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6744 unary-operator, the corresponding tree code is returned. */
6746 static enum tree_code
6747 cp_parser_unary_operator (cp_token* token)
6749 switch (token->type)
6751 case CPP_MULT:
6752 return INDIRECT_REF;
6754 case CPP_AND:
6755 return ADDR_EXPR;
6757 case CPP_PLUS:
6758 return UNARY_PLUS_EXPR;
6760 case CPP_MINUS:
6761 return NEGATE_EXPR;
6763 case CPP_NOT:
6764 return TRUTH_NOT_EXPR;
6766 case CPP_COMPL:
6767 return BIT_NOT_EXPR;
6769 default:
6770 return ERROR_MARK;
6774 /* Parse a new-expression.
6776 new-expression:
6777 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6778 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6780 Returns a representation of the expression. */
6782 static tree
6783 cp_parser_new_expression (cp_parser* parser)
6785 bool global_scope_p;
6786 vec<tree, va_gc> *placement;
6787 tree type;
6788 vec<tree, va_gc> *initializer;
6789 tree nelts = NULL_TREE;
6790 tree ret;
6792 /* Look for the optional `::' operator. */
6793 global_scope_p
6794 = (cp_parser_global_scope_opt (parser,
6795 /*current_scope_valid_p=*/false)
6796 != NULL_TREE);
6797 /* Look for the `new' operator. */
6798 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6799 /* There's no easy way to tell a new-placement from the
6800 `( type-id )' construct. */
6801 cp_parser_parse_tentatively (parser);
6802 /* Look for a new-placement. */
6803 placement = cp_parser_new_placement (parser);
6804 /* If that didn't work out, there's no new-placement. */
6805 if (!cp_parser_parse_definitely (parser))
6807 if (placement != NULL)
6808 release_tree_vector (placement);
6809 placement = NULL;
6812 /* If the next token is a `(', then we have a parenthesized
6813 type-id. */
6814 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6816 cp_token *token;
6817 const char *saved_message = parser->type_definition_forbidden_message;
6819 /* Consume the `('. */
6820 cp_lexer_consume_token (parser->lexer);
6822 /* Parse the type-id. */
6823 parser->type_definition_forbidden_message
6824 = G_("types may not be defined in a new-expression");
6825 type = cp_parser_type_id (parser);
6826 parser->type_definition_forbidden_message = saved_message;
6828 /* Look for the closing `)'. */
6829 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6830 token = cp_lexer_peek_token (parser->lexer);
6831 /* There should not be a direct-new-declarator in this production,
6832 but GCC used to allowed this, so we check and emit a sensible error
6833 message for this case. */
6834 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6836 error_at (token->location,
6837 "array bound forbidden after parenthesized type-id");
6838 inform (token->location,
6839 "try removing the parentheses around the type-id");
6840 cp_parser_direct_new_declarator (parser);
6843 /* Otherwise, there must be a new-type-id. */
6844 else
6845 type = cp_parser_new_type_id (parser, &nelts);
6847 /* If the next token is a `(' or '{', then we have a new-initializer. */
6848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6849 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6850 initializer = cp_parser_new_initializer (parser);
6851 else
6852 initializer = NULL;
6854 /* A new-expression may not appear in an integral constant
6855 expression. */
6856 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6857 ret = error_mark_node;
6858 else
6860 /* Create a representation of the new-expression. */
6861 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6862 tf_warning_or_error);
6865 if (placement != NULL)
6866 release_tree_vector (placement);
6867 if (initializer != NULL)
6868 release_tree_vector (initializer);
6870 return ret;
6873 /* Parse a new-placement.
6875 new-placement:
6876 ( expression-list )
6878 Returns the same representation as for an expression-list. */
6880 static vec<tree, va_gc> *
6881 cp_parser_new_placement (cp_parser* parser)
6883 vec<tree, va_gc> *expression_list;
6885 /* Parse the expression-list. */
6886 expression_list = (cp_parser_parenthesized_expression_list
6887 (parser, non_attr, /*cast_p=*/false,
6888 /*allow_expansion_p=*/true,
6889 /*non_constant_p=*/NULL));
6891 return expression_list;
6894 /* Parse a new-type-id.
6896 new-type-id:
6897 type-specifier-seq new-declarator [opt]
6899 Returns the TYPE allocated. If the new-type-id indicates an array
6900 type, *NELTS is set to the number of elements in the last array
6901 bound; the TYPE will not include the last array bound. */
6903 static tree
6904 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6906 cp_decl_specifier_seq type_specifier_seq;
6907 cp_declarator *new_declarator;
6908 cp_declarator *declarator;
6909 cp_declarator *outer_declarator;
6910 const char *saved_message;
6912 /* The type-specifier sequence must not contain type definitions.
6913 (It cannot contain declarations of new types either, but if they
6914 are not definitions we will catch that because they are not
6915 complete.) */
6916 saved_message = parser->type_definition_forbidden_message;
6917 parser->type_definition_forbidden_message
6918 = G_("types may not be defined in a new-type-id");
6919 /* Parse the type-specifier-seq. */
6920 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6921 /*is_trailing_return=*/false,
6922 &type_specifier_seq);
6923 /* Restore the old message. */
6924 parser->type_definition_forbidden_message = saved_message;
6926 if (type_specifier_seq.type == error_mark_node)
6927 return error_mark_node;
6929 /* Parse the new-declarator. */
6930 new_declarator = cp_parser_new_declarator_opt (parser);
6932 /* Determine the number of elements in the last array dimension, if
6933 any. */
6934 *nelts = NULL_TREE;
6935 /* Skip down to the last array dimension. */
6936 declarator = new_declarator;
6937 outer_declarator = NULL;
6938 while (declarator && (declarator->kind == cdk_pointer
6939 || declarator->kind == cdk_ptrmem))
6941 outer_declarator = declarator;
6942 declarator = declarator->declarator;
6944 while (declarator
6945 && declarator->kind == cdk_array
6946 && declarator->declarator
6947 && declarator->declarator->kind == cdk_array)
6949 outer_declarator = declarator;
6950 declarator = declarator->declarator;
6953 if (declarator && declarator->kind == cdk_array)
6955 *nelts = declarator->u.array.bounds;
6956 if (*nelts == error_mark_node)
6957 *nelts = integer_one_node;
6959 if (outer_declarator)
6960 outer_declarator->declarator = declarator->declarator;
6961 else
6962 new_declarator = NULL;
6965 return groktypename (&type_specifier_seq, new_declarator, false);
6968 /* Parse an (optional) new-declarator.
6970 new-declarator:
6971 ptr-operator new-declarator [opt]
6972 direct-new-declarator
6974 Returns the declarator. */
6976 static cp_declarator *
6977 cp_parser_new_declarator_opt (cp_parser* parser)
6979 enum tree_code code;
6980 tree type, std_attributes = NULL_TREE;
6981 cp_cv_quals cv_quals;
6983 /* We don't know if there's a ptr-operator next, or not. */
6984 cp_parser_parse_tentatively (parser);
6985 /* Look for a ptr-operator. */
6986 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
6987 /* If that worked, look for more new-declarators. */
6988 if (cp_parser_parse_definitely (parser))
6990 cp_declarator *declarator;
6992 /* Parse another optional declarator. */
6993 declarator = cp_parser_new_declarator_opt (parser);
6995 declarator = cp_parser_make_indirect_declarator
6996 (code, type, cv_quals, declarator, std_attributes);
6998 return declarator;
7001 /* If the next token is a `[', there is a direct-new-declarator. */
7002 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7003 return cp_parser_direct_new_declarator (parser);
7005 return NULL;
7008 /* Parse a direct-new-declarator.
7010 direct-new-declarator:
7011 [ expression ]
7012 direct-new-declarator [constant-expression]
7016 static cp_declarator *
7017 cp_parser_direct_new_declarator (cp_parser* parser)
7019 cp_declarator *declarator = NULL;
7021 while (true)
7023 tree expression;
7024 cp_token *token;
7026 /* Look for the opening `['. */
7027 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7029 token = cp_lexer_peek_token (parser->lexer);
7030 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7031 /* The standard requires that the expression have integral
7032 type. DR 74 adds enumeration types. We believe that the
7033 real intent is that these expressions be handled like the
7034 expression in a `switch' condition, which also allows
7035 classes with a single conversion to integral or
7036 enumeration type. */
7037 if (!processing_template_decl)
7039 expression
7040 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7041 expression,
7042 /*complain=*/true);
7043 if (!expression)
7045 error_at (token->location,
7046 "expression in new-declarator must have integral "
7047 "or enumeration type");
7048 expression = error_mark_node;
7052 /* Look for the closing `]'. */
7053 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7055 /* Add this bound to the declarator. */
7056 declarator = make_array_declarator (declarator, expression);
7058 /* If the next token is not a `[', then there are no more
7059 bounds. */
7060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7061 break;
7064 return declarator;
7067 /* Parse a new-initializer.
7069 new-initializer:
7070 ( expression-list [opt] )
7071 braced-init-list
7073 Returns a representation of the expression-list. */
7075 static vec<tree, va_gc> *
7076 cp_parser_new_initializer (cp_parser* parser)
7078 vec<tree, va_gc> *expression_list;
7080 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7082 tree t;
7083 bool expr_non_constant_p;
7084 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7085 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7086 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7087 expression_list = make_tree_vector_single (t);
7089 else
7090 expression_list = (cp_parser_parenthesized_expression_list
7091 (parser, non_attr, /*cast_p=*/false,
7092 /*allow_expansion_p=*/true,
7093 /*non_constant_p=*/NULL));
7095 return expression_list;
7098 /* Parse a delete-expression.
7100 delete-expression:
7101 :: [opt] delete cast-expression
7102 :: [opt] delete [ ] cast-expression
7104 Returns a representation of the expression. */
7106 static tree
7107 cp_parser_delete_expression (cp_parser* parser)
7109 bool global_scope_p;
7110 bool array_p;
7111 tree expression;
7113 /* Look for the optional `::' operator. */
7114 global_scope_p
7115 = (cp_parser_global_scope_opt (parser,
7116 /*current_scope_valid_p=*/false)
7117 != NULL_TREE);
7118 /* Look for the `delete' keyword. */
7119 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7120 /* See if the array syntax is in use. */
7121 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7123 /* Consume the `[' token. */
7124 cp_lexer_consume_token (parser->lexer);
7125 /* Look for the `]' token. */
7126 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7127 /* Remember that this is the `[]' construct. */
7128 array_p = true;
7130 else
7131 array_p = false;
7133 /* Parse the cast-expression. */
7134 expression = cp_parser_simple_cast_expression (parser);
7136 /* A delete-expression may not appear in an integral constant
7137 expression. */
7138 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7139 return error_mark_node;
7141 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7142 tf_warning_or_error);
7145 /* Returns true if TOKEN may start a cast-expression and false
7146 otherwise. */
7148 static bool
7149 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7151 cp_token *token = cp_lexer_peek_token (parser->lexer);
7152 switch (token->type)
7154 case CPP_COMMA:
7155 case CPP_SEMICOLON:
7156 case CPP_QUERY:
7157 case CPP_COLON:
7158 case CPP_CLOSE_SQUARE:
7159 case CPP_CLOSE_PAREN:
7160 case CPP_CLOSE_BRACE:
7161 case CPP_DOT:
7162 case CPP_DOT_STAR:
7163 case CPP_DEREF:
7164 case CPP_DEREF_STAR:
7165 case CPP_DIV:
7166 case CPP_MOD:
7167 case CPP_LSHIFT:
7168 case CPP_RSHIFT:
7169 case CPP_LESS:
7170 case CPP_GREATER:
7171 case CPP_LESS_EQ:
7172 case CPP_GREATER_EQ:
7173 case CPP_EQ_EQ:
7174 case CPP_NOT_EQ:
7175 case CPP_EQ:
7176 case CPP_MULT_EQ:
7177 case CPP_DIV_EQ:
7178 case CPP_MOD_EQ:
7179 case CPP_PLUS_EQ:
7180 case CPP_MINUS_EQ:
7181 case CPP_RSHIFT_EQ:
7182 case CPP_LSHIFT_EQ:
7183 case CPP_AND_EQ:
7184 case CPP_XOR_EQ:
7185 case CPP_OR_EQ:
7186 case CPP_XOR:
7187 case CPP_OR:
7188 case CPP_OR_OR:
7189 case CPP_EOF:
7190 return false;
7192 case CPP_OPEN_PAREN:
7193 /* In ((type ()) () the last () isn't a valid cast-expression,
7194 so the whole must be parsed as postfix-expression. */
7195 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7196 != CPP_CLOSE_PAREN;
7198 /* '[' may start a primary-expression in obj-c++. */
7199 case CPP_OPEN_SQUARE:
7200 return c_dialect_objc ();
7202 default:
7203 return true;
7207 /* Parse a cast-expression.
7209 cast-expression:
7210 unary-expression
7211 ( type-id ) cast-expression
7213 ADDRESS_P is true iff the unary-expression is appearing as the
7214 operand of the `&' operator. CAST_P is true if this expression is
7215 the target of a cast.
7217 Returns a representation of the expression. */
7219 static tree
7220 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7221 bool decltype_p, cp_id_kind * pidk)
7223 /* If it's a `(', then we might be looking at a cast. */
7224 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7226 tree type = NULL_TREE;
7227 tree expr = NULL_TREE;
7228 bool compound_literal_p;
7229 const char *saved_message;
7231 /* There's no way to know yet whether or not this is a cast.
7232 For example, `(int (3))' is a unary-expression, while `(int)
7233 3' is a cast. So, we resort to parsing tentatively. */
7234 cp_parser_parse_tentatively (parser);
7235 /* Types may not be defined in a cast. */
7236 saved_message = parser->type_definition_forbidden_message;
7237 parser->type_definition_forbidden_message
7238 = G_("types may not be defined in casts");
7239 /* Consume the `('. */
7240 cp_lexer_consume_token (parser->lexer);
7241 /* A very tricky bit is that `(struct S) { 3 }' is a
7242 compound-literal (which we permit in C++ as an extension).
7243 But, that construct is not a cast-expression -- it is a
7244 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7245 is legal; if the compound-literal were a cast-expression,
7246 you'd need an extra set of parentheses.) But, if we parse
7247 the type-id, and it happens to be a class-specifier, then we
7248 will commit to the parse at that point, because we cannot
7249 undo the action that is done when creating a new class. So,
7250 then we cannot back up and do a postfix-expression.
7252 Therefore, we scan ahead to the closing `)', and check to see
7253 if the token after the `)' is a `{'. If so, we are not
7254 looking at a cast-expression.
7256 Save tokens so that we can put them back. */
7257 cp_lexer_save_tokens (parser->lexer);
7258 /* Skip tokens until the next token is a closing parenthesis.
7259 If we find the closing `)', and the next token is a `{', then
7260 we are looking at a compound-literal. */
7261 compound_literal_p
7262 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7263 /*consume_paren=*/true)
7264 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7265 /* Roll back the tokens we skipped. */
7266 cp_lexer_rollback_tokens (parser->lexer);
7267 /* If we were looking at a compound-literal, simulate an error
7268 so that the call to cp_parser_parse_definitely below will
7269 fail. */
7270 if (compound_literal_p)
7271 cp_parser_simulate_error (parser);
7272 else
7274 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7275 parser->in_type_id_in_expr_p = true;
7276 /* Look for the type-id. */
7277 type = cp_parser_type_id (parser);
7278 /* Look for the closing `)'. */
7279 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7280 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7283 /* Restore the saved message. */
7284 parser->type_definition_forbidden_message = saved_message;
7286 /* At this point this can only be either a cast or a
7287 parenthesized ctor such as `(T ())' that looks like a cast to
7288 function returning T. */
7289 if (!cp_parser_error_occurred (parser)
7290 && cp_parser_tokens_start_cast_expression (parser))
7292 cp_parser_parse_definitely (parser);
7293 expr = cp_parser_cast_expression (parser,
7294 /*address_p=*/false,
7295 /*cast_p=*/true,
7296 /*decltype_p=*/false,
7297 pidk);
7299 /* Warn about old-style casts, if so requested. */
7300 if (warn_old_style_cast
7301 && !in_system_header
7302 && !VOID_TYPE_P (type)
7303 && current_lang_name != lang_name_c)
7304 warning (OPT_Wold_style_cast, "use of old-style cast");
7306 /* Only type conversions to integral or enumeration types
7307 can be used in constant-expressions. */
7308 if (!cast_valid_in_integral_constant_expression_p (type)
7309 && cp_parser_non_integral_constant_expression (parser,
7310 NIC_CAST))
7311 return error_mark_node;
7313 /* Perform the cast. */
7314 expr = build_c_cast (input_location, type, expr);
7315 return expr;
7317 else
7318 cp_parser_abort_tentative_parse (parser);
7321 /* If we get here, then it's not a cast, so it must be a
7322 unary-expression. */
7323 return cp_parser_unary_expression (parser, address_p, cast_p,
7324 decltype_p, pidk);
7327 /* Parse a binary expression of the general form:
7329 pm-expression:
7330 cast-expression
7331 pm-expression .* cast-expression
7332 pm-expression ->* cast-expression
7334 multiplicative-expression:
7335 pm-expression
7336 multiplicative-expression * pm-expression
7337 multiplicative-expression / pm-expression
7338 multiplicative-expression % pm-expression
7340 additive-expression:
7341 multiplicative-expression
7342 additive-expression + multiplicative-expression
7343 additive-expression - multiplicative-expression
7345 shift-expression:
7346 additive-expression
7347 shift-expression << additive-expression
7348 shift-expression >> additive-expression
7350 relational-expression:
7351 shift-expression
7352 relational-expression < shift-expression
7353 relational-expression > shift-expression
7354 relational-expression <= shift-expression
7355 relational-expression >= shift-expression
7357 GNU Extension:
7359 relational-expression:
7360 relational-expression <? shift-expression
7361 relational-expression >? shift-expression
7363 equality-expression:
7364 relational-expression
7365 equality-expression == relational-expression
7366 equality-expression != relational-expression
7368 and-expression:
7369 equality-expression
7370 and-expression & equality-expression
7372 exclusive-or-expression:
7373 and-expression
7374 exclusive-or-expression ^ and-expression
7376 inclusive-or-expression:
7377 exclusive-or-expression
7378 inclusive-or-expression | exclusive-or-expression
7380 logical-and-expression:
7381 inclusive-or-expression
7382 logical-and-expression && inclusive-or-expression
7384 logical-or-expression:
7385 logical-and-expression
7386 logical-or-expression || logical-and-expression
7388 All these are implemented with a single function like:
7390 binary-expression:
7391 simple-cast-expression
7392 binary-expression <token> binary-expression
7394 CAST_P is true if this expression is the target of a cast.
7396 The binops_by_token map is used to get the tree codes for each <token> type.
7397 binary-expressions are associated according to a precedence table. */
7399 #define TOKEN_PRECEDENCE(token) \
7400 (((token->type == CPP_GREATER \
7401 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7402 && !parser->greater_than_is_operator_p) \
7403 ? PREC_NOT_OPERATOR \
7404 : binops_by_token[token->type].prec)
7406 static tree
7407 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7408 bool no_toplevel_fold_p,
7409 bool decltype_p,
7410 enum cp_parser_prec prec,
7411 cp_id_kind * pidk)
7413 cp_parser_expression_stack stack;
7414 cp_parser_expression_stack_entry *sp = &stack[0];
7415 cp_parser_expression_stack_entry current;
7416 tree rhs;
7417 cp_token *token;
7418 enum tree_code rhs_type;
7419 enum cp_parser_prec new_prec, lookahead_prec;
7420 tree overload;
7422 /* Parse the first expression. */
7423 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7424 cast_p, decltype_p, pidk);
7425 current.lhs_type = ERROR_MARK;
7426 current.prec = prec;
7428 if (cp_parser_error_occurred (parser))
7429 return error_mark_node;
7431 for (;;)
7433 /* Get an operator token. */
7434 token = cp_lexer_peek_token (parser->lexer);
7436 if (warn_cxx0x_compat
7437 && token->type == CPP_RSHIFT
7438 && !parser->greater_than_is_operator_p)
7440 if (warning_at (token->location, OPT_Wc__0x_compat,
7441 "%<>>%> operator is treated"
7442 " as two right angle brackets in C++11"))
7443 inform (token->location,
7444 "suggest parentheses around %<>>%> expression");
7447 new_prec = TOKEN_PRECEDENCE (token);
7449 /* Popping an entry off the stack means we completed a subexpression:
7450 - either we found a token which is not an operator (`>' where it is not
7451 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7452 will happen repeatedly;
7453 - or, we found an operator which has lower priority. This is the case
7454 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7455 parsing `3 * 4'. */
7456 if (new_prec <= current.prec)
7458 if (sp == stack)
7459 break;
7460 else
7461 goto pop;
7464 get_rhs:
7465 current.tree_type = binops_by_token[token->type].tree_type;
7466 current.loc = token->location;
7468 /* We used the operator token. */
7469 cp_lexer_consume_token (parser->lexer);
7471 /* For "false && x" or "true || x", x will never be executed;
7472 disable warnings while evaluating it. */
7473 if (current.tree_type == TRUTH_ANDIF_EXPR)
7474 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7475 else if (current.tree_type == TRUTH_ORIF_EXPR)
7476 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7478 /* Extract another operand. It may be the RHS of this expression
7479 or the LHS of a new, higher priority expression. */
7480 rhs = cp_parser_simple_cast_expression (parser);
7481 rhs_type = ERROR_MARK;
7483 /* Get another operator token. Look up its precedence to avoid
7484 building a useless (immediately popped) stack entry for common
7485 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7486 token = cp_lexer_peek_token (parser->lexer);
7487 lookahead_prec = TOKEN_PRECEDENCE (token);
7488 if (lookahead_prec > new_prec)
7490 /* ... and prepare to parse the RHS of the new, higher priority
7491 expression. Since precedence levels on the stack are
7492 monotonically increasing, we do not have to care about
7493 stack overflows. */
7494 *sp = current;
7495 ++sp;
7496 current.lhs = rhs;
7497 current.lhs_type = rhs_type;
7498 current.prec = new_prec;
7499 new_prec = lookahead_prec;
7500 goto get_rhs;
7502 pop:
7503 lookahead_prec = new_prec;
7504 /* If the stack is not empty, we have parsed into LHS the right side
7505 (`4' in the example above) of an expression we had suspended.
7506 We can use the information on the stack to recover the LHS (`3')
7507 from the stack together with the tree code (`MULT_EXPR'), and
7508 the precedence of the higher level subexpression
7509 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7510 which will be used to actually build the additive expression. */
7511 rhs = current.lhs;
7512 rhs_type = current.lhs_type;
7513 --sp;
7514 current = *sp;
7517 /* Undo the disabling of warnings done above. */
7518 if (current.tree_type == TRUTH_ANDIF_EXPR)
7519 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7520 else if (current.tree_type == TRUTH_ORIF_EXPR)
7521 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7523 overload = NULL;
7524 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7525 ERROR_MARK for everything that is not a binary expression.
7526 This makes warn_about_parentheses miss some warnings that
7527 involve unary operators. For unary expressions we should
7528 pass the correct tree_code unless the unary expression was
7529 surrounded by parentheses.
7531 if (no_toplevel_fold_p
7532 && lookahead_prec <= current.prec
7533 && sp == stack
7534 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7535 current.lhs = build2 (current.tree_type, boolean_type_node,
7536 current.lhs, rhs);
7537 else
7538 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7539 current.lhs, current.lhs_type,
7540 rhs, rhs_type, &overload,
7541 complain_flags (decltype_p));
7542 current.lhs_type = current.tree_type;
7543 if (EXPR_P (current.lhs))
7544 SET_EXPR_LOCATION (current.lhs, current.loc);
7546 /* If the binary operator required the use of an overloaded operator,
7547 then this expression cannot be an integral constant-expression.
7548 An overloaded operator can be used even if both operands are
7549 otherwise permissible in an integral constant-expression if at
7550 least one of the operands is of enumeration type. */
7552 if (overload
7553 && cp_parser_non_integral_constant_expression (parser,
7554 NIC_OVERLOADED))
7555 return error_mark_node;
7558 return current.lhs;
7561 static tree
7562 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7563 bool no_toplevel_fold_p,
7564 enum cp_parser_prec prec,
7565 cp_id_kind * pidk)
7567 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7568 /*decltype*/false, prec, pidk);
7571 /* Parse the `? expression : assignment-expression' part of a
7572 conditional-expression. The LOGICAL_OR_EXPR is the
7573 logical-or-expression that started the conditional-expression.
7574 Returns a representation of the entire conditional-expression.
7576 This routine is used by cp_parser_assignment_expression.
7578 ? expression : assignment-expression
7580 GNU Extensions:
7582 ? : assignment-expression */
7584 static tree
7585 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7587 tree expr;
7588 tree assignment_expr;
7589 struct cp_token *token;
7590 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7592 /* Consume the `?' token. */
7593 cp_lexer_consume_token (parser->lexer);
7594 token = cp_lexer_peek_token (parser->lexer);
7595 if (cp_parser_allow_gnu_extensions_p (parser)
7596 && token->type == CPP_COLON)
7598 pedwarn (token->location, OPT_Wpedantic,
7599 "ISO C++ does not allow ?: with omitted middle operand");
7600 /* Implicit true clause. */
7601 expr = NULL_TREE;
7602 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7603 warn_for_omitted_condop (token->location, logical_or_expr);
7605 else
7607 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7608 parser->colon_corrects_to_scope_p = false;
7609 /* Parse the expression. */
7610 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7611 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7612 c_inhibit_evaluation_warnings +=
7613 ((logical_or_expr == truthvalue_true_node)
7614 - (logical_or_expr == truthvalue_false_node));
7615 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7618 /* The next token should be a `:'. */
7619 cp_parser_require (parser, CPP_COLON, RT_COLON);
7620 /* Parse the assignment-expression. */
7621 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7622 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7624 /* Build the conditional-expression. */
7625 return build_x_conditional_expr (loc, logical_or_expr,
7626 expr,
7627 assignment_expr,
7628 tf_warning_or_error);
7631 /* Parse an assignment-expression.
7633 assignment-expression:
7634 conditional-expression
7635 logical-or-expression assignment-operator assignment_expression
7636 throw-expression
7638 CAST_P is true if this expression is the target of a cast.
7639 DECLTYPE_P is true if this expression is the operand of decltype.
7641 Returns a representation for the expression. */
7643 static tree
7644 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7645 bool decltype_p, cp_id_kind * pidk)
7647 tree expr;
7649 /* If the next token is the `throw' keyword, then we're looking at
7650 a throw-expression. */
7651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7652 expr = cp_parser_throw_expression (parser);
7653 /* Otherwise, it must be that we are looking at a
7654 logical-or-expression. */
7655 else
7657 /* Parse the binary expressions (logical-or-expression). */
7658 expr = cp_parser_binary_expression (parser, cast_p, false,
7659 decltype_p,
7660 PREC_NOT_OPERATOR, pidk);
7661 /* If the next token is a `?' then we're actually looking at a
7662 conditional-expression. */
7663 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7664 return cp_parser_question_colon_clause (parser, expr);
7665 else
7667 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7669 /* If it's an assignment-operator, we're using the second
7670 production. */
7671 enum tree_code assignment_operator
7672 = cp_parser_assignment_operator_opt (parser);
7673 if (assignment_operator != ERROR_MARK)
7675 bool non_constant_p;
7676 location_t saved_input_location;
7678 /* Parse the right-hand side of the assignment. */
7679 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7681 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7682 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7684 /* An assignment may not appear in a
7685 constant-expression. */
7686 if (cp_parser_non_integral_constant_expression (parser,
7687 NIC_ASSIGNMENT))
7688 return error_mark_node;
7689 /* Build the assignment expression. Its default
7690 location is the location of the '=' token. */
7691 saved_input_location = input_location;
7692 input_location = loc;
7693 expr = build_x_modify_expr (loc, expr,
7694 assignment_operator,
7695 rhs,
7696 complain_flags (decltype_p));
7697 input_location = saved_input_location;
7702 return expr;
7705 static tree
7706 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7707 cp_id_kind * pidk)
7709 return cp_parser_assignment_expression (parser, cast_p,
7710 /*decltype*/false, pidk);
7713 /* Parse an (optional) assignment-operator.
7715 assignment-operator: one of
7716 = *= /= %= += -= >>= <<= &= ^= |=
7718 GNU Extension:
7720 assignment-operator: one of
7721 <?= >?=
7723 If the next token is an assignment operator, the corresponding tree
7724 code is returned, and the token is consumed. For example, for
7725 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7726 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7727 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7728 operator, ERROR_MARK is returned. */
7730 static enum tree_code
7731 cp_parser_assignment_operator_opt (cp_parser* parser)
7733 enum tree_code op;
7734 cp_token *token;
7736 /* Peek at the next token. */
7737 token = cp_lexer_peek_token (parser->lexer);
7739 switch (token->type)
7741 case CPP_EQ:
7742 op = NOP_EXPR;
7743 break;
7745 case CPP_MULT_EQ:
7746 op = MULT_EXPR;
7747 break;
7749 case CPP_DIV_EQ:
7750 op = TRUNC_DIV_EXPR;
7751 break;
7753 case CPP_MOD_EQ:
7754 op = TRUNC_MOD_EXPR;
7755 break;
7757 case CPP_PLUS_EQ:
7758 op = PLUS_EXPR;
7759 break;
7761 case CPP_MINUS_EQ:
7762 op = MINUS_EXPR;
7763 break;
7765 case CPP_RSHIFT_EQ:
7766 op = RSHIFT_EXPR;
7767 break;
7769 case CPP_LSHIFT_EQ:
7770 op = LSHIFT_EXPR;
7771 break;
7773 case CPP_AND_EQ:
7774 op = BIT_AND_EXPR;
7775 break;
7777 case CPP_XOR_EQ:
7778 op = BIT_XOR_EXPR;
7779 break;
7781 case CPP_OR_EQ:
7782 op = BIT_IOR_EXPR;
7783 break;
7785 default:
7786 /* Nothing else is an assignment operator. */
7787 op = ERROR_MARK;
7790 /* If it was an assignment operator, consume it. */
7791 if (op != ERROR_MARK)
7792 cp_lexer_consume_token (parser->lexer);
7794 return op;
7797 /* Parse an expression.
7799 expression:
7800 assignment-expression
7801 expression , assignment-expression
7803 CAST_P is true if this expression is the target of a cast.
7804 DECLTYPE_P is true if this expression is the immediate operand of decltype,
7805 except possibly parenthesized or on the RHS of a comma (N3276).
7807 Returns a representation of the expression. */
7809 static tree
7810 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
7811 cp_id_kind * pidk)
7813 tree expression = NULL_TREE;
7814 location_t loc = UNKNOWN_LOCATION;
7816 while (true)
7818 tree assignment_expression;
7820 /* Parse the next assignment-expression. */
7821 assignment_expression
7822 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
7824 /* We don't create a temporary for a call that is the immediate operand
7825 of decltype or on the RHS of a comma. But when we see a comma, we
7826 need to create a temporary for a call on the LHS. */
7827 if (decltype_p && !processing_template_decl
7828 && TREE_CODE (assignment_expression) == CALL_EXPR
7829 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
7830 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7831 assignment_expression
7832 = build_cplus_new (TREE_TYPE (assignment_expression),
7833 assignment_expression, tf_warning_or_error);
7835 /* If this is the first assignment-expression, we can just
7836 save it away. */
7837 if (!expression)
7838 expression = assignment_expression;
7839 else
7840 expression = build_x_compound_expr (loc, expression,
7841 assignment_expression,
7842 complain_flags (decltype_p));
7843 /* If the next token is not a comma, then we are done with the
7844 expression. */
7845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7846 break;
7847 /* Consume the `,'. */
7848 loc = cp_lexer_peek_token (parser->lexer)->location;
7849 cp_lexer_consume_token (parser->lexer);
7850 /* A comma operator cannot appear in a constant-expression. */
7851 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7852 expression = error_mark_node;
7855 return expression;
7858 static inline tree
7859 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7861 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
7864 /* Parse a constant-expression.
7866 constant-expression:
7867 conditional-expression
7869 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7870 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7871 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7872 is false, NON_CONSTANT_P should be NULL. */
7874 static tree
7875 cp_parser_constant_expression (cp_parser* parser,
7876 bool allow_non_constant_p,
7877 bool *non_constant_p)
7879 bool saved_integral_constant_expression_p;
7880 bool saved_allow_non_integral_constant_expression_p;
7881 bool saved_non_integral_constant_expression_p;
7882 tree expression;
7884 /* It might seem that we could simply parse the
7885 conditional-expression, and then check to see if it were
7886 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7887 one that the compiler can figure out is constant, possibly after
7888 doing some simplifications or optimizations. The standard has a
7889 precise definition of constant-expression, and we must honor
7890 that, even though it is somewhat more restrictive.
7892 For example:
7894 int i[(2, 3)];
7896 is not a legal declaration, because `(2, 3)' is not a
7897 constant-expression. The `,' operator is forbidden in a
7898 constant-expression. However, GCC's constant-folding machinery
7899 will fold this operation to an INTEGER_CST for `3'. */
7901 /* Save the old settings. */
7902 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7903 saved_allow_non_integral_constant_expression_p
7904 = parser->allow_non_integral_constant_expression_p;
7905 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7906 /* We are now parsing a constant-expression. */
7907 parser->integral_constant_expression_p = true;
7908 parser->allow_non_integral_constant_expression_p
7909 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7910 parser->non_integral_constant_expression_p = false;
7911 /* Although the grammar says "conditional-expression", we parse an
7912 "assignment-expression", which also permits "throw-expression"
7913 and the use of assignment operators. In the case that
7914 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7915 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7916 actually essential that we look for an assignment-expression.
7917 For example, cp_parser_initializer_clauses uses this function to
7918 determine whether a particular assignment-expression is in fact
7919 constant. */
7920 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7921 /* Restore the old settings. */
7922 parser->integral_constant_expression_p
7923 = saved_integral_constant_expression_p;
7924 parser->allow_non_integral_constant_expression_p
7925 = saved_allow_non_integral_constant_expression_p;
7926 if (cxx_dialect >= cxx0x)
7928 /* Require an rvalue constant expression here; that's what our
7929 callers expect. Reference constant expressions are handled
7930 separately in e.g. cp_parser_template_argument. */
7931 bool is_const = potential_rvalue_constant_expression (expression);
7932 parser->non_integral_constant_expression_p = !is_const;
7933 if (!is_const && !allow_non_constant_p)
7934 require_potential_rvalue_constant_expression (expression);
7936 if (allow_non_constant_p)
7937 *non_constant_p = parser->non_integral_constant_expression_p;
7938 parser->non_integral_constant_expression_p
7939 = saved_non_integral_constant_expression_p;
7941 return expression;
7944 /* Parse __builtin_offsetof.
7946 offsetof-expression:
7947 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7949 offsetof-member-designator:
7950 id-expression
7951 | offsetof-member-designator "." id-expression
7952 | offsetof-member-designator "[" expression "]"
7953 | offsetof-member-designator "->" id-expression */
7955 static tree
7956 cp_parser_builtin_offsetof (cp_parser *parser)
7958 int save_ice_p, save_non_ice_p;
7959 tree type, expr;
7960 cp_id_kind dummy;
7961 cp_token *token;
7963 /* We're about to accept non-integral-constant things, but will
7964 definitely yield an integral constant expression. Save and
7965 restore these values around our local parsing. */
7966 save_ice_p = parser->integral_constant_expression_p;
7967 save_non_ice_p = parser->non_integral_constant_expression_p;
7969 /* Consume the "__builtin_offsetof" token. */
7970 cp_lexer_consume_token (parser->lexer);
7971 /* Consume the opening `('. */
7972 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7973 /* Parse the type-id. */
7974 type = cp_parser_type_id (parser);
7975 /* Look for the `,'. */
7976 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7977 token = cp_lexer_peek_token (parser->lexer);
7979 /* Build the (type *)null that begins the traditional offsetof macro. */
7980 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7981 tf_warning_or_error);
7983 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7984 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7985 true, &dummy, token->location);
7986 while (true)
7988 token = cp_lexer_peek_token (parser->lexer);
7989 switch (token->type)
7991 case CPP_OPEN_SQUARE:
7992 /* offsetof-member-designator "[" expression "]" */
7993 expr = cp_parser_postfix_open_square_expression (parser, expr,
7994 true, false);
7995 break;
7997 case CPP_DEREF:
7998 /* offsetof-member-designator "->" identifier */
7999 expr = grok_array_decl (token->location, expr,
8000 integer_zero_node, false);
8001 /* FALLTHRU */
8003 case CPP_DOT:
8004 /* offsetof-member-designator "." identifier */
8005 cp_lexer_consume_token (parser->lexer);
8006 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8007 expr, true, &dummy,
8008 token->location);
8009 break;
8011 case CPP_CLOSE_PAREN:
8012 /* Consume the ")" token. */
8013 cp_lexer_consume_token (parser->lexer);
8014 goto success;
8016 default:
8017 /* Error. We know the following require will fail, but
8018 that gives the proper error message. */
8019 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8020 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8021 expr = error_mark_node;
8022 goto failure;
8026 success:
8027 /* If we're processing a template, we can't finish the semantics yet.
8028 Otherwise we can fold the entire expression now. */
8029 if (processing_template_decl)
8030 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8031 else
8032 expr = finish_offsetof (expr);
8034 failure:
8035 parser->integral_constant_expression_p = save_ice_p;
8036 parser->non_integral_constant_expression_p = save_non_ice_p;
8038 return expr;
8041 /* Parse a trait expression.
8043 Returns a representation of the expression, the underlying type
8044 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8046 static tree
8047 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8049 cp_trait_kind kind;
8050 tree type1, type2 = NULL_TREE;
8051 bool binary = false;
8052 cp_decl_specifier_seq decl_specs;
8054 switch (keyword)
8056 case RID_HAS_NOTHROW_ASSIGN:
8057 kind = CPTK_HAS_NOTHROW_ASSIGN;
8058 break;
8059 case RID_HAS_NOTHROW_CONSTRUCTOR:
8060 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8061 break;
8062 case RID_HAS_NOTHROW_COPY:
8063 kind = CPTK_HAS_NOTHROW_COPY;
8064 break;
8065 case RID_HAS_TRIVIAL_ASSIGN:
8066 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8067 break;
8068 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8069 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8070 break;
8071 case RID_HAS_TRIVIAL_COPY:
8072 kind = CPTK_HAS_TRIVIAL_COPY;
8073 break;
8074 case RID_HAS_TRIVIAL_DESTRUCTOR:
8075 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8076 break;
8077 case RID_HAS_VIRTUAL_DESTRUCTOR:
8078 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8079 break;
8080 case RID_IS_ABSTRACT:
8081 kind = CPTK_IS_ABSTRACT;
8082 break;
8083 case RID_IS_BASE_OF:
8084 kind = CPTK_IS_BASE_OF;
8085 binary = true;
8086 break;
8087 case RID_IS_CLASS:
8088 kind = CPTK_IS_CLASS;
8089 break;
8090 case RID_IS_CONVERTIBLE_TO:
8091 kind = CPTK_IS_CONVERTIBLE_TO;
8092 binary = true;
8093 break;
8094 case RID_IS_EMPTY:
8095 kind = CPTK_IS_EMPTY;
8096 break;
8097 case RID_IS_ENUM:
8098 kind = CPTK_IS_ENUM;
8099 break;
8100 case RID_IS_FINAL:
8101 kind = CPTK_IS_FINAL;
8102 break;
8103 case RID_IS_LITERAL_TYPE:
8104 kind = CPTK_IS_LITERAL_TYPE;
8105 break;
8106 case RID_IS_POD:
8107 kind = CPTK_IS_POD;
8108 break;
8109 case RID_IS_POLYMORPHIC:
8110 kind = CPTK_IS_POLYMORPHIC;
8111 break;
8112 case RID_IS_STD_LAYOUT:
8113 kind = CPTK_IS_STD_LAYOUT;
8114 break;
8115 case RID_IS_TRIVIAL:
8116 kind = CPTK_IS_TRIVIAL;
8117 break;
8118 case RID_IS_UNION:
8119 kind = CPTK_IS_UNION;
8120 break;
8121 case RID_UNDERLYING_TYPE:
8122 kind = CPTK_UNDERLYING_TYPE;
8123 break;
8124 case RID_BASES:
8125 kind = CPTK_BASES;
8126 break;
8127 case RID_DIRECT_BASES:
8128 kind = CPTK_DIRECT_BASES;
8129 break;
8130 default:
8131 gcc_unreachable ();
8134 /* Consume the token. */
8135 cp_lexer_consume_token (parser->lexer);
8137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8139 type1 = cp_parser_type_id (parser);
8141 if (type1 == error_mark_node)
8142 return error_mark_node;
8144 /* Build a trivial decl-specifier-seq. */
8145 clear_decl_specs (&decl_specs);
8146 decl_specs.type = type1;
8148 /* Call grokdeclarator to figure out what type this is. */
8149 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8150 /*initialized=*/0, /*attrlist=*/NULL);
8152 if (binary)
8154 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8156 type2 = cp_parser_type_id (parser);
8158 if (type2 == error_mark_node)
8159 return error_mark_node;
8161 /* Build a trivial decl-specifier-seq. */
8162 clear_decl_specs (&decl_specs);
8163 decl_specs.type = type2;
8165 /* Call grokdeclarator to figure out what type this is. */
8166 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8167 /*initialized=*/0, /*attrlist=*/NULL);
8170 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8172 /* Complete the trait expression, which may mean either processing
8173 the trait expr now or saving it for template instantiation. */
8174 switch(kind)
8176 case CPTK_UNDERLYING_TYPE:
8177 return finish_underlying_type (type1);
8178 case CPTK_BASES:
8179 return finish_bases (type1, false);
8180 case CPTK_DIRECT_BASES:
8181 return finish_bases (type1, true);
8182 default:
8183 return finish_trait_expr (kind, type1, type2);
8187 /* Lambdas that appear in variable initializer or default argument scope
8188 get that in their mangling, so we need to record it. We might as well
8189 use the count for function and namespace scopes as well. */
8190 static GTY(()) tree lambda_scope;
8191 static GTY(()) int lambda_count;
8192 typedef struct GTY(()) tree_int
8194 tree t;
8195 int i;
8196 } tree_int;
8197 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8199 static void
8200 start_lambda_scope (tree decl)
8202 tree_int ti;
8203 gcc_assert (decl);
8204 /* Once we're inside a function, we ignore other scopes and just push
8205 the function again so that popping works properly. */
8206 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8207 decl = current_function_decl;
8208 ti.t = lambda_scope;
8209 ti.i = lambda_count;
8210 vec_safe_push (lambda_scope_stack, ti);
8211 if (lambda_scope != decl)
8213 /* Don't reset the count if we're still in the same function. */
8214 lambda_scope = decl;
8215 lambda_count = 0;
8219 static void
8220 record_lambda_scope (tree lambda)
8222 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8223 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8226 static void
8227 finish_lambda_scope (void)
8229 tree_int *p = &lambda_scope_stack->last ();
8230 if (lambda_scope != p->t)
8232 lambda_scope = p->t;
8233 lambda_count = p->i;
8235 lambda_scope_stack->pop ();
8238 /* Parse a lambda expression.
8240 lambda-expression:
8241 lambda-introducer lambda-declarator [opt] compound-statement
8243 Returns a representation of the expression. */
8245 static tree
8246 cp_parser_lambda_expression (cp_parser* parser)
8248 tree lambda_expr = build_lambda_expr ();
8249 tree type;
8250 bool ok;
8252 LAMBDA_EXPR_LOCATION (lambda_expr)
8253 = cp_lexer_peek_token (parser->lexer)->location;
8255 if (cp_unevaluated_operand)
8256 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8257 "lambda-expression in unevaluated context");
8259 /* We may be in the middle of deferred access check. Disable
8260 it now. */
8261 push_deferring_access_checks (dk_no_deferred);
8263 cp_parser_lambda_introducer (parser, lambda_expr);
8265 type = begin_lambda_type (lambda_expr);
8266 if (type == error_mark_node)
8267 return error_mark_node;
8269 record_lambda_scope (lambda_expr);
8271 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8272 determine_visibility (TYPE_NAME (type));
8274 /* Now that we've started the type, add the capture fields for any
8275 explicit captures. */
8276 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8279 /* Inside the class, surrounding template-parameter-lists do not apply. */
8280 unsigned int saved_num_template_parameter_lists
8281 = parser->num_template_parameter_lists;
8282 unsigned char in_statement = parser->in_statement;
8283 bool in_switch_statement_p = parser->in_switch_statement_p;
8285 parser->num_template_parameter_lists = 0;
8286 parser->in_statement = 0;
8287 parser->in_switch_statement_p = false;
8289 /* By virtue of defining a local class, a lambda expression has access to
8290 the private variables of enclosing classes. */
8292 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8294 if (ok)
8295 cp_parser_lambda_body (parser, lambda_expr);
8296 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8297 cp_parser_skip_to_end_of_block_or_statement (parser);
8299 /* The capture list was built up in reverse order; fix that now. */
8301 tree newlist = NULL_TREE;
8302 tree elt, next;
8304 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8305 elt; elt = next)
8307 next = TREE_CHAIN (elt);
8308 TREE_CHAIN (elt) = newlist;
8309 newlist = elt;
8311 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8314 if (ok)
8315 maybe_add_lambda_conv_op (type);
8317 type = finish_struct (type, /*attributes=*/NULL_TREE);
8319 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8320 parser->in_statement = in_statement;
8321 parser->in_switch_statement_p = in_switch_statement_p;
8324 pop_deferring_access_checks ();
8326 /* This field is only used during parsing of the lambda. */
8327 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8329 /* This lambda shouldn't have any proxies left at this point. */
8330 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8331 /* And now that we're done, push proxies for an enclosing lambda. */
8332 insert_pending_capture_proxies ();
8334 if (ok)
8335 return build_lambda_object (lambda_expr);
8336 else
8337 return error_mark_node;
8340 /* Parse the beginning of a lambda expression.
8342 lambda-introducer:
8343 [ lambda-capture [opt] ]
8345 LAMBDA_EXPR is the current representation of the lambda expression. */
8347 static void
8348 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8350 /* Need commas after the first capture. */
8351 bool first = true;
8353 /* Eat the leading `['. */
8354 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8356 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8357 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8358 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8359 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8360 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8361 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8363 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8365 cp_lexer_consume_token (parser->lexer);
8366 first = false;
8369 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8371 cp_token* capture_token;
8372 tree capture_id;
8373 tree capture_init_expr;
8374 cp_id_kind idk = CP_ID_KIND_NONE;
8375 bool explicit_init_p = false;
8377 enum capture_kind_type
8379 BY_COPY,
8380 BY_REFERENCE
8382 enum capture_kind_type capture_kind = BY_COPY;
8384 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8386 error ("expected end of capture-list");
8387 return;
8390 if (first)
8391 first = false;
8392 else
8393 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8395 /* Possibly capture `this'. */
8396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8398 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8399 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8400 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8401 "with by-copy capture default");
8402 cp_lexer_consume_token (parser->lexer);
8403 add_capture (lambda_expr,
8404 /*id=*/this_identifier,
8405 /*initializer=*/finish_this_expr(),
8406 /*by_reference_p=*/false,
8407 explicit_init_p);
8408 continue;
8411 /* Remember whether we want to capture as a reference or not. */
8412 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8414 capture_kind = BY_REFERENCE;
8415 cp_lexer_consume_token (parser->lexer);
8418 /* Get the identifier. */
8419 capture_token = cp_lexer_peek_token (parser->lexer);
8420 capture_id = cp_parser_identifier (parser);
8422 if (capture_id == error_mark_node)
8423 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8424 delimiters, but I modified this to stop on unnested ']' as well. It
8425 was already changed to stop on unnested '}', so the
8426 "closing_parenthesis" name is no more misleading with my change. */
8428 cp_parser_skip_to_closing_parenthesis (parser,
8429 /*recovering=*/true,
8430 /*or_comma=*/true,
8431 /*consume_paren=*/true);
8432 break;
8435 /* Find the initializer for this capture. */
8436 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8438 /* An explicit expression exists. */
8439 cp_lexer_consume_token (parser->lexer);
8440 pedwarn (input_location, OPT_Wpedantic,
8441 "ISO C++ does not allow initializers "
8442 "in lambda expression capture lists");
8443 capture_init_expr = cp_parser_assignment_expression (parser,
8444 /*cast_p=*/true,
8445 &idk);
8446 explicit_init_p = true;
8448 else
8450 const char* error_msg;
8452 /* Turn the identifier into an id-expression. */
8453 capture_init_expr
8454 = cp_parser_lookup_name
8455 (parser,
8456 capture_id,
8457 none_type,
8458 /*is_template=*/false,
8459 /*is_namespace=*/false,
8460 /*check_dependency=*/true,
8461 /*ambiguous_decls=*/NULL,
8462 capture_token->location);
8464 if (capture_init_expr == error_mark_node)
8466 unqualified_name_lookup_error (capture_id);
8467 continue;
8469 else if (DECL_P (capture_init_expr)
8470 && (TREE_CODE (capture_init_expr) != VAR_DECL
8471 && TREE_CODE (capture_init_expr) != PARM_DECL))
8473 error_at (capture_token->location,
8474 "capture of non-variable %qD ",
8475 capture_init_expr);
8476 inform (0, "%q+#D declared here", capture_init_expr);
8477 continue;
8479 if (TREE_CODE (capture_init_expr) == VAR_DECL
8480 && decl_storage_duration (capture_init_expr) != dk_auto)
8482 pedwarn (capture_token->location, 0, "capture of variable "
8483 "%qD with non-automatic storage duration",
8484 capture_init_expr);
8485 inform (0, "%q+#D declared here", capture_init_expr);
8486 continue;
8489 capture_init_expr
8490 = finish_id_expression
8491 (capture_id,
8492 capture_init_expr,
8493 parser->scope,
8494 &idk,
8495 /*integral_constant_expression_p=*/false,
8496 /*allow_non_integral_constant_expression_p=*/false,
8497 /*non_integral_constant_expression_p=*/NULL,
8498 /*template_p=*/false,
8499 /*done=*/true,
8500 /*address_p=*/false,
8501 /*template_arg_p=*/false,
8502 &error_msg,
8503 capture_token->location);
8506 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8507 && !explicit_init_p)
8509 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8510 && capture_kind == BY_COPY)
8511 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8512 "of %qD redundant with by-copy capture default",
8513 capture_id);
8514 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8515 && capture_kind == BY_REFERENCE)
8516 pedwarn (capture_token->location, 0, "explicit by-reference "
8517 "capture of %qD redundant with by-reference capture "
8518 "default", capture_id);
8521 add_capture (lambda_expr,
8522 capture_id,
8523 capture_init_expr,
8524 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8525 explicit_init_p);
8528 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8531 /* Parse the (optional) middle of a lambda expression.
8533 lambda-declarator:
8534 ( parameter-declaration-clause [opt] )
8535 attribute-specifier [opt]
8536 mutable [opt]
8537 exception-specification [opt]
8538 lambda-return-type-clause [opt]
8540 LAMBDA_EXPR is the current representation of the lambda expression. */
8542 static bool
8543 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8545 /* 5.1.1.4 of the standard says:
8546 If a lambda-expression does not include a lambda-declarator, it is as if
8547 the lambda-declarator were ().
8548 This means an empty parameter list, no attributes, and no exception
8549 specification. */
8550 tree param_list = void_list_node;
8551 tree attributes = NULL_TREE;
8552 tree exception_spec = NULL_TREE;
8553 tree t;
8555 /* The lambda-declarator is optional, but must begin with an opening
8556 parenthesis if present. */
8557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8559 cp_lexer_consume_token (parser->lexer);
8561 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8563 /* Parse parameters. */
8564 param_list = cp_parser_parameter_declaration_clause (parser);
8566 /* Default arguments shall not be specified in the
8567 parameter-declaration-clause of a lambda-declarator. */
8568 for (t = param_list; t; t = TREE_CHAIN (t))
8569 if (TREE_PURPOSE (t))
8570 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8571 "default argument specified for lambda parameter");
8573 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8575 attributes = cp_parser_attributes_opt (parser);
8577 /* Parse optional `mutable' keyword. */
8578 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8580 cp_lexer_consume_token (parser->lexer);
8581 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8584 /* Parse optional exception specification. */
8585 exception_spec = cp_parser_exception_specification_opt (parser);
8587 /* Parse optional trailing return type. */
8588 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8590 cp_lexer_consume_token (parser->lexer);
8591 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8594 /* The function parameters must be in scope all the way until after the
8595 trailing-return-type in case of decltype. */
8596 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8597 pop_binding (DECL_NAME (t), t);
8599 leave_scope ();
8602 /* Create the function call operator.
8604 Messing with declarators like this is no uglier than building up the
8605 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8606 other code. */
8608 cp_decl_specifier_seq return_type_specs;
8609 cp_declarator* declarator;
8610 tree fco;
8611 int quals;
8612 void *p;
8614 clear_decl_specs (&return_type_specs);
8615 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8616 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8617 else
8618 /* Maybe we will deduce the return type later. */
8619 return_type_specs.type = make_auto ();
8621 p = obstack_alloc (&declarator_obstack, 0);
8623 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8624 sfk_none);
8626 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8627 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8628 declarator = make_call_declarator (declarator, param_list, quals,
8629 VIRT_SPEC_UNSPECIFIED,
8630 REF_QUAL_NONE,
8631 exception_spec,
8632 /*late_return_type=*/NULL_TREE);
8633 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8635 fco = grokmethod (&return_type_specs,
8636 declarator,
8637 attributes);
8638 if (fco != error_mark_node)
8640 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8641 DECL_ARTIFICIAL (fco) = 1;
8642 /* Give the object parameter a different name. */
8643 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8646 finish_member_declaration (fco);
8648 obstack_free (&declarator_obstack, p);
8650 return (fco != error_mark_node);
8654 /* Parse the body of a lambda expression, which is simply
8656 compound-statement
8658 but which requires special handling.
8659 LAMBDA_EXPR is the current representation of the lambda expression. */
8661 static void
8662 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8664 bool nested = (current_function_decl != NULL_TREE);
8665 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8666 if (nested)
8667 push_function_context ();
8668 else
8669 /* Still increment function_depth so that we don't GC in the
8670 middle of an expression. */
8671 ++function_depth;
8672 /* Clear this in case we're in the middle of a default argument. */
8673 parser->local_variables_forbidden_p = false;
8675 /* Finish the function call operator
8676 - class_specifier
8677 + late_parsing_for_member
8678 + function_definition_after_declarator
8679 + ctor_initializer_opt_and_function_body */
8681 tree fco = lambda_function (lambda_expr);
8682 tree body;
8683 bool done = false;
8684 tree compound_stmt;
8685 tree cap;
8687 /* Let the front end know that we are going to be defining this
8688 function. */
8689 start_preparsed_function (fco,
8690 NULL_TREE,
8691 SF_PRE_PARSED | SF_INCLASS_INLINE);
8693 start_lambda_scope (fco);
8694 body = begin_function_body ();
8696 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8697 goto out;
8699 /* Push the proxies for any explicit captures. */
8700 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8701 cap = TREE_CHAIN (cap))
8702 build_capture_proxy (TREE_PURPOSE (cap));
8704 compound_stmt = begin_compound_stmt (0);
8706 /* 5.1.1.4 of the standard says:
8707 If a lambda-expression does not include a trailing-return-type, it
8708 is as if the trailing-return-type denotes the following type:
8709 * if the compound-statement is of the form
8710 { return attribute-specifier [opt] expression ; }
8711 the type of the returned expression after lvalue-to-rvalue
8712 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8713 (_conv.array_ 4.2), and function-to-pointer conversion
8714 (_conv.func_ 4.3);
8715 * otherwise, void. */
8717 /* In a lambda that has neither a lambda-return-type-clause
8718 nor a deducible form, errors should be reported for return statements
8719 in the body. Since we used void as the placeholder return type, parsing
8720 the body as usual will give such desired behavior. */
8721 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8722 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8723 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8725 tree expr = NULL_TREE;
8726 cp_id_kind idk = CP_ID_KIND_NONE;
8728 /* Parse tentatively in case there's more after the initial return
8729 statement. */
8730 cp_parser_parse_tentatively (parser);
8732 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8734 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8736 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8737 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8739 if (cp_parser_parse_definitely (parser))
8741 if (!processing_template_decl)
8742 apply_deduced_return_type (fco, lambda_return_type (expr));
8744 /* Will get error here if type not deduced yet. */
8745 finish_return_stmt (expr);
8747 done = true;
8751 if (!done)
8753 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8754 cp_parser_label_declaration (parser);
8755 cp_parser_statement_seq_opt (parser, NULL_TREE);
8756 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8759 finish_compound_stmt (compound_stmt);
8761 out:
8762 finish_function_body (body);
8763 finish_lambda_scope ();
8765 /* Finish the function and generate code for it if necessary. */
8766 expand_or_defer_fn (finish_function (/*inline*/2));
8769 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8770 if (nested)
8771 pop_function_context();
8772 else
8773 --function_depth;
8776 /* Statements [gram.stmt.stmt] */
8778 /* Parse a statement.
8780 statement:
8781 labeled-statement
8782 expression-statement
8783 compound-statement
8784 selection-statement
8785 iteration-statement
8786 jump-statement
8787 declaration-statement
8788 try-block
8790 C++11:
8792 statement:
8793 labeled-statement
8794 attribute-specifier-seq (opt) expression-statement
8795 attribute-specifier-seq (opt) compound-statement
8796 attribute-specifier-seq (opt) selection-statement
8797 attribute-specifier-seq (opt) iteration-statement
8798 attribute-specifier-seq (opt) jump-statement
8799 declaration-statement
8800 attribute-specifier-seq (opt) try-block
8802 TM Extension:
8804 statement:
8805 atomic-statement
8807 IN_COMPOUND is true when the statement is nested inside a
8808 cp_parser_compound_statement; this matters for certain pragmas.
8810 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8811 is a (possibly labeled) if statement which is not enclosed in braces
8812 and has an else clause. This is used to implement -Wparentheses. */
8814 static void
8815 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8816 bool in_compound, bool *if_p)
8818 tree statement, std_attrs = NULL_TREE;
8819 cp_token *token;
8820 location_t statement_location, attrs_location;
8822 restart:
8823 if (if_p != NULL)
8824 *if_p = false;
8825 /* There is no statement yet. */
8826 statement = NULL_TREE;
8828 cp_lexer_save_tokens (parser->lexer);
8829 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8830 if (c_dialect_objc ())
8831 /* In obj-c++, seing '[[' might be the either the beginning of
8832 c++11 attributes, or a nested objc-message-expression. So
8833 let's parse the c++11 attributes tentatively. */
8834 cp_parser_parse_tentatively (parser);
8835 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8836 if (c_dialect_objc ())
8838 if (!cp_parser_parse_definitely (parser))
8839 std_attrs = NULL_TREE;
8842 /* Peek at the next token. */
8843 token = cp_lexer_peek_token (parser->lexer);
8844 /* Remember the location of the first token in the statement. */
8845 statement_location = token->location;
8846 /* If this is a keyword, then that will often determine what kind of
8847 statement we have. */
8848 if (token->type == CPP_KEYWORD)
8850 enum rid keyword = token->keyword;
8852 switch (keyword)
8854 case RID_CASE:
8855 case RID_DEFAULT:
8856 /* Looks like a labeled-statement with a case label.
8857 Parse the label, and then use tail recursion to parse
8858 the statement. */
8859 cp_parser_label_for_labeled_statement (parser, std_attrs);
8860 goto restart;
8862 case RID_IF:
8863 case RID_SWITCH:
8864 statement = cp_parser_selection_statement (parser, if_p);
8865 break;
8867 case RID_WHILE:
8868 case RID_DO:
8869 case RID_FOR:
8870 statement = cp_parser_iteration_statement (parser);
8871 break;
8873 case RID_BREAK:
8874 case RID_CONTINUE:
8875 case RID_RETURN:
8876 case RID_GOTO:
8877 statement = cp_parser_jump_statement (parser);
8878 break;
8880 /* Objective-C++ exception-handling constructs. */
8881 case RID_AT_TRY:
8882 case RID_AT_CATCH:
8883 case RID_AT_FINALLY:
8884 case RID_AT_SYNCHRONIZED:
8885 case RID_AT_THROW:
8886 statement = cp_parser_objc_statement (parser);
8887 break;
8889 case RID_TRY:
8890 statement = cp_parser_try_block (parser);
8891 break;
8893 case RID_NAMESPACE:
8894 /* This must be a namespace alias definition. */
8895 cp_parser_declaration_statement (parser);
8896 return;
8898 case RID_TRANSACTION_ATOMIC:
8899 case RID_TRANSACTION_RELAXED:
8900 statement = cp_parser_transaction (parser, keyword);
8901 break;
8902 case RID_TRANSACTION_CANCEL:
8903 statement = cp_parser_transaction_cancel (parser);
8904 break;
8906 default:
8907 /* It might be a keyword like `int' that can start a
8908 declaration-statement. */
8909 break;
8912 else if (token->type == CPP_NAME)
8914 /* If the next token is a `:', then we are looking at a
8915 labeled-statement. */
8916 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8917 if (token->type == CPP_COLON)
8919 /* Looks like a labeled-statement with an ordinary label.
8920 Parse the label, and then use tail recursion to parse
8921 the statement. */
8923 cp_parser_label_for_labeled_statement (parser, std_attrs);
8924 goto restart;
8927 /* Anything that starts with a `{' must be a compound-statement. */
8928 else if (token->type == CPP_OPEN_BRACE)
8929 statement = cp_parser_compound_statement (parser, NULL, false, false);
8930 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8931 a statement all its own. */
8932 else if (token->type == CPP_PRAGMA)
8934 /* Only certain OpenMP pragmas are attached to statements, and thus
8935 are considered statements themselves. All others are not. In
8936 the context of a compound, accept the pragma as a "statement" and
8937 return so that we can check for a close brace. Otherwise we
8938 require a real statement and must go back and read one. */
8939 if (in_compound)
8940 cp_parser_pragma (parser, pragma_compound);
8941 else if (!cp_parser_pragma (parser, pragma_stmt))
8942 goto restart;
8943 return;
8945 else if (token->type == CPP_EOF)
8947 cp_parser_error (parser, "expected statement");
8948 return;
8951 /* Everything else must be a declaration-statement or an
8952 expression-statement. Try for the declaration-statement
8953 first, unless we are looking at a `;', in which case we know that
8954 we have an expression-statement. */
8955 if (!statement)
8957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8959 if (std_attrs != NULL_TREE)
8961 /* Attributes should be parsed as part of the the
8962 declaration, so let's un-parse them. */
8963 cp_lexer_rollback_tokens (parser->lexer);
8964 std_attrs = NULL_TREE;
8967 cp_parser_parse_tentatively (parser);
8968 /* Try to parse the declaration-statement. */
8969 cp_parser_declaration_statement (parser);
8970 /* If that worked, we're done. */
8971 if (cp_parser_parse_definitely (parser))
8972 return;
8974 /* Look for an expression-statement instead. */
8975 statement = cp_parser_expression_statement (parser, in_statement_expr);
8978 /* Set the line number for the statement. */
8979 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8980 SET_EXPR_LOCATION (statement, statement_location);
8982 /* Note that for now, we don't do anything with c++11 statements
8983 parsed at this level. */
8984 if (std_attrs != NULL_TREE)
8985 warning_at (attrs_location,
8986 OPT_Wattributes,
8987 "attributes at the beginning of statement are ignored");
8990 /* Parse the label for a labeled-statement, i.e.
8992 identifier :
8993 case constant-expression :
8994 default :
8996 GNU Extension:
8997 case constant-expression ... constant-expression : statement
8999 When a label is parsed without errors, the label is added to the
9000 parse tree by the finish_* functions, so this function doesn't
9001 have to return the label. */
9003 static void
9004 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9006 cp_token *token;
9007 tree label = NULL_TREE;
9008 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9010 /* The next token should be an identifier. */
9011 token = cp_lexer_peek_token (parser->lexer);
9012 if (token->type != CPP_NAME
9013 && token->type != CPP_KEYWORD)
9015 cp_parser_error (parser, "expected labeled-statement");
9016 return;
9019 parser->colon_corrects_to_scope_p = false;
9020 switch (token->keyword)
9022 case RID_CASE:
9024 tree expr, expr_hi;
9025 cp_token *ellipsis;
9027 /* Consume the `case' token. */
9028 cp_lexer_consume_token (parser->lexer);
9029 /* Parse the constant-expression. */
9030 expr = cp_parser_constant_expression (parser,
9031 /*allow_non_constant_p=*/false,
9032 NULL);
9034 ellipsis = cp_lexer_peek_token (parser->lexer);
9035 if (ellipsis->type == CPP_ELLIPSIS)
9037 /* Consume the `...' token. */
9038 cp_lexer_consume_token (parser->lexer);
9039 expr_hi =
9040 cp_parser_constant_expression (parser,
9041 /*allow_non_constant_p=*/false,
9042 NULL);
9043 /* We don't need to emit warnings here, as the common code
9044 will do this for us. */
9046 else
9047 expr_hi = NULL_TREE;
9049 if (parser->in_switch_statement_p)
9050 finish_case_label (token->location, expr, expr_hi);
9051 else
9052 error_at (token->location,
9053 "case label %qE not within a switch statement",
9054 expr);
9056 break;
9058 case RID_DEFAULT:
9059 /* Consume the `default' token. */
9060 cp_lexer_consume_token (parser->lexer);
9062 if (parser->in_switch_statement_p)
9063 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9064 else
9065 error_at (token->location, "case label not within a switch statement");
9066 break;
9068 default:
9069 /* Anything else must be an ordinary label. */
9070 label = finish_label_stmt (cp_parser_identifier (parser));
9071 break;
9074 /* Require the `:' token. */
9075 cp_parser_require (parser, CPP_COLON, RT_COLON);
9077 /* An ordinary label may optionally be followed by attributes.
9078 However, this is only permitted if the attributes are then
9079 followed by a semicolon. This is because, for backward
9080 compatibility, when parsing
9081 lab: __attribute__ ((unused)) int i;
9082 we want the attribute to attach to "i", not "lab". */
9083 if (label != NULL_TREE
9084 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9086 tree attrs;
9087 cp_parser_parse_tentatively (parser);
9088 attrs = cp_parser_gnu_attributes_opt (parser);
9089 if (attrs == NULL_TREE
9090 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9091 cp_parser_abort_tentative_parse (parser);
9092 else if (!cp_parser_parse_definitely (parser))
9094 else
9095 attributes = chainon (attributes, attrs);
9098 if (attributes != NULL_TREE)
9099 cplus_decl_attributes (&label, attributes, 0);
9101 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9104 /* Parse an expression-statement.
9106 expression-statement:
9107 expression [opt] ;
9109 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9110 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9111 indicates whether this expression-statement is part of an
9112 expression statement. */
9114 static tree
9115 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9117 tree statement = NULL_TREE;
9118 cp_token *token = cp_lexer_peek_token (parser->lexer);
9120 /* If the next token is a ';', then there is no expression
9121 statement. */
9122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9123 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9125 /* Give a helpful message for "A<T>::type t;" and the like. */
9126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9127 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9129 if (TREE_CODE (statement) == SCOPE_REF)
9130 error_at (token->location, "need %<typename%> before %qE because "
9131 "%qT is a dependent scope",
9132 statement, TREE_OPERAND (statement, 0));
9133 else if (is_overloaded_fn (statement)
9134 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9136 /* A::A a; */
9137 tree fn = get_first_fn (statement);
9138 error_at (token->location,
9139 "%<%T::%D%> names the constructor, not the type",
9140 DECL_CONTEXT (fn), DECL_NAME (fn));
9144 /* Consume the final `;'. */
9145 cp_parser_consume_semicolon_at_end_of_statement (parser);
9147 if (in_statement_expr
9148 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9149 /* This is the final expression statement of a statement
9150 expression. */
9151 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9152 else if (statement)
9153 statement = finish_expr_stmt (statement);
9154 else
9155 finish_stmt ();
9157 return statement;
9160 /* Parse a compound-statement.
9162 compound-statement:
9163 { statement-seq [opt] }
9165 GNU extension:
9167 compound-statement:
9168 { label-declaration-seq [opt] statement-seq [opt] }
9170 label-declaration-seq:
9171 label-declaration
9172 label-declaration-seq label-declaration
9174 Returns a tree representing the statement. */
9176 static tree
9177 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9178 bool in_try, bool function_body)
9180 tree compound_stmt;
9182 /* Consume the `{'. */
9183 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9184 return error_mark_node;
9185 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9186 && !function_body)
9187 pedwarn (input_location, OPT_Wpedantic,
9188 "compound-statement in constexpr function");
9189 /* Begin the compound-statement. */
9190 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9191 /* If the next keyword is `__label__' we have a label declaration. */
9192 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9193 cp_parser_label_declaration (parser);
9194 /* Parse an (optional) statement-seq. */
9195 cp_parser_statement_seq_opt (parser, in_statement_expr);
9196 /* Finish the compound-statement. */
9197 finish_compound_stmt (compound_stmt);
9198 /* Consume the `}'. */
9199 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9201 return compound_stmt;
9204 /* Parse an (optional) statement-seq.
9206 statement-seq:
9207 statement
9208 statement-seq [opt] statement */
9210 static void
9211 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9213 /* Scan statements until there aren't any more. */
9214 while (true)
9216 cp_token *token = cp_lexer_peek_token (parser->lexer);
9218 /* If we are looking at a `}', then we have run out of
9219 statements; the same is true if we have reached the end
9220 of file, or have stumbled upon a stray '@end'. */
9221 if (token->type == CPP_CLOSE_BRACE
9222 || token->type == CPP_EOF
9223 || token->type == CPP_PRAGMA_EOL
9224 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9225 break;
9227 /* If we are in a compound statement and find 'else' then
9228 something went wrong. */
9229 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9231 if (parser->in_statement & IN_IF_STMT)
9232 break;
9233 else
9235 token = cp_lexer_consume_token (parser->lexer);
9236 error_at (token->location, "%<else%> without a previous %<if%>");
9240 /* Parse the statement. */
9241 cp_parser_statement (parser, in_statement_expr, true, NULL);
9245 /* Parse a selection-statement.
9247 selection-statement:
9248 if ( condition ) statement
9249 if ( condition ) statement else statement
9250 switch ( condition ) statement
9252 Returns the new IF_STMT or SWITCH_STMT.
9254 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9255 is a (possibly labeled) if statement which is not enclosed in
9256 braces and has an else clause. This is used to implement
9257 -Wparentheses. */
9259 static tree
9260 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9262 cp_token *token;
9263 enum rid keyword;
9265 if (if_p != NULL)
9266 *if_p = false;
9268 /* Peek at the next token. */
9269 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9271 /* See what kind of keyword it is. */
9272 keyword = token->keyword;
9273 switch (keyword)
9275 case RID_IF:
9276 case RID_SWITCH:
9278 tree statement;
9279 tree condition;
9281 /* Look for the `('. */
9282 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9284 cp_parser_skip_to_end_of_statement (parser);
9285 return error_mark_node;
9288 /* Begin the selection-statement. */
9289 if (keyword == RID_IF)
9290 statement = begin_if_stmt ();
9291 else
9292 statement = begin_switch_stmt ();
9294 /* Parse the condition. */
9295 condition = cp_parser_condition (parser);
9296 /* Look for the `)'. */
9297 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9298 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9299 /*consume_paren=*/true);
9301 if (keyword == RID_IF)
9303 bool nested_if;
9304 unsigned char in_statement;
9306 /* Add the condition. */
9307 finish_if_stmt_cond (condition, statement);
9309 /* Parse the then-clause. */
9310 in_statement = parser->in_statement;
9311 parser->in_statement |= IN_IF_STMT;
9312 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9314 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9315 add_stmt (build_empty_stmt (loc));
9316 cp_lexer_consume_token (parser->lexer);
9317 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9318 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9319 "empty body in an %<if%> statement");
9320 nested_if = false;
9322 else
9323 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9324 parser->in_statement = in_statement;
9326 finish_then_clause (statement);
9328 /* If the next token is `else', parse the else-clause. */
9329 if (cp_lexer_next_token_is_keyword (parser->lexer,
9330 RID_ELSE))
9332 /* Consume the `else' keyword. */
9333 cp_lexer_consume_token (parser->lexer);
9334 begin_else_clause (statement);
9335 /* Parse the else-clause. */
9336 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9338 location_t loc;
9339 loc = cp_lexer_peek_token (parser->lexer)->location;
9340 warning_at (loc,
9341 OPT_Wempty_body, "suggest braces around "
9342 "empty body in an %<else%> statement");
9343 add_stmt (build_empty_stmt (loc));
9344 cp_lexer_consume_token (parser->lexer);
9346 else
9347 cp_parser_implicitly_scoped_statement (parser, NULL);
9349 finish_else_clause (statement);
9351 /* If we are currently parsing a then-clause, then
9352 IF_P will not be NULL. We set it to true to
9353 indicate that this if statement has an else clause.
9354 This may trigger the Wparentheses warning below
9355 when we get back up to the parent if statement. */
9356 if (if_p != NULL)
9357 *if_p = true;
9359 else
9361 /* This if statement does not have an else clause. If
9362 NESTED_IF is true, then the then-clause is an if
9363 statement which does have an else clause. We warn
9364 about the potential ambiguity. */
9365 if (nested_if)
9366 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9367 "suggest explicit braces to avoid ambiguous"
9368 " %<else%>");
9371 /* Now we're all done with the if-statement. */
9372 finish_if_stmt (statement);
9374 else
9376 bool in_switch_statement_p;
9377 unsigned char in_statement;
9379 /* Add the condition. */
9380 finish_switch_cond (condition, statement);
9382 /* Parse the body of the switch-statement. */
9383 in_switch_statement_p = parser->in_switch_statement_p;
9384 in_statement = parser->in_statement;
9385 parser->in_switch_statement_p = true;
9386 parser->in_statement |= IN_SWITCH_STMT;
9387 cp_parser_implicitly_scoped_statement (parser, NULL);
9388 parser->in_switch_statement_p = in_switch_statement_p;
9389 parser->in_statement = in_statement;
9391 /* Now we're all done with the switch-statement. */
9392 finish_switch_stmt (statement);
9395 return statement;
9397 break;
9399 default:
9400 cp_parser_error (parser, "expected selection-statement");
9401 return error_mark_node;
9405 /* Parse a condition.
9407 condition:
9408 expression
9409 type-specifier-seq declarator = initializer-clause
9410 type-specifier-seq declarator braced-init-list
9412 GNU Extension:
9414 condition:
9415 type-specifier-seq declarator asm-specification [opt]
9416 attributes [opt] = assignment-expression
9418 Returns the expression that should be tested. */
9420 static tree
9421 cp_parser_condition (cp_parser* parser)
9423 cp_decl_specifier_seq type_specifiers;
9424 const char *saved_message;
9425 int declares_class_or_enum;
9427 /* Try the declaration first. */
9428 cp_parser_parse_tentatively (parser);
9429 /* New types are not allowed in the type-specifier-seq for a
9430 condition. */
9431 saved_message = parser->type_definition_forbidden_message;
9432 parser->type_definition_forbidden_message
9433 = G_("types may not be defined in conditions");
9434 /* Parse the type-specifier-seq. */
9435 cp_parser_decl_specifier_seq (parser,
9436 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9437 &type_specifiers,
9438 &declares_class_or_enum);
9439 /* Restore the saved message. */
9440 parser->type_definition_forbidden_message = saved_message;
9441 /* If all is well, we might be looking at a declaration. */
9442 if (!cp_parser_error_occurred (parser))
9444 tree decl;
9445 tree asm_specification;
9446 tree attributes;
9447 cp_declarator *declarator;
9448 tree initializer = NULL_TREE;
9450 /* Parse the declarator. */
9451 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9452 /*ctor_dtor_or_conv_p=*/NULL,
9453 /*parenthesized_p=*/NULL,
9454 /*member_p=*/false);
9455 /* Parse the attributes. */
9456 attributes = cp_parser_attributes_opt (parser);
9457 /* Parse the asm-specification. */
9458 asm_specification = cp_parser_asm_specification_opt (parser);
9459 /* If the next token is not an `=' or '{', then we might still be
9460 looking at an expression. For example:
9462 if (A(a).x)
9464 looks like a decl-specifier-seq and a declarator -- but then
9465 there is no `=', so this is an expression. */
9466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9467 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9468 cp_parser_simulate_error (parser);
9470 /* If we did see an `=' or '{', then we are looking at a declaration
9471 for sure. */
9472 if (cp_parser_parse_definitely (parser))
9474 tree pushed_scope;
9475 bool non_constant_p;
9476 bool flags = LOOKUP_ONLYCONVERTING;
9478 /* Create the declaration. */
9479 decl = start_decl (declarator, &type_specifiers,
9480 /*initialized_p=*/true,
9481 attributes, /*prefix_attributes=*/NULL_TREE,
9482 &pushed_scope);
9484 /* Parse the initializer. */
9485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9487 initializer = cp_parser_braced_list (parser, &non_constant_p);
9488 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9489 flags = 0;
9491 else
9493 /* Consume the `='. */
9494 cp_parser_require (parser, CPP_EQ, RT_EQ);
9495 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9497 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9498 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9500 /* Process the initializer. */
9501 cp_finish_decl (decl,
9502 initializer, !non_constant_p,
9503 asm_specification,
9504 flags);
9506 if (pushed_scope)
9507 pop_scope (pushed_scope);
9509 return convert_from_reference (decl);
9512 /* If we didn't even get past the declarator successfully, we are
9513 definitely not looking at a declaration. */
9514 else
9515 cp_parser_abort_tentative_parse (parser);
9517 /* Otherwise, we are looking at an expression. */
9518 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9521 /* Parses a for-statement or range-for-statement until the closing ')',
9522 not included. */
9524 static tree
9525 cp_parser_for (cp_parser *parser)
9527 tree init, scope, decl;
9528 bool is_range_for;
9530 /* Begin the for-statement. */
9531 scope = begin_for_scope (&init);
9533 /* Parse the initialization. */
9534 is_range_for = cp_parser_for_init_statement (parser, &decl);
9536 if (is_range_for)
9537 return cp_parser_range_for (parser, scope, init, decl);
9538 else
9539 return cp_parser_c_for (parser, scope, init);
9542 static tree
9543 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9545 /* Normal for loop */
9546 tree condition = NULL_TREE;
9547 tree expression = NULL_TREE;
9548 tree stmt;
9550 stmt = begin_for_stmt (scope, init);
9551 /* The for-init-statement has already been parsed in
9552 cp_parser_for_init_statement, so no work is needed here. */
9553 finish_for_init_stmt (stmt);
9555 /* If there's a condition, process it. */
9556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9557 condition = cp_parser_condition (parser);
9558 finish_for_cond (condition, stmt);
9559 /* Look for the `;'. */
9560 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9562 /* If there's an expression, process it. */
9563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9564 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9565 finish_for_expr (expression, stmt);
9567 return stmt;
9570 /* Tries to parse a range-based for-statement:
9572 range-based-for:
9573 decl-specifier-seq declarator : expression
9575 The decl-specifier-seq declarator and the `:' are already parsed by
9576 cp_parser_for_init_statement. If processing_template_decl it returns a
9577 newly created RANGE_FOR_STMT; if not, it is converted to a
9578 regular FOR_STMT. */
9580 static tree
9581 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9583 tree stmt, range_expr;
9585 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9587 bool expr_non_constant_p;
9588 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9590 else
9591 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9593 /* If in template, STMT is converted to a normal for-statement
9594 at instantiation. If not, it is done just ahead. */
9595 if (processing_template_decl)
9597 if (check_for_bare_parameter_packs (range_expr))
9598 range_expr = error_mark_node;
9599 stmt = begin_range_for_stmt (scope, init);
9600 finish_range_for_decl (stmt, range_decl, range_expr);
9601 if (!type_dependent_expression_p (range_expr)
9602 /* do_auto_deduction doesn't mess with template init-lists. */
9603 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9604 do_range_for_auto_deduction (range_decl, range_expr);
9606 else
9608 stmt = begin_for_stmt (scope, init);
9609 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9611 return stmt;
9614 /* Subroutine of cp_convert_range_for: given the initializer expression,
9615 builds up the range temporary. */
9617 static tree
9618 build_range_temp (tree range_expr)
9620 tree range_type, range_temp;
9622 /* Find out the type deduced by the declaration
9623 `auto &&__range = range_expr'. */
9624 range_type = cp_build_reference_type (make_auto (), true);
9625 range_type = do_auto_deduction (range_type, range_expr,
9626 type_uses_auto (range_type));
9628 /* Create the __range variable. */
9629 range_temp = build_decl (input_location, VAR_DECL,
9630 get_identifier ("__for_range"), range_type);
9631 TREE_USED (range_temp) = 1;
9632 DECL_ARTIFICIAL (range_temp) = 1;
9634 return range_temp;
9637 /* Used by cp_parser_range_for in template context: we aren't going to
9638 do a full conversion yet, but we still need to resolve auto in the
9639 type of the for-range-declaration if present. This is basically
9640 a shortcut version of cp_convert_range_for. */
9642 static void
9643 do_range_for_auto_deduction (tree decl, tree range_expr)
9645 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9646 if (auto_node)
9648 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9649 range_temp = convert_from_reference (build_range_temp (range_expr));
9650 iter_type = (cp_parser_perform_range_for_lookup
9651 (range_temp, &begin_dummy, &end_dummy));
9652 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9653 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9654 tf_warning_or_error);
9655 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9656 iter_decl, auto_node);
9660 /* Converts a range-based for-statement into a normal
9661 for-statement, as per the definition.
9663 for (RANGE_DECL : RANGE_EXPR)
9664 BLOCK
9666 should be equivalent to:
9669 auto &&__range = RANGE_EXPR;
9670 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9671 __begin != __end;
9672 ++__begin)
9674 RANGE_DECL = *__begin;
9675 BLOCK
9679 If RANGE_EXPR is an array:
9680 BEGIN_EXPR = __range
9681 END_EXPR = __range + ARRAY_SIZE(__range)
9682 Else if RANGE_EXPR has a member 'begin' or 'end':
9683 BEGIN_EXPR = __range.begin()
9684 END_EXPR = __range.end()
9685 Else:
9686 BEGIN_EXPR = begin(__range)
9687 END_EXPR = end(__range);
9689 If __range has a member 'begin' but not 'end', or vice versa, we must
9690 still use the second alternative (it will surely fail, however).
9691 When calling begin()/end() in the third alternative we must use
9692 argument dependent lookup, but always considering 'std' as an associated
9693 namespace. */
9695 tree
9696 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9698 tree begin, end;
9699 tree iter_type, begin_expr, end_expr;
9700 tree condition, expression;
9702 if (range_decl == error_mark_node || range_expr == error_mark_node)
9703 /* If an error happened previously do nothing or else a lot of
9704 unhelpful errors would be issued. */
9705 begin_expr = end_expr = iter_type = error_mark_node;
9706 else
9708 tree range_temp = build_range_temp (range_expr);
9709 pushdecl (range_temp);
9710 cp_finish_decl (range_temp, range_expr,
9711 /*is_constant_init*/false, NULL_TREE,
9712 LOOKUP_ONLYCONVERTING);
9714 range_temp = convert_from_reference (range_temp);
9715 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9716 &begin_expr, &end_expr);
9719 /* The new for initialization statement. */
9720 begin = build_decl (input_location, VAR_DECL,
9721 get_identifier ("__for_begin"), iter_type);
9722 TREE_USED (begin) = 1;
9723 DECL_ARTIFICIAL (begin) = 1;
9724 pushdecl (begin);
9725 cp_finish_decl (begin, begin_expr,
9726 /*is_constant_init*/false, NULL_TREE,
9727 LOOKUP_ONLYCONVERTING);
9729 end = build_decl (input_location, VAR_DECL,
9730 get_identifier ("__for_end"), iter_type);
9731 TREE_USED (end) = 1;
9732 DECL_ARTIFICIAL (end) = 1;
9733 pushdecl (end);
9734 cp_finish_decl (end, end_expr,
9735 /*is_constant_init*/false, NULL_TREE,
9736 LOOKUP_ONLYCONVERTING);
9738 finish_for_init_stmt (statement);
9740 /* The new for condition. */
9741 condition = build_x_binary_op (input_location, NE_EXPR,
9742 begin, ERROR_MARK,
9743 end, ERROR_MARK,
9744 NULL, tf_warning_or_error);
9745 finish_for_cond (condition, statement);
9747 /* The new increment expression. */
9748 expression = finish_unary_op_expr (input_location,
9749 PREINCREMENT_EXPR, begin,
9750 tf_warning_or_error);
9751 finish_for_expr (expression, statement);
9753 /* The declaration is initialized with *__begin inside the loop body. */
9754 cp_finish_decl (range_decl,
9755 build_x_indirect_ref (input_location, begin, RO_NULL,
9756 tf_warning_or_error),
9757 /*is_constant_init*/false, NULL_TREE,
9758 LOOKUP_ONLYCONVERTING);
9760 return statement;
9763 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9764 We need to solve both at the same time because the method used
9765 depends on the existence of members begin or end.
9766 Returns the type deduced for the iterator expression. */
9768 static tree
9769 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9771 if (error_operand_p (range))
9773 *begin = *end = error_mark_node;
9774 return error_mark_node;
9777 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9779 error ("range-based %<for%> expression of type %qT "
9780 "has incomplete type", TREE_TYPE (range));
9781 *begin = *end = error_mark_node;
9782 return error_mark_node;
9784 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9786 /* If RANGE is an array, we will use pointer arithmetic. */
9787 *begin = range;
9788 *end = build_binary_op (input_location, PLUS_EXPR,
9789 range,
9790 array_type_nelts_top (TREE_TYPE (range)),
9792 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9794 else
9796 /* If it is not an array, we must do a bit of magic. */
9797 tree id_begin, id_end;
9798 tree member_begin, member_end;
9800 *begin = *end = error_mark_node;
9802 id_begin = get_identifier ("begin");
9803 id_end = get_identifier ("end");
9804 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9805 /*protect=*/2, /*want_type=*/false,
9806 tf_warning_or_error);
9807 member_end = lookup_member (TREE_TYPE (range), id_end,
9808 /*protect=*/2, /*want_type=*/false,
9809 tf_warning_or_error);
9811 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9813 /* Use the member functions. */
9814 if (member_begin != NULL_TREE)
9815 *begin = cp_parser_range_for_member_function (range, id_begin);
9816 else
9817 error ("range-based %<for%> expression of type %qT has an "
9818 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9820 if (member_end != NULL_TREE)
9821 *end = cp_parser_range_for_member_function (range, id_end);
9822 else
9823 error ("range-based %<for%> expression of type %qT has a "
9824 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9826 else
9828 /* Use global functions with ADL. */
9829 vec<tree, va_gc> *vec;
9830 vec = make_tree_vector ();
9832 vec_safe_push (vec, range);
9834 member_begin = perform_koenig_lookup (id_begin, vec,
9835 /*include_std=*/true,
9836 tf_warning_or_error);
9837 *begin = finish_call_expr (member_begin, &vec, false, true,
9838 tf_warning_or_error);
9839 member_end = perform_koenig_lookup (id_end, vec,
9840 /*include_std=*/true,
9841 tf_warning_or_error);
9842 *end = finish_call_expr (member_end, &vec, false, true,
9843 tf_warning_or_error);
9845 release_tree_vector (vec);
9848 /* Last common checks. */
9849 if (*begin == error_mark_node || *end == error_mark_node)
9851 /* If one of the expressions is an error do no more checks. */
9852 *begin = *end = error_mark_node;
9853 return error_mark_node;
9855 else
9857 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9858 /* The unqualified type of the __begin and __end temporaries should
9859 be the same, as required by the multiple auto declaration. */
9860 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9861 error ("inconsistent begin/end types in range-based %<for%> "
9862 "statement: %qT and %qT",
9863 TREE_TYPE (*begin), TREE_TYPE (*end));
9864 return iter_type;
9869 /* Helper function for cp_parser_perform_range_for_lookup.
9870 Builds a tree for RANGE.IDENTIFIER(). */
9872 static tree
9873 cp_parser_range_for_member_function (tree range, tree identifier)
9875 tree member, res;
9876 vec<tree, va_gc> *vec;
9878 member = finish_class_member_access_expr (range, identifier,
9879 false, tf_warning_or_error);
9880 if (member == error_mark_node)
9881 return error_mark_node;
9883 vec = make_tree_vector ();
9884 res = finish_call_expr (member, &vec,
9885 /*disallow_virtual=*/false,
9886 /*koenig_p=*/false,
9887 tf_warning_or_error);
9888 release_tree_vector (vec);
9889 return res;
9892 /* Parse an iteration-statement.
9894 iteration-statement:
9895 while ( condition ) statement
9896 do statement while ( expression ) ;
9897 for ( for-init-statement condition [opt] ; expression [opt] )
9898 statement
9900 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9902 static tree
9903 cp_parser_iteration_statement (cp_parser* parser)
9905 cp_token *token;
9906 enum rid keyword;
9907 tree statement;
9908 unsigned char in_statement;
9910 /* Peek at the next token. */
9911 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9912 if (!token)
9913 return error_mark_node;
9915 /* Remember whether or not we are already within an iteration
9916 statement. */
9917 in_statement = parser->in_statement;
9919 /* See what kind of keyword it is. */
9920 keyword = token->keyword;
9921 switch (keyword)
9923 case RID_WHILE:
9925 tree condition;
9927 /* Begin the while-statement. */
9928 statement = begin_while_stmt ();
9929 /* Look for the `('. */
9930 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9931 /* Parse the condition. */
9932 condition = cp_parser_condition (parser);
9933 finish_while_stmt_cond (condition, statement);
9934 /* Look for the `)'. */
9935 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9936 /* Parse the dependent statement. */
9937 parser->in_statement = IN_ITERATION_STMT;
9938 cp_parser_already_scoped_statement (parser);
9939 parser->in_statement = in_statement;
9940 /* We're done with the while-statement. */
9941 finish_while_stmt (statement);
9943 break;
9945 case RID_DO:
9947 tree expression;
9949 /* Begin the do-statement. */
9950 statement = begin_do_stmt ();
9951 /* Parse the body of the do-statement. */
9952 parser->in_statement = IN_ITERATION_STMT;
9953 cp_parser_implicitly_scoped_statement (parser, NULL);
9954 parser->in_statement = in_statement;
9955 finish_do_body (statement);
9956 /* Look for the `while' keyword. */
9957 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9958 /* Look for the `('. */
9959 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9960 /* Parse the expression. */
9961 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9962 /* We're done with the do-statement. */
9963 finish_do_stmt (expression, statement);
9964 /* Look for the `)'. */
9965 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9966 /* Look for the `;'. */
9967 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9969 break;
9971 case RID_FOR:
9973 /* Look for the `('. */
9974 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9976 statement = cp_parser_for (parser);
9978 /* Look for the `)'. */
9979 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9981 /* Parse the body of the for-statement. */
9982 parser->in_statement = IN_ITERATION_STMT;
9983 cp_parser_already_scoped_statement (parser);
9984 parser->in_statement = in_statement;
9986 /* We're done with the for-statement. */
9987 finish_for_stmt (statement);
9989 break;
9991 default:
9992 cp_parser_error (parser, "expected iteration-statement");
9993 statement = error_mark_node;
9994 break;
9997 return statement;
10000 /* Parse a for-init-statement or the declarator of a range-based-for.
10001 Returns true if a range-based-for declaration is seen.
10003 for-init-statement:
10004 expression-statement
10005 simple-declaration */
10007 static bool
10008 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10010 /* If the next token is a `;', then we have an empty
10011 expression-statement. Grammatically, this is also a
10012 simple-declaration, but an invalid one, because it does not
10013 declare anything. Therefore, if we did not handle this case
10014 specially, we would issue an error message about an invalid
10015 declaration. */
10016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10018 bool is_range_for = false;
10019 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10021 parser->colon_corrects_to_scope_p = false;
10023 /* We're going to speculatively look for a declaration, falling back
10024 to an expression, if necessary. */
10025 cp_parser_parse_tentatively (parser);
10026 /* Parse the declaration. */
10027 cp_parser_simple_declaration (parser,
10028 /*function_definition_allowed_p=*/false,
10029 decl);
10030 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10031 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10033 /* It is a range-for, consume the ':' */
10034 cp_lexer_consume_token (parser->lexer);
10035 is_range_for = true;
10036 if (cxx_dialect < cxx0x)
10038 error_at (cp_lexer_peek_token (parser->lexer)->location,
10039 "range-based %<for%> loops are not allowed "
10040 "in C++98 mode");
10041 *decl = error_mark_node;
10044 else
10045 /* The ';' is not consumed yet because we told
10046 cp_parser_simple_declaration not to. */
10047 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10049 if (cp_parser_parse_definitely (parser))
10050 return is_range_for;
10051 /* If the tentative parse failed, then we shall need to look for an
10052 expression-statement. */
10054 /* If we are here, it is an expression-statement. */
10055 cp_parser_expression_statement (parser, NULL_TREE);
10056 return false;
10059 /* Parse a jump-statement.
10061 jump-statement:
10062 break ;
10063 continue ;
10064 return expression [opt] ;
10065 return braced-init-list ;
10066 goto identifier ;
10068 GNU extension:
10070 jump-statement:
10071 goto * expression ;
10073 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10075 static tree
10076 cp_parser_jump_statement (cp_parser* parser)
10078 tree statement = error_mark_node;
10079 cp_token *token;
10080 enum rid keyword;
10081 unsigned char in_statement;
10083 /* Peek at the next token. */
10084 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10085 if (!token)
10086 return error_mark_node;
10088 /* See what kind of keyword it is. */
10089 keyword = token->keyword;
10090 switch (keyword)
10092 case RID_BREAK:
10093 in_statement = parser->in_statement & ~IN_IF_STMT;
10094 switch (in_statement)
10096 case 0:
10097 error_at (token->location, "break statement not within loop or switch");
10098 break;
10099 default:
10100 gcc_assert ((in_statement & IN_SWITCH_STMT)
10101 || in_statement == IN_ITERATION_STMT);
10102 statement = finish_break_stmt ();
10103 break;
10104 case IN_OMP_BLOCK:
10105 error_at (token->location, "invalid exit from OpenMP structured block");
10106 break;
10107 case IN_OMP_FOR:
10108 error_at (token->location, "break statement used with OpenMP for loop");
10109 break;
10111 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10112 break;
10114 case RID_CONTINUE:
10115 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10117 case 0:
10118 error_at (token->location, "continue statement not within a loop");
10119 break;
10120 case IN_ITERATION_STMT:
10121 case IN_OMP_FOR:
10122 statement = finish_continue_stmt ();
10123 break;
10124 case IN_OMP_BLOCK:
10125 error_at (token->location, "invalid exit from OpenMP structured block");
10126 break;
10127 default:
10128 gcc_unreachable ();
10130 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10131 break;
10133 case RID_RETURN:
10135 tree expr;
10136 bool expr_non_constant_p;
10138 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10140 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10141 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10143 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10144 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10145 else
10146 /* If the next token is a `;', then there is no
10147 expression. */
10148 expr = NULL_TREE;
10149 /* Build the return-statement. */
10150 statement = finish_return_stmt (expr);
10151 /* Look for the final `;'. */
10152 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10154 break;
10156 case RID_GOTO:
10157 /* Create the goto-statement. */
10158 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10160 /* Issue a warning about this use of a GNU extension. */
10161 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10162 /* Consume the '*' token. */
10163 cp_lexer_consume_token (parser->lexer);
10164 /* Parse the dependent expression. */
10165 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10167 else
10168 finish_goto_stmt (cp_parser_identifier (parser));
10169 /* Look for the final `;'. */
10170 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10171 break;
10173 default:
10174 cp_parser_error (parser, "expected jump-statement");
10175 break;
10178 return statement;
10181 /* Parse a declaration-statement.
10183 declaration-statement:
10184 block-declaration */
10186 static void
10187 cp_parser_declaration_statement (cp_parser* parser)
10189 void *p;
10191 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10192 p = obstack_alloc (&declarator_obstack, 0);
10194 /* Parse the block-declaration. */
10195 cp_parser_block_declaration (parser, /*statement_p=*/true);
10197 /* Free any declarators allocated. */
10198 obstack_free (&declarator_obstack, p);
10200 /* Finish off the statement. */
10201 finish_stmt ();
10204 /* Some dependent statements (like `if (cond) statement'), are
10205 implicitly in their own scope. In other words, if the statement is
10206 a single statement (as opposed to a compound-statement), it is
10207 none-the-less treated as if it were enclosed in braces. Any
10208 declarations appearing in the dependent statement are out of scope
10209 after control passes that point. This function parses a statement,
10210 but ensures that is in its own scope, even if it is not a
10211 compound-statement.
10213 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10214 is a (possibly labeled) if statement which is not enclosed in
10215 braces and has an else clause. This is used to implement
10216 -Wparentheses.
10218 Returns the new statement. */
10220 static tree
10221 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10223 tree statement;
10225 if (if_p != NULL)
10226 *if_p = false;
10228 /* Mark if () ; with a special NOP_EXPR. */
10229 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10231 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10232 cp_lexer_consume_token (parser->lexer);
10233 statement = add_stmt (build_empty_stmt (loc));
10235 /* if a compound is opened, we simply parse the statement directly. */
10236 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10237 statement = cp_parser_compound_statement (parser, NULL, false, false);
10238 /* If the token is not a `{', then we must take special action. */
10239 else
10241 /* Create a compound-statement. */
10242 statement = begin_compound_stmt (0);
10243 /* Parse the dependent-statement. */
10244 cp_parser_statement (parser, NULL_TREE, false, if_p);
10245 /* Finish the dummy compound-statement. */
10246 finish_compound_stmt (statement);
10249 /* Return the statement. */
10250 return statement;
10253 /* For some dependent statements (like `while (cond) statement'), we
10254 have already created a scope. Therefore, even if the dependent
10255 statement is a compound-statement, we do not want to create another
10256 scope. */
10258 static void
10259 cp_parser_already_scoped_statement (cp_parser* parser)
10261 /* If the token is a `{', then we must take special action. */
10262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10263 cp_parser_statement (parser, NULL_TREE, false, NULL);
10264 else
10266 /* Avoid calling cp_parser_compound_statement, so that we
10267 don't create a new scope. Do everything else by hand. */
10268 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10269 /* If the next keyword is `__label__' we have a label declaration. */
10270 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10271 cp_parser_label_declaration (parser);
10272 /* Parse an (optional) statement-seq. */
10273 cp_parser_statement_seq_opt (parser, NULL_TREE);
10274 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10278 /* Declarations [gram.dcl.dcl] */
10280 /* Parse an optional declaration-sequence.
10282 declaration-seq:
10283 declaration
10284 declaration-seq declaration */
10286 static void
10287 cp_parser_declaration_seq_opt (cp_parser* parser)
10289 while (true)
10291 cp_token *token;
10293 token = cp_lexer_peek_token (parser->lexer);
10295 if (token->type == CPP_CLOSE_BRACE
10296 || token->type == CPP_EOF
10297 || token->type == CPP_PRAGMA_EOL)
10298 break;
10300 if (token->type == CPP_SEMICOLON)
10302 /* A declaration consisting of a single semicolon is
10303 invalid. Allow it unless we're being pedantic. */
10304 cp_lexer_consume_token (parser->lexer);
10305 if (!in_system_header)
10306 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10307 continue;
10310 /* If we're entering or exiting a region that's implicitly
10311 extern "C", modify the lang context appropriately. */
10312 if (!parser->implicit_extern_c && token->implicit_extern_c)
10314 push_lang_context (lang_name_c);
10315 parser->implicit_extern_c = true;
10317 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10319 pop_lang_context ();
10320 parser->implicit_extern_c = false;
10323 if (token->type == CPP_PRAGMA)
10325 /* A top-level declaration can consist solely of a #pragma.
10326 A nested declaration cannot, so this is done here and not
10327 in cp_parser_declaration. (A #pragma at block scope is
10328 handled in cp_parser_statement.) */
10329 cp_parser_pragma (parser, pragma_external);
10330 continue;
10333 /* Parse the declaration itself. */
10334 cp_parser_declaration (parser);
10338 /* Parse a declaration.
10340 declaration:
10341 block-declaration
10342 function-definition
10343 template-declaration
10344 explicit-instantiation
10345 explicit-specialization
10346 linkage-specification
10347 namespace-definition
10349 GNU extension:
10351 declaration:
10352 __extension__ declaration */
10354 static void
10355 cp_parser_declaration (cp_parser* parser)
10357 cp_token token1;
10358 cp_token token2;
10359 int saved_pedantic;
10360 void *p;
10361 tree attributes = NULL_TREE;
10363 /* Check for the `__extension__' keyword. */
10364 if (cp_parser_extension_opt (parser, &saved_pedantic))
10366 /* Parse the qualified declaration. */
10367 cp_parser_declaration (parser);
10368 /* Restore the PEDANTIC flag. */
10369 pedantic = saved_pedantic;
10371 return;
10374 /* Try to figure out what kind of declaration is present. */
10375 token1 = *cp_lexer_peek_token (parser->lexer);
10377 if (token1.type != CPP_EOF)
10378 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10379 else
10381 token2.type = CPP_EOF;
10382 token2.keyword = RID_MAX;
10385 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10386 p = obstack_alloc (&declarator_obstack, 0);
10388 /* If the next token is `extern' and the following token is a string
10389 literal, then we have a linkage specification. */
10390 if (token1.keyword == RID_EXTERN
10391 && cp_parser_is_pure_string_literal (&token2))
10392 cp_parser_linkage_specification (parser);
10393 /* If the next token is `template', then we have either a template
10394 declaration, an explicit instantiation, or an explicit
10395 specialization. */
10396 else if (token1.keyword == RID_TEMPLATE)
10398 /* `template <>' indicates a template specialization. */
10399 if (token2.type == CPP_LESS
10400 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10401 cp_parser_explicit_specialization (parser);
10402 /* `template <' indicates a template declaration. */
10403 else if (token2.type == CPP_LESS)
10404 cp_parser_template_declaration (parser, /*member_p=*/false);
10405 /* Anything else must be an explicit instantiation. */
10406 else
10407 cp_parser_explicit_instantiation (parser);
10409 /* If the next token is `export', then we have a template
10410 declaration. */
10411 else if (token1.keyword == RID_EXPORT)
10412 cp_parser_template_declaration (parser, /*member_p=*/false);
10413 /* If the next token is `extern', 'static' or 'inline' and the one
10414 after that is `template', we have a GNU extended explicit
10415 instantiation directive. */
10416 else if (cp_parser_allow_gnu_extensions_p (parser)
10417 && (token1.keyword == RID_EXTERN
10418 || token1.keyword == RID_STATIC
10419 || token1.keyword == RID_INLINE)
10420 && token2.keyword == RID_TEMPLATE)
10421 cp_parser_explicit_instantiation (parser);
10422 /* If the next token is `namespace', check for a named or unnamed
10423 namespace definition. */
10424 else if (token1.keyword == RID_NAMESPACE
10425 && (/* A named namespace definition. */
10426 (token2.type == CPP_NAME
10427 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10428 != CPP_EQ))
10429 /* An unnamed namespace definition. */
10430 || token2.type == CPP_OPEN_BRACE
10431 || token2.keyword == RID_ATTRIBUTE))
10432 cp_parser_namespace_definition (parser);
10433 /* An inline (associated) namespace definition. */
10434 else if (token1.keyword == RID_INLINE
10435 && token2.keyword == RID_NAMESPACE)
10436 cp_parser_namespace_definition (parser);
10437 /* Objective-C++ declaration/definition. */
10438 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10439 cp_parser_objc_declaration (parser, NULL_TREE);
10440 else if (c_dialect_objc ()
10441 && token1.keyword == RID_ATTRIBUTE
10442 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10443 cp_parser_objc_declaration (parser, attributes);
10444 /* We must have either a block declaration or a function
10445 definition. */
10446 else
10447 /* Try to parse a block-declaration, or a function-definition. */
10448 cp_parser_block_declaration (parser, /*statement_p=*/false);
10450 /* Free any declarators allocated. */
10451 obstack_free (&declarator_obstack, p);
10454 /* Parse a block-declaration.
10456 block-declaration:
10457 simple-declaration
10458 asm-definition
10459 namespace-alias-definition
10460 using-declaration
10461 using-directive
10463 GNU Extension:
10465 block-declaration:
10466 __extension__ block-declaration
10468 C++0x Extension:
10470 block-declaration:
10471 static_assert-declaration
10473 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10474 part of a declaration-statement. */
10476 static void
10477 cp_parser_block_declaration (cp_parser *parser,
10478 bool statement_p)
10480 cp_token *token1;
10481 int saved_pedantic;
10483 /* Check for the `__extension__' keyword. */
10484 if (cp_parser_extension_opt (parser, &saved_pedantic))
10486 /* Parse the qualified declaration. */
10487 cp_parser_block_declaration (parser, statement_p);
10488 /* Restore the PEDANTIC flag. */
10489 pedantic = saved_pedantic;
10491 return;
10494 /* Peek at the next token to figure out which kind of declaration is
10495 present. */
10496 token1 = cp_lexer_peek_token (parser->lexer);
10498 /* If the next keyword is `asm', we have an asm-definition. */
10499 if (token1->keyword == RID_ASM)
10501 if (statement_p)
10502 cp_parser_commit_to_tentative_parse (parser);
10503 cp_parser_asm_definition (parser);
10505 /* If the next keyword is `namespace', we have a
10506 namespace-alias-definition. */
10507 else if (token1->keyword == RID_NAMESPACE)
10508 cp_parser_namespace_alias_definition (parser);
10509 /* If the next keyword is `using', we have a
10510 using-declaration, a using-directive, or an alias-declaration. */
10511 else if (token1->keyword == RID_USING)
10513 cp_token *token2;
10515 if (statement_p)
10516 cp_parser_commit_to_tentative_parse (parser);
10517 /* If the token after `using' is `namespace', then we have a
10518 using-directive. */
10519 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10520 if (token2->keyword == RID_NAMESPACE)
10521 cp_parser_using_directive (parser);
10522 /* If the second token after 'using' is '=', then we have an
10523 alias-declaration. */
10524 else if (cxx_dialect >= cxx0x
10525 && token2->type == CPP_NAME
10526 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10527 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10528 cp_parser_alias_declaration (parser);
10529 /* Otherwise, it's a using-declaration. */
10530 else
10531 cp_parser_using_declaration (parser,
10532 /*access_declaration_p=*/false);
10534 /* If the next keyword is `__label__' we have a misplaced label
10535 declaration. */
10536 else if (token1->keyword == RID_LABEL)
10538 cp_lexer_consume_token (parser->lexer);
10539 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10540 cp_parser_skip_to_end_of_statement (parser);
10541 /* If the next token is now a `;', consume it. */
10542 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10543 cp_lexer_consume_token (parser->lexer);
10545 /* If the next token is `static_assert' we have a static assertion. */
10546 else if (token1->keyword == RID_STATIC_ASSERT)
10547 cp_parser_static_assert (parser, /*member_p=*/false);
10548 /* Anything else must be a simple-declaration. */
10549 else
10550 cp_parser_simple_declaration (parser, !statement_p,
10551 /*maybe_range_for_decl*/NULL);
10554 /* Parse a simple-declaration.
10556 simple-declaration:
10557 decl-specifier-seq [opt] init-declarator-list [opt] ;
10559 init-declarator-list:
10560 init-declarator
10561 init-declarator-list , init-declarator
10563 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10564 function-definition as a simple-declaration.
10566 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10567 parsed declaration if it is an uninitialized single declarator not followed
10568 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10569 if present, will not be consumed. */
10571 static void
10572 cp_parser_simple_declaration (cp_parser* parser,
10573 bool function_definition_allowed_p,
10574 tree *maybe_range_for_decl)
10576 cp_decl_specifier_seq decl_specifiers;
10577 int declares_class_or_enum;
10578 bool saw_declarator;
10580 if (maybe_range_for_decl)
10581 *maybe_range_for_decl = NULL_TREE;
10583 /* Defer access checks until we know what is being declared; the
10584 checks for names appearing in the decl-specifier-seq should be
10585 done as if we were in the scope of the thing being declared. */
10586 push_deferring_access_checks (dk_deferred);
10588 /* Parse the decl-specifier-seq. We have to keep track of whether
10589 or not the decl-specifier-seq declares a named class or
10590 enumeration type, since that is the only case in which the
10591 init-declarator-list is allowed to be empty.
10593 [dcl.dcl]
10595 In a simple-declaration, the optional init-declarator-list can be
10596 omitted only when declaring a class or enumeration, that is when
10597 the decl-specifier-seq contains either a class-specifier, an
10598 elaborated-type-specifier, or an enum-specifier. */
10599 cp_parser_decl_specifier_seq (parser,
10600 CP_PARSER_FLAGS_OPTIONAL,
10601 &decl_specifiers,
10602 &declares_class_or_enum);
10603 /* We no longer need to defer access checks. */
10604 stop_deferring_access_checks ();
10606 /* In a block scope, a valid declaration must always have a
10607 decl-specifier-seq. By not trying to parse declarators, we can
10608 resolve the declaration/expression ambiguity more quickly. */
10609 if (!function_definition_allowed_p
10610 && !decl_specifiers.any_specifiers_p)
10612 cp_parser_error (parser, "expected declaration");
10613 goto done;
10616 /* If the next two tokens are both identifiers, the code is
10617 erroneous. The usual cause of this situation is code like:
10619 T t;
10621 where "T" should name a type -- but does not. */
10622 if (!decl_specifiers.any_type_specifiers_p
10623 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10625 /* If parsing tentatively, we should commit; we really are
10626 looking at a declaration. */
10627 cp_parser_commit_to_tentative_parse (parser);
10628 /* Give up. */
10629 goto done;
10632 /* If we have seen at least one decl-specifier, and the next token
10633 is not a parenthesis, then we must be looking at a declaration.
10634 (After "int (" we might be looking at a functional cast.) */
10635 if (decl_specifiers.any_specifiers_p
10636 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10637 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10638 && !cp_parser_error_occurred (parser))
10639 cp_parser_commit_to_tentative_parse (parser);
10641 /* Keep going until we hit the `;' at the end of the simple
10642 declaration. */
10643 saw_declarator = false;
10644 while (cp_lexer_next_token_is_not (parser->lexer,
10645 CPP_SEMICOLON))
10647 cp_token *token;
10648 bool function_definition_p;
10649 tree decl;
10651 if (saw_declarator)
10653 /* If we are processing next declarator, coma is expected */
10654 token = cp_lexer_peek_token (parser->lexer);
10655 gcc_assert (token->type == CPP_COMMA);
10656 cp_lexer_consume_token (parser->lexer);
10657 if (maybe_range_for_decl)
10658 *maybe_range_for_decl = error_mark_node;
10660 else
10661 saw_declarator = true;
10663 /* Parse the init-declarator. */
10664 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10665 /*checks=*/NULL,
10666 function_definition_allowed_p,
10667 /*member_p=*/false,
10668 declares_class_or_enum,
10669 &function_definition_p,
10670 maybe_range_for_decl);
10671 /* If an error occurred while parsing tentatively, exit quickly.
10672 (That usually happens when in the body of a function; each
10673 statement is treated as a declaration-statement until proven
10674 otherwise.) */
10675 if (cp_parser_error_occurred (parser))
10676 goto done;
10677 /* Handle function definitions specially. */
10678 if (function_definition_p)
10680 /* If the next token is a `,', then we are probably
10681 processing something like:
10683 void f() {}, *p;
10685 which is erroneous. */
10686 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10688 cp_token *token = cp_lexer_peek_token (parser->lexer);
10689 error_at (token->location,
10690 "mixing"
10691 " declarations and function-definitions is forbidden");
10693 /* Otherwise, we're done with the list of declarators. */
10694 else
10696 pop_deferring_access_checks ();
10697 return;
10700 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10701 *maybe_range_for_decl = decl;
10702 /* The next token should be either a `,' or a `;'. */
10703 token = cp_lexer_peek_token (parser->lexer);
10704 /* If it's a `,', there are more declarators to come. */
10705 if (token->type == CPP_COMMA)
10706 /* will be consumed next time around */;
10707 /* If it's a `;', we are done. */
10708 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10709 break;
10710 /* Anything else is an error. */
10711 else
10713 /* If we have already issued an error message we don't need
10714 to issue another one. */
10715 if (decl != error_mark_node
10716 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10717 cp_parser_error (parser, "expected %<,%> or %<;%>");
10718 /* Skip tokens until we reach the end of the statement. */
10719 cp_parser_skip_to_end_of_statement (parser);
10720 /* If the next token is now a `;', consume it. */
10721 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10722 cp_lexer_consume_token (parser->lexer);
10723 goto done;
10725 /* After the first time around, a function-definition is not
10726 allowed -- even if it was OK at first. For example:
10728 int i, f() {}
10730 is not valid. */
10731 function_definition_allowed_p = false;
10734 /* Issue an error message if no declarators are present, and the
10735 decl-specifier-seq does not itself declare a class or
10736 enumeration. */
10737 if (!saw_declarator)
10739 if (cp_parser_declares_only_class_p (parser))
10740 shadow_tag (&decl_specifiers);
10741 /* Perform any deferred access checks. */
10742 perform_deferred_access_checks (tf_warning_or_error);
10745 /* Consume the `;'. */
10746 if (!maybe_range_for_decl)
10747 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10749 done:
10750 pop_deferring_access_checks ();
10753 /* Parse a decl-specifier-seq.
10755 decl-specifier-seq:
10756 decl-specifier-seq [opt] decl-specifier
10757 decl-specifier attribute-specifier-seq [opt] (C++11)
10759 decl-specifier:
10760 storage-class-specifier
10761 type-specifier
10762 function-specifier
10763 friend
10764 typedef
10766 GNU Extension:
10768 decl-specifier:
10769 attributes
10771 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10773 The parser flags FLAGS is used to control type-specifier parsing.
10775 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10776 flags:
10778 1: one of the decl-specifiers is an elaborated-type-specifier
10779 (i.e., a type declaration)
10780 2: one of the decl-specifiers is an enum-specifier or a
10781 class-specifier (i.e., a type definition)
10785 static void
10786 cp_parser_decl_specifier_seq (cp_parser* parser,
10787 cp_parser_flags flags,
10788 cp_decl_specifier_seq *decl_specs,
10789 int* declares_class_or_enum)
10791 bool constructor_possible_p = !parser->in_declarator_p;
10792 bool found_decl_spec = false;
10793 cp_token *start_token = NULL;
10794 cp_decl_spec ds;
10796 /* Clear DECL_SPECS. */
10797 clear_decl_specs (decl_specs);
10799 /* Assume no class or enumeration type is declared. */
10800 *declares_class_or_enum = 0;
10802 /* Keep reading specifiers until there are no more to read. */
10803 while (true)
10805 bool constructor_p;
10806 cp_token *token;
10807 ds = ds_last;
10809 /* Peek at the next token. */
10810 token = cp_lexer_peek_token (parser->lexer);
10812 /* Save the first token of the decl spec list for error
10813 reporting. */
10814 if (!start_token)
10815 start_token = token;
10816 /* Handle attributes. */
10817 if (cp_next_tokens_can_be_attribute_p (parser))
10819 /* Parse the attributes. */
10820 tree attrs = cp_parser_attributes_opt (parser);
10822 /* In a sequence of declaration specifiers, c++11 attributes
10823 appertain to the type that precede them. In that case
10824 [dcl.spec]/1 says:
10826 The attribute-specifier-seq affects the type only for
10827 the declaration it appears in, not other declarations
10828 involving the same type.
10830 But for now let's force the user to position the
10831 attribute either at the beginning of the declaration or
10832 after the declarator-id, which would clearly mean that it
10833 applies to the declarator. */
10834 if (cxx11_attribute_p (attrs))
10836 if (!found_decl_spec)
10837 /* The c++11 attribute is at the beginning of the
10838 declaration. It appertains to the entity being
10839 declared. */;
10840 else
10842 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10844 /* This is an attribute following a
10845 class-specifier. */
10846 if (decl_specs->type_definition_p)
10847 warn_misplaced_attr_for_class_type (token->location,
10848 decl_specs->type);
10849 attrs = NULL_TREE;
10851 else
10853 decl_specs->std_attributes
10854 = chainon (decl_specs->std_attributes,
10855 attrs);
10856 if (decl_specs->locations[ds_std_attribute] == 0)
10857 decl_specs->locations[ds_std_attribute] = token->location;
10859 continue;
10863 decl_specs->attributes
10864 = chainon (decl_specs->attributes,
10865 attrs);
10866 if (decl_specs->locations[ds_attribute] == 0)
10867 decl_specs->locations[ds_attribute] = token->location;
10868 continue;
10870 /* Assume we will find a decl-specifier keyword. */
10871 found_decl_spec = true;
10872 /* If the next token is an appropriate keyword, we can simply
10873 add it to the list. */
10874 switch (token->keyword)
10876 /* decl-specifier:
10877 friend
10878 constexpr */
10879 case RID_FRIEND:
10880 if (!at_class_scope_p ())
10882 error_at (token->location, "%<friend%> used outside of class");
10883 cp_lexer_purge_token (parser->lexer);
10885 else
10887 ds = ds_friend;
10888 /* Consume the token. */
10889 cp_lexer_consume_token (parser->lexer);
10891 break;
10893 case RID_CONSTEXPR:
10894 ds = ds_constexpr;
10895 cp_lexer_consume_token (parser->lexer);
10896 break;
10898 /* function-specifier:
10899 inline
10900 virtual
10901 explicit */
10902 case RID_INLINE:
10903 case RID_VIRTUAL:
10904 case RID_EXPLICIT:
10905 cp_parser_function_specifier_opt (parser, decl_specs);
10906 break;
10908 /* decl-specifier:
10909 typedef */
10910 case RID_TYPEDEF:
10911 ds = ds_typedef;
10912 /* Consume the token. */
10913 cp_lexer_consume_token (parser->lexer);
10914 /* A constructor declarator cannot appear in a typedef. */
10915 constructor_possible_p = false;
10916 /* The "typedef" keyword can only occur in a declaration; we
10917 may as well commit at this point. */
10918 cp_parser_commit_to_tentative_parse (parser);
10920 if (decl_specs->storage_class != sc_none)
10921 decl_specs->conflicting_specifiers_p = true;
10922 break;
10924 /* storage-class-specifier:
10925 auto
10926 register
10927 static
10928 extern
10929 mutable
10931 GNU Extension:
10932 thread */
10933 case RID_AUTO:
10934 if (cxx_dialect == cxx98)
10936 /* Consume the token. */
10937 cp_lexer_consume_token (parser->lexer);
10939 /* Complain about `auto' as a storage specifier, if
10940 we're complaining about C++0x compatibility. */
10941 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10942 " changes meaning in C++11; please remove it");
10944 /* Set the storage class anyway. */
10945 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10946 token);
10948 else
10949 /* C++0x auto type-specifier. */
10950 found_decl_spec = false;
10951 break;
10953 case RID_REGISTER:
10954 case RID_STATIC:
10955 case RID_EXTERN:
10956 case RID_MUTABLE:
10957 /* Consume the token. */
10958 cp_lexer_consume_token (parser->lexer);
10959 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10960 token);
10961 break;
10962 case RID_THREAD:
10963 /* Consume the token. */
10964 ds = ds_thread;
10965 cp_lexer_consume_token (parser->lexer);
10966 break;
10968 default:
10969 /* We did not yet find a decl-specifier yet. */
10970 found_decl_spec = false;
10971 break;
10974 if (found_decl_spec
10975 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10976 && token->keyword != RID_CONSTEXPR)
10977 error ("decl-specifier invalid in condition");
10979 if (ds != ds_last)
10980 set_and_check_decl_spec_loc (decl_specs, ds, token);
10982 /* Constructors are a special case. The `S' in `S()' is not a
10983 decl-specifier; it is the beginning of the declarator. */
10984 constructor_p
10985 = (!found_decl_spec
10986 && constructor_possible_p
10987 && (cp_parser_constructor_declarator_p
10988 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10990 /* If we don't have a DECL_SPEC yet, then we must be looking at
10991 a type-specifier. */
10992 if (!found_decl_spec && !constructor_p)
10994 int decl_spec_declares_class_or_enum;
10995 bool is_cv_qualifier;
10996 tree type_spec;
10998 type_spec
10999 = cp_parser_type_specifier (parser, flags,
11000 decl_specs,
11001 /*is_declaration=*/true,
11002 &decl_spec_declares_class_or_enum,
11003 &is_cv_qualifier);
11004 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11006 /* If this type-specifier referenced a user-defined type
11007 (a typedef, class-name, etc.), then we can't allow any
11008 more such type-specifiers henceforth.
11010 [dcl.spec]
11012 The longest sequence of decl-specifiers that could
11013 possibly be a type name is taken as the
11014 decl-specifier-seq of a declaration. The sequence shall
11015 be self-consistent as described below.
11017 [dcl.type]
11019 As a general rule, at most one type-specifier is allowed
11020 in the complete decl-specifier-seq of a declaration. The
11021 only exceptions are the following:
11023 -- const or volatile can be combined with any other
11024 type-specifier.
11026 -- signed or unsigned can be combined with char, long,
11027 short, or int.
11029 -- ..
11031 Example:
11033 typedef char* Pc;
11034 void g (const int Pc);
11036 Here, Pc is *not* part of the decl-specifier seq; it's
11037 the declarator. Therefore, once we see a type-specifier
11038 (other than a cv-qualifier), we forbid any additional
11039 user-defined types. We *do* still allow things like `int
11040 int' to be considered a decl-specifier-seq, and issue the
11041 error message later. */
11042 if (type_spec && !is_cv_qualifier)
11043 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11044 /* A constructor declarator cannot follow a type-specifier. */
11045 if (type_spec)
11047 constructor_possible_p = false;
11048 found_decl_spec = true;
11049 if (!is_cv_qualifier)
11050 decl_specs->any_type_specifiers_p = true;
11054 /* If we still do not have a DECL_SPEC, then there are no more
11055 decl-specifiers. */
11056 if (!found_decl_spec)
11057 break;
11059 decl_specs->any_specifiers_p = true;
11060 /* After we see one decl-specifier, further decl-specifiers are
11061 always optional. */
11062 flags |= CP_PARSER_FLAGS_OPTIONAL;
11065 /* Don't allow a friend specifier with a class definition. */
11066 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11067 && (*declares_class_or_enum & 2))
11068 error_at (decl_specs->locations[ds_friend],
11069 "class definition may not be declared a friend");
11072 /* Parse an (optional) storage-class-specifier.
11074 storage-class-specifier:
11075 auto
11076 register
11077 static
11078 extern
11079 mutable
11081 GNU Extension:
11083 storage-class-specifier:
11084 thread
11086 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11088 static tree
11089 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11091 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11093 case RID_AUTO:
11094 if (cxx_dialect != cxx98)
11095 return NULL_TREE;
11096 /* Fall through for C++98. */
11098 case RID_REGISTER:
11099 case RID_STATIC:
11100 case RID_EXTERN:
11101 case RID_MUTABLE:
11102 case RID_THREAD:
11103 /* Consume the token. */
11104 return cp_lexer_consume_token (parser->lexer)->u.value;
11106 default:
11107 return NULL_TREE;
11111 /* Parse an (optional) function-specifier.
11113 function-specifier:
11114 inline
11115 virtual
11116 explicit
11118 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11119 Updates DECL_SPECS, if it is non-NULL. */
11121 static tree
11122 cp_parser_function_specifier_opt (cp_parser* parser,
11123 cp_decl_specifier_seq *decl_specs)
11125 cp_token *token = cp_lexer_peek_token (parser->lexer);
11126 switch (token->keyword)
11128 case RID_INLINE:
11129 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11130 break;
11132 case RID_VIRTUAL:
11133 /* 14.5.2.3 [temp.mem]
11135 A member function template shall not be virtual. */
11136 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11137 error_at (token->location, "templates may not be %<virtual%>");
11138 else
11139 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11140 break;
11142 case RID_EXPLICIT:
11143 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11144 break;
11146 default:
11147 return NULL_TREE;
11150 /* Consume the token. */
11151 return cp_lexer_consume_token (parser->lexer)->u.value;
11154 /* Parse a linkage-specification.
11156 linkage-specification:
11157 extern string-literal { declaration-seq [opt] }
11158 extern string-literal declaration */
11160 static void
11161 cp_parser_linkage_specification (cp_parser* parser)
11163 tree linkage;
11165 /* Look for the `extern' keyword. */
11166 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11168 /* Look for the string-literal. */
11169 linkage = cp_parser_string_literal (parser, false, false);
11171 /* Transform the literal into an identifier. If the literal is a
11172 wide-character string, or contains embedded NULs, then we can't
11173 handle it as the user wants. */
11174 if (strlen (TREE_STRING_POINTER (linkage))
11175 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11177 cp_parser_error (parser, "invalid linkage-specification");
11178 /* Assume C++ linkage. */
11179 linkage = lang_name_cplusplus;
11181 else
11182 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11184 /* We're now using the new linkage. */
11185 push_lang_context (linkage);
11187 /* If the next token is a `{', then we're using the first
11188 production. */
11189 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11191 /* Consume the `{' token. */
11192 cp_lexer_consume_token (parser->lexer);
11193 /* Parse the declarations. */
11194 cp_parser_declaration_seq_opt (parser);
11195 /* Look for the closing `}'. */
11196 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11198 /* Otherwise, there's just one declaration. */
11199 else
11201 bool saved_in_unbraced_linkage_specification_p;
11203 saved_in_unbraced_linkage_specification_p
11204 = parser->in_unbraced_linkage_specification_p;
11205 parser->in_unbraced_linkage_specification_p = true;
11206 cp_parser_declaration (parser);
11207 parser->in_unbraced_linkage_specification_p
11208 = saved_in_unbraced_linkage_specification_p;
11211 /* We're done with the linkage-specification. */
11212 pop_lang_context ();
11215 /* Parse a static_assert-declaration.
11217 static_assert-declaration:
11218 static_assert ( constant-expression , string-literal ) ;
11220 If MEMBER_P, this static_assert is a class member. */
11222 static void
11223 cp_parser_static_assert(cp_parser *parser, bool member_p)
11225 tree condition;
11226 tree message;
11227 cp_token *token;
11228 location_t saved_loc;
11229 bool dummy;
11231 /* Peek at the `static_assert' token so we can keep track of exactly
11232 where the static assertion started. */
11233 token = cp_lexer_peek_token (parser->lexer);
11234 saved_loc = token->location;
11236 /* Look for the `static_assert' keyword. */
11237 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11238 RT_STATIC_ASSERT))
11239 return;
11241 /* We know we are in a static assertion; commit to any tentative
11242 parse. */
11243 if (cp_parser_parsing_tentatively (parser))
11244 cp_parser_commit_to_tentative_parse (parser);
11246 /* Parse the `(' starting the static assertion condition. */
11247 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11249 /* Parse the constant-expression. Allow a non-constant expression
11250 here in order to give better diagnostics in finish_static_assert. */
11251 condition =
11252 cp_parser_constant_expression (parser,
11253 /*allow_non_constant_p=*/true,
11254 /*non_constant_p=*/&dummy);
11256 /* Parse the separating `,'. */
11257 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11259 /* Parse the string-literal message. */
11260 message = cp_parser_string_literal (parser,
11261 /*translate=*/false,
11262 /*wide_ok=*/true);
11264 /* A `)' completes the static assertion. */
11265 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11266 cp_parser_skip_to_closing_parenthesis (parser,
11267 /*recovering=*/true,
11268 /*or_comma=*/false,
11269 /*consume_paren=*/true);
11271 /* A semicolon terminates the declaration. */
11272 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11274 /* Complete the static assertion, which may mean either processing
11275 the static assert now or saving it for template instantiation. */
11276 finish_static_assert (condition, message, saved_loc, member_p);
11279 /* Parse a `decltype' type. Returns the type.
11281 simple-type-specifier:
11282 decltype ( expression ) */
11284 static tree
11285 cp_parser_decltype (cp_parser *parser)
11287 tree expr;
11288 bool id_expression_or_member_access_p = false;
11289 const char *saved_message;
11290 bool saved_integral_constant_expression_p;
11291 bool saved_non_integral_constant_expression_p;
11292 cp_token *id_expr_start_token;
11293 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11295 if (start_token->type == CPP_DECLTYPE)
11297 /* Already parsed. */
11298 cp_lexer_consume_token (parser->lexer);
11299 return start_token->u.value;
11302 /* Look for the `decltype' token. */
11303 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11304 return error_mark_node;
11306 /* Types cannot be defined in a `decltype' expression. Save away the
11307 old message. */
11308 saved_message = parser->type_definition_forbidden_message;
11310 /* And create the new one. */
11311 parser->type_definition_forbidden_message
11312 = G_("types may not be defined in %<decltype%> expressions");
11314 /* The restrictions on constant-expressions do not apply inside
11315 decltype expressions. */
11316 saved_integral_constant_expression_p
11317 = parser->integral_constant_expression_p;
11318 saved_non_integral_constant_expression_p
11319 = parser->non_integral_constant_expression_p;
11320 parser->integral_constant_expression_p = false;
11322 /* Do not actually evaluate the expression. */
11323 ++cp_unevaluated_operand;
11325 /* Do not warn about problems with the expression. */
11326 ++c_inhibit_evaluation_warnings;
11328 /* Parse the opening `('. */
11329 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11330 return error_mark_node;
11332 /* First, try parsing an id-expression. */
11333 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11334 cp_parser_parse_tentatively (parser);
11335 expr = cp_parser_id_expression (parser,
11336 /*template_keyword_p=*/false,
11337 /*check_dependency_p=*/true,
11338 /*template_p=*/NULL,
11339 /*declarator_p=*/false,
11340 /*optional_p=*/false);
11342 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11344 bool non_integral_constant_expression_p = false;
11345 tree id_expression = expr;
11346 cp_id_kind idk;
11347 const char *error_msg;
11349 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11350 /* Lookup the name we got back from the id-expression. */
11351 expr = cp_parser_lookup_name (parser, expr,
11352 none_type,
11353 /*is_template=*/false,
11354 /*is_namespace=*/false,
11355 /*check_dependency=*/true,
11356 /*ambiguous_decls=*/NULL,
11357 id_expr_start_token->location);
11359 if (expr
11360 && expr != error_mark_node
11361 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11362 && TREE_CODE (expr) != TYPE_DECL
11363 && (TREE_CODE (expr) != BIT_NOT_EXPR
11364 || !TYPE_P (TREE_OPERAND (expr, 0)))
11365 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11367 /* Complete lookup of the id-expression. */
11368 expr = (finish_id_expression
11369 (id_expression, expr, parser->scope, &idk,
11370 /*integral_constant_expression_p=*/false,
11371 /*allow_non_integral_constant_expression_p=*/true,
11372 &non_integral_constant_expression_p,
11373 /*template_p=*/false,
11374 /*done=*/true,
11375 /*address_p=*/false,
11376 /*template_arg_p=*/false,
11377 &error_msg,
11378 id_expr_start_token->location));
11380 if (expr == error_mark_node)
11381 /* We found an id-expression, but it was something that we
11382 should not have found. This is an error, not something
11383 we can recover from, so note that we found an
11384 id-expression and we'll recover as gracefully as
11385 possible. */
11386 id_expression_or_member_access_p = true;
11389 if (expr
11390 && expr != error_mark_node
11391 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11392 /* We have an id-expression. */
11393 id_expression_or_member_access_p = true;
11396 if (!id_expression_or_member_access_p)
11398 /* Abort the id-expression parse. */
11399 cp_parser_abort_tentative_parse (parser);
11401 /* Parsing tentatively, again. */
11402 cp_parser_parse_tentatively (parser);
11404 /* Parse a class member access. */
11405 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11406 /*cast_p=*/false, /*decltype*/true,
11407 /*member_access_only_p=*/true, NULL);
11409 if (expr
11410 && expr != error_mark_node
11411 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11412 /* We have an id-expression. */
11413 id_expression_or_member_access_p = true;
11416 if (id_expression_or_member_access_p)
11417 /* We have parsed the complete id-expression or member access. */
11418 cp_parser_parse_definitely (parser);
11419 else
11421 bool saved_greater_than_is_operator_p;
11423 /* Abort our attempt to parse an id-expression or member access
11424 expression. */
11425 cp_parser_abort_tentative_parse (parser);
11427 /* Within a parenthesized expression, a `>' token is always
11428 the greater-than operator. */
11429 saved_greater_than_is_operator_p
11430 = parser->greater_than_is_operator_p;
11431 parser->greater_than_is_operator_p = true;
11433 /* Parse a full expression. */
11434 expr = cp_parser_expression (parser, /*cast_p=*/false,
11435 /*decltype*/true, NULL);
11437 /* The `>' token might be the end of a template-id or
11438 template-parameter-list now. */
11439 parser->greater_than_is_operator_p
11440 = saved_greater_than_is_operator_p;
11443 /* Go back to evaluating expressions. */
11444 --cp_unevaluated_operand;
11445 --c_inhibit_evaluation_warnings;
11447 /* Restore the old message and the integral constant expression
11448 flags. */
11449 parser->type_definition_forbidden_message = saved_message;
11450 parser->integral_constant_expression_p
11451 = saved_integral_constant_expression_p;
11452 parser->non_integral_constant_expression_p
11453 = saved_non_integral_constant_expression_p;
11455 /* Parse to the closing `)'. */
11456 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11458 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11459 /*consume_paren=*/true);
11460 return error_mark_node;
11463 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11464 tf_warning_or_error);
11466 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11467 it again. */
11468 start_token->type = CPP_DECLTYPE;
11469 start_token->u.value = expr;
11470 start_token->keyword = RID_MAX;
11471 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11473 return expr;
11476 /* Special member functions [gram.special] */
11478 /* Parse a conversion-function-id.
11480 conversion-function-id:
11481 operator conversion-type-id
11483 Returns an IDENTIFIER_NODE representing the operator. */
11485 static tree
11486 cp_parser_conversion_function_id (cp_parser* parser)
11488 tree type;
11489 tree saved_scope;
11490 tree saved_qualifying_scope;
11491 tree saved_object_scope;
11492 tree pushed_scope = NULL_TREE;
11494 /* Look for the `operator' token. */
11495 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11496 return error_mark_node;
11497 /* When we parse the conversion-type-id, the current scope will be
11498 reset. However, we need that information in able to look up the
11499 conversion function later, so we save it here. */
11500 saved_scope = parser->scope;
11501 saved_qualifying_scope = parser->qualifying_scope;
11502 saved_object_scope = parser->object_scope;
11503 /* We must enter the scope of the class so that the names of
11504 entities declared within the class are available in the
11505 conversion-type-id. For example, consider:
11507 struct S {
11508 typedef int I;
11509 operator I();
11512 S::operator I() { ... }
11514 In order to see that `I' is a type-name in the definition, we
11515 must be in the scope of `S'. */
11516 if (saved_scope)
11517 pushed_scope = push_scope (saved_scope);
11518 /* Parse the conversion-type-id. */
11519 type = cp_parser_conversion_type_id (parser);
11520 /* Leave the scope of the class, if any. */
11521 if (pushed_scope)
11522 pop_scope (pushed_scope);
11523 /* Restore the saved scope. */
11524 parser->scope = saved_scope;
11525 parser->qualifying_scope = saved_qualifying_scope;
11526 parser->object_scope = saved_object_scope;
11527 /* If the TYPE is invalid, indicate failure. */
11528 if (type == error_mark_node)
11529 return error_mark_node;
11530 return mangle_conv_op_name_for_type (type);
11533 /* Parse a conversion-type-id:
11535 conversion-type-id:
11536 type-specifier-seq conversion-declarator [opt]
11538 Returns the TYPE specified. */
11540 static tree
11541 cp_parser_conversion_type_id (cp_parser* parser)
11543 tree attributes;
11544 cp_decl_specifier_seq type_specifiers;
11545 cp_declarator *declarator;
11546 tree type_specified;
11548 /* Parse the attributes. */
11549 attributes = cp_parser_attributes_opt (parser);
11550 /* Parse the type-specifiers. */
11551 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11552 /*is_trailing_return=*/false,
11553 &type_specifiers);
11554 /* If that didn't work, stop. */
11555 if (type_specifiers.type == error_mark_node)
11556 return error_mark_node;
11557 /* Parse the conversion-declarator. */
11558 declarator = cp_parser_conversion_declarator_opt (parser);
11560 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11561 /*initialized=*/0, &attributes);
11562 if (attributes)
11563 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11565 /* Don't give this error when parsing tentatively. This happens to
11566 work because we always parse this definitively once. */
11567 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11568 && type_uses_auto (type_specified))
11570 if (cxx_dialect < cxx1y)
11572 error ("invalid use of %<auto%> in conversion operator");
11573 return error_mark_node;
11575 else if (template_parm_scope_p ())
11576 warning (0, "use of %<auto%> in member template "
11577 "conversion operator can never be deduced");
11580 return type_specified;
11583 /* Parse an (optional) conversion-declarator.
11585 conversion-declarator:
11586 ptr-operator conversion-declarator [opt]
11590 static cp_declarator *
11591 cp_parser_conversion_declarator_opt (cp_parser* parser)
11593 enum tree_code code;
11594 tree class_type, std_attributes = NULL_TREE;
11595 cp_cv_quals cv_quals;
11597 /* We don't know if there's a ptr-operator next, or not. */
11598 cp_parser_parse_tentatively (parser);
11599 /* Try the ptr-operator. */
11600 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11601 &std_attributes);
11602 /* If it worked, look for more conversion-declarators. */
11603 if (cp_parser_parse_definitely (parser))
11605 cp_declarator *declarator;
11607 /* Parse another optional declarator. */
11608 declarator = cp_parser_conversion_declarator_opt (parser);
11610 declarator = cp_parser_make_indirect_declarator
11611 (code, class_type, cv_quals, declarator, std_attributes);
11613 return declarator;
11616 return NULL;
11619 /* Parse an (optional) ctor-initializer.
11621 ctor-initializer:
11622 : mem-initializer-list
11624 Returns TRUE iff the ctor-initializer was actually present. */
11626 static bool
11627 cp_parser_ctor_initializer_opt (cp_parser* parser)
11629 /* If the next token is not a `:', then there is no
11630 ctor-initializer. */
11631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11633 /* Do default initialization of any bases and members. */
11634 if (DECL_CONSTRUCTOR_P (current_function_decl))
11635 finish_mem_initializers (NULL_TREE);
11637 return false;
11640 /* Consume the `:' token. */
11641 cp_lexer_consume_token (parser->lexer);
11642 /* And the mem-initializer-list. */
11643 cp_parser_mem_initializer_list (parser);
11645 return true;
11648 /* Parse a mem-initializer-list.
11650 mem-initializer-list:
11651 mem-initializer ... [opt]
11652 mem-initializer ... [opt] , mem-initializer-list */
11654 static void
11655 cp_parser_mem_initializer_list (cp_parser* parser)
11657 tree mem_initializer_list = NULL_TREE;
11658 tree target_ctor = error_mark_node;
11659 cp_token *token = cp_lexer_peek_token (parser->lexer);
11661 /* Let the semantic analysis code know that we are starting the
11662 mem-initializer-list. */
11663 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11664 error_at (token->location,
11665 "only constructors take member initializers");
11667 /* Loop through the list. */
11668 while (true)
11670 tree mem_initializer;
11672 token = cp_lexer_peek_token (parser->lexer);
11673 /* Parse the mem-initializer. */
11674 mem_initializer = cp_parser_mem_initializer (parser);
11675 /* If the next token is a `...', we're expanding member initializers. */
11676 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11678 /* Consume the `...'. */
11679 cp_lexer_consume_token (parser->lexer);
11681 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11682 can be expanded but members cannot. */
11683 if (mem_initializer != error_mark_node
11684 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11686 error_at (token->location,
11687 "cannot expand initializer for member %<%D%>",
11688 TREE_PURPOSE (mem_initializer));
11689 mem_initializer = error_mark_node;
11692 /* Construct the pack expansion type. */
11693 if (mem_initializer != error_mark_node)
11694 mem_initializer = make_pack_expansion (mem_initializer);
11696 if (target_ctor != error_mark_node
11697 && mem_initializer != error_mark_node)
11699 error ("mem-initializer for %qD follows constructor delegation",
11700 TREE_PURPOSE (mem_initializer));
11701 mem_initializer = error_mark_node;
11703 /* Look for a target constructor. */
11704 if (mem_initializer != error_mark_node
11705 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
11706 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11708 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11709 if (mem_initializer_list)
11711 error ("constructor delegation follows mem-initializer for %qD",
11712 TREE_PURPOSE (mem_initializer_list));
11713 mem_initializer = error_mark_node;
11715 target_ctor = mem_initializer;
11717 /* Add it to the list, unless it was erroneous. */
11718 if (mem_initializer != error_mark_node)
11720 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11721 mem_initializer_list = mem_initializer;
11723 /* If the next token is not a `,', we're done. */
11724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11725 break;
11726 /* Consume the `,' token. */
11727 cp_lexer_consume_token (parser->lexer);
11730 /* Perform semantic analysis. */
11731 if (DECL_CONSTRUCTOR_P (current_function_decl))
11732 finish_mem_initializers (mem_initializer_list);
11735 /* Parse a mem-initializer.
11737 mem-initializer:
11738 mem-initializer-id ( expression-list [opt] )
11739 mem-initializer-id braced-init-list
11741 GNU extension:
11743 mem-initializer:
11744 ( expression-list [opt] )
11746 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11747 class) or FIELD_DECL (for a non-static data member) to initialize;
11748 the TREE_VALUE is the expression-list. An empty initialization
11749 list is represented by void_list_node. */
11751 static tree
11752 cp_parser_mem_initializer (cp_parser* parser)
11754 tree mem_initializer_id;
11755 tree expression_list;
11756 tree member;
11757 cp_token *token = cp_lexer_peek_token (parser->lexer);
11759 /* Find out what is being initialized. */
11760 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11762 permerror (token->location,
11763 "anachronistic old-style base class initializer");
11764 mem_initializer_id = NULL_TREE;
11766 else
11768 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11769 if (mem_initializer_id == error_mark_node)
11770 return mem_initializer_id;
11772 member = expand_member_init (mem_initializer_id);
11773 if (member && !DECL_P (member))
11774 in_base_initializer = 1;
11776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11778 bool expr_non_constant_p;
11779 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11780 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11781 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11782 expression_list = build_tree_list (NULL_TREE, expression_list);
11784 else
11786 vec<tree, va_gc> *vec;
11787 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11788 /*cast_p=*/false,
11789 /*allow_expansion_p=*/true,
11790 /*non_constant_p=*/NULL);
11791 if (vec == NULL)
11792 return error_mark_node;
11793 expression_list = build_tree_list_vec (vec);
11794 release_tree_vector (vec);
11797 if (expression_list == error_mark_node)
11798 return error_mark_node;
11799 if (!expression_list)
11800 expression_list = void_type_node;
11802 in_base_initializer = 0;
11804 return member ? build_tree_list (member, expression_list) : error_mark_node;
11807 /* Parse a mem-initializer-id.
11809 mem-initializer-id:
11810 :: [opt] nested-name-specifier [opt] class-name
11811 identifier
11813 Returns a TYPE indicating the class to be initializer for the first
11814 production. Returns an IDENTIFIER_NODE indicating the data member
11815 to be initialized for the second production. */
11817 static tree
11818 cp_parser_mem_initializer_id (cp_parser* parser)
11820 bool global_scope_p;
11821 bool nested_name_specifier_p;
11822 bool template_p = false;
11823 tree id;
11825 cp_token *token = cp_lexer_peek_token (parser->lexer);
11827 /* `typename' is not allowed in this context ([temp.res]). */
11828 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11830 error_at (token->location,
11831 "keyword %<typename%> not allowed in this context (a qualified "
11832 "member initializer is implicitly a type)");
11833 cp_lexer_consume_token (parser->lexer);
11835 /* Look for the optional `::' operator. */
11836 global_scope_p
11837 = (cp_parser_global_scope_opt (parser,
11838 /*current_scope_valid_p=*/false)
11839 != NULL_TREE);
11840 /* Look for the optional nested-name-specifier. The simplest way to
11841 implement:
11843 [temp.res]
11845 The keyword `typename' is not permitted in a base-specifier or
11846 mem-initializer; in these contexts a qualified name that
11847 depends on a template-parameter is implicitly assumed to be a
11848 type name.
11850 is to assume that we have seen the `typename' keyword at this
11851 point. */
11852 nested_name_specifier_p
11853 = (cp_parser_nested_name_specifier_opt (parser,
11854 /*typename_keyword_p=*/true,
11855 /*check_dependency_p=*/true,
11856 /*type_p=*/true,
11857 /*is_declaration=*/true)
11858 != NULL_TREE);
11859 if (nested_name_specifier_p)
11860 template_p = cp_parser_optional_template_keyword (parser);
11861 /* If there is a `::' operator or a nested-name-specifier, then we
11862 are definitely looking for a class-name. */
11863 if (global_scope_p || nested_name_specifier_p)
11864 return cp_parser_class_name (parser,
11865 /*typename_keyword_p=*/true,
11866 /*template_keyword_p=*/template_p,
11867 typename_type,
11868 /*check_dependency_p=*/true,
11869 /*class_head_p=*/false,
11870 /*is_declaration=*/true);
11871 /* Otherwise, we could also be looking for an ordinary identifier. */
11872 cp_parser_parse_tentatively (parser);
11873 /* Try a class-name. */
11874 id = cp_parser_class_name (parser,
11875 /*typename_keyword_p=*/true,
11876 /*template_keyword_p=*/false,
11877 none_type,
11878 /*check_dependency_p=*/true,
11879 /*class_head_p=*/false,
11880 /*is_declaration=*/true);
11881 /* If we found one, we're done. */
11882 if (cp_parser_parse_definitely (parser))
11883 return id;
11884 /* Otherwise, look for an ordinary identifier. */
11885 return cp_parser_identifier (parser);
11888 /* Overloading [gram.over] */
11890 /* Parse an operator-function-id.
11892 operator-function-id:
11893 operator operator
11895 Returns an IDENTIFIER_NODE for the operator which is a
11896 human-readable spelling of the identifier, e.g., `operator +'. */
11898 static tree
11899 cp_parser_operator_function_id (cp_parser* parser)
11901 /* Look for the `operator' keyword. */
11902 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11903 return error_mark_node;
11904 /* And then the name of the operator itself. */
11905 return cp_parser_operator (parser);
11908 /* Return an identifier node for a user-defined literal operator.
11909 The suffix identifier is chained to the operator name identifier. */
11911 static tree
11912 cp_literal_operator_id (const char* name)
11914 tree identifier;
11915 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11916 + strlen (name) + 10);
11917 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11918 identifier = get_identifier (buffer);
11919 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11921 return identifier;
11924 /* Parse an operator.
11926 operator:
11927 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11928 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11929 || ++ -- , ->* -> () []
11931 GNU Extensions:
11933 operator:
11934 <? >? <?= >?=
11936 Returns an IDENTIFIER_NODE for the operator which is a
11937 human-readable spelling of the identifier, e.g., `operator +'. */
11939 static tree
11940 cp_parser_operator (cp_parser* parser)
11942 tree id = NULL_TREE;
11943 cp_token *token;
11945 /* Peek at the next token. */
11946 token = cp_lexer_peek_token (parser->lexer);
11947 /* Figure out which operator we have. */
11948 switch (token->type)
11950 case CPP_KEYWORD:
11952 enum tree_code op;
11954 /* The keyword should be either `new' or `delete'. */
11955 if (token->keyword == RID_NEW)
11956 op = NEW_EXPR;
11957 else if (token->keyword == RID_DELETE)
11958 op = DELETE_EXPR;
11959 else
11960 break;
11962 /* Consume the `new' or `delete' token. */
11963 cp_lexer_consume_token (parser->lexer);
11965 /* Peek at the next token. */
11966 token = cp_lexer_peek_token (parser->lexer);
11967 /* If it's a `[' token then this is the array variant of the
11968 operator. */
11969 if (token->type == CPP_OPEN_SQUARE)
11971 /* Consume the `[' token. */
11972 cp_lexer_consume_token (parser->lexer);
11973 /* Look for the `]' token. */
11974 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11975 id = ansi_opname (op == NEW_EXPR
11976 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11978 /* Otherwise, we have the non-array variant. */
11979 else
11980 id = ansi_opname (op);
11982 return id;
11985 case CPP_PLUS:
11986 id = ansi_opname (PLUS_EXPR);
11987 break;
11989 case CPP_MINUS:
11990 id = ansi_opname (MINUS_EXPR);
11991 break;
11993 case CPP_MULT:
11994 id = ansi_opname (MULT_EXPR);
11995 break;
11997 case CPP_DIV:
11998 id = ansi_opname (TRUNC_DIV_EXPR);
11999 break;
12001 case CPP_MOD:
12002 id = ansi_opname (TRUNC_MOD_EXPR);
12003 break;
12005 case CPP_XOR:
12006 id = ansi_opname (BIT_XOR_EXPR);
12007 break;
12009 case CPP_AND:
12010 id = ansi_opname (BIT_AND_EXPR);
12011 break;
12013 case CPP_OR:
12014 id = ansi_opname (BIT_IOR_EXPR);
12015 break;
12017 case CPP_COMPL:
12018 id = ansi_opname (BIT_NOT_EXPR);
12019 break;
12021 case CPP_NOT:
12022 id = ansi_opname (TRUTH_NOT_EXPR);
12023 break;
12025 case CPP_EQ:
12026 id = ansi_assopname (NOP_EXPR);
12027 break;
12029 case CPP_LESS:
12030 id = ansi_opname (LT_EXPR);
12031 break;
12033 case CPP_GREATER:
12034 id = ansi_opname (GT_EXPR);
12035 break;
12037 case CPP_PLUS_EQ:
12038 id = ansi_assopname (PLUS_EXPR);
12039 break;
12041 case CPP_MINUS_EQ:
12042 id = ansi_assopname (MINUS_EXPR);
12043 break;
12045 case CPP_MULT_EQ:
12046 id = ansi_assopname (MULT_EXPR);
12047 break;
12049 case CPP_DIV_EQ:
12050 id = ansi_assopname (TRUNC_DIV_EXPR);
12051 break;
12053 case CPP_MOD_EQ:
12054 id = ansi_assopname (TRUNC_MOD_EXPR);
12055 break;
12057 case CPP_XOR_EQ:
12058 id = ansi_assopname (BIT_XOR_EXPR);
12059 break;
12061 case CPP_AND_EQ:
12062 id = ansi_assopname (BIT_AND_EXPR);
12063 break;
12065 case CPP_OR_EQ:
12066 id = ansi_assopname (BIT_IOR_EXPR);
12067 break;
12069 case CPP_LSHIFT:
12070 id = ansi_opname (LSHIFT_EXPR);
12071 break;
12073 case CPP_RSHIFT:
12074 id = ansi_opname (RSHIFT_EXPR);
12075 break;
12077 case CPP_LSHIFT_EQ:
12078 id = ansi_assopname (LSHIFT_EXPR);
12079 break;
12081 case CPP_RSHIFT_EQ:
12082 id = ansi_assopname (RSHIFT_EXPR);
12083 break;
12085 case CPP_EQ_EQ:
12086 id = ansi_opname (EQ_EXPR);
12087 break;
12089 case CPP_NOT_EQ:
12090 id = ansi_opname (NE_EXPR);
12091 break;
12093 case CPP_LESS_EQ:
12094 id = ansi_opname (LE_EXPR);
12095 break;
12097 case CPP_GREATER_EQ:
12098 id = ansi_opname (GE_EXPR);
12099 break;
12101 case CPP_AND_AND:
12102 id = ansi_opname (TRUTH_ANDIF_EXPR);
12103 break;
12105 case CPP_OR_OR:
12106 id = ansi_opname (TRUTH_ORIF_EXPR);
12107 break;
12109 case CPP_PLUS_PLUS:
12110 id = ansi_opname (POSTINCREMENT_EXPR);
12111 break;
12113 case CPP_MINUS_MINUS:
12114 id = ansi_opname (PREDECREMENT_EXPR);
12115 break;
12117 case CPP_COMMA:
12118 id = ansi_opname (COMPOUND_EXPR);
12119 break;
12121 case CPP_DEREF_STAR:
12122 id = ansi_opname (MEMBER_REF);
12123 break;
12125 case CPP_DEREF:
12126 id = ansi_opname (COMPONENT_REF);
12127 break;
12129 case CPP_OPEN_PAREN:
12130 /* Consume the `('. */
12131 cp_lexer_consume_token (parser->lexer);
12132 /* Look for the matching `)'. */
12133 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12134 return ansi_opname (CALL_EXPR);
12136 case CPP_OPEN_SQUARE:
12137 /* Consume the `['. */
12138 cp_lexer_consume_token (parser->lexer);
12139 /* Look for the matching `]'. */
12140 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12141 return ansi_opname (ARRAY_REF);
12143 case CPP_STRING:
12144 if (cxx_dialect == cxx98)
12145 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12146 if (TREE_STRING_LENGTH (token->u.value) > 2)
12148 error ("expected empty string after %<operator%> keyword");
12149 return error_mark_node;
12151 /* Consume the string. */
12152 cp_lexer_consume_token (parser->lexer);
12153 /* Look for the suffix identifier. */
12154 token = cp_lexer_peek_token (parser->lexer);
12155 if (token->type == CPP_NAME)
12157 id = cp_parser_identifier (parser);
12158 if (id != error_mark_node)
12160 const char *name = IDENTIFIER_POINTER (id);
12161 return cp_literal_operator_id (name);
12164 else
12166 error ("expected suffix identifier");
12167 return error_mark_node;
12170 case CPP_STRING_USERDEF:
12171 error ("missing space between %<\"\"%> and suffix identifier");
12172 return error_mark_node;
12174 default:
12175 /* Anything else is an error. */
12176 break;
12179 /* If we have selected an identifier, we need to consume the
12180 operator token. */
12181 if (id)
12182 cp_lexer_consume_token (parser->lexer);
12183 /* Otherwise, no valid operator name was present. */
12184 else
12186 cp_parser_error (parser, "expected operator");
12187 id = error_mark_node;
12190 return id;
12193 /* Parse a template-declaration.
12195 template-declaration:
12196 export [opt] template < template-parameter-list > declaration
12198 If MEMBER_P is TRUE, this template-declaration occurs within a
12199 class-specifier.
12201 The grammar rule given by the standard isn't correct. What
12202 is really meant is:
12204 template-declaration:
12205 export [opt] template-parameter-list-seq
12206 decl-specifier-seq [opt] init-declarator [opt] ;
12207 export [opt] template-parameter-list-seq
12208 function-definition
12210 template-parameter-list-seq:
12211 template-parameter-list-seq [opt]
12212 template < template-parameter-list > */
12214 static void
12215 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12217 /* Check for `export'. */
12218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12220 /* Consume the `export' token. */
12221 cp_lexer_consume_token (parser->lexer);
12222 /* Warn that we do not support `export'. */
12223 warning (0, "keyword %<export%> not implemented, and will be ignored");
12226 cp_parser_template_declaration_after_export (parser, member_p);
12229 /* Parse a template-parameter-list.
12231 template-parameter-list:
12232 template-parameter
12233 template-parameter-list , template-parameter
12235 Returns a TREE_LIST. Each node represents a template parameter.
12236 The nodes are connected via their TREE_CHAINs. */
12238 static tree
12239 cp_parser_template_parameter_list (cp_parser* parser)
12241 tree parameter_list = NULL_TREE;
12243 begin_template_parm_list ();
12245 /* The loop below parses the template parms. We first need to know
12246 the total number of template parms to be able to compute proper
12247 canonical types of each dependent type. So after the loop, when
12248 we know the total number of template parms,
12249 end_template_parm_list computes the proper canonical types and
12250 fixes up the dependent types accordingly. */
12251 while (true)
12253 tree parameter;
12254 bool is_non_type;
12255 bool is_parameter_pack;
12256 location_t parm_loc;
12258 /* Parse the template-parameter. */
12259 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12260 parameter = cp_parser_template_parameter (parser,
12261 &is_non_type,
12262 &is_parameter_pack);
12263 /* Add it to the list. */
12264 if (parameter != error_mark_node)
12265 parameter_list = process_template_parm (parameter_list,
12266 parm_loc,
12267 parameter,
12268 is_non_type,
12269 is_parameter_pack);
12270 else
12272 tree err_parm = build_tree_list (parameter, parameter);
12273 parameter_list = chainon (parameter_list, err_parm);
12276 /* If the next token is not a `,', we're done. */
12277 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12278 break;
12279 /* Otherwise, consume the `,' token. */
12280 cp_lexer_consume_token (parser->lexer);
12283 return end_template_parm_list (parameter_list);
12286 /* Parse a template-parameter.
12288 template-parameter:
12289 type-parameter
12290 parameter-declaration
12292 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12293 the parameter. The TREE_PURPOSE is the default value, if any.
12294 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12295 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12296 set to true iff this parameter is a parameter pack. */
12298 static tree
12299 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12300 bool *is_parameter_pack)
12302 cp_token *token;
12303 cp_parameter_declarator *parameter_declarator;
12304 cp_declarator *id_declarator;
12305 tree parm;
12307 /* Assume it is a type parameter or a template parameter. */
12308 *is_non_type = false;
12309 /* Assume it not a parameter pack. */
12310 *is_parameter_pack = false;
12311 /* Peek at the next token. */
12312 token = cp_lexer_peek_token (parser->lexer);
12313 /* If it is `class' or `template', we have a type-parameter. */
12314 if (token->keyword == RID_TEMPLATE)
12315 return cp_parser_type_parameter (parser, is_parameter_pack);
12316 /* If it is `class' or `typename' we do not know yet whether it is a
12317 type parameter or a non-type parameter. Consider:
12319 template <typename T, typename T::X X> ...
12323 template <class C, class D*> ...
12325 Here, the first parameter is a type parameter, and the second is
12326 a non-type parameter. We can tell by looking at the token after
12327 the identifier -- if it is a `,', `=', or `>' then we have a type
12328 parameter. */
12329 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12331 /* Peek at the token after `class' or `typename'. */
12332 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12333 /* If it's an ellipsis, we have a template type parameter
12334 pack. */
12335 if (token->type == CPP_ELLIPSIS)
12336 return cp_parser_type_parameter (parser, is_parameter_pack);
12337 /* If it's an identifier, skip it. */
12338 if (token->type == CPP_NAME)
12339 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12340 /* Now, see if the token looks like the end of a template
12341 parameter. */
12342 if (token->type == CPP_COMMA
12343 || token->type == CPP_EQ
12344 || token->type == CPP_GREATER)
12345 return cp_parser_type_parameter (parser, is_parameter_pack);
12348 /* Otherwise, it is a non-type parameter.
12350 [temp.param]
12352 When parsing a default template-argument for a non-type
12353 template-parameter, the first non-nested `>' is taken as the end
12354 of the template parameter-list rather than a greater-than
12355 operator. */
12356 *is_non_type = true;
12357 parameter_declarator
12358 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12359 /*parenthesized_p=*/NULL);
12361 /* If the parameter declaration is marked as a parameter pack, set
12362 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12363 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12364 grokdeclarator. */
12365 if (parameter_declarator
12366 && parameter_declarator->declarator
12367 && parameter_declarator->declarator->parameter_pack_p)
12369 *is_parameter_pack = true;
12370 parameter_declarator->declarator->parameter_pack_p = false;
12373 if (parameter_declarator
12374 && parameter_declarator->default_argument)
12376 /* Can happen in some cases of erroneous input (c++/34892). */
12377 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12378 /* Consume the `...' for better error recovery. */
12379 cp_lexer_consume_token (parser->lexer);
12381 /* If the next token is an ellipsis, and we don't already have it
12382 marked as a parameter pack, then we have a parameter pack (that
12383 has no declarator). */
12384 else if (!*is_parameter_pack
12385 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12386 && (declarator_can_be_parameter_pack
12387 (parameter_declarator->declarator)))
12389 /* Consume the `...'. */
12390 cp_lexer_consume_token (parser->lexer);
12391 maybe_warn_variadic_templates ();
12393 *is_parameter_pack = true;
12395 /* We might end up with a pack expansion as the type of the non-type
12396 template parameter, in which case this is a non-type template
12397 parameter pack. */
12398 else if (parameter_declarator
12399 && parameter_declarator->decl_specifiers.type
12400 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12402 *is_parameter_pack = true;
12403 parameter_declarator->decl_specifiers.type =
12404 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12407 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12409 /* Parameter packs cannot have default arguments. However, a
12410 user may try to do so, so we'll parse them and give an
12411 appropriate diagnostic here. */
12413 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12415 /* Find the name of the parameter pack. */
12416 id_declarator = parameter_declarator->declarator;
12417 while (id_declarator && id_declarator->kind != cdk_id)
12418 id_declarator = id_declarator->declarator;
12420 if (id_declarator && id_declarator->kind == cdk_id)
12421 error_at (start_token->location,
12422 "template parameter pack %qD cannot have a default argument",
12423 id_declarator->u.id.unqualified_name);
12424 else
12425 error_at (start_token->location,
12426 "template parameter pack cannot have a default argument");
12428 /* Parse the default argument, but throw away the result. */
12429 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12432 parm = grokdeclarator (parameter_declarator->declarator,
12433 &parameter_declarator->decl_specifiers,
12434 TPARM, /*initialized=*/0,
12435 /*attrlist=*/NULL);
12436 if (parm == error_mark_node)
12437 return error_mark_node;
12439 return build_tree_list (parameter_declarator->default_argument, parm);
12442 /* Parse a type-parameter.
12444 type-parameter:
12445 class identifier [opt]
12446 class identifier [opt] = type-id
12447 typename identifier [opt]
12448 typename identifier [opt] = type-id
12449 template < template-parameter-list > class identifier [opt]
12450 template < template-parameter-list > class identifier [opt]
12451 = id-expression
12453 GNU Extension (variadic templates):
12455 type-parameter:
12456 class ... identifier [opt]
12457 typename ... identifier [opt]
12459 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12460 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12461 the declaration of the parameter.
12463 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12465 static tree
12466 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12468 cp_token *token;
12469 tree parameter;
12471 /* Look for a keyword to tell us what kind of parameter this is. */
12472 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12473 if (!token)
12474 return error_mark_node;
12476 switch (token->keyword)
12478 case RID_CLASS:
12479 case RID_TYPENAME:
12481 tree identifier;
12482 tree default_argument;
12484 /* If the next token is an ellipsis, we have a template
12485 argument pack. */
12486 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12488 /* Consume the `...' token. */
12489 cp_lexer_consume_token (parser->lexer);
12490 maybe_warn_variadic_templates ();
12492 *is_parameter_pack = true;
12495 /* If the next token is an identifier, then it names the
12496 parameter. */
12497 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12498 identifier = cp_parser_identifier (parser);
12499 else
12500 identifier = NULL_TREE;
12502 /* Create the parameter. */
12503 parameter = finish_template_type_parm (class_type_node, identifier);
12505 /* If the next token is an `=', we have a default argument. */
12506 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12508 /* Consume the `=' token. */
12509 cp_lexer_consume_token (parser->lexer);
12510 /* Parse the default-argument. */
12511 push_deferring_access_checks (dk_no_deferred);
12512 default_argument = cp_parser_type_id (parser);
12514 /* Template parameter packs cannot have default
12515 arguments. */
12516 if (*is_parameter_pack)
12518 if (identifier)
12519 error_at (token->location,
12520 "template parameter pack %qD cannot have a "
12521 "default argument", identifier);
12522 else
12523 error_at (token->location,
12524 "template parameter packs cannot have "
12525 "default arguments");
12526 default_argument = NULL_TREE;
12528 pop_deferring_access_checks ();
12530 else
12531 default_argument = NULL_TREE;
12533 /* Create the combined representation of the parameter and the
12534 default argument. */
12535 parameter = build_tree_list (default_argument, parameter);
12537 break;
12539 case RID_TEMPLATE:
12541 tree identifier;
12542 tree default_argument;
12544 /* Look for the `<'. */
12545 cp_parser_require (parser, CPP_LESS, RT_LESS);
12546 /* Parse the template-parameter-list. */
12547 cp_parser_template_parameter_list (parser);
12548 /* Look for the `>'. */
12549 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12550 /* Look for the `class' keyword. */
12551 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12552 /* If the next token is an ellipsis, we have a template
12553 argument pack. */
12554 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12556 /* Consume the `...' token. */
12557 cp_lexer_consume_token (parser->lexer);
12558 maybe_warn_variadic_templates ();
12560 *is_parameter_pack = true;
12562 /* If the next token is an `=', then there is a
12563 default-argument. If the next token is a `>', we are at
12564 the end of the parameter-list. If the next token is a `,',
12565 then we are at the end of this parameter. */
12566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12567 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12568 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12570 identifier = cp_parser_identifier (parser);
12571 /* Treat invalid names as if the parameter were nameless. */
12572 if (identifier == error_mark_node)
12573 identifier = NULL_TREE;
12575 else
12576 identifier = NULL_TREE;
12578 /* Create the template parameter. */
12579 parameter = finish_template_template_parm (class_type_node,
12580 identifier);
12582 /* If the next token is an `=', then there is a
12583 default-argument. */
12584 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12586 bool is_template;
12588 /* Consume the `='. */
12589 cp_lexer_consume_token (parser->lexer);
12590 /* Parse the id-expression. */
12591 push_deferring_access_checks (dk_no_deferred);
12592 /* save token before parsing the id-expression, for error
12593 reporting */
12594 token = cp_lexer_peek_token (parser->lexer);
12595 default_argument
12596 = cp_parser_id_expression (parser,
12597 /*template_keyword_p=*/false,
12598 /*check_dependency_p=*/true,
12599 /*template_p=*/&is_template,
12600 /*declarator_p=*/false,
12601 /*optional_p=*/false);
12602 if (TREE_CODE (default_argument) == TYPE_DECL)
12603 /* If the id-expression was a template-id that refers to
12604 a template-class, we already have the declaration here,
12605 so no further lookup is needed. */
12607 else
12608 /* Look up the name. */
12609 default_argument
12610 = cp_parser_lookup_name (parser, default_argument,
12611 none_type,
12612 /*is_template=*/is_template,
12613 /*is_namespace=*/false,
12614 /*check_dependency=*/true,
12615 /*ambiguous_decls=*/NULL,
12616 token->location);
12617 /* See if the default argument is valid. */
12618 default_argument
12619 = check_template_template_default_arg (default_argument);
12621 /* Template parameter packs cannot have default
12622 arguments. */
12623 if (*is_parameter_pack)
12625 if (identifier)
12626 error_at (token->location,
12627 "template parameter pack %qD cannot "
12628 "have a default argument",
12629 identifier);
12630 else
12631 error_at (token->location, "template parameter packs cannot "
12632 "have default arguments");
12633 default_argument = NULL_TREE;
12635 pop_deferring_access_checks ();
12637 else
12638 default_argument = NULL_TREE;
12640 /* Create the combined representation of the parameter and the
12641 default argument. */
12642 parameter = build_tree_list (default_argument, parameter);
12644 break;
12646 default:
12647 gcc_unreachable ();
12648 break;
12651 return parameter;
12654 /* Parse a template-id.
12656 template-id:
12657 template-name < template-argument-list [opt] >
12659 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12660 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12661 returned. Otherwise, if the template-name names a function, or set
12662 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12663 names a class, returns a TYPE_DECL for the specialization.
12665 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12666 uninstantiated templates. */
12668 static tree
12669 cp_parser_template_id (cp_parser *parser,
12670 bool template_keyword_p,
12671 bool check_dependency_p,
12672 enum tag_types tag_type,
12673 bool is_declaration)
12675 int i;
12676 tree templ;
12677 tree arguments;
12678 tree template_id;
12679 cp_token_position start_of_id = 0;
12680 deferred_access_check *chk;
12681 vec<deferred_access_check, va_gc> *access_check;
12682 cp_token *next_token = NULL, *next_token_2 = NULL;
12683 bool is_identifier;
12685 /* If the next token corresponds to a template-id, there is no need
12686 to reparse it. */
12687 next_token = cp_lexer_peek_token (parser->lexer);
12688 if (next_token->type == CPP_TEMPLATE_ID)
12690 struct tree_check *check_value;
12692 /* Get the stored value. */
12693 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12694 /* Perform any access checks that were deferred. */
12695 access_check = check_value->checks;
12696 if (access_check)
12698 FOR_EACH_VEC_ELT (*access_check, i, chk)
12699 perform_or_defer_access_check (chk->binfo,
12700 chk->decl,
12701 chk->diag_decl,
12702 tf_warning_or_error);
12704 /* Return the stored value. */
12705 return check_value->value;
12708 /* Avoid performing name lookup if there is no possibility of
12709 finding a template-id. */
12710 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12711 || (next_token->type == CPP_NAME
12712 && !cp_parser_nth_token_starts_template_argument_list_p
12713 (parser, 2)))
12715 cp_parser_error (parser, "expected template-id");
12716 return error_mark_node;
12719 /* Remember where the template-id starts. */
12720 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12721 start_of_id = cp_lexer_token_position (parser->lexer, false);
12723 push_deferring_access_checks (dk_deferred);
12725 /* Parse the template-name. */
12726 is_identifier = false;
12727 templ = cp_parser_template_name (parser, template_keyword_p,
12728 check_dependency_p,
12729 is_declaration,
12730 tag_type,
12731 &is_identifier);
12732 if (templ == error_mark_node || is_identifier)
12734 pop_deferring_access_checks ();
12735 return templ;
12738 /* If we find the sequence `[:' after a template-name, it's probably
12739 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12740 parse correctly the argument list. */
12741 next_token = cp_lexer_peek_token (parser->lexer);
12742 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12743 if (next_token->type == CPP_OPEN_SQUARE
12744 && next_token->flags & DIGRAPH
12745 && next_token_2->type == CPP_COLON
12746 && !(next_token_2->flags & PREV_WHITE))
12748 cp_parser_parse_tentatively (parser);
12749 /* Change `:' into `::'. */
12750 next_token_2->type = CPP_SCOPE;
12751 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12752 CPP_LESS. */
12753 cp_lexer_consume_token (parser->lexer);
12755 /* Parse the arguments. */
12756 arguments = cp_parser_enclosed_template_argument_list (parser);
12757 if (!cp_parser_parse_definitely (parser))
12759 /* If we couldn't parse an argument list, then we revert our changes
12760 and return simply an error. Maybe this is not a template-id
12761 after all. */
12762 next_token_2->type = CPP_COLON;
12763 cp_parser_error (parser, "expected %<<%>");
12764 pop_deferring_access_checks ();
12765 return error_mark_node;
12767 /* Otherwise, emit an error about the invalid digraph, but continue
12768 parsing because we got our argument list. */
12769 if (permerror (next_token->location,
12770 "%<<::%> cannot begin a template-argument list"))
12772 static bool hint = false;
12773 inform (next_token->location,
12774 "%<<:%> is an alternate spelling for %<[%>."
12775 " Insert whitespace between %<<%> and %<::%>");
12776 if (!hint && !flag_permissive)
12778 inform (next_token->location, "(if you use %<-fpermissive%> "
12779 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12780 "accept your code)");
12781 hint = true;
12785 else
12787 /* Look for the `<' that starts the template-argument-list. */
12788 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12790 pop_deferring_access_checks ();
12791 return error_mark_node;
12793 /* Parse the arguments. */
12794 arguments = cp_parser_enclosed_template_argument_list (parser);
12797 /* Build a representation of the specialization. */
12798 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12799 template_id = build_min_nt_loc (next_token->location,
12800 TEMPLATE_ID_EXPR,
12801 templ, arguments);
12802 else if (DECL_TYPE_TEMPLATE_P (templ)
12803 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12805 bool entering_scope;
12806 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12807 template (rather than some instantiation thereof) only if
12808 is not nested within some other construct. For example, in
12809 "template <typename T> void f(T) { A<T>::", A<T> is just an
12810 instantiation of A. */
12811 entering_scope = (template_parm_scope_p ()
12812 && cp_lexer_next_token_is (parser->lexer,
12813 CPP_SCOPE));
12814 template_id
12815 = finish_template_type (templ, arguments, entering_scope);
12817 else
12819 /* If it's not a class-template or a template-template, it should be
12820 a function-template. */
12821 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12822 || TREE_CODE (templ) == OVERLOAD
12823 || BASELINK_P (templ)));
12825 template_id = lookup_template_function (templ, arguments);
12828 /* If parsing tentatively, replace the sequence of tokens that makes
12829 up the template-id with a CPP_TEMPLATE_ID token. That way,
12830 should we re-parse the token stream, we will not have to repeat
12831 the effort required to do the parse, nor will we issue duplicate
12832 error messages about problems during instantiation of the
12833 template. */
12834 if (start_of_id
12835 /* Don't do this if we had a parse error in a declarator; re-parsing
12836 might succeed if a name changes meaning (60361). */
12837 && !(cp_parser_error_occurred (parser)
12838 && cp_parser_parsing_tentatively (parser)
12839 && parser->in_declarator_p))
12841 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12843 /* Reset the contents of the START_OF_ID token. */
12844 token->type = CPP_TEMPLATE_ID;
12845 /* Retrieve any deferred checks. Do not pop this access checks yet
12846 so the memory will not be reclaimed during token replacing below. */
12847 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12848 token->u.tree_check_value->value = template_id;
12849 token->u.tree_check_value->checks = get_deferred_access_checks ();
12850 token->keyword = RID_MAX;
12852 /* Purge all subsequent tokens. */
12853 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12855 /* ??? Can we actually assume that, if template_id ==
12856 error_mark_node, we will have issued a diagnostic to the
12857 user, as opposed to simply marking the tentative parse as
12858 failed? */
12859 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12860 error_at (token->location, "parse error in template argument list");
12863 pop_deferring_access_checks ();
12864 return template_id;
12867 /* Parse a template-name.
12869 template-name:
12870 identifier
12872 The standard should actually say:
12874 template-name:
12875 identifier
12876 operator-function-id
12878 A defect report has been filed about this issue.
12880 A conversion-function-id cannot be a template name because they cannot
12881 be part of a template-id. In fact, looking at this code:
12883 a.operator K<int>()
12885 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12886 It is impossible to call a templated conversion-function-id with an
12887 explicit argument list, since the only allowed template parameter is
12888 the type to which it is converting.
12890 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12891 `template' keyword, in a construction like:
12893 T::template f<3>()
12895 In that case `f' is taken to be a template-name, even though there
12896 is no way of knowing for sure.
12898 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12899 name refers to a set of overloaded functions, at least one of which
12900 is a template, or an IDENTIFIER_NODE with the name of the template,
12901 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12902 names are looked up inside uninstantiated templates. */
12904 static tree
12905 cp_parser_template_name (cp_parser* parser,
12906 bool template_keyword_p,
12907 bool check_dependency_p,
12908 bool is_declaration,
12909 enum tag_types tag_type,
12910 bool *is_identifier)
12912 tree identifier;
12913 tree decl;
12914 tree fns;
12915 cp_token *token = cp_lexer_peek_token (parser->lexer);
12917 /* If the next token is `operator', then we have either an
12918 operator-function-id or a conversion-function-id. */
12919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12921 /* We don't know whether we're looking at an
12922 operator-function-id or a conversion-function-id. */
12923 cp_parser_parse_tentatively (parser);
12924 /* Try an operator-function-id. */
12925 identifier = cp_parser_operator_function_id (parser);
12926 /* If that didn't work, try a conversion-function-id. */
12927 if (!cp_parser_parse_definitely (parser))
12929 cp_parser_error (parser, "expected template-name");
12930 return error_mark_node;
12933 /* Look for the identifier. */
12934 else
12935 identifier = cp_parser_identifier (parser);
12937 /* If we didn't find an identifier, we don't have a template-id. */
12938 if (identifier == error_mark_node)
12939 return error_mark_node;
12941 /* If the name immediately followed the `template' keyword, then it
12942 is a template-name. However, if the next token is not `<', then
12943 we do not treat it as a template-name, since it is not being used
12944 as part of a template-id. This enables us to handle constructs
12945 like:
12947 template <typename T> struct S { S(); };
12948 template <typename T> S<T>::S();
12950 correctly. We would treat `S' as a template -- if it were `S<T>'
12951 -- but we do not if there is no `<'. */
12953 if (processing_template_decl
12954 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12956 /* In a declaration, in a dependent context, we pretend that the
12957 "template" keyword was present in order to improve error
12958 recovery. For example, given:
12960 template <typename T> void f(T::X<int>);
12962 we want to treat "X<int>" as a template-id. */
12963 if (is_declaration
12964 && !template_keyword_p
12965 && parser->scope && TYPE_P (parser->scope)
12966 && check_dependency_p
12967 && dependent_scope_p (parser->scope)
12968 /* Do not do this for dtors (or ctors), since they never
12969 need the template keyword before their name. */
12970 && !constructor_name_p (identifier, parser->scope))
12972 cp_token_position start = 0;
12974 /* Explain what went wrong. */
12975 error_at (token->location, "non-template %qD used as template",
12976 identifier);
12977 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12978 parser->scope, identifier);
12979 /* If parsing tentatively, find the location of the "<" token. */
12980 if (cp_parser_simulate_error (parser))
12981 start = cp_lexer_token_position (parser->lexer, true);
12982 /* Parse the template arguments so that we can issue error
12983 messages about them. */
12984 cp_lexer_consume_token (parser->lexer);
12985 cp_parser_enclosed_template_argument_list (parser);
12986 /* Skip tokens until we find a good place from which to
12987 continue parsing. */
12988 cp_parser_skip_to_closing_parenthesis (parser,
12989 /*recovering=*/true,
12990 /*or_comma=*/true,
12991 /*consume_paren=*/false);
12992 /* If parsing tentatively, permanently remove the
12993 template argument list. That will prevent duplicate
12994 error messages from being issued about the missing
12995 "template" keyword. */
12996 if (start)
12997 cp_lexer_purge_tokens_after (parser->lexer, start);
12998 if (is_identifier)
12999 *is_identifier = true;
13000 return identifier;
13003 /* If the "template" keyword is present, then there is generally
13004 no point in doing name-lookup, so we just return IDENTIFIER.
13005 But, if the qualifying scope is non-dependent then we can
13006 (and must) do name-lookup normally. */
13007 if (template_keyword_p
13008 && (!parser->scope
13009 || (TYPE_P (parser->scope)
13010 && dependent_type_p (parser->scope))))
13011 return identifier;
13014 /* Look up the name. */
13015 decl = cp_parser_lookup_name (parser, identifier,
13016 tag_type,
13017 /*is_template=*/true,
13018 /*is_namespace=*/false,
13019 check_dependency_p,
13020 /*ambiguous_decls=*/NULL,
13021 token->location);
13023 /* If DECL is a template, then the name was a template-name. */
13024 if (TREE_CODE (decl) == TEMPLATE_DECL)
13026 else
13028 tree fn = NULL_TREE;
13030 /* The standard does not explicitly indicate whether a name that
13031 names a set of overloaded declarations, some of which are
13032 templates, is a template-name. However, such a name should
13033 be a template-name; otherwise, there is no way to form a
13034 template-id for the overloaded templates. */
13035 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13036 if (TREE_CODE (fns) == OVERLOAD)
13037 for (fn = fns; fn; fn = OVL_NEXT (fn))
13038 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13039 break;
13041 if (!fn)
13043 /* The name does not name a template. */
13044 cp_parser_error (parser, "expected template-name");
13045 return error_mark_node;
13049 /* If DECL is dependent, and refers to a function, then just return
13050 its name; we will look it up again during template instantiation. */
13051 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13053 tree scope = ovl_scope (decl);
13054 if (TYPE_P (scope) && dependent_type_p (scope))
13055 return identifier;
13058 return decl;
13061 /* Parse a template-argument-list.
13063 template-argument-list:
13064 template-argument ... [opt]
13065 template-argument-list , template-argument ... [opt]
13067 Returns a TREE_VEC containing the arguments. */
13069 static tree
13070 cp_parser_template_argument_list (cp_parser* parser)
13072 tree fixed_args[10];
13073 unsigned n_args = 0;
13074 unsigned alloced = 10;
13075 tree *arg_ary = fixed_args;
13076 tree vec;
13077 bool saved_in_template_argument_list_p;
13078 bool saved_ice_p;
13079 bool saved_non_ice_p;
13081 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13082 parser->in_template_argument_list_p = true;
13083 /* Even if the template-id appears in an integral
13084 constant-expression, the contents of the argument list do
13085 not. */
13086 saved_ice_p = parser->integral_constant_expression_p;
13087 parser->integral_constant_expression_p = false;
13088 saved_non_ice_p = parser->non_integral_constant_expression_p;
13089 parser->non_integral_constant_expression_p = false;
13091 /* Parse the arguments. */
13094 tree argument;
13096 if (n_args)
13097 /* Consume the comma. */
13098 cp_lexer_consume_token (parser->lexer);
13100 /* Parse the template-argument. */
13101 argument = cp_parser_template_argument (parser);
13103 /* If the next token is an ellipsis, we're expanding a template
13104 argument pack. */
13105 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13107 if (argument == error_mark_node)
13109 cp_token *token = cp_lexer_peek_token (parser->lexer);
13110 error_at (token->location,
13111 "expected parameter pack before %<...%>");
13113 /* Consume the `...' token. */
13114 cp_lexer_consume_token (parser->lexer);
13116 /* Make the argument into a TYPE_PACK_EXPANSION or
13117 EXPR_PACK_EXPANSION. */
13118 argument = make_pack_expansion (argument);
13121 if (n_args == alloced)
13123 alloced *= 2;
13125 if (arg_ary == fixed_args)
13127 arg_ary = XNEWVEC (tree, alloced);
13128 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13130 else
13131 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13133 arg_ary[n_args++] = argument;
13135 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13137 vec = make_tree_vec (n_args);
13139 while (n_args--)
13140 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13142 if (arg_ary != fixed_args)
13143 free (arg_ary);
13144 parser->non_integral_constant_expression_p = saved_non_ice_p;
13145 parser->integral_constant_expression_p = saved_ice_p;
13146 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13147 #ifdef ENABLE_CHECKING
13148 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13149 #endif
13150 return vec;
13153 /* Parse a template-argument.
13155 template-argument:
13156 assignment-expression
13157 type-id
13158 id-expression
13160 The representation is that of an assignment-expression, type-id, or
13161 id-expression -- except that the qualified id-expression is
13162 evaluated, so that the value returned is either a DECL or an
13163 OVERLOAD.
13165 Although the standard says "assignment-expression", it forbids
13166 throw-expressions or assignments in the template argument.
13167 Therefore, we use "conditional-expression" instead. */
13169 static tree
13170 cp_parser_template_argument (cp_parser* parser)
13172 tree argument;
13173 bool template_p;
13174 bool address_p;
13175 bool maybe_type_id = false;
13176 cp_token *token = NULL, *argument_start_token = NULL;
13177 location_t loc = 0;
13178 cp_id_kind idk;
13180 /* There's really no way to know what we're looking at, so we just
13181 try each alternative in order.
13183 [temp.arg]
13185 In a template-argument, an ambiguity between a type-id and an
13186 expression is resolved to a type-id, regardless of the form of
13187 the corresponding template-parameter.
13189 Therefore, we try a type-id first. */
13190 cp_parser_parse_tentatively (parser);
13191 argument = cp_parser_template_type_arg (parser);
13192 /* If there was no error parsing the type-id but the next token is a
13193 '>>', our behavior depends on which dialect of C++ we're
13194 parsing. In C++98, we probably found a typo for '> >'. But there
13195 are type-id which are also valid expressions. For instance:
13197 struct X { int operator >> (int); };
13198 template <int V> struct Foo {};
13199 Foo<X () >> 5> r;
13201 Here 'X()' is a valid type-id of a function type, but the user just
13202 wanted to write the expression "X() >> 5". Thus, we remember that we
13203 found a valid type-id, but we still try to parse the argument as an
13204 expression to see what happens.
13206 In C++0x, the '>>' will be considered two separate '>'
13207 tokens. */
13208 if (!cp_parser_error_occurred (parser)
13209 && cxx_dialect == cxx98
13210 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13212 maybe_type_id = true;
13213 cp_parser_abort_tentative_parse (parser);
13215 else
13217 /* If the next token isn't a `,' or a `>', then this argument wasn't
13218 really finished. This means that the argument is not a valid
13219 type-id. */
13220 if (!cp_parser_next_token_ends_template_argument_p (parser))
13221 cp_parser_error (parser, "expected template-argument");
13222 /* If that worked, we're done. */
13223 if (cp_parser_parse_definitely (parser))
13224 return argument;
13226 /* We're still not sure what the argument will be. */
13227 cp_parser_parse_tentatively (parser);
13228 /* Try a template. */
13229 argument_start_token = cp_lexer_peek_token (parser->lexer);
13230 argument = cp_parser_id_expression (parser,
13231 /*template_keyword_p=*/false,
13232 /*check_dependency_p=*/true,
13233 &template_p,
13234 /*declarator_p=*/false,
13235 /*optional_p=*/false);
13236 /* If the next token isn't a `,' or a `>', then this argument wasn't
13237 really finished. */
13238 if (!cp_parser_next_token_ends_template_argument_p (parser))
13239 cp_parser_error (parser, "expected template-argument");
13240 if (!cp_parser_error_occurred (parser))
13242 /* Figure out what is being referred to. If the id-expression
13243 was for a class template specialization, then we will have a
13244 TYPE_DECL at this point. There is no need to do name lookup
13245 at this point in that case. */
13246 if (TREE_CODE (argument) != TYPE_DECL)
13247 argument = cp_parser_lookup_name (parser, argument,
13248 none_type,
13249 /*is_template=*/template_p,
13250 /*is_namespace=*/false,
13251 /*check_dependency=*/true,
13252 /*ambiguous_decls=*/NULL,
13253 argument_start_token->location);
13254 if (TREE_CODE (argument) != TEMPLATE_DECL
13255 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13256 cp_parser_error (parser, "expected template-name");
13258 if (cp_parser_parse_definitely (parser))
13259 return argument;
13260 /* It must be a non-type argument. There permitted cases are given
13261 in [temp.arg.nontype]:
13263 -- an integral constant-expression of integral or enumeration
13264 type; or
13266 -- the name of a non-type template-parameter; or
13268 -- the name of an object or function with external linkage...
13270 -- the address of an object or function with external linkage...
13272 -- a pointer to member... */
13273 /* Look for a non-type template parameter. */
13274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13276 cp_parser_parse_tentatively (parser);
13277 argument = cp_parser_primary_expression (parser,
13278 /*address_p=*/false,
13279 /*cast_p=*/false,
13280 /*template_arg_p=*/true,
13281 &idk);
13282 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13283 || !cp_parser_next_token_ends_template_argument_p (parser))
13284 cp_parser_simulate_error (parser);
13285 if (cp_parser_parse_definitely (parser))
13286 return argument;
13289 /* If the next token is "&", the argument must be the address of an
13290 object or function with external linkage. */
13291 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13292 if (address_p)
13294 loc = cp_lexer_peek_token (parser->lexer)->location;
13295 cp_lexer_consume_token (parser->lexer);
13297 /* See if we might have an id-expression. */
13298 token = cp_lexer_peek_token (parser->lexer);
13299 if (token->type == CPP_NAME
13300 || token->keyword == RID_OPERATOR
13301 || token->type == CPP_SCOPE
13302 || token->type == CPP_TEMPLATE_ID
13303 || token->type == CPP_NESTED_NAME_SPECIFIER)
13305 cp_parser_parse_tentatively (parser);
13306 argument = cp_parser_primary_expression (parser,
13307 address_p,
13308 /*cast_p=*/false,
13309 /*template_arg_p=*/true,
13310 &idk);
13311 if (cp_parser_error_occurred (parser)
13312 || !cp_parser_next_token_ends_template_argument_p (parser))
13313 cp_parser_abort_tentative_parse (parser);
13314 else
13316 tree probe;
13318 if (TREE_CODE (argument) == INDIRECT_REF)
13320 gcc_assert (REFERENCE_REF_P (argument));
13321 argument = TREE_OPERAND (argument, 0);
13324 /* If we're in a template, we represent a qualified-id referring
13325 to a static data member as a SCOPE_REF even if the scope isn't
13326 dependent so that we can check access control later. */
13327 probe = argument;
13328 if (TREE_CODE (probe) == SCOPE_REF)
13329 probe = TREE_OPERAND (probe, 1);
13330 if (TREE_CODE (probe) == VAR_DECL)
13332 /* A variable without external linkage might still be a
13333 valid constant-expression, so no error is issued here
13334 if the external-linkage check fails. */
13335 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13336 cp_parser_simulate_error (parser);
13338 else if (is_overloaded_fn (argument))
13339 /* All overloaded functions are allowed; if the external
13340 linkage test does not pass, an error will be issued
13341 later. */
13343 else if (address_p
13344 && (TREE_CODE (argument) == OFFSET_REF
13345 || TREE_CODE (argument) == SCOPE_REF))
13346 /* A pointer-to-member. */
13348 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13350 else
13351 cp_parser_simulate_error (parser);
13353 if (cp_parser_parse_definitely (parser))
13355 if (address_p)
13356 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13357 tf_warning_or_error);
13358 return argument;
13362 /* If the argument started with "&", there are no other valid
13363 alternatives at this point. */
13364 if (address_p)
13366 cp_parser_error (parser, "invalid non-type template argument");
13367 return error_mark_node;
13370 /* If the argument wasn't successfully parsed as a type-id followed
13371 by '>>', the argument can only be a constant expression now.
13372 Otherwise, we try parsing the constant-expression tentatively,
13373 because the argument could really be a type-id. */
13374 if (maybe_type_id)
13375 cp_parser_parse_tentatively (parser);
13376 argument = cp_parser_constant_expression (parser,
13377 /*allow_non_constant_p=*/false,
13378 /*non_constant_p=*/NULL);
13379 argument = fold_non_dependent_expr (argument);
13380 if (!maybe_type_id)
13381 return argument;
13382 if (!cp_parser_next_token_ends_template_argument_p (parser))
13383 cp_parser_error (parser, "expected template-argument");
13384 if (cp_parser_parse_definitely (parser))
13385 return argument;
13386 /* We did our best to parse the argument as a non type-id, but that
13387 was the only alternative that matched (albeit with a '>' after
13388 it). We can assume it's just a typo from the user, and a
13389 diagnostic will then be issued. */
13390 return cp_parser_template_type_arg (parser);
13393 /* Parse an explicit-instantiation.
13395 explicit-instantiation:
13396 template declaration
13398 Although the standard says `declaration', what it really means is:
13400 explicit-instantiation:
13401 template decl-specifier-seq [opt] declarator [opt] ;
13403 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13404 supposed to be allowed. A defect report has been filed about this
13405 issue.
13407 GNU Extension:
13409 explicit-instantiation:
13410 storage-class-specifier template
13411 decl-specifier-seq [opt] declarator [opt] ;
13412 function-specifier template
13413 decl-specifier-seq [opt] declarator [opt] ; */
13415 static void
13416 cp_parser_explicit_instantiation (cp_parser* parser)
13418 int declares_class_or_enum;
13419 cp_decl_specifier_seq decl_specifiers;
13420 tree extension_specifier = NULL_TREE;
13422 timevar_push (TV_TEMPLATE_INST);
13424 /* Look for an (optional) storage-class-specifier or
13425 function-specifier. */
13426 if (cp_parser_allow_gnu_extensions_p (parser))
13428 extension_specifier
13429 = cp_parser_storage_class_specifier_opt (parser);
13430 if (!extension_specifier)
13431 extension_specifier
13432 = cp_parser_function_specifier_opt (parser,
13433 /*decl_specs=*/NULL);
13436 /* Look for the `template' keyword. */
13437 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13438 /* Let the front end know that we are processing an explicit
13439 instantiation. */
13440 begin_explicit_instantiation ();
13441 /* [temp.explicit] says that we are supposed to ignore access
13442 control while processing explicit instantiation directives. */
13443 push_deferring_access_checks (dk_no_check);
13444 /* Parse a decl-specifier-seq. */
13445 cp_parser_decl_specifier_seq (parser,
13446 CP_PARSER_FLAGS_OPTIONAL,
13447 &decl_specifiers,
13448 &declares_class_or_enum);
13449 /* If there was exactly one decl-specifier, and it declared a class,
13450 and there's no declarator, then we have an explicit type
13451 instantiation. */
13452 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13454 tree type;
13456 type = check_tag_decl (&decl_specifiers,
13457 /*explicit_type_instantiation_p=*/true);
13458 /* Turn access control back on for names used during
13459 template instantiation. */
13460 pop_deferring_access_checks ();
13461 if (type)
13462 do_type_instantiation (type, extension_specifier,
13463 /*complain=*/tf_error);
13465 else
13467 cp_declarator *declarator;
13468 tree decl;
13470 /* Parse the declarator. */
13471 declarator
13472 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13473 /*ctor_dtor_or_conv_p=*/NULL,
13474 /*parenthesized_p=*/NULL,
13475 /*member_p=*/false);
13476 if (declares_class_or_enum & 2)
13477 cp_parser_check_for_definition_in_return_type (declarator,
13478 decl_specifiers.type,
13479 decl_specifiers.locations[ds_type_spec]);
13480 if (declarator != cp_error_declarator)
13482 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13483 permerror (decl_specifiers.locations[ds_inline],
13484 "explicit instantiation shall not use"
13485 " %<inline%> specifier");
13486 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13487 permerror (decl_specifiers.locations[ds_constexpr],
13488 "explicit instantiation shall not use"
13489 " %<constexpr%> specifier");
13491 decl = grokdeclarator (declarator, &decl_specifiers,
13492 NORMAL, 0, &decl_specifiers.attributes);
13493 /* Turn access control back on for names used during
13494 template instantiation. */
13495 pop_deferring_access_checks ();
13496 /* Do the explicit instantiation. */
13497 do_decl_instantiation (decl, extension_specifier);
13499 else
13501 pop_deferring_access_checks ();
13502 /* Skip the body of the explicit instantiation. */
13503 cp_parser_skip_to_end_of_statement (parser);
13506 /* We're done with the instantiation. */
13507 end_explicit_instantiation ();
13509 cp_parser_consume_semicolon_at_end_of_statement (parser);
13511 timevar_pop (TV_TEMPLATE_INST);
13514 /* Parse an explicit-specialization.
13516 explicit-specialization:
13517 template < > declaration
13519 Although the standard says `declaration', what it really means is:
13521 explicit-specialization:
13522 template <> decl-specifier [opt] init-declarator [opt] ;
13523 template <> function-definition
13524 template <> explicit-specialization
13525 template <> template-declaration */
13527 static void
13528 cp_parser_explicit_specialization (cp_parser* parser)
13530 bool need_lang_pop;
13531 cp_token *token = cp_lexer_peek_token (parser->lexer);
13533 /* Look for the `template' keyword. */
13534 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13535 /* Look for the `<'. */
13536 cp_parser_require (parser, CPP_LESS, RT_LESS);
13537 /* Look for the `>'. */
13538 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13539 /* We have processed another parameter list. */
13540 ++parser->num_template_parameter_lists;
13541 /* [temp]
13543 A template ... explicit specialization ... shall not have C
13544 linkage. */
13545 if (current_lang_name == lang_name_c)
13547 error_at (token->location, "template specialization with C linkage");
13548 /* Give it C++ linkage to avoid confusing other parts of the
13549 front end. */
13550 push_lang_context (lang_name_cplusplus);
13551 need_lang_pop = true;
13553 else
13554 need_lang_pop = false;
13555 /* Let the front end know that we are beginning a specialization. */
13556 if (!begin_specialization ())
13558 end_specialization ();
13559 return;
13562 /* If the next keyword is `template', we need to figure out whether
13563 or not we're looking a template-declaration. */
13564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13566 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13567 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13568 cp_parser_template_declaration_after_export (parser,
13569 /*member_p=*/false);
13570 else
13571 cp_parser_explicit_specialization (parser);
13573 else
13574 /* Parse the dependent declaration. */
13575 cp_parser_single_declaration (parser,
13576 /*checks=*/NULL,
13577 /*member_p=*/false,
13578 /*explicit_specialization_p=*/true,
13579 /*friend_p=*/NULL);
13580 /* We're done with the specialization. */
13581 end_specialization ();
13582 /* For the erroneous case of a template with C linkage, we pushed an
13583 implicit C++ linkage scope; exit that scope now. */
13584 if (need_lang_pop)
13585 pop_lang_context ();
13586 /* We're done with this parameter list. */
13587 --parser->num_template_parameter_lists;
13590 /* Parse a type-specifier.
13592 type-specifier:
13593 simple-type-specifier
13594 class-specifier
13595 enum-specifier
13596 elaborated-type-specifier
13597 cv-qualifier
13599 GNU Extension:
13601 type-specifier:
13602 __complex__
13604 Returns a representation of the type-specifier. For a
13605 class-specifier, enum-specifier, or elaborated-type-specifier, a
13606 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13608 The parser flags FLAGS is used to control type-specifier parsing.
13610 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13611 in a decl-specifier-seq.
13613 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13614 class-specifier, enum-specifier, or elaborated-type-specifier, then
13615 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13616 if a type is declared; 2 if it is defined. Otherwise, it is set to
13617 zero.
13619 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13620 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13621 is set to FALSE. */
13623 static tree
13624 cp_parser_type_specifier (cp_parser* parser,
13625 cp_parser_flags flags,
13626 cp_decl_specifier_seq *decl_specs,
13627 bool is_declaration,
13628 int* declares_class_or_enum,
13629 bool* is_cv_qualifier)
13631 tree type_spec = NULL_TREE;
13632 cp_token *token;
13633 enum rid keyword;
13634 cp_decl_spec ds = ds_last;
13636 /* Assume this type-specifier does not declare a new type. */
13637 if (declares_class_or_enum)
13638 *declares_class_or_enum = 0;
13639 /* And that it does not specify a cv-qualifier. */
13640 if (is_cv_qualifier)
13641 *is_cv_qualifier = false;
13642 /* Peek at the next token. */
13643 token = cp_lexer_peek_token (parser->lexer);
13645 /* If we're looking at a keyword, we can use that to guide the
13646 production we choose. */
13647 keyword = token->keyword;
13648 switch (keyword)
13650 case RID_ENUM:
13651 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13652 goto elaborated_type_specifier;
13654 /* Look for the enum-specifier. */
13655 type_spec = cp_parser_enum_specifier (parser);
13656 /* If that worked, we're done. */
13657 if (type_spec)
13659 if (declares_class_or_enum)
13660 *declares_class_or_enum = 2;
13661 if (decl_specs)
13662 cp_parser_set_decl_spec_type (decl_specs,
13663 type_spec,
13664 token,
13665 /*type_definition_p=*/true);
13666 return type_spec;
13668 else
13669 goto elaborated_type_specifier;
13671 /* Any of these indicate either a class-specifier, or an
13672 elaborated-type-specifier. */
13673 case RID_CLASS:
13674 case RID_STRUCT:
13675 case RID_UNION:
13676 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13677 goto elaborated_type_specifier;
13679 /* Parse tentatively so that we can back up if we don't find a
13680 class-specifier. */
13681 cp_parser_parse_tentatively (parser);
13682 /* Look for the class-specifier. */
13683 type_spec = cp_parser_class_specifier (parser);
13684 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13685 /* If that worked, we're done. */
13686 if (cp_parser_parse_definitely (parser))
13688 if (declares_class_or_enum)
13689 *declares_class_or_enum = 2;
13690 if (decl_specs)
13691 cp_parser_set_decl_spec_type (decl_specs,
13692 type_spec,
13693 token,
13694 /*type_definition_p=*/true);
13695 return type_spec;
13698 /* Fall through. */
13699 elaborated_type_specifier:
13700 /* We're declaring (not defining) a class or enum. */
13701 if (declares_class_or_enum)
13702 *declares_class_or_enum = 1;
13704 /* Fall through. */
13705 case RID_TYPENAME:
13706 /* Look for an elaborated-type-specifier. */
13707 type_spec
13708 = (cp_parser_elaborated_type_specifier
13709 (parser,
13710 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13711 is_declaration));
13712 if (decl_specs)
13713 cp_parser_set_decl_spec_type (decl_specs,
13714 type_spec,
13715 token,
13716 /*type_definition_p=*/false);
13717 return type_spec;
13719 case RID_CONST:
13720 ds = ds_const;
13721 if (is_cv_qualifier)
13722 *is_cv_qualifier = true;
13723 break;
13725 case RID_VOLATILE:
13726 ds = ds_volatile;
13727 if (is_cv_qualifier)
13728 *is_cv_qualifier = true;
13729 break;
13731 case RID_RESTRICT:
13732 ds = ds_restrict;
13733 if (is_cv_qualifier)
13734 *is_cv_qualifier = true;
13735 break;
13737 case RID_COMPLEX:
13738 /* The `__complex__' keyword is a GNU extension. */
13739 ds = ds_complex;
13740 break;
13742 default:
13743 break;
13746 /* Handle simple keywords. */
13747 if (ds != ds_last)
13749 if (decl_specs)
13751 set_and_check_decl_spec_loc (decl_specs, ds, token);
13752 decl_specs->any_specifiers_p = true;
13754 return cp_lexer_consume_token (parser->lexer)->u.value;
13757 /* If we do not already have a type-specifier, assume we are looking
13758 at a simple-type-specifier. */
13759 type_spec = cp_parser_simple_type_specifier (parser,
13760 decl_specs,
13761 flags);
13763 /* If we didn't find a type-specifier, and a type-specifier was not
13764 optional in this context, issue an error message. */
13765 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13767 cp_parser_error (parser, "expected type specifier");
13768 return error_mark_node;
13771 return type_spec;
13774 /* Parse a simple-type-specifier.
13776 simple-type-specifier:
13777 :: [opt] nested-name-specifier [opt] type-name
13778 :: [opt] nested-name-specifier template template-id
13779 char
13780 wchar_t
13781 bool
13782 short
13784 long
13785 signed
13786 unsigned
13787 float
13788 double
13789 void
13791 C++0x Extension:
13793 simple-type-specifier:
13794 auto
13795 decltype ( expression )
13796 char16_t
13797 char32_t
13798 __underlying_type ( type-id )
13800 GNU Extension:
13802 simple-type-specifier:
13803 __int128
13804 __typeof__ unary-expression
13805 __typeof__ ( type-id )
13807 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13808 appropriately updated. */
13810 static tree
13811 cp_parser_simple_type_specifier (cp_parser* parser,
13812 cp_decl_specifier_seq *decl_specs,
13813 cp_parser_flags flags)
13815 tree type = NULL_TREE;
13816 cp_token *token;
13818 /* Peek at the next token. */
13819 token = cp_lexer_peek_token (parser->lexer);
13821 /* If we're looking at a keyword, things are easy. */
13822 switch (token->keyword)
13824 case RID_CHAR:
13825 if (decl_specs)
13826 decl_specs->explicit_char_p = true;
13827 type = char_type_node;
13828 break;
13829 case RID_CHAR16:
13830 type = char16_type_node;
13831 break;
13832 case RID_CHAR32:
13833 type = char32_type_node;
13834 break;
13835 case RID_WCHAR:
13836 type = wchar_type_node;
13837 break;
13838 case RID_BOOL:
13839 type = boolean_type_node;
13840 break;
13841 case RID_SHORT:
13842 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13843 type = short_integer_type_node;
13844 break;
13845 case RID_INT:
13846 if (decl_specs)
13847 decl_specs->explicit_int_p = true;
13848 type = integer_type_node;
13849 break;
13850 case RID_INT128:
13851 if (!int128_integer_type_node)
13852 break;
13853 if (decl_specs)
13854 decl_specs->explicit_int128_p = true;
13855 type = int128_integer_type_node;
13856 break;
13857 case RID_LONG:
13858 if (decl_specs)
13859 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13860 type = long_integer_type_node;
13861 break;
13862 case RID_SIGNED:
13863 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13864 type = integer_type_node;
13865 break;
13866 case RID_UNSIGNED:
13867 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13868 type = unsigned_type_node;
13869 break;
13870 case RID_FLOAT:
13871 type = float_type_node;
13872 break;
13873 case RID_DOUBLE:
13874 type = double_type_node;
13875 break;
13876 case RID_VOID:
13877 type = void_type_node;
13878 break;
13880 case RID_AUTO:
13881 maybe_warn_cpp0x (CPP0X_AUTO);
13882 type = make_auto ();
13883 break;
13885 case RID_DECLTYPE:
13886 /* Since DR 743, decltype can either be a simple-type-specifier by
13887 itself or begin a nested-name-specifier. Parsing it will replace
13888 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13889 handling below decide what to do. */
13890 cp_parser_decltype (parser);
13891 cp_lexer_set_token_position (parser->lexer, token);
13892 break;
13894 case RID_TYPEOF:
13895 /* Consume the `typeof' token. */
13896 cp_lexer_consume_token (parser->lexer);
13897 /* Parse the operand to `typeof'. */
13898 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13899 /* If it is not already a TYPE, take its type. */
13900 if (!TYPE_P (type))
13901 type = finish_typeof (type);
13903 if (decl_specs)
13904 cp_parser_set_decl_spec_type (decl_specs, type,
13905 token,
13906 /*type_definition_p=*/false);
13908 return type;
13910 case RID_UNDERLYING_TYPE:
13911 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13912 if (decl_specs)
13913 cp_parser_set_decl_spec_type (decl_specs, type,
13914 token,
13915 /*type_definition_p=*/false);
13917 return type;
13919 case RID_BASES:
13920 case RID_DIRECT_BASES:
13921 type = cp_parser_trait_expr (parser, token->keyword);
13922 if (decl_specs)
13923 cp_parser_set_decl_spec_type (decl_specs, type,
13924 token,
13925 /*type_definition_p=*/false);
13926 return type;
13927 default:
13928 break;
13931 /* If token is an already-parsed decltype not followed by ::,
13932 it's a simple-type-specifier. */
13933 if (token->type == CPP_DECLTYPE
13934 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13936 type = token->u.value;
13937 if (decl_specs)
13938 cp_parser_set_decl_spec_type (decl_specs, type,
13939 token,
13940 /*type_definition_p=*/false);
13941 cp_lexer_consume_token (parser->lexer);
13942 return type;
13945 /* If the type-specifier was for a built-in type, we're done. */
13946 if (type)
13948 /* Record the type. */
13949 if (decl_specs
13950 && (token->keyword != RID_SIGNED
13951 && token->keyword != RID_UNSIGNED
13952 && token->keyword != RID_SHORT
13953 && token->keyword != RID_LONG))
13954 cp_parser_set_decl_spec_type (decl_specs,
13955 type,
13956 token,
13957 /*type_definition_p=*/false);
13958 if (decl_specs)
13959 decl_specs->any_specifiers_p = true;
13961 /* Consume the token. */
13962 cp_lexer_consume_token (parser->lexer);
13964 /* There is no valid C++ program where a non-template type is
13965 followed by a "<". That usually indicates that the user thought
13966 that the type was a template. */
13967 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13968 token->location);
13970 return TYPE_NAME (type);
13973 /* The type-specifier must be a user-defined type. */
13974 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13976 bool qualified_p;
13977 bool global_p;
13979 /* Don't gobble tokens or issue error messages if this is an
13980 optional type-specifier. */
13981 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13982 cp_parser_parse_tentatively (parser);
13984 /* Look for the optional `::' operator. */
13985 global_p
13986 = (cp_parser_global_scope_opt (parser,
13987 /*current_scope_valid_p=*/false)
13988 != NULL_TREE);
13989 /* Look for the nested-name specifier. */
13990 qualified_p
13991 = (cp_parser_nested_name_specifier_opt (parser,
13992 /*typename_keyword_p=*/false,
13993 /*check_dependency_p=*/true,
13994 /*type_p=*/false,
13995 /*is_declaration=*/false)
13996 != NULL_TREE);
13997 token = cp_lexer_peek_token (parser->lexer);
13998 /* If we have seen a nested-name-specifier, and the next token
13999 is `template', then we are using the template-id production. */
14000 if (parser->scope
14001 && cp_parser_optional_template_keyword (parser))
14003 /* Look for the template-id. */
14004 type = cp_parser_template_id (parser,
14005 /*template_keyword_p=*/true,
14006 /*check_dependency_p=*/true,
14007 none_type,
14008 /*is_declaration=*/false);
14009 /* If the template-id did not name a type, we are out of
14010 luck. */
14011 if (TREE_CODE (type) != TYPE_DECL)
14013 cp_parser_error (parser, "expected template-id for type");
14014 type = NULL_TREE;
14017 /* Otherwise, look for a type-name. */
14018 else
14019 type = cp_parser_type_name (parser);
14020 /* Keep track of all name-lookups performed in class scopes. */
14021 if (type
14022 && !global_p
14023 && !qualified_p
14024 && TREE_CODE (type) == TYPE_DECL
14025 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
14026 maybe_note_name_used_in_class (DECL_NAME (type), type);
14027 /* If it didn't work out, we don't have a TYPE. */
14028 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14029 && !cp_parser_parse_definitely (parser))
14030 type = NULL_TREE;
14031 if (type && decl_specs)
14032 cp_parser_set_decl_spec_type (decl_specs, type,
14033 token,
14034 /*type_definition_p=*/false);
14037 /* If we didn't get a type-name, issue an error message. */
14038 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14040 cp_parser_error (parser, "expected type-name");
14041 return error_mark_node;
14044 if (type && type != error_mark_node)
14046 /* See if TYPE is an Objective-C type, and if so, parse and
14047 accept any protocol references following it. Do this before
14048 the cp_parser_check_for_invalid_template_id() call, because
14049 Objective-C types can be followed by '<...>' which would
14050 enclose protocol names rather than template arguments, and so
14051 everything is fine. */
14052 if (c_dialect_objc () && !parser->scope
14053 && (objc_is_id (type) || objc_is_class_name (type)))
14055 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14056 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14058 /* Clobber the "unqualified" type previously entered into
14059 DECL_SPECS with the new, improved protocol-qualified version. */
14060 if (decl_specs)
14061 decl_specs->type = qual_type;
14063 return qual_type;
14066 /* There is no valid C++ program where a non-template type is
14067 followed by a "<". That usually indicates that the user
14068 thought that the type was a template. */
14069 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14070 none_type,
14071 token->location);
14074 return type;
14077 /* Parse a type-name.
14079 type-name:
14080 class-name
14081 enum-name
14082 typedef-name
14083 simple-template-id [in c++0x]
14085 enum-name:
14086 identifier
14088 typedef-name:
14089 identifier
14091 Returns a TYPE_DECL for the type. */
14093 static tree
14094 cp_parser_type_name (cp_parser* parser)
14096 tree type_decl;
14098 /* We can't know yet whether it is a class-name or not. */
14099 cp_parser_parse_tentatively (parser);
14100 /* Try a class-name. */
14101 type_decl = cp_parser_class_name (parser,
14102 /*typename_keyword_p=*/false,
14103 /*template_keyword_p=*/false,
14104 none_type,
14105 /*check_dependency_p=*/true,
14106 /*class_head_p=*/false,
14107 /*is_declaration=*/false);
14108 /* If it's not a class-name, keep looking. */
14109 if (!cp_parser_parse_definitely (parser))
14111 if (cxx_dialect < cxx0x)
14112 /* It must be a typedef-name or an enum-name. */
14113 return cp_parser_nonclass_name (parser);
14115 cp_parser_parse_tentatively (parser);
14116 /* It is either a simple-template-id representing an
14117 instantiation of an alias template... */
14118 type_decl = cp_parser_template_id (parser,
14119 /*template_keyword_p=*/false,
14120 /*check_dependency_p=*/false,
14121 none_type,
14122 /*is_declaration=*/false);
14123 /* Note that this must be an instantiation of an alias template
14124 because [temp.names]/6 says:
14126 A template-id that names an alias template specialization
14127 is a type-name.
14129 Whereas [temp.names]/7 says:
14131 A simple-template-id that names a class template
14132 specialization is a class-name. */
14133 if (type_decl != NULL_TREE
14134 && TREE_CODE (type_decl) == TYPE_DECL
14135 && TYPE_DECL_ALIAS_P (type_decl))
14136 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14137 else
14138 cp_parser_simulate_error (parser);
14140 if (!cp_parser_parse_definitely (parser))
14141 /* ... Or a typedef-name or an enum-name. */
14142 return cp_parser_nonclass_name (parser);
14145 return type_decl;
14148 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14150 enum-name:
14151 identifier
14153 typedef-name:
14154 identifier
14156 Returns a TYPE_DECL for the type. */
14158 static tree
14159 cp_parser_nonclass_name (cp_parser* parser)
14161 tree type_decl;
14162 tree identifier;
14164 cp_token *token = cp_lexer_peek_token (parser->lexer);
14165 identifier = cp_parser_identifier (parser);
14166 if (identifier == error_mark_node)
14167 return error_mark_node;
14169 /* Look up the type-name. */
14170 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14172 type_decl = strip_using_decl (type_decl);
14174 if (TREE_CODE (type_decl) != TYPE_DECL
14175 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14177 /* See if this is an Objective-C type. */
14178 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14179 tree type = objc_get_protocol_qualified_type (identifier, protos);
14180 if (type)
14181 type_decl = TYPE_NAME (type);
14184 /* Issue an error if we did not find a type-name. */
14185 if (TREE_CODE (type_decl) != TYPE_DECL
14186 /* In Objective-C, we have the complication that class names are
14187 normally type names and start declarations (eg, the
14188 "NSObject" in "NSObject *object;"), but can be used in an
14189 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14190 is an expression. So, a classname followed by a dot is not a
14191 valid type-name. */
14192 || (objc_is_class_name (TREE_TYPE (type_decl))
14193 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14195 if (!cp_parser_simulate_error (parser))
14196 cp_parser_name_lookup_error (parser, identifier, type_decl,
14197 NLE_TYPE, token->location);
14198 return error_mark_node;
14200 /* Remember that the name was used in the definition of the
14201 current class so that we can check later to see if the
14202 meaning would have been different after the class was
14203 entirely defined. */
14204 else if (type_decl != error_mark_node
14205 && !parser->scope)
14206 maybe_note_name_used_in_class (identifier, type_decl);
14208 return type_decl;
14211 /* Parse an elaborated-type-specifier. Note that the grammar given
14212 here incorporates the resolution to DR68.
14214 elaborated-type-specifier:
14215 class-key :: [opt] nested-name-specifier [opt] identifier
14216 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14217 enum-key :: [opt] nested-name-specifier [opt] identifier
14218 typename :: [opt] nested-name-specifier identifier
14219 typename :: [opt] nested-name-specifier template [opt]
14220 template-id
14222 GNU extension:
14224 elaborated-type-specifier:
14225 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14226 class-key attributes :: [opt] nested-name-specifier [opt]
14227 template [opt] template-id
14228 enum attributes :: [opt] nested-name-specifier [opt] identifier
14230 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14231 declared `friend'. If IS_DECLARATION is TRUE, then this
14232 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14233 something is being declared.
14235 Returns the TYPE specified. */
14237 static tree
14238 cp_parser_elaborated_type_specifier (cp_parser* parser,
14239 bool is_friend,
14240 bool is_declaration)
14242 enum tag_types tag_type;
14243 tree identifier;
14244 tree type = NULL_TREE;
14245 tree attributes = NULL_TREE;
14246 tree globalscope;
14247 cp_token *token = NULL;
14249 /* See if we're looking at the `enum' keyword. */
14250 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14252 /* Consume the `enum' token. */
14253 cp_lexer_consume_token (parser->lexer);
14254 /* Remember that it's an enumeration type. */
14255 tag_type = enum_type;
14256 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14257 enums) is used here. */
14258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14259 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14261 pedwarn (input_location, 0, "elaborated-type-specifier "
14262 "for a scoped enum must not use the %<%D%> keyword",
14263 cp_lexer_peek_token (parser->lexer)->u.value);
14264 /* Consume the `struct' or `class' and parse it anyway. */
14265 cp_lexer_consume_token (parser->lexer);
14267 /* Parse the attributes. */
14268 attributes = cp_parser_attributes_opt (parser);
14270 /* Or, it might be `typename'. */
14271 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14272 RID_TYPENAME))
14274 /* Consume the `typename' token. */
14275 cp_lexer_consume_token (parser->lexer);
14276 /* Remember that it's a `typename' type. */
14277 tag_type = typename_type;
14279 /* Otherwise it must be a class-key. */
14280 else
14282 tag_type = cp_parser_class_key (parser);
14283 if (tag_type == none_type)
14284 return error_mark_node;
14285 /* Parse the attributes. */
14286 attributes = cp_parser_attributes_opt (parser);
14289 /* Look for the `::' operator. */
14290 globalscope = cp_parser_global_scope_opt (parser,
14291 /*current_scope_valid_p=*/false);
14292 /* Look for the nested-name-specifier. */
14293 if (tag_type == typename_type && !globalscope)
14295 if (!cp_parser_nested_name_specifier (parser,
14296 /*typename_keyword_p=*/true,
14297 /*check_dependency_p=*/true,
14298 /*type_p=*/true,
14299 is_declaration))
14300 return error_mark_node;
14302 else
14303 /* Even though `typename' is not present, the proposed resolution
14304 to Core Issue 180 says that in `class A<T>::B', `B' should be
14305 considered a type-name, even if `A<T>' is dependent. */
14306 cp_parser_nested_name_specifier_opt (parser,
14307 /*typename_keyword_p=*/true,
14308 /*check_dependency_p=*/true,
14309 /*type_p=*/true,
14310 is_declaration);
14311 /* For everything but enumeration types, consider a template-id.
14312 For an enumeration type, consider only a plain identifier. */
14313 if (tag_type != enum_type)
14315 bool template_p = false;
14316 tree decl;
14318 /* Allow the `template' keyword. */
14319 template_p = cp_parser_optional_template_keyword (parser);
14320 /* If we didn't see `template', we don't know if there's a
14321 template-id or not. */
14322 if (!template_p)
14323 cp_parser_parse_tentatively (parser);
14324 /* Parse the template-id. */
14325 token = cp_lexer_peek_token (parser->lexer);
14326 decl = cp_parser_template_id (parser, template_p,
14327 /*check_dependency_p=*/true,
14328 tag_type,
14329 is_declaration);
14330 /* If we didn't find a template-id, look for an ordinary
14331 identifier. */
14332 if (!template_p && !cp_parser_parse_definitely (parser))
14334 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14335 in effect, then we must assume that, upon instantiation, the
14336 template will correspond to a class. */
14337 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14338 && tag_type == typename_type)
14339 type = make_typename_type (parser->scope, decl,
14340 typename_type,
14341 /*complain=*/tf_error);
14342 /* If the `typename' keyword is in effect and DECL is not a type
14343 decl, then type is non existent. */
14344 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14346 else if (TREE_CODE (decl) == TYPE_DECL)
14347 type = check_elaborated_type_specifier (tag_type, decl,
14348 /*allow_template_p=*/true);
14349 else if (decl == error_mark_node)
14350 type = error_mark_node;
14353 if (!type)
14355 token = cp_lexer_peek_token (parser->lexer);
14356 identifier = cp_parser_identifier (parser);
14358 if (identifier == error_mark_node)
14360 parser->scope = NULL_TREE;
14361 return error_mark_node;
14364 /* For a `typename', we needn't call xref_tag. */
14365 if (tag_type == typename_type
14366 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14367 return cp_parser_make_typename_type (parser, parser->scope,
14368 identifier,
14369 token->location);
14370 /* Look up a qualified name in the usual way. */
14371 if (parser->scope)
14373 tree decl;
14374 tree ambiguous_decls;
14376 decl = cp_parser_lookup_name (parser, identifier,
14377 tag_type,
14378 /*is_template=*/false,
14379 /*is_namespace=*/false,
14380 /*check_dependency=*/true,
14381 &ambiguous_decls,
14382 token->location);
14384 /* If the lookup was ambiguous, an error will already have been
14385 issued. */
14386 if (ambiguous_decls)
14387 return error_mark_node;
14389 /* If we are parsing friend declaration, DECL may be a
14390 TEMPLATE_DECL tree node here. However, we need to check
14391 whether this TEMPLATE_DECL results in valid code. Consider
14392 the following example:
14394 namespace N {
14395 template <class T> class C {};
14397 class X {
14398 template <class T> friend class N::C; // #1, valid code
14400 template <class T> class Y {
14401 friend class N::C; // #2, invalid code
14404 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14405 name lookup of `N::C'. We see that friend declaration must
14406 be template for the code to be valid. Note that
14407 processing_template_decl does not work here since it is
14408 always 1 for the above two cases. */
14410 decl = (cp_parser_maybe_treat_template_as_class
14411 (decl, /*tag_name_p=*/is_friend
14412 && parser->num_template_parameter_lists));
14414 if (TREE_CODE (decl) != TYPE_DECL)
14416 cp_parser_diagnose_invalid_type_name (parser,
14417 parser->scope,
14418 identifier,
14419 token->location);
14420 return error_mark_node;
14423 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14425 bool allow_template = (parser->num_template_parameter_lists
14426 || DECL_SELF_REFERENCE_P (decl));
14427 type = check_elaborated_type_specifier (tag_type, decl,
14428 allow_template);
14430 if (type == error_mark_node)
14431 return error_mark_node;
14434 /* Forward declarations of nested types, such as
14436 class C1::C2;
14437 class C1::C2::C3;
14439 are invalid unless all components preceding the final '::'
14440 are complete. If all enclosing types are complete, these
14441 declarations become merely pointless.
14443 Invalid forward declarations of nested types are errors
14444 caught elsewhere in parsing. Those that are pointless arrive
14445 here. */
14447 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14448 && !is_friend && !processing_explicit_instantiation)
14449 warning (0, "declaration %qD does not declare anything", decl);
14451 type = TREE_TYPE (decl);
14453 else
14455 /* An elaborated-type-specifier sometimes introduces a new type and
14456 sometimes names an existing type. Normally, the rule is that it
14457 introduces a new type only if there is not an existing type of
14458 the same name already in scope. For example, given:
14460 struct S {};
14461 void f() { struct S s; }
14463 the `struct S' in the body of `f' is the same `struct S' as in
14464 the global scope; the existing definition is used. However, if
14465 there were no global declaration, this would introduce a new
14466 local class named `S'.
14468 An exception to this rule applies to the following code:
14470 namespace N { struct S; }
14472 Here, the elaborated-type-specifier names a new type
14473 unconditionally; even if there is already an `S' in the
14474 containing scope this declaration names a new type.
14475 This exception only applies if the elaborated-type-specifier
14476 forms the complete declaration:
14478 [class.name]
14480 A declaration consisting solely of `class-key identifier ;' is
14481 either a redeclaration of the name in the current scope or a
14482 forward declaration of the identifier as a class name. It
14483 introduces the name into the current scope.
14485 We are in this situation precisely when the next token is a `;'.
14487 An exception to the exception is that a `friend' declaration does
14488 *not* name a new type; i.e., given:
14490 struct S { friend struct T; };
14492 `T' is not a new type in the scope of `S'.
14494 Also, `new struct S' or `sizeof (struct S)' never results in the
14495 definition of a new type; a new type can only be declared in a
14496 declaration context. */
14498 tag_scope ts;
14499 bool template_p;
14501 if (is_friend)
14502 /* Friends have special name lookup rules. */
14503 ts = ts_within_enclosing_non_class;
14504 else if (is_declaration
14505 && cp_lexer_next_token_is (parser->lexer,
14506 CPP_SEMICOLON))
14507 /* This is a `class-key identifier ;' */
14508 ts = ts_current;
14509 else
14510 ts = ts_global;
14512 template_p =
14513 (parser->num_template_parameter_lists
14514 && (cp_parser_next_token_starts_class_definition_p (parser)
14515 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14516 /* An unqualified name was used to reference this type, so
14517 there were no qualifying templates. */
14518 if (!cp_parser_check_template_parameters (parser,
14519 /*num_templates=*/0,
14520 token->location,
14521 /*declarator=*/NULL))
14522 return error_mark_node;
14523 type = xref_tag (tag_type, identifier, ts, template_p);
14527 if (type == error_mark_node)
14528 return error_mark_node;
14530 /* Allow attributes on forward declarations of classes. */
14531 if (attributes)
14533 if (TREE_CODE (type) == TYPENAME_TYPE)
14534 warning (OPT_Wattributes,
14535 "attributes ignored on uninstantiated type");
14536 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14537 && ! processing_explicit_instantiation)
14538 warning (OPT_Wattributes,
14539 "attributes ignored on template instantiation");
14540 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14541 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14542 else
14543 warning (OPT_Wattributes,
14544 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14547 if (tag_type != enum_type)
14549 /* Indicate whether this class was declared as a `class' or as a
14550 `struct'. */
14551 if (TREE_CODE (type) == RECORD_TYPE)
14552 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14553 cp_parser_check_class_key (tag_type, type);
14556 /* A "<" cannot follow an elaborated type specifier. If that
14557 happens, the user was probably trying to form a template-id. */
14558 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14559 token->location);
14561 return type;
14564 /* Parse an enum-specifier.
14566 enum-specifier:
14567 enum-head { enumerator-list [opt] }
14568 enum-head { enumerator-list , } [C++0x]
14570 enum-head:
14571 enum-key identifier [opt] enum-base [opt]
14572 enum-key nested-name-specifier identifier enum-base [opt]
14574 enum-key:
14575 enum
14576 enum class [C++0x]
14577 enum struct [C++0x]
14579 enum-base: [C++0x]
14580 : type-specifier-seq
14582 opaque-enum-specifier:
14583 enum-key identifier enum-base [opt] ;
14585 GNU Extensions:
14586 enum-key attributes[opt] identifier [opt] enum-base [opt]
14587 { enumerator-list [opt] }attributes[opt]
14588 enum-key attributes[opt] identifier [opt] enum-base [opt]
14589 { enumerator-list, }attributes[opt] [C++0x]
14591 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14592 if the token stream isn't an enum-specifier after all. */
14594 static tree
14595 cp_parser_enum_specifier (cp_parser* parser)
14597 tree identifier;
14598 tree type = NULL_TREE;
14599 tree prev_scope;
14600 tree nested_name_specifier = NULL_TREE;
14601 tree attributes;
14602 bool scoped_enum_p = false;
14603 bool has_underlying_type = false;
14604 bool nested_being_defined = false;
14605 bool new_value_list = false;
14606 bool is_new_type = false;
14607 bool is_anonymous = false;
14608 tree underlying_type = NULL_TREE;
14609 cp_token *type_start_token = NULL;
14610 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14612 parser->colon_corrects_to_scope_p = false;
14614 /* Parse tentatively so that we can back up if we don't find a
14615 enum-specifier. */
14616 cp_parser_parse_tentatively (parser);
14618 /* Caller guarantees that the current token is 'enum', an identifier
14619 possibly follows, and the token after that is an opening brace.
14620 If we don't have an identifier, fabricate an anonymous name for
14621 the enumeration being defined. */
14622 cp_lexer_consume_token (parser->lexer);
14624 /* Parse the "class" or "struct", which indicates a scoped
14625 enumeration type in C++0x. */
14626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14627 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14629 if (cxx_dialect < cxx0x)
14630 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14632 /* Consume the `struct' or `class' token. */
14633 cp_lexer_consume_token (parser->lexer);
14635 scoped_enum_p = true;
14638 attributes = cp_parser_attributes_opt (parser);
14640 /* Clear the qualification. */
14641 parser->scope = NULL_TREE;
14642 parser->qualifying_scope = NULL_TREE;
14643 parser->object_scope = NULL_TREE;
14645 /* Figure out in what scope the declaration is being placed. */
14646 prev_scope = current_scope ();
14648 type_start_token = cp_lexer_peek_token (parser->lexer);
14650 push_deferring_access_checks (dk_no_check);
14651 nested_name_specifier
14652 = cp_parser_nested_name_specifier_opt (parser,
14653 /*typename_keyword_p=*/true,
14654 /*check_dependency_p=*/false,
14655 /*type_p=*/false,
14656 /*is_declaration=*/false);
14658 if (nested_name_specifier)
14660 tree name;
14662 identifier = cp_parser_identifier (parser);
14663 name = cp_parser_lookup_name (parser, identifier,
14664 enum_type,
14665 /*is_template=*/false,
14666 /*is_namespace=*/false,
14667 /*check_dependency=*/true,
14668 /*ambiguous_decls=*/NULL,
14669 input_location);
14670 if (name && name != error_mark_node)
14672 type = TREE_TYPE (name);
14673 if (TREE_CODE (type) == TYPENAME_TYPE)
14675 /* Are template enums allowed in ISO? */
14676 if (template_parm_scope_p ())
14677 pedwarn (type_start_token->location, OPT_Wpedantic,
14678 "%qD is an enumeration template", name);
14679 /* ignore a typename reference, for it will be solved by name
14680 in start_enum. */
14681 type = NULL_TREE;
14684 else
14685 error_at (type_start_token->location,
14686 "%qD is not an enumerator-name", identifier);
14688 else
14690 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14691 identifier = cp_parser_identifier (parser);
14692 else
14694 identifier = make_anon_name ();
14695 is_anonymous = true;
14698 pop_deferring_access_checks ();
14700 /* Check for the `:' that denotes a specified underlying type in C++0x.
14701 Note that a ':' could also indicate a bitfield width, however. */
14702 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14704 cp_decl_specifier_seq type_specifiers;
14706 /* Consume the `:'. */
14707 cp_lexer_consume_token (parser->lexer);
14709 /* Parse the type-specifier-seq. */
14710 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14711 /*is_trailing_return=*/false,
14712 &type_specifiers);
14714 /* At this point this is surely not elaborated type specifier. */
14715 if (!cp_parser_parse_definitely (parser))
14716 return NULL_TREE;
14718 if (cxx_dialect < cxx0x)
14719 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14721 has_underlying_type = true;
14723 /* If that didn't work, stop. */
14724 if (type_specifiers.type != error_mark_node)
14726 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14727 /*initialized=*/0, NULL);
14728 if (underlying_type == error_mark_node
14729 || check_for_bare_parameter_packs (underlying_type))
14730 underlying_type = NULL_TREE;
14734 /* Look for the `{' but don't consume it yet. */
14735 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14737 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14739 cp_parser_error (parser, "expected %<{%>");
14740 if (has_underlying_type)
14742 type = NULL_TREE;
14743 goto out;
14746 /* An opaque-enum-specifier must have a ';' here. */
14747 if ((scoped_enum_p || underlying_type)
14748 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14750 cp_parser_error (parser, "expected %<;%> or %<{%>");
14751 if (has_underlying_type)
14753 type = NULL_TREE;
14754 goto out;
14759 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14760 return NULL_TREE;
14762 if (nested_name_specifier)
14764 if (CLASS_TYPE_P (nested_name_specifier))
14766 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14767 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14768 push_scope (nested_name_specifier);
14770 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14772 push_nested_namespace (nested_name_specifier);
14776 /* Issue an error message if type-definitions are forbidden here. */
14777 if (!cp_parser_check_type_definition (parser))
14778 type = error_mark_node;
14779 else
14780 /* Create the new type. We do this before consuming the opening
14781 brace so the enum will be recorded as being on the line of its
14782 tag (or the 'enum' keyword, if there is no tag). */
14783 type = start_enum (identifier, type, underlying_type,
14784 scoped_enum_p, &is_new_type);
14786 /* If the next token is not '{' it is an opaque-enum-specifier or an
14787 elaborated-type-specifier. */
14788 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14790 timevar_push (TV_PARSE_ENUM);
14791 if (nested_name_specifier)
14793 /* The following catches invalid code such as:
14794 enum class S<int>::E { A, B, C }; */
14795 if (!processing_specialization
14796 && CLASS_TYPE_P (nested_name_specifier)
14797 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14798 error_at (type_start_token->location, "cannot add an enumerator "
14799 "list to a template instantiation");
14801 /* If that scope does not contain the scope in which the
14802 class was originally declared, the program is invalid. */
14803 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14805 if (at_namespace_scope_p ())
14806 error_at (type_start_token->location,
14807 "declaration of %qD in namespace %qD which does not "
14808 "enclose %qD",
14809 type, prev_scope, nested_name_specifier);
14810 else
14811 error_at (type_start_token->location,
14812 "declaration of %qD in %qD which does not enclose %qD",
14813 type, prev_scope, nested_name_specifier);
14814 type = error_mark_node;
14818 if (scoped_enum_p)
14819 begin_scope (sk_scoped_enum, type);
14821 /* Consume the opening brace. */
14822 cp_lexer_consume_token (parser->lexer);
14824 if (type == error_mark_node)
14825 ; /* Nothing to add */
14826 else if (OPAQUE_ENUM_P (type)
14827 || (cxx_dialect > cxx98 && processing_specialization))
14829 new_value_list = true;
14830 SET_OPAQUE_ENUM_P (type, false);
14831 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14833 else
14835 error_at (type_start_token->location, "multiple definition of %q#T", type);
14836 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14837 "previous definition here");
14838 type = error_mark_node;
14841 if (type == error_mark_node)
14842 cp_parser_skip_to_end_of_block_or_statement (parser);
14843 /* If the next token is not '}', then there are some enumerators. */
14844 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14845 cp_parser_enumerator_list (parser, type);
14847 /* Consume the final '}'. */
14848 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14850 if (scoped_enum_p)
14851 finish_scope ();
14852 timevar_pop (TV_PARSE_ENUM);
14854 else
14856 /* If a ';' follows, then it is an opaque-enum-specifier
14857 and additional restrictions apply. */
14858 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14860 if (is_anonymous)
14861 error_at (type_start_token->location,
14862 "opaque-enum-specifier without name");
14863 else if (nested_name_specifier)
14864 error_at (type_start_token->location,
14865 "opaque-enum-specifier must use a simple identifier");
14869 /* Look for trailing attributes to apply to this enumeration, and
14870 apply them if appropriate. */
14871 if (cp_parser_allow_gnu_extensions_p (parser))
14873 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14874 trailing_attr = chainon (trailing_attr, attributes);
14875 cplus_decl_attributes (&type,
14876 trailing_attr,
14877 (int) ATTR_FLAG_TYPE_IN_PLACE);
14880 /* Finish up the enumeration. */
14881 if (type != error_mark_node)
14883 if (new_value_list)
14884 finish_enum_value_list (type);
14885 if (is_new_type)
14886 finish_enum (type);
14889 if (nested_name_specifier)
14891 if (CLASS_TYPE_P (nested_name_specifier))
14893 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14894 pop_scope (nested_name_specifier);
14896 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14898 pop_nested_namespace (nested_name_specifier);
14901 out:
14902 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14903 return type;
14906 /* Parse an enumerator-list. The enumerators all have the indicated
14907 TYPE.
14909 enumerator-list:
14910 enumerator-definition
14911 enumerator-list , enumerator-definition */
14913 static void
14914 cp_parser_enumerator_list (cp_parser* parser, tree type)
14916 while (true)
14918 /* Parse an enumerator-definition. */
14919 cp_parser_enumerator_definition (parser, type);
14921 /* If the next token is not a ',', we've reached the end of
14922 the list. */
14923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14924 break;
14925 /* Otherwise, consume the `,' and keep going. */
14926 cp_lexer_consume_token (parser->lexer);
14927 /* If the next token is a `}', there is a trailing comma. */
14928 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14930 if (cxx_dialect < cxx0x && !in_system_header)
14931 pedwarn (input_location, OPT_Wpedantic,
14932 "comma at end of enumerator list");
14933 break;
14938 /* Parse an enumerator-definition. The enumerator has the indicated
14939 TYPE.
14941 enumerator-definition:
14942 enumerator
14943 enumerator = constant-expression
14945 enumerator:
14946 identifier */
14948 static void
14949 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14951 tree identifier;
14952 tree value;
14953 location_t loc;
14955 /* Save the input location because we are interested in the location
14956 of the identifier and not the location of the explicit value. */
14957 loc = cp_lexer_peek_token (parser->lexer)->location;
14959 /* Look for the identifier. */
14960 identifier = cp_parser_identifier (parser);
14961 if (identifier == error_mark_node)
14962 return;
14964 /* If the next token is an '=', then there is an explicit value. */
14965 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14967 /* Consume the `=' token. */
14968 cp_lexer_consume_token (parser->lexer);
14969 /* Parse the value. */
14970 value = cp_parser_constant_expression (parser,
14971 /*allow_non_constant_p=*/false,
14972 NULL);
14974 else
14975 value = NULL_TREE;
14977 /* If we are processing a template, make sure the initializer of the
14978 enumerator doesn't contain any bare template parameter pack. */
14979 if (check_for_bare_parameter_packs (value))
14980 value = error_mark_node;
14982 /* integral_constant_value will pull out this expression, so make sure
14983 it's folded as appropriate. */
14984 value = fold_non_dependent_expr (value);
14986 /* Create the enumerator. */
14987 build_enumerator (identifier, value, type, loc);
14990 /* Parse a namespace-name.
14992 namespace-name:
14993 original-namespace-name
14994 namespace-alias
14996 Returns the NAMESPACE_DECL for the namespace. */
14998 static tree
14999 cp_parser_namespace_name (cp_parser* parser)
15001 tree identifier;
15002 tree namespace_decl;
15004 cp_token *token = cp_lexer_peek_token (parser->lexer);
15006 /* Get the name of the namespace. */
15007 identifier = cp_parser_identifier (parser);
15008 if (identifier == error_mark_node)
15009 return error_mark_node;
15011 /* Look up the identifier in the currently active scope. Look only
15012 for namespaces, due to:
15014 [basic.lookup.udir]
15016 When looking up a namespace-name in a using-directive or alias
15017 definition, only namespace names are considered.
15019 And:
15021 [basic.lookup.qual]
15023 During the lookup of a name preceding the :: scope resolution
15024 operator, object, function, and enumerator names are ignored.
15026 (Note that cp_parser_qualifying_entity only calls this
15027 function if the token after the name is the scope resolution
15028 operator.) */
15029 namespace_decl = cp_parser_lookup_name (parser, identifier,
15030 none_type,
15031 /*is_template=*/false,
15032 /*is_namespace=*/true,
15033 /*check_dependency=*/true,
15034 /*ambiguous_decls=*/NULL,
15035 token->location);
15036 /* If it's not a namespace, issue an error. */
15037 if (namespace_decl == error_mark_node
15038 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15040 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15041 error_at (token->location, "%qD is not a namespace-name", identifier);
15042 cp_parser_error (parser, "expected namespace-name");
15043 namespace_decl = error_mark_node;
15046 return namespace_decl;
15049 /* Parse a namespace-definition.
15051 namespace-definition:
15052 named-namespace-definition
15053 unnamed-namespace-definition
15055 named-namespace-definition:
15056 original-namespace-definition
15057 extension-namespace-definition
15059 original-namespace-definition:
15060 namespace identifier { namespace-body }
15062 extension-namespace-definition:
15063 namespace original-namespace-name { namespace-body }
15065 unnamed-namespace-definition:
15066 namespace { namespace-body } */
15068 static void
15069 cp_parser_namespace_definition (cp_parser* parser)
15071 tree identifier, attribs;
15072 bool has_visibility;
15073 bool is_inline;
15075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15077 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15078 is_inline = true;
15079 cp_lexer_consume_token (parser->lexer);
15081 else
15082 is_inline = false;
15084 /* Look for the `namespace' keyword. */
15085 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15087 /* Get the name of the namespace. We do not attempt to distinguish
15088 between an original-namespace-definition and an
15089 extension-namespace-definition at this point. The semantic
15090 analysis routines are responsible for that. */
15091 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15092 identifier = cp_parser_identifier (parser);
15093 else
15094 identifier = NULL_TREE;
15096 /* Parse any specified attributes. */
15097 attribs = cp_parser_attributes_opt (parser);
15099 /* Look for the `{' to start the namespace. */
15100 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15101 /* Start the namespace. */
15102 push_namespace (identifier);
15104 /* "inline namespace" is equivalent to a stub namespace definition
15105 followed by a strong using directive. */
15106 if (is_inline)
15108 tree name_space = current_namespace;
15109 /* Set up namespace association. */
15110 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15111 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15112 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15113 /* Import the contents of the inline namespace. */
15114 pop_namespace ();
15115 do_using_directive (name_space);
15116 push_namespace (identifier);
15119 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15121 /* Parse the body of the namespace. */
15122 cp_parser_namespace_body (parser);
15124 if (has_visibility)
15125 pop_visibility (1);
15127 /* Finish the namespace. */
15128 pop_namespace ();
15129 /* Look for the final `}'. */
15130 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15133 /* Parse a namespace-body.
15135 namespace-body:
15136 declaration-seq [opt] */
15138 static void
15139 cp_parser_namespace_body (cp_parser* parser)
15141 cp_parser_declaration_seq_opt (parser);
15144 /* Parse a namespace-alias-definition.
15146 namespace-alias-definition:
15147 namespace identifier = qualified-namespace-specifier ; */
15149 static void
15150 cp_parser_namespace_alias_definition (cp_parser* parser)
15152 tree identifier;
15153 tree namespace_specifier;
15155 cp_token *token = cp_lexer_peek_token (parser->lexer);
15157 /* Look for the `namespace' keyword. */
15158 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15159 /* Look for the identifier. */
15160 identifier = cp_parser_identifier (parser);
15161 if (identifier == error_mark_node)
15162 return;
15163 /* Look for the `=' token. */
15164 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15165 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15167 error_at (token->location, "%<namespace%> definition is not allowed here");
15168 /* Skip the definition. */
15169 cp_lexer_consume_token (parser->lexer);
15170 if (cp_parser_skip_to_closing_brace (parser))
15171 cp_lexer_consume_token (parser->lexer);
15172 return;
15174 cp_parser_require (parser, CPP_EQ, RT_EQ);
15175 /* Look for the qualified-namespace-specifier. */
15176 namespace_specifier
15177 = cp_parser_qualified_namespace_specifier (parser);
15178 /* Look for the `;' token. */
15179 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15181 /* Register the alias in the symbol table. */
15182 do_namespace_alias (identifier, namespace_specifier);
15185 /* Parse a qualified-namespace-specifier.
15187 qualified-namespace-specifier:
15188 :: [opt] nested-name-specifier [opt] namespace-name
15190 Returns a NAMESPACE_DECL corresponding to the specified
15191 namespace. */
15193 static tree
15194 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15196 /* Look for the optional `::'. */
15197 cp_parser_global_scope_opt (parser,
15198 /*current_scope_valid_p=*/false);
15200 /* Look for the optional nested-name-specifier. */
15201 cp_parser_nested_name_specifier_opt (parser,
15202 /*typename_keyword_p=*/false,
15203 /*check_dependency_p=*/true,
15204 /*type_p=*/false,
15205 /*is_declaration=*/true);
15207 return cp_parser_namespace_name (parser);
15210 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15211 access declaration.
15213 using-declaration:
15214 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15215 using :: unqualified-id ;
15217 access-declaration:
15218 qualified-id ;
15222 static bool
15223 cp_parser_using_declaration (cp_parser* parser,
15224 bool access_declaration_p)
15226 cp_token *token;
15227 bool typename_p = false;
15228 bool global_scope_p;
15229 tree decl;
15230 tree identifier;
15231 tree qscope;
15232 int oldcount = errorcount;
15233 cp_token *diag_token = NULL;
15235 if (access_declaration_p)
15237 diag_token = cp_lexer_peek_token (parser->lexer);
15238 cp_parser_parse_tentatively (parser);
15240 else
15242 /* Look for the `using' keyword. */
15243 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15245 /* Peek at the next token. */
15246 token = cp_lexer_peek_token (parser->lexer);
15247 /* See if it's `typename'. */
15248 if (token->keyword == RID_TYPENAME)
15250 /* Remember that we've seen it. */
15251 typename_p = true;
15252 /* Consume the `typename' token. */
15253 cp_lexer_consume_token (parser->lexer);
15257 /* Look for the optional global scope qualification. */
15258 global_scope_p
15259 = (cp_parser_global_scope_opt (parser,
15260 /*current_scope_valid_p=*/false)
15261 != NULL_TREE);
15263 /* If we saw `typename', or didn't see `::', then there must be a
15264 nested-name-specifier present. */
15265 if (typename_p || !global_scope_p)
15266 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15267 /*check_dependency_p=*/true,
15268 /*type_p=*/false,
15269 /*is_declaration=*/true);
15270 /* Otherwise, we could be in either of the two productions. In that
15271 case, treat the nested-name-specifier as optional. */
15272 else
15273 qscope = cp_parser_nested_name_specifier_opt (parser,
15274 /*typename_keyword_p=*/false,
15275 /*check_dependency_p=*/true,
15276 /*type_p=*/false,
15277 /*is_declaration=*/true);
15278 if (!qscope)
15279 qscope = global_namespace;
15281 if (access_declaration_p && cp_parser_error_occurred (parser))
15282 /* Something has already gone wrong; there's no need to parse
15283 further. Since an error has occurred, the return value of
15284 cp_parser_parse_definitely will be false, as required. */
15285 return cp_parser_parse_definitely (parser);
15287 token = cp_lexer_peek_token (parser->lexer);
15288 /* Parse the unqualified-id. */
15289 identifier = cp_parser_unqualified_id (parser,
15290 /*template_keyword_p=*/false,
15291 /*check_dependency_p=*/true,
15292 /*declarator_p=*/true,
15293 /*optional_p=*/false);
15295 if (access_declaration_p)
15297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15298 cp_parser_simulate_error (parser);
15299 if (!cp_parser_parse_definitely (parser))
15300 return false;
15303 /* The function we call to handle a using-declaration is different
15304 depending on what scope we are in. */
15305 if (qscope == error_mark_node || identifier == error_mark_node)
15307 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15308 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15309 /* [namespace.udecl]
15311 A using declaration shall not name a template-id. */
15312 error_at (token->location,
15313 "a template-id may not appear in a using-declaration");
15314 else
15316 if (at_class_scope_p ())
15318 /* Create the USING_DECL. */
15319 decl = do_class_using_decl (parser->scope, identifier);
15321 if (decl && typename_p)
15322 USING_DECL_TYPENAME_P (decl) = 1;
15324 if (check_for_bare_parameter_packs (decl))
15325 return false;
15326 else
15327 /* Add it to the list of members in this class. */
15328 finish_member_declaration (decl);
15330 else
15332 decl = cp_parser_lookup_name_simple (parser,
15333 identifier,
15334 token->location);
15335 if (decl == error_mark_node)
15336 cp_parser_name_lookup_error (parser, identifier,
15337 decl, NLE_NULL,
15338 token->location);
15339 else if (check_for_bare_parameter_packs (decl))
15340 return false;
15341 else if (!at_namespace_scope_p ())
15342 do_local_using_decl (decl, qscope, identifier);
15343 else
15344 do_toplevel_using_decl (decl, qscope, identifier);
15348 /* Look for the final `;'. */
15349 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15351 if (access_declaration_p && errorcount == oldcount)
15352 warning_at (diag_token->location, OPT_Wdeprecated,
15353 "access declarations are deprecated "
15354 "in favour of using-declarations; "
15355 "suggestion: add the %<using%> keyword");
15357 return true;
15360 /* Parse an alias-declaration.
15362 alias-declaration:
15363 using identifier attribute-specifier-seq [opt] = type-id */
15365 static tree
15366 cp_parser_alias_declaration (cp_parser* parser)
15368 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15369 location_t id_location;
15370 cp_declarator *declarator;
15371 cp_decl_specifier_seq decl_specs;
15372 bool member_p;
15373 const char *saved_message = NULL;
15375 /* Look for the `using' keyword. */
15376 cp_token *using_token
15377 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15378 if (using_token == NULL)
15379 return error_mark_node;
15381 id_location = cp_lexer_peek_token (parser->lexer)->location;
15382 id = cp_parser_identifier (parser);
15383 if (id == error_mark_node)
15384 return error_mark_node;
15386 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15387 attributes = cp_parser_attributes_opt (parser);
15388 if (attributes == error_mark_node)
15389 return error_mark_node;
15391 cp_parser_require (parser, CPP_EQ, RT_EQ);
15393 if (cp_parser_error_occurred (parser))
15394 return error_mark_node;
15396 cp_parser_commit_to_tentative_parse (parser);
15398 /* Now we are going to parse the type-id of the declaration. */
15401 [dcl.type]/3 says:
15403 "A type-specifier-seq shall not define a class or enumeration
15404 unless it appears in the type-id of an alias-declaration (7.1.3) that
15405 is not the declaration of a template-declaration."
15407 In other words, if we currently are in an alias template, the
15408 type-id should not define a type.
15410 So let's set parser->type_definition_forbidden_message in that
15411 case; cp_parser_check_type_definition (called by
15412 cp_parser_class_specifier) will then emit an error if a type is
15413 defined in the type-id. */
15414 if (parser->num_template_parameter_lists)
15416 saved_message = parser->type_definition_forbidden_message;
15417 parser->type_definition_forbidden_message =
15418 G_("types may not be defined in alias template declarations");
15421 type = cp_parser_type_id (parser);
15423 /* Restore the error message if need be. */
15424 if (parser->num_template_parameter_lists)
15425 parser->type_definition_forbidden_message = saved_message;
15427 if (type == error_mark_node)
15429 cp_parser_skip_to_end_of_block_or_statement (parser);
15430 return error_mark_node;
15433 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15435 if (cp_parser_error_occurred (parser))
15437 cp_parser_skip_to_end_of_block_or_statement (parser);
15438 return error_mark_node;
15441 /* A typedef-name can also be introduced by an alias-declaration. The
15442 identifier following the using keyword becomes a typedef-name. It has
15443 the same semantics as if it were introduced by the typedef
15444 specifier. In particular, it does not define a new type and it shall
15445 not appear in the type-id. */
15447 clear_decl_specs (&decl_specs);
15448 decl_specs.type = type;
15449 if (attributes != NULL_TREE)
15451 decl_specs.attributes = attributes;
15452 set_and_check_decl_spec_loc (&decl_specs,
15453 ds_attribute,
15454 attrs_token);
15456 set_and_check_decl_spec_loc (&decl_specs,
15457 ds_typedef,
15458 using_token);
15459 set_and_check_decl_spec_loc (&decl_specs,
15460 ds_alias,
15461 using_token);
15463 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15464 declarator->id_loc = id_location;
15466 member_p = at_class_scope_p ();
15467 if (member_p)
15468 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15469 NULL_TREE, attributes);
15470 else
15471 decl = start_decl (declarator, &decl_specs, 0,
15472 attributes, NULL_TREE, &pushed_scope);
15473 if (decl == error_mark_node)
15474 return decl;
15476 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15478 if (pushed_scope)
15479 pop_scope (pushed_scope);
15481 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15482 added into the symbol table; otherwise, return the TYPE_DECL. */
15483 if (DECL_LANG_SPECIFIC (decl)
15484 && DECL_TEMPLATE_INFO (decl)
15485 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15487 decl = DECL_TI_TEMPLATE (decl);
15488 if (member_p)
15489 check_member_template (decl);
15492 return decl;
15495 /* Parse a using-directive.
15497 using-directive:
15498 using namespace :: [opt] nested-name-specifier [opt]
15499 namespace-name ; */
15501 static void
15502 cp_parser_using_directive (cp_parser* parser)
15504 tree namespace_decl;
15505 tree attribs;
15507 /* Look for the `using' keyword. */
15508 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15509 /* And the `namespace' keyword. */
15510 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15511 /* Look for the optional `::' operator. */
15512 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15513 /* And the optional nested-name-specifier. */
15514 cp_parser_nested_name_specifier_opt (parser,
15515 /*typename_keyword_p=*/false,
15516 /*check_dependency_p=*/true,
15517 /*type_p=*/false,
15518 /*is_declaration=*/true);
15519 /* Get the namespace being used. */
15520 namespace_decl = cp_parser_namespace_name (parser);
15521 /* And any specified attributes. */
15522 attribs = cp_parser_attributes_opt (parser);
15523 /* Update the symbol table. */
15524 parse_using_directive (namespace_decl, attribs);
15525 /* Look for the final `;'. */
15526 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15529 /* Parse an asm-definition.
15531 asm-definition:
15532 asm ( string-literal ) ;
15534 GNU Extension:
15536 asm-definition:
15537 asm volatile [opt] ( string-literal ) ;
15538 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15539 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15540 : asm-operand-list [opt] ) ;
15541 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15542 : asm-operand-list [opt]
15543 : asm-clobber-list [opt] ) ;
15544 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15545 : asm-clobber-list [opt]
15546 : asm-goto-list ) ; */
15548 static void
15549 cp_parser_asm_definition (cp_parser* parser)
15551 tree string;
15552 tree outputs = NULL_TREE;
15553 tree inputs = NULL_TREE;
15554 tree clobbers = NULL_TREE;
15555 tree labels = NULL_TREE;
15556 tree asm_stmt;
15557 bool volatile_p = false;
15558 bool extended_p = false;
15559 bool invalid_inputs_p = false;
15560 bool invalid_outputs_p = false;
15561 bool goto_p = false;
15562 required_token missing = RT_NONE;
15564 /* Look for the `asm' keyword. */
15565 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15566 /* See if the next token is `volatile'. */
15567 if (cp_parser_allow_gnu_extensions_p (parser)
15568 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15570 /* Remember that we saw the `volatile' keyword. */
15571 volatile_p = true;
15572 /* Consume the token. */
15573 cp_lexer_consume_token (parser->lexer);
15575 if (cp_parser_allow_gnu_extensions_p (parser)
15576 && parser->in_function_body
15577 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15579 /* Remember that we saw the `goto' keyword. */
15580 goto_p = true;
15581 /* Consume the token. */
15582 cp_lexer_consume_token (parser->lexer);
15584 /* Look for the opening `('. */
15585 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15586 return;
15587 /* Look for the string. */
15588 string = cp_parser_string_literal (parser, false, false);
15589 if (string == error_mark_node)
15591 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15592 /*consume_paren=*/true);
15593 return;
15596 /* If we're allowing GNU extensions, check for the extended assembly
15597 syntax. Unfortunately, the `:' tokens need not be separated by
15598 a space in C, and so, for compatibility, we tolerate that here
15599 too. Doing that means that we have to treat the `::' operator as
15600 two `:' tokens. */
15601 if (cp_parser_allow_gnu_extensions_p (parser)
15602 && parser->in_function_body
15603 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15604 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15606 bool inputs_p = false;
15607 bool clobbers_p = false;
15608 bool labels_p = false;
15610 /* The extended syntax was used. */
15611 extended_p = true;
15613 /* Look for outputs. */
15614 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15616 /* Consume the `:'. */
15617 cp_lexer_consume_token (parser->lexer);
15618 /* Parse the output-operands. */
15619 if (cp_lexer_next_token_is_not (parser->lexer,
15620 CPP_COLON)
15621 && cp_lexer_next_token_is_not (parser->lexer,
15622 CPP_SCOPE)
15623 && cp_lexer_next_token_is_not (parser->lexer,
15624 CPP_CLOSE_PAREN)
15625 && !goto_p)
15626 outputs = cp_parser_asm_operand_list (parser);
15628 if (outputs == error_mark_node)
15629 invalid_outputs_p = true;
15631 /* If the next token is `::', there are no outputs, and the
15632 next token is the beginning of the inputs. */
15633 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15634 /* The inputs are coming next. */
15635 inputs_p = true;
15637 /* Look for inputs. */
15638 if (inputs_p
15639 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15641 /* Consume the `:' or `::'. */
15642 cp_lexer_consume_token (parser->lexer);
15643 /* Parse the output-operands. */
15644 if (cp_lexer_next_token_is_not (parser->lexer,
15645 CPP_COLON)
15646 && cp_lexer_next_token_is_not (parser->lexer,
15647 CPP_SCOPE)
15648 && cp_lexer_next_token_is_not (parser->lexer,
15649 CPP_CLOSE_PAREN))
15650 inputs = cp_parser_asm_operand_list (parser);
15652 if (inputs == error_mark_node)
15653 invalid_inputs_p = true;
15655 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15656 /* The clobbers are coming next. */
15657 clobbers_p = true;
15659 /* Look for clobbers. */
15660 if (clobbers_p
15661 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15663 clobbers_p = true;
15664 /* Consume the `:' or `::'. */
15665 cp_lexer_consume_token (parser->lexer);
15666 /* Parse the clobbers. */
15667 if (cp_lexer_next_token_is_not (parser->lexer,
15668 CPP_COLON)
15669 && cp_lexer_next_token_is_not (parser->lexer,
15670 CPP_CLOSE_PAREN))
15671 clobbers = cp_parser_asm_clobber_list (parser);
15673 else if (goto_p
15674 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15675 /* The labels are coming next. */
15676 labels_p = true;
15678 /* Look for labels. */
15679 if (labels_p
15680 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15682 labels_p = true;
15683 /* Consume the `:' or `::'. */
15684 cp_lexer_consume_token (parser->lexer);
15685 /* Parse the labels. */
15686 labels = cp_parser_asm_label_list (parser);
15689 if (goto_p && !labels_p)
15690 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15692 else if (goto_p)
15693 missing = RT_COLON_SCOPE;
15695 /* Look for the closing `)'. */
15696 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15697 missing ? missing : RT_CLOSE_PAREN))
15698 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15699 /*consume_paren=*/true);
15700 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15702 if (!invalid_inputs_p && !invalid_outputs_p)
15704 /* Create the ASM_EXPR. */
15705 if (parser->in_function_body)
15707 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15708 inputs, clobbers, labels);
15709 /* If the extended syntax was not used, mark the ASM_EXPR. */
15710 if (!extended_p)
15712 tree temp = asm_stmt;
15713 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15714 temp = TREE_OPERAND (temp, 0);
15716 ASM_INPUT_P (temp) = 1;
15719 else
15720 add_asm_node (string);
15724 /* Declarators [gram.dcl.decl] */
15726 /* Parse an init-declarator.
15728 init-declarator:
15729 declarator initializer [opt]
15731 GNU Extension:
15733 init-declarator:
15734 declarator asm-specification [opt] attributes [opt] initializer [opt]
15736 function-definition:
15737 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15738 function-body
15739 decl-specifier-seq [opt] declarator function-try-block
15741 GNU Extension:
15743 function-definition:
15744 __extension__ function-definition
15746 TM Extension:
15748 function-definition:
15749 decl-specifier-seq [opt] declarator function-transaction-block
15751 The DECL_SPECIFIERS apply to this declarator. Returns a
15752 representation of the entity declared. If MEMBER_P is TRUE, then
15753 this declarator appears in a class scope. The new DECL created by
15754 this declarator is returned.
15756 The CHECKS are access checks that should be performed once we know
15757 what entity is being declared (and, therefore, what classes have
15758 befriended it).
15760 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15761 for a function-definition here as well. If the declarator is a
15762 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15763 be TRUE upon return. By that point, the function-definition will
15764 have been completely parsed.
15766 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15767 is FALSE.
15769 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15770 parsed declaration if it is an uninitialized single declarator not followed
15771 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15772 if present, will not be consumed. If returned, this declarator will be
15773 created with SD_INITIALIZED but will not call cp_finish_decl. */
15775 static tree
15776 cp_parser_init_declarator (cp_parser* parser,
15777 cp_decl_specifier_seq *decl_specifiers,
15778 vec<deferred_access_check, va_gc> *checks,
15779 bool function_definition_allowed_p,
15780 bool member_p,
15781 int declares_class_or_enum,
15782 bool* function_definition_p,
15783 tree* maybe_range_for_decl)
15785 cp_token *token = NULL, *asm_spec_start_token = NULL,
15786 *attributes_start_token = NULL;
15787 cp_declarator *declarator;
15788 tree prefix_attributes;
15789 tree attributes = NULL;
15790 tree asm_specification;
15791 tree initializer;
15792 tree decl = NULL_TREE;
15793 tree scope;
15794 int is_initialized;
15795 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15796 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15797 "(...)". */
15798 enum cpp_ttype initialization_kind;
15799 bool is_direct_init = false;
15800 bool is_non_constant_init;
15801 int ctor_dtor_or_conv_p;
15802 bool friend_p;
15803 tree pushed_scope = NULL_TREE;
15804 bool range_for_decl_p = false;
15806 /* Gather the attributes that were provided with the
15807 decl-specifiers. */
15808 prefix_attributes = decl_specifiers->attributes;
15810 /* Assume that this is not the declarator for a function
15811 definition. */
15812 if (function_definition_p)
15813 *function_definition_p = false;
15815 /* Defer access checks while parsing the declarator; we cannot know
15816 what names are accessible until we know what is being
15817 declared. */
15818 resume_deferring_access_checks ();
15820 /* Parse the declarator. */
15821 token = cp_lexer_peek_token (parser->lexer);
15822 declarator
15823 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15824 &ctor_dtor_or_conv_p,
15825 /*parenthesized_p=*/NULL,
15826 member_p);
15827 /* Gather up the deferred checks. */
15828 stop_deferring_access_checks ();
15830 /* If the DECLARATOR was erroneous, there's no need to go
15831 further. */
15832 if (declarator == cp_error_declarator)
15833 return error_mark_node;
15835 /* Check that the number of template-parameter-lists is OK. */
15836 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15837 token->location))
15838 return error_mark_node;
15840 if (declares_class_or_enum & 2)
15841 cp_parser_check_for_definition_in_return_type (declarator,
15842 decl_specifiers->type,
15843 decl_specifiers->locations[ds_type_spec]);
15845 /* Figure out what scope the entity declared by the DECLARATOR is
15846 located in. `grokdeclarator' sometimes changes the scope, so
15847 we compute it now. */
15848 scope = get_scope_of_declarator (declarator);
15850 /* Perform any lookups in the declared type which were thought to be
15851 dependent, but are not in the scope of the declarator. */
15852 decl_specifiers->type
15853 = maybe_update_decl_type (decl_specifiers->type, scope);
15855 /* If we're allowing GNU extensions, look for an
15856 asm-specification. */
15857 if (cp_parser_allow_gnu_extensions_p (parser))
15859 /* Look for an asm-specification. */
15860 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15861 asm_specification = cp_parser_asm_specification_opt (parser);
15863 else
15864 asm_specification = NULL_TREE;
15866 /* Look for attributes. */
15867 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15868 attributes = cp_parser_attributes_opt (parser);
15870 /* Peek at the next token. */
15871 token = cp_lexer_peek_token (parser->lexer);
15872 /* Check to see if the token indicates the start of a
15873 function-definition. */
15874 if (function_declarator_p (declarator)
15875 && cp_parser_token_starts_function_definition_p (token))
15877 if (!function_definition_allowed_p)
15879 /* If a function-definition should not appear here, issue an
15880 error message. */
15881 cp_parser_error (parser,
15882 "a function-definition is not allowed here");
15883 return error_mark_node;
15885 else
15887 location_t func_brace_location
15888 = cp_lexer_peek_token (parser->lexer)->location;
15890 /* Neither attributes nor an asm-specification are allowed
15891 on a function-definition. */
15892 if (asm_specification)
15893 error_at (asm_spec_start_token->location,
15894 "an asm-specification is not allowed "
15895 "on a function-definition");
15896 if (attributes)
15897 error_at (attributes_start_token->location,
15898 "attributes are not allowed on a function-definition");
15899 /* This is a function-definition. */
15900 *function_definition_p = true;
15902 /* Parse the function definition. */
15903 if (member_p)
15904 decl = cp_parser_save_member_function_body (parser,
15905 decl_specifiers,
15906 declarator,
15907 prefix_attributes);
15908 else
15909 decl
15910 = (cp_parser_function_definition_from_specifiers_and_declarator
15911 (parser, decl_specifiers, prefix_attributes, declarator));
15913 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15915 /* This is where the prologue starts... */
15916 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15917 = func_brace_location;
15920 return decl;
15924 /* [dcl.dcl]
15926 Only in function declarations for constructors, destructors, and
15927 type conversions can the decl-specifier-seq be omitted.
15929 We explicitly postpone this check past the point where we handle
15930 function-definitions because we tolerate function-definitions
15931 that are missing their return types in some modes. */
15932 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15934 cp_parser_error (parser,
15935 "expected constructor, destructor, or type conversion");
15936 return error_mark_node;
15939 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15940 if (token->type == CPP_EQ
15941 || token->type == CPP_OPEN_PAREN
15942 || token->type == CPP_OPEN_BRACE)
15944 is_initialized = SD_INITIALIZED;
15945 initialization_kind = token->type;
15946 if (maybe_range_for_decl)
15947 *maybe_range_for_decl = error_mark_node;
15949 if (token->type == CPP_EQ
15950 && function_declarator_p (declarator))
15952 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15953 if (t2->keyword == RID_DEFAULT)
15954 is_initialized = SD_DEFAULTED;
15955 else if (t2->keyword == RID_DELETE)
15956 is_initialized = SD_DELETED;
15959 else
15961 /* If the init-declarator isn't initialized and isn't followed by a
15962 `,' or `;', it's not a valid init-declarator. */
15963 if (token->type != CPP_COMMA
15964 && token->type != CPP_SEMICOLON)
15966 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15967 range_for_decl_p = true;
15968 else
15970 cp_parser_error (parser, "expected initializer");
15971 return error_mark_node;
15974 is_initialized = SD_UNINITIALIZED;
15975 initialization_kind = CPP_EOF;
15978 /* Because start_decl has side-effects, we should only call it if we
15979 know we're going ahead. By this point, we know that we cannot
15980 possibly be looking at any other construct. */
15981 cp_parser_commit_to_tentative_parse (parser);
15983 /* If the decl specifiers were bad, issue an error now that we're
15984 sure this was intended to be a declarator. Then continue
15985 declaring the variable(s), as int, to try to cut down on further
15986 errors. */
15987 if (decl_specifiers->any_specifiers_p
15988 && decl_specifiers->type == error_mark_node)
15990 cp_parser_error (parser, "invalid type in declaration");
15991 decl_specifiers->type = integer_type_node;
15994 /* Check to see whether or not this declaration is a friend. */
15995 friend_p = cp_parser_friend_p (decl_specifiers);
15997 /* Enter the newly declared entry in the symbol table. If we're
15998 processing a declaration in a class-specifier, we wait until
15999 after processing the initializer. */
16000 if (!member_p)
16002 if (parser->in_unbraced_linkage_specification_p)
16003 decl_specifiers->storage_class = sc_extern;
16004 decl = start_decl (declarator, decl_specifiers,
16005 range_for_decl_p? SD_INITIALIZED : is_initialized,
16006 attributes, prefix_attributes,
16007 &pushed_scope);
16008 /* Adjust location of decl if declarator->id_loc is more appropriate:
16009 set, and decl wasn't merged with another decl, in which case its
16010 location would be different from input_location, and more accurate. */
16011 if (DECL_P (decl)
16012 && declarator->id_loc != UNKNOWN_LOCATION
16013 && DECL_SOURCE_LOCATION (decl) == input_location)
16014 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16016 else if (scope)
16017 /* Enter the SCOPE. That way unqualified names appearing in the
16018 initializer will be looked up in SCOPE. */
16019 pushed_scope = push_scope (scope);
16021 /* Perform deferred access control checks, now that we know in which
16022 SCOPE the declared entity resides. */
16023 if (!member_p && decl)
16025 tree saved_current_function_decl = NULL_TREE;
16027 /* If the entity being declared is a function, pretend that we
16028 are in its scope. If it is a `friend', it may have access to
16029 things that would not otherwise be accessible. */
16030 if (TREE_CODE (decl) == FUNCTION_DECL)
16032 saved_current_function_decl = current_function_decl;
16033 current_function_decl = decl;
16036 /* Perform access checks for template parameters. */
16037 cp_parser_perform_template_parameter_access_checks (checks);
16039 /* Perform the access control checks for the declarator and the
16040 decl-specifiers. */
16041 perform_deferred_access_checks (tf_warning_or_error);
16043 /* Restore the saved value. */
16044 if (TREE_CODE (decl) == FUNCTION_DECL)
16045 current_function_decl = saved_current_function_decl;
16048 /* Parse the initializer. */
16049 initializer = NULL_TREE;
16050 is_direct_init = false;
16051 is_non_constant_init = true;
16052 if (is_initialized)
16054 if (function_declarator_p (declarator))
16056 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16057 if (initialization_kind == CPP_EQ)
16058 initializer = cp_parser_pure_specifier (parser);
16059 else
16061 /* If the declaration was erroneous, we don't really
16062 know what the user intended, so just silently
16063 consume the initializer. */
16064 if (decl != error_mark_node)
16065 error_at (initializer_start_token->location,
16066 "initializer provided for function");
16067 cp_parser_skip_to_closing_parenthesis (parser,
16068 /*recovering=*/true,
16069 /*or_comma=*/false,
16070 /*consume_paren=*/true);
16073 else
16075 /* We want to record the extra mangling scope for in-class
16076 initializers of class members and initializers of static data
16077 member templates. The former involves deferring
16078 parsing of the initializer until end of class as with default
16079 arguments. So right here we only handle the latter. */
16080 if (!member_p && processing_template_decl)
16081 start_lambda_scope (decl);
16082 initializer = cp_parser_initializer (parser,
16083 &is_direct_init,
16084 &is_non_constant_init);
16085 if (!member_p && processing_template_decl)
16086 finish_lambda_scope ();
16087 if (initializer == error_mark_node)
16088 cp_parser_skip_to_end_of_statement (parser);
16092 /* The old parser allows attributes to appear after a parenthesized
16093 initializer. Mark Mitchell proposed removing this functionality
16094 on the GCC mailing lists on 2002-08-13. This parser accepts the
16095 attributes -- but ignores them. */
16096 if (cp_parser_allow_gnu_extensions_p (parser)
16097 && initialization_kind == CPP_OPEN_PAREN)
16098 if (cp_parser_attributes_opt (parser))
16099 warning (OPT_Wattributes,
16100 "attributes after parenthesized initializer ignored");
16102 /* For an in-class declaration, use `grokfield' to create the
16103 declaration. */
16104 if (member_p)
16106 if (pushed_scope)
16108 pop_scope (pushed_scope);
16109 pushed_scope = NULL_TREE;
16111 decl = grokfield (declarator, decl_specifiers,
16112 initializer, !is_non_constant_init,
16113 /*asmspec=*/NULL_TREE,
16114 prefix_attributes);
16115 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16116 cp_parser_save_default_args (parser, decl);
16119 /* Finish processing the declaration. But, skip member
16120 declarations. */
16121 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16123 cp_finish_decl (decl,
16124 initializer, !is_non_constant_init,
16125 asm_specification,
16126 /* If the initializer is in parentheses, then this is
16127 a direct-initialization, which means that an
16128 `explicit' constructor is OK. Otherwise, an
16129 `explicit' constructor cannot be used. */
16130 ((is_direct_init || !is_initialized)
16131 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16133 else if ((cxx_dialect != cxx98) && friend_p
16134 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16135 /* Core issue #226 (C++0x only): A default template-argument
16136 shall not be specified in a friend class template
16137 declaration. */
16138 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16139 /*is_partial=*/false, /*is_friend_decl=*/1);
16141 if (!friend_p && pushed_scope)
16142 pop_scope (pushed_scope);
16144 return decl;
16147 /* Parse a declarator.
16149 declarator:
16150 direct-declarator
16151 ptr-operator declarator
16153 abstract-declarator:
16154 ptr-operator abstract-declarator [opt]
16155 direct-abstract-declarator
16157 GNU Extensions:
16159 declarator:
16160 attributes [opt] direct-declarator
16161 attributes [opt] ptr-operator declarator
16163 abstract-declarator:
16164 attributes [opt] ptr-operator abstract-declarator [opt]
16165 attributes [opt] direct-abstract-declarator
16167 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16168 detect constructor, destructor or conversion operators. It is set
16169 to -1 if the declarator is a name, and +1 if it is a
16170 function. Otherwise it is set to zero. Usually you just want to
16171 test for >0, but internally the negative value is used.
16173 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16174 a decl-specifier-seq unless it declares a constructor, destructor,
16175 or conversion. It might seem that we could check this condition in
16176 semantic analysis, rather than parsing, but that makes it difficult
16177 to handle something like `f()'. We want to notice that there are
16178 no decl-specifiers, and therefore realize that this is an
16179 expression, not a declaration.)
16181 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16182 the declarator is a direct-declarator of the form "(...)".
16184 MEMBER_P is true iff this declarator is a member-declarator. */
16186 static cp_declarator *
16187 cp_parser_declarator (cp_parser* parser,
16188 cp_parser_declarator_kind dcl_kind,
16189 int* ctor_dtor_or_conv_p,
16190 bool* parenthesized_p,
16191 bool member_p)
16193 cp_declarator *declarator;
16194 enum tree_code code;
16195 cp_cv_quals cv_quals;
16196 tree class_type;
16197 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16199 /* Assume this is not a constructor, destructor, or type-conversion
16200 operator. */
16201 if (ctor_dtor_or_conv_p)
16202 *ctor_dtor_or_conv_p = 0;
16204 if (cp_parser_allow_gnu_extensions_p (parser))
16205 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16207 /* Check for the ptr-operator production. */
16208 cp_parser_parse_tentatively (parser);
16209 /* Parse the ptr-operator. */
16210 code = cp_parser_ptr_operator (parser,
16211 &class_type,
16212 &cv_quals,
16213 &std_attributes);
16215 /* If that worked, then we have a ptr-operator. */
16216 if (cp_parser_parse_definitely (parser))
16218 /* If a ptr-operator was found, then this declarator was not
16219 parenthesized. */
16220 if (parenthesized_p)
16221 *parenthesized_p = true;
16222 /* The dependent declarator is optional if we are parsing an
16223 abstract-declarator. */
16224 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16225 cp_parser_parse_tentatively (parser);
16227 /* Parse the dependent declarator. */
16228 declarator = cp_parser_declarator (parser, dcl_kind,
16229 /*ctor_dtor_or_conv_p=*/NULL,
16230 /*parenthesized_p=*/NULL,
16231 /*member_p=*/false);
16233 /* If we are parsing an abstract-declarator, we must handle the
16234 case where the dependent declarator is absent. */
16235 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16236 && !cp_parser_parse_definitely (parser))
16237 declarator = NULL;
16239 declarator = cp_parser_make_indirect_declarator
16240 (code, class_type, cv_quals, declarator, std_attributes);
16242 /* Everything else is a direct-declarator. */
16243 else
16245 if (parenthesized_p)
16246 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16247 CPP_OPEN_PAREN);
16248 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16249 ctor_dtor_or_conv_p,
16250 member_p);
16253 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16254 declarator->attributes = gnu_attributes;
16255 return declarator;
16258 /* Parse a direct-declarator or direct-abstract-declarator.
16260 direct-declarator:
16261 declarator-id
16262 direct-declarator ( parameter-declaration-clause )
16263 cv-qualifier-seq [opt]
16264 ref-qualifier [opt]
16265 exception-specification [opt]
16266 direct-declarator [ constant-expression [opt] ]
16267 ( declarator )
16269 direct-abstract-declarator:
16270 direct-abstract-declarator [opt]
16271 ( parameter-declaration-clause )
16272 cv-qualifier-seq [opt]
16273 ref-qualifier [opt]
16274 exception-specification [opt]
16275 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16276 ( abstract-declarator )
16278 Returns a representation of the declarator. DCL_KIND is
16279 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16280 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16281 we are parsing a direct-declarator. It is
16282 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16283 of ambiguity we prefer an abstract declarator, as per
16284 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16285 cp_parser_declarator. */
16287 static cp_declarator *
16288 cp_parser_direct_declarator (cp_parser* parser,
16289 cp_parser_declarator_kind dcl_kind,
16290 int* ctor_dtor_or_conv_p,
16291 bool member_p)
16293 cp_token *token;
16294 cp_declarator *declarator = NULL;
16295 tree scope = NULL_TREE;
16296 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16297 bool saved_in_declarator_p = parser->in_declarator_p;
16298 bool first = true;
16299 tree pushed_scope = NULL_TREE;
16301 while (true)
16303 /* Peek at the next token. */
16304 token = cp_lexer_peek_token (parser->lexer);
16305 if (token->type == CPP_OPEN_PAREN)
16307 /* This is either a parameter-declaration-clause, or a
16308 parenthesized declarator. When we know we are parsing a
16309 named declarator, it must be a parenthesized declarator
16310 if FIRST is true. For instance, `(int)' is a
16311 parameter-declaration-clause, with an omitted
16312 direct-abstract-declarator. But `((*))', is a
16313 parenthesized abstract declarator. Finally, when T is a
16314 template parameter `(T)' is a
16315 parameter-declaration-clause, and not a parenthesized
16316 named declarator.
16318 We first try and parse a parameter-declaration-clause,
16319 and then try a nested declarator (if FIRST is true).
16321 It is not an error for it not to be a
16322 parameter-declaration-clause, even when FIRST is
16323 false. Consider,
16325 int i (int);
16326 int i (3);
16328 The first is the declaration of a function while the
16329 second is the definition of a variable, including its
16330 initializer.
16332 Having seen only the parenthesis, we cannot know which of
16333 these two alternatives should be selected. Even more
16334 complex are examples like:
16336 int i (int (a));
16337 int i (int (3));
16339 The former is a function-declaration; the latter is a
16340 variable initialization.
16342 Thus again, we try a parameter-declaration-clause, and if
16343 that fails, we back out and return. */
16345 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16347 tree params;
16348 unsigned saved_num_template_parameter_lists;
16349 bool is_declarator = false;
16350 tree t;
16352 /* In a member-declarator, the only valid interpretation
16353 of a parenthesis is the start of a
16354 parameter-declaration-clause. (It is invalid to
16355 initialize a static data member with a parenthesized
16356 initializer; only the "=" form of initialization is
16357 permitted.) */
16358 if (!member_p)
16359 cp_parser_parse_tentatively (parser);
16361 /* Consume the `('. */
16362 cp_lexer_consume_token (parser->lexer);
16363 if (first)
16365 /* If this is going to be an abstract declarator, we're
16366 in a declarator and we can't have default args. */
16367 parser->default_arg_ok_p = false;
16368 parser->in_declarator_p = true;
16371 /* Inside the function parameter list, surrounding
16372 template-parameter-lists do not apply. */
16373 saved_num_template_parameter_lists
16374 = parser->num_template_parameter_lists;
16375 parser->num_template_parameter_lists = 0;
16377 begin_scope (sk_function_parms, NULL_TREE);
16379 /* Parse the parameter-declaration-clause. */
16380 params = cp_parser_parameter_declaration_clause (parser);
16382 parser->num_template_parameter_lists
16383 = saved_num_template_parameter_lists;
16385 /* Consume the `)'. */
16386 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16388 /* If all went well, parse the cv-qualifier-seq,
16389 ref-qualifier and the exception-specification. */
16390 if (member_p || cp_parser_parse_definitely (parser))
16392 cp_cv_quals cv_quals;
16393 cp_virt_specifiers virt_specifiers;
16394 cp_ref_qualifier ref_qual;
16395 tree exception_specification;
16396 tree late_return;
16397 tree attrs;
16398 bool memfn = (member_p || (pushed_scope
16399 && CLASS_TYPE_P (pushed_scope)));
16401 is_declarator = true;
16403 if (ctor_dtor_or_conv_p)
16404 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16405 first = false;
16407 /* Parse the cv-qualifier-seq. */
16408 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16409 /* Parse the ref-qualifier. */
16410 ref_qual = cp_parser_ref_qualifier_seq_opt (parser);
16411 /* And the exception-specification. */
16412 exception_specification
16413 = cp_parser_exception_specification_opt (parser);
16415 attrs = cp_parser_std_attribute_spec_seq (parser);
16417 late_return = (cp_parser_late_return_type_opt
16418 (parser, memfn ? cv_quals : -1));
16420 /* Parse the virt-specifier-seq. */
16421 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16423 /* Create the function-declarator. */
16424 declarator = make_call_declarator (declarator,
16425 params,
16426 cv_quals,
16427 virt_specifiers,
16428 ref_qual,
16429 exception_specification,
16430 late_return);
16431 declarator->std_attributes = attrs;
16432 /* Any subsequent parameter lists are to do with
16433 return type, so are not those of the declared
16434 function. */
16435 parser->default_arg_ok_p = false;
16438 /* Remove the function parms from scope. */
16439 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16440 pop_binding (DECL_NAME (t), t);
16441 leave_scope();
16443 if (is_declarator)
16444 /* Repeat the main loop. */
16445 continue;
16448 /* If this is the first, we can try a parenthesized
16449 declarator. */
16450 if (first)
16452 bool saved_in_type_id_in_expr_p;
16454 parser->default_arg_ok_p = saved_default_arg_ok_p;
16455 parser->in_declarator_p = saved_in_declarator_p;
16457 /* Consume the `('. */
16458 cp_lexer_consume_token (parser->lexer);
16459 /* Parse the nested declarator. */
16460 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16461 parser->in_type_id_in_expr_p = true;
16462 declarator
16463 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16464 /*parenthesized_p=*/NULL,
16465 member_p);
16466 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16467 first = false;
16468 /* Expect a `)'. */
16469 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16470 declarator = cp_error_declarator;
16471 if (declarator == cp_error_declarator)
16472 break;
16474 goto handle_declarator;
16476 /* Otherwise, we must be done. */
16477 else
16478 break;
16480 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16481 && token->type == CPP_OPEN_SQUARE
16482 && !cp_next_tokens_can_be_attribute_p (parser))
16484 /* Parse an array-declarator. */
16485 tree bounds, attrs;
16487 if (ctor_dtor_or_conv_p)
16488 *ctor_dtor_or_conv_p = 0;
16490 first = false;
16491 parser->default_arg_ok_p = false;
16492 parser->in_declarator_p = true;
16493 /* Consume the `['. */
16494 cp_lexer_consume_token (parser->lexer);
16495 /* Peek at the next token. */
16496 token = cp_lexer_peek_token (parser->lexer);
16497 /* If the next token is `]', then there is no
16498 constant-expression. */
16499 if (token->type != CPP_CLOSE_SQUARE)
16501 bool non_constant_p;
16503 bounds
16504 = cp_parser_constant_expression (parser,
16505 /*allow_non_constant=*/true,
16506 &non_constant_p);
16507 if (!non_constant_p)
16508 /* OK */;
16509 else if (error_operand_p (bounds))
16510 /* Already gave an error. */;
16511 else if (!parser->in_function_body
16512 || current_binding_level->kind == sk_function_parms)
16514 /* Normally, the array bound must be an integral constant
16515 expression. However, as an extension, we allow VLAs
16516 in function scopes as long as they aren't part of a
16517 parameter declaration. */
16518 cp_parser_error (parser,
16519 "array bound is not an integer constant");
16520 bounds = error_mark_node;
16522 else if (processing_template_decl)
16524 /* Remember this wasn't a constant-expression. */
16525 bounds = build_nop (TREE_TYPE (bounds), bounds);
16526 TREE_SIDE_EFFECTS (bounds) = 1;
16529 else
16530 bounds = NULL_TREE;
16531 /* Look for the closing `]'. */
16532 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16534 declarator = cp_error_declarator;
16535 break;
16538 attrs = cp_parser_std_attribute_spec_seq (parser);
16539 declarator = make_array_declarator (declarator, bounds);
16540 declarator->std_attributes = attrs;
16542 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16545 tree qualifying_scope;
16546 tree unqualified_name;
16547 tree attrs;
16548 special_function_kind sfk;
16549 bool abstract_ok;
16550 bool pack_expansion_p = false;
16551 cp_token *declarator_id_start_token;
16553 /* Parse a declarator-id */
16554 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16555 if (abstract_ok)
16557 cp_parser_parse_tentatively (parser);
16559 /* If we see an ellipsis, we should be looking at a
16560 parameter pack. */
16561 if (token->type == CPP_ELLIPSIS)
16563 /* Consume the `...' */
16564 cp_lexer_consume_token (parser->lexer);
16566 pack_expansion_p = true;
16570 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16571 unqualified_name
16572 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16573 qualifying_scope = parser->scope;
16574 if (abstract_ok)
16576 bool okay = false;
16578 if (!unqualified_name && pack_expansion_p)
16580 /* Check whether an error occurred. */
16581 okay = !cp_parser_error_occurred (parser);
16583 /* We already consumed the ellipsis to mark a
16584 parameter pack, but we have no way to report it,
16585 so abort the tentative parse. We will be exiting
16586 immediately anyway. */
16587 cp_parser_abort_tentative_parse (parser);
16589 else
16590 okay = cp_parser_parse_definitely (parser);
16592 if (!okay)
16593 unqualified_name = error_mark_node;
16594 else if (unqualified_name
16595 && (qualifying_scope
16596 || (TREE_CODE (unqualified_name)
16597 != IDENTIFIER_NODE)))
16599 cp_parser_error (parser, "expected unqualified-id");
16600 unqualified_name = error_mark_node;
16604 if (!unqualified_name)
16605 return NULL;
16606 if (unqualified_name == error_mark_node)
16608 declarator = cp_error_declarator;
16609 pack_expansion_p = false;
16610 declarator->parameter_pack_p = false;
16611 break;
16614 attrs = cp_parser_std_attribute_spec_seq (parser);
16616 if (qualifying_scope && at_namespace_scope_p ()
16617 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16619 /* In the declaration of a member of a template class
16620 outside of the class itself, the SCOPE will sometimes
16621 be a TYPENAME_TYPE. For example, given:
16623 template <typename T>
16624 int S<T>::R::i = 3;
16626 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16627 this context, we must resolve S<T>::R to an ordinary
16628 type, rather than a typename type.
16630 The reason we normally avoid resolving TYPENAME_TYPEs
16631 is that a specialization of `S' might render
16632 `S<T>::R' not a type. However, if `S' is
16633 specialized, then this `i' will not be used, so there
16634 is no harm in resolving the types here. */
16635 tree type;
16637 /* Resolve the TYPENAME_TYPE. */
16638 type = resolve_typename_type (qualifying_scope,
16639 /*only_current_p=*/false);
16640 /* If that failed, the declarator is invalid. */
16641 if (TREE_CODE (type) == TYPENAME_TYPE)
16643 if (typedef_variant_p (type))
16644 error_at (declarator_id_start_token->location,
16645 "cannot define member of dependent typedef "
16646 "%qT", type);
16647 else
16648 error_at (declarator_id_start_token->location,
16649 "%<%T::%E%> is not a type",
16650 TYPE_CONTEXT (qualifying_scope),
16651 TYPE_IDENTIFIER (qualifying_scope));
16653 qualifying_scope = type;
16656 sfk = sfk_none;
16658 if (unqualified_name)
16660 tree class_type;
16662 if (qualifying_scope
16663 && CLASS_TYPE_P (qualifying_scope))
16664 class_type = qualifying_scope;
16665 else
16666 class_type = current_class_type;
16668 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16670 tree name_type = TREE_TYPE (unqualified_name);
16671 if (class_type && same_type_p (name_type, class_type))
16673 if (qualifying_scope
16674 && CLASSTYPE_USE_TEMPLATE (name_type))
16676 error_at (declarator_id_start_token->location,
16677 "invalid use of constructor as a template");
16678 inform (declarator_id_start_token->location,
16679 "use %<%T::%D%> instead of %<%T::%D%> to "
16680 "name the constructor in a qualified name",
16681 class_type,
16682 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16683 class_type, name_type);
16684 declarator = cp_error_declarator;
16685 break;
16687 else
16688 unqualified_name = constructor_name (class_type);
16690 else
16692 /* We do not attempt to print the declarator
16693 here because we do not have enough
16694 information about its original syntactic
16695 form. */
16696 cp_parser_error (parser, "invalid declarator");
16697 declarator = cp_error_declarator;
16698 break;
16702 if (class_type)
16704 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16705 sfk = sfk_destructor;
16706 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16707 sfk = sfk_conversion;
16708 else if (/* There's no way to declare a constructor
16709 for an anonymous type, even if the type
16710 got a name for linkage purposes. */
16711 !TYPE_WAS_ANONYMOUS (class_type)
16712 && constructor_name_p (unqualified_name,
16713 class_type))
16715 unqualified_name = constructor_name (class_type);
16716 sfk = sfk_constructor;
16718 else if (is_overloaded_fn (unqualified_name)
16719 && DECL_CONSTRUCTOR_P (get_first_fn
16720 (unqualified_name)))
16721 sfk = sfk_constructor;
16723 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16724 *ctor_dtor_or_conv_p = -1;
16727 declarator = make_id_declarator (qualifying_scope,
16728 unqualified_name,
16729 sfk);
16730 declarator->std_attributes = attrs;
16731 declarator->id_loc = token->location;
16732 declarator->parameter_pack_p = pack_expansion_p;
16734 if (pack_expansion_p)
16735 maybe_warn_variadic_templates ();
16738 handle_declarator:;
16739 scope = get_scope_of_declarator (declarator);
16740 if (scope)
16741 /* Any names that appear after the declarator-id for a
16742 member are looked up in the containing scope. */
16743 pushed_scope = push_scope (scope);
16744 parser->in_declarator_p = true;
16745 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16746 || (declarator && declarator->kind == cdk_id))
16747 /* Default args are only allowed on function
16748 declarations. */
16749 parser->default_arg_ok_p = saved_default_arg_ok_p;
16750 else
16751 parser->default_arg_ok_p = false;
16753 first = false;
16755 /* We're done. */
16756 else
16757 break;
16760 /* For an abstract declarator, we might wind up with nothing at this
16761 point. That's an error; the declarator is not optional. */
16762 if (!declarator)
16763 cp_parser_error (parser, "expected declarator");
16765 /* If we entered a scope, we must exit it now. */
16766 if (pushed_scope)
16767 pop_scope (pushed_scope);
16769 parser->default_arg_ok_p = saved_default_arg_ok_p;
16770 parser->in_declarator_p = saved_in_declarator_p;
16772 return declarator;
16775 /* Parse a ptr-operator.
16777 ptr-operator:
16778 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16779 * cv-qualifier-seq [opt]
16781 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16782 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16784 GNU Extension:
16786 ptr-operator:
16787 & cv-qualifier-seq [opt]
16789 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16790 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16791 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16792 filled in with the TYPE containing the member. *CV_QUALS is
16793 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16794 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16795 Note that the tree codes returned by this function have nothing
16796 to do with the types of trees that will be eventually be created
16797 to represent the pointer or reference type being parsed. They are
16798 just constants with suggestive names. */
16799 static enum tree_code
16800 cp_parser_ptr_operator (cp_parser* parser,
16801 tree* type,
16802 cp_cv_quals *cv_quals,
16803 tree *attributes)
16805 enum tree_code code = ERROR_MARK;
16806 cp_token *token;
16807 tree attrs = NULL_TREE;
16809 /* Assume that it's not a pointer-to-member. */
16810 *type = NULL_TREE;
16811 /* And that there are no cv-qualifiers. */
16812 *cv_quals = TYPE_UNQUALIFIED;
16814 /* Peek at the next token. */
16815 token = cp_lexer_peek_token (parser->lexer);
16817 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16818 if (token->type == CPP_MULT)
16819 code = INDIRECT_REF;
16820 else if (token->type == CPP_AND)
16821 code = ADDR_EXPR;
16822 else if ((cxx_dialect != cxx98) &&
16823 token->type == CPP_AND_AND) /* C++0x only */
16824 code = NON_LVALUE_EXPR;
16826 if (code != ERROR_MARK)
16828 /* Consume the `*', `&' or `&&'. */
16829 cp_lexer_consume_token (parser->lexer);
16831 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16832 `&', if we are allowing GNU extensions. (The only qualifier
16833 that can legally appear after `&' is `restrict', but that is
16834 enforced during semantic analysis. */
16835 if (code == INDIRECT_REF
16836 || cp_parser_allow_gnu_extensions_p (parser))
16837 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16839 attrs = cp_parser_std_attribute_spec_seq (parser);
16840 if (attributes != NULL)
16841 *attributes = attrs;
16843 else
16845 /* Try the pointer-to-member case. */
16846 cp_parser_parse_tentatively (parser);
16847 /* Look for the optional `::' operator. */
16848 cp_parser_global_scope_opt (parser,
16849 /*current_scope_valid_p=*/false);
16850 /* Look for the nested-name specifier. */
16851 token = cp_lexer_peek_token (parser->lexer);
16852 cp_parser_nested_name_specifier (parser,
16853 /*typename_keyword_p=*/false,
16854 /*check_dependency_p=*/true,
16855 /*type_p=*/false,
16856 /*is_declaration=*/false);
16857 /* If we found it, and the next token is a `*', then we are
16858 indeed looking at a pointer-to-member operator. */
16859 if (!cp_parser_error_occurred (parser)
16860 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16862 /* Indicate that the `*' operator was used. */
16863 code = INDIRECT_REF;
16865 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16866 error_at (token->location, "%qD is a namespace", parser->scope);
16867 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16868 error_at (token->location, "cannot form pointer to member of "
16869 "non-class %q#T", parser->scope);
16870 else
16872 /* The type of which the member is a member is given by the
16873 current SCOPE. */
16874 *type = parser->scope;
16875 /* The next name will not be qualified. */
16876 parser->scope = NULL_TREE;
16877 parser->qualifying_scope = NULL_TREE;
16878 parser->object_scope = NULL_TREE;
16879 /* Look for optional c++11 attributes. */
16880 attrs = cp_parser_std_attribute_spec_seq (parser);
16881 if (attributes != NULL)
16882 *attributes = attrs;
16883 /* Look for the optional cv-qualifier-seq. */
16884 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16887 /* If that didn't work we don't have a ptr-operator. */
16888 if (!cp_parser_parse_definitely (parser))
16889 cp_parser_error (parser, "expected ptr-operator");
16892 return code;
16895 /* Parse an (optional) cv-qualifier-seq.
16897 cv-qualifier-seq:
16898 cv-qualifier cv-qualifier-seq [opt]
16900 cv-qualifier:
16901 const
16902 volatile
16904 GNU Extension:
16906 cv-qualifier:
16907 __restrict__
16909 Returns a bitmask representing the cv-qualifiers. */
16911 static cp_cv_quals
16912 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16914 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16916 while (true)
16918 cp_token *token;
16919 cp_cv_quals cv_qualifier;
16921 /* Peek at the next token. */
16922 token = cp_lexer_peek_token (parser->lexer);
16923 /* See if it's a cv-qualifier. */
16924 switch (token->keyword)
16926 case RID_CONST:
16927 cv_qualifier = TYPE_QUAL_CONST;
16928 break;
16930 case RID_VOLATILE:
16931 cv_qualifier = TYPE_QUAL_VOLATILE;
16932 break;
16934 case RID_RESTRICT:
16935 cv_qualifier = TYPE_QUAL_RESTRICT;
16936 break;
16938 default:
16939 cv_qualifier = TYPE_UNQUALIFIED;
16940 break;
16943 if (!cv_qualifier)
16944 break;
16946 if (cv_quals & cv_qualifier)
16948 error_at (token->location, "duplicate cv-qualifier");
16949 cp_lexer_purge_token (parser->lexer);
16951 else
16953 cp_lexer_consume_token (parser->lexer);
16954 cv_quals |= cv_qualifier;
16958 return cv_quals;
16961 /* Parse an (optional) ref-qualifier
16963 ref-qualifier:
16967 Returns cp_ref_qualifier representing ref-qualifier. */
16969 static cp_ref_qualifier
16970 cp_parser_ref_qualifier_seq_opt (cp_parser* parser)
16972 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
16973 cp_token *token = cp_lexer_peek_token (parser->lexer);
16975 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
16976 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
16977 return ref_qual;
16979 switch (token->type)
16981 case CPP_AND:
16982 ref_qual = REF_QUAL_LVALUE;
16983 break;
16984 case CPP_AND_AND:
16985 ref_qual = REF_QUAL_RVALUE;
16986 break;
16989 if (ref_qual)
16991 cp_lexer_consume_token (parser->lexer);
16994 return ref_qual;
16997 /* Parse an (optional) virt-specifier-seq.
16999 virt-specifier-seq:
17000 virt-specifier virt-specifier-seq [opt]
17002 virt-specifier:
17003 override
17004 final
17006 Returns a bitmask representing the virt-specifiers. */
17008 static cp_virt_specifiers
17009 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17011 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17013 while (true)
17015 cp_token *token;
17016 cp_virt_specifiers virt_specifier;
17018 /* Peek at the next token. */
17019 token = cp_lexer_peek_token (parser->lexer);
17020 /* See if it's a virt-specifier-qualifier. */
17021 if (token->type != CPP_NAME)
17022 break;
17023 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17025 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17026 virt_specifier = VIRT_SPEC_OVERRIDE;
17028 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17030 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17031 virt_specifier = VIRT_SPEC_FINAL;
17033 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17035 virt_specifier = VIRT_SPEC_FINAL;
17037 else
17038 break;
17040 if (virt_specifiers & virt_specifier)
17042 error_at (token->location, "duplicate virt-specifier");
17043 cp_lexer_purge_token (parser->lexer);
17045 else
17047 cp_lexer_consume_token (parser->lexer);
17048 virt_specifiers |= virt_specifier;
17051 return virt_specifiers;
17054 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17055 is in scope even though it isn't real. */
17057 static void
17058 inject_this_parameter (tree ctype, cp_cv_quals quals)
17060 tree this_parm;
17062 if (current_class_ptr)
17064 /* We don't clear this between NSDMIs. Is it already what we want? */
17065 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17066 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17067 && cp_type_quals (type) == quals)
17068 return;
17071 this_parm = build_this_parm (ctype, quals);
17072 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17073 current_class_ptr = NULL_TREE;
17074 current_class_ref
17075 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17076 current_class_ptr = this_parm;
17079 /* Parse a late-specified return type, if any. This is not a separate
17080 non-terminal, but part of a function declarator, which looks like
17082 -> trailing-type-specifier-seq abstract-declarator(opt)
17084 Returns the type indicated by the type-id.
17086 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17087 function. */
17089 static tree
17090 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
17092 cp_token *token;
17093 tree type;
17095 /* Peek at the next token. */
17096 token = cp_lexer_peek_token (parser->lexer);
17097 /* A late-specified return type is indicated by an initial '->'. */
17098 if (token->type != CPP_DEREF)
17099 return NULL_TREE;
17101 /* Consume the ->. */
17102 cp_lexer_consume_token (parser->lexer);
17104 tree save_ccp = current_class_ptr;
17105 tree save_ccr = current_class_ref;
17106 if (quals >= 0)
17108 /* DR 1207: 'this' is in scope in the trailing return type. */
17109 inject_this_parameter (current_class_type, quals);
17112 type = cp_parser_trailing_type_id (parser);
17114 if (quals >= 0)
17116 current_class_ptr = save_ccp;
17117 current_class_ref = save_ccr;
17120 return type;
17123 /* Parse a declarator-id.
17125 declarator-id:
17126 id-expression
17127 :: [opt] nested-name-specifier [opt] type-name
17129 In the `id-expression' case, the value returned is as for
17130 cp_parser_id_expression if the id-expression was an unqualified-id.
17131 If the id-expression was a qualified-id, then a SCOPE_REF is
17132 returned. The first operand is the scope (either a NAMESPACE_DECL
17133 or TREE_TYPE), but the second is still just a representation of an
17134 unqualified-id. */
17136 static tree
17137 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17139 tree id;
17140 /* The expression must be an id-expression. Assume that qualified
17141 names are the names of types so that:
17143 template <class T>
17144 int S<T>::R::i = 3;
17146 will work; we must treat `S<T>::R' as the name of a type.
17147 Similarly, assume that qualified names are templates, where
17148 required, so that:
17150 template <class T>
17151 int S<T>::R<T>::i = 3;
17153 will work, too. */
17154 id = cp_parser_id_expression (parser,
17155 /*template_keyword_p=*/false,
17156 /*check_dependency_p=*/false,
17157 /*template_p=*/NULL,
17158 /*declarator_p=*/true,
17159 optional_p);
17160 if (id && BASELINK_P (id))
17161 id = BASELINK_FUNCTIONS (id);
17162 return id;
17165 /* Parse a type-id.
17167 type-id:
17168 type-specifier-seq abstract-declarator [opt]
17170 Returns the TYPE specified. */
17172 static tree
17173 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17174 bool is_trailing_return)
17176 cp_decl_specifier_seq type_specifier_seq;
17177 cp_declarator *abstract_declarator;
17179 /* Parse the type-specifier-seq. */
17180 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17181 is_trailing_return,
17182 &type_specifier_seq);
17183 if (type_specifier_seq.type == error_mark_node)
17184 return error_mark_node;
17186 /* There might or might not be an abstract declarator. */
17187 cp_parser_parse_tentatively (parser);
17188 /* Look for the declarator. */
17189 abstract_declarator
17190 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17191 /*parenthesized_p=*/NULL,
17192 /*member_p=*/false);
17193 /* Check to see if there really was a declarator. */
17194 if (!cp_parser_parse_definitely (parser))
17195 abstract_declarator = NULL;
17197 if (type_specifier_seq.type
17198 && type_uses_auto (type_specifier_seq.type))
17200 /* A type-id with type 'auto' is only ok if the abstract declarator
17201 is a function declarator with a late-specified return type. */
17202 if (abstract_declarator
17203 && abstract_declarator->kind == cdk_function
17204 && abstract_declarator->u.function.late_return_type)
17205 /* OK */;
17206 else
17208 error ("invalid use of %<auto%>");
17209 return error_mark_node;
17213 return groktypename (&type_specifier_seq, abstract_declarator,
17214 is_template_arg);
17217 static tree cp_parser_type_id (cp_parser *parser)
17219 return cp_parser_type_id_1 (parser, false, false);
17222 static tree cp_parser_template_type_arg (cp_parser *parser)
17224 tree r;
17225 const char *saved_message = parser->type_definition_forbidden_message;
17226 parser->type_definition_forbidden_message
17227 = G_("types may not be defined in template arguments");
17228 r = cp_parser_type_id_1 (parser, true, false);
17229 parser->type_definition_forbidden_message = saved_message;
17230 return r;
17233 static tree cp_parser_trailing_type_id (cp_parser *parser)
17235 return cp_parser_type_id_1 (parser, false, true);
17238 /* Parse a type-specifier-seq.
17240 type-specifier-seq:
17241 type-specifier type-specifier-seq [opt]
17243 GNU extension:
17245 type-specifier-seq:
17246 attributes type-specifier-seq [opt]
17248 If IS_DECLARATION is true, we are at the start of a "condition" or
17249 exception-declaration, so we might be followed by a declarator-id.
17251 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17252 i.e. we've just seen "->".
17254 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17256 static void
17257 cp_parser_type_specifier_seq (cp_parser* parser,
17258 bool is_declaration,
17259 bool is_trailing_return,
17260 cp_decl_specifier_seq *type_specifier_seq)
17262 bool seen_type_specifier = false;
17263 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17264 cp_token *start_token = NULL;
17266 /* Clear the TYPE_SPECIFIER_SEQ. */
17267 clear_decl_specs (type_specifier_seq);
17269 /* In the context of a trailing return type, enum E { } is an
17270 elaborated-type-specifier followed by a function-body, not an
17271 enum-specifier. */
17272 if (is_trailing_return)
17273 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17275 /* Parse the type-specifiers and attributes. */
17276 while (true)
17278 tree type_specifier;
17279 bool is_cv_qualifier;
17281 /* Check for attributes first. */
17282 if (cp_next_tokens_can_be_attribute_p (parser))
17284 type_specifier_seq->attributes =
17285 chainon (type_specifier_seq->attributes,
17286 cp_parser_attributes_opt (parser));
17287 continue;
17290 /* record the token of the beginning of the type specifier seq,
17291 for error reporting purposes*/
17292 if (!start_token)
17293 start_token = cp_lexer_peek_token (parser->lexer);
17295 /* Look for the type-specifier. */
17296 type_specifier = cp_parser_type_specifier (parser,
17297 flags,
17298 type_specifier_seq,
17299 /*is_declaration=*/false,
17300 NULL,
17301 &is_cv_qualifier);
17302 if (!type_specifier)
17304 /* If the first type-specifier could not be found, this is not a
17305 type-specifier-seq at all. */
17306 if (!seen_type_specifier)
17308 cp_parser_error (parser, "expected type-specifier");
17309 type_specifier_seq->type = error_mark_node;
17310 return;
17312 /* If subsequent type-specifiers could not be found, the
17313 type-specifier-seq is complete. */
17314 break;
17317 seen_type_specifier = true;
17318 /* The standard says that a condition can be:
17320 type-specifier-seq declarator = assignment-expression
17322 However, given:
17324 struct S {};
17325 if (int S = ...)
17327 we should treat the "S" as a declarator, not as a
17328 type-specifier. The standard doesn't say that explicitly for
17329 type-specifier-seq, but it does say that for
17330 decl-specifier-seq in an ordinary declaration. Perhaps it
17331 would be clearer just to allow a decl-specifier-seq here, and
17332 then add a semantic restriction that if any decl-specifiers
17333 that are not type-specifiers appear, the program is invalid. */
17334 if (is_declaration && !is_cv_qualifier)
17335 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17339 /* Parse a parameter-declaration-clause.
17341 parameter-declaration-clause:
17342 parameter-declaration-list [opt] ... [opt]
17343 parameter-declaration-list , ...
17345 Returns a representation for the parameter declarations. A return
17346 value of NULL indicates a parameter-declaration-clause consisting
17347 only of an ellipsis. */
17349 static tree
17350 cp_parser_parameter_declaration_clause (cp_parser* parser)
17352 tree parameters;
17353 cp_token *token;
17354 bool ellipsis_p;
17355 bool is_error;
17357 /* Peek at the next token. */
17358 token = cp_lexer_peek_token (parser->lexer);
17359 /* Check for trivial parameter-declaration-clauses. */
17360 if (token->type == CPP_ELLIPSIS)
17362 /* Consume the `...' token. */
17363 cp_lexer_consume_token (parser->lexer);
17364 return NULL_TREE;
17366 else if (token->type == CPP_CLOSE_PAREN)
17367 /* There are no parameters. */
17369 #ifndef NO_IMPLICIT_EXTERN_C
17370 if (in_system_header && current_class_type == NULL
17371 && current_lang_name == lang_name_c)
17372 return NULL_TREE;
17373 else
17374 #endif
17375 return void_list_node;
17377 /* Check for `(void)', too, which is a special case. */
17378 else if (token->keyword == RID_VOID
17379 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17380 == CPP_CLOSE_PAREN))
17382 /* Consume the `void' token. */
17383 cp_lexer_consume_token (parser->lexer);
17384 /* There are no parameters. */
17385 return void_list_node;
17388 /* Parse the parameter-declaration-list. */
17389 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17390 /* If a parse error occurred while parsing the
17391 parameter-declaration-list, then the entire
17392 parameter-declaration-clause is erroneous. */
17393 if (is_error)
17394 return NULL;
17396 /* Peek at the next token. */
17397 token = cp_lexer_peek_token (parser->lexer);
17398 /* If it's a `,', the clause should terminate with an ellipsis. */
17399 if (token->type == CPP_COMMA)
17401 /* Consume the `,'. */
17402 cp_lexer_consume_token (parser->lexer);
17403 /* Expect an ellipsis. */
17404 ellipsis_p
17405 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17407 /* It might also be `...' if the optional trailing `,' was
17408 omitted. */
17409 else if (token->type == CPP_ELLIPSIS)
17411 /* Consume the `...' token. */
17412 cp_lexer_consume_token (parser->lexer);
17413 /* And remember that we saw it. */
17414 ellipsis_p = true;
17416 else
17417 ellipsis_p = false;
17419 /* Finish the parameter list. */
17420 if (!ellipsis_p)
17421 parameters = chainon (parameters, void_list_node);
17423 return parameters;
17426 /* Parse a parameter-declaration-list.
17428 parameter-declaration-list:
17429 parameter-declaration
17430 parameter-declaration-list , parameter-declaration
17432 Returns a representation of the parameter-declaration-list, as for
17433 cp_parser_parameter_declaration_clause. However, the
17434 `void_list_node' is never appended to the list. Upon return,
17435 *IS_ERROR will be true iff an error occurred. */
17437 static tree
17438 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17440 tree parameters = NULL_TREE;
17441 tree *tail = &parameters;
17442 bool saved_in_unbraced_linkage_specification_p;
17443 int index = 0;
17445 /* Assume all will go well. */
17446 *is_error = false;
17447 /* The special considerations that apply to a function within an
17448 unbraced linkage specifications do not apply to the parameters
17449 to the function. */
17450 saved_in_unbraced_linkage_specification_p
17451 = parser->in_unbraced_linkage_specification_p;
17452 parser->in_unbraced_linkage_specification_p = false;
17454 /* Look for more parameters. */
17455 while (true)
17457 cp_parameter_declarator *parameter;
17458 tree decl = error_mark_node;
17459 bool parenthesized_p = false;
17460 /* Parse the parameter. */
17461 parameter
17462 = cp_parser_parameter_declaration (parser,
17463 /*template_parm_p=*/false,
17464 &parenthesized_p);
17466 /* We don't know yet if the enclosing context is deprecated, so wait
17467 and warn in grokparms if appropriate. */
17468 deprecated_state = DEPRECATED_SUPPRESS;
17470 if (parameter)
17471 decl = grokdeclarator (parameter->declarator,
17472 &parameter->decl_specifiers,
17473 PARM,
17474 parameter->default_argument != NULL_TREE,
17475 &parameter->decl_specifiers.attributes);
17477 deprecated_state = DEPRECATED_NORMAL;
17479 /* If a parse error occurred parsing the parameter declaration,
17480 then the entire parameter-declaration-list is erroneous. */
17481 if (decl == error_mark_node)
17483 *is_error = true;
17484 parameters = error_mark_node;
17485 break;
17488 if (parameter->decl_specifiers.attributes)
17489 cplus_decl_attributes (&decl,
17490 parameter->decl_specifiers.attributes,
17492 if (DECL_NAME (decl))
17493 decl = pushdecl (decl);
17495 if (decl != error_mark_node)
17497 retrofit_lang_decl (decl);
17498 DECL_PARM_INDEX (decl) = ++index;
17499 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17502 /* Add the new parameter to the list. */
17503 *tail = build_tree_list (parameter->default_argument, decl);
17504 tail = &TREE_CHAIN (*tail);
17506 /* Peek at the next token. */
17507 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17508 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17509 /* These are for Objective-C++ */
17510 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17511 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17512 /* The parameter-declaration-list is complete. */
17513 break;
17514 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17516 cp_token *token;
17518 /* Peek at the next token. */
17519 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17520 /* If it's an ellipsis, then the list is complete. */
17521 if (token->type == CPP_ELLIPSIS)
17522 break;
17523 /* Otherwise, there must be more parameters. Consume the
17524 `,'. */
17525 cp_lexer_consume_token (parser->lexer);
17526 /* When parsing something like:
17528 int i(float f, double d)
17530 we can tell after seeing the declaration for "f" that we
17531 are not looking at an initialization of a variable "i",
17532 but rather at the declaration of a function "i".
17534 Due to the fact that the parsing of template arguments
17535 (as specified to a template-id) requires backtracking we
17536 cannot use this technique when inside a template argument
17537 list. */
17538 if (!parser->in_template_argument_list_p
17539 && !parser->in_type_id_in_expr_p
17540 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17541 /* However, a parameter-declaration of the form
17542 "foat(f)" (which is a valid declaration of a
17543 parameter "f") can also be interpreted as an
17544 expression (the conversion of "f" to "float"). */
17545 && !parenthesized_p)
17546 cp_parser_commit_to_tentative_parse (parser);
17548 else
17550 cp_parser_error (parser, "expected %<,%> or %<...%>");
17551 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17552 cp_parser_skip_to_closing_parenthesis (parser,
17553 /*recovering=*/true,
17554 /*or_comma=*/false,
17555 /*consume_paren=*/false);
17556 break;
17560 parser->in_unbraced_linkage_specification_p
17561 = saved_in_unbraced_linkage_specification_p;
17563 return parameters;
17566 /* Parse a parameter declaration.
17568 parameter-declaration:
17569 decl-specifier-seq ... [opt] declarator
17570 decl-specifier-seq declarator = assignment-expression
17571 decl-specifier-seq ... [opt] abstract-declarator [opt]
17572 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17574 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17575 declares a template parameter. (In that case, a non-nested `>'
17576 token encountered during the parsing of the assignment-expression
17577 is not interpreted as a greater-than operator.)
17579 Returns a representation of the parameter, or NULL if an error
17580 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17581 true iff the declarator is of the form "(p)". */
17583 static cp_parameter_declarator *
17584 cp_parser_parameter_declaration (cp_parser *parser,
17585 bool template_parm_p,
17586 bool *parenthesized_p)
17588 int declares_class_or_enum;
17589 cp_decl_specifier_seq decl_specifiers;
17590 cp_declarator *declarator;
17591 tree default_argument;
17592 cp_token *token = NULL, *declarator_token_start = NULL;
17593 const char *saved_message;
17595 /* In a template parameter, `>' is not an operator.
17597 [temp.param]
17599 When parsing a default template-argument for a non-type
17600 template-parameter, the first non-nested `>' is taken as the end
17601 of the template parameter-list rather than a greater-than
17602 operator. */
17604 /* Type definitions may not appear in parameter types. */
17605 saved_message = parser->type_definition_forbidden_message;
17606 parser->type_definition_forbidden_message
17607 = G_("types may not be defined in parameter types");
17609 /* Parse the declaration-specifiers. */
17610 cp_parser_decl_specifier_seq (parser,
17611 CP_PARSER_FLAGS_NONE,
17612 &decl_specifiers,
17613 &declares_class_or_enum);
17615 /* Complain about missing 'typename' or other invalid type names. */
17616 if (!decl_specifiers.any_type_specifiers_p)
17617 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17619 /* If an error occurred, there's no reason to attempt to parse the
17620 rest of the declaration. */
17621 if (cp_parser_error_occurred (parser))
17623 parser->type_definition_forbidden_message = saved_message;
17624 return NULL;
17627 /* Peek at the next token. */
17628 token = cp_lexer_peek_token (parser->lexer);
17630 /* If the next token is a `)', `,', `=', `>', or `...', then there
17631 is no declarator. However, when variadic templates are enabled,
17632 there may be a declarator following `...'. */
17633 if (token->type == CPP_CLOSE_PAREN
17634 || token->type == CPP_COMMA
17635 || token->type == CPP_EQ
17636 || token->type == CPP_GREATER)
17638 declarator = NULL;
17639 if (parenthesized_p)
17640 *parenthesized_p = false;
17642 /* Otherwise, there should be a declarator. */
17643 else
17645 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17646 parser->default_arg_ok_p = false;
17648 /* After seeing a decl-specifier-seq, if the next token is not a
17649 "(", there is no possibility that the code is a valid
17650 expression. Therefore, if parsing tentatively, we commit at
17651 this point. */
17652 if (!parser->in_template_argument_list_p
17653 /* In an expression context, having seen:
17655 (int((char ...
17657 we cannot be sure whether we are looking at a
17658 function-type (taking a "char" as a parameter) or a cast
17659 of some object of type "char" to "int". */
17660 && !parser->in_type_id_in_expr_p
17661 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17662 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17663 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17664 cp_parser_commit_to_tentative_parse (parser);
17665 /* Parse the declarator. */
17666 declarator_token_start = token;
17667 declarator = cp_parser_declarator (parser,
17668 CP_PARSER_DECLARATOR_EITHER,
17669 /*ctor_dtor_or_conv_p=*/NULL,
17670 parenthesized_p,
17671 /*member_p=*/false);
17672 parser->default_arg_ok_p = saved_default_arg_ok_p;
17673 /* After the declarator, allow more attributes. */
17674 decl_specifiers.attributes
17675 = chainon (decl_specifiers.attributes,
17676 cp_parser_attributes_opt (parser));
17679 /* If the next token is an ellipsis, and we have not seen a
17680 declarator name, and the type of the declarator contains parameter
17681 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17682 a parameter pack expansion expression. Otherwise, leave the
17683 ellipsis for a C-style variadic function. */
17684 token = cp_lexer_peek_token (parser->lexer);
17685 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17687 tree type = decl_specifiers.type;
17689 if (type && DECL_P (type))
17690 type = TREE_TYPE (type);
17692 if (type
17693 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17694 && declarator_can_be_parameter_pack (declarator)
17695 && (!declarator || !declarator->parameter_pack_p)
17696 && uses_parameter_packs (type))
17698 /* Consume the `...'. */
17699 cp_lexer_consume_token (parser->lexer);
17700 maybe_warn_variadic_templates ();
17702 /* Build a pack expansion type */
17703 if (declarator)
17704 declarator->parameter_pack_p = true;
17705 else
17706 decl_specifiers.type = make_pack_expansion (type);
17710 /* The restriction on defining new types applies only to the type
17711 of the parameter, not to the default argument. */
17712 parser->type_definition_forbidden_message = saved_message;
17714 /* If the next token is `=', then process a default argument. */
17715 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17717 token = cp_lexer_peek_token (parser->lexer);
17718 /* If we are defining a class, then the tokens that make up the
17719 default argument must be saved and processed later. */
17720 if (!template_parm_p && at_class_scope_p ()
17721 && TYPE_BEING_DEFINED (current_class_type)
17722 && !LAMBDA_TYPE_P (current_class_type))
17723 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17724 /* Outside of a class definition, we can just parse the
17725 assignment-expression. */
17726 else
17727 default_argument
17728 = cp_parser_default_argument (parser, template_parm_p);
17730 if (!parser->default_arg_ok_p)
17732 if (flag_permissive)
17733 warning (0, "deprecated use of default argument for parameter of non-function");
17734 else
17736 error_at (token->location,
17737 "default arguments are only "
17738 "permitted for function parameters");
17739 default_argument = NULL_TREE;
17742 else if ((declarator && declarator->parameter_pack_p)
17743 || (decl_specifiers.type
17744 && PACK_EXPANSION_P (decl_specifiers.type)))
17746 /* Find the name of the parameter pack. */
17747 cp_declarator *id_declarator = declarator;
17748 while (id_declarator && id_declarator->kind != cdk_id)
17749 id_declarator = id_declarator->declarator;
17751 if (id_declarator && id_declarator->kind == cdk_id)
17752 error_at (declarator_token_start->location,
17753 template_parm_p
17754 ? G_("template parameter pack %qD "
17755 "cannot have a default argument")
17756 : G_("parameter pack %qD cannot have "
17757 "a default argument"),
17758 id_declarator->u.id.unqualified_name);
17759 else
17760 error_at (declarator_token_start->location,
17761 template_parm_p
17762 ? G_("template parameter pack cannot have "
17763 "a default argument")
17764 : G_("parameter pack cannot have a "
17765 "default argument"));
17767 default_argument = NULL_TREE;
17770 else
17771 default_argument = NULL_TREE;
17773 return make_parameter_declarator (&decl_specifiers,
17774 declarator,
17775 default_argument);
17778 /* Parse a default argument and return it.
17780 TEMPLATE_PARM_P is true if this is a default argument for a
17781 non-type template parameter. */
17782 static tree
17783 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17785 tree default_argument = NULL_TREE;
17786 bool saved_greater_than_is_operator_p;
17787 bool saved_local_variables_forbidden_p;
17788 bool non_constant_p, is_direct_init;
17790 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17791 set correctly. */
17792 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17793 parser->greater_than_is_operator_p = !template_parm_p;
17794 /* Local variable names (and the `this' keyword) may not
17795 appear in a default argument. */
17796 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17797 parser->local_variables_forbidden_p = true;
17798 /* Parse the assignment-expression. */
17799 if (template_parm_p)
17800 push_deferring_access_checks (dk_no_deferred);
17801 default_argument
17802 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17803 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17804 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17805 if (template_parm_p)
17806 pop_deferring_access_checks ();
17807 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17808 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17810 return default_argument;
17813 /* Parse a function-body.
17815 function-body:
17816 compound_statement */
17818 static void
17819 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17821 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17824 /* Parse a ctor-initializer-opt followed by a function-body. Return
17825 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17826 is true we are parsing a function-try-block. */
17828 static bool
17829 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17830 bool in_function_try_block)
17832 tree body, list;
17833 bool ctor_initializer_p;
17834 const bool check_body_p =
17835 DECL_CONSTRUCTOR_P (current_function_decl)
17836 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17837 tree last = NULL;
17839 /* Begin the function body. */
17840 body = begin_function_body ();
17841 /* Parse the optional ctor-initializer. */
17842 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17844 /* If we're parsing a constexpr constructor definition, we need
17845 to check that the constructor body is indeed empty. However,
17846 before we get to cp_parser_function_body lot of junk has been
17847 generated, so we can't just check that we have an empty block.
17848 Rather we take a snapshot of the outermost block, and check whether
17849 cp_parser_function_body changed its state. */
17850 if (check_body_p)
17852 list = cur_stmt_list;
17853 if (STATEMENT_LIST_TAIL (list))
17854 last = STATEMENT_LIST_TAIL (list)->stmt;
17856 /* Parse the function-body. */
17857 cp_parser_function_body (parser, in_function_try_block);
17858 if (check_body_p)
17859 check_constexpr_ctor_body (last, list);
17860 /* Finish the function body. */
17861 finish_function_body (body);
17863 return ctor_initializer_p;
17866 /* Parse an initializer.
17868 initializer:
17869 = initializer-clause
17870 ( expression-list )
17872 Returns an expression representing the initializer. If no
17873 initializer is present, NULL_TREE is returned.
17875 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17876 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17877 set to TRUE if there is no initializer present. If there is an
17878 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17879 is set to true; otherwise it is set to false. */
17881 static tree
17882 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17883 bool* non_constant_p)
17885 cp_token *token;
17886 tree init;
17888 /* Peek at the next token. */
17889 token = cp_lexer_peek_token (parser->lexer);
17891 /* Let our caller know whether or not this initializer was
17892 parenthesized. */
17893 *is_direct_init = (token->type != CPP_EQ);
17894 /* Assume that the initializer is constant. */
17895 *non_constant_p = false;
17897 if (token->type == CPP_EQ)
17899 /* Consume the `='. */
17900 cp_lexer_consume_token (parser->lexer);
17901 /* Parse the initializer-clause. */
17902 init = cp_parser_initializer_clause (parser, non_constant_p);
17904 else if (token->type == CPP_OPEN_PAREN)
17906 vec<tree, va_gc> *vec;
17907 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17908 /*cast_p=*/false,
17909 /*allow_expansion_p=*/true,
17910 non_constant_p);
17911 if (vec == NULL)
17912 return error_mark_node;
17913 init = build_tree_list_vec (vec);
17914 release_tree_vector (vec);
17916 else if (token->type == CPP_OPEN_BRACE)
17918 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17919 init = cp_parser_braced_list (parser, non_constant_p);
17920 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17922 else
17924 /* Anything else is an error. */
17925 cp_parser_error (parser, "expected initializer");
17926 init = error_mark_node;
17929 return init;
17932 /* Parse an initializer-clause.
17934 initializer-clause:
17935 assignment-expression
17936 braced-init-list
17938 Returns an expression representing the initializer.
17940 If the `assignment-expression' production is used the value
17941 returned is simply a representation for the expression.
17943 Otherwise, calls cp_parser_braced_list. */
17945 static tree
17946 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17948 tree initializer;
17950 /* Assume the expression is constant. */
17951 *non_constant_p = false;
17953 /* If it is not a `{', then we are looking at an
17954 assignment-expression. */
17955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17957 initializer
17958 = cp_parser_constant_expression (parser,
17959 /*allow_non_constant_p=*/true,
17960 non_constant_p);
17962 else
17963 initializer = cp_parser_braced_list (parser, non_constant_p);
17965 return initializer;
17968 /* Parse a brace-enclosed initializer list.
17970 braced-init-list:
17971 { initializer-list , [opt] }
17974 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17975 the elements of the initializer-list (or NULL, if the last
17976 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17977 NULL_TREE. There is no way to detect whether or not the optional
17978 trailing `,' was provided. NON_CONSTANT_P is as for
17979 cp_parser_initializer. */
17981 static tree
17982 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17984 tree initializer;
17986 /* Consume the `{' token. */
17987 cp_lexer_consume_token (parser->lexer);
17988 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17989 initializer = make_node (CONSTRUCTOR);
17990 /* If it's not a `}', then there is a non-trivial initializer. */
17991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17993 /* Parse the initializer list. */
17994 CONSTRUCTOR_ELTS (initializer)
17995 = cp_parser_initializer_list (parser, non_constant_p);
17996 /* A trailing `,' token is allowed. */
17997 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17998 cp_lexer_consume_token (parser->lexer);
18000 else
18001 *non_constant_p = false;
18002 /* Now, there should be a trailing `}'. */
18003 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18004 TREE_TYPE (initializer) = init_list_type_node;
18005 return initializer;
18008 /* Parse an initializer-list.
18010 initializer-list:
18011 initializer-clause ... [opt]
18012 initializer-list , initializer-clause ... [opt]
18014 GNU Extension:
18016 initializer-list:
18017 designation initializer-clause ...[opt]
18018 initializer-list , designation initializer-clause ...[opt]
18020 designation:
18021 . identifier =
18022 identifier :
18023 [ constant-expression ] =
18025 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18026 for the initializer. If the INDEX of the elt is non-NULL, it is the
18027 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18028 as for cp_parser_initializer. */
18030 static vec<constructor_elt, va_gc> *
18031 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18033 vec<constructor_elt, va_gc> *v = NULL;
18035 /* Assume all of the expressions are constant. */
18036 *non_constant_p = false;
18038 /* Parse the rest of the list. */
18039 while (true)
18041 cp_token *token;
18042 tree designator;
18043 tree initializer;
18044 bool clause_non_constant_p;
18046 /* If the next token is an identifier and the following one is a
18047 colon, we are looking at the GNU designated-initializer
18048 syntax. */
18049 if (cp_parser_allow_gnu_extensions_p (parser)
18050 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18051 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18053 /* Warn the user that they are using an extension. */
18054 pedwarn (input_location, OPT_Wpedantic,
18055 "ISO C++ does not allow designated initializers");
18056 /* Consume the identifier. */
18057 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18058 /* Consume the `:'. */
18059 cp_lexer_consume_token (parser->lexer);
18061 /* Also handle the C99 syntax, '. id ='. */
18062 else if (cp_parser_allow_gnu_extensions_p (parser)
18063 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18064 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18065 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18067 /* Warn the user that they are using an extension. */
18068 pedwarn (input_location, OPT_Wpedantic,
18069 "ISO C++ does not allow C99 designated initializers");
18070 /* Consume the `.'. */
18071 cp_lexer_consume_token (parser->lexer);
18072 /* Consume the identifier. */
18073 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18074 /* Consume the `='. */
18075 cp_lexer_consume_token (parser->lexer);
18077 /* Also handle C99 array designators, '[ const ] ='. */
18078 else if (cp_parser_allow_gnu_extensions_p (parser)
18079 && !c_dialect_objc ()
18080 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18082 /* In C++11, [ could start a lambda-introducer. */
18083 bool non_const = false;
18085 cp_parser_parse_tentatively (parser);
18086 cp_lexer_consume_token (parser->lexer);
18087 designator = cp_parser_constant_expression (parser, true, &non_const);
18088 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18089 cp_parser_require (parser, CPP_EQ, RT_EQ);
18090 if (!cp_parser_parse_definitely (parser))
18091 designator = NULL_TREE;
18092 else if (non_const)
18093 require_potential_rvalue_constant_expression (designator);
18095 else
18096 designator = NULL_TREE;
18098 /* Parse the initializer. */
18099 initializer = cp_parser_initializer_clause (parser,
18100 &clause_non_constant_p);
18101 /* If any clause is non-constant, so is the entire initializer. */
18102 if (clause_non_constant_p)
18103 *non_constant_p = true;
18105 /* If we have an ellipsis, this is an initializer pack
18106 expansion. */
18107 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18109 /* Consume the `...'. */
18110 cp_lexer_consume_token (parser->lexer);
18112 /* Turn the initializer into an initializer expansion. */
18113 initializer = make_pack_expansion (initializer);
18116 /* Add it to the vector. */
18117 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18119 /* If the next token is not a comma, we have reached the end of
18120 the list. */
18121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18122 break;
18124 /* Peek at the next token. */
18125 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18126 /* If the next token is a `}', then we're still done. An
18127 initializer-clause can have a trailing `,' after the
18128 initializer-list and before the closing `}'. */
18129 if (token->type == CPP_CLOSE_BRACE)
18130 break;
18132 /* Consume the `,' token. */
18133 cp_lexer_consume_token (parser->lexer);
18136 return v;
18139 /* Classes [gram.class] */
18141 /* Parse a class-name.
18143 class-name:
18144 identifier
18145 template-id
18147 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18148 to indicate that names looked up in dependent types should be
18149 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18150 keyword has been used to indicate that the name that appears next
18151 is a template. TAG_TYPE indicates the explicit tag given before
18152 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18153 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18154 is the class being defined in a class-head.
18156 Returns the TYPE_DECL representing the class. */
18158 static tree
18159 cp_parser_class_name (cp_parser *parser,
18160 bool typename_keyword_p,
18161 bool template_keyword_p,
18162 enum tag_types tag_type,
18163 bool check_dependency_p,
18164 bool class_head_p,
18165 bool is_declaration)
18167 tree decl;
18168 tree scope;
18169 bool typename_p;
18170 cp_token *token;
18171 tree identifier = NULL_TREE;
18173 /* All class-names start with an identifier. */
18174 token = cp_lexer_peek_token (parser->lexer);
18175 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18177 cp_parser_error (parser, "expected class-name");
18178 return error_mark_node;
18181 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18182 to a template-id, so we save it here. */
18183 scope = parser->scope;
18184 if (scope == error_mark_node)
18185 return error_mark_node;
18187 /* Any name names a type if we're following the `typename' keyword
18188 in a qualified name where the enclosing scope is type-dependent. */
18189 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18190 && dependent_type_p (scope));
18191 /* Handle the common case (an identifier, but not a template-id)
18192 efficiently. */
18193 if (token->type == CPP_NAME
18194 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18196 cp_token *identifier_token;
18197 bool ambiguous_p;
18199 /* Look for the identifier. */
18200 identifier_token = cp_lexer_peek_token (parser->lexer);
18201 ambiguous_p = identifier_token->ambiguous_p;
18202 identifier = cp_parser_identifier (parser);
18203 /* If the next token isn't an identifier, we are certainly not
18204 looking at a class-name. */
18205 if (identifier == error_mark_node)
18206 decl = error_mark_node;
18207 /* If we know this is a type-name, there's no need to look it
18208 up. */
18209 else if (typename_p)
18210 decl = identifier;
18211 else
18213 tree ambiguous_decls;
18214 /* If we already know that this lookup is ambiguous, then
18215 we've already issued an error message; there's no reason
18216 to check again. */
18217 if (ambiguous_p)
18219 cp_parser_simulate_error (parser);
18220 return error_mark_node;
18222 /* If the next token is a `::', then the name must be a type
18223 name.
18225 [basic.lookup.qual]
18227 During the lookup for a name preceding the :: scope
18228 resolution operator, object, function, and enumerator
18229 names are ignored. */
18230 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18231 tag_type = typename_type;
18232 /* Look up the name. */
18233 decl = cp_parser_lookup_name (parser, identifier,
18234 tag_type,
18235 /*is_template=*/false,
18236 /*is_namespace=*/false,
18237 check_dependency_p,
18238 &ambiguous_decls,
18239 identifier_token->location);
18240 if (ambiguous_decls)
18242 if (cp_parser_parsing_tentatively (parser))
18243 cp_parser_simulate_error (parser);
18244 return error_mark_node;
18248 else
18250 /* Try a template-id. */
18251 decl = cp_parser_template_id (parser, template_keyword_p,
18252 check_dependency_p,
18253 tag_type,
18254 is_declaration);
18255 if (decl == error_mark_node)
18256 return error_mark_node;
18259 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18261 /* If this is a typename, create a TYPENAME_TYPE. */
18262 if (typename_p && decl != error_mark_node)
18264 decl = make_typename_type (scope, decl, typename_type,
18265 /*complain=*/tf_error);
18266 if (decl != error_mark_node)
18267 decl = TYPE_NAME (decl);
18270 decl = strip_using_decl (decl);
18272 /* Check to see that it is really the name of a class. */
18273 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18274 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
18275 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18276 /* Situations like this:
18278 template <typename T> struct A {
18279 typename T::template X<int>::I i;
18282 are problematic. Is `T::template X<int>' a class-name? The
18283 standard does not seem to be definitive, but there is no other
18284 valid interpretation of the following `::'. Therefore, those
18285 names are considered class-names. */
18287 decl = make_typename_type (scope, decl, tag_type, tf_error);
18288 if (decl != error_mark_node)
18289 decl = TYPE_NAME (decl);
18291 else if (TREE_CODE (decl) != TYPE_DECL
18292 || TREE_TYPE (decl) == error_mark_node
18293 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18294 /* In Objective-C 2.0, a classname followed by '.' starts a
18295 dot-syntax expression, and it's not a type-name. */
18296 || (c_dialect_objc ()
18297 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18298 && objc_is_class_name (decl)))
18299 decl = error_mark_node;
18301 if (decl == error_mark_node)
18302 cp_parser_error (parser, "expected class-name");
18303 else if (identifier && !parser->scope)
18304 maybe_note_name_used_in_class (identifier, decl);
18306 return decl;
18309 /* Parse a class-specifier.
18311 class-specifier:
18312 class-head { member-specification [opt] }
18314 Returns the TREE_TYPE representing the class. */
18316 static tree
18317 cp_parser_class_specifier_1 (cp_parser* parser)
18319 tree type;
18320 tree attributes = NULL_TREE;
18321 bool nested_name_specifier_p;
18322 unsigned saved_num_template_parameter_lists;
18323 bool saved_in_function_body;
18324 unsigned char in_statement;
18325 bool in_switch_statement_p;
18326 bool saved_in_unbraced_linkage_specification_p;
18327 tree old_scope = NULL_TREE;
18328 tree scope = NULL_TREE;
18329 cp_token *closing_brace;
18331 push_deferring_access_checks (dk_no_deferred);
18333 /* Parse the class-head. */
18334 type = cp_parser_class_head (parser,
18335 &nested_name_specifier_p);
18336 /* If the class-head was a semantic disaster, skip the entire body
18337 of the class. */
18338 if (!type)
18340 cp_parser_skip_to_end_of_block_or_statement (parser);
18341 pop_deferring_access_checks ();
18342 return error_mark_node;
18345 /* Look for the `{'. */
18346 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18348 pop_deferring_access_checks ();
18349 return error_mark_node;
18352 /* Issue an error message if type-definitions are forbidden here. */
18353 cp_parser_check_type_definition (parser);
18354 /* Remember that we are defining one more class. */
18355 ++parser->num_classes_being_defined;
18356 /* Inside the class, surrounding template-parameter-lists do not
18357 apply. */
18358 saved_num_template_parameter_lists
18359 = parser->num_template_parameter_lists;
18360 parser->num_template_parameter_lists = 0;
18361 /* We are not in a function body. */
18362 saved_in_function_body = parser->in_function_body;
18363 parser->in_function_body = false;
18364 /* Or in a loop. */
18365 in_statement = parser->in_statement;
18366 parser->in_statement = 0;
18367 /* Or in a switch. */
18368 in_switch_statement_p = parser->in_switch_statement_p;
18369 parser->in_switch_statement_p = false;
18370 /* We are not immediately inside an extern "lang" block. */
18371 saved_in_unbraced_linkage_specification_p
18372 = parser->in_unbraced_linkage_specification_p;
18373 parser->in_unbraced_linkage_specification_p = false;
18375 /* Start the class. */
18376 if (nested_name_specifier_p)
18378 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18379 old_scope = push_inner_scope (scope);
18381 type = begin_class_definition (type);
18383 if (type == error_mark_node)
18384 /* If the type is erroneous, skip the entire body of the class. */
18385 cp_parser_skip_to_closing_brace (parser);
18386 else
18387 /* Parse the member-specification. */
18388 cp_parser_member_specification_opt (parser);
18390 /* Look for the trailing `}'. */
18391 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18392 /* Look for trailing attributes to apply to this class. */
18393 if (cp_parser_allow_gnu_extensions_p (parser))
18394 attributes = cp_parser_gnu_attributes_opt (parser);
18395 if (type != error_mark_node)
18396 type = finish_struct (type, attributes);
18397 if (nested_name_specifier_p)
18398 pop_inner_scope (old_scope, scope);
18400 /* We've finished a type definition. Check for the common syntax
18401 error of forgetting a semicolon after the definition. We need to
18402 be careful, as we can't just check for not-a-semicolon and be done
18403 with it; the user might have typed:
18405 class X { } c = ...;
18406 class X { } *p = ...;
18408 and so forth. Instead, enumerate all the possible tokens that
18409 might follow this production; if we don't see one of them, then
18410 complain and silently insert the semicolon. */
18412 cp_token *token = cp_lexer_peek_token (parser->lexer);
18413 bool want_semicolon = true;
18415 if (cp_next_tokens_can_be_std_attribute_p (parser))
18416 /* Don't try to parse c++11 attributes here. As per the
18417 grammar, that should be a task for
18418 cp_parser_decl_specifier_seq. */
18419 want_semicolon = false;
18421 switch (token->type)
18423 case CPP_NAME:
18424 case CPP_SEMICOLON:
18425 case CPP_MULT:
18426 case CPP_AND:
18427 case CPP_OPEN_PAREN:
18428 case CPP_CLOSE_PAREN:
18429 case CPP_COMMA:
18430 want_semicolon = false;
18431 break;
18433 /* While it's legal for type qualifiers and storage class
18434 specifiers to follow type definitions in the grammar, only
18435 compiler testsuites contain code like that. Assume that if
18436 we see such code, then what we're really seeing is a case
18437 like:
18439 class X { }
18440 const <type> var = ...;
18444 class Y { }
18445 static <type> func (...) ...
18447 i.e. the qualifier or specifier applies to the next
18448 declaration. To do so, however, we need to look ahead one
18449 more token to see if *that* token is a type specifier.
18451 This code could be improved to handle:
18453 class Z { }
18454 static const <type> var = ...; */
18455 case CPP_KEYWORD:
18456 if (keyword_is_decl_specifier (token->keyword))
18458 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18460 /* Handling user-defined types here would be nice, but very
18461 tricky. */
18462 want_semicolon
18463 = (lookahead->type == CPP_KEYWORD
18464 && keyword_begins_type_specifier (lookahead->keyword));
18466 break;
18467 default:
18468 break;
18471 /* If we don't have a type, then something is very wrong and we
18472 shouldn't try to do anything clever. Likewise for not seeing the
18473 closing brace. */
18474 if (closing_brace && TYPE_P (type) && want_semicolon)
18476 cp_token_position prev
18477 = cp_lexer_previous_token_position (parser->lexer);
18478 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18479 location_t loc = prev_token->location;
18481 if (CLASSTYPE_DECLARED_CLASS (type))
18482 error_at (loc, "expected %<;%> after class definition");
18483 else if (TREE_CODE (type) == RECORD_TYPE)
18484 error_at (loc, "expected %<;%> after struct definition");
18485 else if (TREE_CODE (type) == UNION_TYPE)
18486 error_at (loc, "expected %<;%> after union definition");
18487 else
18488 gcc_unreachable ();
18490 /* Unget one token and smash it to look as though we encountered
18491 a semicolon in the input stream. */
18492 cp_lexer_set_token_position (parser->lexer, prev);
18493 token = cp_lexer_peek_token (parser->lexer);
18494 token->type = CPP_SEMICOLON;
18495 token->keyword = RID_MAX;
18499 /* If this class is not itself within the scope of another class,
18500 then we need to parse the bodies of all of the queued function
18501 definitions. Note that the queued functions defined in a class
18502 are not always processed immediately following the
18503 class-specifier for that class. Consider:
18505 struct A {
18506 struct B { void f() { sizeof (A); } };
18509 If `f' were processed before the processing of `A' were
18510 completed, there would be no way to compute the size of `A'.
18511 Note that the nesting we are interested in here is lexical --
18512 not the semantic nesting given by TYPE_CONTEXT. In particular,
18513 for:
18515 struct A { struct B; };
18516 struct A::B { void f() { } };
18518 there is no need to delay the parsing of `A::B::f'. */
18519 if (--parser->num_classes_being_defined == 0)
18521 tree decl;
18522 tree class_type = NULL_TREE;
18523 tree pushed_scope = NULL_TREE;
18524 unsigned ix;
18525 cp_default_arg_entry *e;
18526 tree save_ccp, save_ccr;
18528 /* In a first pass, parse default arguments to the functions.
18529 Then, in a second pass, parse the bodies of the functions.
18530 This two-phased approach handles cases like:
18532 struct S {
18533 void f() { g(); }
18534 void g(int i = 3);
18538 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18540 decl = e->decl;
18541 /* If there are default arguments that have not yet been processed,
18542 take care of them now. */
18543 if (class_type != e->class_type)
18545 if (pushed_scope)
18546 pop_scope (pushed_scope);
18547 class_type = e->class_type;
18548 pushed_scope = push_scope (class_type);
18550 /* Make sure that any template parameters are in scope. */
18551 maybe_begin_member_template_processing (decl);
18552 /* Parse the default argument expressions. */
18553 cp_parser_late_parsing_default_args (parser, decl);
18554 /* Remove any template parameters from the symbol table. */
18555 maybe_end_member_template_processing ();
18557 vec_safe_truncate (unparsed_funs_with_default_args, 0);
18558 /* Now parse any NSDMIs. */
18559 save_ccp = current_class_ptr;
18560 save_ccr = current_class_ref;
18561 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18563 if (class_type != DECL_CONTEXT (decl))
18565 if (pushed_scope)
18566 pop_scope (pushed_scope);
18567 class_type = DECL_CONTEXT (decl);
18568 pushed_scope = push_scope (class_type);
18570 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18571 cp_parser_late_parsing_nsdmi (parser, decl);
18573 vec_safe_truncate (unparsed_nsdmis, 0);
18574 current_class_ptr = save_ccp;
18575 current_class_ref = save_ccr;
18576 if (pushed_scope)
18577 pop_scope (pushed_scope);
18578 /* Now parse the body of the functions. */
18579 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
18580 cp_parser_late_parsing_for_member (parser, decl);
18581 vec_safe_truncate (unparsed_funs_with_definitions, 0);
18584 /* Put back any saved access checks. */
18585 pop_deferring_access_checks ();
18587 /* Restore saved state. */
18588 parser->in_switch_statement_p = in_switch_statement_p;
18589 parser->in_statement = in_statement;
18590 parser->in_function_body = saved_in_function_body;
18591 parser->num_template_parameter_lists
18592 = saved_num_template_parameter_lists;
18593 parser->in_unbraced_linkage_specification_p
18594 = saved_in_unbraced_linkage_specification_p;
18596 return type;
18599 static tree
18600 cp_parser_class_specifier (cp_parser* parser)
18602 tree ret;
18603 timevar_push (TV_PARSE_STRUCT);
18604 ret = cp_parser_class_specifier_1 (parser);
18605 timevar_pop (TV_PARSE_STRUCT);
18606 return ret;
18609 /* Parse a class-head.
18611 class-head:
18612 class-key identifier [opt] base-clause [opt]
18613 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18614 class-key nested-name-specifier [opt] template-id
18615 base-clause [opt]
18617 class-virt-specifier:
18618 final
18620 GNU Extensions:
18621 class-key attributes identifier [opt] base-clause [opt]
18622 class-key attributes nested-name-specifier identifier base-clause [opt]
18623 class-key attributes nested-name-specifier [opt] template-id
18624 base-clause [opt]
18626 Upon return BASES is initialized to the list of base classes (or
18627 NULL, if there are none) in the same form returned by
18628 cp_parser_base_clause.
18630 Returns the TYPE of the indicated class. Sets
18631 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18632 involving a nested-name-specifier was used, and FALSE otherwise.
18634 Returns error_mark_node if this is not a class-head.
18636 Returns NULL_TREE if the class-head is syntactically valid, but
18637 semantically invalid in a way that means we should skip the entire
18638 body of the class. */
18640 static tree
18641 cp_parser_class_head (cp_parser* parser,
18642 bool* nested_name_specifier_p)
18644 tree nested_name_specifier;
18645 enum tag_types class_key;
18646 tree id = NULL_TREE;
18647 tree type = NULL_TREE;
18648 tree attributes;
18649 tree bases;
18650 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18651 bool template_id_p = false;
18652 bool qualified_p = false;
18653 bool invalid_nested_name_p = false;
18654 bool invalid_explicit_specialization_p = false;
18655 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18656 tree pushed_scope = NULL_TREE;
18657 unsigned num_templates;
18658 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18659 /* Assume no nested-name-specifier will be present. */
18660 *nested_name_specifier_p = false;
18661 /* Assume no template parameter lists will be used in defining the
18662 type. */
18663 num_templates = 0;
18664 parser->colon_corrects_to_scope_p = false;
18666 /* Look for the class-key. */
18667 class_key = cp_parser_class_key (parser);
18668 if (class_key == none_type)
18669 return error_mark_node;
18671 /* Parse the attributes. */
18672 attributes = cp_parser_attributes_opt (parser);
18674 /* If the next token is `::', that is invalid -- but sometimes
18675 people do try to write:
18677 struct ::S {};
18679 Handle this gracefully by accepting the extra qualifier, and then
18680 issuing an error about it later if this really is a
18681 class-head. If it turns out just to be an elaborated type
18682 specifier, remain silent. */
18683 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18684 qualified_p = true;
18686 push_deferring_access_checks (dk_no_check);
18688 /* Determine the name of the class. Begin by looking for an
18689 optional nested-name-specifier. */
18690 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18691 nested_name_specifier
18692 = cp_parser_nested_name_specifier_opt (parser,
18693 /*typename_keyword_p=*/false,
18694 /*check_dependency_p=*/false,
18695 /*type_p=*/true,
18696 /*is_declaration=*/false);
18697 /* If there was a nested-name-specifier, then there *must* be an
18698 identifier. */
18699 if (nested_name_specifier)
18701 type_start_token = cp_lexer_peek_token (parser->lexer);
18702 /* Although the grammar says `identifier', it really means
18703 `class-name' or `template-name'. You are only allowed to
18704 define a class that has already been declared with this
18705 syntax.
18707 The proposed resolution for Core Issue 180 says that wherever
18708 you see `class T::X' you should treat `X' as a type-name.
18710 It is OK to define an inaccessible class; for example:
18712 class A { class B; };
18713 class A::B {};
18715 We do not know if we will see a class-name, or a
18716 template-name. We look for a class-name first, in case the
18717 class-name is a template-id; if we looked for the
18718 template-name first we would stop after the template-name. */
18719 cp_parser_parse_tentatively (parser);
18720 type = cp_parser_class_name (parser,
18721 /*typename_keyword_p=*/false,
18722 /*template_keyword_p=*/false,
18723 class_type,
18724 /*check_dependency_p=*/false,
18725 /*class_head_p=*/true,
18726 /*is_declaration=*/false);
18727 /* If that didn't work, ignore the nested-name-specifier. */
18728 if (!cp_parser_parse_definitely (parser))
18730 invalid_nested_name_p = true;
18731 type_start_token = cp_lexer_peek_token (parser->lexer);
18732 id = cp_parser_identifier (parser);
18733 if (id == error_mark_node)
18734 id = NULL_TREE;
18736 /* If we could not find a corresponding TYPE, treat this
18737 declaration like an unqualified declaration. */
18738 if (type == error_mark_node)
18739 nested_name_specifier = NULL_TREE;
18740 /* Otherwise, count the number of templates used in TYPE and its
18741 containing scopes. */
18742 else
18744 tree scope;
18746 for (scope = TREE_TYPE (type);
18747 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18748 scope = (TYPE_P (scope)
18749 ? TYPE_CONTEXT (scope)
18750 : DECL_CONTEXT (scope)))
18751 if (TYPE_P (scope)
18752 && CLASS_TYPE_P (scope)
18753 && CLASSTYPE_TEMPLATE_INFO (scope)
18754 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18755 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
18756 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
18757 ++num_templates;
18760 /* Otherwise, the identifier is optional. */
18761 else
18763 /* We don't know whether what comes next is a template-id,
18764 an identifier, or nothing at all. */
18765 cp_parser_parse_tentatively (parser);
18766 /* Check for a template-id. */
18767 type_start_token = cp_lexer_peek_token (parser->lexer);
18768 id = cp_parser_template_id (parser,
18769 /*template_keyword_p=*/false,
18770 /*check_dependency_p=*/true,
18771 class_key,
18772 /*is_declaration=*/true);
18773 /* If that didn't work, it could still be an identifier. */
18774 if (!cp_parser_parse_definitely (parser))
18776 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18778 type_start_token = cp_lexer_peek_token (parser->lexer);
18779 id = cp_parser_identifier (parser);
18781 else
18782 id = NULL_TREE;
18784 else
18786 template_id_p = true;
18787 ++num_templates;
18791 pop_deferring_access_checks ();
18793 if (id)
18795 cp_parser_check_for_invalid_template_id (parser, id,
18796 class_key,
18797 type_start_token->location);
18799 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18801 /* If it's not a `:' or a `{' then we can't really be looking at a
18802 class-head, since a class-head only appears as part of a
18803 class-specifier. We have to detect this situation before calling
18804 xref_tag, since that has irreversible side-effects. */
18805 if (!cp_parser_next_token_starts_class_definition_p (parser))
18807 cp_parser_error (parser, "expected %<{%> or %<:%>");
18808 type = error_mark_node;
18809 goto out;
18812 /* At this point, we're going ahead with the class-specifier, even
18813 if some other problem occurs. */
18814 cp_parser_commit_to_tentative_parse (parser);
18815 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18817 cp_parser_error (parser,
18818 "cannot specify %<override%> for a class");
18819 type = error_mark_node;
18820 goto out;
18822 /* Issue the error about the overly-qualified name now. */
18823 if (qualified_p)
18825 cp_parser_error (parser,
18826 "global qualification of class name is invalid");
18827 type = error_mark_node;
18828 goto out;
18830 else if (invalid_nested_name_p)
18832 cp_parser_error (parser,
18833 "qualified name does not name a class");
18834 type = error_mark_node;
18835 goto out;
18837 else if (nested_name_specifier)
18839 tree scope;
18841 /* Reject typedef-names in class heads. */
18842 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18844 error_at (type_start_token->location,
18845 "invalid class name in declaration of %qD",
18846 type);
18847 type = NULL_TREE;
18848 goto done;
18851 /* Figure out in what scope the declaration is being placed. */
18852 scope = current_scope ();
18853 /* If that scope does not contain the scope in which the
18854 class was originally declared, the program is invalid. */
18855 if (scope && !is_ancestor (scope, nested_name_specifier))
18857 if (at_namespace_scope_p ())
18858 error_at (type_start_token->location,
18859 "declaration of %qD in namespace %qD which does not "
18860 "enclose %qD",
18861 type, scope, nested_name_specifier);
18862 else
18863 error_at (type_start_token->location,
18864 "declaration of %qD in %qD which does not enclose %qD",
18865 type, scope, nested_name_specifier);
18866 type = NULL_TREE;
18867 goto done;
18869 /* [dcl.meaning]
18871 A declarator-id shall not be qualified except for the
18872 definition of a ... nested class outside of its class
18873 ... [or] the definition or explicit instantiation of a
18874 class member of a namespace outside of its namespace. */
18875 if (scope == nested_name_specifier)
18877 permerror (nested_name_specifier_token_start->location,
18878 "extra qualification not allowed");
18879 nested_name_specifier = NULL_TREE;
18880 num_templates = 0;
18883 /* An explicit-specialization must be preceded by "template <>". If
18884 it is not, try to recover gracefully. */
18885 if (at_namespace_scope_p ()
18886 && parser->num_template_parameter_lists == 0
18887 && template_id_p)
18889 error_at (type_start_token->location,
18890 "an explicit specialization must be preceded by %<template <>%>");
18891 invalid_explicit_specialization_p = true;
18892 /* Take the same action that would have been taken by
18893 cp_parser_explicit_specialization. */
18894 ++parser->num_template_parameter_lists;
18895 begin_specialization ();
18897 /* There must be no "return" statements between this point and the
18898 end of this function; set "type "to the correct return value and
18899 use "goto done;" to return. */
18900 /* Make sure that the right number of template parameters were
18901 present. */
18902 if (!cp_parser_check_template_parameters (parser, num_templates,
18903 type_start_token->location,
18904 /*declarator=*/NULL))
18906 /* If something went wrong, there is no point in even trying to
18907 process the class-definition. */
18908 type = NULL_TREE;
18909 goto done;
18912 /* Look up the type. */
18913 if (template_id_p)
18915 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18916 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18917 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18919 error_at (type_start_token->location,
18920 "function template %qD redeclared as a class template", id);
18921 type = error_mark_node;
18923 else
18925 type = TREE_TYPE (id);
18926 type = maybe_process_partial_specialization (type);
18928 if (nested_name_specifier)
18929 pushed_scope = push_scope (nested_name_specifier);
18931 else if (nested_name_specifier)
18933 tree class_type;
18935 /* Given:
18937 template <typename T> struct S { struct T };
18938 template <typename T> struct S<T>::T { };
18940 we will get a TYPENAME_TYPE when processing the definition of
18941 `S::T'. We need to resolve it to the actual type before we
18942 try to define it. */
18943 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18945 class_type = resolve_typename_type (TREE_TYPE (type),
18946 /*only_current_p=*/false);
18947 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18948 type = TYPE_NAME (class_type);
18949 else
18951 cp_parser_error (parser, "could not resolve typename type");
18952 type = error_mark_node;
18956 if (maybe_process_partial_specialization (TREE_TYPE (type))
18957 == error_mark_node)
18959 type = NULL_TREE;
18960 goto done;
18963 class_type = current_class_type;
18964 /* Enter the scope indicated by the nested-name-specifier. */
18965 pushed_scope = push_scope (nested_name_specifier);
18966 /* Get the canonical version of this type. */
18967 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18968 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18969 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18971 type = push_template_decl (type);
18972 if (type == error_mark_node)
18974 type = NULL_TREE;
18975 goto done;
18979 type = TREE_TYPE (type);
18980 *nested_name_specifier_p = true;
18982 else /* The name is not a nested name. */
18984 /* If the class was unnamed, create a dummy name. */
18985 if (!id)
18986 id = make_anon_name ();
18987 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18988 parser->num_template_parameter_lists);
18991 /* Indicate whether this class was declared as a `class' or as a
18992 `struct'. */
18993 if (TREE_CODE (type) == RECORD_TYPE)
18994 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18995 cp_parser_check_class_key (class_key, type);
18997 /* If this type was already complete, and we see another definition,
18998 that's an error. */
18999 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19001 error_at (type_start_token->location, "redefinition of %q#T",
19002 type);
19003 error_at (type_start_token->location, "previous definition of %q+#T",
19004 type);
19005 type = NULL_TREE;
19006 goto done;
19008 else if (type == error_mark_node)
19009 type = NULL_TREE;
19011 if (type)
19013 /* Apply attributes now, before any use of the class as a template
19014 argument in its base list. */
19015 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19016 fixup_attribute_variants (type);
19019 /* We will have entered the scope containing the class; the names of
19020 base classes should be looked up in that context. For example:
19022 struct A { struct B {}; struct C; };
19023 struct A::C : B {};
19025 is valid. */
19027 /* Get the list of base-classes, if there is one. */
19028 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19029 bases = cp_parser_base_clause (parser);
19030 else
19031 bases = NULL_TREE;
19033 /* If we're really defining a class, process the base classes.
19034 If they're invalid, fail. */
19035 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19036 && !xref_basetypes (type, bases))
19037 type = NULL_TREE;
19039 done:
19040 /* Leave the scope given by the nested-name-specifier. We will
19041 enter the class scope itself while processing the members. */
19042 if (pushed_scope)
19043 pop_scope (pushed_scope);
19045 if (invalid_explicit_specialization_p)
19047 end_specialization ();
19048 --parser->num_template_parameter_lists;
19051 if (type)
19052 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19053 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19054 CLASSTYPE_FINAL (type) = 1;
19055 out:
19056 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19057 return type;
19060 /* Parse a class-key.
19062 class-key:
19063 class
19064 struct
19065 union
19067 Returns the kind of class-key specified, or none_type to indicate
19068 error. */
19070 static enum tag_types
19071 cp_parser_class_key (cp_parser* parser)
19073 cp_token *token;
19074 enum tag_types tag_type;
19076 /* Look for the class-key. */
19077 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19078 if (!token)
19079 return none_type;
19081 /* Check to see if the TOKEN is a class-key. */
19082 tag_type = cp_parser_token_is_class_key (token);
19083 if (!tag_type)
19084 cp_parser_error (parser, "expected class-key");
19085 return tag_type;
19088 /* Parse an (optional) member-specification.
19090 member-specification:
19091 member-declaration member-specification [opt]
19092 access-specifier : member-specification [opt] */
19094 static void
19095 cp_parser_member_specification_opt (cp_parser* parser)
19097 while (true)
19099 cp_token *token;
19100 enum rid keyword;
19102 /* Peek at the next token. */
19103 token = cp_lexer_peek_token (parser->lexer);
19104 /* If it's a `}', or EOF then we've seen all the members. */
19105 if (token->type == CPP_CLOSE_BRACE
19106 || token->type == CPP_EOF
19107 || token->type == CPP_PRAGMA_EOL)
19108 break;
19110 /* See if this token is a keyword. */
19111 keyword = token->keyword;
19112 switch (keyword)
19114 case RID_PUBLIC:
19115 case RID_PROTECTED:
19116 case RID_PRIVATE:
19117 /* Consume the access-specifier. */
19118 cp_lexer_consume_token (parser->lexer);
19119 /* Remember which access-specifier is active. */
19120 current_access_specifier = token->u.value;
19121 /* Look for the `:'. */
19122 cp_parser_require (parser, CPP_COLON, RT_COLON);
19123 break;
19125 default:
19126 /* Accept #pragmas at class scope. */
19127 if (token->type == CPP_PRAGMA)
19129 cp_parser_pragma (parser, pragma_external);
19130 break;
19133 /* Otherwise, the next construction must be a
19134 member-declaration. */
19135 cp_parser_member_declaration (parser);
19140 /* Parse a member-declaration.
19142 member-declaration:
19143 decl-specifier-seq [opt] member-declarator-list [opt] ;
19144 function-definition ; [opt]
19145 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19146 using-declaration
19147 template-declaration
19148 alias-declaration
19150 member-declarator-list:
19151 member-declarator
19152 member-declarator-list , member-declarator
19154 member-declarator:
19155 declarator pure-specifier [opt]
19156 declarator constant-initializer [opt]
19157 identifier [opt] : constant-expression
19159 GNU Extensions:
19161 member-declaration:
19162 __extension__ member-declaration
19164 member-declarator:
19165 declarator attributes [opt] pure-specifier [opt]
19166 declarator attributes [opt] constant-initializer [opt]
19167 identifier [opt] attributes [opt] : constant-expression
19169 C++0x Extensions:
19171 member-declaration:
19172 static_assert-declaration */
19174 static void
19175 cp_parser_member_declaration (cp_parser* parser)
19177 cp_decl_specifier_seq decl_specifiers;
19178 tree prefix_attributes;
19179 tree decl;
19180 int declares_class_or_enum;
19181 bool friend_p;
19182 cp_token *token = NULL;
19183 cp_token *decl_spec_token_start = NULL;
19184 cp_token *initializer_token_start = NULL;
19185 int saved_pedantic;
19186 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19188 /* Check for the `__extension__' keyword. */
19189 if (cp_parser_extension_opt (parser, &saved_pedantic))
19191 /* Recurse. */
19192 cp_parser_member_declaration (parser);
19193 /* Restore the old value of the PEDANTIC flag. */
19194 pedantic = saved_pedantic;
19196 return;
19199 /* Check for a template-declaration. */
19200 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19202 /* An explicit specialization here is an error condition, and we
19203 expect the specialization handler to detect and report this. */
19204 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19205 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19206 cp_parser_explicit_specialization (parser);
19207 else
19208 cp_parser_template_declaration (parser, /*member_p=*/true);
19210 return;
19213 /* Check for a using-declaration. */
19214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19216 if (cxx_dialect < cxx0x)
19218 /* Parse the using-declaration. */
19219 cp_parser_using_declaration (parser,
19220 /*access_declaration_p=*/false);
19221 return;
19223 else
19225 tree decl;
19226 bool alias_decl_expected;
19227 cp_parser_parse_tentatively (parser);
19228 decl = cp_parser_alias_declaration (parser);
19229 /* Note that if we actually see the '=' token after the
19230 identifier, cp_parser_alias_declaration commits the
19231 tentative parse. In that case, we really expects an
19232 alias-declaration. Otherwise, we expect a using
19233 declaration. */
19234 alias_decl_expected =
19235 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19236 cp_parser_parse_definitely (parser);
19238 if (alias_decl_expected)
19239 finish_member_declaration (decl);
19240 else
19241 cp_parser_using_declaration (parser,
19242 /*access_declaration_p=*/false);
19243 return;
19247 /* Check for @defs. */
19248 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19250 tree ivar, member;
19251 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19252 ivar = ivar_chains;
19253 while (ivar)
19255 member = ivar;
19256 ivar = TREE_CHAIN (member);
19257 TREE_CHAIN (member) = NULL_TREE;
19258 finish_member_declaration (member);
19260 return;
19263 /* If the next token is `static_assert' we have a static assertion. */
19264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19266 cp_parser_static_assert (parser, /*member_p=*/true);
19267 return;
19270 parser->colon_corrects_to_scope_p = false;
19272 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19273 goto out;
19275 /* Parse the decl-specifier-seq. */
19276 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19277 cp_parser_decl_specifier_seq (parser,
19278 CP_PARSER_FLAGS_OPTIONAL,
19279 &decl_specifiers,
19280 &declares_class_or_enum);
19281 /* Check for an invalid type-name. */
19282 if (!decl_specifiers.any_type_specifiers_p
19283 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19284 goto out;
19285 /* If there is no declarator, then the decl-specifier-seq should
19286 specify a type. */
19287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19289 /* If there was no decl-specifier-seq, and the next token is a
19290 `;', then we have something like:
19292 struct S { ; };
19294 [class.mem]
19296 Each member-declaration shall declare at least one member
19297 name of the class. */
19298 if (!decl_specifiers.any_specifiers_p)
19300 cp_token *token = cp_lexer_peek_token (parser->lexer);
19301 if (!in_system_header_at (token->location))
19302 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19304 else
19306 tree type;
19308 /* See if this declaration is a friend. */
19309 friend_p = cp_parser_friend_p (&decl_specifiers);
19310 /* If there were decl-specifiers, check to see if there was
19311 a class-declaration. */
19312 type = check_tag_decl (&decl_specifiers,
19313 /*explicit_type_instantiation_p=*/false);
19314 /* Nested classes have already been added to the class, but
19315 a `friend' needs to be explicitly registered. */
19316 if (friend_p)
19318 /* If the `friend' keyword was present, the friend must
19319 be introduced with a class-key. */
19320 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19321 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19322 "in C++03 a class-key must be used "
19323 "when declaring a friend");
19324 /* In this case:
19326 template <typename T> struct A {
19327 friend struct A<T>::B;
19330 A<T>::B will be represented by a TYPENAME_TYPE, and
19331 therefore not recognized by check_tag_decl. */
19332 if (!type)
19334 type = decl_specifiers.type;
19335 if (type && TREE_CODE (type) == TYPE_DECL)
19336 type = TREE_TYPE (type);
19338 if (!type || !TYPE_P (type))
19339 error_at (decl_spec_token_start->location,
19340 "friend declaration does not name a class or "
19341 "function");
19342 else
19343 make_friend_class (current_class_type, type,
19344 /*complain=*/true);
19346 /* If there is no TYPE, an error message will already have
19347 been issued. */
19348 else if (!type || type == error_mark_node)
19350 /* An anonymous aggregate has to be handled specially; such
19351 a declaration really declares a data member (with a
19352 particular type), as opposed to a nested class. */
19353 else if (ANON_AGGR_TYPE_P (type))
19355 /* C++11 9.5/6. */
19356 if (decl_specifiers.storage_class != sc_none)
19357 error_at (decl_spec_token_start->location,
19358 "a storage class on an anonymous aggregate "
19359 "in class scope is not allowed");
19361 /* Remove constructors and such from TYPE, now that we
19362 know it is an anonymous aggregate. */
19363 fixup_anonymous_aggr (type);
19364 /* And make the corresponding data member. */
19365 decl = build_decl (decl_spec_token_start->location,
19366 FIELD_DECL, NULL_TREE, type);
19367 /* Add it to the class. */
19368 finish_member_declaration (decl);
19370 else
19371 cp_parser_check_access_in_redeclaration
19372 (TYPE_NAME (type),
19373 decl_spec_token_start->location);
19376 else
19378 bool assume_semicolon = false;
19380 /* Clear attributes from the decl_specifiers but keep them
19381 around as prefix attributes that apply them to the entity
19382 being declared. */
19383 prefix_attributes = decl_specifiers.attributes;
19384 decl_specifiers.attributes = NULL_TREE;
19386 /* See if these declarations will be friends. */
19387 friend_p = cp_parser_friend_p (&decl_specifiers);
19389 /* Keep going until we hit the `;' at the end of the
19390 declaration. */
19391 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19393 tree attributes = NULL_TREE;
19394 tree first_attribute;
19396 /* Peek at the next token. */
19397 token = cp_lexer_peek_token (parser->lexer);
19399 /* Check for a bitfield declaration. */
19400 if (token->type == CPP_COLON
19401 || (token->type == CPP_NAME
19402 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19403 == CPP_COLON))
19405 tree identifier;
19406 tree width;
19408 /* Get the name of the bitfield. Note that we cannot just
19409 check TOKEN here because it may have been invalidated by
19410 the call to cp_lexer_peek_nth_token above. */
19411 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19412 identifier = cp_parser_identifier (parser);
19413 else
19414 identifier = NULL_TREE;
19416 /* Consume the `:' token. */
19417 cp_lexer_consume_token (parser->lexer);
19418 /* Get the width of the bitfield. */
19419 width
19420 = cp_parser_constant_expression (parser,
19421 /*allow_non_constant=*/false,
19422 NULL);
19424 /* Look for attributes that apply to the bitfield. */
19425 attributes = cp_parser_attributes_opt (parser);
19426 /* Remember which attributes are prefix attributes and
19427 which are not. */
19428 first_attribute = attributes;
19429 /* Combine the attributes. */
19430 attributes = chainon (prefix_attributes, attributes);
19432 /* Create the bitfield declaration. */
19433 decl = grokbitfield (identifier
19434 ? make_id_declarator (NULL_TREE,
19435 identifier,
19436 sfk_none)
19437 : NULL,
19438 &decl_specifiers,
19439 width,
19440 attributes);
19442 else
19444 cp_declarator *declarator;
19445 tree initializer;
19446 tree asm_specification;
19447 int ctor_dtor_or_conv_p;
19449 /* Parse the declarator. */
19450 declarator
19451 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19452 &ctor_dtor_or_conv_p,
19453 /*parenthesized_p=*/NULL,
19454 /*member_p=*/true);
19456 /* If something went wrong parsing the declarator, make sure
19457 that we at least consume some tokens. */
19458 if (declarator == cp_error_declarator)
19460 /* Skip to the end of the statement. */
19461 cp_parser_skip_to_end_of_statement (parser);
19462 /* If the next token is not a semicolon, that is
19463 probably because we just skipped over the body of
19464 a function. So, we consume a semicolon if
19465 present, but do not issue an error message if it
19466 is not present. */
19467 if (cp_lexer_next_token_is (parser->lexer,
19468 CPP_SEMICOLON))
19469 cp_lexer_consume_token (parser->lexer);
19470 goto out;
19473 if (declares_class_or_enum & 2)
19474 cp_parser_check_for_definition_in_return_type
19475 (declarator, decl_specifiers.type,
19476 decl_specifiers.locations[ds_type_spec]);
19478 /* Look for an asm-specification. */
19479 asm_specification = cp_parser_asm_specification_opt (parser);
19480 /* Look for attributes that apply to the declaration. */
19481 attributes = cp_parser_attributes_opt (parser);
19482 /* Remember which attributes are prefix attributes and
19483 which are not. */
19484 first_attribute = attributes;
19485 /* Combine the attributes. */
19486 attributes = chainon (prefix_attributes, attributes);
19488 /* If it's an `=', then we have a constant-initializer or a
19489 pure-specifier. It is not correct to parse the
19490 initializer before registering the member declaration
19491 since the member declaration should be in scope while
19492 its initializer is processed. However, the rest of the
19493 front end does not yet provide an interface that allows
19494 us to handle this correctly. */
19495 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19497 /* In [class.mem]:
19499 A pure-specifier shall be used only in the declaration of
19500 a virtual function.
19502 A member-declarator can contain a constant-initializer
19503 only if it declares a static member of integral or
19504 enumeration type.
19506 Therefore, if the DECLARATOR is for a function, we look
19507 for a pure-specifier; otherwise, we look for a
19508 constant-initializer. When we call `grokfield', it will
19509 perform more stringent semantics checks. */
19510 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19511 if (function_declarator_p (declarator)
19512 || (decl_specifiers.type
19513 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19514 && declarator->kind == cdk_id
19515 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19516 == FUNCTION_TYPE)))
19517 initializer = cp_parser_pure_specifier (parser);
19518 else if (decl_specifiers.storage_class != sc_static)
19519 initializer = cp_parser_save_nsdmi (parser);
19520 else if (cxx_dialect >= cxx0x)
19522 bool nonconst;
19523 /* Don't require a constant rvalue in C++11, since we
19524 might want a reference constant. We'll enforce
19525 constancy later. */
19526 cp_lexer_consume_token (parser->lexer);
19527 /* Parse the initializer. */
19528 initializer = cp_parser_initializer_clause (parser,
19529 &nonconst);
19531 else
19532 /* Parse the initializer. */
19533 initializer = cp_parser_constant_initializer (parser);
19535 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19536 && !function_declarator_p (declarator))
19538 bool x;
19539 if (decl_specifiers.storage_class != sc_static)
19540 initializer = cp_parser_save_nsdmi (parser);
19541 else
19542 initializer = cp_parser_initializer (parser, &x, &x);
19544 /* Otherwise, there is no initializer. */
19545 else
19546 initializer = NULL_TREE;
19548 /* See if we are probably looking at a function
19549 definition. We are certainly not looking at a
19550 member-declarator. Calling `grokfield' has
19551 side-effects, so we must not do it unless we are sure
19552 that we are looking at a member-declarator. */
19553 if (cp_parser_token_starts_function_definition_p
19554 (cp_lexer_peek_token (parser->lexer)))
19556 /* The grammar does not allow a pure-specifier to be
19557 used when a member function is defined. (It is
19558 possible that this fact is an oversight in the
19559 standard, since a pure function may be defined
19560 outside of the class-specifier. */
19561 if (initializer && initializer_token_start)
19562 error_at (initializer_token_start->location,
19563 "pure-specifier on function-definition");
19564 decl = cp_parser_save_member_function_body (parser,
19565 &decl_specifiers,
19566 declarator,
19567 attributes);
19568 /* If the member was not a friend, declare it here. */
19569 if (!friend_p)
19570 finish_member_declaration (decl);
19571 /* Peek at the next token. */
19572 token = cp_lexer_peek_token (parser->lexer);
19573 /* If the next token is a semicolon, consume it. */
19574 if (token->type == CPP_SEMICOLON)
19575 cp_lexer_consume_token (parser->lexer);
19576 goto out;
19578 else
19579 if (declarator->kind == cdk_function)
19580 declarator->id_loc = token->location;
19581 /* Create the declaration. */
19582 decl = grokfield (declarator, &decl_specifiers,
19583 initializer, /*init_const_expr_p=*/true,
19584 asm_specification,
19585 attributes);
19588 /* Reset PREFIX_ATTRIBUTES. */
19589 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19590 attributes = TREE_CHAIN (attributes);
19591 if (attributes)
19592 TREE_CHAIN (attributes) = NULL_TREE;
19594 /* If there is any qualification still in effect, clear it
19595 now; we will be starting fresh with the next declarator. */
19596 parser->scope = NULL_TREE;
19597 parser->qualifying_scope = NULL_TREE;
19598 parser->object_scope = NULL_TREE;
19599 /* If it's a `,', then there are more declarators. */
19600 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19602 cp_lexer_consume_token (parser->lexer);
19603 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19605 cp_token *token = cp_lexer_previous_token (parser->lexer);
19606 error_at (token->location,
19607 "stray %<,%> at end of member declaration");
19610 /* If the next token isn't a `;', then we have a parse error. */
19611 else if (cp_lexer_next_token_is_not (parser->lexer,
19612 CPP_SEMICOLON))
19614 /* The next token might be a ways away from where the
19615 actual semicolon is missing. Find the previous token
19616 and use that for our error position. */
19617 cp_token *token = cp_lexer_previous_token (parser->lexer);
19618 error_at (token->location,
19619 "expected %<;%> at end of member declaration");
19621 /* Assume that the user meant to provide a semicolon. If
19622 we were to cp_parser_skip_to_end_of_statement, we might
19623 skip to a semicolon inside a member function definition
19624 and issue nonsensical error messages. */
19625 assume_semicolon = true;
19628 if (decl)
19630 /* Add DECL to the list of members. */
19631 if (!friend_p)
19632 finish_member_declaration (decl);
19634 if (TREE_CODE (decl) == FUNCTION_DECL)
19635 cp_parser_save_default_args (parser, decl);
19636 else if (TREE_CODE (decl) == FIELD_DECL
19637 && !DECL_C_BIT_FIELD (decl)
19638 && DECL_INITIAL (decl))
19639 /* Add DECL to the queue of NSDMI to be parsed later. */
19640 vec_safe_push (unparsed_nsdmis, decl);
19643 if (assume_semicolon)
19644 goto out;
19648 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19649 out:
19650 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19653 /* Parse a pure-specifier.
19655 pure-specifier:
19658 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19659 Otherwise, ERROR_MARK_NODE is returned. */
19661 static tree
19662 cp_parser_pure_specifier (cp_parser* parser)
19664 cp_token *token;
19666 /* Look for the `=' token. */
19667 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19668 return error_mark_node;
19669 /* Look for the `0' token. */
19670 token = cp_lexer_peek_token (parser->lexer);
19672 if (token->type == CPP_EOF
19673 || token->type == CPP_PRAGMA_EOL)
19674 return error_mark_node;
19676 cp_lexer_consume_token (parser->lexer);
19678 /* Accept = default or = delete in c++0x mode. */
19679 if (token->keyword == RID_DEFAULT
19680 || token->keyword == RID_DELETE)
19682 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19683 return token->u.value;
19686 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19687 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19689 cp_parser_error (parser,
19690 "invalid pure specifier (only %<= 0%> is allowed)");
19691 cp_parser_skip_to_end_of_statement (parser);
19692 return error_mark_node;
19694 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19696 error_at (token->location, "templates may not be %<virtual%>");
19697 return error_mark_node;
19700 return integer_zero_node;
19703 /* Parse a constant-initializer.
19705 constant-initializer:
19706 = constant-expression
19708 Returns a representation of the constant-expression. */
19710 static tree
19711 cp_parser_constant_initializer (cp_parser* parser)
19713 /* Look for the `=' token. */
19714 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19715 return error_mark_node;
19717 /* It is invalid to write:
19719 struct S { static const int i = { 7 }; };
19722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19724 cp_parser_error (parser,
19725 "a brace-enclosed initializer is not allowed here");
19726 /* Consume the opening brace. */
19727 cp_lexer_consume_token (parser->lexer);
19728 /* Skip the initializer. */
19729 cp_parser_skip_to_closing_brace (parser);
19730 /* Look for the trailing `}'. */
19731 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19733 return error_mark_node;
19736 return cp_parser_constant_expression (parser,
19737 /*allow_non_constant=*/false,
19738 NULL);
19741 /* Derived classes [gram.class.derived] */
19743 /* Parse a base-clause.
19745 base-clause:
19746 : base-specifier-list
19748 base-specifier-list:
19749 base-specifier ... [opt]
19750 base-specifier-list , base-specifier ... [opt]
19752 Returns a TREE_LIST representing the base-classes, in the order in
19753 which they were declared. The representation of each node is as
19754 described by cp_parser_base_specifier.
19756 In the case that no bases are specified, this function will return
19757 NULL_TREE, not ERROR_MARK_NODE. */
19759 static tree
19760 cp_parser_base_clause (cp_parser* parser)
19762 tree bases = NULL_TREE;
19764 /* Look for the `:' that begins the list. */
19765 cp_parser_require (parser, CPP_COLON, RT_COLON);
19767 /* Scan the base-specifier-list. */
19768 while (true)
19770 cp_token *token;
19771 tree base;
19772 bool pack_expansion_p = false;
19774 /* Look for the base-specifier. */
19775 base = cp_parser_base_specifier (parser);
19776 /* Look for the (optional) ellipsis. */
19777 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19779 /* Consume the `...'. */
19780 cp_lexer_consume_token (parser->lexer);
19782 pack_expansion_p = true;
19785 /* Add BASE to the front of the list. */
19786 if (base && base != error_mark_node)
19788 if (pack_expansion_p)
19789 /* Make this a pack expansion type. */
19790 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19792 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19794 TREE_CHAIN (base) = bases;
19795 bases = base;
19798 /* Peek at the next token. */
19799 token = cp_lexer_peek_token (parser->lexer);
19800 /* If it's not a comma, then the list is complete. */
19801 if (token->type != CPP_COMMA)
19802 break;
19803 /* Consume the `,'. */
19804 cp_lexer_consume_token (parser->lexer);
19807 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19808 base class had a qualified name. However, the next name that
19809 appears is certainly not qualified. */
19810 parser->scope = NULL_TREE;
19811 parser->qualifying_scope = NULL_TREE;
19812 parser->object_scope = NULL_TREE;
19814 return nreverse (bases);
19817 /* Parse a base-specifier.
19819 base-specifier:
19820 :: [opt] nested-name-specifier [opt] class-name
19821 virtual access-specifier [opt] :: [opt] nested-name-specifier
19822 [opt] class-name
19823 access-specifier virtual [opt] :: [opt] nested-name-specifier
19824 [opt] class-name
19826 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19827 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19828 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19829 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19831 static tree
19832 cp_parser_base_specifier (cp_parser* parser)
19834 cp_token *token;
19835 bool done = false;
19836 bool virtual_p = false;
19837 bool duplicate_virtual_error_issued_p = false;
19838 bool duplicate_access_error_issued_p = false;
19839 bool class_scope_p, template_p;
19840 tree access = access_default_node;
19841 tree type;
19843 /* Process the optional `virtual' and `access-specifier'. */
19844 while (!done)
19846 /* Peek at the next token. */
19847 token = cp_lexer_peek_token (parser->lexer);
19848 /* Process `virtual'. */
19849 switch (token->keyword)
19851 case RID_VIRTUAL:
19852 /* If `virtual' appears more than once, issue an error. */
19853 if (virtual_p && !duplicate_virtual_error_issued_p)
19855 cp_parser_error (parser,
19856 "%<virtual%> specified more than once in base-specified");
19857 duplicate_virtual_error_issued_p = true;
19860 virtual_p = true;
19862 /* Consume the `virtual' token. */
19863 cp_lexer_consume_token (parser->lexer);
19865 break;
19867 case RID_PUBLIC:
19868 case RID_PROTECTED:
19869 case RID_PRIVATE:
19870 /* If more than one access specifier appears, issue an
19871 error. */
19872 if (access != access_default_node
19873 && !duplicate_access_error_issued_p)
19875 cp_parser_error (parser,
19876 "more than one access specifier in base-specified");
19877 duplicate_access_error_issued_p = true;
19880 access = ridpointers[(int) token->keyword];
19882 /* Consume the access-specifier. */
19883 cp_lexer_consume_token (parser->lexer);
19885 break;
19887 default:
19888 done = true;
19889 break;
19892 /* It is not uncommon to see programs mechanically, erroneously, use
19893 the 'typename' keyword to denote (dependent) qualified types
19894 as base classes. */
19895 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19897 token = cp_lexer_peek_token (parser->lexer);
19898 if (!processing_template_decl)
19899 error_at (token->location,
19900 "keyword %<typename%> not allowed outside of templates");
19901 else
19902 error_at (token->location,
19903 "keyword %<typename%> not allowed in this context "
19904 "(the base class is implicitly a type)");
19905 cp_lexer_consume_token (parser->lexer);
19908 /* Look for the optional `::' operator. */
19909 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19910 /* Look for the nested-name-specifier. The simplest way to
19911 implement:
19913 [temp.res]
19915 The keyword `typename' is not permitted in a base-specifier or
19916 mem-initializer; in these contexts a qualified name that
19917 depends on a template-parameter is implicitly assumed to be a
19918 type name.
19920 is to pretend that we have seen the `typename' keyword at this
19921 point. */
19922 cp_parser_nested_name_specifier_opt (parser,
19923 /*typename_keyword_p=*/true,
19924 /*check_dependency_p=*/true,
19925 typename_type,
19926 /*is_declaration=*/true);
19927 /* If the base class is given by a qualified name, assume that names
19928 we see are type names or templates, as appropriate. */
19929 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19930 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19932 if (!parser->scope
19933 && cp_lexer_next_token_is_decltype (parser->lexer))
19934 /* DR 950 allows decltype as a base-specifier. */
19935 type = cp_parser_decltype (parser);
19936 else
19938 /* Otherwise, look for the class-name. */
19939 type = cp_parser_class_name (parser,
19940 class_scope_p,
19941 template_p,
19942 typename_type,
19943 /*check_dependency_p=*/true,
19944 /*class_head_p=*/false,
19945 /*is_declaration=*/true);
19946 type = TREE_TYPE (type);
19949 if (type == error_mark_node)
19950 return error_mark_node;
19952 return finish_base_specifier (type, access, virtual_p);
19955 /* Exception handling [gram.exception] */
19957 /* Parse an (optional) noexcept-specification.
19959 noexcept-specification:
19960 noexcept ( constant-expression ) [opt]
19962 If no noexcept-specification is present, returns NULL_TREE.
19963 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19964 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19965 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19966 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19967 in which case a boolean condition is returned instead. */
19969 static tree
19970 cp_parser_noexcept_specification_opt (cp_parser* parser,
19971 bool require_constexpr,
19972 bool* consumed_expr,
19973 bool return_cond)
19975 cp_token *token;
19976 const char *saved_message;
19978 /* Peek at the next token. */
19979 token = cp_lexer_peek_token (parser->lexer);
19981 /* Is it a noexcept-specification? */
19982 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19984 tree expr;
19985 cp_lexer_consume_token (parser->lexer);
19987 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19989 cp_lexer_consume_token (parser->lexer);
19991 if (require_constexpr)
19993 /* Types may not be defined in an exception-specification. */
19994 saved_message = parser->type_definition_forbidden_message;
19995 parser->type_definition_forbidden_message
19996 = G_("types may not be defined in an exception-specification");
19998 expr = cp_parser_constant_expression (parser, false, NULL);
20000 /* Restore the saved message. */
20001 parser->type_definition_forbidden_message = saved_message;
20003 else
20005 expr = cp_parser_expression (parser, false, NULL);
20006 *consumed_expr = true;
20009 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20011 else
20013 expr = boolean_true_node;
20014 if (!require_constexpr)
20015 *consumed_expr = false;
20018 /* We cannot build a noexcept-spec right away because this will check
20019 that expr is a constexpr. */
20020 if (!return_cond)
20021 return build_noexcept_spec (expr, tf_warning_or_error);
20022 else
20023 return expr;
20025 else
20026 return NULL_TREE;
20029 /* Parse an (optional) exception-specification.
20031 exception-specification:
20032 throw ( type-id-list [opt] )
20034 Returns a TREE_LIST representing the exception-specification. The
20035 TREE_VALUE of each node is a type. */
20037 static tree
20038 cp_parser_exception_specification_opt (cp_parser* parser)
20040 cp_token *token;
20041 tree type_id_list;
20042 const char *saved_message;
20044 /* Peek at the next token. */
20045 token = cp_lexer_peek_token (parser->lexer);
20047 /* Is it a noexcept-specification? */
20048 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20049 false);
20050 if (type_id_list != NULL_TREE)
20051 return type_id_list;
20053 /* If it's not `throw', then there's no exception-specification. */
20054 if (!cp_parser_is_keyword (token, RID_THROW))
20055 return NULL_TREE;
20057 #if 0
20058 /* Enable this once a lot of code has transitioned to noexcept? */
20059 if (cxx_dialect >= cxx0x && !in_system_header)
20060 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20061 "deprecated in C++0x; use %<noexcept%> instead");
20062 #endif
20064 /* Consume the `throw'. */
20065 cp_lexer_consume_token (parser->lexer);
20067 /* Look for the `('. */
20068 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20070 /* Peek at the next token. */
20071 token = cp_lexer_peek_token (parser->lexer);
20072 /* If it's not a `)', then there is a type-id-list. */
20073 if (token->type != CPP_CLOSE_PAREN)
20075 /* Types may not be defined in an exception-specification. */
20076 saved_message = parser->type_definition_forbidden_message;
20077 parser->type_definition_forbidden_message
20078 = G_("types may not be defined in an exception-specification");
20079 /* Parse the type-id-list. */
20080 type_id_list = cp_parser_type_id_list (parser);
20081 /* Restore the saved message. */
20082 parser->type_definition_forbidden_message = saved_message;
20084 else
20085 type_id_list = empty_except_spec;
20087 /* Look for the `)'. */
20088 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20090 return type_id_list;
20093 /* Parse an (optional) type-id-list.
20095 type-id-list:
20096 type-id ... [opt]
20097 type-id-list , type-id ... [opt]
20099 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20100 in the order that the types were presented. */
20102 static tree
20103 cp_parser_type_id_list (cp_parser* parser)
20105 tree types = NULL_TREE;
20107 while (true)
20109 cp_token *token;
20110 tree type;
20112 /* Get the next type-id. */
20113 type = cp_parser_type_id (parser);
20114 /* Parse the optional ellipsis. */
20115 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20117 /* Consume the `...'. */
20118 cp_lexer_consume_token (parser->lexer);
20120 /* Turn the type into a pack expansion expression. */
20121 type = make_pack_expansion (type);
20123 /* Add it to the list. */
20124 types = add_exception_specifier (types, type, /*complain=*/1);
20125 /* Peek at the next token. */
20126 token = cp_lexer_peek_token (parser->lexer);
20127 /* If it is not a `,', we are done. */
20128 if (token->type != CPP_COMMA)
20129 break;
20130 /* Consume the `,'. */
20131 cp_lexer_consume_token (parser->lexer);
20134 return nreverse (types);
20137 /* Parse a try-block.
20139 try-block:
20140 try compound-statement handler-seq */
20142 static tree
20143 cp_parser_try_block (cp_parser* parser)
20145 tree try_block;
20147 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20148 try_block = begin_try_block ();
20149 cp_parser_compound_statement (parser, NULL, true, false);
20150 finish_try_block (try_block);
20151 cp_parser_handler_seq (parser);
20152 finish_handler_sequence (try_block);
20154 return try_block;
20157 /* Parse a function-try-block.
20159 function-try-block:
20160 try ctor-initializer [opt] function-body handler-seq */
20162 static bool
20163 cp_parser_function_try_block (cp_parser* parser)
20165 tree compound_stmt;
20166 tree try_block;
20167 bool ctor_initializer_p;
20169 /* Look for the `try' keyword. */
20170 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20171 return false;
20172 /* Let the rest of the front end know where we are. */
20173 try_block = begin_function_try_block (&compound_stmt);
20174 /* Parse the function-body. */
20175 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20176 (parser, /*in_function_try_block=*/true);
20177 /* We're done with the `try' part. */
20178 finish_function_try_block (try_block);
20179 /* Parse the handlers. */
20180 cp_parser_handler_seq (parser);
20181 /* We're done with the handlers. */
20182 finish_function_handler_sequence (try_block, compound_stmt);
20184 return ctor_initializer_p;
20187 /* Parse a handler-seq.
20189 handler-seq:
20190 handler handler-seq [opt] */
20192 static void
20193 cp_parser_handler_seq (cp_parser* parser)
20195 while (true)
20197 cp_token *token;
20199 /* Parse the handler. */
20200 cp_parser_handler (parser);
20201 /* Peek at the next token. */
20202 token = cp_lexer_peek_token (parser->lexer);
20203 /* If it's not `catch' then there are no more handlers. */
20204 if (!cp_parser_is_keyword (token, RID_CATCH))
20205 break;
20209 /* Parse a handler.
20211 handler:
20212 catch ( exception-declaration ) compound-statement */
20214 static void
20215 cp_parser_handler (cp_parser* parser)
20217 tree handler;
20218 tree declaration;
20220 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20221 handler = begin_handler ();
20222 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20223 declaration = cp_parser_exception_declaration (parser);
20224 finish_handler_parms (declaration, handler);
20225 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20226 cp_parser_compound_statement (parser, NULL, false, false);
20227 finish_handler (handler);
20230 /* Parse an exception-declaration.
20232 exception-declaration:
20233 type-specifier-seq declarator
20234 type-specifier-seq abstract-declarator
20235 type-specifier-seq
20238 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20239 ellipsis variant is used. */
20241 static tree
20242 cp_parser_exception_declaration (cp_parser* parser)
20244 cp_decl_specifier_seq type_specifiers;
20245 cp_declarator *declarator;
20246 const char *saved_message;
20248 /* If it's an ellipsis, it's easy to handle. */
20249 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20251 /* Consume the `...' token. */
20252 cp_lexer_consume_token (parser->lexer);
20253 return NULL_TREE;
20256 /* Types may not be defined in exception-declarations. */
20257 saved_message = parser->type_definition_forbidden_message;
20258 parser->type_definition_forbidden_message
20259 = G_("types may not be defined in exception-declarations");
20261 /* Parse the type-specifier-seq. */
20262 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20263 /*is_trailing_return=*/false,
20264 &type_specifiers);
20265 /* If it's a `)', then there is no declarator. */
20266 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20267 declarator = NULL;
20268 else
20269 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20270 /*ctor_dtor_or_conv_p=*/NULL,
20271 /*parenthesized_p=*/NULL,
20272 /*member_p=*/false);
20274 /* Restore the saved message. */
20275 parser->type_definition_forbidden_message = saved_message;
20277 if (!type_specifiers.any_specifiers_p)
20278 return error_mark_node;
20280 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20283 /* Parse a throw-expression.
20285 throw-expression:
20286 throw assignment-expression [opt]
20288 Returns a THROW_EXPR representing the throw-expression. */
20290 static tree
20291 cp_parser_throw_expression (cp_parser* parser)
20293 tree expression;
20294 cp_token* token;
20296 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20297 token = cp_lexer_peek_token (parser->lexer);
20298 /* Figure out whether or not there is an assignment-expression
20299 following the "throw" keyword. */
20300 if (token->type == CPP_COMMA
20301 || token->type == CPP_SEMICOLON
20302 || token->type == CPP_CLOSE_PAREN
20303 || token->type == CPP_CLOSE_SQUARE
20304 || token->type == CPP_CLOSE_BRACE
20305 || token->type == CPP_COLON)
20306 expression = NULL_TREE;
20307 else
20308 expression = cp_parser_assignment_expression (parser,
20309 /*cast_p=*/false, NULL);
20311 return build_throw (expression);
20314 /* GNU Extensions */
20316 /* Parse an (optional) asm-specification.
20318 asm-specification:
20319 asm ( string-literal )
20321 If the asm-specification is present, returns a STRING_CST
20322 corresponding to the string-literal. Otherwise, returns
20323 NULL_TREE. */
20325 static tree
20326 cp_parser_asm_specification_opt (cp_parser* parser)
20328 cp_token *token;
20329 tree asm_specification;
20331 /* Peek at the next token. */
20332 token = cp_lexer_peek_token (parser->lexer);
20333 /* If the next token isn't the `asm' keyword, then there's no
20334 asm-specification. */
20335 if (!cp_parser_is_keyword (token, RID_ASM))
20336 return NULL_TREE;
20338 /* Consume the `asm' token. */
20339 cp_lexer_consume_token (parser->lexer);
20340 /* Look for the `('. */
20341 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20343 /* Look for the string-literal. */
20344 asm_specification = cp_parser_string_literal (parser, false, false);
20346 /* Look for the `)'. */
20347 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20349 return asm_specification;
20352 /* Parse an asm-operand-list.
20354 asm-operand-list:
20355 asm-operand
20356 asm-operand-list , asm-operand
20358 asm-operand:
20359 string-literal ( expression )
20360 [ string-literal ] string-literal ( expression )
20362 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20363 each node is the expression. The TREE_PURPOSE is itself a
20364 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20365 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20366 is a STRING_CST for the string literal before the parenthesis. Returns
20367 ERROR_MARK_NODE if any of the operands are invalid. */
20369 static tree
20370 cp_parser_asm_operand_list (cp_parser* parser)
20372 tree asm_operands = NULL_TREE;
20373 bool invalid_operands = false;
20375 while (true)
20377 tree string_literal;
20378 tree expression;
20379 tree name;
20381 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20383 /* Consume the `[' token. */
20384 cp_lexer_consume_token (parser->lexer);
20385 /* Read the operand name. */
20386 name = cp_parser_identifier (parser);
20387 if (name != error_mark_node)
20388 name = build_string (IDENTIFIER_LENGTH (name),
20389 IDENTIFIER_POINTER (name));
20390 /* Look for the closing `]'. */
20391 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20393 else
20394 name = NULL_TREE;
20395 /* Look for the string-literal. */
20396 string_literal = cp_parser_string_literal (parser, false, false);
20398 /* Look for the `('. */
20399 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20400 /* Parse the expression. */
20401 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20402 /* Look for the `)'. */
20403 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20405 if (name == error_mark_node
20406 || string_literal == error_mark_node
20407 || expression == error_mark_node)
20408 invalid_operands = true;
20410 /* Add this operand to the list. */
20411 asm_operands = tree_cons (build_tree_list (name, string_literal),
20412 expression,
20413 asm_operands);
20414 /* If the next token is not a `,', there are no more
20415 operands. */
20416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20417 break;
20418 /* Consume the `,'. */
20419 cp_lexer_consume_token (parser->lexer);
20422 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20425 /* Parse an asm-clobber-list.
20427 asm-clobber-list:
20428 string-literal
20429 asm-clobber-list , string-literal
20431 Returns a TREE_LIST, indicating the clobbers in the order that they
20432 appeared. The TREE_VALUE of each node is a STRING_CST. */
20434 static tree
20435 cp_parser_asm_clobber_list (cp_parser* parser)
20437 tree clobbers = NULL_TREE;
20439 while (true)
20441 tree string_literal;
20443 /* Look for the string literal. */
20444 string_literal = cp_parser_string_literal (parser, false, false);
20445 /* Add it to the list. */
20446 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20447 /* If the next token is not a `,', then the list is
20448 complete. */
20449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20450 break;
20451 /* Consume the `,' token. */
20452 cp_lexer_consume_token (parser->lexer);
20455 return clobbers;
20458 /* Parse an asm-label-list.
20460 asm-label-list:
20461 identifier
20462 asm-label-list , identifier
20464 Returns a TREE_LIST, indicating the labels in the order that they
20465 appeared. The TREE_VALUE of each node is a label. */
20467 static tree
20468 cp_parser_asm_label_list (cp_parser* parser)
20470 tree labels = NULL_TREE;
20472 while (true)
20474 tree identifier, label, name;
20476 /* Look for the identifier. */
20477 identifier = cp_parser_identifier (parser);
20478 if (!error_operand_p (identifier))
20480 label = lookup_label (identifier);
20481 if (TREE_CODE (label) == LABEL_DECL)
20483 TREE_USED (label) = 1;
20484 check_goto (label);
20485 name = build_string (IDENTIFIER_LENGTH (identifier),
20486 IDENTIFIER_POINTER (identifier));
20487 labels = tree_cons (name, label, labels);
20490 /* If the next token is not a `,', then the list is
20491 complete. */
20492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20493 break;
20494 /* Consume the `,' token. */
20495 cp_lexer_consume_token (parser->lexer);
20498 return nreverse (labels);
20501 /* Return TRUE iff the next tokens in the stream are possibly the
20502 beginning of a GNU extension attribute. */
20504 static bool
20505 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20507 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20510 /* Return TRUE iff the next tokens in the stream are possibly the
20511 beginning of a standard C++-11 attribute specifier. */
20513 static bool
20514 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20516 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20519 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20520 beginning of a standard C++-11 attribute specifier. */
20522 static bool
20523 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20525 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20527 return (cxx_dialect >= cxx0x
20528 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20529 || (token->type == CPP_OPEN_SQUARE
20530 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20531 && token->type == CPP_OPEN_SQUARE)));
20534 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20535 beginning of a GNU extension attribute. */
20537 static bool
20538 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20540 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20542 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20545 /* Return true iff the next tokens can be the beginning of either a
20546 GNU attribute list, or a standard C++11 attribute sequence. */
20548 static bool
20549 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20551 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20552 || cp_next_tokens_can_be_std_attribute_p (parser));
20555 /* Return true iff the next Nth tokens can be the beginning of either
20556 a GNU attribute list, or a standard C++11 attribute sequence. */
20558 static bool
20559 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20561 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20562 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20565 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20566 of GNU attributes, or return NULL. */
20568 static tree
20569 cp_parser_attributes_opt (cp_parser *parser)
20571 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20572 return cp_parser_gnu_attributes_opt (parser);
20573 return cp_parser_std_attribute_spec_seq (parser);
20576 /* Parse an (optional) series of attributes.
20578 attributes:
20579 attributes attribute
20581 attribute:
20582 __attribute__ (( attribute-list [opt] ))
20584 The return value is as for cp_parser_gnu_attribute_list. */
20586 static tree
20587 cp_parser_gnu_attributes_opt (cp_parser* parser)
20589 tree attributes = NULL_TREE;
20591 while (true)
20593 cp_token *token;
20594 tree attribute_list;
20595 bool ok = true;
20597 /* Peek at the next token. */
20598 token = cp_lexer_peek_token (parser->lexer);
20599 /* If it's not `__attribute__', then we're done. */
20600 if (token->keyword != RID_ATTRIBUTE)
20601 break;
20603 /* Consume the `__attribute__' keyword. */
20604 cp_lexer_consume_token (parser->lexer);
20605 /* Look for the two `(' tokens. */
20606 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20607 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20609 /* Peek at the next token. */
20610 token = cp_lexer_peek_token (parser->lexer);
20611 if (token->type != CPP_CLOSE_PAREN)
20612 /* Parse the attribute-list. */
20613 attribute_list = cp_parser_gnu_attribute_list (parser);
20614 else
20615 /* If the next token is a `)', then there is no attribute
20616 list. */
20617 attribute_list = NULL;
20619 /* Look for the two `)' tokens. */
20620 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20621 ok = false;
20622 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20623 ok = false;
20624 if (!ok)
20625 cp_parser_skip_to_end_of_statement (parser);
20627 /* Add these new attributes to the list. */
20628 attributes = chainon (attributes, attribute_list);
20631 return attributes;
20634 /* Parse a GNU attribute-list.
20636 attribute-list:
20637 attribute
20638 attribute-list , attribute
20640 attribute:
20641 identifier
20642 identifier ( identifier )
20643 identifier ( identifier , expression-list )
20644 identifier ( expression-list )
20646 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20647 to an attribute. The TREE_PURPOSE of each node is the identifier
20648 indicating which attribute is in use. The TREE_VALUE represents
20649 the arguments, if any. */
20651 static tree
20652 cp_parser_gnu_attribute_list (cp_parser* parser)
20654 tree attribute_list = NULL_TREE;
20655 bool save_translate_strings_p = parser->translate_strings_p;
20657 parser->translate_strings_p = false;
20658 while (true)
20660 cp_token *token;
20661 tree identifier;
20662 tree attribute;
20664 /* Look for the identifier. We also allow keywords here; for
20665 example `__attribute__ ((const))' is legal. */
20666 token = cp_lexer_peek_token (parser->lexer);
20667 if (token->type == CPP_NAME
20668 || token->type == CPP_KEYWORD)
20670 tree arguments = NULL_TREE;
20672 /* Consume the token. */
20673 token = cp_lexer_consume_token (parser->lexer);
20675 /* Save away the identifier that indicates which attribute
20676 this is. */
20677 identifier = (token->type == CPP_KEYWORD)
20678 /* For keywords, use the canonical spelling, not the
20679 parsed identifier. */
20680 ? ridpointers[(int) token->keyword]
20681 : token->u.value;
20683 attribute = build_tree_list (identifier, NULL_TREE);
20685 /* Peek at the next token. */
20686 token = cp_lexer_peek_token (parser->lexer);
20687 /* If it's an `(', then parse the attribute arguments. */
20688 if (token->type == CPP_OPEN_PAREN)
20690 vec<tree, va_gc> *vec;
20691 int attr_flag = (attribute_takes_identifier_p (identifier)
20692 ? id_attr : normal_attr);
20693 vec = cp_parser_parenthesized_expression_list
20694 (parser, attr_flag, /*cast_p=*/false,
20695 /*allow_expansion_p=*/false,
20696 /*non_constant_p=*/NULL);
20697 if (vec == NULL)
20698 arguments = error_mark_node;
20699 else
20701 arguments = build_tree_list_vec (vec);
20702 release_tree_vector (vec);
20704 /* Save the arguments away. */
20705 TREE_VALUE (attribute) = arguments;
20708 if (arguments != error_mark_node)
20710 /* Add this attribute to the list. */
20711 TREE_CHAIN (attribute) = attribute_list;
20712 attribute_list = attribute;
20715 token = cp_lexer_peek_token (parser->lexer);
20717 /* Now, look for more attributes. If the next token isn't a
20718 `,', we're done. */
20719 if (token->type != CPP_COMMA)
20720 break;
20722 /* Consume the comma and keep going. */
20723 cp_lexer_consume_token (parser->lexer);
20725 parser->translate_strings_p = save_translate_strings_p;
20727 /* We built up the list in reverse order. */
20728 return nreverse (attribute_list);
20731 /* Parse a standard C++11 attribute.
20733 The returned representation is a TREE_LIST which TREE_PURPOSE is
20734 the scoped name of the attribute, and the TREE_VALUE is its
20735 arguments list.
20737 Note that the scoped name of the attribute is itself a TREE_LIST
20738 which TREE_PURPOSE is the namespace of the attribute, and
20739 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20740 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20741 and which TREE_PURPOSE is directly the attribute name.
20743 Clients of the attribute code should use get_attribute_namespace
20744 and get_attribute_name to get the actual namespace and name of
20745 attributes, regardless of their being GNU or C++11 attributes.
20747 attribute:
20748 attribute-token attribute-argument-clause [opt]
20750 attribute-token:
20751 identifier
20752 attribute-scoped-token
20754 attribute-scoped-token:
20755 attribute-namespace :: identifier
20757 attribute-namespace:
20758 identifier
20760 attribute-argument-clause:
20761 ( balanced-token-seq )
20763 balanced-token-seq:
20764 balanced-token [opt]
20765 balanced-token-seq balanced-token
20767 balanced-token:
20768 ( balanced-token-seq )
20769 [ balanced-token-seq ]
20770 { balanced-token-seq }. */
20772 static tree
20773 cp_parser_std_attribute (cp_parser *parser)
20775 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20776 cp_token *token;
20778 /* First, parse name of the the attribute, a.k.a
20779 attribute-token. */
20781 token = cp_lexer_peek_token (parser->lexer);
20782 if (token->type == CPP_NAME)
20783 attr_id = token->u.value;
20784 else if (token->type == CPP_KEYWORD)
20785 attr_id = ridpointers[(int) token->keyword];
20786 else if (token->flags & NAMED_OP)
20787 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20789 if (attr_id == NULL_TREE)
20790 return NULL_TREE;
20792 cp_lexer_consume_token (parser->lexer);
20794 token = cp_lexer_peek_token (parser->lexer);
20795 if (token->type == CPP_SCOPE)
20797 /* We are seeing a scoped attribute token. */
20799 cp_lexer_consume_token (parser->lexer);
20800 attr_ns = attr_id;
20802 token = cp_lexer_consume_token (parser->lexer);
20803 if (token->type == CPP_NAME)
20804 attr_id = token->u.value;
20805 else if (token->type == CPP_KEYWORD)
20806 attr_id = ridpointers[(int) token->keyword];
20807 else
20809 error_at (token->location,
20810 "expected an identifier for the attribute name");
20811 return error_mark_node;
20813 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20814 NULL_TREE);
20815 token = cp_lexer_peek_token (parser->lexer);
20817 else
20819 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20820 NULL_TREE);
20821 /* C++11 noreturn attribute is equivalent to GNU's. */
20822 if (is_attribute_p ("noreturn", attr_id))
20823 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
20826 /* Now parse the optional argument clause of the attribute. */
20828 if (token->type != CPP_OPEN_PAREN)
20829 return attribute;
20832 vec<tree, va_gc> *vec;
20833 int attr_flag = normal_attr;
20835 if (attr_ns == get_identifier ("gnu")
20836 && attribute_takes_identifier_p (attr_id))
20837 /* A GNU attribute that takes an identifier in parameter. */
20838 attr_flag = id_attr;
20840 vec = cp_parser_parenthesized_expression_list
20841 (parser, attr_flag, /*cast_p=*/false,
20842 /*allow_expansion_p=*/true,
20843 /*non_constant_p=*/NULL);
20844 if (vec == NULL)
20845 arguments = error_mark_node;
20846 else
20848 arguments = build_tree_list_vec (vec);
20849 release_tree_vector (vec);
20852 if (arguments == error_mark_node)
20853 attribute = error_mark_node;
20854 else
20855 TREE_VALUE (attribute) = arguments;
20858 return attribute;
20861 /* Parse a list of standard C++-11 attributes.
20863 attribute-list:
20864 attribute [opt]
20865 attribute-list , attribute[opt]
20866 attribute ...
20867 attribute-list , attribute ...
20870 static tree
20871 cp_parser_std_attribute_list (cp_parser *parser)
20873 tree attributes = NULL_TREE, attribute = NULL_TREE;
20874 cp_token *token = NULL;
20876 while (true)
20878 attribute = cp_parser_std_attribute (parser);
20879 if (attribute == error_mark_node)
20880 break;
20881 if (attribute != NULL_TREE)
20883 TREE_CHAIN (attribute) = attributes;
20884 attributes = attribute;
20886 token = cp_lexer_peek_token (parser->lexer);
20887 if (token->type != CPP_COMMA)
20888 break;
20889 cp_lexer_consume_token (parser->lexer);
20891 attributes = nreverse (attributes);
20892 return attributes;
20895 /* Parse a standard C++-11 attribute specifier.
20897 attribute-specifier:
20898 [ [ attribute-list ] ]
20899 alignment-specifier
20901 alignment-specifier:
20902 alignas ( type-id ... [opt] )
20903 alignas ( alignment-expression ... [opt] ). */
20905 static tree
20906 cp_parser_std_attribute_spec (cp_parser *parser)
20908 tree attributes = NULL_TREE;
20909 cp_token *token = cp_lexer_peek_token (parser->lexer);
20911 if (token->type == CPP_OPEN_SQUARE
20912 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
20914 cp_lexer_consume_token (parser->lexer);
20915 cp_lexer_consume_token (parser->lexer);
20917 attributes = cp_parser_std_attribute_list (parser);
20919 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
20920 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20921 cp_parser_skip_to_end_of_statement (parser);
20922 else
20923 /* Warn about parsing c++11 attribute in non-c++1 mode, only
20924 when we are sure that we have actually parsed them. */
20925 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20927 else
20929 tree alignas_expr;
20931 /* Look for an alignment-specifier. */
20933 token = cp_lexer_peek_token (parser->lexer);
20935 if (token->type != CPP_KEYWORD
20936 || token->keyword != RID_ALIGNAS)
20937 return NULL_TREE;
20939 cp_lexer_consume_token (parser->lexer);
20940 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20942 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
20944 cp_parser_error (parser, "expected %<(%>");
20945 return error_mark_node;
20948 cp_parser_parse_tentatively (parser);
20949 alignas_expr = cp_parser_type_id (parser);
20951 if (!cp_parser_parse_definitely (parser))
20953 gcc_assert (alignas_expr == error_mark_node
20954 || alignas_expr == NULL_TREE);
20956 alignas_expr =
20957 cp_parser_assignment_expression (parser, /*cast_p=*/false,
20958 /**cp_id_kind=*/NULL);
20959 if (alignas_expr == NULL_TREE
20960 || alignas_expr == error_mark_node)
20961 return alignas_expr;
20964 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
20966 cp_parser_error (parser, "expected %<)%>");
20967 return error_mark_node;
20970 alignas_expr = cxx_alignas_expr (alignas_expr);
20972 /* Build the C++-11 representation of an 'aligned'
20973 attribute. */
20974 attributes =
20975 build_tree_list (build_tree_list (get_identifier ("gnu"),
20976 get_identifier ("aligned")),
20977 build_tree_list (NULL_TREE, alignas_expr));
20980 return attributes;
20983 /* Parse a standard C++-11 attribute-specifier-seq.
20985 attribute-specifier-seq:
20986 attribute-specifier-seq [opt] attribute-specifier
20989 static tree
20990 cp_parser_std_attribute_spec_seq (cp_parser *parser)
20992 tree attr_specs = NULL;
20994 while (true)
20996 tree attr_spec = cp_parser_std_attribute_spec (parser);
20997 if (attr_spec == NULL_TREE)
20998 break;
20999 if (attr_spec == error_mark_node)
21000 return error_mark_node;
21002 TREE_CHAIN (attr_spec) = attr_specs;
21003 attr_specs = attr_spec;
21006 attr_specs = nreverse (attr_specs);
21007 return attr_specs;
21010 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21011 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21012 current value of the PEDANTIC flag, regardless of whether or not
21013 the `__extension__' keyword is present. The caller is responsible
21014 for restoring the value of the PEDANTIC flag. */
21016 static bool
21017 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21019 /* Save the old value of the PEDANTIC flag. */
21020 *saved_pedantic = pedantic;
21022 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21024 /* Consume the `__extension__' token. */
21025 cp_lexer_consume_token (parser->lexer);
21026 /* We're not being pedantic while the `__extension__' keyword is
21027 in effect. */
21028 pedantic = 0;
21030 return true;
21033 return false;
21036 /* Parse a label declaration.
21038 label-declaration:
21039 __label__ label-declarator-seq ;
21041 label-declarator-seq:
21042 identifier , label-declarator-seq
21043 identifier */
21045 static void
21046 cp_parser_label_declaration (cp_parser* parser)
21048 /* Look for the `__label__' keyword. */
21049 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21051 while (true)
21053 tree identifier;
21055 /* Look for an identifier. */
21056 identifier = cp_parser_identifier (parser);
21057 /* If we failed, stop. */
21058 if (identifier == error_mark_node)
21059 break;
21060 /* Declare it as a label. */
21061 finish_label_decl (identifier);
21062 /* If the next token is a `;', stop. */
21063 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21064 break;
21065 /* Look for the `,' separating the label declarations. */
21066 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21069 /* Look for the final `;'. */
21070 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21073 /* Support Functions */
21075 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21076 NAME should have one of the representations used for an
21077 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21078 is returned. If PARSER->SCOPE is a dependent type, then a
21079 SCOPE_REF is returned.
21081 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21082 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21083 was formed. Abstractly, such entities should not be passed to this
21084 function, because they do not need to be looked up, but it is
21085 simpler to check for this special case here, rather than at the
21086 call-sites.
21088 In cases not explicitly covered above, this function returns a
21089 DECL, OVERLOAD, or baselink representing the result of the lookup.
21090 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21091 is returned.
21093 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21094 (e.g., "struct") that was used. In that case bindings that do not
21095 refer to types are ignored.
21097 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21098 ignored.
21100 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21101 are ignored.
21103 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21104 types.
21106 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21107 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21108 NULL_TREE otherwise. */
21110 static tree
21111 cp_parser_lookup_name (cp_parser *parser, tree name,
21112 enum tag_types tag_type,
21113 bool is_template,
21114 bool is_namespace,
21115 bool check_dependency,
21116 tree *ambiguous_decls,
21117 location_t name_location)
21119 tree decl;
21120 tree object_type = parser->context->object_type;
21122 /* Assume that the lookup will be unambiguous. */
21123 if (ambiguous_decls)
21124 *ambiguous_decls = NULL_TREE;
21126 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21127 no longer valid. Note that if we are parsing tentatively, and
21128 the parse fails, OBJECT_TYPE will be automatically restored. */
21129 parser->context->object_type = NULL_TREE;
21131 if (name == error_mark_node)
21132 return error_mark_node;
21134 /* A template-id has already been resolved; there is no lookup to
21135 do. */
21136 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21137 return name;
21138 if (BASELINK_P (name))
21140 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21141 == TEMPLATE_ID_EXPR);
21142 return name;
21145 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21146 it should already have been checked to make sure that the name
21147 used matches the type being destroyed. */
21148 if (TREE_CODE (name) == BIT_NOT_EXPR)
21150 tree type;
21152 /* Figure out to which type this destructor applies. */
21153 if (parser->scope)
21154 type = parser->scope;
21155 else if (object_type)
21156 type = object_type;
21157 else
21158 type = current_class_type;
21159 /* If that's not a class type, there is no destructor. */
21160 if (!type || !CLASS_TYPE_P (type))
21161 return error_mark_node;
21162 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21163 lazily_declare_fn (sfk_destructor, type);
21164 if (!CLASSTYPE_DESTRUCTORS (type))
21165 return error_mark_node;
21166 /* If it was a class type, return the destructor. */
21167 return CLASSTYPE_DESTRUCTORS (type);
21170 /* By this point, the NAME should be an ordinary identifier. If
21171 the id-expression was a qualified name, the qualifying scope is
21172 stored in PARSER->SCOPE at this point. */
21173 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21175 /* Perform the lookup. */
21176 if (parser->scope)
21178 bool dependent_p;
21180 if (parser->scope == error_mark_node)
21181 return error_mark_node;
21183 /* If the SCOPE is dependent, the lookup must be deferred until
21184 the template is instantiated -- unless we are explicitly
21185 looking up names in uninstantiated templates. Even then, we
21186 cannot look up the name if the scope is not a class type; it
21187 might, for example, be a template type parameter. */
21188 dependent_p = (TYPE_P (parser->scope)
21189 && dependent_scope_p (parser->scope));
21190 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21191 && dependent_p)
21192 /* Defer lookup. */
21193 decl = error_mark_node;
21194 else
21196 tree pushed_scope = NULL_TREE;
21198 /* If PARSER->SCOPE is a dependent type, then it must be a
21199 class type, and we must not be checking dependencies;
21200 otherwise, we would have processed this lookup above. So
21201 that PARSER->SCOPE is not considered a dependent base by
21202 lookup_member, we must enter the scope here. */
21203 if (dependent_p)
21204 pushed_scope = push_scope (parser->scope);
21206 /* If the PARSER->SCOPE is a template specialization, it
21207 may be instantiated during name lookup. In that case,
21208 errors may be issued. Even if we rollback the current
21209 tentative parse, those errors are valid. */
21210 decl = lookup_qualified_name (parser->scope, name,
21211 tag_type != none_type,
21212 /*complain=*/true);
21214 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21215 lookup result and the nested-name-specifier nominates a class C:
21216 * if the name specified after the nested-name-specifier, when
21217 looked up in C, is the injected-class-name of C (Clause 9), or
21218 * if the name specified after the nested-name-specifier is the
21219 same as the identifier or the simple-template-id's template-
21220 name in the last component of the nested-name-specifier,
21221 the name is instead considered to name the constructor of
21222 class C. [ Note: for example, the constructor is not an
21223 acceptable lookup result in an elaborated-type-specifier so
21224 the constructor would not be used in place of the
21225 injected-class-name. --end note ] Such a constructor name
21226 shall be used only in the declarator-id of a declaration that
21227 names a constructor or in a using-declaration. */
21228 if (tag_type == none_type
21229 && DECL_SELF_REFERENCE_P (decl)
21230 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21231 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21232 tag_type != none_type,
21233 /*complain=*/true);
21235 /* If we have a single function from a using decl, pull it out. */
21236 if (TREE_CODE (decl) == OVERLOAD
21237 && !really_overloaded_fn (decl))
21238 decl = OVL_FUNCTION (decl);
21240 if (pushed_scope)
21241 pop_scope (pushed_scope);
21244 /* If the scope is a dependent type and either we deferred lookup or
21245 we did lookup but didn't find the name, rememeber the name. */
21246 if (decl == error_mark_node && TYPE_P (parser->scope)
21247 && dependent_type_p (parser->scope))
21249 if (tag_type)
21251 tree type;
21253 /* The resolution to Core Issue 180 says that `struct
21254 A::B' should be considered a type-name, even if `A'
21255 is dependent. */
21256 type = make_typename_type (parser->scope, name, tag_type,
21257 /*complain=*/tf_error);
21258 decl = TYPE_NAME (type);
21260 else if (is_template
21261 && (cp_parser_next_token_ends_template_argument_p (parser)
21262 || cp_lexer_next_token_is (parser->lexer,
21263 CPP_CLOSE_PAREN)))
21264 decl = make_unbound_class_template (parser->scope,
21265 name, NULL_TREE,
21266 /*complain=*/tf_error);
21267 else
21268 decl = build_qualified_name (/*type=*/NULL_TREE,
21269 parser->scope, name,
21270 is_template);
21272 parser->qualifying_scope = parser->scope;
21273 parser->object_scope = NULL_TREE;
21275 else if (object_type)
21277 tree object_decl = NULL_TREE;
21278 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21279 OBJECT_TYPE is not a class. */
21280 if (CLASS_TYPE_P (object_type))
21281 /* If the OBJECT_TYPE is a template specialization, it may
21282 be instantiated during name lookup. In that case, errors
21283 may be issued. Even if we rollback the current tentative
21284 parse, those errors are valid. */
21285 object_decl = lookup_member (object_type,
21286 name,
21287 /*protect=*/0,
21288 tag_type != none_type,
21289 tf_warning_or_error);
21290 /* Look it up in the enclosing context, too. */
21291 decl = lookup_name_real (name, tag_type != none_type,
21292 /*nonclass=*/0,
21293 /*block_p=*/true, is_namespace, 0);
21294 parser->object_scope = object_type;
21295 parser->qualifying_scope = NULL_TREE;
21296 if (object_decl)
21297 decl = object_decl;
21299 else
21301 decl = lookup_name_real (name, tag_type != none_type,
21302 /*nonclass=*/0,
21303 /*block_p=*/true, is_namespace, 0);
21304 parser->qualifying_scope = NULL_TREE;
21305 parser->object_scope = NULL_TREE;
21308 /* If the lookup failed, let our caller know. */
21309 if (!decl || decl == error_mark_node)
21310 return error_mark_node;
21312 /* Pull out the template from an injected-class-name (or multiple). */
21313 if (is_template)
21314 decl = maybe_get_template_decl_from_type_decl (decl);
21316 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21317 if (TREE_CODE (decl) == TREE_LIST)
21319 if (ambiguous_decls)
21320 *ambiguous_decls = decl;
21321 /* The error message we have to print is too complicated for
21322 cp_parser_error, so we incorporate its actions directly. */
21323 if (!cp_parser_simulate_error (parser))
21325 error_at (name_location, "reference to %qD is ambiguous",
21326 name);
21327 print_candidates (decl);
21329 return error_mark_node;
21332 gcc_assert (DECL_P (decl)
21333 || TREE_CODE (decl) == OVERLOAD
21334 || TREE_CODE (decl) == SCOPE_REF
21335 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21336 || BASELINK_P (decl));
21338 /* If we have resolved the name of a member declaration, check to
21339 see if the declaration is accessible. When the name resolves to
21340 set of overloaded functions, accessibility is checked when
21341 overload resolution is done.
21343 During an explicit instantiation, access is not checked at all,
21344 as per [temp.explicit]. */
21345 if (DECL_P (decl))
21346 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21348 maybe_record_typedef_use (decl);
21350 return decl;
21353 /* Like cp_parser_lookup_name, but for use in the typical case where
21354 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21355 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21357 static tree
21358 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21360 return cp_parser_lookup_name (parser, name,
21361 none_type,
21362 /*is_template=*/false,
21363 /*is_namespace=*/false,
21364 /*check_dependency=*/true,
21365 /*ambiguous_decls=*/NULL,
21366 location);
21369 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21370 the current context, return the TYPE_DECL. If TAG_NAME_P is
21371 true, the DECL indicates the class being defined in a class-head,
21372 or declared in an elaborated-type-specifier.
21374 Otherwise, return DECL. */
21376 static tree
21377 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21379 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21380 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21382 struct A {
21383 template <typename T> struct B;
21386 template <typename T> struct A::B {};
21388 Similarly, in an elaborated-type-specifier:
21390 namespace N { struct X{}; }
21392 struct A {
21393 template <typename T> friend struct N::X;
21396 However, if the DECL refers to a class type, and we are in
21397 the scope of the class, then the name lookup automatically
21398 finds the TYPE_DECL created by build_self_reference rather
21399 than a TEMPLATE_DECL. For example, in:
21401 template <class T> struct S {
21402 S s;
21405 there is no need to handle such case. */
21407 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21408 return DECL_TEMPLATE_RESULT (decl);
21410 return decl;
21413 /* If too many, or too few, template-parameter lists apply to the
21414 declarator, issue an error message. Returns TRUE if all went well,
21415 and FALSE otherwise. */
21417 static bool
21418 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21419 cp_declarator *declarator,
21420 location_t declarator_location)
21422 switch (declarator->kind)
21424 case cdk_id:
21426 unsigned num_templates = 0;
21427 tree scope = declarator->u.id.qualifying_scope;
21429 if (scope)
21430 num_templates = num_template_headers_for_class (scope);
21431 else if (TREE_CODE (declarator->u.id.unqualified_name)
21432 == TEMPLATE_ID_EXPR)
21433 /* If the DECLARATOR has the form `X<y>' then it uses one
21434 additional level of template parameters. */
21435 ++num_templates;
21437 return cp_parser_check_template_parameters
21438 (parser, num_templates, declarator_location, declarator);
21441 case cdk_function:
21442 case cdk_array:
21443 case cdk_pointer:
21444 case cdk_reference:
21445 case cdk_ptrmem:
21446 return (cp_parser_check_declarator_template_parameters
21447 (parser, declarator->declarator, declarator_location));
21449 case cdk_error:
21450 return true;
21452 default:
21453 gcc_unreachable ();
21455 return false;
21458 /* NUM_TEMPLATES were used in the current declaration. If that is
21459 invalid, return FALSE and issue an error messages. Otherwise,
21460 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21461 declarator and we can print more accurate diagnostics. */
21463 static bool
21464 cp_parser_check_template_parameters (cp_parser* parser,
21465 unsigned num_templates,
21466 location_t location,
21467 cp_declarator *declarator)
21469 /* If there are the same number of template classes and parameter
21470 lists, that's OK. */
21471 if (parser->num_template_parameter_lists == num_templates)
21472 return true;
21473 /* If there are more, but only one more, then we are referring to a
21474 member template. That's OK too. */
21475 if (parser->num_template_parameter_lists == num_templates + 1)
21476 return true;
21477 /* If there are more template classes than parameter lists, we have
21478 something like:
21480 template <class T> void S<T>::R<T>::f (); */
21481 if (parser->num_template_parameter_lists < num_templates)
21483 if (declarator && !current_function_decl)
21484 error_at (location, "specializing member %<%T::%E%> "
21485 "requires %<template<>%> syntax",
21486 declarator->u.id.qualifying_scope,
21487 declarator->u.id.unqualified_name);
21488 else if (declarator)
21489 error_at (location, "invalid declaration of %<%T::%E%>",
21490 declarator->u.id.qualifying_scope,
21491 declarator->u.id.unqualified_name);
21492 else
21493 error_at (location, "too few template-parameter-lists");
21494 return false;
21496 /* Otherwise, there are too many template parameter lists. We have
21497 something like:
21499 template <class T> template <class U> void S::f(); */
21500 error_at (location, "too many template-parameter-lists");
21501 return false;
21504 /* Parse an optional `::' token indicating that the following name is
21505 from the global namespace. If so, PARSER->SCOPE is set to the
21506 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21507 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21508 Returns the new value of PARSER->SCOPE, if the `::' token is
21509 present, and NULL_TREE otherwise. */
21511 static tree
21512 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21514 cp_token *token;
21516 /* Peek at the next token. */
21517 token = cp_lexer_peek_token (parser->lexer);
21518 /* If we're looking at a `::' token then we're starting from the
21519 global namespace, not our current location. */
21520 if (token->type == CPP_SCOPE)
21522 /* Consume the `::' token. */
21523 cp_lexer_consume_token (parser->lexer);
21524 /* Set the SCOPE so that we know where to start the lookup. */
21525 parser->scope = global_namespace;
21526 parser->qualifying_scope = global_namespace;
21527 parser->object_scope = NULL_TREE;
21529 return parser->scope;
21531 else if (!current_scope_valid_p)
21533 parser->scope = NULL_TREE;
21534 parser->qualifying_scope = NULL_TREE;
21535 parser->object_scope = NULL_TREE;
21538 return NULL_TREE;
21541 /* Returns TRUE if the upcoming token sequence is the start of a
21542 constructor declarator. If FRIEND_P is true, the declarator is
21543 preceded by the `friend' specifier. */
21545 static bool
21546 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21548 bool constructor_p;
21549 tree nested_name_specifier;
21550 cp_token *next_token;
21552 /* The common case is that this is not a constructor declarator, so
21553 try to avoid doing lots of work if at all possible. It's not
21554 valid declare a constructor at function scope. */
21555 if (parser->in_function_body)
21556 return false;
21557 /* And only certain tokens can begin a constructor declarator. */
21558 next_token = cp_lexer_peek_token (parser->lexer);
21559 if (next_token->type != CPP_NAME
21560 && next_token->type != CPP_SCOPE
21561 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21562 && next_token->type != CPP_TEMPLATE_ID)
21563 return false;
21565 /* Parse tentatively; we are going to roll back all of the tokens
21566 consumed here. */
21567 cp_parser_parse_tentatively (parser);
21568 /* Assume that we are looking at a constructor declarator. */
21569 constructor_p = true;
21571 /* Look for the optional `::' operator. */
21572 cp_parser_global_scope_opt (parser,
21573 /*current_scope_valid_p=*/false);
21574 /* Look for the nested-name-specifier. */
21575 nested_name_specifier
21576 = (cp_parser_nested_name_specifier_opt (parser,
21577 /*typename_keyword_p=*/false,
21578 /*check_dependency_p=*/false,
21579 /*type_p=*/false,
21580 /*is_declaration=*/false));
21581 /* Outside of a class-specifier, there must be a
21582 nested-name-specifier. */
21583 if (!nested_name_specifier &&
21584 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21585 || friend_p))
21586 constructor_p = false;
21587 else if (nested_name_specifier == error_mark_node)
21588 constructor_p = false;
21590 /* If we have a class scope, this is easy; DR 147 says that S::S always
21591 names the constructor, and no other qualified name could. */
21592 if (constructor_p && nested_name_specifier
21593 && CLASS_TYPE_P (nested_name_specifier))
21595 tree id = cp_parser_unqualified_id (parser,
21596 /*template_keyword_p=*/false,
21597 /*check_dependency_p=*/false,
21598 /*declarator_p=*/true,
21599 /*optional_p=*/false);
21600 if (is_overloaded_fn (id))
21601 id = DECL_NAME (get_first_fn (id));
21602 if (!constructor_name_p (id, nested_name_specifier))
21603 constructor_p = false;
21605 /* If we still think that this might be a constructor-declarator,
21606 look for a class-name. */
21607 else if (constructor_p)
21609 /* If we have:
21611 template <typename T> struct S {
21612 S();
21615 we must recognize that the nested `S' names a class. */
21616 tree type_decl;
21617 type_decl = cp_parser_class_name (parser,
21618 /*typename_keyword_p=*/false,
21619 /*template_keyword_p=*/false,
21620 none_type,
21621 /*check_dependency_p=*/false,
21622 /*class_head_p=*/false,
21623 /*is_declaration=*/false);
21624 /* If there was no class-name, then this is not a constructor. */
21625 constructor_p = !cp_parser_error_occurred (parser);
21627 /* If we're still considering a constructor, we have to see a `(',
21628 to begin the parameter-declaration-clause, followed by either a
21629 `)', an `...', or a decl-specifier. We need to check for a
21630 type-specifier to avoid being fooled into thinking that:
21632 S (f) (int);
21634 is a constructor. (It is actually a function named `f' that
21635 takes one parameter (of type `int') and returns a value of type
21636 `S'. */
21637 if (constructor_p
21638 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21639 constructor_p = false;
21641 if (constructor_p
21642 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21643 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21644 /* A parameter declaration begins with a decl-specifier,
21645 which is either the "attribute" keyword, a storage class
21646 specifier, or (usually) a type-specifier. */
21647 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21649 tree type;
21650 tree pushed_scope = NULL_TREE;
21651 unsigned saved_num_template_parameter_lists;
21653 /* Names appearing in the type-specifier should be looked up
21654 in the scope of the class. */
21655 if (current_class_type)
21656 type = NULL_TREE;
21657 else
21659 type = TREE_TYPE (type_decl);
21660 if (TREE_CODE (type) == TYPENAME_TYPE)
21662 type = resolve_typename_type (type,
21663 /*only_current_p=*/false);
21664 if (TREE_CODE (type) == TYPENAME_TYPE)
21666 cp_parser_abort_tentative_parse (parser);
21667 return false;
21670 pushed_scope = push_scope (type);
21673 /* Inside the constructor parameter list, surrounding
21674 template-parameter-lists do not apply. */
21675 saved_num_template_parameter_lists
21676 = parser->num_template_parameter_lists;
21677 parser->num_template_parameter_lists = 0;
21679 /* Look for the type-specifier. */
21680 cp_parser_type_specifier (parser,
21681 CP_PARSER_FLAGS_NONE,
21682 /*decl_specs=*/NULL,
21683 /*is_declarator=*/true,
21684 /*declares_class_or_enum=*/NULL,
21685 /*is_cv_qualifier=*/NULL);
21687 parser->num_template_parameter_lists
21688 = saved_num_template_parameter_lists;
21690 /* Leave the scope of the class. */
21691 if (pushed_scope)
21692 pop_scope (pushed_scope);
21694 constructor_p = !cp_parser_error_occurred (parser);
21698 /* We did not really want to consume any tokens. */
21699 cp_parser_abort_tentative_parse (parser);
21701 return constructor_p;
21704 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21705 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21706 they must be performed once we are in the scope of the function.
21708 Returns the function defined. */
21710 static tree
21711 cp_parser_function_definition_from_specifiers_and_declarator
21712 (cp_parser* parser,
21713 cp_decl_specifier_seq *decl_specifiers,
21714 tree attributes,
21715 const cp_declarator *declarator)
21717 tree fn;
21718 bool success_p;
21720 /* Begin the function-definition. */
21721 success_p = start_function (decl_specifiers, declarator, attributes);
21723 /* The things we're about to see are not directly qualified by any
21724 template headers we've seen thus far. */
21725 reset_specialization ();
21727 /* If there were names looked up in the decl-specifier-seq that we
21728 did not check, check them now. We must wait until we are in the
21729 scope of the function to perform the checks, since the function
21730 might be a friend. */
21731 perform_deferred_access_checks (tf_warning_or_error);
21733 if (!success_p)
21735 /* Skip the entire function. */
21736 cp_parser_skip_to_end_of_block_or_statement (parser);
21737 fn = error_mark_node;
21739 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21741 /* Seen already, skip it. An error message has already been output. */
21742 cp_parser_skip_to_end_of_block_or_statement (parser);
21743 fn = current_function_decl;
21744 current_function_decl = NULL_TREE;
21745 /* If this is a function from a class, pop the nested class. */
21746 if (current_class_name)
21747 pop_nested_class ();
21749 else
21751 timevar_id_t tv;
21752 if (DECL_DECLARED_INLINE_P (current_function_decl))
21753 tv = TV_PARSE_INLINE;
21754 else
21755 tv = TV_PARSE_FUNC;
21756 timevar_push (tv);
21757 fn = cp_parser_function_definition_after_declarator (parser,
21758 /*inline_p=*/false);
21759 timevar_pop (tv);
21762 return fn;
21765 /* Parse the part of a function-definition that follows the
21766 declarator. INLINE_P is TRUE iff this function is an inline
21767 function defined within a class-specifier.
21769 Returns the function defined. */
21771 static tree
21772 cp_parser_function_definition_after_declarator (cp_parser* parser,
21773 bool inline_p)
21775 tree fn;
21776 bool ctor_initializer_p = false;
21777 bool saved_in_unbraced_linkage_specification_p;
21778 bool saved_in_function_body;
21779 unsigned saved_num_template_parameter_lists;
21780 cp_token *token;
21782 saved_in_function_body = parser->in_function_body;
21783 parser->in_function_body = true;
21784 /* If the next token is `return', then the code may be trying to
21785 make use of the "named return value" extension that G++ used to
21786 support. */
21787 token = cp_lexer_peek_token (parser->lexer);
21788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21790 /* Consume the `return' keyword. */
21791 cp_lexer_consume_token (parser->lexer);
21792 /* Look for the identifier that indicates what value is to be
21793 returned. */
21794 cp_parser_identifier (parser);
21795 /* Issue an error message. */
21796 error_at (token->location,
21797 "named return values are no longer supported");
21798 /* Skip tokens until we reach the start of the function body. */
21799 while (true)
21801 cp_token *token = cp_lexer_peek_token (parser->lexer);
21802 if (token->type == CPP_OPEN_BRACE
21803 || token->type == CPP_EOF
21804 || token->type == CPP_PRAGMA_EOL)
21805 break;
21806 cp_lexer_consume_token (parser->lexer);
21809 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21810 anything declared inside `f'. */
21811 saved_in_unbraced_linkage_specification_p
21812 = parser->in_unbraced_linkage_specification_p;
21813 parser->in_unbraced_linkage_specification_p = false;
21814 /* Inside the function, surrounding template-parameter-lists do not
21815 apply. */
21816 saved_num_template_parameter_lists
21817 = parser->num_template_parameter_lists;
21818 parser->num_template_parameter_lists = 0;
21820 start_lambda_scope (current_function_decl);
21822 /* If the next token is `try', `__transaction_atomic', or
21823 `__transaction_relaxed`, then we are looking at either function-try-block
21824 or function-transaction-block. Note that all of these include the
21825 function-body. */
21826 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21827 ctor_initializer_p = cp_parser_function_transaction (parser,
21828 RID_TRANSACTION_ATOMIC);
21829 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21830 RID_TRANSACTION_RELAXED))
21831 ctor_initializer_p = cp_parser_function_transaction (parser,
21832 RID_TRANSACTION_RELAXED);
21833 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21834 ctor_initializer_p = cp_parser_function_try_block (parser);
21835 else
21836 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21837 (parser, /*in_function_try_block=*/false);
21839 finish_lambda_scope ();
21841 /* Finish the function. */
21842 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21843 (inline_p ? 2 : 0));
21844 /* Generate code for it, if necessary. */
21845 expand_or_defer_fn (fn);
21846 /* Restore the saved values. */
21847 parser->in_unbraced_linkage_specification_p
21848 = saved_in_unbraced_linkage_specification_p;
21849 parser->num_template_parameter_lists
21850 = saved_num_template_parameter_lists;
21851 parser->in_function_body = saved_in_function_body;
21853 return fn;
21856 /* Parse a template-declaration, assuming that the `export' (and
21857 `extern') keywords, if present, has already been scanned. MEMBER_P
21858 is as for cp_parser_template_declaration. */
21860 static void
21861 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21863 tree decl = NULL_TREE;
21864 vec<deferred_access_check, va_gc> *checks;
21865 tree parameter_list;
21866 bool friend_p = false;
21867 bool need_lang_pop;
21868 cp_token *token;
21870 /* Look for the `template' keyword. */
21871 token = cp_lexer_peek_token (parser->lexer);
21872 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21873 return;
21875 /* And the `<'. */
21876 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21877 return;
21878 if (at_class_scope_p () && current_function_decl)
21880 /* 14.5.2.2 [temp.mem]
21882 A local class shall not have member templates. */
21883 error_at (token->location,
21884 "invalid declaration of member template in local class");
21885 cp_parser_skip_to_end_of_block_or_statement (parser);
21886 return;
21888 /* [temp]
21890 A template ... shall not have C linkage. */
21891 if (current_lang_name == lang_name_c)
21893 error_at (token->location, "template with C linkage");
21894 /* Give it C++ linkage to avoid confusing other parts of the
21895 front end. */
21896 push_lang_context (lang_name_cplusplus);
21897 need_lang_pop = true;
21899 else
21900 need_lang_pop = false;
21902 /* We cannot perform access checks on the template parameter
21903 declarations until we know what is being declared, just as we
21904 cannot check the decl-specifier list. */
21905 push_deferring_access_checks (dk_deferred);
21907 /* If the next token is `>', then we have an invalid
21908 specialization. Rather than complain about an invalid template
21909 parameter, issue an error message here. */
21910 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21912 cp_parser_error (parser, "invalid explicit specialization");
21913 begin_specialization ();
21914 parameter_list = NULL_TREE;
21916 else
21918 /* Parse the template parameters. */
21919 parameter_list = cp_parser_template_parameter_list (parser);
21922 /* Get the deferred access checks from the parameter list. These
21923 will be checked once we know what is being declared, as for a
21924 member template the checks must be performed in the scope of the
21925 class containing the member. */
21926 checks = get_deferred_access_checks ();
21928 /* Look for the `>'. */
21929 cp_parser_skip_to_end_of_template_parameter_list (parser);
21930 /* We just processed one more parameter list. */
21931 ++parser->num_template_parameter_lists;
21932 /* If the next token is `template', there are more template
21933 parameters. */
21934 if (cp_lexer_next_token_is_keyword (parser->lexer,
21935 RID_TEMPLATE))
21936 cp_parser_template_declaration_after_export (parser, member_p);
21937 else if (cxx_dialect >= cxx0x
21938 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21939 decl = cp_parser_alias_declaration (parser);
21940 else
21942 /* There are no access checks when parsing a template, as we do not
21943 know if a specialization will be a friend. */
21944 push_deferring_access_checks (dk_no_check);
21945 token = cp_lexer_peek_token (parser->lexer);
21946 decl = cp_parser_single_declaration (parser,
21947 checks,
21948 member_p,
21949 /*explicit_specialization_p=*/false,
21950 &friend_p);
21951 pop_deferring_access_checks ();
21953 /* If this is a member template declaration, let the front
21954 end know. */
21955 if (member_p && !friend_p && decl)
21957 if (TREE_CODE (decl) == TYPE_DECL)
21958 cp_parser_check_access_in_redeclaration (decl, token->location);
21960 decl = finish_member_template_decl (decl);
21962 else if (friend_p && decl
21963 && (TREE_CODE (decl) == TYPE_DECL
21964 || DECL_TYPE_TEMPLATE_P (decl)))
21965 make_friend_class (current_class_type, TREE_TYPE (decl),
21966 /*complain=*/true);
21968 /* We are done with the current parameter list. */
21969 --parser->num_template_parameter_lists;
21971 pop_deferring_access_checks ();
21973 /* Finish up. */
21974 finish_template_decl (parameter_list);
21976 /* Check the template arguments for a literal operator template. */
21977 if (decl
21978 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21979 && UDLIT_OPER_P (DECL_NAME (decl)))
21981 bool ok = true;
21982 if (parameter_list == NULL_TREE)
21983 ok = false;
21984 else
21986 int num_parms = TREE_VEC_LENGTH (parameter_list);
21987 if (num_parms != 1)
21988 ok = false;
21989 else
21991 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21992 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21993 if (TREE_TYPE (parm) != char_type_node
21994 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21995 ok = false;
21998 if (!ok)
21999 error ("literal operator template %qD has invalid parameter list."
22000 " Expected non-type template argument pack <char...>",
22001 decl);
22003 /* Register member declarations. */
22004 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22005 finish_member_declaration (decl);
22006 /* For the erroneous case of a template with C linkage, we pushed an
22007 implicit C++ linkage scope; exit that scope now. */
22008 if (need_lang_pop)
22009 pop_lang_context ();
22010 /* If DECL is a function template, we must return to parse it later.
22011 (Even though there is no definition, there might be default
22012 arguments that need handling.) */
22013 if (member_p && decl
22014 && (TREE_CODE (decl) == FUNCTION_DECL
22015 || DECL_FUNCTION_TEMPLATE_P (decl)))
22016 vec_safe_push (unparsed_funs_with_definitions, decl);
22019 /* Perform the deferred access checks from a template-parameter-list.
22020 CHECKS is a TREE_LIST of access checks, as returned by
22021 get_deferred_access_checks. */
22023 static void
22024 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22026 ++processing_template_parmlist;
22027 perform_access_checks (checks, tf_warning_or_error);
22028 --processing_template_parmlist;
22031 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22032 `function-definition' sequence that follows a template header.
22033 If MEMBER_P is true, this declaration appears in a class scope.
22035 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22036 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22038 static tree
22039 cp_parser_single_declaration (cp_parser* parser,
22040 vec<deferred_access_check, va_gc> *checks,
22041 bool member_p,
22042 bool explicit_specialization_p,
22043 bool* friend_p)
22045 int declares_class_or_enum;
22046 tree decl = NULL_TREE;
22047 cp_decl_specifier_seq decl_specifiers;
22048 bool function_definition_p = false;
22049 cp_token *decl_spec_token_start;
22051 /* This function is only used when processing a template
22052 declaration. */
22053 gcc_assert (innermost_scope_kind () == sk_template_parms
22054 || innermost_scope_kind () == sk_template_spec);
22056 /* Defer access checks until we know what is being declared. */
22057 push_deferring_access_checks (dk_deferred);
22059 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22060 alternative. */
22061 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22062 cp_parser_decl_specifier_seq (parser,
22063 CP_PARSER_FLAGS_OPTIONAL,
22064 &decl_specifiers,
22065 &declares_class_or_enum);
22066 if (friend_p)
22067 *friend_p = cp_parser_friend_p (&decl_specifiers);
22069 /* There are no template typedefs. */
22070 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22072 error_at (decl_spec_token_start->location,
22073 "template declaration of %<typedef%>");
22074 decl = error_mark_node;
22077 /* Gather up the access checks that occurred the
22078 decl-specifier-seq. */
22079 stop_deferring_access_checks ();
22081 /* Check for the declaration of a template class. */
22082 if (declares_class_or_enum)
22084 if (cp_parser_declares_only_class_p (parser))
22086 decl = shadow_tag (&decl_specifiers);
22088 /* In this case:
22090 struct C {
22091 friend template <typename T> struct A<T>::B;
22094 A<T>::B will be represented by a TYPENAME_TYPE, and
22095 therefore not recognized by shadow_tag. */
22096 if (friend_p && *friend_p
22097 && !decl
22098 && decl_specifiers.type
22099 && TYPE_P (decl_specifiers.type))
22100 decl = decl_specifiers.type;
22102 if (decl && decl != error_mark_node)
22103 decl = TYPE_NAME (decl);
22104 else
22105 decl = error_mark_node;
22107 /* Perform access checks for template parameters. */
22108 cp_parser_perform_template_parameter_access_checks (checks);
22112 /* Complain about missing 'typename' or other invalid type names. */
22113 if (!decl_specifiers.any_type_specifiers_p
22114 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22116 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22117 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22118 the rest of this declaration. */
22119 decl = error_mark_node;
22120 goto out;
22123 /* If it's not a template class, try for a template function. If
22124 the next token is a `;', then this declaration does not declare
22125 anything. But, if there were errors in the decl-specifiers, then
22126 the error might well have come from an attempted class-specifier.
22127 In that case, there's no need to warn about a missing declarator. */
22128 if (!decl
22129 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22130 || decl_specifiers.type != error_mark_node))
22132 decl = cp_parser_init_declarator (parser,
22133 &decl_specifiers,
22134 checks,
22135 /*function_definition_allowed_p=*/true,
22136 member_p,
22137 declares_class_or_enum,
22138 &function_definition_p,
22139 NULL);
22141 /* 7.1.1-1 [dcl.stc]
22143 A storage-class-specifier shall not be specified in an explicit
22144 specialization... */
22145 if (decl
22146 && explicit_specialization_p
22147 && decl_specifiers.storage_class != sc_none)
22149 error_at (decl_spec_token_start->location,
22150 "explicit template specialization cannot have a storage class");
22151 decl = error_mark_node;
22154 if (decl && TREE_CODE (decl) == VAR_DECL)
22155 check_template_variable (decl);
22158 /* Look for a trailing `;' after the declaration. */
22159 if (!function_definition_p
22160 && (decl == error_mark_node
22161 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22162 cp_parser_skip_to_end_of_block_or_statement (parser);
22164 out:
22165 pop_deferring_access_checks ();
22167 /* Clear any current qualification; whatever comes next is the start
22168 of something new. */
22169 parser->scope = NULL_TREE;
22170 parser->qualifying_scope = NULL_TREE;
22171 parser->object_scope = NULL_TREE;
22173 return decl;
22176 /* Parse a cast-expression that is not the operand of a unary "&". */
22178 static tree
22179 cp_parser_simple_cast_expression (cp_parser *parser)
22181 return cp_parser_cast_expression (parser, /*address_p=*/false,
22182 /*cast_p=*/false, /*decltype*/false, NULL);
22185 /* Parse a functional cast to TYPE. Returns an expression
22186 representing the cast. */
22188 static tree
22189 cp_parser_functional_cast (cp_parser* parser, tree type)
22191 vec<tree, va_gc> *vec;
22192 tree expression_list;
22193 tree cast;
22194 bool nonconst_p;
22196 if (!type)
22197 type = error_mark_node;
22199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22201 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22202 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22203 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22204 if (TREE_CODE (type) == TYPE_DECL)
22205 type = TREE_TYPE (type);
22206 return finish_compound_literal (type, expression_list,
22207 tf_warning_or_error);
22211 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22212 /*cast_p=*/true,
22213 /*allow_expansion_p=*/true,
22214 /*non_constant_p=*/NULL);
22215 if (vec == NULL)
22216 expression_list = error_mark_node;
22217 else
22219 expression_list = build_tree_list_vec (vec);
22220 release_tree_vector (vec);
22223 cast = build_functional_cast (type, expression_list,
22224 tf_warning_or_error);
22225 /* [expr.const]/1: In an integral constant expression "only type
22226 conversions to integral or enumeration type can be used". */
22227 if (TREE_CODE (type) == TYPE_DECL)
22228 type = TREE_TYPE (type);
22229 if (cast != error_mark_node
22230 && !cast_valid_in_integral_constant_expression_p (type)
22231 && cp_parser_non_integral_constant_expression (parser,
22232 NIC_CONSTRUCTOR))
22233 return error_mark_node;
22234 return cast;
22237 /* Save the tokens that make up the body of a member function defined
22238 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22239 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22240 specifiers applied to the declaration. Returns the FUNCTION_DECL
22241 for the member function. */
22243 static tree
22244 cp_parser_save_member_function_body (cp_parser* parser,
22245 cp_decl_specifier_seq *decl_specifiers,
22246 cp_declarator *declarator,
22247 tree attributes)
22249 cp_token *first;
22250 cp_token *last;
22251 tree fn;
22253 /* Create the FUNCTION_DECL. */
22254 fn = grokmethod (decl_specifiers, declarator, attributes);
22255 /* If something went badly wrong, bail out now. */
22256 if (fn == error_mark_node)
22258 /* If there's a function-body, skip it. */
22259 if (cp_parser_token_starts_function_definition_p
22260 (cp_lexer_peek_token (parser->lexer)))
22261 cp_parser_skip_to_end_of_block_or_statement (parser);
22262 return error_mark_node;
22265 /* Remember it, if there default args to post process. */
22266 cp_parser_save_default_args (parser, fn);
22268 /* Save away the tokens that make up the body of the
22269 function. */
22270 first = parser->lexer->next_token;
22271 /* We can have braced-init-list mem-initializers before the fn body. */
22272 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22274 cp_lexer_consume_token (parser->lexer);
22275 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22276 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22278 /* cache_group will stop after an un-nested { } pair, too. */
22279 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22280 break;
22282 /* variadic mem-inits have ... after the ')'. */
22283 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22284 cp_lexer_consume_token (parser->lexer);
22287 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22288 /* Handle function try blocks. */
22289 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22290 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22291 last = parser->lexer->next_token;
22293 /* Save away the inline definition; we will process it when the
22294 class is complete. */
22295 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22296 DECL_PENDING_INLINE_P (fn) = 1;
22298 /* We need to know that this was defined in the class, so that
22299 friend templates are handled correctly. */
22300 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22302 /* Add FN to the queue of functions to be parsed later. */
22303 vec_safe_push (unparsed_funs_with_definitions, fn);
22305 return fn;
22308 /* Save the tokens that make up the in-class initializer for a non-static
22309 data member. Returns a DEFAULT_ARG. */
22311 static tree
22312 cp_parser_save_nsdmi (cp_parser* parser)
22314 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22317 /* Parse a template-argument-list, as well as the trailing ">" (but
22318 not the opening "<"). See cp_parser_template_argument_list for the
22319 return value. */
22321 static tree
22322 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22324 tree arguments;
22325 tree saved_scope;
22326 tree saved_qualifying_scope;
22327 tree saved_object_scope;
22328 bool saved_greater_than_is_operator_p;
22329 int saved_unevaluated_operand;
22330 int saved_inhibit_evaluation_warnings;
22332 /* [temp.names]
22334 When parsing a template-id, the first non-nested `>' is taken as
22335 the end of the template-argument-list rather than a greater-than
22336 operator. */
22337 saved_greater_than_is_operator_p
22338 = parser->greater_than_is_operator_p;
22339 parser->greater_than_is_operator_p = false;
22340 /* Parsing the argument list may modify SCOPE, so we save it
22341 here. */
22342 saved_scope = parser->scope;
22343 saved_qualifying_scope = parser->qualifying_scope;
22344 saved_object_scope = parser->object_scope;
22345 /* We need to evaluate the template arguments, even though this
22346 template-id may be nested within a "sizeof". */
22347 saved_unevaluated_operand = cp_unevaluated_operand;
22348 cp_unevaluated_operand = 0;
22349 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22350 c_inhibit_evaluation_warnings = 0;
22351 /* Parse the template-argument-list itself. */
22352 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22353 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22354 arguments = NULL_TREE;
22355 else
22356 arguments = cp_parser_template_argument_list (parser);
22357 /* Look for the `>' that ends the template-argument-list. If we find
22358 a '>>' instead, it's probably just a typo. */
22359 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22361 if (cxx_dialect != cxx98)
22363 /* In C++0x, a `>>' in a template argument list or cast
22364 expression is considered to be two separate `>'
22365 tokens. So, change the current token to a `>', but don't
22366 consume it: it will be consumed later when the outer
22367 template argument list (or cast expression) is parsed.
22368 Note that this replacement of `>' for `>>' is necessary
22369 even if we are parsing tentatively: in the tentative
22370 case, after calling
22371 cp_parser_enclosed_template_argument_list we will always
22372 throw away all of the template arguments and the first
22373 closing `>', either because the template argument list
22374 was erroneous or because we are replacing those tokens
22375 with a CPP_TEMPLATE_ID token. The second `>' (which will
22376 not have been thrown away) is needed either to close an
22377 outer template argument list or to complete a new-style
22378 cast. */
22379 cp_token *token = cp_lexer_peek_token (parser->lexer);
22380 token->type = CPP_GREATER;
22382 else if (!saved_greater_than_is_operator_p)
22384 /* If we're in a nested template argument list, the '>>' has
22385 to be a typo for '> >'. We emit the error message, but we
22386 continue parsing and we push a '>' as next token, so that
22387 the argument list will be parsed correctly. Note that the
22388 global source location is still on the token before the
22389 '>>', so we need to say explicitly where we want it. */
22390 cp_token *token = cp_lexer_peek_token (parser->lexer);
22391 error_at (token->location, "%<>>%> should be %<> >%> "
22392 "within a nested template argument list");
22394 token->type = CPP_GREATER;
22396 else
22398 /* If this is not a nested template argument list, the '>>'
22399 is a typo for '>'. Emit an error message and continue.
22400 Same deal about the token location, but here we can get it
22401 right by consuming the '>>' before issuing the diagnostic. */
22402 cp_token *token = cp_lexer_consume_token (parser->lexer);
22403 error_at (token->location,
22404 "spurious %<>>%>, use %<>%> to terminate "
22405 "a template argument list");
22408 else
22409 cp_parser_skip_to_end_of_template_parameter_list (parser);
22410 /* The `>' token might be a greater-than operator again now. */
22411 parser->greater_than_is_operator_p
22412 = saved_greater_than_is_operator_p;
22413 /* Restore the SAVED_SCOPE. */
22414 parser->scope = saved_scope;
22415 parser->qualifying_scope = saved_qualifying_scope;
22416 parser->object_scope = saved_object_scope;
22417 cp_unevaluated_operand = saved_unevaluated_operand;
22418 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22420 return arguments;
22423 /* MEMBER_FUNCTION is a member function, or a friend. If default
22424 arguments, or the body of the function have not yet been parsed,
22425 parse them now. */
22427 static void
22428 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22430 timevar_push (TV_PARSE_INMETH);
22431 /* If this member is a template, get the underlying
22432 FUNCTION_DECL. */
22433 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22434 member_function = DECL_TEMPLATE_RESULT (member_function);
22436 /* There should not be any class definitions in progress at this
22437 point; the bodies of members are only parsed outside of all class
22438 definitions. */
22439 gcc_assert (parser->num_classes_being_defined == 0);
22440 /* While we're parsing the member functions we might encounter more
22441 classes. We want to handle them right away, but we don't want
22442 them getting mixed up with functions that are currently in the
22443 queue. */
22444 push_unparsed_function_queues (parser);
22446 /* Make sure that any template parameters are in scope. */
22447 maybe_begin_member_template_processing (member_function);
22449 /* If the body of the function has not yet been parsed, parse it
22450 now. */
22451 if (DECL_PENDING_INLINE_P (member_function))
22453 tree function_scope;
22454 cp_token_cache *tokens;
22456 /* The function is no longer pending; we are processing it. */
22457 tokens = DECL_PENDING_INLINE_INFO (member_function);
22458 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22459 DECL_PENDING_INLINE_P (member_function) = 0;
22461 /* If this is a local class, enter the scope of the containing
22462 function. */
22463 function_scope = current_function_decl;
22464 if (function_scope)
22465 push_function_context ();
22467 /* Push the body of the function onto the lexer stack. */
22468 cp_parser_push_lexer_for_tokens (parser, tokens);
22470 /* Let the front end know that we going to be defining this
22471 function. */
22472 start_preparsed_function (member_function, NULL_TREE,
22473 SF_PRE_PARSED | SF_INCLASS_INLINE);
22475 /* Don't do access checking if it is a templated function. */
22476 if (processing_template_decl)
22477 push_deferring_access_checks (dk_no_check);
22479 /* Now, parse the body of the function. */
22480 cp_parser_function_definition_after_declarator (parser,
22481 /*inline_p=*/true);
22483 if (processing_template_decl)
22484 pop_deferring_access_checks ();
22486 /* Leave the scope of the containing function. */
22487 if (function_scope)
22488 pop_function_context ();
22489 cp_parser_pop_lexer (parser);
22492 /* Remove any template parameters from the symbol table. */
22493 maybe_end_member_template_processing ();
22495 /* Restore the queue. */
22496 pop_unparsed_function_queues (parser);
22497 timevar_pop (TV_PARSE_INMETH);
22500 /* If DECL contains any default args, remember it on the unparsed
22501 functions queue. */
22503 static void
22504 cp_parser_save_default_args (cp_parser* parser, tree decl)
22506 tree probe;
22508 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22509 probe;
22510 probe = TREE_CHAIN (probe))
22511 if (TREE_PURPOSE (probe))
22513 cp_default_arg_entry entry = {current_class_type, decl};
22514 vec_safe_push (unparsed_funs_with_default_args, entry);
22515 break;
22519 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22520 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22521 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22522 from the parameter-type-list. */
22524 static tree
22525 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22526 tree default_arg, tree parmtype)
22528 cp_token_cache *tokens;
22529 tree parsed_arg;
22530 bool dummy;
22532 if (default_arg == error_mark_node)
22533 return error_mark_node;
22535 /* Push the saved tokens for the default argument onto the parser's
22536 lexer stack. */
22537 tokens = DEFARG_TOKENS (default_arg);
22538 cp_parser_push_lexer_for_tokens (parser, tokens);
22540 start_lambda_scope (decl);
22542 /* Parse the default argument. */
22543 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22544 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22547 finish_lambda_scope ();
22549 if (parsed_arg == error_mark_node)
22550 cp_parser_skip_to_end_of_statement (parser);
22552 if (!processing_template_decl)
22554 /* In a non-template class, check conversions now. In a template,
22555 we'll wait and instantiate these as needed. */
22556 if (TREE_CODE (decl) == PARM_DECL)
22557 parsed_arg = check_default_argument (parmtype, parsed_arg,
22558 tf_warning_or_error);
22559 else
22561 int flags = LOOKUP_IMPLICIT;
22562 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22563 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22564 flags = LOOKUP_NORMAL;
22565 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22566 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
22567 /* This represents the whole initialization. */
22568 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
22572 /* If the token stream has not been completely used up, then
22573 there was extra junk after the end of the default
22574 argument. */
22575 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22577 if (TREE_CODE (decl) == PARM_DECL)
22578 cp_parser_error (parser, "expected %<,%>");
22579 else
22580 cp_parser_error (parser, "expected %<;%>");
22583 /* Revert to the main lexer. */
22584 cp_parser_pop_lexer (parser);
22586 return parsed_arg;
22589 /* FIELD is a non-static data member with an initializer which we saved for
22590 later; parse it now. */
22592 static void
22593 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22595 tree def;
22597 push_unparsed_function_queues (parser);
22598 def = cp_parser_late_parse_one_default_arg (parser, field,
22599 DECL_INITIAL (field),
22600 NULL_TREE);
22601 pop_unparsed_function_queues (parser);
22603 DECL_INITIAL (field) = def;
22606 /* FN is a FUNCTION_DECL which may contains a parameter with an
22607 unparsed DEFAULT_ARG. Parse the default args now. This function
22608 assumes that the current scope is the scope in which the default
22609 argument should be processed. */
22611 static void
22612 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22614 bool saved_local_variables_forbidden_p;
22615 tree parm, parmdecl;
22617 /* While we're parsing the default args, we might (due to the
22618 statement expression extension) encounter more classes. We want
22619 to handle them right away, but we don't want them getting mixed
22620 up with default args that are currently in the queue. */
22621 push_unparsed_function_queues (parser);
22623 /* Local variable names (and the `this' keyword) may not appear
22624 in a default argument. */
22625 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22626 parser->local_variables_forbidden_p = true;
22628 push_defarg_context (fn);
22630 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22631 parmdecl = DECL_ARGUMENTS (fn);
22632 parm && parm != void_list_node;
22633 parm = TREE_CHAIN (parm),
22634 parmdecl = DECL_CHAIN (parmdecl))
22636 tree default_arg = TREE_PURPOSE (parm);
22637 tree parsed_arg;
22638 vec<tree, va_gc> *insts;
22639 tree copy;
22640 unsigned ix;
22642 if (!default_arg)
22643 continue;
22645 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22646 /* This can happen for a friend declaration for a function
22647 already declared with default arguments. */
22648 continue;
22650 parsed_arg
22651 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22652 default_arg,
22653 TREE_VALUE (parm));
22654 if (parsed_arg == error_mark_node)
22656 continue;
22659 TREE_PURPOSE (parm) = parsed_arg;
22661 /* Update any instantiations we've already created. */
22662 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22663 vec_safe_iterate (insts, ix, &copy); ix++)
22664 TREE_PURPOSE (copy) = parsed_arg;
22667 pop_defarg_context ();
22669 /* Make sure no default arg is missing. */
22670 check_default_args (fn);
22672 /* Restore the state of local_variables_forbidden_p. */
22673 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22675 /* Restore the queue. */
22676 pop_unparsed_function_queues (parser);
22679 /* Parse the operand of `sizeof' (or a similar operator). Returns
22680 either a TYPE or an expression, depending on the form of the
22681 input. The KEYWORD indicates which kind of expression we have
22682 encountered. */
22684 static tree
22685 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22687 tree expr = NULL_TREE;
22688 const char *saved_message;
22689 char *tmp;
22690 bool saved_integral_constant_expression_p;
22691 bool saved_non_integral_constant_expression_p;
22692 bool pack_expansion_p = false;
22694 /* Types cannot be defined in a `sizeof' expression. Save away the
22695 old message. */
22696 saved_message = parser->type_definition_forbidden_message;
22697 /* And create the new one. */
22698 tmp = concat ("types may not be defined in %<",
22699 IDENTIFIER_POINTER (ridpointers[keyword]),
22700 "%> expressions", NULL);
22701 parser->type_definition_forbidden_message = tmp;
22703 /* The restrictions on constant-expressions do not apply inside
22704 sizeof expressions. */
22705 saved_integral_constant_expression_p
22706 = parser->integral_constant_expression_p;
22707 saved_non_integral_constant_expression_p
22708 = parser->non_integral_constant_expression_p;
22709 parser->integral_constant_expression_p = false;
22711 /* If it's a `...', then we are computing the length of a parameter
22712 pack. */
22713 if (keyword == RID_SIZEOF
22714 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22716 /* Consume the `...'. */
22717 cp_lexer_consume_token (parser->lexer);
22718 maybe_warn_variadic_templates ();
22720 /* Note that this is an expansion. */
22721 pack_expansion_p = true;
22724 /* Do not actually evaluate the expression. */
22725 ++cp_unevaluated_operand;
22726 ++c_inhibit_evaluation_warnings;
22727 /* If it's a `(', then we might be looking at the type-id
22728 construction. */
22729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22731 tree type;
22732 bool saved_in_type_id_in_expr_p;
22734 /* We can't be sure yet whether we're looking at a type-id or an
22735 expression. */
22736 cp_parser_parse_tentatively (parser);
22737 /* Consume the `('. */
22738 cp_lexer_consume_token (parser->lexer);
22739 /* Parse the type-id. */
22740 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22741 parser->in_type_id_in_expr_p = true;
22742 type = cp_parser_type_id (parser);
22743 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22744 /* Now, look for the trailing `)'. */
22745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22746 /* If all went well, then we're done. */
22747 if (cp_parser_parse_definitely (parser))
22749 cp_decl_specifier_seq decl_specs;
22751 /* Build a trivial decl-specifier-seq. */
22752 clear_decl_specs (&decl_specs);
22753 decl_specs.type = type;
22755 /* Call grokdeclarator to figure out what type this is. */
22756 expr = grokdeclarator (NULL,
22757 &decl_specs,
22758 TYPENAME,
22759 /*initialized=*/0,
22760 /*attrlist=*/NULL);
22763 else if (pack_expansion_p)
22764 permerror (cp_lexer_peek_token (parser->lexer)->location,
22765 "%<sizeof...%> argument must be surrounded by parentheses");
22767 /* If the type-id production did not work out, then we must be
22768 looking at the unary-expression production. */
22769 if (!expr)
22770 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22771 /*cast_p=*/false, NULL);
22773 if (pack_expansion_p)
22774 /* Build a pack expansion. */
22775 expr = make_pack_expansion (expr);
22777 /* Go back to evaluating expressions. */
22778 --cp_unevaluated_operand;
22779 --c_inhibit_evaluation_warnings;
22781 /* Free the message we created. */
22782 free (tmp);
22783 /* And restore the old one. */
22784 parser->type_definition_forbidden_message = saved_message;
22785 parser->integral_constant_expression_p
22786 = saved_integral_constant_expression_p;
22787 parser->non_integral_constant_expression_p
22788 = saved_non_integral_constant_expression_p;
22790 return expr;
22793 /* If the current declaration has no declarator, return true. */
22795 static bool
22796 cp_parser_declares_only_class_p (cp_parser *parser)
22798 /* If the next token is a `;' or a `,' then there is no
22799 declarator. */
22800 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22801 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22804 /* Update the DECL_SPECS to reflect the storage class indicated by
22805 KEYWORD. */
22807 static void
22808 cp_parser_set_storage_class (cp_parser *parser,
22809 cp_decl_specifier_seq *decl_specs,
22810 enum rid keyword,
22811 cp_token *token)
22813 cp_storage_class storage_class;
22815 if (parser->in_unbraced_linkage_specification_p)
22817 error_at (token->location, "invalid use of %qD in linkage specification",
22818 ridpointers[keyword]);
22819 return;
22821 else if (decl_specs->storage_class != sc_none)
22823 decl_specs->conflicting_specifiers_p = true;
22824 return;
22827 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22828 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22829 && decl_specs->gnu_thread_keyword_p)
22831 pedwarn (decl_specs->locations[ds_thread], 0,
22832 "%<__thread%> before %qD", ridpointers[keyword]);
22835 switch (keyword)
22837 case RID_AUTO:
22838 storage_class = sc_auto;
22839 break;
22840 case RID_REGISTER:
22841 storage_class = sc_register;
22842 break;
22843 case RID_STATIC:
22844 storage_class = sc_static;
22845 break;
22846 case RID_EXTERN:
22847 storage_class = sc_extern;
22848 break;
22849 case RID_MUTABLE:
22850 storage_class = sc_mutable;
22851 break;
22852 default:
22853 gcc_unreachable ();
22855 decl_specs->storage_class = storage_class;
22856 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22858 /* A storage class specifier cannot be applied alongside a typedef
22859 specifier. If there is a typedef specifier present then set
22860 conflicting_specifiers_p which will trigger an error later
22861 on in grokdeclarator. */
22862 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22863 decl_specs->conflicting_specifiers_p = true;
22866 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22867 is true, the type is a class or enum definition. */
22869 static void
22870 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22871 tree type_spec,
22872 cp_token *token,
22873 bool type_definition_p)
22875 decl_specs->any_specifiers_p = true;
22877 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22878 (with, for example, in "typedef int wchar_t;") we remember that
22879 this is what happened. In system headers, we ignore these
22880 declarations so that G++ can work with system headers that are not
22881 C++-safe. */
22882 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22883 && !type_definition_p
22884 && (type_spec == boolean_type_node
22885 || type_spec == char16_type_node
22886 || type_spec == char32_type_node
22887 || type_spec == wchar_type_node)
22888 && (decl_specs->type
22889 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22890 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22891 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22892 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22894 decl_specs->redefined_builtin_type = type_spec;
22895 set_and_check_decl_spec_loc (decl_specs,
22896 ds_redefined_builtin_type_spec,
22897 token);
22898 if (!decl_specs->type)
22900 decl_specs->type = type_spec;
22901 decl_specs->type_definition_p = false;
22902 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
22905 else if (decl_specs->type)
22906 decl_specs->multiple_types_p = true;
22907 else
22909 decl_specs->type = type_spec;
22910 decl_specs->type_definition_p = type_definition_p;
22911 decl_specs->redefined_builtin_type = NULL_TREE;
22912 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
22916 /* True iff TOKEN is the GNU keyword __thread. */
22918 static bool
22919 token_is__thread (cp_token *token)
22921 gcc_assert (token->keyword == RID_THREAD);
22922 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
22925 /* Set the location for a declarator specifier and check if it is
22926 duplicated.
22928 DECL_SPECS is the sequence of declarator specifiers onto which to
22929 set the location.
22931 DS is the single declarator specifier to set which location is to
22932 be set onto the existing sequence of declarators.
22934 LOCATION is the location for the declarator specifier to
22935 consider. */
22937 static void
22938 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22939 cp_decl_spec ds, cp_token *token)
22941 gcc_assert (ds < ds_last);
22943 if (decl_specs == NULL)
22944 return;
22946 source_location location = token->location;
22948 if (decl_specs->locations[ds] == 0)
22950 decl_specs->locations[ds] = location;
22951 if (ds == ds_thread)
22952 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
22954 else
22956 if (ds == ds_long)
22958 if (decl_specs->locations[ds_long_long] != 0)
22959 error_at (location,
22960 "%<long long long%> is too long for GCC");
22961 else
22963 decl_specs->locations[ds_long_long] = location;
22964 pedwarn_cxx98 (location,
22965 OPT_Wlong_long,
22966 "ISO C++ 1998 does not support %<long long%>");
22969 else if (ds == ds_thread)
22971 bool gnu = token_is__thread (token);
22972 if (gnu != decl_specs->gnu_thread_keyword_p)
22973 error_at (location,
22974 "both %<__thread%> and %<thread_local%> specified");
22975 else
22976 error_at (location, "duplicate %qD", token->u.value);
22978 else
22980 static const char *const decl_spec_names[] = {
22981 "signed",
22982 "unsigned",
22983 "short",
22984 "long",
22985 "const",
22986 "volatile",
22987 "restrict",
22988 "inline",
22989 "virtual",
22990 "explicit",
22991 "friend",
22992 "typedef",
22993 "using",
22994 "constexpr",
22995 "__complex"
22997 error_at (location,
22998 "duplicate %qs", decl_spec_names[ds]);
23003 /* Return true iff the declarator specifier DS is present in the
23004 sequence of declarator specifiers DECL_SPECS. */
23006 bool
23007 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23008 cp_decl_spec ds)
23010 gcc_assert (ds < ds_last);
23012 if (decl_specs == NULL)
23013 return false;
23015 return decl_specs->locations[ds] != 0;
23018 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23019 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23021 static bool
23022 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23024 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23027 /* Issue an error message indicating that TOKEN_DESC was expected.
23028 If KEYWORD is true, it indicated this function is called by
23029 cp_parser_require_keword and the required token can only be
23030 a indicated keyword. */
23032 static void
23033 cp_parser_required_error (cp_parser *parser,
23034 required_token token_desc,
23035 bool keyword)
23037 switch (token_desc)
23039 case RT_NEW:
23040 cp_parser_error (parser, "expected %<new%>");
23041 return;
23042 case RT_DELETE:
23043 cp_parser_error (parser, "expected %<delete%>");
23044 return;
23045 case RT_RETURN:
23046 cp_parser_error (parser, "expected %<return%>");
23047 return;
23048 case RT_WHILE:
23049 cp_parser_error (parser, "expected %<while%>");
23050 return;
23051 case RT_EXTERN:
23052 cp_parser_error (parser, "expected %<extern%>");
23053 return;
23054 case RT_STATIC_ASSERT:
23055 cp_parser_error (parser, "expected %<static_assert%>");
23056 return;
23057 case RT_DECLTYPE:
23058 cp_parser_error (parser, "expected %<decltype%>");
23059 return;
23060 case RT_OPERATOR:
23061 cp_parser_error (parser, "expected %<operator%>");
23062 return;
23063 case RT_CLASS:
23064 cp_parser_error (parser, "expected %<class%>");
23065 return;
23066 case RT_TEMPLATE:
23067 cp_parser_error (parser, "expected %<template%>");
23068 return;
23069 case RT_NAMESPACE:
23070 cp_parser_error (parser, "expected %<namespace%>");
23071 return;
23072 case RT_USING:
23073 cp_parser_error (parser, "expected %<using%>");
23074 return;
23075 case RT_ASM:
23076 cp_parser_error (parser, "expected %<asm%>");
23077 return;
23078 case RT_TRY:
23079 cp_parser_error (parser, "expected %<try%>");
23080 return;
23081 case RT_CATCH:
23082 cp_parser_error (parser, "expected %<catch%>");
23083 return;
23084 case RT_THROW:
23085 cp_parser_error (parser, "expected %<throw%>");
23086 return;
23087 case RT_LABEL:
23088 cp_parser_error (parser, "expected %<__label__%>");
23089 return;
23090 case RT_AT_TRY:
23091 cp_parser_error (parser, "expected %<@try%>");
23092 return;
23093 case RT_AT_SYNCHRONIZED:
23094 cp_parser_error (parser, "expected %<@synchronized%>");
23095 return;
23096 case RT_AT_THROW:
23097 cp_parser_error (parser, "expected %<@throw%>");
23098 return;
23099 case RT_TRANSACTION_ATOMIC:
23100 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23101 return;
23102 case RT_TRANSACTION_RELAXED:
23103 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23104 return;
23105 default:
23106 break;
23108 if (!keyword)
23110 switch (token_desc)
23112 case RT_SEMICOLON:
23113 cp_parser_error (parser, "expected %<;%>");
23114 return;
23115 case RT_OPEN_PAREN:
23116 cp_parser_error (parser, "expected %<(%>");
23117 return;
23118 case RT_CLOSE_BRACE:
23119 cp_parser_error (parser, "expected %<}%>");
23120 return;
23121 case RT_OPEN_BRACE:
23122 cp_parser_error (parser, "expected %<{%>");
23123 return;
23124 case RT_CLOSE_SQUARE:
23125 cp_parser_error (parser, "expected %<]%>");
23126 return;
23127 case RT_OPEN_SQUARE:
23128 cp_parser_error (parser, "expected %<[%>");
23129 return;
23130 case RT_COMMA:
23131 cp_parser_error (parser, "expected %<,%>");
23132 return;
23133 case RT_SCOPE:
23134 cp_parser_error (parser, "expected %<::%>");
23135 return;
23136 case RT_LESS:
23137 cp_parser_error (parser, "expected %<<%>");
23138 return;
23139 case RT_GREATER:
23140 cp_parser_error (parser, "expected %<>%>");
23141 return;
23142 case RT_EQ:
23143 cp_parser_error (parser, "expected %<=%>");
23144 return;
23145 case RT_ELLIPSIS:
23146 cp_parser_error (parser, "expected %<...%>");
23147 return;
23148 case RT_MULT:
23149 cp_parser_error (parser, "expected %<*%>");
23150 return;
23151 case RT_COMPL:
23152 cp_parser_error (parser, "expected %<~%>");
23153 return;
23154 case RT_COLON:
23155 cp_parser_error (parser, "expected %<:%>");
23156 return;
23157 case RT_COLON_SCOPE:
23158 cp_parser_error (parser, "expected %<:%> or %<::%>");
23159 return;
23160 case RT_CLOSE_PAREN:
23161 cp_parser_error (parser, "expected %<)%>");
23162 return;
23163 case RT_COMMA_CLOSE_PAREN:
23164 cp_parser_error (parser, "expected %<,%> or %<)%>");
23165 return;
23166 case RT_PRAGMA_EOL:
23167 cp_parser_error (parser, "expected end of line");
23168 return;
23169 case RT_NAME:
23170 cp_parser_error (parser, "expected identifier");
23171 return;
23172 case RT_SELECT:
23173 cp_parser_error (parser, "expected selection-statement");
23174 return;
23175 case RT_INTERATION:
23176 cp_parser_error (parser, "expected iteration-statement");
23177 return;
23178 case RT_JUMP:
23179 cp_parser_error (parser, "expected jump-statement");
23180 return;
23181 case RT_CLASS_KEY:
23182 cp_parser_error (parser, "expected class-key");
23183 return;
23184 case RT_CLASS_TYPENAME_TEMPLATE:
23185 cp_parser_error (parser,
23186 "expected %<class%>, %<typename%>, or %<template%>");
23187 return;
23188 default:
23189 gcc_unreachable ();
23192 else
23193 gcc_unreachable ();
23198 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23199 issue an error message indicating that TOKEN_DESC was expected.
23201 Returns the token consumed, if the token had the appropriate type.
23202 Otherwise, returns NULL. */
23204 static cp_token *
23205 cp_parser_require (cp_parser* parser,
23206 enum cpp_ttype type,
23207 required_token token_desc)
23209 if (cp_lexer_next_token_is (parser->lexer, type))
23210 return cp_lexer_consume_token (parser->lexer);
23211 else
23213 /* Output the MESSAGE -- unless we're parsing tentatively. */
23214 if (!cp_parser_simulate_error (parser))
23215 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23216 return NULL;
23220 /* An error message is produced if the next token is not '>'.
23221 All further tokens are skipped until the desired token is
23222 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23224 static void
23225 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23227 /* Current level of '< ... >'. */
23228 unsigned level = 0;
23229 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23230 unsigned nesting_depth = 0;
23232 /* Are we ready, yet? If not, issue error message. */
23233 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23234 return;
23236 /* Skip tokens until the desired token is found. */
23237 while (true)
23239 /* Peek at the next token. */
23240 switch (cp_lexer_peek_token (parser->lexer)->type)
23242 case CPP_LESS:
23243 if (!nesting_depth)
23244 ++level;
23245 break;
23247 case CPP_RSHIFT:
23248 if (cxx_dialect == cxx98)
23249 /* C++0x views the `>>' operator as two `>' tokens, but
23250 C++98 does not. */
23251 break;
23252 else if (!nesting_depth && level-- == 0)
23254 /* We've hit a `>>' where the first `>' closes the
23255 template argument list, and the second `>' is
23256 spurious. Just consume the `>>' and stop; we've
23257 already produced at least one error. */
23258 cp_lexer_consume_token (parser->lexer);
23259 return;
23261 /* Fall through for C++0x, so we handle the second `>' in
23262 the `>>'. */
23264 case CPP_GREATER:
23265 if (!nesting_depth && level-- == 0)
23267 /* We've reached the token we want, consume it and stop. */
23268 cp_lexer_consume_token (parser->lexer);
23269 return;
23271 break;
23273 case CPP_OPEN_PAREN:
23274 case CPP_OPEN_SQUARE:
23275 ++nesting_depth;
23276 break;
23278 case CPP_CLOSE_PAREN:
23279 case CPP_CLOSE_SQUARE:
23280 if (nesting_depth-- == 0)
23281 return;
23282 break;
23284 case CPP_EOF:
23285 case CPP_PRAGMA_EOL:
23286 case CPP_SEMICOLON:
23287 case CPP_OPEN_BRACE:
23288 case CPP_CLOSE_BRACE:
23289 /* The '>' was probably forgotten, don't look further. */
23290 return;
23292 default:
23293 break;
23296 /* Consume this token. */
23297 cp_lexer_consume_token (parser->lexer);
23301 /* If the next token is the indicated keyword, consume it. Otherwise,
23302 issue an error message indicating that TOKEN_DESC was expected.
23304 Returns the token consumed, if the token had the appropriate type.
23305 Otherwise, returns NULL. */
23307 static cp_token *
23308 cp_parser_require_keyword (cp_parser* parser,
23309 enum rid keyword,
23310 required_token token_desc)
23312 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23314 if (token && token->keyword != keyword)
23316 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23317 return NULL;
23320 return token;
23323 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23324 function-definition. */
23326 static bool
23327 cp_parser_token_starts_function_definition_p (cp_token* token)
23329 return (/* An ordinary function-body begins with an `{'. */
23330 token->type == CPP_OPEN_BRACE
23331 /* A ctor-initializer begins with a `:'. */
23332 || token->type == CPP_COLON
23333 /* A function-try-block begins with `try'. */
23334 || token->keyword == RID_TRY
23335 /* A function-transaction-block begins with `__transaction_atomic'
23336 or `__transaction_relaxed'. */
23337 || token->keyword == RID_TRANSACTION_ATOMIC
23338 || token->keyword == RID_TRANSACTION_RELAXED
23339 /* The named return value extension begins with `return'. */
23340 || token->keyword == RID_RETURN);
23343 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23344 definition. */
23346 static bool
23347 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23349 cp_token *token;
23351 token = cp_lexer_peek_token (parser->lexer);
23352 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23355 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23356 C++0x) ending a template-argument. */
23358 static bool
23359 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23361 cp_token *token;
23363 token = cp_lexer_peek_token (parser->lexer);
23364 return (token->type == CPP_COMMA
23365 || token->type == CPP_GREATER
23366 || token->type == CPP_ELLIPSIS
23367 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23370 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23371 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23373 static bool
23374 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23375 size_t n)
23377 cp_token *token;
23379 token = cp_lexer_peek_nth_token (parser->lexer, n);
23380 if (token->type == CPP_LESS)
23381 return true;
23382 /* Check for the sequence `<::' in the original code. It would be lexed as
23383 `[:', where `[' is a digraph, and there is no whitespace before
23384 `:'. */
23385 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23387 cp_token *token2;
23388 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23389 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23390 return true;
23392 return false;
23395 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23396 or none_type otherwise. */
23398 static enum tag_types
23399 cp_parser_token_is_class_key (cp_token* token)
23401 switch (token->keyword)
23403 case RID_CLASS:
23404 return class_type;
23405 case RID_STRUCT:
23406 return record_type;
23407 case RID_UNION:
23408 return union_type;
23410 default:
23411 return none_type;
23415 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23417 static void
23418 cp_parser_check_class_key (enum tag_types class_key, tree type)
23420 if (type == error_mark_node)
23421 return;
23422 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23424 permerror (input_location, "%qs tag used in naming %q#T",
23425 class_key == union_type ? "union"
23426 : class_key == record_type ? "struct" : "class",
23427 type);
23428 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23429 "%q#T was previously declared here", type);
23433 /* Issue an error message if DECL is redeclared with different
23434 access than its original declaration [class.access.spec/3].
23435 This applies to nested classes and nested class templates.
23436 [class.mem/1]. */
23438 static void
23439 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23441 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23442 return;
23444 if ((TREE_PRIVATE (decl)
23445 != (current_access_specifier == access_private_node))
23446 || (TREE_PROTECTED (decl)
23447 != (current_access_specifier == access_protected_node)))
23448 error_at (location, "%qD redeclared with different access", decl);
23451 /* Look for the `template' keyword, as a syntactic disambiguator.
23452 Return TRUE iff it is present, in which case it will be
23453 consumed. */
23455 static bool
23456 cp_parser_optional_template_keyword (cp_parser *parser)
23458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23460 /* In C++98 the `template' keyword can only be used within templates;
23461 outside templates the parser can always figure out what is a
23462 template and what is not. In C++11, per the resolution of DR 468,
23463 `template' is allowed in cases where it is not strictly necessary. */
23464 if (!processing_template_decl
23465 && pedantic && cxx_dialect == cxx98)
23467 cp_token *token = cp_lexer_peek_token (parser->lexer);
23468 pedwarn (token->location, OPT_Wpedantic,
23469 "in C++98 %<template%> (as a disambiguator) is only "
23470 "allowed within templates");
23471 /* If this part of the token stream is rescanned, the same
23472 error message would be generated. So, we purge the token
23473 from the stream. */
23474 cp_lexer_purge_token (parser->lexer);
23475 return false;
23477 else
23479 /* Consume the `template' keyword. */
23480 cp_lexer_consume_token (parser->lexer);
23481 return true;
23484 return false;
23487 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23488 set PARSER->SCOPE, and perform other related actions. */
23490 static void
23491 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23493 int i;
23494 struct tree_check *check_value;
23495 deferred_access_check *chk;
23496 vec<deferred_access_check, va_gc> *checks;
23498 /* Get the stored value. */
23499 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23500 /* Perform any access checks that were deferred. */
23501 checks = check_value->checks;
23502 if (checks)
23504 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23505 perform_or_defer_access_check (chk->binfo,
23506 chk->decl,
23507 chk->diag_decl, tf_warning_or_error);
23509 /* Set the scope from the stored value. */
23510 parser->scope = check_value->value;
23511 parser->qualifying_scope = check_value->qualifying_scope;
23512 parser->object_scope = NULL_TREE;
23515 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23516 encounter the end of a block before what we were looking for. */
23518 static bool
23519 cp_parser_cache_group (cp_parser *parser,
23520 enum cpp_ttype end,
23521 unsigned depth)
23523 while (true)
23525 cp_token *token = cp_lexer_peek_token (parser->lexer);
23527 /* Abort a parenthesized expression if we encounter a semicolon. */
23528 if ((end == CPP_CLOSE_PAREN || depth == 0)
23529 && token->type == CPP_SEMICOLON)
23530 return true;
23531 /* If we've reached the end of the file, stop. */
23532 if (token->type == CPP_EOF
23533 || (end != CPP_PRAGMA_EOL
23534 && token->type == CPP_PRAGMA_EOL))
23535 return true;
23536 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23537 /* We've hit the end of an enclosing block, so there's been some
23538 kind of syntax error. */
23539 return true;
23541 /* Consume the token. */
23542 cp_lexer_consume_token (parser->lexer);
23543 /* See if it starts a new group. */
23544 if (token->type == CPP_OPEN_BRACE)
23546 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23547 /* In theory this should probably check end == '}', but
23548 cp_parser_save_member_function_body needs it to exit
23549 after either '}' or ')' when called with ')'. */
23550 if (depth == 0)
23551 return false;
23553 else if (token->type == CPP_OPEN_PAREN)
23555 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23556 if (depth == 0 && end == CPP_CLOSE_PAREN)
23557 return false;
23559 else if (token->type == CPP_PRAGMA)
23560 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23561 else if (token->type == end)
23562 return false;
23566 /* Like above, for caching a default argument or NSDMI. Both of these are
23567 terminated by a non-nested comma, but it can be unclear whether or not a
23568 comma is nested in a template argument list unless we do more parsing.
23569 In order to handle this ambiguity, when we encounter a ',' after a '<'
23570 we try to parse what follows as a parameter-declaration-list (in the
23571 case of a default argument) or a member-declarator (in the case of an
23572 NSDMI). If that succeeds, then we stop caching. */
23574 static tree
23575 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23577 unsigned depth = 0;
23578 int maybe_template_id = 0;
23579 cp_token *first_token;
23580 cp_token *token;
23581 tree default_argument;
23583 /* Add tokens until we have processed the entire default
23584 argument. We add the range [first_token, token). */
23585 first_token = cp_lexer_peek_token (parser->lexer);
23586 if (first_token->type == CPP_OPEN_BRACE)
23588 /* For list-initialization, this is straightforward. */
23589 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23590 token = cp_lexer_peek_token (parser->lexer);
23592 else while (true)
23594 bool done = false;
23596 /* Peek at the next token. */
23597 token = cp_lexer_peek_token (parser->lexer);
23598 /* What we do depends on what token we have. */
23599 switch (token->type)
23601 /* In valid code, a default argument must be
23602 immediately followed by a `,' `)', or `...'. */
23603 case CPP_COMMA:
23604 if (depth == 0 && maybe_template_id)
23606 /* If we've seen a '<', we might be in a
23607 template-argument-list. Until Core issue 325 is
23608 resolved, we don't know how this situation ought
23609 to be handled, so try to DTRT. We check whether
23610 what comes after the comma is a valid parameter
23611 declaration list. If it is, then the comma ends
23612 the default argument; otherwise the default
23613 argument continues. */
23614 bool error = false;
23615 tree t;
23617 /* Set ITALP so cp_parser_parameter_declaration_list
23618 doesn't decide to commit to this parse. */
23619 bool saved_italp = parser->in_template_argument_list_p;
23620 parser->in_template_argument_list_p = true;
23622 cp_parser_parse_tentatively (parser);
23623 cp_lexer_consume_token (parser->lexer);
23625 if (nsdmi)
23627 int ctor_dtor_or_conv_p;
23628 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23629 &ctor_dtor_or_conv_p,
23630 /*parenthesized_p=*/NULL,
23631 /*member_p=*/true);
23633 else
23635 begin_scope (sk_function_parms, NULL_TREE);
23636 cp_parser_parameter_declaration_list (parser, &error);
23637 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23638 pop_binding (DECL_NAME (t), t);
23639 leave_scope ();
23641 if (!cp_parser_error_occurred (parser) && !error)
23642 done = true;
23643 cp_parser_abort_tentative_parse (parser);
23645 parser->in_template_argument_list_p = saved_italp;
23646 break;
23648 case CPP_CLOSE_PAREN:
23649 case CPP_ELLIPSIS:
23650 /* If we run into a non-nested `;', `}', or `]',
23651 then the code is invalid -- but the default
23652 argument is certainly over. */
23653 case CPP_SEMICOLON:
23654 case CPP_CLOSE_BRACE:
23655 case CPP_CLOSE_SQUARE:
23656 if (depth == 0)
23657 done = true;
23658 /* Update DEPTH, if necessary. */
23659 else if (token->type == CPP_CLOSE_PAREN
23660 || token->type == CPP_CLOSE_BRACE
23661 || token->type == CPP_CLOSE_SQUARE)
23662 --depth;
23663 break;
23665 case CPP_OPEN_PAREN:
23666 case CPP_OPEN_SQUARE:
23667 case CPP_OPEN_BRACE:
23668 ++depth;
23669 break;
23671 case CPP_LESS:
23672 if (depth == 0)
23673 /* This might be the comparison operator, or it might
23674 start a template argument list. */
23675 ++maybe_template_id;
23676 break;
23678 case CPP_RSHIFT:
23679 if (cxx_dialect == cxx98)
23680 break;
23681 /* Fall through for C++0x, which treats the `>>'
23682 operator like two `>' tokens in certain
23683 cases. */
23685 case CPP_GREATER:
23686 if (depth == 0)
23688 /* This might be an operator, or it might close a
23689 template argument list. But if a previous '<'
23690 started a template argument list, this will have
23691 closed it, so we can't be in one anymore. */
23692 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23693 if (maybe_template_id < 0)
23694 maybe_template_id = 0;
23696 break;
23698 /* If we run out of tokens, issue an error message. */
23699 case CPP_EOF:
23700 case CPP_PRAGMA_EOL:
23701 error_at (token->location, "file ends in default argument");
23702 done = true;
23703 break;
23705 case CPP_NAME:
23706 case CPP_SCOPE:
23707 /* In these cases, we should look for template-ids.
23708 For example, if the default argument is
23709 `X<int, double>()', we need to do name lookup to
23710 figure out whether or not `X' is a template; if
23711 so, the `,' does not end the default argument.
23713 That is not yet done. */
23714 break;
23716 default:
23717 break;
23720 /* If we've reached the end, stop. */
23721 if (done)
23722 break;
23724 /* Add the token to the token block. */
23725 token = cp_lexer_consume_token (parser->lexer);
23728 /* Create a DEFAULT_ARG to represent the unparsed default
23729 argument. */
23730 default_argument = make_node (DEFAULT_ARG);
23731 DEFARG_TOKENS (default_argument)
23732 = cp_token_cache_new (first_token, token);
23733 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23735 return default_argument;
23738 /* Begin parsing tentatively. We always save tokens while parsing
23739 tentatively so that if the tentative parsing fails we can restore the
23740 tokens. */
23742 static void
23743 cp_parser_parse_tentatively (cp_parser* parser)
23745 /* Enter a new parsing context. */
23746 parser->context = cp_parser_context_new (parser->context);
23747 /* Begin saving tokens. */
23748 cp_lexer_save_tokens (parser->lexer);
23749 /* In order to avoid repetitive access control error messages,
23750 access checks are queued up until we are no longer parsing
23751 tentatively. */
23752 push_deferring_access_checks (dk_deferred);
23755 /* Commit to the currently active tentative parse. */
23757 static void
23758 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23760 cp_parser_context *context;
23761 cp_lexer *lexer;
23763 /* Mark all of the levels as committed. */
23764 lexer = parser->lexer;
23765 for (context = parser->context; context->next; context = context->next)
23767 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23768 break;
23769 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23770 while (!cp_lexer_saving_tokens (lexer))
23771 lexer = lexer->next;
23772 cp_lexer_commit_tokens (lexer);
23776 /* Abort the currently active tentative parse. All consumed tokens
23777 will be rolled back, and no diagnostics will be issued. */
23779 static void
23780 cp_parser_abort_tentative_parse (cp_parser* parser)
23782 cp_parser_simulate_error (parser);
23783 /* Now, pretend that we want to see if the construct was
23784 successfully parsed. */
23785 cp_parser_parse_definitely (parser);
23788 /* Stop parsing tentatively. If a parse error has occurred, restore the
23789 token stream. Otherwise, commit to the tokens we have consumed.
23790 Returns true if no error occurred; false otherwise. */
23792 static bool
23793 cp_parser_parse_definitely (cp_parser* parser)
23795 bool error_occurred;
23796 cp_parser_context *context;
23798 /* Remember whether or not an error occurred, since we are about to
23799 destroy that information. */
23800 error_occurred = cp_parser_error_occurred (parser);
23801 /* Remove the topmost context from the stack. */
23802 context = parser->context;
23803 parser->context = context->next;
23804 /* If no parse errors occurred, commit to the tentative parse. */
23805 if (!error_occurred)
23807 /* Commit to the tokens read tentatively, unless that was
23808 already done. */
23809 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23810 cp_lexer_commit_tokens (parser->lexer);
23812 pop_to_parent_deferring_access_checks ();
23814 /* Otherwise, if errors occurred, roll back our state so that things
23815 are just as they were before we began the tentative parse. */
23816 else
23818 cp_lexer_rollback_tokens (parser->lexer);
23819 pop_deferring_access_checks ();
23821 /* Add the context to the front of the free list. */
23822 context->next = cp_parser_context_free_list;
23823 cp_parser_context_free_list = context;
23825 return !error_occurred;
23828 /* Returns true if we are parsing tentatively and are not committed to
23829 this tentative parse. */
23831 static bool
23832 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23834 return (cp_parser_parsing_tentatively (parser)
23835 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23838 /* Returns nonzero iff an error has occurred during the most recent
23839 tentative parse. */
23841 static bool
23842 cp_parser_error_occurred (cp_parser* parser)
23844 return (cp_parser_parsing_tentatively (parser)
23845 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23848 /* Returns nonzero if GNU extensions are allowed. */
23850 static bool
23851 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23853 return parser->allow_gnu_extensions_p;
23856 /* Objective-C++ Productions */
23859 /* Parse an Objective-C expression, which feeds into a primary-expression
23860 above.
23862 objc-expression:
23863 objc-message-expression
23864 objc-string-literal
23865 objc-encode-expression
23866 objc-protocol-expression
23867 objc-selector-expression
23869 Returns a tree representation of the expression. */
23871 static tree
23872 cp_parser_objc_expression (cp_parser* parser)
23874 /* Try to figure out what kind of declaration is present. */
23875 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23877 switch (kwd->type)
23879 case CPP_OPEN_SQUARE:
23880 return cp_parser_objc_message_expression (parser);
23882 case CPP_OBJC_STRING:
23883 kwd = cp_lexer_consume_token (parser->lexer);
23884 return objc_build_string_object (kwd->u.value);
23886 case CPP_KEYWORD:
23887 switch (kwd->keyword)
23889 case RID_AT_ENCODE:
23890 return cp_parser_objc_encode_expression (parser);
23892 case RID_AT_PROTOCOL:
23893 return cp_parser_objc_protocol_expression (parser);
23895 case RID_AT_SELECTOR:
23896 return cp_parser_objc_selector_expression (parser);
23898 default:
23899 break;
23901 default:
23902 error_at (kwd->location,
23903 "misplaced %<@%D%> Objective-C++ construct",
23904 kwd->u.value);
23905 cp_parser_skip_to_end_of_block_or_statement (parser);
23908 return error_mark_node;
23911 /* Parse an Objective-C message expression.
23913 objc-message-expression:
23914 [ objc-message-receiver objc-message-args ]
23916 Returns a representation of an Objective-C message. */
23918 static tree
23919 cp_parser_objc_message_expression (cp_parser* parser)
23921 tree receiver, messageargs;
23923 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23924 receiver = cp_parser_objc_message_receiver (parser);
23925 messageargs = cp_parser_objc_message_args (parser);
23926 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23928 return objc_build_message_expr (receiver, messageargs);
23931 /* Parse an objc-message-receiver.
23933 objc-message-receiver:
23934 expression
23935 simple-type-specifier
23937 Returns a representation of the type or expression. */
23939 static tree
23940 cp_parser_objc_message_receiver (cp_parser* parser)
23942 tree rcv;
23944 /* An Objective-C message receiver may be either (1) a type
23945 or (2) an expression. */
23946 cp_parser_parse_tentatively (parser);
23947 rcv = cp_parser_expression (parser, false, NULL);
23949 if (cp_parser_parse_definitely (parser))
23950 return rcv;
23952 rcv = cp_parser_simple_type_specifier (parser,
23953 /*decl_specs=*/NULL,
23954 CP_PARSER_FLAGS_NONE);
23956 return objc_get_class_reference (rcv);
23959 /* Parse the arguments and selectors comprising an Objective-C message.
23961 objc-message-args:
23962 objc-selector
23963 objc-selector-args
23964 objc-selector-args , objc-comma-args
23966 objc-selector-args:
23967 objc-selector [opt] : assignment-expression
23968 objc-selector-args objc-selector [opt] : assignment-expression
23970 objc-comma-args:
23971 assignment-expression
23972 objc-comma-args , assignment-expression
23974 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23975 selector arguments and TREE_VALUE containing a list of comma
23976 arguments. */
23978 static tree
23979 cp_parser_objc_message_args (cp_parser* parser)
23981 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23982 bool maybe_unary_selector_p = true;
23983 cp_token *token = cp_lexer_peek_token (parser->lexer);
23985 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23987 tree selector = NULL_TREE, arg;
23989 if (token->type != CPP_COLON)
23990 selector = cp_parser_objc_selector (parser);
23992 /* Detect if we have a unary selector. */
23993 if (maybe_unary_selector_p
23994 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23995 return build_tree_list (selector, NULL_TREE);
23997 maybe_unary_selector_p = false;
23998 cp_parser_require (parser, CPP_COLON, RT_COLON);
23999 arg = cp_parser_assignment_expression (parser, false, NULL);
24001 sel_args
24002 = chainon (sel_args,
24003 build_tree_list (selector, arg));
24005 token = cp_lexer_peek_token (parser->lexer);
24008 /* Handle non-selector arguments, if any. */
24009 while (token->type == CPP_COMMA)
24011 tree arg;
24013 cp_lexer_consume_token (parser->lexer);
24014 arg = cp_parser_assignment_expression (parser, false, NULL);
24016 addl_args
24017 = chainon (addl_args,
24018 build_tree_list (NULL_TREE, arg));
24020 token = cp_lexer_peek_token (parser->lexer);
24023 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24025 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24026 return build_tree_list (error_mark_node, error_mark_node);
24029 return build_tree_list (sel_args, addl_args);
24032 /* Parse an Objective-C encode expression.
24034 objc-encode-expression:
24035 @encode objc-typename
24037 Returns an encoded representation of the type argument. */
24039 static tree
24040 cp_parser_objc_encode_expression (cp_parser* parser)
24042 tree type;
24043 cp_token *token;
24045 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24046 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24047 token = cp_lexer_peek_token (parser->lexer);
24048 type = complete_type (cp_parser_type_id (parser));
24049 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24051 if (!type)
24053 error_at (token->location,
24054 "%<@encode%> must specify a type as an argument");
24055 return error_mark_node;
24058 /* This happens if we find @encode(T) (where T is a template
24059 typename or something dependent on a template typename) when
24060 parsing a template. In that case, we can't compile it
24061 immediately, but we rather create an AT_ENCODE_EXPR which will
24062 need to be instantiated when the template is used.
24064 if (dependent_type_p (type))
24066 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24067 TREE_READONLY (value) = 1;
24068 return value;
24071 return objc_build_encode_expr (type);
24074 /* Parse an Objective-C @defs expression. */
24076 static tree
24077 cp_parser_objc_defs_expression (cp_parser *parser)
24079 tree name;
24081 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24082 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24083 name = cp_parser_identifier (parser);
24084 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24086 return objc_get_class_ivars (name);
24089 /* Parse an Objective-C protocol expression.
24091 objc-protocol-expression:
24092 @protocol ( identifier )
24094 Returns a representation of the protocol expression. */
24096 static tree
24097 cp_parser_objc_protocol_expression (cp_parser* parser)
24099 tree proto;
24101 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24102 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24103 proto = cp_parser_identifier (parser);
24104 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24106 return objc_build_protocol_expr (proto);
24109 /* Parse an Objective-C selector expression.
24111 objc-selector-expression:
24112 @selector ( objc-method-signature )
24114 objc-method-signature:
24115 objc-selector
24116 objc-selector-seq
24118 objc-selector-seq:
24119 objc-selector :
24120 objc-selector-seq objc-selector :
24122 Returns a representation of the method selector. */
24124 static tree
24125 cp_parser_objc_selector_expression (cp_parser* parser)
24127 tree sel_seq = NULL_TREE;
24128 bool maybe_unary_selector_p = true;
24129 cp_token *token;
24130 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24132 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
24133 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24134 token = cp_lexer_peek_token (parser->lexer);
24136 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24137 || token->type == CPP_SCOPE)
24139 tree selector = NULL_TREE;
24141 if (token->type != CPP_COLON
24142 || token->type == CPP_SCOPE)
24143 selector = cp_parser_objc_selector (parser);
24145 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24146 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24148 /* Detect if we have a unary selector. */
24149 if (maybe_unary_selector_p)
24151 sel_seq = selector;
24152 goto finish_selector;
24154 else
24156 cp_parser_error (parser, "expected %<:%>");
24159 maybe_unary_selector_p = false;
24160 token = cp_lexer_consume_token (parser->lexer);
24162 if (token->type == CPP_SCOPE)
24164 sel_seq
24165 = chainon (sel_seq,
24166 build_tree_list (selector, NULL_TREE));
24167 sel_seq
24168 = chainon (sel_seq,
24169 build_tree_list (NULL_TREE, NULL_TREE));
24171 else
24172 sel_seq
24173 = chainon (sel_seq,
24174 build_tree_list (selector, NULL_TREE));
24176 token = cp_lexer_peek_token (parser->lexer);
24179 finish_selector:
24180 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24182 return objc_build_selector_expr (loc, sel_seq);
24185 /* Parse a list of identifiers.
24187 objc-identifier-list:
24188 identifier
24189 objc-identifier-list , identifier
24191 Returns a TREE_LIST of identifier nodes. */
24193 static tree
24194 cp_parser_objc_identifier_list (cp_parser* parser)
24196 tree identifier;
24197 tree list;
24198 cp_token *sep;
24200 identifier = cp_parser_identifier (parser);
24201 if (identifier == error_mark_node)
24202 return error_mark_node;
24204 list = build_tree_list (NULL_TREE, identifier);
24205 sep = cp_lexer_peek_token (parser->lexer);
24207 while (sep->type == CPP_COMMA)
24209 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24210 identifier = cp_parser_identifier (parser);
24211 if (identifier == error_mark_node)
24212 return list;
24214 list = chainon (list, build_tree_list (NULL_TREE,
24215 identifier));
24216 sep = cp_lexer_peek_token (parser->lexer);
24219 return list;
24222 /* Parse an Objective-C alias declaration.
24224 objc-alias-declaration:
24225 @compatibility_alias identifier identifier ;
24227 This function registers the alias mapping with the Objective-C front end.
24228 It returns nothing. */
24230 static void
24231 cp_parser_objc_alias_declaration (cp_parser* parser)
24233 tree alias, orig;
24235 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24236 alias = cp_parser_identifier (parser);
24237 orig = cp_parser_identifier (parser);
24238 objc_declare_alias (alias, orig);
24239 cp_parser_consume_semicolon_at_end_of_statement (parser);
24242 /* Parse an Objective-C class forward-declaration.
24244 objc-class-declaration:
24245 @class objc-identifier-list ;
24247 The function registers the forward declarations with the Objective-C
24248 front end. It returns nothing. */
24250 static void
24251 cp_parser_objc_class_declaration (cp_parser* parser)
24253 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24254 while (true)
24256 tree id;
24258 id = cp_parser_identifier (parser);
24259 if (id == error_mark_node)
24260 break;
24262 objc_declare_class (id);
24264 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24265 cp_lexer_consume_token (parser->lexer);
24266 else
24267 break;
24269 cp_parser_consume_semicolon_at_end_of_statement (parser);
24272 /* Parse a list of Objective-C protocol references.
24274 objc-protocol-refs-opt:
24275 objc-protocol-refs [opt]
24277 objc-protocol-refs:
24278 < objc-identifier-list >
24280 Returns a TREE_LIST of identifiers, if any. */
24282 static tree
24283 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24285 tree protorefs = NULL_TREE;
24287 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24289 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24290 protorefs = cp_parser_objc_identifier_list (parser);
24291 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24294 return protorefs;
24297 /* Parse a Objective-C visibility specification. */
24299 static void
24300 cp_parser_objc_visibility_spec (cp_parser* parser)
24302 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24304 switch (vis->keyword)
24306 case RID_AT_PRIVATE:
24307 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24308 break;
24309 case RID_AT_PROTECTED:
24310 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24311 break;
24312 case RID_AT_PUBLIC:
24313 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24314 break;
24315 case RID_AT_PACKAGE:
24316 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24317 break;
24318 default:
24319 return;
24322 /* Eat '@private'/'@protected'/'@public'. */
24323 cp_lexer_consume_token (parser->lexer);
24326 /* Parse an Objective-C method type. Return 'true' if it is a class
24327 (+) method, and 'false' if it is an instance (-) method. */
24329 static inline bool
24330 cp_parser_objc_method_type (cp_parser* parser)
24332 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24333 return true;
24334 else
24335 return false;
24338 /* Parse an Objective-C protocol qualifier. */
24340 static tree
24341 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24343 tree quals = NULL_TREE, node;
24344 cp_token *token = cp_lexer_peek_token (parser->lexer);
24346 node = token->u.value;
24348 while (node && TREE_CODE (node) == IDENTIFIER_NODE
24349 && (node == ridpointers [(int) RID_IN]
24350 || node == ridpointers [(int) RID_OUT]
24351 || node == ridpointers [(int) RID_INOUT]
24352 || node == ridpointers [(int) RID_BYCOPY]
24353 || node == ridpointers [(int) RID_BYREF]
24354 || node == ridpointers [(int) RID_ONEWAY]))
24356 quals = tree_cons (NULL_TREE, node, quals);
24357 cp_lexer_consume_token (parser->lexer);
24358 token = cp_lexer_peek_token (parser->lexer);
24359 node = token->u.value;
24362 return quals;
24365 /* Parse an Objective-C typename. */
24367 static tree
24368 cp_parser_objc_typename (cp_parser* parser)
24370 tree type_name = NULL_TREE;
24372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24374 tree proto_quals, cp_type = NULL_TREE;
24376 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24377 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24379 /* An ObjC type name may consist of just protocol qualifiers, in which
24380 case the type shall default to 'id'. */
24381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24383 cp_type = cp_parser_type_id (parser);
24385 /* If the type could not be parsed, an error has already
24386 been produced. For error recovery, behave as if it had
24387 not been specified, which will use the default type
24388 'id'. */
24389 if (cp_type == error_mark_node)
24391 cp_type = NULL_TREE;
24392 /* We need to skip to the closing parenthesis as
24393 cp_parser_type_id() does not seem to do it for
24394 us. */
24395 cp_parser_skip_to_closing_parenthesis (parser,
24396 /*recovering=*/true,
24397 /*or_comma=*/false,
24398 /*consume_paren=*/false);
24402 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24403 type_name = build_tree_list (proto_quals, cp_type);
24406 return type_name;
24409 /* Check to see if TYPE refers to an Objective-C selector name. */
24411 static bool
24412 cp_parser_objc_selector_p (enum cpp_ttype type)
24414 return (type == CPP_NAME || type == CPP_KEYWORD
24415 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24416 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24417 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24418 || type == CPP_XOR || type == CPP_XOR_EQ);
24421 /* Parse an Objective-C selector. */
24423 static tree
24424 cp_parser_objc_selector (cp_parser* parser)
24426 cp_token *token = cp_lexer_consume_token (parser->lexer);
24428 if (!cp_parser_objc_selector_p (token->type))
24430 error_at (token->location, "invalid Objective-C++ selector name");
24431 return error_mark_node;
24434 /* C++ operator names are allowed to appear in ObjC selectors. */
24435 switch (token->type)
24437 case CPP_AND_AND: return get_identifier ("and");
24438 case CPP_AND_EQ: return get_identifier ("and_eq");
24439 case CPP_AND: return get_identifier ("bitand");
24440 case CPP_OR: return get_identifier ("bitor");
24441 case CPP_COMPL: return get_identifier ("compl");
24442 case CPP_NOT: return get_identifier ("not");
24443 case CPP_NOT_EQ: return get_identifier ("not_eq");
24444 case CPP_OR_OR: return get_identifier ("or");
24445 case CPP_OR_EQ: return get_identifier ("or_eq");
24446 case CPP_XOR: return get_identifier ("xor");
24447 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24448 default: return token->u.value;
24452 /* Parse an Objective-C params list. */
24454 static tree
24455 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24457 tree params = NULL_TREE;
24458 bool maybe_unary_selector_p = true;
24459 cp_token *token = cp_lexer_peek_token (parser->lexer);
24461 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24463 tree selector = NULL_TREE, type_name, identifier;
24464 tree parm_attr = NULL_TREE;
24466 if (token->keyword == RID_ATTRIBUTE)
24467 break;
24469 if (token->type != CPP_COLON)
24470 selector = cp_parser_objc_selector (parser);
24472 /* Detect if we have a unary selector. */
24473 if (maybe_unary_selector_p
24474 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24476 params = selector; /* Might be followed by attributes. */
24477 break;
24480 maybe_unary_selector_p = false;
24481 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24483 /* Something went quite wrong. There should be a colon
24484 here, but there is not. Stop parsing parameters. */
24485 break;
24487 type_name = cp_parser_objc_typename (parser);
24488 /* New ObjC allows attributes on parameters too. */
24489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24490 parm_attr = cp_parser_attributes_opt (parser);
24491 identifier = cp_parser_identifier (parser);
24493 params
24494 = chainon (params,
24495 objc_build_keyword_decl (selector,
24496 type_name,
24497 identifier,
24498 parm_attr));
24500 token = cp_lexer_peek_token (parser->lexer);
24503 if (params == NULL_TREE)
24505 cp_parser_error (parser, "objective-c++ method declaration is expected");
24506 return error_mark_node;
24509 /* We allow tail attributes for the method. */
24510 if (token->keyword == RID_ATTRIBUTE)
24512 *attributes = cp_parser_attributes_opt (parser);
24513 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24514 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24515 return params;
24516 cp_parser_error (parser,
24517 "method attributes must be specified at the end");
24518 return error_mark_node;
24521 if (params == NULL_TREE)
24523 cp_parser_error (parser, "objective-c++ method declaration is expected");
24524 return error_mark_node;
24526 return params;
24529 /* Parse the non-keyword Objective-C params. */
24531 static tree
24532 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24533 tree* attributes)
24535 tree params = make_node (TREE_LIST);
24536 cp_token *token = cp_lexer_peek_token (parser->lexer);
24537 *ellipsisp = false; /* Initially, assume no ellipsis. */
24539 while (token->type == CPP_COMMA)
24541 cp_parameter_declarator *parmdecl;
24542 tree parm;
24544 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24545 token = cp_lexer_peek_token (parser->lexer);
24547 if (token->type == CPP_ELLIPSIS)
24549 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24550 *ellipsisp = true;
24551 token = cp_lexer_peek_token (parser->lexer);
24552 break;
24555 /* TODO: parse attributes for tail parameters. */
24556 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24557 parm = grokdeclarator (parmdecl->declarator,
24558 &parmdecl->decl_specifiers,
24559 PARM, /*initialized=*/0,
24560 /*attrlist=*/NULL);
24562 chainon (params, build_tree_list (NULL_TREE, parm));
24563 token = cp_lexer_peek_token (parser->lexer);
24566 /* We allow tail attributes for the method. */
24567 if (token->keyword == RID_ATTRIBUTE)
24569 if (*attributes == NULL_TREE)
24571 *attributes = cp_parser_attributes_opt (parser);
24572 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24573 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24574 return params;
24576 else
24577 /* We have an error, but parse the attributes, so that we can
24578 carry on. */
24579 *attributes = cp_parser_attributes_opt (parser);
24581 cp_parser_error (parser,
24582 "method attributes must be specified at the end");
24583 return error_mark_node;
24586 return params;
24589 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24591 static void
24592 cp_parser_objc_interstitial_code (cp_parser* parser)
24594 cp_token *token = cp_lexer_peek_token (parser->lexer);
24596 /* If the next token is `extern' and the following token is a string
24597 literal, then we have a linkage specification. */
24598 if (token->keyword == RID_EXTERN
24599 && cp_parser_is_pure_string_literal
24600 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24601 cp_parser_linkage_specification (parser);
24602 /* Handle #pragma, if any. */
24603 else if (token->type == CPP_PRAGMA)
24604 cp_parser_pragma (parser, pragma_external);
24605 /* Allow stray semicolons. */
24606 else if (token->type == CPP_SEMICOLON)
24607 cp_lexer_consume_token (parser->lexer);
24608 /* Mark methods as optional or required, when building protocols. */
24609 else if (token->keyword == RID_AT_OPTIONAL)
24611 cp_lexer_consume_token (parser->lexer);
24612 objc_set_method_opt (true);
24614 else if (token->keyword == RID_AT_REQUIRED)
24616 cp_lexer_consume_token (parser->lexer);
24617 objc_set_method_opt (false);
24619 else if (token->keyword == RID_NAMESPACE)
24620 cp_parser_namespace_definition (parser);
24621 /* Other stray characters must generate errors. */
24622 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24624 cp_lexer_consume_token (parser->lexer);
24625 error ("stray %qs between Objective-C++ methods",
24626 token->type == CPP_OPEN_BRACE ? "{" : "}");
24628 /* Finally, try to parse a block-declaration, or a function-definition. */
24629 else
24630 cp_parser_block_declaration (parser, /*statement_p=*/false);
24633 /* Parse a method signature. */
24635 static tree
24636 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24638 tree rettype, kwdparms, optparms;
24639 bool ellipsis = false;
24640 bool is_class_method;
24642 is_class_method = cp_parser_objc_method_type (parser);
24643 rettype = cp_parser_objc_typename (parser);
24644 *attributes = NULL_TREE;
24645 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24646 if (kwdparms == error_mark_node)
24647 return error_mark_node;
24648 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24649 if (optparms == error_mark_node)
24650 return error_mark_node;
24652 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24655 static bool
24656 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24658 tree tattr;
24659 cp_lexer_save_tokens (parser->lexer);
24660 tattr = cp_parser_attributes_opt (parser);
24661 gcc_assert (tattr) ;
24663 /* If the attributes are followed by a method introducer, this is not allowed.
24664 Dump the attributes and flag the situation. */
24665 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24666 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24667 return true;
24669 /* Otherwise, the attributes introduce some interstitial code, possibly so
24670 rewind to allow that check. */
24671 cp_lexer_rollback_tokens (parser->lexer);
24672 return false;
24675 /* Parse an Objective-C method prototype list. */
24677 static void
24678 cp_parser_objc_method_prototype_list (cp_parser* parser)
24680 cp_token *token = cp_lexer_peek_token (parser->lexer);
24682 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24684 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24686 tree attributes, sig;
24687 bool is_class_method;
24688 if (token->type == CPP_PLUS)
24689 is_class_method = true;
24690 else
24691 is_class_method = false;
24692 sig = cp_parser_objc_method_signature (parser, &attributes);
24693 if (sig == error_mark_node)
24695 cp_parser_skip_to_end_of_block_or_statement (parser);
24696 token = cp_lexer_peek_token (parser->lexer);
24697 continue;
24699 objc_add_method_declaration (is_class_method, sig, attributes);
24700 cp_parser_consume_semicolon_at_end_of_statement (parser);
24702 else if (token->keyword == RID_AT_PROPERTY)
24703 cp_parser_objc_at_property_declaration (parser);
24704 else if (token->keyword == RID_ATTRIBUTE
24705 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24706 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24707 OPT_Wattributes,
24708 "prefix attributes are ignored for methods");
24709 else
24710 /* Allow for interspersed non-ObjC++ code. */
24711 cp_parser_objc_interstitial_code (parser);
24713 token = cp_lexer_peek_token (parser->lexer);
24716 if (token->type != CPP_EOF)
24717 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24718 else
24719 cp_parser_error (parser, "expected %<@end%>");
24721 objc_finish_interface ();
24724 /* Parse an Objective-C method definition list. */
24726 static void
24727 cp_parser_objc_method_definition_list (cp_parser* parser)
24729 cp_token *token = cp_lexer_peek_token (parser->lexer);
24731 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24733 tree meth;
24735 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24737 cp_token *ptk;
24738 tree sig, attribute;
24739 bool is_class_method;
24740 if (token->type == CPP_PLUS)
24741 is_class_method = true;
24742 else
24743 is_class_method = false;
24744 push_deferring_access_checks (dk_deferred);
24745 sig = cp_parser_objc_method_signature (parser, &attribute);
24746 if (sig == error_mark_node)
24748 cp_parser_skip_to_end_of_block_or_statement (parser);
24749 token = cp_lexer_peek_token (parser->lexer);
24750 continue;
24752 objc_start_method_definition (is_class_method, sig, attribute,
24753 NULL_TREE);
24755 /* For historical reasons, we accept an optional semicolon. */
24756 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24757 cp_lexer_consume_token (parser->lexer);
24759 ptk = cp_lexer_peek_token (parser->lexer);
24760 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24761 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24763 perform_deferred_access_checks (tf_warning_or_error);
24764 stop_deferring_access_checks ();
24765 meth = cp_parser_function_definition_after_declarator (parser,
24766 false);
24767 pop_deferring_access_checks ();
24768 objc_finish_method_definition (meth);
24771 /* The following case will be removed once @synthesize is
24772 completely implemented. */
24773 else if (token->keyword == RID_AT_PROPERTY)
24774 cp_parser_objc_at_property_declaration (parser);
24775 else if (token->keyword == RID_AT_SYNTHESIZE)
24776 cp_parser_objc_at_synthesize_declaration (parser);
24777 else if (token->keyword == RID_AT_DYNAMIC)
24778 cp_parser_objc_at_dynamic_declaration (parser);
24779 else if (token->keyword == RID_ATTRIBUTE
24780 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24781 warning_at (token->location, OPT_Wattributes,
24782 "prefix attributes are ignored for methods");
24783 else
24784 /* Allow for interspersed non-ObjC++ code. */
24785 cp_parser_objc_interstitial_code (parser);
24787 token = cp_lexer_peek_token (parser->lexer);
24790 if (token->type != CPP_EOF)
24791 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24792 else
24793 cp_parser_error (parser, "expected %<@end%>");
24795 objc_finish_implementation ();
24798 /* Parse Objective-C ivars. */
24800 static void
24801 cp_parser_objc_class_ivars (cp_parser* parser)
24803 cp_token *token = cp_lexer_peek_token (parser->lexer);
24805 if (token->type != CPP_OPEN_BRACE)
24806 return; /* No ivars specified. */
24808 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24809 token = cp_lexer_peek_token (parser->lexer);
24811 while (token->type != CPP_CLOSE_BRACE
24812 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24814 cp_decl_specifier_seq declspecs;
24815 int decl_class_or_enum_p;
24816 tree prefix_attributes;
24818 cp_parser_objc_visibility_spec (parser);
24820 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24821 break;
24823 cp_parser_decl_specifier_seq (parser,
24824 CP_PARSER_FLAGS_OPTIONAL,
24825 &declspecs,
24826 &decl_class_or_enum_p);
24828 /* auto, register, static, extern, mutable. */
24829 if (declspecs.storage_class != sc_none)
24831 cp_parser_error (parser, "invalid type for instance variable");
24832 declspecs.storage_class = sc_none;
24835 /* thread_local. */
24836 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24838 cp_parser_error (parser, "invalid type for instance variable");
24839 declspecs.locations[ds_thread] = 0;
24842 /* typedef. */
24843 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24845 cp_parser_error (parser, "invalid type for instance variable");
24846 declspecs.locations[ds_typedef] = 0;
24849 prefix_attributes = declspecs.attributes;
24850 declspecs.attributes = NULL_TREE;
24852 /* Keep going until we hit the `;' at the end of the
24853 declaration. */
24854 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24856 tree width = NULL_TREE, attributes, first_attribute, decl;
24857 cp_declarator *declarator = NULL;
24858 int ctor_dtor_or_conv_p;
24860 /* Check for a (possibly unnamed) bitfield declaration. */
24861 token = cp_lexer_peek_token (parser->lexer);
24862 if (token->type == CPP_COLON)
24863 goto eat_colon;
24865 if (token->type == CPP_NAME
24866 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24867 == CPP_COLON))
24869 /* Get the name of the bitfield. */
24870 declarator = make_id_declarator (NULL_TREE,
24871 cp_parser_identifier (parser),
24872 sfk_none);
24874 eat_colon:
24875 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24876 /* Get the width of the bitfield. */
24877 width
24878 = cp_parser_constant_expression (parser,
24879 /*allow_non_constant=*/false,
24880 NULL);
24882 else
24884 /* Parse the declarator. */
24885 declarator
24886 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24887 &ctor_dtor_or_conv_p,
24888 /*parenthesized_p=*/NULL,
24889 /*member_p=*/false);
24892 /* Look for attributes that apply to the ivar. */
24893 attributes = cp_parser_attributes_opt (parser);
24894 /* Remember which attributes are prefix attributes and
24895 which are not. */
24896 first_attribute = attributes;
24897 /* Combine the attributes. */
24898 attributes = chainon (prefix_attributes, attributes);
24900 if (width)
24901 /* Create the bitfield declaration. */
24902 decl = grokbitfield (declarator, &declspecs,
24903 width,
24904 attributes);
24905 else
24906 decl = grokfield (declarator, &declspecs,
24907 NULL_TREE, /*init_const_expr_p=*/false,
24908 NULL_TREE, attributes);
24910 /* Add the instance variable. */
24911 if (decl != error_mark_node && decl != NULL_TREE)
24912 objc_add_instance_variable (decl);
24914 /* Reset PREFIX_ATTRIBUTES. */
24915 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24916 attributes = TREE_CHAIN (attributes);
24917 if (attributes)
24918 TREE_CHAIN (attributes) = NULL_TREE;
24920 token = cp_lexer_peek_token (parser->lexer);
24922 if (token->type == CPP_COMMA)
24924 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24925 continue;
24927 break;
24930 cp_parser_consume_semicolon_at_end_of_statement (parser);
24931 token = cp_lexer_peek_token (parser->lexer);
24934 if (token->keyword == RID_AT_END)
24935 cp_parser_error (parser, "expected %<}%>");
24937 /* Do not consume the RID_AT_END, so it will be read again as terminating
24938 the @interface of @implementation. */
24939 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24940 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24942 /* For historical reasons, we accept an optional semicolon. */
24943 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24944 cp_lexer_consume_token (parser->lexer);
24947 /* Parse an Objective-C protocol declaration. */
24949 static void
24950 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24952 tree proto, protorefs;
24953 cp_token *tok;
24955 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24958 tok = cp_lexer_peek_token (parser->lexer);
24959 error_at (tok->location, "identifier expected after %<@protocol%>");
24960 cp_parser_consume_semicolon_at_end_of_statement (parser);
24961 return;
24964 /* See if we have a forward declaration or a definition. */
24965 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24967 /* Try a forward declaration first. */
24968 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24970 while (true)
24972 tree id;
24974 id = cp_parser_identifier (parser);
24975 if (id == error_mark_node)
24976 break;
24978 objc_declare_protocol (id, attributes);
24980 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24981 cp_lexer_consume_token (parser->lexer);
24982 else
24983 break;
24985 cp_parser_consume_semicolon_at_end_of_statement (parser);
24988 /* Ok, we got a full-fledged definition (or at least should). */
24989 else
24991 proto = cp_parser_identifier (parser);
24992 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24993 objc_start_protocol (proto, protorefs, attributes);
24994 cp_parser_objc_method_prototype_list (parser);
24998 /* Parse an Objective-C superclass or category. */
25000 static void
25001 cp_parser_objc_superclass_or_category (cp_parser *parser,
25002 bool iface_p,
25003 tree *super,
25004 tree *categ, bool *is_class_extension)
25006 cp_token *next = cp_lexer_peek_token (parser->lexer);
25008 *super = *categ = NULL_TREE;
25009 *is_class_extension = false;
25010 if (next->type == CPP_COLON)
25012 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25013 *super = cp_parser_identifier (parser);
25015 else if (next->type == CPP_OPEN_PAREN)
25017 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25019 /* If there is no category name, and this is an @interface, we
25020 have a class extension. */
25021 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25023 *categ = NULL_TREE;
25024 *is_class_extension = true;
25026 else
25027 *categ = cp_parser_identifier (parser);
25029 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25033 /* Parse an Objective-C class interface. */
25035 static void
25036 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25038 tree name, super, categ, protos;
25039 bool is_class_extension;
25041 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25042 name = cp_parser_identifier (parser);
25043 if (name == error_mark_node)
25045 /* It's hard to recover because even if valid @interface stuff
25046 is to follow, we can't compile it (or validate it) if we
25047 don't even know which class it refers to. Let's assume this
25048 was a stray '@interface' token in the stream and skip it.
25050 return;
25052 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25053 &is_class_extension);
25054 protos = cp_parser_objc_protocol_refs_opt (parser);
25056 /* We have either a class or a category on our hands. */
25057 if (categ || is_class_extension)
25058 objc_start_category_interface (name, categ, protos, attributes);
25059 else
25061 objc_start_class_interface (name, super, protos, attributes);
25062 /* Handle instance variable declarations, if any. */
25063 cp_parser_objc_class_ivars (parser);
25064 objc_continue_interface ();
25067 cp_parser_objc_method_prototype_list (parser);
25070 /* Parse an Objective-C class implementation. */
25072 static void
25073 cp_parser_objc_class_implementation (cp_parser* parser)
25075 tree name, super, categ;
25076 bool is_class_extension;
25078 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25079 name = cp_parser_identifier (parser);
25080 if (name == error_mark_node)
25082 /* It's hard to recover because even if valid @implementation
25083 stuff is to follow, we can't compile it (or validate it) if
25084 we don't even know which class it refers to. Let's assume
25085 this was a stray '@implementation' token in the stream and
25086 skip it.
25088 return;
25090 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25091 &is_class_extension);
25093 /* We have either a class or a category on our hands. */
25094 if (categ)
25095 objc_start_category_implementation (name, categ);
25096 else
25098 objc_start_class_implementation (name, super);
25099 /* Handle instance variable declarations, if any. */
25100 cp_parser_objc_class_ivars (parser);
25101 objc_continue_implementation ();
25104 cp_parser_objc_method_definition_list (parser);
25107 /* Consume the @end token and finish off the implementation. */
25109 static void
25110 cp_parser_objc_end_implementation (cp_parser* parser)
25112 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25113 objc_finish_implementation ();
25116 /* Parse an Objective-C declaration. */
25118 static void
25119 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25121 /* Try to figure out what kind of declaration is present. */
25122 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25124 if (attributes)
25125 switch (kwd->keyword)
25127 case RID_AT_ALIAS:
25128 case RID_AT_CLASS:
25129 case RID_AT_END:
25130 error_at (kwd->location, "attributes may not be specified before"
25131 " the %<@%D%> Objective-C++ keyword",
25132 kwd->u.value);
25133 attributes = NULL;
25134 break;
25135 case RID_AT_IMPLEMENTATION:
25136 warning_at (kwd->location, OPT_Wattributes,
25137 "prefix attributes are ignored before %<@%D%>",
25138 kwd->u.value);
25139 attributes = NULL;
25140 default:
25141 break;
25144 switch (kwd->keyword)
25146 case RID_AT_ALIAS:
25147 cp_parser_objc_alias_declaration (parser);
25148 break;
25149 case RID_AT_CLASS:
25150 cp_parser_objc_class_declaration (parser);
25151 break;
25152 case RID_AT_PROTOCOL:
25153 cp_parser_objc_protocol_declaration (parser, attributes);
25154 break;
25155 case RID_AT_INTERFACE:
25156 cp_parser_objc_class_interface (parser, attributes);
25157 break;
25158 case RID_AT_IMPLEMENTATION:
25159 cp_parser_objc_class_implementation (parser);
25160 break;
25161 case RID_AT_END:
25162 cp_parser_objc_end_implementation (parser);
25163 break;
25164 default:
25165 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25166 kwd->u.value);
25167 cp_parser_skip_to_end_of_block_or_statement (parser);
25171 /* Parse an Objective-C try-catch-finally statement.
25173 objc-try-catch-finally-stmt:
25174 @try compound-statement objc-catch-clause-seq [opt]
25175 objc-finally-clause [opt]
25177 objc-catch-clause-seq:
25178 objc-catch-clause objc-catch-clause-seq [opt]
25180 objc-catch-clause:
25181 @catch ( objc-exception-declaration ) compound-statement
25183 objc-finally-clause:
25184 @finally compound-statement
25186 objc-exception-declaration:
25187 parameter-declaration
25188 '...'
25190 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25192 Returns NULL_TREE.
25194 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25195 for C. Keep them in sync. */
25197 static tree
25198 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25200 location_t location;
25201 tree stmt;
25203 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25204 location = cp_lexer_peek_token (parser->lexer)->location;
25205 objc_maybe_warn_exceptions (location);
25206 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25207 node, lest it get absorbed into the surrounding block. */
25208 stmt = push_stmt_list ();
25209 cp_parser_compound_statement (parser, NULL, false, false);
25210 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25212 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25214 cp_parameter_declarator *parm;
25215 tree parameter_declaration = error_mark_node;
25216 bool seen_open_paren = false;
25218 cp_lexer_consume_token (parser->lexer);
25219 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25220 seen_open_paren = true;
25221 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25223 /* We have "@catch (...)" (where the '...' are literally
25224 what is in the code). Skip the '...'.
25225 parameter_declaration is set to NULL_TREE, and
25226 objc_being_catch_clauses() knows that that means
25227 '...'. */
25228 cp_lexer_consume_token (parser->lexer);
25229 parameter_declaration = NULL_TREE;
25231 else
25233 /* We have "@catch (NSException *exception)" or something
25234 like that. Parse the parameter declaration. */
25235 parm = cp_parser_parameter_declaration (parser, false, NULL);
25236 if (parm == NULL)
25237 parameter_declaration = error_mark_node;
25238 else
25239 parameter_declaration = grokdeclarator (parm->declarator,
25240 &parm->decl_specifiers,
25241 PARM, /*initialized=*/0,
25242 /*attrlist=*/NULL);
25244 if (seen_open_paren)
25245 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25246 else
25248 /* If there was no open parenthesis, we are recovering from
25249 an error, and we are trying to figure out what mistake
25250 the user has made. */
25252 /* If there is an immediate closing parenthesis, the user
25253 probably forgot the opening one (ie, they typed "@catch
25254 NSException *e)". Parse the closing parenthesis and keep
25255 going. */
25256 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25257 cp_lexer_consume_token (parser->lexer);
25259 /* If these is no immediate closing parenthesis, the user
25260 probably doesn't know that parenthesis are required at
25261 all (ie, they typed "@catch NSException *e"). So, just
25262 forget about the closing parenthesis and keep going. */
25264 objc_begin_catch_clause (parameter_declaration);
25265 cp_parser_compound_statement (parser, NULL, false, false);
25266 objc_finish_catch_clause ();
25268 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25270 cp_lexer_consume_token (parser->lexer);
25271 location = cp_lexer_peek_token (parser->lexer)->location;
25272 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25273 node, lest it get absorbed into the surrounding block. */
25274 stmt = push_stmt_list ();
25275 cp_parser_compound_statement (parser, NULL, false, false);
25276 objc_build_finally_clause (location, pop_stmt_list (stmt));
25279 return objc_finish_try_stmt ();
25282 /* Parse an Objective-C synchronized statement.
25284 objc-synchronized-stmt:
25285 @synchronized ( expression ) compound-statement
25287 Returns NULL_TREE. */
25289 static tree
25290 cp_parser_objc_synchronized_statement (cp_parser *parser)
25292 location_t location;
25293 tree lock, stmt;
25295 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25297 location = cp_lexer_peek_token (parser->lexer)->location;
25298 objc_maybe_warn_exceptions (location);
25299 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25300 lock = cp_parser_expression (parser, false, NULL);
25301 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25303 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25304 node, lest it get absorbed into the surrounding block. */
25305 stmt = push_stmt_list ();
25306 cp_parser_compound_statement (parser, NULL, false, false);
25308 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25311 /* Parse an Objective-C throw statement.
25313 objc-throw-stmt:
25314 @throw assignment-expression [opt] ;
25316 Returns a constructed '@throw' statement. */
25318 static tree
25319 cp_parser_objc_throw_statement (cp_parser *parser)
25321 tree expr = NULL_TREE;
25322 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25324 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25327 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25329 cp_parser_consume_semicolon_at_end_of_statement (parser);
25331 return objc_build_throw_stmt (loc, expr);
25334 /* Parse an Objective-C statement. */
25336 static tree
25337 cp_parser_objc_statement (cp_parser * parser)
25339 /* Try to figure out what kind of declaration is present. */
25340 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25342 switch (kwd->keyword)
25344 case RID_AT_TRY:
25345 return cp_parser_objc_try_catch_finally_statement (parser);
25346 case RID_AT_SYNCHRONIZED:
25347 return cp_parser_objc_synchronized_statement (parser);
25348 case RID_AT_THROW:
25349 return cp_parser_objc_throw_statement (parser);
25350 default:
25351 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25352 kwd->u.value);
25353 cp_parser_skip_to_end_of_block_or_statement (parser);
25356 return error_mark_node;
25359 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25360 look ahead to see if an objc keyword follows the attributes. This
25361 is to detect the use of prefix attributes on ObjC @interface and
25362 @protocol. */
25364 static bool
25365 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25367 cp_lexer_save_tokens (parser->lexer);
25368 *attrib = cp_parser_attributes_opt (parser);
25369 gcc_assert (*attrib);
25370 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25372 cp_lexer_commit_tokens (parser->lexer);
25373 return true;
25375 cp_lexer_rollback_tokens (parser->lexer);
25376 return false;
25379 /* This routine is a minimal replacement for
25380 c_parser_struct_declaration () used when parsing the list of
25381 types/names or ObjC++ properties. For example, when parsing the
25382 code
25384 @property (readonly) int a, b, c;
25386 this function is responsible for parsing "int a, int b, int c" and
25387 returning the declarations as CHAIN of DECLs.
25389 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25390 similar parsing. */
25391 static tree
25392 cp_parser_objc_struct_declaration (cp_parser *parser)
25394 tree decls = NULL_TREE;
25395 cp_decl_specifier_seq declspecs;
25396 int decl_class_or_enum_p;
25397 tree prefix_attributes;
25399 cp_parser_decl_specifier_seq (parser,
25400 CP_PARSER_FLAGS_NONE,
25401 &declspecs,
25402 &decl_class_or_enum_p);
25404 if (declspecs.type == error_mark_node)
25405 return error_mark_node;
25407 /* auto, register, static, extern, mutable. */
25408 if (declspecs.storage_class != sc_none)
25410 cp_parser_error (parser, "invalid type for property");
25411 declspecs.storage_class = sc_none;
25414 /* thread_local. */
25415 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25417 cp_parser_error (parser, "invalid type for property");
25418 declspecs.locations[ds_thread] = 0;
25421 /* typedef. */
25422 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25424 cp_parser_error (parser, "invalid type for property");
25425 declspecs.locations[ds_typedef] = 0;
25428 prefix_attributes = declspecs.attributes;
25429 declspecs.attributes = NULL_TREE;
25431 /* Keep going until we hit the `;' at the end of the declaration. */
25432 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25434 tree attributes, first_attribute, decl;
25435 cp_declarator *declarator;
25436 cp_token *token;
25438 /* Parse the declarator. */
25439 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25440 NULL, NULL, false);
25442 /* Look for attributes that apply to the ivar. */
25443 attributes = cp_parser_attributes_opt (parser);
25444 /* Remember which attributes are prefix attributes and
25445 which are not. */
25446 first_attribute = attributes;
25447 /* Combine the attributes. */
25448 attributes = chainon (prefix_attributes, attributes);
25450 decl = grokfield (declarator, &declspecs,
25451 NULL_TREE, /*init_const_expr_p=*/false,
25452 NULL_TREE, attributes);
25454 if (decl == error_mark_node || decl == NULL_TREE)
25455 return error_mark_node;
25457 /* Reset PREFIX_ATTRIBUTES. */
25458 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25459 attributes = TREE_CHAIN (attributes);
25460 if (attributes)
25461 TREE_CHAIN (attributes) = NULL_TREE;
25463 DECL_CHAIN (decl) = decls;
25464 decls = decl;
25466 token = cp_lexer_peek_token (parser->lexer);
25467 if (token->type == CPP_COMMA)
25469 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25470 continue;
25472 else
25473 break;
25475 return decls;
25478 /* Parse an Objective-C @property declaration. The syntax is:
25480 objc-property-declaration:
25481 '@property' objc-property-attributes[opt] struct-declaration ;
25483 objc-property-attributes:
25484 '(' objc-property-attribute-list ')'
25486 objc-property-attribute-list:
25487 objc-property-attribute
25488 objc-property-attribute-list, objc-property-attribute
25490 objc-property-attribute
25491 'getter' = identifier
25492 'setter' = identifier
25493 'readonly'
25494 'readwrite'
25495 'assign'
25496 'retain'
25497 'copy'
25498 'nonatomic'
25500 For example:
25501 @property NSString *name;
25502 @property (readonly) id object;
25503 @property (retain, nonatomic, getter=getTheName) id name;
25504 @property int a, b, c;
25506 PS: This function is identical to
25507 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25508 static void
25509 cp_parser_objc_at_property_declaration (cp_parser *parser)
25511 /* The following variables hold the attributes of the properties as
25512 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25513 seen. When we see an attribute, we set them to 'true' (if they
25514 are boolean properties) or to the identifier (if they have an
25515 argument, ie, for getter and setter). Note that here we only
25516 parse the list of attributes, check the syntax and accumulate the
25517 attributes that we find. objc_add_property_declaration() will
25518 then process the information. */
25519 bool property_assign = false;
25520 bool property_copy = false;
25521 tree property_getter_ident = NULL_TREE;
25522 bool property_nonatomic = false;
25523 bool property_readonly = false;
25524 bool property_readwrite = false;
25525 bool property_retain = false;
25526 tree property_setter_ident = NULL_TREE;
25528 /* 'properties' is the list of properties that we read. Usually a
25529 single one, but maybe more (eg, in "@property int a, b, c;" there
25530 are three). */
25531 tree properties;
25532 location_t loc;
25534 loc = cp_lexer_peek_token (parser->lexer)->location;
25536 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25538 /* Parse the optional attribute list... */
25539 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25541 /* Eat the '('. */
25542 cp_lexer_consume_token (parser->lexer);
25544 while (true)
25546 bool syntax_error = false;
25547 cp_token *token = cp_lexer_peek_token (parser->lexer);
25548 enum rid keyword;
25550 if (token->type != CPP_NAME)
25552 cp_parser_error (parser, "expected identifier");
25553 break;
25555 keyword = C_RID_CODE (token->u.value);
25556 cp_lexer_consume_token (parser->lexer);
25557 switch (keyword)
25559 case RID_ASSIGN: property_assign = true; break;
25560 case RID_COPY: property_copy = true; break;
25561 case RID_NONATOMIC: property_nonatomic = true; break;
25562 case RID_READONLY: property_readonly = true; break;
25563 case RID_READWRITE: property_readwrite = true; break;
25564 case RID_RETAIN: property_retain = true; break;
25566 case RID_GETTER:
25567 case RID_SETTER:
25568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25570 if (keyword == RID_GETTER)
25571 cp_parser_error (parser,
25572 "missing %<=%> (after %<getter%> attribute)");
25573 else
25574 cp_parser_error (parser,
25575 "missing %<=%> (after %<setter%> attribute)");
25576 syntax_error = true;
25577 break;
25579 cp_lexer_consume_token (parser->lexer); /* eat the = */
25580 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25582 cp_parser_error (parser, "expected identifier");
25583 syntax_error = true;
25584 break;
25586 if (keyword == RID_SETTER)
25588 if (property_setter_ident != NULL_TREE)
25590 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25591 cp_lexer_consume_token (parser->lexer);
25593 else
25594 property_setter_ident = cp_parser_objc_selector (parser);
25595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25596 cp_parser_error (parser, "setter name must terminate with %<:%>");
25597 else
25598 cp_lexer_consume_token (parser->lexer);
25600 else
25602 if (property_getter_ident != NULL_TREE)
25604 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25605 cp_lexer_consume_token (parser->lexer);
25607 else
25608 property_getter_ident = cp_parser_objc_selector (parser);
25610 break;
25611 default:
25612 cp_parser_error (parser, "unknown property attribute");
25613 syntax_error = true;
25614 break;
25617 if (syntax_error)
25618 break;
25620 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25621 cp_lexer_consume_token (parser->lexer);
25622 else
25623 break;
25626 /* FIXME: "@property (setter, assign);" will generate a spurious
25627 "error: expected ‘)’ before ‘,’ token". This is because
25628 cp_parser_require, unlike the C counterpart, will produce an
25629 error even if we are in error recovery. */
25630 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25632 cp_parser_skip_to_closing_parenthesis (parser,
25633 /*recovering=*/true,
25634 /*or_comma=*/false,
25635 /*consume_paren=*/true);
25639 /* ... and the property declaration(s). */
25640 properties = cp_parser_objc_struct_declaration (parser);
25642 if (properties == error_mark_node)
25644 cp_parser_skip_to_end_of_statement (parser);
25645 /* If the next token is now a `;', consume it. */
25646 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25647 cp_lexer_consume_token (parser->lexer);
25648 return;
25651 if (properties == NULL_TREE)
25652 cp_parser_error (parser, "expected identifier");
25653 else
25655 /* Comma-separated properties are chained together in
25656 reverse order; add them one by one. */
25657 properties = nreverse (properties);
25659 for (; properties; properties = TREE_CHAIN (properties))
25660 objc_add_property_declaration (loc, copy_node (properties),
25661 property_readonly, property_readwrite,
25662 property_assign, property_retain,
25663 property_copy, property_nonatomic,
25664 property_getter_ident, property_setter_ident);
25667 cp_parser_consume_semicolon_at_end_of_statement (parser);
25670 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25672 objc-synthesize-declaration:
25673 @synthesize objc-synthesize-identifier-list ;
25675 objc-synthesize-identifier-list:
25676 objc-synthesize-identifier
25677 objc-synthesize-identifier-list, objc-synthesize-identifier
25679 objc-synthesize-identifier
25680 identifier
25681 identifier = identifier
25683 For example:
25684 @synthesize MyProperty;
25685 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25687 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25688 for C. Keep them in sync.
25690 static void
25691 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25693 tree list = NULL_TREE;
25694 location_t loc;
25695 loc = cp_lexer_peek_token (parser->lexer)->location;
25697 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25698 while (true)
25700 tree property, ivar;
25701 property = cp_parser_identifier (parser);
25702 if (property == error_mark_node)
25704 cp_parser_consume_semicolon_at_end_of_statement (parser);
25705 return;
25707 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25709 cp_lexer_consume_token (parser->lexer);
25710 ivar = cp_parser_identifier (parser);
25711 if (ivar == error_mark_node)
25713 cp_parser_consume_semicolon_at_end_of_statement (parser);
25714 return;
25717 else
25718 ivar = NULL_TREE;
25719 list = chainon (list, build_tree_list (ivar, property));
25720 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25721 cp_lexer_consume_token (parser->lexer);
25722 else
25723 break;
25725 cp_parser_consume_semicolon_at_end_of_statement (parser);
25726 objc_add_synthesize_declaration (loc, list);
25729 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25731 objc-dynamic-declaration:
25732 @dynamic identifier-list ;
25734 For example:
25735 @dynamic MyProperty;
25736 @dynamic MyProperty, AnotherProperty;
25738 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25739 for C. Keep them in sync.
25741 static void
25742 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25744 tree list = NULL_TREE;
25745 location_t loc;
25746 loc = cp_lexer_peek_token (parser->lexer)->location;
25748 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25749 while (true)
25751 tree property;
25752 property = cp_parser_identifier (parser);
25753 if (property == error_mark_node)
25755 cp_parser_consume_semicolon_at_end_of_statement (parser);
25756 return;
25758 list = chainon (list, build_tree_list (NULL, property));
25759 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25760 cp_lexer_consume_token (parser->lexer);
25761 else
25762 break;
25764 cp_parser_consume_semicolon_at_end_of_statement (parser);
25765 objc_add_dynamic_declaration (loc, list);
25769 /* OpenMP 2.5 parsing routines. */
25771 /* Returns name of the next clause.
25772 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25773 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25774 returned and the token is consumed. */
25776 static pragma_omp_clause
25777 cp_parser_omp_clause_name (cp_parser *parser)
25779 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25782 result = PRAGMA_OMP_CLAUSE_IF;
25783 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25784 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25785 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25786 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25787 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25789 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25790 const char *p = IDENTIFIER_POINTER (id);
25792 switch (p[0])
25794 case 'c':
25795 if (!strcmp ("collapse", p))
25796 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25797 else if (!strcmp ("copyin", p))
25798 result = PRAGMA_OMP_CLAUSE_COPYIN;
25799 else if (!strcmp ("copyprivate", p))
25800 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25801 break;
25802 case 'f':
25803 if (!strcmp ("final", p))
25804 result = PRAGMA_OMP_CLAUSE_FINAL;
25805 else if (!strcmp ("firstprivate", p))
25806 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25807 break;
25808 case 'l':
25809 if (!strcmp ("lastprivate", p))
25810 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25811 break;
25812 case 'm':
25813 if (!strcmp ("mergeable", p))
25814 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25815 break;
25816 case 'n':
25817 if (!strcmp ("nowait", p))
25818 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25819 else if (!strcmp ("num_threads", p))
25820 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25821 break;
25822 case 'o':
25823 if (!strcmp ("ordered", p))
25824 result = PRAGMA_OMP_CLAUSE_ORDERED;
25825 break;
25826 case 'r':
25827 if (!strcmp ("reduction", p))
25828 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25829 break;
25830 case 's':
25831 if (!strcmp ("schedule", p))
25832 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25833 else if (!strcmp ("shared", p))
25834 result = PRAGMA_OMP_CLAUSE_SHARED;
25835 break;
25836 case 'u':
25837 if (!strcmp ("untied", p))
25838 result = PRAGMA_OMP_CLAUSE_UNTIED;
25839 break;
25843 if (result != PRAGMA_OMP_CLAUSE_NONE)
25844 cp_lexer_consume_token (parser->lexer);
25846 return result;
25849 /* Validate that a clause of the given type does not already exist. */
25851 static void
25852 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25853 const char *name, location_t location)
25855 tree c;
25857 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25858 if (OMP_CLAUSE_CODE (c) == code)
25860 error_at (location, "too many %qs clauses", name);
25861 break;
25865 /* OpenMP 2.5:
25866 variable-list:
25867 identifier
25868 variable-list , identifier
25870 In addition, we match a closing parenthesis. An opening parenthesis
25871 will have been consumed by the caller.
25873 If KIND is nonzero, create the appropriate node and install the decl
25874 in OMP_CLAUSE_DECL and add the node to the head of the list.
25876 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25877 return the list created. */
25879 static tree
25880 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25881 tree list)
25883 cp_token *token;
25884 while (1)
25886 tree name, decl;
25888 token = cp_lexer_peek_token (parser->lexer);
25889 name = cp_parser_id_expression (parser, /*template_p=*/false,
25890 /*check_dependency_p=*/true,
25891 /*template_p=*/NULL,
25892 /*declarator_p=*/false,
25893 /*optional_p=*/false);
25894 if (name == error_mark_node)
25895 goto skip_comma;
25897 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25898 if (decl == error_mark_node)
25899 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25900 token->location);
25901 else if (kind != 0)
25903 tree u = build_omp_clause (token->location, kind);
25904 OMP_CLAUSE_DECL (u) = decl;
25905 OMP_CLAUSE_CHAIN (u) = list;
25906 list = u;
25908 else
25909 list = tree_cons (decl, NULL_TREE, list);
25911 get_comma:
25912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25913 break;
25914 cp_lexer_consume_token (parser->lexer);
25917 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25919 int ending;
25921 /* Try to resync to an unnested comma. Copied from
25922 cp_parser_parenthesized_expression_list. */
25923 skip_comma:
25924 ending = cp_parser_skip_to_closing_parenthesis (parser,
25925 /*recovering=*/true,
25926 /*or_comma=*/true,
25927 /*consume_paren=*/true);
25928 if (ending < 0)
25929 goto get_comma;
25932 return list;
25935 /* Similarly, but expect leading and trailing parenthesis. This is a very
25936 common case for omp clauses. */
25938 static tree
25939 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25941 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25942 return cp_parser_omp_var_list_no_open (parser, kind, list);
25943 return list;
25946 /* OpenMP 3.0:
25947 collapse ( constant-expression ) */
25949 static tree
25950 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25952 tree c, num;
25953 location_t loc;
25954 HOST_WIDE_INT n;
25956 loc = cp_lexer_peek_token (parser->lexer)->location;
25957 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25958 return list;
25960 num = cp_parser_constant_expression (parser, false, NULL);
25962 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25963 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25964 /*or_comma=*/false,
25965 /*consume_paren=*/true);
25967 if (num == error_mark_node)
25968 return list;
25969 num = fold_non_dependent_expr (num);
25970 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25971 || !host_integerp (num, 0)
25972 || (n = tree_low_cst (num, 0)) <= 0
25973 || (int) n != n)
25975 error_at (loc, "collapse argument needs positive constant integer expression");
25976 return list;
25979 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25980 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25981 OMP_CLAUSE_CHAIN (c) = list;
25982 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25984 return c;
25987 /* OpenMP 2.5:
25988 default ( shared | none ) */
25990 static tree
25991 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25993 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25994 tree c;
25996 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25997 return list;
25998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26000 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26001 const char *p = IDENTIFIER_POINTER (id);
26003 switch (p[0])
26005 case 'n':
26006 if (strcmp ("none", p) != 0)
26007 goto invalid_kind;
26008 kind = OMP_CLAUSE_DEFAULT_NONE;
26009 break;
26011 case 's':
26012 if (strcmp ("shared", p) != 0)
26013 goto invalid_kind;
26014 kind = OMP_CLAUSE_DEFAULT_SHARED;
26015 break;
26017 default:
26018 goto invalid_kind;
26021 cp_lexer_consume_token (parser->lexer);
26023 else
26025 invalid_kind:
26026 cp_parser_error (parser, "expected %<none%> or %<shared%>");
26029 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26030 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26031 /*or_comma=*/false,
26032 /*consume_paren=*/true);
26034 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26035 return list;
26037 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26038 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26039 OMP_CLAUSE_CHAIN (c) = list;
26040 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26042 return c;
26045 /* OpenMP 3.1:
26046 final ( expression ) */
26048 static tree
26049 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26051 tree t, c;
26053 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26054 return list;
26056 t = cp_parser_condition (parser);
26058 if (t == error_mark_node
26059 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26060 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26061 /*or_comma=*/false,
26062 /*consume_paren=*/true);
26064 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26066 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26067 OMP_CLAUSE_FINAL_EXPR (c) = t;
26068 OMP_CLAUSE_CHAIN (c) = list;
26070 return c;
26073 /* OpenMP 2.5:
26074 if ( expression ) */
26076 static tree
26077 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26079 tree t, c;
26081 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26082 return list;
26084 t = cp_parser_condition (parser);
26086 if (t == error_mark_node
26087 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26088 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26089 /*or_comma=*/false,
26090 /*consume_paren=*/true);
26092 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26094 c = build_omp_clause (location, OMP_CLAUSE_IF);
26095 OMP_CLAUSE_IF_EXPR (c) = t;
26096 OMP_CLAUSE_CHAIN (c) = list;
26098 return c;
26101 /* OpenMP 3.1:
26102 mergeable */
26104 static tree
26105 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26106 tree list, location_t location)
26108 tree c;
26110 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26111 location);
26113 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
26114 OMP_CLAUSE_CHAIN (c) = list;
26115 return c;
26118 /* OpenMP 2.5:
26119 nowait */
26121 static tree
26122 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
26123 tree list, location_t location)
26125 tree c;
26127 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
26129 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
26130 OMP_CLAUSE_CHAIN (c) = list;
26131 return c;
26134 /* OpenMP 2.5:
26135 num_threads ( expression ) */
26137 static tree
26138 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
26139 location_t location)
26141 tree t, c;
26143 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26144 return list;
26146 t = cp_parser_expression (parser, false, NULL);
26148 if (t == error_mark_node
26149 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26151 /*or_comma=*/false,
26152 /*consume_paren=*/true);
26154 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
26155 "num_threads", location);
26157 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
26158 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
26159 OMP_CLAUSE_CHAIN (c) = list;
26161 return c;
26164 /* OpenMP 2.5:
26165 ordered */
26167 static tree
26168 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
26169 tree list, location_t location)
26171 tree c;
26173 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
26174 "ordered", location);
26176 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
26177 OMP_CLAUSE_CHAIN (c) = list;
26178 return c;
26181 /* OpenMP 2.5:
26182 reduction ( reduction-operator : variable-list )
26184 reduction-operator:
26185 One of: + * - & ^ | && ||
26187 OpenMP 3.1:
26189 reduction-operator:
26190 One of: + * - & ^ | && || min max */
26192 static tree
26193 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26195 enum tree_code code;
26196 tree nlist, c;
26198 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26199 return list;
26201 switch (cp_lexer_peek_token (parser->lexer)->type)
26203 case CPP_PLUS:
26204 code = PLUS_EXPR;
26205 break;
26206 case CPP_MULT:
26207 code = MULT_EXPR;
26208 break;
26209 case CPP_MINUS:
26210 code = MINUS_EXPR;
26211 break;
26212 case CPP_AND:
26213 code = BIT_AND_EXPR;
26214 break;
26215 case CPP_XOR:
26216 code = BIT_XOR_EXPR;
26217 break;
26218 case CPP_OR:
26219 code = BIT_IOR_EXPR;
26220 break;
26221 case CPP_AND_AND:
26222 code = TRUTH_ANDIF_EXPR;
26223 break;
26224 case CPP_OR_OR:
26225 code = TRUTH_ORIF_EXPR;
26226 break;
26227 case CPP_NAME:
26229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26230 const char *p = IDENTIFIER_POINTER (id);
26232 if (strcmp (p, "min") == 0)
26234 code = MIN_EXPR;
26235 break;
26237 if (strcmp (p, "max") == 0)
26239 code = MAX_EXPR;
26240 break;
26243 /* FALLTHROUGH */
26244 default:
26245 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26246 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26247 resync_fail:
26248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26249 /*or_comma=*/false,
26250 /*consume_paren=*/true);
26251 return list;
26253 cp_lexer_consume_token (parser->lexer);
26255 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26256 goto resync_fail;
26258 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26259 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26260 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26262 return nlist;
26265 /* OpenMP 2.5:
26266 schedule ( schedule-kind )
26267 schedule ( schedule-kind , expression )
26269 schedule-kind:
26270 static | dynamic | guided | runtime | auto */
26272 static tree
26273 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26275 tree c, t;
26277 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26278 return list;
26280 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26282 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26284 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26285 const char *p = IDENTIFIER_POINTER (id);
26287 switch (p[0])
26289 case 'd':
26290 if (strcmp ("dynamic", p) != 0)
26291 goto invalid_kind;
26292 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26293 break;
26295 case 'g':
26296 if (strcmp ("guided", p) != 0)
26297 goto invalid_kind;
26298 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26299 break;
26301 case 'r':
26302 if (strcmp ("runtime", p) != 0)
26303 goto invalid_kind;
26304 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26305 break;
26307 default:
26308 goto invalid_kind;
26311 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26312 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26313 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26314 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26315 else
26316 goto invalid_kind;
26317 cp_lexer_consume_token (parser->lexer);
26319 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26321 cp_token *token;
26322 cp_lexer_consume_token (parser->lexer);
26324 token = cp_lexer_peek_token (parser->lexer);
26325 t = cp_parser_assignment_expression (parser, false, NULL);
26327 if (t == error_mark_node)
26328 goto resync_fail;
26329 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26330 error_at (token->location, "schedule %<runtime%> does not take "
26331 "a %<chunk_size%> parameter");
26332 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26333 error_at (token->location, "schedule %<auto%> does not take "
26334 "a %<chunk_size%> parameter");
26335 else
26336 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26338 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26339 goto resync_fail;
26341 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26342 goto resync_fail;
26344 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26345 OMP_CLAUSE_CHAIN (c) = list;
26346 return c;
26348 invalid_kind:
26349 cp_parser_error (parser, "invalid schedule kind");
26350 resync_fail:
26351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26352 /*or_comma=*/false,
26353 /*consume_paren=*/true);
26354 return list;
26357 /* OpenMP 3.0:
26358 untied */
26360 static tree
26361 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26362 tree list, location_t location)
26364 tree c;
26366 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26368 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26369 OMP_CLAUSE_CHAIN (c) = list;
26370 return c;
26373 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26374 is a bitmask in MASK. Return the list of clauses found; the result
26375 of clause default goes in *pdefault. */
26377 static tree
26378 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26379 const char *where, cp_token *pragma_tok)
26381 tree clauses = NULL;
26382 bool first = true;
26383 cp_token *token = NULL;
26385 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26387 pragma_omp_clause c_kind;
26388 const char *c_name;
26389 tree prev = clauses;
26391 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26392 cp_lexer_consume_token (parser->lexer);
26394 token = cp_lexer_peek_token (parser->lexer);
26395 c_kind = cp_parser_omp_clause_name (parser);
26396 first = false;
26398 switch (c_kind)
26400 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26401 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26402 token->location);
26403 c_name = "collapse";
26404 break;
26405 case PRAGMA_OMP_CLAUSE_COPYIN:
26406 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26407 c_name = "copyin";
26408 break;
26409 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26410 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26411 clauses);
26412 c_name = "copyprivate";
26413 break;
26414 case PRAGMA_OMP_CLAUSE_DEFAULT:
26415 clauses = cp_parser_omp_clause_default (parser, clauses,
26416 token->location);
26417 c_name = "default";
26418 break;
26419 case PRAGMA_OMP_CLAUSE_FINAL:
26420 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26421 c_name = "final";
26422 break;
26423 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26424 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26425 clauses);
26426 c_name = "firstprivate";
26427 break;
26428 case PRAGMA_OMP_CLAUSE_IF:
26429 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26430 c_name = "if";
26431 break;
26432 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26433 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26434 clauses);
26435 c_name = "lastprivate";
26436 break;
26437 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26438 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26439 token->location);
26440 c_name = "mergeable";
26441 break;
26442 case PRAGMA_OMP_CLAUSE_NOWAIT:
26443 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26444 c_name = "nowait";
26445 break;
26446 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26447 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26448 token->location);
26449 c_name = "num_threads";
26450 break;
26451 case PRAGMA_OMP_CLAUSE_ORDERED:
26452 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26453 token->location);
26454 c_name = "ordered";
26455 break;
26456 case PRAGMA_OMP_CLAUSE_PRIVATE:
26457 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26458 clauses);
26459 c_name = "private";
26460 break;
26461 case PRAGMA_OMP_CLAUSE_REDUCTION:
26462 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26463 c_name = "reduction";
26464 break;
26465 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26466 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26467 token->location);
26468 c_name = "schedule";
26469 break;
26470 case PRAGMA_OMP_CLAUSE_SHARED:
26471 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26472 clauses);
26473 c_name = "shared";
26474 break;
26475 case PRAGMA_OMP_CLAUSE_UNTIED:
26476 clauses = cp_parser_omp_clause_untied (parser, clauses,
26477 token->location);
26478 c_name = "nowait";
26479 break;
26480 default:
26481 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26482 goto saw_error;
26485 if (((mask >> c_kind) & 1) == 0)
26487 /* Remove the invalid clause(s) from the list to avoid
26488 confusing the rest of the compiler. */
26489 clauses = prev;
26490 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26493 saw_error:
26494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26495 return finish_omp_clauses (clauses);
26498 /* OpenMP 2.5:
26499 structured-block:
26500 statement
26502 In practice, we're also interested in adding the statement to an
26503 outer node. So it is convenient if we work around the fact that
26504 cp_parser_statement calls add_stmt. */
26506 static unsigned
26507 cp_parser_begin_omp_structured_block (cp_parser *parser)
26509 unsigned save = parser->in_statement;
26511 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26512 This preserves the "not within loop or switch" style error messages
26513 for nonsense cases like
26514 void foo() {
26515 #pragma omp single
26516 break;
26519 if (parser->in_statement)
26520 parser->in_statement = IN_OMP_BLOCK;
26522 return save;
26525 static void
26526 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26528 parser->in_statement = save;
26531 static tree
26532 cp_parser_omp_structured_block (cp_parser *parser)
26534 tree stmt = begin_omp_structured_block ();
26535 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26537 cp_parser_statement (parser, NULL_TREE, false, NULL);
26539 cp_parser_end_omp_structured_block (parser, save);
26540 return finish_omp_structured_block (stmt);
26543 /* OpenMP 2.5:
26544 # pragma omp atomic new-line
26545 expression-stmt
26547 expression-stmt:
26548 x binop= expr | x++ | ++x | x-- | --x
26549 binop:
26550 +, *, -, /, &, ^, |, <<, >>
26552 where x is an lvalue expression with scalar type.
26554 OpenMP 3.1:
26555 # pragma omp atomic new-line
26556 update-stmt
26558 # pragma omp atomic read new-line
26559 read-stmt
26561 # pragma omp atomic write new-line
26562 write-stmt
26564 # pragma omp atomic update new-line
26565 update-stmt
26567 # pragma omp atomic capture new-line
26568 capture-stmt
26570 # pragma omp atomic capture new-line
26571 capture-block
26573 read-stmt:
26574 v = x
26575 write-stmt:
26576 x = expr
26577 update-stmt:
26578 expression-stmt | x = x binop expr
26579 capture-stmt:
26580 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26581 capture-block:
26582 { v = x; update-stmt; } | { update-stmt; v = x; }
26584 where x and v are lvalue expressions with scalar type. */
26586 static void
26587 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26589 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26590 tree rhs1 = NULL_TREE, orig_lhs;
26591 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26592 bool structured_block = false;
26594 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26597 const char *p = IDENTIFIER_POINTER (id);
26599 if (!strcmp (p, "read"))
26600 code = OMP_ATOMIC_READ;
26601 else if (!strcmp (p, "write"))
26602 code = NOP_EXPR;
26603 else if (!strcmp (p, "update"))
26604 code = OMP_ATOMIC;
26605 else if (!strcmp (p, "capture"))
26606 code = OMP_ATOMIC_CAPTURE_NEW;
26607 else
26608 p = NULL;
26609 if (p)
26610 cp_lexer_consume_token (parser->lexer);
26612 cp_parser_require_pragma_eol (parser, pragma_tok);
26614 switch (code)
26616 case OMP_ATOMIC_READ:
26617 case NOP_EXPR: /* atomic write */
26618 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26619 /*cast_p=*/false, NULL);
26620 if (v == error_mark_node)
26621 goto saw_error;
26622 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26623 goto saw_error;
26624 if (code == NOP_EXPR)
26625 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26626 else
26627 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26628 /*cast_p=*/false, NULL);
26629 if (lhs == error_mark_node)
26630 goto saw_error;
26631 if (code == NOP_EXPR)
26633 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26634 opcode. */
26635 code = OMP_ATOMIC;
26636 rhs = lhs;
26637 lhs = v;
26638 v = NULL_TREE;
26640 goto done;
26641 case OMP_ATOMIC_CAPTURE_NEW:
26642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26644 cp_lexer_consume_token (parser->lexer);
26645 structured_block = true;
26647 else
26649 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26650 /*cast_p=*/false, NULL);
26651 if (v == error_mark_node)
26652 goto saw_error;
26653 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26654 goto saw_error;
26656 default:
26657 break;
26660 restart:
26661 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26662 /*cast_p=*/false, NULL);
26663 orig_lhs = lhs;
26664 switch (TREE_CODE (lhs))
26666 case ERROR_MARK:
26667 goto saw_error;
26669 case POSTINCREMENT_EXPR:
26670 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26671 code = OMP_ATOMIC_CAPTURE_OLD;
26672 /* FALLTHROUGH */
26673 case PREINCREMENT_EXPR:
26674 lhs = TREE_OPERAND (lhs, 0);
26675 opcode = PLUS_EXPR;
26676 rhs = integer_one_node;
26677 break;
26679 case POSTDECREMENT_EXPR:
26680 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26681 code = OMP_ATOMIC_CAPTURE_OLD;
26682 /* FALLTHROUGH */
26683 case PREDECREMENT_EXPR:
26684 lhs = TREE_OPERAND (lhs, 0);
26685 opcode = MINUS_EXPR;
26686 rhs = integer_one_node;
26687 break;
26689 case COMPOUND_EXPR:
26690 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26691 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26692 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26693 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26694 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26695 (TREE_OPERAND (lhs, 1), 0), 0)))
26696 == BOOLEAN_TYPE)
26697 /* Undo effects of boolean_increment for post {in,de}crement. */
26698 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26699 /* FALLTHRU */
26700 case MODIFY_EXPR:
26701 if (TREE_CODE (lhs) == MODIFY_EXPR
26702 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26704 /* Undo effects of boolean_increment. */
26705 if (integer_onep (TREE_OPERAND (lhs, 1)))
26707 /* This is pre or post increment. */
26708 rhs = TREE_OPERAND (lhs, 1);
26709 lhs = TREE_OPERAND (lhs, 0);
26710 opcode = NOP_EXPR;
26711 if (code == OMP_ATOMIC_CAPTURE_NEW
26712 && !structured_block
26713 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26714 code = OMP_ATOMIC_CAPTURE_OLD;
26715 break;
26718 /* FALLTHRU */
26719 default:
26720 switch (cp_lexer_peek_token (parser->lexer)->type)
26722 case CPP_MULT_EQ:
26723 opcode = MULT_EXPR;
26724 break;
26725 case CPP_DIV_EQ:
26726 opcode = TRUNC_DIV_EXPR;
26727 break;
26728 case CPP_PLUS_EQ:
26729 opcode = PLUS_EXPR;
26730 break;
26731 case CPP_MINUS_EQ:
26732 opcode = MINUS_EXPR;
26733 break;
26734 case CPP_LSHIFT_EQ:
26735 opcode = LSHIFT_EXPR;
26736 break;
26737 case CPP_RSHIFT_EQ:
26738 opcode = RSHIFT_EXPR;
26739 break;
26740 case CPP_AND_EQ:
26741 opcode = BIT_AND_EXPR;
26742 break;
26743 case CPP_OR_EQ:
26744 opcode = BIT_IOR_EXPR;
26745 break;
26746 case CPP_XOR_EQ:
26747 opcode = BIT_XOR_EXPR;
26748 break;
26749 case CPP_EQ:
26750 if (structured_block || code == OMP_ATOMIC)
26752 enum cp_parser_prec oprec;
26753 cp_token *token;
26754 cp_lexer_consume_token (parser->lexer);
26755 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26756 /*cast_p=*/false, NULL);
26757 if (rhs1 == error_mark_node)
26758 goto saw_error;
26759 token = cp_lexer_peek_token (parser->lexer);
26760 switch (token->type)
26762 case CPP_SEMICOLON:
26763 if (code == OMP_ATOMIC_CAPTURE_NEW)
26765 code = OMP_ATOMIC_CAPTURE_OLD;
26766 v = lhs;
26767 lhs = NULL_TREE;
26768 lhs1 = rhs1;
26769 rhs1 = NULL_TREE;
26770 cp_lexer_consume_token (parser->lexer);
26771 goto restart;
26773 cp_parser_error (parser,
26774 "invalid form of %<#pragma omp atomic%>");
26775 goto saw_error;
26776 case CPP_MULT:
26777 opcode = MULT_EXPR;
26778 break;
26779 case CPP_DIV:
26780 opcode = TRUNC_DIV_EXPR;
26781 break;
26782 case CPP_PLUS:
26783 opcode = PLUS_EXPR;
26784 break;
26785 case CPP_MINUS:
26786 opcode = MINUS_EXPR;
26787 break;
26788 case CPP_LSHIFT:
26789 opcode = LSHIFT_EXPR;
26790 break;
26791 case CPP_RSHIFT:
26792 opcode = RSHIFT_EXPR;
26793 break;
26794 case CPP_AND:
26795 opcode = BIT_AND_EXPR;
26796 break;
26797 case CPP_OR:
26798 opcode = BIT_IOR_EXPR;
26799 break;
26800 case CPP_XOR:
26801 opcode = BIT_XOR_EXPR;
26802 break;
26803 default:
26804 cp_parser_error (parser,
26805 "invalid operator for %<#pragma omp atomic%>");
26806 goto saw_error;
26808 oprec = TOKEN_PRECEDENCE (token);
26809 gcc_assert (oprec != PREC_NOT_OPERATOR);
26810 if (commutative_tree_code (opcode))
26811 oprec = (enum cp_parser_prec) (oprec - 1);
26812 cp_lexer_consume_token (parser->lexer);
26813 rhs = cp_parser_binary_expression (parser, false, false,
26814 oprec, NULL);
26815 if (rhs == error_mark_node)
26816 goto saw_error;
26817 goto stmt_done;
26819 /* FALLTHROUGH */
26820 default:
26821 cp_parser_error (parser,
26822 "invalid operator for %<#pragma omp atomic%>");
26823 goto saw_error;
26825 cp_lexer_consume_token (parser->lexer);
26827 rhs = cp_parser_expression (parser, false, NULL);
26828 if (rhs == error_mark_node)
26829 goto saw_error;
26830 break;
26832 stmt_done:
26833 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26835 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26836 goto saw_error;
26837 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26838 /*cast_p=*/false, NULL);
26839 if (v == error_mark_node)
26840 goto saw_error;
26841 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26842 goto saw_error;
26843 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26844 /*cast_p=*/false, NULL);
26845 if (lhs1 == error_mark_node)
26846 goto saw_error;
26848 if (structured_block)
26850 cp_parser_consume_semicolon_at_end_of_statement (parser);
26851 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26853 done:
26854 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26855 if (!structured_block)
26856 cp_parser_consume_semicolon_at_end_of_statement (parser);
26857 return;
26859 saw_error:
26860 cp_parser_skip_to_end_of_block_or_statement (parser);
26861 if (structured_block)
26863 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26864 cp_lexer_consume_token (parser->lexer);
26865 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26867 cp_parser_skip_to_end_of_block_or_statement (parser);
26868 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26869 cp_lexer_consume_token (parser->lexer);
26875 /* OpenMP 2.5:
26876 # pragma omp barrier new-line */
26878 static void
26879 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26881 cp_parser_require_pragma_eol (parser, pragma_tok);
26882 finish_omp_barrier ();
26885 /* OpenMP 2.5:
26886 # pragma omp critical [(name)] new-line
26887 structured-block */
26889 static tree
26890 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26892 tree stmt, name = NULL;
26894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26896 cp_lexer_consume_token (parser->lexer);
26898 name = cp_parser_identifier (parser);
26900 if (name == error_mark_node
26901 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26903 /*or_comma=*/false,
26904 /*consume_paren=*/true);
26905 if (name == error_mark_node)
26906 name = NULL;
26908 cp_parser_require_pragma_eol (parser, pragma_tok);
26910 stmt = cp_parser_omp_structured_block (parser);
26911 return c_finish_omp_critical (input_location, stmt, name);
26914 /* OpenMP 2.5:
26915 # pragma omp flush flush-vars[opt] new-line
26917 flush-vars:
26918 ( variable-list ) */
26920 static void
26921 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26924 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26925 cp_parser_require_pragma_eol (parser, pragma_tok);
26927 finish_omp_flush ();
26930 /* Helper function, to parse omp for increment expression. */
26932 static tree
26933 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26935 tree cond = cp_parser_binary_expression (parser, false, true,
26936 PREC_NOT_OPERATOR, NULL);
26937 if (cond == error_mark_node
26938 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26940 cp_parser_skip_to_end_of_statement (parser);
26941 return error_mark_node;
26944 switch (TREE_CODE (cond))
26946 case GT_EXPR:
26947 case GE_EXPR:
26948 case LT_EXPR:
26949 case LE_EXPR:
26950 break;
26951 default:
26952 return error_mark_node;
26955 /* If decl is an iterator, preserve LHS and RHS of the relational
26956 expr until finish_omp_for. */
26957 if (decl
26958 && (type_dependent_expression_p (decl)
26959 || CLASS_TYPE_P (TREE_TYPE (decl))))
26960 return cond;
26962 return build_x_binary_op (input_location, TREE_CODE (cond),
26963 TREE_OPERAND (cond, 0), ERROR_MARK,
26964 TREE_OPERAND (cond, 1), ERROR_MARK,
26965 /*overload=*/NULL, tf_warning_or_error);
26968 /* Helper function, to parse omp for increment expression. */
26970 static tree
26971 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26973 cp_token *token = cp_lexer_peek_token (parser->lexer);
26974 enum tree_code op;
26975 tree lhs, rhs;
26976 cp_id_kind idk;
26977 bool decl_first;
26979 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26981 op = (token->type == CPP_PLUS_PLUS
26982 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26983 cp_lexer_consume_token (parser->lexer);
26984 lhs = cp_parser_simple_cast_expression (parser);
26985 if (lhs != decl)
26986 return error_mark_node;
26987 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26990 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26991 if (lhs != decl)
26992 return error_mark_node;
26994 token = cp_lexer_peek_token (parser->lexer);
26995 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26997 op = (token->type == CPP_PLUS_PLUS
26998 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26999 cp_lexer_consume_token (parser->lexer);
27000 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27003 op = cp_parser_assignment_operator_opt (parser);
27004 if (op == ERROR_MARK)
27005 return error_mark_node;
27007 if (op != NOP_EXPR)
27009 rhs = cp_parser_assignment_expression (parser, false, NULL);
27010 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
27011 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27014 lhs = cp_parser_binary_expression (parser, false, false,
27015 PREC_ADDITIVE_EXPRESSION, NULL);
27016 token = cp_lexer_peek_token (parser->lexer);
27017 decl_first = lhs == decl;
27018 if (decl_first)
27019 lhs = NULL_TREE;
27020 if (token->type != CPP_PLUS
27021 && token->type != CPP_MINUS)
27022 return error_mark_node;
27026 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
27027 cp_lexer_consume_token (parser->lexer);
27028 rhs = cp_parser_binary_expression (parser, false, false,
27029 PREC_ADDITIVE_EXPRESSION, NULL);
27030 token = cp_lexer_peek_token (parser->lexer);
27031 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
27033 if (lhs == NULL_TREE)
27035 if (op == PLUS_EXPR)
27036 lhs = rhs;
27037 else
27038 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
27039 tf_warning_or_error);
27041 else
27042 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
27043 ERROR_MARK, NULL, tf_warning_or_error);
27046 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
27048 if (!decl_first)
27050 if (rhs != decl || op == MINUS_EXPR)
27051 return error_mark_node;
27052 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
27054 else
27055 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
27057 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27060 /* Parse the restricted form of the for statement allowed by OpenMP. */
27062 static tree
27063 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
27065 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
27066 tree real_decl, initv, condv, incrv, declv;
27067 tree this_pre_body, cl;
27068 location_t loc_first;
27069 bool collapse_err = false;
27070 int i, collapse = 1, nbraces = 0;
27071 vec<tree, va_gc> *for_block = make_tree_vector ();
27073 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
27074 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
27075 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
27077 gcc_assert (collapse >= 1);
27079 declv = make_tree_vec (collapse);
27080 initv = make_tree_vec (collapse);
27081 condv = make_tree_vec (collapse);
27082 incrv = make_tree_vec (collapse);
27084 loc_first = cp_lexer_peek_token (parser->lexer)->location;
27086 for (i = 0; i < collapse; i++)
27088 int bracecount = 0;
27089 bool add_private_clause = false;
27090 location_t loc;
27092 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27094 cp_parser_error (parser, "for statement expected");
27095 return NULL;
27097 loc = cp_lexer_consume_token (parser->lexer)->location;
27099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27100 return NULL;
27102 init = decl = real_decl = NULL;
27103 this_pre_body = push_stmt_list ();
27104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27106 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
27108 init-expr:
27109 var = lb
27110 integer-type var = lb
27111 random-access-iterator-type var = lb
27112 pointer-type var = lb
27114 cp_decl_specifier_seq type_specifiers;
27116 /* First, try to parse as an initialized declaration. See
27117 cp_parser_condition, from whence the bulk of this is copied. */
27119 cp_parser_parse_tentatively (parser);
27120 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
27121 /*is_trailing_return=*/false,
27122 &type_specifiers);
27123 if (cp_parser_parse_definitely (parser))
27125 /* If parsing a type specifier seq succeeded, then this
27126 MUST be a initialized declaration. */
27127 tree asm_specification, attributes;
27128 cp_declarator *declarator;
27130 declarator = cp_parser_declarator (parser,
27131 CP_PARSER_DECLARATOR_NAMED,
27132 /*ctor_dtor_or_conv_p=*/NULL,
27133 /*parenthesized_p=*/NULL,
27134 /*member_p=*/false);
27135 attributes = cp_parser_attributes_opt (parser);
27136 asm_specification = cp_parser_asm_specification_opt (parser);
27138 if (declarator == cp_error_declarator)
27139 cp_parser_skip_to_end_of_statement (parser);
27141 else
27143 tree pushed_scope, auto_node;
27145 decl = start_decl (declarator, &type_specifiers,
27146 SD_INITIALIZED, attributes,
27147 /*prefix_attributes=*/NULL_TREE,
27148 &pushed_scope);
27150 auto_node = type_uses_auto (TREE_TYPE (decl));
27151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27153 if (cp_lexer_next_token_is (parser->lexer,
27154 CPP_OPEN_PAREN))
27155 error ("parenthesized initialization is not allowed in "
27156 "OpenMP %<for%> loop");
27157 else
27158 /* Trigger an error. */
27159 cp_parser_require (parser, CPP_EQ, RT_EQ);
27161 init = error_mark_node;
27162 cp_parser_skip_to_end_of_statement (parser);
27164 else if (CLASS_TYPE_P (TREE_TYPE (decl))
27165 || type_dependent_expression_p (decl)
27166 || auto_node)
27168 bool is_direct_init, is_non_constant_init;
27170 init = cp_parser_initializer (parser,
27171 &is_direct_init,
27172 &is_non_constant_init);
27174 if (auto_node)
27176 TREE_TYPE (decl)
27177 = do_auto_deduction (TREE_TYPE (decl), init,
27178 auto_node);
27180 if (!CLASS_TYPE_P (TREE_TYPE (decl))
27181 && !type_dependent_expression_p (decl))
27182 goto non_class;
27185 cp_finish_decl (decl, init, !is_non_constant_init,
27186 asm_specification,
27187 LOOKUP_ONLYCONVERTING);
27188 if (CLASS_TYPE_P (TREE_TYPE (decl)))
27190 vec_safe_push (for_block, this_pre_body);
27191 init = NULL_TREE;
27193 else
27194 init = pop_stmt_list (this_pre_body);
27195 this_pre_body = NULL_TREE;
27197 else
27199 /* Consume '='. */
27200 cp_lexer_consume_token (parser->lexer);
27201 init = cp_parser_assignment_expression (parser, false, NULL);
27203 non_class:
27204 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27205 init = error_mark_node;
27206 else
27207 cp_finish_decl (decl, NULL_TREE,
27208 /*init_const_expr_p=*/false,
27209 asm_specification,
27210 LOOKUP_ONLYCONVERTING);
27213 if (pushed_scope)
27214 pop_scope (pushed_scope);
27217 else
27219 cp_id_kind idk;
27220 /* If parsing a type specifier sequence failed, then
27221 this MUST be a simple expression. */
27222 cp_parser_parse_tentatively (parser);
27223 decl = cp_parser_primary_expression (parser, false, false,
27224 false, &idk);
27225 if (!cp_parser_error_occurred (parser)
27226 && decl
27227 && DECL_P (decl)
27228 && CLASS_TYPE_P (TREE_TYPE (decl)))
27230 tree rhs;
27232 cp_parser_parse_definitely (parser);
27233 cp_parser_require (parser, CPP_EQ, RT_EQ);
27234 rhs = cp_parser_assignment_expression (parser, false, NULL);
27235 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27236 decl, NOP_EXPR,
27237 rhs,
27238 tf_warning_or_error));
27239 add_private_clause = true;
27241 else
27243 decl = NULL;
27244 cp_parser_abort_tentative_parse (parser);
27245 init = cp_parser_expression (parser, false, NULL);
27246 if (init)
27248 if (TREE_CODE (init) == MODIFY_EXPR
27249 || TREE_CODE (init) == MODOP_EXPR)
27250 real_decl = TREE_OPERAND (init, 0);
27255 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27256 if (this_pre_body)
27258 this_pre_body = pop_stmt_list (this_pre_body);
27259 if (pre_body)
27261 tree t = pre_body;
27262 pre_body = push_stmt_list ();
27263 add_stmt (t);
27264 add_stmt (this_pre_body);
27265 pre_body = pop_stmt_list (pre_body);
27267 else
27268 pre_body = this_pre_body;
27271 if (decl)
27272 real_decl = decl;
27273 if (par_clauses != NULL && real_decl != NULL_TREE)
27275 tree *c;
27276 for (c = par_clauses; *c ; )
27277 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27278 && OMP_CLAUSE_DECL (*c) == real_decl)
27280 error_at (loc, "iteration variable %qD"
27281 " should not be firstprivate", real_decl);
27282 *c = OMP_CLAUSE_CHAIN (*c);
27284 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27285 && OMP_CLAUSE_DECL (*c) == real_decl)
27287 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27288 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27289 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27290 OMP_CLAUSE_DECL (l) = real_decl;
27291 OMP_CLAUSE_CHAIN (l) = clauses;
27292 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27293 clauses = l;
27294 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27295 CP_OMP_CLAUSE_INFO (*c) = NULL;
27296 add_private_clause = false;
27298 else
27300 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27301 && OMP_CLAUSE_DECL (*c) == real_decl)
27302 add_private_clause = false;
27303 c = &OMP_CLAUSE_CHAIN (*c);
27307 if (add_private_clause)
27309 tree c;
27310 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27312 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27313 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27314 && OMP_CLAUSE_DECL (c) == decl)
27315 break;
27316 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27317 && OMP_CLAUSE_DECL (c) == decl)
27318 error_at (loc, "iteration variable %qD "
27319 "should not be firstprivate",
27320 decl);
27321 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27322 && OMP_CLAUSE_DECL (c) == decl)
27323 error_at (loc, "iteration variable %qD should not be reduction",
27324 decl);
27326 if (c == NULL)
27328 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27329 OMP_CLAUSE_DECL (c) = decl;
27330 c = finish_omp_clauses (c);
27331 if (c)
27333 OMP_CLAUSE_CHAIN (c) = clauses;
27334 clauses = c;
27339 cond = NULL;
27340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27341 cond = cp_parser_omp_for_cond (parser, decl);
27342 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27344 incr = NULL;
27345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27347 /* If decl is an iterator, preserve the operator on decl
27348 until finish_omp_for. */
27349 if (real_decl
27350 && ((processing_template_decl
27351 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27352 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27353 incr = cp_parser_omp_for_incr (parser, real_decl);
27354 else
27355 incr = cp_parser_expression (parser, false, NULL);
27356 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27357 SET_EXPR_LOCATION (incr, input_location);
27360 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27362 /*or_comma=*/false,
27363 /*consume_paren=*/true);
27365 TREE_VEC_ELT (declv, i) = decl;
27366 TREE_VEC_ELT (initv, i) = init;
27367 TREE_VEC_ELT (condv, i) = cond;
27368 TREE_VEC_ELT (incrv, i) = incr;
27370 if (i == collapse - 1)
27371 break;
27373 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27374 in between the collapsed for loops to be still considered perfectly
27375 nested. Hopefully the final version clarifies this.
27376 For now handle (multiple) {'s and empty statements. */
27377 cp_parser_parse_tentatively (parser);
27380 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27381 break;
27382 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27384 cp_lexer_consume_token (parser->lexer);
27385 bracecount++;
27387 else if (bracecount
27388 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27389 cp_lexer_consume_token (parser->lexer);
27390 else
27392 loc = cp_lexer_peek_token (parser->lexer)->location;
27393 error_at (loc, "not enough collapsed for loops");
27394 collapse_err = true;
27395 cp_parser_abort_tentative_parse (parser);
27396 declv = NULL_TREE;
27397 break;
27400 while (1);
27402 if (declv)
27404 cp_parser_parse_definitely (parser);
27405 nbraces += bracecount;
27409 /* Note that we saved the original contents of this flag when we entered
27410 the structured block, and so we don't need to re-save it here. */
27411 parser->in_statement = IN_OMP_FOR;
27413 /* Note that the grammar doesn't call for a structured block here,
27414 though the loop as a whole is a structured block. */
27415 body = push_stmt_list ();
27416 cp_parser_statement (parser, NULL_TREE, false, NULL);
27417 body = pop_stmt_list (body);
27419 if (declv == NULL_TREE)
27420 ret = NULL_TREE;
27421 else
27422 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27423 pre_body, clauses);
27425 while (nbraces)
27427 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27429 cp_lexer_consume_token (parser->lexer);
27430 nbraces--;
27432 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27433 cp_lexer_consume_token (parser->lexer);
27434 else
27436 if (!collapse_err)
27438 error_at (cp_lexer_peek_token (parser->lexer)->location,
27439 "collapsed loops not perfectly nested");
27441 collapse_err = true;
27442 cp_parser_statement_seq_opt (parser, NULL);
27443 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27444 break;
27448 while (!for_block->is_empty ())
27449 add_stmt (pop_stmt_list (for_block->pop ()));
27450 release_tree_vector (for_block);
27452 return ret;
27455 /* OpenMP 2.5:
27456 #pragma omp for for-clause[optseq] new-line
27457 for-loop */
27459 #define OMP_FOR_CLAUSE_MASK \
27460 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27461 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27462 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27463 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27464 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27465 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27466 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27467 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27469 static tree
27470 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27472 tree clauses, sb, ret;
27473 unsigned int save;
27475 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27476 "#pragma omp for", pragma_tok);
27478 sb = begin_omp_structured_block ();
27479 save = cp_parser_begin_omp_structured_block (parser);
27481 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27483 cp_parser_end_omp_structured_block (parser, save);
27484 add_stmt (finish_omp_structured_block (sb));
27486 return ret;
27489 /* OpenMP 2.5:
27490 # pragma omp master new-line
27491 structured-block */
27493 static tree
27494 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27496 cp_parser_require_pragma_eol (parser, pragma_tok);
27497 return c_finish_omp_master (input_location,
27498 cp_parser_omp_structured_block (parser));
27501 /* OpenMP 2.5:
27502 # pragma omp ordered new-line
27503 structured-block */
27505 static tree
27506 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27508 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27509 cp_parser_require_pragma_eol (parser, pragma_tok);
27510 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27513 /* OpenMP 2.5:
27515 section-scope:
27516 { section-sequence }
27518 section-sequence:
27519 section-directive[opt] structured-block
27520 section-sequence section-directive structured-block */
27522 static tree
27523 cp_parser_omp_sections_scope (cp_parser *parser)
27525 tree stmt, substmt;
27526 bool error_suppress = false;
27527 cp_token *tok;
27529 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27530 return NULL_TREE;
27532 stmt = push_stmt_list ();
27534 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27536 unsigned save;
27538 substmt = begin_omp_structured_block ();
27539 save = cp_parser_begin_omp_structured_block (parser);
27541 while (1)
27543 cp_parser_statement (parser, NULL_TREE, false, NULL);
27545 tok = cp_lexer_peek_token (parser->lexer);
27546 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27547 break;
27548 if (tok->type == CPP_CLOSE_BRACE)
27549 break;
27550 if (tok->type == CPP_EOF)
27551 break;
27554 cp_parser_end_omp_structured_block (parser, save);
27555 substmt = finish_omp_structured_block (substmt);
27556 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27557 add_stmt (substmt);
27560 while (1)
27562 tok = cp_lexer_peek_token (parser->lexer);
27563 if (tok->type == CPP_CLOSE_BRACE)
27564 break;
27565 if (tok->type == CPP_EOF)
27566 break;
27568 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27570 cp_lexer_consume_token (parser->lexer);
27571 cp_parser_require_pragma_eol (parser, tok);
27572 error_suppress = false;
27574 else if (!error_suppress)
27576 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27577 error_suppress = true;
27580 substmt = cp_parser_omp_structured_block (parser);
27581 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27582 add_stmt (substmt);
27584 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27586 substmt = pop_stmt_list (stmt);
27588 stmt = make_node (OMP_SECTIONS);
27589 TREE_TYPE (stmt) = void_type_node;
27590 OMP_SECTIONS_BODY (stmt) = substmt;
27592 add_stmt (stmt);
27593 return stmt;
27596 /* OpenMP 2.5:
27597 # pragma omp sections sections-clause[optseq] newline
27598 sections-scope */
27600 #define OMP_SECTIONS_CLAUSE_MASK \
27601 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27602 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27603 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27604 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27605 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27607 static tree
27608 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27610 tree clauses, ret;
27612 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27613 "#pragma omp sections", pragma_tok);
27615 ret = cp_parser_omp_sections_scope (parser);
27616 if (ret)
27617 OMP_SECTIONS_CLAUSES (ret) = clauses;
27619 return ret;
27622 /* OpenMP 2.5:
27623 # pragma parallel parallel-clause new-line
27624 # pragma parallel for parallel-for-clause new-line
27625 # pragma parallel sections parallel-sections-clause new-line */
27627 #define OMP_PARALLEL_CLAUSE_MASK \
27628 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27629 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27630 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27631 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27632 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27633 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27634 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27635 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27637 static tree
27638 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27640 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27641 const char *p_name = "#pragma omp parallel";
27642 tree stmt, clauses, par_clause, ws_clause, block;
27643 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27644 unsigned int save;
27645 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27649 cp_lexer_consume_token (parser->lexer);
27650 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27651 p_name = "#pragma omp parallel for";
27652 mask |= OMP_FOR_CLAUSE_MASK;
27653 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27655 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27658 const char *p = IDENTIFIER_POINTER (id);
27659 if (strcmp (p, "sections") == 0)
27661 cp_lexer_consume_token (parser->lexer);
27662 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27663 p_name = "#pragma omp parallel sections";
27664 mask |= OMP_SECTIONS_CLAUSE_MASK;
27665 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27669 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27670 block = begin_omp_parallel ();
27671 save = cp_parser_begin_omp_structured_block (parser);
27673 switch (p_kind)
27675 case PRAGMA_OMP_PARALLEL:
27676 cp_parser_statement (parser, NULL_TREE, false, NULL);
27677 par_clause = clauses;
27678 break;
27680 case PRAGMA_OMP_PARALLEL_FOR:
27681 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27682 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27683 break;
27685 case PRAGMA_OMP_PARALLEL_SECTIONS:
27686 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27687 stmt = cp_parser_omp_sections_scope (parser);
27688 if (stmt)
27689 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27690 break;
27692 default:
27693 gcc_unreachable ();
27696 cp_parser_end_omp_structured_block (parser, save);
27697 stmt = finish_omp_parallel (par_clause, block);
27698 if (p_kind != PRAGMA_OMP_PARALLEL)
27699 OMP_PARALLEL_COMBINED (stmt) = 1;
27700 return stmt;
27703 /* OpenMP 2.5:
27704 # pragma omp single single-clause[optseq] new-line
27705 structured-block */
27707 #define OMP_SINGLE_CLAUSE_MASK \
27708 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27709 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27710 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27711 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27713 static tree
27714 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27716 tree stmt = make_node (OMP_SINGLE);
27717 TREE_TYPE (stmt) = void_type_node;
27719 OMP_SINGLE_CLAUSES (stmt)
27720 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27721 "#pragma omp single", pragma_tok);
27722 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27724 return add_stmt (stmt);
27727 /* OpenMP 3.0:
27728 # pragma omp task task-clause[optseq] new-line
27729 structured-block */
27731 #define OMP_TASK_CLAUSE_MASK \
27732 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27733 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27734 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27735 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27736 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27737 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27738 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27739 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27741 static tree
27742 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27744 tree clauses, block;
27745 unsigned int save;
27747 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27748 "#pragma omp task", pragma_tok);
27749 block = begin_omp_task ();
27750 save = cp_parser_begin_omp_structured_block (parser);
27751 cp_parser_statement (parser, NULL_TREE, false, NULL);
27752 cp_parser_end_omp_structured_block (parser, save);
27753 return finish_omp_task (clauses, block);
27756 /* OpenMP 3.0:
27757 # pragma omp taskwait new-line */
27759 static void
27760 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27762 cp_parser_require_pragma_eol (parser, pragma_tok);
27763 finish_omp_taskwait ();
27766 /* OpenMP 3.1:
27767 # pragma omp taskyield new-line */
27769 static void
27770 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27772 cp_parser_require_pragma_eol (parser, pragma_tok);
27773 finish_omp_taskyield ();
27776 /* OpenMP 2.5:
27777 # pragma omp threadprivate (variable-list) */
27779 static void
27780 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27782 tree vars;
27784 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27785 cp_parser_require_pragma_eol (parser, pragma_tok);
27787 finish_omp_threadprivate (vars);
27790 /* Main entry point to OpenMP statement pragmas. */
27792 static void
27793 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27795 tree stmt;
27797 switch (pragma_tok->pragma_kind)
27799 case PRAGMA_OMP_ATOMIC:
27800 cp_parser_omp_atomic (parser, pragma_tok);
27801 return;
27802 case PRAGMA_OMP_CRITICAL:
27803 stmt = cp_parser_omp_critical (parser, pragma_tok);
27804 break;
27805 case PRAGMA_OMP_FOR:
27806 stmt = cp_parser_omp_for (parser, pragma_tok);
27807 break;
27808 case PRAGMA_OMP_MASTER:
27809 stmt = cp_parser_omp_master (parser, pragma_tok);
27810 break;
27811 case PRAGMA_OMP_ORDERED:
27812 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27813 break;
27814 case PRAGMA_OMP_PARALLEL:
27815 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27816 break;
27817 case PRAGMA_OMP_SECTIONS:
27818 stmt = cp_parser_omp_sections (parser, pragma_tok);
27819 break;
27820 case PRAGMA_OMP_SINGLE:
27821 stmt = cp_parser_omp_single (parser, pragma_tok);
27822 break;
27823 case PRAGMA_OMP_TASK:
27824 stmt = cp_parser_omp_task (parser, pragma_tok);
27825 break;
27826 default:
27827 gcc_unreachable ();
27830 if (stmt)
27831 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27834 /* Transactional Memory parsing routines. */
27836 /* Parse a transaction attribute.
27838 txn-attribute:
27839 attribute
27840 [ [ identifier ] ]
27842 ??? Simplify this when C++0x bracket attributes are
27843 implemented properly. */
27845 static tree
27846 cp_parser_txn_attribute_opt (cp_parser *parser)
27848 cp_token *token;
27849 tree attr_name, attr = NULL;
27851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27852 return cp_parser_attributes_opt (parser);
27854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27855 return NULL_TREE;
27856 cp_lexer_consume_token (parser->lexer);
27857 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27858 goto error1;
27860 token = cp_lexer_peek_token (parser->lexer);
27861 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27863 token = cp_lexer_consume_token (parser->lexer);
27865 attr_name = (token->type == CPP_KEYWORD
27866 /* For keywords, use the canonical spelling,
27867 not the parsed identifier. */
27868 ? ridpointers[(int) token->keyword]
27869 : token->u.value);
27870 attr = build_tree_list (attr_name, NULL_TREE);
27872 else
27873 cp_parser_error (parser, "expected identifier");
27875 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27876 error1:
27877 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27878 return attr;
27881 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27883 transaction-statement:
27884 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27885 compound-statement
27886 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27889 static tree
27890 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27892 unsigned char old_in = parser->in_transaction;
27893 unsigned char this_in = 1, new_in;
27894 cp_token *token;
27895 tree stmt, attrs, noex;
27897 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27898 || keyword == RID_TRANSACTION_RELAXED);
27899 token = cp_parser_require_keyword (parser, keyword,
27900 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27901 : RT_TRANSACTION_RELAXED));
27902 gcc_assert (token != NULL);
27904 if (keyword == RID_TRANSACTION_RELAXED)
27905 this_in |= TM_STMT_ATTR_RELAXED;
27906 else
27908 attrs = cp_parser_txn_attribute_opt (parser);
27909 if (attrs)
27910 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27913 /* Parse a noexcept specification. */
27914 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27916 /* Keep track if we're in the lexical scope of an outer transaction. */
27917 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27919 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27921 parser->in_transaction = new_in;
27922 cp_parser_compound_statement (parser, NULL, false, false);
27923 parser->in_transaction = old_in;
27925 finish_transaction_stmt (stmt, NULL, this_in, noex);
27927 return stmt;
27930 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27932 transaction-expression:
27933 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27934 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27937 static tree
27938 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27940 unsigned char old_in = parser->in_transaction;
27941 unsigned char this_in = 1;
27942 cp_token *token;
27943 tree expr, noex;
27944 bool noex_expr;
27946 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27947 || keyword == RID_TRANSACTION_RELAXED);
27949 if (!flag_tm)
27950 error (keyword == RID_TRANSACTION_RELAXED
27951 ? G_("%<__transaction_relaxed%> without transactional memory "
27952 "support enabled")
27953 : G_("%<__transaction_atomic%> without transactional memory "
27954 "support enabled"));
27956 token = cp_parser_require_keyword (parser, keyword,
27957 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27958 : RT_TRANSACTION_RELAXED));
27959 gcc_assert (token != NULL);
27961 if (keyword == RID_TRANSACTION_RELAXED)
27962 this_in |= TM_STMT_ATTR_RELAXED;
27964 /* Set this early. This might mean that we allow transaction_cancel in
27965 an expression that we find out later actually has to be a constexpr.
27966 However, we expect that cxx_constant_value will be able to deal with
27967 this; also, if the noexcept has no constexpr, then what we parse next
27968 really is a transaction's body. */
27969 parser->in_transaction = this_in;
27971 /* Parse a noexcept specification. */
27972 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27973 true);
27975 if (!noex || !noex_expr
27976 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27978 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27980 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27981 finish_parenthesized_expr (expr);
27983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27985 else
27987 /* The only expression that is available got parsed for the noexcept
27988 already. noexcept is true then. */
27989 expr = noex;
27990 noex = boolean_true_node;
27993 expr = build_transaction_expr (token->location, expr, this_in, noex);
27994 parser->in_transaction = old_in;
27996 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27997 return error_mark_node;
27999 return (flag_tm ? expr : error_mark_node);
28002 /* Parse a function-transaction-block.
28004 function-transaction-block:
28005 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
28006 function-body
28007 __transaction_atomic txn-attribute[opt] function-try-block
28008 __transaction_relaxed ctor-initializer[opt] function-body
28009 __transaction_relaxed function-try-block
28012 static bool
28013 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
28015 unsigned char old_in = parser->in_transaction;
28016 unsigned char new_in = 1;
28017 tree compound_stmt, stmt, attrs;
28018 bool ctor_initializer_p;
28019 cp_token *token;
28021 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28022 || keyword == RID_TRANSACTION_RELAXED);
28023 token = cp_parser_require_keyword (parser, keyword,
28024 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28025 : RT_TRANSACTION_RELAXED));
28026 gcc_assert (token != NULL);
28028 if (keyword == RID_TRANSACTION_RELAXED)
28029 new_in |= TM_STMT_ATTR_RELAXED;
28030 else
28032 attrs = cp_parser_txn_attribute_opt (parser);
28033 if (attrs)
28034 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28037 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
28039 parser->in_transaction = new_in;
28041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28042 ctor_initializer_p = cp_parser_function_try_block (parser);
28043 else
28044 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
28045 (parser, /*in_function_try_block=*/false);
28047 parser->in_transaction = old_in;
28049 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
28051 return ctor_initializer_p;
28054 /* Parse a __transaction_cancel statement.
28056 cancel-statement:
28057 __transaction_cancel txn-attribute[opt] ;
28058 __transaction_cancel txn-attribute[opt] throw-expression ;
28060 ??? Cancel and throw is not yet implemented. */
28062 static tree
28063 cp_parser_transaction_cancel (cp_parser *parser)
28065 cp_token *token;
28066 bool is_outer = false;
28067 tree stmt, attrs;
28069 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
28070 RT_TRANSACTION_CANCEL);
28071 gcc_assert (token != NULL);
28073 attrs = cp_parser_txn_attribute_opt (parser);
28074 if (attrs)
28075 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
28077 /* ??? Parse cancel-and-throw here. */
28079 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28081 if (!flag_tm)
28083 error_at (token->location, "%<__transaction_cancel%> without "
28084 "transactional memory support enabled");
28085 return error_mark_node;
28087 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
28089 error_at (token->location, "%<__transaction_cancel%> within a "
28090 "%<__transaction_relaxed%>");
28091 return error_mark_node;
28093 else if (is_outer)
28095 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
28096 && !is_tm_may_cancel_outer (current_function_decl))
28098 error_at (token->location, "outer %<__transaction_cancel%> not "
28099 "within outer %<__transaction_atomic%>");
28100 error_at (token->location,
28101 " or a %<transaction_may_cancel_outer%> function");
28102 return error_mark_node;
28105 else if (parser->in_transaction == 0)
28107 error_at (token->location, "%<__transaction_cancel%> not within "
28108 "%<__transaction_atomic%>");
28109 return error_mark_node;
28112 stmt = build_tm_abort_call (token->location, is_outer);
28113 add_stmt (stmt);
28114 finish_stmt ();
28116 return stmt;
28119 /* The parser. */
28121 static GTY (()) cp_parser *the_parser;
28124 /* Special handling for the first token or line in the file. The first
28125 thing in the file might be #pragma GCC pch_preprocess, which loads a
28126 PCH file, which is a GC collection point. So we need to handle this
28127 first pragma without benefit of an existing lexer structure.
28129 Always returns one token to the caller in *FIRST_TOKEN. This is
28130 either the true first token of the file, or the first token after
28131 the initial pragma. */
28133 static void
28134 cp_parser_initial_pragma (cp_token *first_token)
28136 tree name = NULL;
28138 cp_lexer_get_preprocessor_token (NULL, first_token);
28139 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
28140 return;
28142 cp_lexer_get_preprocessor_token (NULL, first_token);
28143 if (first_token->type == CPP_STRING)
28145 name = first_token->u.value;
28147 cp_lexer_get_preprocessor_token (NULL, first_token);
28148 if (first_token->type != CPP_PRAGMA_EOL)
28149 error_at (first_token->location,
28150 "junk at end of %<#pragma GCC pch_preprocess%>");
28152 else
28153 error_at (first_token->location, "expected string literal");
28155 /* Skip to the end of the pragma. */
28156 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
28157 cp_lexer_get_preprocessor_token (NULL, first_token);
28159 /* Now actually load the PCH file. */
28160 if (name)
28161 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
28163 /* Read one more token to return to our caller. We have to do this
28164 after reading the PCH file in, since its pointers have to be
28165 live. */
28166 cp_lexer_get_preprocessor_token (NULL, first_token);
28169 /* Normal parsing of a pragma token. Here we can (and must) use the
28170 regular lexer. */
28172 static bool
28173 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
28175 cp_token *pragma_tok;
28176 unsigned int id;
28178 pragma_tok = cp_lexer_consume_token (parser->lexer);
28179 gcc_assert (pragma_tok->type == CPP_PRAGMA);
28180 parser->lexer->in_pragma = true;
28182 id = pragma_tok->pragma_kind;
28183 switch (id)
28185 case PRAGMA_GCC_PCH_PREPROCESS:
28186 error_at (pragma_tok->location,
28187 "%<#pragma GCC pch_preprocess%> must be first");
28188 break;
28190 case PRAGMA_OMP_BARRIER:
28191 switch (context)
28193 case pragma_compound:
28194 cp_parser_omp_barrier (parser, pragma_tok);
28195 return false;
28196 case pragma_stmt:
28197 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28198 "used in compound statements");
28199 break;
28200 default:
28201 goto bad_stmt;
28203 break;
28205 case PRAGMA_OMP_FLUSH:
28206 switch (context)
28208 case pragma_compound:
28209 cp_parser_omp_flush (parser, pragma_tok);
28210 return false;
28211 case pragma_stmt:
28212 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28213 "used in compound statements");
28214 break;
28215 default:
28216 goto bad_stmt;
28218 break;
28220 case PRAGMA_OMP_TASKWAIT:
28221 switch (context)
28223 case pragma_compound:
28224 cp_parser_omp_taskwait (parser, pragma_tok);
28225 return false;
28226 case pragma_stmt:
28227 error_at (pragma_tok->location,
28228 "%<#pragma omp taskwait%> may only be "
28229 "used in compound statements");
28230 break;
28231 default:
28232 goto bad_stmt;
28234 break;
28236 case PRAGMA_OMP_TASKYIELD:
28237 switch (context)
28239 case pragma_compound:
28240 cp_parser_omp_taskyield (parser, pragma_tok);
28241 return false;
28242 case pragma_stmt:
28243 error_at (pragma_tok->location,
28244 "%<#pragma omp taskyield%> may only be "
28245 "used in compound statements");
28246 break;
28247 default:
28248 goto bad_stmt;
28250 break;
28252 case PRAGMA_OMP_THREADPRIVATE:
28253 cp_parser_omp_threadprivate (parser, pragma_tok);
28254 return false;
28256 case PRAGMA_OMP_ATOMIC:
28257 case PRAGMA_OMP_CRITICAL:
28258 case PRAGMA_OMP_FOR:
28259 case PRAGMA_OMP_MASTER:
28260 case PRAGMA_OMP_ORDERED:
28261 case PRAGMA_OMP_PARALLEL:
28262 case PRAGMA_OMP_SECTIONS:
28263 case PRAGMA_OMP_SINGLE:
28264 case PRAGMA_OMP_TASK:
28265 if (context == pragma_external)
28266 goto bad_stmt;
28267 cp_parser_omp_construct (parser, pragma_tok);
28268 return true;
28270 case PRAGMA_OMP_SECTION:
28271 error_at (pragma_tok->location,
28272 "%<#pragma omp section%> may only be used in "
28273 "%<#pragma omp sections%> construct");
28274 break;
28276 default:
28277 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28278 c_invoke_pragma_handler (id);
28279 break;
28281 bad_stmt:
28282 cp_parser_error (parser, "expected declaration specifiers");
28283 break;
28286 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28287 return false;
28290 /* The interface the pragma parsers have to the lexer. */
28292 enum cpp_ttype
28293 pragma_lex (tree *value)
28295 cp_token *tok;
28296 enum cpp_ttype ret;
28298 tok = cp_lexer_peek_token (the_parser->lexer);
28300 ret = tok->type;
28301 *value = tok->u.value;
28303 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28304 ret = CPP_EOF;
28305 else if (ret == CPP_STRING)
28306 *value = cp_parser_string_literal (the_parser, false, false);
28307 else
28309 cp_lexer_consume_token (the_parser->lexer);
28310 if (ret == CPP_KEYWORD)
28311 ret = CPP_NAME;
28314 return ret;
28318 /* External interface. */
28320 /* Parse one entire translation unit. */
28322 void
28323 c_parse_file (void)
28325 static bool already_called = false;
28327 if (already_called)
28329 sorry ("inter-module optimizations not implemented for C++");
28330 return;
28332 already_called = true;
28334 the_parser = cp_parser_new ();
28335 push_deferring_access_checks (flag_access_control
28336 ? dk_no_deferred : dk_no_check);
28337 cp_parser_translation_unit (the_parser);
28338 the_parser = NULL;
28341 #include "gt-cp-parser.h"