2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "c-family/c-pragma.h"
34 #include "diagnostic-core.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
40 #include "tree-pretty-print.h"
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant {
57 /* floating-point literal */
61 /* %<__FUNCTION__%> */
63 /* %<__PRETTY_FUNCTION__%> */
71 /* %<typeid%> operator */
73 /* non-constant compound literals */
81 /* an array reference */
87 /* the address of a label */
101 /* calls to overloaded operators */
105 /* a comma operator */
107 /* a call to a constructor */
109 /* a transaction expression */
111 } non_integral_constant;
113 /* The various kinds of errors about name-lookup failing. */
114 typedef enum name_lookup_error {
119 /* is not a class or namespace */
121 /* is not a class, namespace, or enumeration */
125 /* The various kinds of required token */
126 typedef enum required_token {
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
137 RT_GREATER, /* '>' */
139 RT_ELLIPSIS, /* '...' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
149 /* The type is CPP_KEYWORD */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
183 static cp_lexer *cp_lexer_new_main
185 static cp_lexer *cp_lexer_new_from_tokens
186 (cp_token_cache *tokens);
187 static void cp_lexer_destroy
189 static int cp_lexer_saving_tokens
191 static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
197 static cp_token *cp_lexer_peek_nth_token
198 (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
207 static void cp_lexer_purge_token
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
213 static void cp_lexer_commit_tokens
215 static void cp_lexer_rollback_tokens
217 static void cp_lexer_print_token
218 (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
221 static void cp_lexer_start_debugging
222 (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224 (cp_lexer *) ATTRIBUTE_UNUSED;
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
229 static void cp_parser_initial_pragma
232 static tree cp_literal_operator_id
235 /* Manifest constants. */
236 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
237 #define CP_SAVED_TOKEN_STACK 5
241 /* The stream to which debugging output should be written. */
242 static FILE *cp_lexer_debug_stream;
244 /* Nonzero if we are parsing an unevaluated operand: an operand to
245 sizeof, typeof, or alignof. */
246 int cp_unevaluated_operand;
248 /* Dump up to NUM tokens in BUFFER to FILE starting with token
249 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
250 first token in BUFFER. If NUM is 0, dump all the tokens. If
251 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
252 highlighted by surrounding it in [[ ]]. */
255 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
256 cp_token *start_token, unsigned num,
257 cp_token *curr_token)
259 unsigned i, nprinted;
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
269 num = VEC_length (cp_token, buffer);
271 if (start_token == NULL)
272 start_token = VEC_address (cp_token, buffer);
274 if (start_token > VEC_address (cp_token, buffer))
276 cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
277 fprintf (file, " ... ");
282 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284 if (token == start_token)
291 if (token == curr_token)
292 fprintf (file, "[[");
294 cp_lexer_print_token (file, token);
296 if (token == curr_token)
297 fprintf (file, "]]");
303 case CPP_CLOSE_BRACE:
313 if (i == num && i < VEC_length (cp_token, buffer))
315 fprintf (file, " ... ");
316 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
317 VEC_length (cp_token, buffer) - 1));
320 fprintf (file, "\n");
324 /* Dump all tokens in BUFFER to stderr. */
327 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
329 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
333 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
334 description for T. */
337 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
341 fprintf (file, "%s: ", desc);
342 print_node_brief (file, "", t, 0);
347 /* Dump parser context C to FILE. */
350 cp_debug_print_context (FILE *file, cp_parser_context *c)
352 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
353 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
354 print_node_brief (file, "", c->object_type, 0);
355 fprintf (file, "}\n");
359 /* Print the stack of parsing contexts to FILE starting with FIRST. */
362 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
365 cp_parser_context *c;
367 fprintf (file, "Parsing context stack:\n");
368 for (i = 0, c = first; c; c = c->next, i++)
370 fprintf (file, "\t#%u: ", i);
371 cp_debug_print_context (file, c);
376 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
379 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
382 fprintf (file, "%s: true\n", desc);
386 /* Print an unparsed function entry UF to FILE. */
389 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
392 cp_default_arg_entry *default_arg_fn;
395 fprintf (file, "\tFunctions with default args:\n");
397 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
401 fprintf (file, "\t\tClass type: ");
402 print_node_brief (file, "", default_arg_fn->class_type, 0);
403 fprintf (file, "\t\tDeclaration: ");
404 print_node_brief (file, "", default_arg_fn->decl, 0);
405 fprintf (file, "\n");
408 fprintf (file, "\n\tFunctions with definitions that require "
409 "post-processing\n\t\t");
410 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
412 print_node_brief (file, "", fn, 0);
415 fprintf (file, "\n");
417 fprintf (file, "\n\tNon-static data members with initializers that require "
418 "post-processing\n\t\t");
419 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
421 print_node_brief (file, "", fn, 0);
424 fprintf (file, "\n");
428 /* Print the stack of unparsed member functions S to FILE. */
431 cp_debug_print_unparsed_queues (FILE *file,
432 VEC(cp_unparsed_functions_entry, gc) *s)
435 cp_unparsed_functions_entry *uf;
437 fprintf (file, "Unparsed functions\n");
438 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
440 fprintf (file, "#%u:\n", i);
441 cp_debug_print_unparsed_function (file, uf);
446 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
447 the given PARSER. If FILE is NULL, the output is printed on stderr. */
450 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
452 cp_token *next_token, *first_token, *start_token;
457 next_token = parser->lexer->next_token;
458 first_token = VEC_address (cp_token, parser->lexer->buffer);
459 start_token = (next_token > first_token + window_size / 2)
460 ? next_token - window_size / 2
462 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
467 /* Dump debugging information for the given PARSER. If FILE is NULL,
468 the output is printed on stderr. */
471 cp_debug_parser (FILE *file, cp_parser *parser)
473 const size_t window_size = 20;
475 expanded_location eloc;
480 fprintf (file, "Parser state\n\n");
481 fprintf (file, "Number of tokens: %u\n",
482 VEC_length (cp_token, parser->lexer->buffer));
483 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
484 cp_debug_print_tree_if_set (file, "Object scope",
485 parser->object_scope);
486 cp_debug_print_tree_if_set (file, "Qualifying scope",
487 parser->qualifying_scope);
488 cp_debug_print_context_stack (file, parser->context);
489 cp_debug_print_flag (file, "Allow GNU extensions",
490 parser->allow_gnu_extensions_p);
491 cp_debug_print_flag (file, "'>' token is greater-than",
492 parser->greater_than_is_operator_p);
493 cp_debug_print_flag (file, "Default args allowed in current "
494 "parameter list", parser->default_arg_ok_p);
495 cp_debug_print_flag (file, "Parsing integral constant-expression",
496 parser->integral_constant_expression_p);
497 cp_debug_print_flag (file, "Allow non-constant expression in current "
498 "constant-expression",
499 parser->allow_non_integral_constant_expression_p);
500 cp_debug_print_flag (file, "Seen non-constant expression",
501 parser->non_integral_constant_expression_p);
502 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
504 parser->local_variables_forbidden_p);
505 cp_debug_print_flag (file, "In unbraced linkage specification",
506 parser->in_unbraced_linkage_specification_p);
507 cp_debug_print_flag (file, "Parsing a declarator",
508 parser->in_declarator_p);
509 cp_debug_print_flag (file, "In template argument list",
510 parser->in_template_argument_list_p);
511 cp_debug_print_flag (file, "Parsing an iteration statement",
512 parser->in_statement & IN_ITERATION_STMT);
513 cp_debug_print_flag (file, "Parsing a switch statement",
514 parser->in_statement & IN_SWITCH_STMT);
515 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
516 parser->in_statement & IN_OMP_BLOCK);
517 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
518 parser->in_statement & IN_OMP_FOR);
519 cp_debug_print_flag (file, "Parsing an if statement",
520 parser->in_statement & IN_IF_STMT);
521 cp_debug_print_flag (file, "Parsing a type-id in an expression "
522 "context", parser->in_type_id_in_expr_p);
523 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
524 parser->implicit_extern_c);
525 cp_debug_print_flag (file, "String expressions should be translated "
526 "to execution character set",
527 parser->translate_strings_p);
528 cp_debug_print_flag (file, "Parsing function body outside of a "
529 "local class", parser->in_function_body);
530 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
531 parser->colon_corrects_to_scope_p);
532 if (parser->type_definition_forbidden_message)
533 fprintf (file, "Error message for forbidden type definitions: %s\n",
534 parser->type_definition_forbidden_message);
535 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
536 fprintf (file, "Number of class definitions in progress: %u\n",
537 parser->num_classes_being_defined);
538 fprintf (file, "Number of template parameter lists for the current "
539 "declaration: %u\n", parser->num_template_parameter_lists);
540 cp_debug_parser_tokens (file, parser, window_size);
541 token = parser->lexer->next_token;
542 fprintf (file, "Next token to parse:\n");
543 fprintf (file, "\tToken: ");
544 cp_lexer_print_token (file, token);
545 eloc = expand_location (token->location);
546 fprintf (file, "\n\tFile: %s\n", eloc.file);
547 fprintf (file, "\tLine: %d\n", eloc.line);
548 fprintf (file, "\tColumn: %d\n", eloc.column);
552 /* Allocate memory for a new lexer object and return it. */
555 cp_lexer_alloc (void)
559 c_common_no_more_pch ();
561 /* Allocate the memory. */
562 lexer = ggc_alloc_cleared_cp_lexer ();
564 /* Initially we are not debugging. */
565 lexer->debugging_p = false;
567 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
568 CP_SAVED_TOKEN_STACK);
570 /* Create the buffer. */
571 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
577 /* Create a new main C++ lexer, the lexer that gets tokens from the
581 cp_lexer_new_main (void)
586 /* It's possible that parsing the first pragma will load a PCH file,
587 which is a GC collection point. So we have to do that before
588 allocating any memory. */
589 cp_parser_initial_pragma (&token);
591 lexer = cp_lexer_alloc ();
593 /* Put the first token in the buffer. */
594 VEC_quick_push (cp_token, lexer->buffer, &token);
596 /* Get the remaining tokens from the preprocessor. */
597 while (token.type != CPP_EOF)
599 cp_lexer_get_preprocessor_token (lexer, &token);
600 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
603 lexer->last_token = VEC_address (cp_token, lexer->buffer)
604 + VEC_length (cp_token, lexer->buffer)
606 lexer->next_token = VEC_length (cp_token, lexer->buffer)
607 ? VEC_address (cp_token, lexer->buffer)
610 /* Subsequent preprocessor diagnostics should use compiler
611 diagnostic functions to get the compiler source location. */
614 gcc_assert (!lexer->next_token->purged_p);
618 /* Create a new lexer whose token stream is primed with the tokens in
619 CACHE. When these tokens are exhausted, no new tokens will be read. */
622 cp_lexer_new_from_tokens (cp_token_cache *cache)
624 cp_token *first = cache->first;
625 cp_token *last = cache->last;
626 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
628 /* We do not own the buffer. */
629 lexer->buffer = NULL;
630 lexer->next_token = first == last ? &eof_token : first;
631 lexer->last_token = last;
633 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
634 CP_SAVED_TOKEN_STACK);
636 /* Initially we are not debugging. */
637 lexer->debugging_p = false;
639 gcc_assert (!lexer->next_token->purged_p);
643 /* Frees all resources associated with LEXER. */
646 cp_lexer_destroy (cp_lexer *lexer)
648 VEC_free (cp_token, gc, lexer->buffer);
649 VEC_free (cp_token_position, heap, lexer->saved_tokens);
653 /* Returns nonzero if debugging information should be output. */
656 cp_lexer_debugging_p (cp_lexer *lexer)
658 return lexer->debugging_p;
662 static inline cp_token_position
663 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
665 gcc_assert (!previous_p || lexer->next_token != &eof_token);
667 return lexer->next_token - previous_p;
670 static inline cp_token *
671 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
677 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
679 lexer->next_token = cp_lexer_token_at (lexer, pos);
682 static inline cp_token_position
683 cp_lexer_previous_token_position (cp_lexer *lexer)
685 if (lexer->next_token == &eof_token)
686 return lexer->last_token - 1;
688 return cp_lexer_token_position (lexer, true);
691 static inline cp_token *
692 cp_lexer_previous_token (cp_lexer *lexer)
694 cp_token_position tp = cp_lexer_previous_token_position (lexer);
696 return cp_lexer_token_at (lexer, tp);
699 /* nonzero if we are presently saving tokens. */
702 cp_lexer_saving_tokens (const cp_lexer* lexer)
704 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
707 /* Store the next token from the preprocessor in *TOKEN. Return true
708 if we reach EOF. If LEXER is NULL, assume we are handling an
709 initial #pragma pch_preprocess, and thus want the lexer to return
710 processed strings. */
713 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
715 static int is_extern_c = 0;
717 /* Get a new token from the preprocessor. */
719 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
720 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
721 token->keyword = RID_MAX;
722 token->pragma_kind = PRAGMA_NONE;
723 token->purged_p = false;
725 /* On some systems, some header files are surrounded by an
726 implicit extern "C" block. Set a flag in the token if it
727 comes from such a header. */
728 is_extern_c += pending_lang_change;
729 pending_lang_change = 0;
730 token->implicit_extern_c = is_extern_c > 0;
732 /* Check to see if this token is a keyword. */
733 if (token->type == CPP_NAME)
735 if (C_IS_RESERVED_WORD (token->u.value))
737 /* Mark this token as a keyword. */
738 token->type = CPP_KEYWORD;
739 /* Record which keyword. */
740 token->keyword = C_RID_CODE (token->u.value);
744 if (warn_cxx0x_compat
745 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
746 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
748 /* Warn about the C++0x keyword (but still treat it as
750 warning (OPT_Wc__0x_compat,
751 "identifier %qE is a keyword in C++11",
754 /* Clear out the C_RID_CODE so we don't warn about this
755 particular identifier-turned-keyword again. */
756 C_SET_RID_CODE (token->u.value, RID_MAX);
759 token->ambiguous_p = false;
760 token->keyword = RID_MAX;
763 else if (token->type == CPP_AT_NAME)
765 /* This only happens in Objective-C++; it must be a keyword. */
766 token->type = CPP_KEYWORD;
767 switch (C_RID_CODE (token->u.value))
769 /* Replace 'class' with '@class', 'private' with '@private',
770 etc. This prevents confusion with the C++ keyword
771 'class', and makes the tokens consistent with other
772 Objective-C 'AT' keywords. For example '@class' is
773 reported as RID_AT_CLASS which is consistent with
774 '@synchronized', which is reported as
777 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
778 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
779 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
780 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
781 case RID_THROW: token->keyword = RID_AT_THROW; break;
782 case RID_TRY: token->keyword = RID_AT_TRY; break;
783 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
784 default: token->keyword = C_RID_CODE (token->u.value);
787 else if (token->type == CPP_PRAGMA)
789 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
790 token->pragma_kind = ((enum pragma_kind)
791 TREE_INT_CST_LOW (token->u.value));
792 token->u.value = NULL_TREE;
796 /* Update the globals input_location and the input file stack from TOKEN. */
798 cp_lexer_set_source_position_from_token (cp_token *token)
800 if (token->type != CPP_EOF)
802 input_location = token->location;
806 /* Return a pointer to the next token in the token stream, but do not
809 static inline cp_token *
810 cp_lexer_peek_token (cp_lexer *lexer)
812 if (cp_lexer_debugging_p (lexer))
814 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
815 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
816 putc ('\n', cp_lexer_debug_stream);
818 return lexer->next_token;
821 /* Return true if the next token has the indicated TYPE. */
824 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
826 return cp_lexer_peek_token (lexer)->type == type;
829 /* Return true if the next token does not have the indicated TYPE. */
832 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
834 return !cp_lexer_next_token_is (lexer, type);
837 /* Return true if the next token is the indicated KEYWORD. */
840 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
842 return cp_lexer_peek_token (lexer)->keyword == keyword;
845 /* Return true if the next token is not the indicated KEYWORD. */
848 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
850 return cp_lexer_peek_token (lexer)->keyword != keyword;
853 /* Return true if the next token is a keyword for a decl-specifier. */
856 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
860 token = cp_lexer_peek_token (lexer);
861 switch (token->keyword)
863 /* auto specifier: storage-class-specifier in C++,
864 simple-type-specifier in C++0x. */
866 /* Storage classes. */
872 /* Elaborated type specifiers. */
878 /* Simple type specifiers. */
893 /* GNU extensions. */
896 /* C++0x extensions. */
898 case RID_UNDERLYING_TYPE:
906 /* Returns TRUE iff the token T begins a decltype type. */
909 token_is_decltype (cp_token *t)
911 return (t->keyword == RID_DECLTYPE
912 || t->type == CPP_DECLTYPE);
915 /* Returns TRUE iff the next token begins a decltype type. */
918 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
920 cp_token *t = cp_lexer_peek_token (lexer);
921 return token_is_decltype (t);
924 /* Return a pointer to the Nth token in the token stream. If N is 1,
925 then this is precisely equivalent to cp_lexer_peek_token (except
926 that it is not inline). One would like to disallow that case, but
927 there is one case (cp_parser_nth_token_starts_template_id) where
928 the caller passes a variable for N and it might be 1. */
931 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
935 /* N is 1-based, not zero-based. */
938 if (cp_lexer_debugging_p (lexer))
939 fprintf (cp_lexer_debug_stream,
940 "cp_lexer: peeking ahead %ld at token: ", (long)n);
943 token = lexer->next_token;
944 gcc_assert (!n || token != &eof_token);
948 if (token == lexer->last_token)
954 if (!token->purged_p)
958 if (cp_lexer_debugging_p (lexer))
960 cp_lexer_print_token (cp_lexer_debug_stream, token);
961 putc ('\n', cp_lexer_debug_stream);
967 /* Return the next token, and advance the lexer's next_token pointer
968 to point to the next non-purged token. */
971 cp_lexer_consume_token (cp_lexer* lexer)
973 cp_token *token = lexer->next_token;
975 gcc_assert (token != &eof_token);
976 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
981 if (lexer->next_token == lexer->last_token)
983 lexer->next_token = &eof_token;
988 while (lexer->next_token->purged_p);
990 cp_lexer_set_source_position_from_token (token);
992 /* Provide debugging output. */
993 if (cp_lexer_debugging_p (lexer))
995 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
996 cp_lexer_print_token (cp_lexer_debug_stream, token);
997 putc ('\n', cp_lexer_debug_stream);
1003 /* Permanently remove the next token from the token stream, and
1004 advance the next_token pointer to refer to the next non-purged
1008 cp_lexer_purge_token (cp_lexer *lexer)
1010 cp_token *tok = lexer->next_token;
1012 gcc_assert (tok != &eof_token);
1013 tok->purged_p = true;
1014 tok->location = UNKNOWN_LOCATION;
1015 tok->u.value = NULL_TREE;
1016 tok->keyword = RID_MAX;
1021 if (tok == lexer->last_token)
1027 while (tok->purged_p);
1028 lexer->next_token = tok;
1031 /* Permanently remove all tokens after TOK, up to, but not
1032 including, the token that will be returned next by
1033 cp_lexer_peek_token. */
1036 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1038 cp_token *peek = lexer->next_token;
1040 if (peek == &eof_token)
1041 peek = lexer->last_token;
1043 gcc_assert (tok < peek);
1045 for ( tok += 1; tok != peek; tok += 1)
1047 tok->purged_p = true;
1048 tok->location = UNKNOWN_LOCATION;
1049 tok->u.value = NULL_TREE;
1050 tok->keyword = RID_MAX;
1054 /* Begin saving tokens. All tokens consumed after this point will be
1058 cp_lexer_save_tokens (cp_lexer* lexer)
1060 /* Provide debugging output. */
1061 if (cp_lexer_debugging_p (lexer))
1062 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1064 VEC_safe_push (cp_token_position, heap,
1065 lexer->saved_tokens, lexer->next_token);
1068 /* Commit to the portion of the token stream most recently saved. */
1071 cp_lexer_commit_tokens (cp_lexer* lexer)
1073 /* Provide debugging output. */
1074 if (cp_lexer_debugging_p (lexer))
1075 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1077 VEC_pop (cp_token_position, lexer->saved_tokens);
1080 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1081 to the token stream. Stop saving tokens. */
1084 cp_lexer_rollback_tokens (cp_lexer* lexer)
1086 /* Provide debugging output. */
1087 if (cp_lexer_debugging_p (lexer))
1088 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1090 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1093 /* Print a representation of the TOKEN on the STREAM. */
1096 cp_lexer_print_token (FILE * stream, cp_token *token)
1098 /* We don't use cpp_type2name here because the parser defines
1099 a few tokens of its own. */
1100 static const char *const token_names[] = {
1101 /* cpplib-defined token types */
1102 #define OP(e, s) #e,
1103 #define TK(e, s) #e,
1107 /* C++ parser token types - see "Manifest constants", above. */
1110 "NESTED_NAME_SPECIFIER",
1113 /* For some tokens, print the associated data. */
1114 switch (token->type)
1117 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1118 For example, `struct' is mapped to an INTEGER_CST. */
1119 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1121 /* else fall through */
1123 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1130 case CPP_UTF8STRING:
1131 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1135 print_generic_expr (stream, token->u.value, 0);
1139 /* If we have a name for the token, print it out. Otherwise, we
1140 simply give the numeric code. */
1141 if (token->type < ARRAY_SIZE(token_names))
1142 fputs (token_names[token->type], stream);
1144 fprintf (stream, "[%d]", token->type);
1149 /* Start emitting debugging information. */
1152 cp_lexer_start_debugging (cp_lexer* lexer)
1154 lexer->debugging_p = true;
1155 cp_lexer_debug_stream = stderr;
1158 /* Stop emitting debugging information. */
1161 cp_lexer_stop_debugging (cp_lexer* lexer)
1163 lexer->debugging_p = false;
1164 cp_lexer_debug_stream = NULL;
1167 /* Create a new cp_token_cache, representing a range of tokens. */
1169 static cp_token_cache *
1170 cp_token_cache_new (cp_token *first, cp_token *last)
1172 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1173 cache->first = first;
1179 /* Decl-specifiers. */
1181 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1184 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1186 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1191 /* Nothing other than the parser should be creating declarators;
1192 declarators are a semi-syntactic representation of C++ entities.
1193 Other parts of the front end that need to create entities (like
1194 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1196 static cp_declarator *make_call_declarator
1197 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1198 static cp_declarator *make_array_declarator
1199 (cp_declarator *, tree);
1200 static cp_declarator *make_pointer_declarator
1201 (cp_cv_quals, cp_declarator *);
1202 static cp_declarator *make_reference_declarator
1203 (cp_cv_quals, cp_declarator *, bool);
1204 static cp_parameter_declarator *make_parameter_declarator
1205 (cp_decl_specifier_seq *, cp_declarator *, tree);
1206 static cp_declarator *make_ptrmem_declarator
1207 (cp_cv_quals, tree, cp_declarator *);
1209 /* An erroneous declarator. */
1210 static cp_declarator *cp_error_declarator;
1212 /* The obstack on which declarators and related data structures are
1214 static struct obstack declarator_obstack;
1216 /* Alloc BYTES from the declarator memory pool. */
1218 static inline void *
1219 alloc_declarator (size_t bytes)
1221 return obstack_alloc (&declarator_obstack, bytes);
1224 /* Allocate a declarator of the indicated KIND. Clear fields that are
1225 common to all declarators. */
1227 static cp_declarator *
1228 make_declarator (cp_declarator_kind kind)
1230 cp_declarator *declarator;
1232 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1233 declarator->kind = kind;
1234 declarator->attributes = NULL_TREE;
1235 declarator->declarator = NULL;
1236 declarator->parameter_pack_p = false;
1237 declarator->id_loc = UNKNOWN_LOCATION;
1242 /* Make a declarator for a generalized identifier. If
1243 QUALIFYING_SCOPE is non-NULL, the identifier is
1244 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1245 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1248 static cp_declarator *
1249 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1250 special_function_kind sfk)
1252 cp_declarator *declarator;
1254 /* It is valid to write:
1256 class C { void f(); };
1260 The standard is not clear about whether `typedef const C D' is
1261 legal; as of 2002-09-15 the committee is considering that
1262 question. EDG 3.0 allows that syntax. Therefore, we do as
1264 if (qualifying_scope && TYPE_P (qualifying_scope))
1265 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1268 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1269 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271 declarator = make_declarator (cdk_id);
1272 declarator->u.id.qualifying_scope = qualifying_scope;
1273 declarator->u.id.unqualified_name = unqualified_name;
1274 declarator->u.id.sfk = sfk;
1279 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1280 of modifiers such as const or volatile to apply to the pointer
1281 type, represented as identifiers. */
1284 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1286 cp_declarator *declarator;
1288 declarator = make_declarator (cdk_pointer);
1289 declarator->declarator = target;
1290 declarator->u.pointer.qualifiers = cv_qualifiers;
1291 declarator->u.pointer.class_type = NULL_TREE;
1294 declarator->id_loc = target->id_loc;
1295 declarator->parameter_pack_p = target->parameter_pack_p;
1296 target->parameter_pack_p = false;
1299 declarator->parameter_pack_p = false;
1304 /* Like make_pointer_declarator -- but for references. */
1307 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1310 cp_declarator *declarator;
1312 declarator = make_declarator (cdk_reference);
1313 declarator->declarator = target;
1314 declarator->u.reference.qualifiers = cv_qualifiers;
1315 declarator->u.reference.rvalue_ref = rvalue_ref;
1318 declarator->id_loc = target->id_loc;
1319 declarator->parameter_pack_p = target->parameter_pack_p;
1320 target->parameter_pack_p = false;
1323 declarator->parameter_pack_p = false;
1328 /* Like make_pointer_declarator -- but for a pointer to a non-static
1329 member of CLASS_TYPE. */
1332 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1333 cp_declarator *pointee)
1335 cp_declarator *declarator;
1337 declarator = make_declarator (cdk_ptrmem);
1338 declarator->declarator = pointee;
1339 declarator->u.pointer.qualifiers = cv_qualifiers;
1340 declarator->u.pointer.class_type = class_type;
1344 declarator->parameter_pack_p = pointee->parameter_pack_p;
1345 pointee->parameter_pack_p = false;
1348 declarator->parameter_pack_p = false;
1353 /* Make a declarator for the function given by TARGET, with the
1354 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1355 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1356 indicates what exceptions can be thrown. */
1359 make_call_declarator (cp_declarator *target,
1361 cp_cv_quals cv_qualifiers,
1362 cp_virt_specifiers virt_specifiers,
1363 tree exception_specification,
1364 tree late_return_type)
1366 cp_declarator *declarator;
1368 declarator = make_declarator (cdk_function);
1369 declarator->declarator = target;
1370 declarator->u.function.parameters = parms;
1371 declarator->u.function.qualifiers = cv_qualifiers;
1372 declarator->u.function.virt_specifiers = virt_specifiers;
1373 declarator->u.function.exception_specification = exception_specification;
1374 declarator->u.function.late_return_type = late_return_type;
1377 declarator->id_loc = target->id_loc;
1378 declarator->parameter_pack_p = target->parameter_pack_p;
1379 target->parameter_pack_p = false;
1382 declarator->parameter_pack_p = false;
1387 /* Make a declarator for an array of BOUNDS elements, each of which is
1388 defined by ELEMENT. */
1391 make_array_declarator (cp_declarator *element, tree bounds)
1393 cp_declarator *declarator;
1395 declarator = make_declarator (cdk_array);
1396 declarator->declarator = element;
1397 declarator->u.array.bounds = bounds;
1400 declarator->id_loc = element->id_loc;
1401 declarator->parameter_pack_p = element->parameter_pack_p;
1402 element->parameter_pack_p = false;
1405 declarator->parameter_pack_p = false;
1410 /* Determine whether the declarator we've seen so far can be a
1411 parameter pack, when followed by an ellipsis. */
1413 declarator_can_be_parameter_pack (cp_declarator *declarator)
1415 /* Search for a declarator name, or any other declarator that goes
1416 after the point where the ellipsis could appear in a parameter
1417 pack. If we find any of these, then this declarator can not be
1418 made into a parameter pack. */
1420 while (declarator && !found)
1422 switch ((int)declarator->kind)
1433 declarator = declarator->declarator;
1441 cp_parameter_declarator *no_parameters;
1443 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1444 DECLARATOR and DEFAULT_ARGUMENT. */
1446 cp_parameter_declarator *
1447 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1448 cp_declarator *declarator,
1449 tree default_argument)
1451 cp_parameter_declarator *parameter;
1453 parameter = ((cp_parameter_declarator *)
1454 alloc_declarator (sizeof (cp_parameter_declarator)));
1455 parameter->next = NULL;
1456 if (decl_specifiers)
1457 parameter->decl_specifiers = *decl_specifiers;
1459 clear_decl_specs (¶meter->decl_specifiers);
1460 parameter->declarator = declarator;
1461 parameter->default_argument = default_argument;
1462 parameter->ellipsis_p = false;
1467 /* Returns true iff DECLARATOR is a declaration for a function. */
1470 function_declarator_p (const cp_declarator *declarator)
1474 if (declarator->kind == cdk_function
1475 && declarator->declarator->kind == cdk_id)
1477 if (declarator->kind == cdk_id
1478 || declarator->kind == cdk_error)
1480 declarator = declarator->declarator;
1490 A cp_parser parses the token stream as specified by the C++
1491 grammar. Its job is purely parsing, not semantic analysis. For
1492 example, the parser breaks the token stream into declarators,
1493 expressions, statements, and other similar syntactic constructs.
1494 It does not check that the types of the expressions on either side
1495 of an assignment-statement are compatible, or that a function is
1496 not declared with a parameter of type `void'.
1498 The parser invokes routines elsewhere in the compiler to perform
1499 semantic analysis and to build up the abstract syntax tree for the
1502 The parser (and the template instantiation code, which is, in a
1503 way, a close relative of parsing) are the only parts of the
1504 compiler that should be calling push_scope and pop_scope, or
1505 related functions. The parser (and template instantiation code)
1506 keeps track of what scope is presently active; everything else
1507 should simply honor that. (The code that generates static
1508 initializers may also need to set the scope, in order to check
1509 access control correctly when emitting the initializers.)
1514 The parser is of the standard recursive-descent variety. Upcoming
1515 tokens in the token stream are examined in order to determine which
1516 production to use when parsing a non-terminal. Some C++ constructs
1517 require arbitrary look ahead to disambiguate. For example, it is
1518 impossible, in the general case, to tell whether a statement is an
1519 expression or declaration without scanning the entire statement.
1520 Therefore, the parser is capable of "parsing tentatively." When the
1521 parser is not sure what construct comes next, it enters this mode.
1522 Then, while we attempt to parse the construct, the parser queues up
1523 error messages, rather than issuing them immediately, and saves the
1524 tokens it consumes. If the construct is parsed successfully, the
1525 parser "commits", i.e., it issues any queued error messages and
1526 the tokens that were being preserved are permanently discarded.
1527 If, however, the construct is not parsed successfully, the parser
1528 rolls back its state completely so that it can resume parsing using
1529 a different alternative.
1534 The performance of the parser could probably be improved substantially.
1535 We could often eliminate the need to parse tentatively by looking ahead
1536 a little bit. In some places, this approach might not entirely eliminate
1537 the need to parse tentatively, but it might still speed up the average
1540 /* Flags that are passed to some parsing functions. These values can
1541 be bitwise-ored together. */
1546 CP_PARSER_FLAGS_NONE = 0x0,
1547 /* The construct is optional. If it is not present, then no error
1548 should be issued. */
1549 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1550 /* When parsing a type-specifier, treat user-defined type-names
1551 as non-type identifiers. */
1552 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1553 /* When parsing a type-specifier, do not try to parse a class-specifier
1554 or enum-specifier. */
1555 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1556 /* When parsing a decl-specifier-seq, only allow type-specifier or
1558 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1561 /* This type is used for parameters and variables which hold
1562 combinations of the above flags. */
1563 typedef int cp_parser_flags;
1565 /* The different kinds of declarators we want to parse. */
1567 typedef enum cp_parser_declarator_kind
1569 /* We want an abstract declarator. */
1570 CP_PARSER_DECLARATOR_ABSTRACT,
1571 /* We want a named declarator. */
1572 CP_PARSER_DECLARATOR_NAMED,
1573 /* We don't mind, but the name must be an unqualified-id. */
1574 CP_PARSER_DECLARATOR_EITHER
1575 } cp_parser_declarator_kind;
1577 /* The precedence values used to parse binary expressions. The minimum value
1578 of PREC must be 1, because zero is reserved to quickly discriminate
1579 binary operators from other tokens. */
1584 PREC_LOGICAL_OR_EXPRESSION,
1585 PREC_LOGICAL_AND_EXPRESSION,
1586 PREC_INCLUSIVE_OR_EXPRESSION,
1587 PREC_EXCLUSIVE_OR_EXPRESSION,
1588 PREC_AND_EXPRESSION,
1589 PREC_EQUALITY_EXPRESSION,
1590 PREC_RELATIONAL_EXPRESSION,
1591 PREC_SHIFT_EXPRESSION,
1592 PREC_ADDITIVE_EXPRESSION,
1593 PREC_MULTIPLICATIVE_EXPRESSION,
1595 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1598 /* A mapping from a token type to a corresponding tree node type, with a
1599 precedence value. */
1601 typedef struct cp_parser_binary_operations_map_node
1603 /* The token type. */
1604 enum cpp_ttype token_type;
1605 /* The corresponding tree code. */
1606 enum tree_code tree_type;
1607 /* The precedence of this operator. */
1608 enum cp_parser_prec prec;
1609 } cp_parser_binary_operations_map_node;
1611 typedef struct cp_parser_expression_stack_entry
1613 /* Left hand side of the binary operation we are currently
1616 /* Original tree code for left hand side, if it was a binary
1617 expression itself (used for -Wparentheses). */
1618 enum tree_code lhs_type;
1619 /* Tree code for the binary operation we are parsing. */
1620 enum tree_code tree_type;
1621 /* Precedence of the binary operation we are parsing. */
1622 enum cp_parser_prec prec;
1623 /* Location of the binary operation we are parsing. */
1625 } cp_parser_expression_stack_entry;
1627 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1628 entries because precedence levels on the stack are monotonically
1630 typedef struct cp_parser_expression_stack_entry
1631 cp_parser_expression_stack[NUM_PREC_VALUES];
1635 /* Constructors and destructors. */
1637 static cp_parser_context *cp_parser_context_new
1638 (cp_parser_context *);
1640 /* Class variables. */
1642 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1644 /* The operator-precedence table used by cp_parser_binary_expression.
1645 Transformed into an associative array (binops_by_token) by
1648 static const cp_parser_binary_operations_map_node binops[] = {
1649 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1650 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1652 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1656 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1659 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1662 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1664 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1667 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1668 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1670 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1672 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1674 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1676 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1678 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1681 /* The same as binops, but initialized by cp_parser_new so that
1682 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1684 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1686 /* Constructors and destructors. */
1688 /* Construct a new context. The context below this one on the stack
1689 is given by NEXT. */
1691 static cp_parser_context *
1692 cp_parser_context_new (cp_parser_context* next)
1694 cp_parser_context *context;
1696 /* Allocate the storage. */
1697 if (cp_parser_context_free_list != NULL)
1699 /* Pull the first entry from the free list. */
1700 context = cp_parser_context_free_list;
1701 cp_parser_context_free_list = context->next;
1702 memset (context, 0, sizeof (*context));
1705 context = ggc_alloc_cleared_cp_parser_context ();
1707 /* No errors have occurred yet in this context. */
1708 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1709 /* If this is not the bottommost context, copy information that we
1710 need from the previous context. */
1713 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1714 expression, then we are parsing one in this context, too. */
1715 context->object_type = next->object_type;
1716 /* Thread the stack. */
1717 context->next = next;
1723 /* Managing the unparsed function queues. */
1725 #define unparsed_funs_with_default_args \
1726 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1727 #define unparsed_funs_with_definitions \
1728 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1729 #define unparsed_nsdmis \
1730 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1733 push_unparsed_function_queues (cp_parser *parser)
1735 VEC_safe_push (cp_unparsed_functions_entry, gc,
1736 parser->unparsed_queues, NULL);
1737 unparsed_funs_with_default_args = NULL;
1738 unparsed_funs_with_definitions = make_tree_vector ();
1739 unparsed_nsdmis = NULL;
1743 pop_unparsed_function_queues (cp_parser *parser)
1745 release_tree_vector (unparsed_funs_with_definitions);
1746 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1751 /* Constructors and destructors. */
1753 static cp_parser *cp_parser_new
1756 /* Routines to parse various constructs.
1758 Those that return `tree' will return the error_mark_node (rather
1759 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1760 Sometimes, they will return an ordinary node if error-recovery was
1761 attempted, even though a parse error occurred. So, to check
1762 whether or not a parse error occurred, you should always use
1763 cp_parser_error_occurred. If the construct is optional (indicated
1764 either by an `_opt' in the name of the function that does the
1765 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1766 the construct is not present. */
1768 /* Lexical conventions [gram.lex] */
1770 static tree cp_parser_identifier
1772 static tree cp_parser_string_literal
1773 (cp_parser *, bool, bool);
1774 static tree cp_parser_userdef_char_literal
1776 static tree cp_parser_userdef_string_literal
1778 static tree cp_parser_userdef_numeric_literal
1781 /* Basic concepts [gram.basic] */
1783 static bool cp_parser_translation_unit
1786 /* Expressions [gram.expr] */
1788 static tree cp_parser_primary_expression
1789 (cp_parser *, bool, bool, bool, cp_id_kind *);
1790 static tree cp_parser_id_expression
1791 (cp_parser *, bool, bool, bool *, bool, bool);
1792 static tree cp_parser_unqualified_id
1793 (cp_parser *, bool, bool, bool, bool);
1794 static tree cp_parser_nested_name_specifier_opt
1795 (cp_parser *, bool, bool, bool, bool);
1796 static tree cp_parser_nested_name_specifier
1797 (cp_parser *, bool, bool, bool, bool);
1798 static tree cp_parser_qualifying_entity
1799 (cp_parser *, bool, bool, bool, bool, bool);
1800 static tree cp_parser_postfix_expression
1801 (cp_parser *, bool, bool, bool, cp_id_kind *);
1802 static tree cp_parser_postfix_open_square_expression
1803 (cp_parser *, tree, bool);
1804 static tree cp_parser_postfix_dot_deref_expression
1805 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1806 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1807 (cp_parser *, int, bool, bool, bool *);
1808 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1809 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1810 static void cp_parser_pseudo_destructor_name
1811 (cp_parser *, tree *, tree *);
1812 static tree cp_parser_unary_expression
1813 (cp_parser *, bool, bool, cp_id_kind *);
1814 static enum tree_code cp_parser_unary_operator
1816 static tree cp_parser_new_expression
1818 static VEC(tree,gc) *cp_parser_new_placement
1820 static tree cp_parser_new_type_id
1821 (cp_parser *, tree *);
1822 static cp_declarator *cp_parser_new_declarator_opt
1824 static cp_declarator *cp_parser_direct_new_declarator
1826 static VEC(tree,gc) *cp_parser_new_initializer
1828 static tree cp_parser_delete_expression
1830 static tree cp_parser_cast_expression
1831 (cp_parser *, bool, bool, cp_id_kind *);
1832 static tree cp_parser_binary_expression
1833 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1834 static tree cp_parser_question_colon_clause
1835 (cp_parser *, tree);
1836 static tree cp_parser_assignment_expression
1837 (cp_parser *, bool, cp_id_kind *);
1838 static enum tree_code cp_parser_assignment_operator_opt
1840 static tree cp_parser_expression
1841 (cp_parser *, bool, cp_id_kind *);
1842 static tree cp_parser_constant_expression
1843 (cp_parser *, bool, bool *);
1844 static tree cp_parser_builtin_offsetof
1846 static tree cp_parser_lambda_expression
1848 static void cp_parser_lambda_introducer
1849 (cp_parser *, tree);
1850 static bool cp_parser_lambda_declarator_opt
1851 (cp_parser *, tree);
1852 static void cp_parser_lambda_body
1853 (cp_parser *, tree);
1855 /* Statements [gram.stmt.stmt] */
1857 static void cp_parser_statement
1858 (cp_parser *, tree, bool, bool *);
1859 static void cp_parser_label_for_labeled_statement
1861 static tree cp_parser_expression_statement
1862 (cp_parser *, tree);
1863 static tree cp_parser_compound_statement
1864 (cp_parser *, tree, bool, bool);
1865 static void cp_parser_statement_seq_opt
1866 (cp_parser *, tree);
1867 static tree cp_parser_selection_statement
1868 (cp_parser *, bool *);
1869 static tree cp_parser_condition
1871 static tree cp_parser_iteration_statement
1873 static bool cp_parser_for_init_statement
1874 (cp_parser *, tree *decl);
1875 static tree cp_parser_for
1877 static tree cp_parser_c_for
1878 (cp_parser *, tree, tree);
1879 static tree cp_parser_range_for
1880 (cp_parser *, tree, tree, tree);
1881 static void do_range_for_auto_deduction
1883 static tree cp_parser_perform_range_for_lookup
1884 (tree, tree *, tree *);
1885 static tree cp_parser_range_for_member_function
1887 static tree cp_parser_jump_statement
1889 static void cp_parser_declaration_statement
1892 static tree cp_parser_implicitly_scoped_statement
1893 (cp_parser *, bool *);
1894 static void cp_parser_already_scoped_statement
1897 /* Declarations [gram.dcl.dcl] */
1899 static void cp_parser_declaration_seq_opt
1901 static void cp_parser_declaration
1903 static void cp_parser_block_declaration
1904 (cp_parser *, bool);
1905 static void cp_parser_simple_declaration
1906 (cp_parser *, bool, tree *);
1907 static void cp_parser_decl_specifier_seq
1908 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1909 static tree cp_parser_storage_class_specifier_opt
1911 static tree cp_parser_function_specifier_opt
1912 (cp_parser *, cp_decl_specifier_seq *);
1913 static tree cp_parser_type_specifier
1914 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1916 static tree cp_parser_simple_type_specifier
1917 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1918 static tree cp_parser_type_name
1920 static tree cp_parser_nonclass_name
1921 (cp_parser* parser);
1922 static tree cp_parser_elaborated_type_specifier
1923 (cp_parser *, bool, bool);
1924 static tree cp_parser_enum_specifier
1926 static void cp_parser_enumerator_list
1927 (cp_parser *, tree);
1928 static void cp_parser_enumerator_definition
1929 (cp_parser *, tree);
1930 static tree cp_parser_namespace_name
1932 static void cp_parser_namespace_definition
1934 static void cp_parser_namespace_body
1936 static tree cp_parser_qualified_namespace_specifier
1938 static void cp_parser_namespace_alias_definition
1940 static bool cp_parser_using_declaration
1941 (cp_parser *, bool);
1942 static void cp_parser_using_directive
1944 static tree cp_parser_alias_declaration
1946 static void cp_parser_asm_definition
1948 static void cp_parser_linkage_specification
1950 static void cp_parser_static_assert
1951 (cp_parser *, bool);
1952 static tree cp_parser_decltype
1955 /* Declarators [gram.dcl.decl] */
1957 static tree cp_parser_init_declarator
1958 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1959 static cp_declarator *cp_parser_declarator
1960 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1961 static cp_declarator *cp_parser_direct_declarator
1962 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1963 static enum tree_code cp_parser_ptr_operator
1964 (cp_parser *, tree *, cp_cv_quals *);
1965 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1967 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1969 static tree cp_parser_late_return_type_opt
1970 (cp_parser *, cp_cv_quals);
1971 static tree cp_parser_declarator_id
1972 (cp_parser *, bool);
1973 static tree cp_parser_type_id
1975 static tree cp_parser_template_type_arg
1977 static tree cp_parser_trailing_type_id (cp_parser *);
1978 static tree cp_parser_type_id_1
1979 (cp_parser *, bool, bool);
1980 static void cp_parser_type_specifier_seq
1981 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1982 static tree cp_parser_parameter_declaration_clause
1984 static tree cp_parser_parameter_declaration_list
1985 (cp_parser *, bool *);
1986 static cp_parameter_declarator *cp_parser_parameter_declaration
1987 (cp_parser *, bool, bool *);
1988 static tree cp_parser_default_argument
1989 (cp_parser *, bool);
1990 static void cp_parser_function_body
1991 (cp_parser *, bool);
1992 static tree cp_parser_initializer
1993 (cp_parser *, bool *, bool *);
1994 static tree cp_parser_initializer_clause
1995 (cp_parser *, bool *);
1996 static tree cp_parser_braced_list
1997 (cp_parser*, bool*);
1998 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1999 (cp_parser *, bool *);
2001 static bool cp_parser_ctor_initializer_opt_and_function_body
2002 (cp_parser *, bool);
2004 /* Classes [gram.class] */
2006 static tree cp_parser_class_name
2007 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2008 static tree cp_parser_class_specifier
2010 static tree cp_parser_class_head
2011 (cp_parser *, bool *);
2012 static enum tag_types cp_parser_class_key
2014 static void cp_parser_member_specification_opt
2016 static void cp_parser_member_declaration
2018 static tree cp_parser_pure_specifier
2020 static tree cp_parser_constant_initializer
2023 /* Derived classes [gram.class.derived] */
2025 static tree cp_parser_base_clause
2027 static tree cp_parser_base_specifier
2030 /* Special member functions [gram.special] */
2032 static tree cp_parser_conversion_function_id
2034 static tree cp_parser_conversion_type_id
2036 static cp_declarator *cp_parser_conversion_declarator_opt
2038 static bool cp_parser_ctor_initializer_opt
2040 static void cp_parser_mem_initializer_list
2042 static tree cp_parser_mem_initializer
2044 static tree cp_parser_mem_initializer_id
2047 /* Overloading [gram.over] */
2049 static tree cp_parser_operator_function_id
2051 static tree cp_parser_operator
2054 /* Templates [gram.temp] */
2056 static void cp_parser_template_declaration
2057 (cp_parser *, bool);
2058 static tree cp_parser_template_parameter_list
2060 static tree cp_parser_template_parameter
2061 (cp_parser *, bool *, bool *);
2062 static tree cp_parser_type_parameter
2063 (cp_parser *, bool *);
2064 static tree cp_parser_template_id
2065 (cp_parser *, bool, bool, enum tag_types, bool);
2066 static tree cp_parser_template_name
2067 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2068 static tree cp_parser_template_argument_list
2070 static tree cp_parser_template_argument
2072 static void cp_parser_explicit_instantiation
2074 static void cp_parser_explicit_specialization
2077 /* Exception handling [gram.exception] */
2079 static tree cp_parser_try_block
2081 static bool cp_parser_function_try_block
2083 static void cp_parser_handler_seq
2085 static void cp_parser_handler
2087 static tree cp_parser_exception_declaration
2089 static tree cp_parser_throw_expression
2091 static tree cp_parser_exception_specification_opt
2093 static tree cp_parser_type_id_list
2096 /* GNU Extensions */
2098 static tree cp_parser_asm_specification_opt
2100 static tree cp_parser_asm_operand_list
2102 static tree cp_parser_asm_clobber_list
2104 static tree cp_parser_asm_label_list
2106 static tree cp_parser_attributes_opt
2108 static tree cp_parser_attribute_list
2110 static bool cp_parser_extension_opt
2111 (cp_parser *, int *);
2112 static void cp_parser_label_declaration
2115 /* Transactional Memory Extensions */
2117 static tree cp_parser_transaction
2118 (cp_parser *, enum rid);
2119 static tree cp_parser_transaction_expression
2120 (cp_parser *, enum rid);
2121 static bool cp_parser_function_transaction
2122 (cp_parser *, enum rid);
2123 static tree cp_parser_transaction_cancel
2126 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2127 static bool cp_parser_pragma
2128 (cp_parser *, enum pragma_context);
2130 /* Objective-C++ Productions */
2132 static tree cp_parser_objc_message_receiver
2134 static tree cp_parser_objc_message_args
2136 static tree cp_parser_objc_message_expression
2138 static tree cp_parser_objc_encode_expression
2140 static tree cp_parser_objc_defs_expression
2142 static tree cp_parser_objc_protocol_expression
2144 static tree cp_parser_objc_selector_expression
2146 static tree cp_parser_objc_expression
2148 static bool cp_parser_objc_selector_p
2150 static tree cp_parser_objc_selector
2152 static tree cp_parser_objc_protocol_refs_opt
2154 static void cp_parser_objc_declaration
2155 (cp_parser *, tree);
2156 static tree cp_parser_objc_statement
2158 static bool cp_parser_objc_valid_prefix_attributes
2159 (cp_parser *, tree *);
2160 static void cp_parser_objc_at_property_declaration
2162 static void cp_parser_objc_at_synthesize_declaration
2164 static void cp_parser_objc_at_dynamic_declaration
2166 static tree cp_parser_objc_struct_declaration
2169 /* Utility Routines */
2171 static tree cp_parser_lookup_name
2172 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2173 static tree cp_parser_lookup_name_simple
2174 (cp_parser *, tree, location_t);
2175 static tree cp_parser_maybe_treat_template_as_class
2177 static bool cp_parser_check_declarator_template_parameters
2178 (cp_parser *, cp_declarator *, location_t);
2179 static bool cp_parser_check_template_parameters
2180 (cp_parser *, unsigned, location_t, cp_declarator *);
2181 static tree cp_parser_simple_cast_expression
2183 static tree cp_parser_global_scope_opt
2184 (cp_parser *, bool);
2185 static bool cp_parser_constructor_declarator_p
2186 (cp_parser *, bool);
2187 static tree cp_parser_function_definition_from_specifiers_and_declarator
2188 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2189 static tree cp_parser_function_definition_after_declarator
2190 (cp_parser *, bool);
2191 static void cp_parser_template_declaration_after_export
2192 (cp_parser *, bool);
2193 static void cp_parser_perform_template_parameter_access_checks
2194 (VEC (deferred_access_check,gc)*);
2195 static tree cp_parser_single_declaration
2196 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2197 static tree cp_parser_functional_cast
2198 (cp_parser *, tree);
2199 static tree cp_parser_save_member_function_body
2200 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2201 static tree cp_parser_save_nsdmi
2203 static tree cp_parser_enclosed_template_argument_list
2205 static void cp_parser_save_default_args
2206 (cp_parser *, tree);
2207 static void cp_parser_late_parsing_for_member
2208 (cp_parser *, tree);
2209 static tree cp_parser_late_parse_one_default_arg
2210 (cp_parser *, tree, tree, tree);
2211 static void cp_parser_late_parsing_nsdmi
2212 (cp_parser *, tree);
2213 static void cp_parser_late_parsing_default_args
2214 (cp_parser *, tree);
2215 static tree cp_parser_sizeof_operand
2216 (cp_parser *, enum rid);
2217 static tree cp_parser_trait_expr
2218 (cp_parser *, enum rid);
2219 static bool cp_parser_declares_only_class_p
2221 static void cp_parser_set_storage_class
2222 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2223 static void cp_parser_set_decl_spec_type
2224 (cp_decl_specifier_seq *, tree, location_t, bool);
2225 static void set_and_check_decl_spec_loc
2226 (cp_decl_specifier_seq *decl_specs,
2227 cp_decl_spec ds, source_location location);
2228 static bool cp_parser_friend_p
2229 (const cp_decl_specifier_seq *);
2230 static void cp_parser_required_error
2231 (cp_parser *, required_token, bool);
2232 static cp_token *cp_parser_require
2233 (cp_parser *, enum cpp_ttype, required_token);
2234 static cp_token *cp_parser_require_keyword
2235 (cp_parser *, enum rid, required_token);
2236 static bool cp_parser_token_starts_function_definition_p
2238 static bool cp_parser_next_token_starts_class_definition_p
2240 static bool cp_parser_next_token_ends_template_argument_p
2242 static bool cp_parser_nth_token_starts_template_argument_list_p
2243 (cp_parser *, size_t);
2244 static enum tag_types cp_parser_token_is_class_key
2246 static void cp_parser_check_class_key
2247 (enum tag_types, tree type);
2248 static void cp_parser_check_access_in_redeclaration
2249 (tree type, location_t location);
2250 static bool cp_parser_optional_template_keyword
2252 static void cp_parser_pre_parsed_nested_name_specifier
2254 static bool cp_parser_cache_group
2255 (cp_parser *, enum cpp_ttype, unsigned);
2256 static tree cp_parser_cache_defarg
2257 (cp_parser *parser, bool nsdmi);
2258 static void cp_parser_parse_tentatively
2260 static void cp_parser_commit_to_tentative_parse
2262 static void cp_parser_abort_tentative_parse
2264 static bool cp_parser_parse_definitely
2266 static inline bool cp_parser_parsing_tentatively
2268 static bool cp_parser_uncommitted_to_tentative_parse_p
2270 static void cp_parser_error
2271 (cp_parser *, const char *);
2272 static void cp_parser_name_lookup_error
2273 (cp_parser *, tree, tree, name_lookup_error, location_t);
2274 static bool cp_parser_simulate_error
2276 static bool cp_parser_check_type_definition
2278 static void cp_parser_check_for_definition_in_return_type
2279 (cp_declarator *, tree, location_t type_location);
2280 static void cp_parser_check_for_invalid_template_id
2281 (cp_parser *, tree, enum tag_types, location_t location);
2282 static bool cp_parser_non_integral_constant_expression
2283 (cp_parser *, non_integral_constant);
2284 static void cp_parser_diagnose_invalid_type_name
2285 (cp_parser *, tree, tree, location_t);
2286 static bool cp_parser_parse_and_diagnose_invalid_type_name
2288 static int cp_parser_skip_to_closing_parenthesis
2289 (cp_parser *, bool, bool, bool);
2290 static void cp_parser_skip_to_end_of_statement
2292 static void cp_parser_consume_semicolon_at_end_of_statement
2294 static void cp_parser_skip_to_end_of_block_or_statement
2296 static bool cp_parser_skip_to_closing_brace
2298 static void cp_parser_skip_to_end_of_template_parameter_list
2300 static void cp_parser_skip_to_pragma_eol
2301 (cp_parser*, cp_token *);
2302 static bool cp_parser_error_occurred
2304 static bool cp_parser_allow_gnu_extensions_p
2306 static bool cp_parser_is_pure_string_literal
2308 static bool cp_parser_is_string_literal
2310 static bool cp_parser_is_keyword
2311 (cp_token *, enum rid);
2312 static tree cp_parser_make_typename_type
2313 (cp_parser *, tree, tree, location_t location);
2314 static cp_declarator * cp_parser_make_indirect_declarator
2315 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2317 /* Returns nonzero if we are parsing tentatively. */
2320 cp_parser_parsing_tentatively (cp_parser* parser)
2322 return parser->context->next != NULL;
2325 /* Returns nonzero if TOKEN is a string literal. */
2328 cp_parser_is_pure_string_literal (cp_token* token)
2330 return (token->type == CPP_STRING ||
2331 token->type == CPP_STRING16 ||
2332 token->type == CPP_STRING32 ||
2333 token->type == CPP_WSTRING ||
2334 token->type == CPP_UTF8STRING);
2337 /* Returns nonzero if TOKEN is a string literal
2338 of a user-defined string literal. */
2341 cp_parser_is_string_literal (cp_token* token)
2343 return (cp_parser_is_pure_string_literal (token) ||
2344 token->type == CPP_STRING_USERDEF ||
2345 token->type == CPP_STRING16_USERDEF ||
2346 token->type == CPP_STRING32_USERDEF ||
2347 token->type == CPP_WSTRING_USERDEF ||
2348 token->type == CPP_UTF8STRING_USERDEF);
2351 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2354 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2356 return token->keyword == keyword;
2359 /* If not parsing tentatively, issue a diagnostic of the form
2360 FILE:LINE: MESSAGE before TOKEN
2361 where TOKEN is the next token in the input stream. MESSAGE
2362 (specified by the caller) is usually of the form "expected
2366 cp_parser_error (cp_parser* parser, const char* gmsgid)
2368 if (!cp_parser_simulate_error (parser))
2370 cp_token *token = cp_lexer_peek_token (parser->lexer);
2371 /* This diagnostic makes more sense if it is tagged to the line
2372 of the token we just peeked at. */
2373 cp_lexer_set_source_position_from_token (token);
2375 if (token->type == CPP_PRAGMA)
2377 error_at (token->location,
2378 "%<#pragma%> is not allowed here");
2379 cp_parser_skip_to_pragma_eol (parser, token);
2383 c_parse_error (gmsgid,
2384 /* Because c_parser_error does not understand
2385 CPP_KEYWORD, keywords are treated like
2387 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2388 token->u.value, token->flags);
2392 /* Issue an error about name-lookup failing. NAME is the
2393 IDENTIFIER_NODE DECL is the result of
2394 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2395 the thing that we hoped to find. */
2398 cp_parser_name_lookup_error (cp_parser* parser,
2401 name_lookup_error desired,
2402 location_t location)
2404 /* If name lookup completely failed, tell the user that NAME was not
2406 if (decl == error_mark_node)
2408 if (parser->scope && parser->scope != global_namespace)
2409 error_at (location, "%<%E::%E%> has not been declared",
2410 parser->scope, name);
2411 else if (parser->scope == global_namespace)
2412 error_at (location, "%<::%E%> has not been declared", name);
2413 else if (parser->object_scope
2414 && !CLASS_TYPE_P (parser->object_scope))
2415 error_at (location, "request for member %qE in non-class type %qT",
2416 name, parser->object_scope);
2417 else if (parser->object_scope)
2418 error_at (location, "%<%T::%E%> has not been declared",
2419 parser->object_scope, name);
2421 error_at (location, "%qE has not been declared", name);
2423 else if (parser->scope && parser->scope != global_namespace)
2428 error_at (location, "%<%E::%E%> is not a type",
2429 parser->scope, name);
2432 error_at (location, "%<%E::%E%> is not a class or namespace",
2433 parser->scope, name);
2437 "%<%E::%E%> is not a class, namespace, or enumeration",
2438 parser->scope, name);
2445 else if (parser->scope == global_namespace)
2450 error_at (location, "%<::%E%> is not a type", name);
2453 error_at (location, "%<::%E%> is not a class or namespace", name);
2457 "%<::%E%> is not a class, namespace, or enumeration",
2469 error_at (location, "%qE is not a type", name);
2472 error_at (location, "%qE is not a class or namespace", name);
2476 "%qE is not a class, namespace, or enumeration", name);
2484 /* If we are parsing tentatively, remember that an error has occurred
2485 during this tentative parse. Returns true if the error was
2486 simulated; false if a message should be issued by the caller. */
2489 cp_parser_simulate_error (cp_parser* parser)
2491 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2493 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2499 /* This function is called when a type is defined. If type
2500 definitions are forbidden at this point, an error message is
2504 cp_parser_check_type_definition (cp_parser* parser)
2506 /* If types are forbidden here, issue a message. */
2507 if (parser->type_definition_forbidden_message)
2509 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2510 in the message need to be interpreted. */
2511 error (parser->type_definition_forbidden_message);
2517 /* This function is called when the DECLARATOR is processed. The TYPE
2518 was a type defined in the decl-specifiers. If it is invalid to
2519 define a type in the decl-specifiers for DECLARATOR, an error is
2520 issued. TYPE_LOCATION is the location of TYPE and is used
2521 for error reporting. */
2524 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2525 tree type, location_t type_location)
2527 /* [dcl.fct] forbids type definitions in return types.
2528 Unfortunately, it's not easy to know whether or not we are
2529 processing a return type until after the fact. */
2531 && (declarator->kind == cdk_pointer
2532 || declarator->kind == cdk_reference
2533 || declarator->kind == cdk_ptrmem))
2534 declarator = declarator->declarator;
2536 && declarator->kind == cdk_function)
2538 error_at (type_location,
2539 "new types may not be defined in a return type");
2540 inform (type_location,
2541 "(perhaps a semicolon is missing after the definition of %qT)",
2546 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2547 "<" in any valid C++ program. If the next token is indeed "<",
2548 issue a message warning the user about what appears to be an
2549 invalid attempt to form a template-id. LOCATION is the location
2550 of the type-specifier (TYPE) */
2553 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2555 enum tag_types tag_type,
2556 location_t location)
2558 cp_token_position start = 0;
2560 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2563 error_at (location, "%qT is not a template", type);
2564 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2566 if (tag_type != none_type)
2567 error_at (location, "%qE is not a class template", type);
2569 error_at (location, "%qE is not a template", type);
2572 error_at (location, "invalid template-id");
2573 /* Remember the location of the invalid "<". */
2574 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2575 start = cp_lexer_token_position (parser->lexer, true);
2576 /* Consume the "<". */
2577 cp_lexer_consume_token (parser->lexer);
2578 /* Parse the template arguments. */
2579 cp_parser_enclosed_template_argument_list (parser);
2580 /* Permanently remove the invalid template arguments so that
2581 this error message is not issued again. */
2583 cp_lexer_purge_tokens_after (parser->lexer, start);
2587 /* If parsing an integral constant-expression, issue an error message
2588 about the fact that THING appeared and return true. Otherwise,
2589 return false. In either case, set
2590 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2593 cp_parser_non_integral_constant_expression (cp_parser *parser,
2594 non_integral_constant thing)
2596 parser->non_integral_constant_expression_p = true;
2597 if (parser->integral_constant_expression_p)
2599 if (!parser->allow_non_integral_constant_expression_p)
2601 const char *msg = NULL;
2605 error ("floating-point literal "
2606 "cannot appear in a constant-expression");
2609 error ("a cast to a type other than an integral or "
2610 "enumeration type cannot appear in a "
2611 "constant-expression");
2614 error ("%<typeid%> operator "
2615 "cannot appear in a constant-expression");
2618 error ("non-constant compound literals "
2619 "cannot appear in a constant-expression");
2622 error ("a function call "
2623 "cannot appear in a constant-expression");
2626 error ("an increment "
2627 "cannot appear in a constant-expression");
2630 error ("an decrement "
2631 "cannot appear in a constant-expression");
2634 error ("an array reference "
2635 "cannot appear in a constant-expression");
2637 case NIC_ADDR_LABEL:
2638 error ("the address of a label "
2639 "cannot appear in a constant-expression");
2641 case NIC_OVERLOADED:
2642 error ("calls to overloaded operators "
2643 "cannot appear in a constant-expression");
2645 case NIC_ASSIGNMENT:
2646 error ("an assignment cannot appear in a constant-expression");
2649 error ("a comma operator "
2650 "cannot appear in a constant-expression");
2652 case NIC_CONSTRUCTOR:
2653 error ("a call to a constructor "
2654 "cannot appear in a constant-expression");
2656 case NIC_TRANSACTION:
2657 error ("a transaction expression "
2658 "cannot appear in a constant-expression");
2664 msg = "__FUNCTION__";
2666 case NIC_PRETTY_FUNC:
2667 msg = "__PRETTY_FUNCTION__";
2687 case NIC_PREINCREMENT:
2690 case NIC_PREDECREMENT:
2703 error ("%qs cannot appear in a constant-expression", msg);
2710 /* Emit a diagnostic for an invalid type name. SCOPE is the
2711 qualifying scope (or NULL, if none) for ID. This function commits
2712 to the current active tentative parse, if any. (Otherwise, the
2713 problematic construct might be encountered again later, resulting
2714 in duplicate error messages.) LOCATION is the location of ID. */
2717 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2718 tree scope, tree id,
2719 location_t location)
2721 tree decl, old_scope;
2722 cp_parser_commit_to_tentative_parse (parser);
2723 /* Try to lookup the identifier. */
2724 old_scope = parser->scope;
2725 parser->scope = scope;
2726 decl = cp_parser_lookup_name_simple (parser, id, location);
2727 parser->scope = old_scope;
2728 /* If the lookup found a template-name, it means that the user forgot
2729 to specify an argument list. Emit a useful error message. */
2730 if (TREE_CODE (decl) == TEMPLATE_DECL)
2732 "invalid use of template-name %qE without an argument list",
2734 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2735 error_at (location, "invalid use of destructor %qD as a type", id);
2736 else if (TREE_CODE (decl) == TYPE_DECL)
2737 /* Something like 'unsigned A a;' */
2738 error_at (location, "invalid combination of multiple type-specifiers");
2739 else if (!parser->scope)
2741 /* Issue an error message. */
2742 error_at (location, "%qE does not name a type", id);
2743 /* If we're in a template class, it's possible that the user was
2744 referring to a type from a base class. For example:
2746 template <typename T> struct A { typedef T X; };
2747 template <typename T> struct B : public A<T> { X x; };
2749 The user should have said "typename A<T>::X". */
2750 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2751 inform (location, "C++11 %<constexpr%> only available with "
2752 "-std=c++11 or -std=gnu++11");
2753 else if (processing_template_decl && current_class_type
2754 && TYPE_BINFO (current_class_type))
2758 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2762 tree base_type = BINFO_TYPE (b);
2763 if (CLASS_TYPE_P (base_type)
2764 && dependent_type_p (base_type))
2767 /* Go from a particular instantiation of the
2768 template (which will have an empty TYPE_FIELDs),
2769 to the main version. */
2770 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2771 for (field = TYPE_FIELDS (base_type);
2773 field = DECL_CHAIN (field))
2774 if (TREE_CODE (field) == TYPE_DECL
2775 && DECL_NAME (field) == id)
2778 "(perhaps %<typename %T::%E%> was intended)",
2779 BINFO_TYPE (b), id);
2788 /* Here we diagnose qualified-ids where the scope is actually correct,
2789 but the identifier does not resolve to a valid type name. */
2790 else if (parser->scope != error_mark_node)
2792 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2793 error_at (location, "%qE in namespace %qE does not name a type",
2795 else if (CLASS_TYPE_P (parser->scope)
2796 && constructor_name_p (id, parser->scope))
2799 error_at (location, "%<%T::%E%> names the constructor, not"
2800 " the type", parser->scope, id);
2801 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2802 error_at (location, "and %qT has no template constructors",
2805 else if (TYPE_P (parser->scope)
2806 && dependent_scope_p (parser->scope))
2807 error_at (location, "need %<typename%> before %<%T::%E%> because "
2808 "%qT is a dependent scope",
2809 parser->scope, id, parser->scope);
2810 else if (TYPE_P (parser->scope))
2811 error_at (location, "%qE in %q#T does not name a type",
2818 /* Check for a common situation where a type-name should be present,
2819 but is not, and issue a sensible error message. Returns true if an
2820 invalid type-name was detected.
2822 The situation handled by this function are variable declarations of the
2823 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2824 Usually, `ID' should name a type, but if we got here it means that it
2825 does not. We try to emit the best possible error message depending on
2826 how exactly the id-expression looks like. */
2829 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2832 cp_token *token = cp_lexer_peek_token (parser->lexer);
2834 /* Avoid duplicate error about ambiguous lookup. */
2835 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2837 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2838 if (next->type == CPP_NAME && next->ambiguous_p)
2842 cp_parser_parse_tentatively (parser);
2843 id = cp_parser_id_expression (parser,
2844 /*template_keyword_p=*/false,
2845 /*check_dependency_p=*/true,
2846 /*template_p=*/NULL,
2847 /*declarator_p=*/true,
2848 /*optional_p=*/false);
2849 /* If the next token is a (, this is a function with no explicit return
2850 type, i.e. constructor, destructor or conversion op. */
2851 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2852 || TREE_CODE (id) == TYPE_DECL)
2854 cp_parser_abort_tentative_parse (parser);
2857 if (!cp_parser_parse_definitely (parser))
2860 /* Emit a diagnostic for the invalid type. */
2861 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2862 id, token->location);
2864 /* If we aren't in the middle of a declarator (i.e. in a
2865 parameter-declaration-clause), skip to the end of the declaration;
2866 there's no point in trying to process it. */
2867 if (!parser->in_declarator_p)
2868 cp_parser_skip_to_end_of_block_or_statement (parser);
2872 /* Consume tokens up to, and including, the next non-nested closing `)'.
2873 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2874 are doing error recovery. Returns -1 if OR_COMMA is true and we
2875 found an unnested comma. */
2878 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2883 unsigned paren_depth = 0;
2884 unsigned brace_depth = 0;
2885 unsigned square_depth = 0;
2887 if (recovering && !or_comma
2888 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2893 cp_token * token = cp_lexer_peek_token (parser->lexer);
2895 switch (token->type)
2898 case CPP_PRAGMA_EOL:
2899 /* If we've run out of tokens, then there is no closing `)'. */
2902 /* This is good for lambda expression capture-lists. */
2903 case CPP_OPEN_SQUARE:
2906 case CPP_CLOSE_SQUARE:
2907 if (!square_depth--)
2912 /* This matches the processing in skip_to_end_of_statement. */
2917 case CPP_OPEN_BRACE:
2920 case CPP_CLOSE_BRACE:
2926 if (recovering && or_comma && !brace_depth && !paren_depth
2931 case CPP_OPEN_PAREN:
2936 case CPP_CLOSE_PAREN:
2937 if (!brace_depth && !paren_depth--)
2940 cp_lexer_consume_token (parser->lexer);
2949 /* Consume the token. */
2950 cp_lexer_consume_token (parser->lexer);
2954 /* Consume tokens until we reach the end of the current statement.
2955 Normally, that will be just before consuming a `;'. However, if a
2956 non-nested `}' comes first, then we stop before consuming that. */
2959 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2961 unsigned nesting_depth = 0;
2965 cp_token *token = cp_lexer_peek_token (parser->lexer);
2967 switch (token->type)
2970 case CPP_PRAGMA_EOL:
2971 /* If we've run out of tokens, stop. */
2975 /* If the next token is a `;', we have reached the end of the
2981 case CPP_CLOSE_BRACE:
2982 /* If this is a non-nested '}', stop before consuming it.
2983 That way, when confronted with something like:
2987 we stop before consuming the closing '}', even though we
2988 have not yet reached a `;'. */
2989 if (nesting_depth == 0)
2992 /* If it is the closing '}' for a block that we have
2993 scanned, stop -- but only after consuming the token.
2999 we will stop after the body of the erroneously declared
3000 function, but before consuming the following `typedef'
3002 if (--nesting_depth == 0)
3004 cp_lexer_consume_token (parser->lexer);
3008 case CPP_OPEN_BRACE:
3016 /* Consume the token. */
3017 cp_lexer_consume_token (parser->lexer);
3021 /* This function is called at the end of a statement or declaration.
3022 If the next token is a semicolon, it is consumed; otherwise, error
3023 recovery is attempted. */
3026 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3028 /* Look for the trailing `;'. */
3029 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3031 /* If there is additional (erroneous) input, skip to the end of
3033 cp_parser_skip_to_end_of_statement (parser);
3034 /* If the next token is now a `;', consume it. */
3035 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3036 cp_lexer_consume_token (parser->lexer);
3040 /* Skip tokens until we have consumed an entire block, or until we
3041 have consumed a non-nested `;'. */
3044 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3046 int nesting_depth = 0;
3048 while (nesting_depth >= 0)
3050 cp_token *token = cp_lexer_peek_token (parser->lexer);
3052 switch (token->type)
3055 case CPP_PRAGMA_EOL:
3056 /* If we've run out of tokens, stop. */
3060 /* Stop if this is an unnested ';'. */
3065 case CPP_CLOSE_BRACE:
3066 /* Stop if this is an unnested '}', or closes the outermost
3069 if (nesting_depth < 0)
3075 case CPP_OPEN_BRACE:
3084 /* Consume the token. */
3085 cp_lexer_consume_token (parser->lexer);
3089 /* Skip tokens until a non-nested closing curly brace is the next
3090 token, or there are no more tokens. Return true in the first case,
3094 cp_parser_skip_to_closing_brace (cp_parser *parser)
3096 unsigned nesting_depth = 0;
3100 cp_token *token = cp_lexer_peek_token (parser->lexer);
3102 switch (token->type)
3105 case CPP_PRAGMA_EOL:
3106 /* If we've run out of tokens, stop. */
3109 case CPP_CLOSE_BRACE:
3110 /* If the next token is a non-nested `}', then we have reached
3111 the end of the current block. */
3112 if (nesting_depth-- == 0)
3116 case CPP_OPEN_BRACE:
3117 /* If it the next token is a `{', then we are entering a new
3118 block. Consume the entire block. */
3126 /* Consume the token. */
3127 cp_lexer_consume_token (parser->lexer);
3131 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3132 parameter is the PRAGMA token, allowing us to purge the entire pragma
3136 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3140 parser->lexer->in_pragma = false;
3143 token = cp_lexer_consume_token (parser->lexer);
3144 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3146 /* Ensure that the pragma is not parsed again. */
3147 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3150 /* Require pragma end of line, resyncing with it as necessary. The
3151 arguments are as for cp_parser_skip_to_pragma_eol. */
3154 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3156 parser->lexer->in_pragma = false;
3157 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3158 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3161 /* This is a simple wrapper around make_typename_type. When the id is
3162 an unresolved identifier node, we can provide a superior diagnostic
3163 using cp_parser_diagnose_invalid_type_name. */
3166 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3167 tree id, location_t id_location)
3170 if (TREE_CODE (id) == IDENTIFIER_NODE)
3172 result = make_typename_type (scope, id, typename_type,
3173 /*complain=*/tf_none);
3174 if (result == error_mark_node)
3175 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3178 return make_typename_type (scope, id, typename_type, tf_error);
3181 /* This is a wrapper around the
3182 make_{pointer,ptrmem,reference}_declarator functions that decides
3183 which one to call based on the CODE and CLASS_TYPE arguments. The
3184 CODE argument should be one of the values returned by
3185 cp_parser_ptr_operator. */
3186 static cp_declarator *
3187 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3188 cp_cv_quals cv_qualifiers,
3189 cp_declarator *target)
3191 if (code == ERROR_MARK)
3192 return cp_error_declarator;
3194 if (code == INDIRECT_REF)
3195 if (class_type == NULL_TREE)
3196 return make_pointer_declarator (cv_qualifiers, target);
3198 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3199 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3200 return make_reference_declarator (cv_qualifiers, target, false);
3201 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3202 return make_reference_declarator (cv_qualifiers, target, true);
3206 /* Create a new C++ parser. */
3209 cp_parser_new (void)
3215 /* cp_lexer_new_main is called before doing GC allocation because
3216 cp_lexer_new_main might load a PCH file. */
3217 lexer = cp_lexer_new_main ();
3219 /* Initialize the binops_by_token so that we can get the tree
3220 directly from the token. */
3221 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3222 binops_by_token[binops[i].token_type] = binops[i];
3224 parser = ggc_alloc_cleared_cp_parser ();
3225 parser->lexer = lexer;
3226 parser->context = cp_parser_context_new (NULL);
3228 /* For now, we always accept GNU extensions. */
3229 parser->allow_gnu_extensions_p = 1;
3231 /* The `>' token is a greater-than operator, not the end of a
3233 parser->greater_than_is_operator_p = true;
3235 parser->default_arg_ok_p = true;
3237 /* We are not parsing a constant-expression. */
3238 parser->integral_constant_expression_p = false;
3239 parser->allow_non_integral_constant_expression_p = false;
3240 parser->non_integral_constant_expression_p = false;
3242 /* Local variable names are not forbidden. */
3243 parser->local_variables_forbidden_p = false;
3245 /* We are not processing an `extern "C"' declaration. */
3246 parser->in_unbraced_linkage_specification_p = false;
3248 /* We are not processing a declarator. */
3249 parser->in_declarator_p = false;
3251 /* We are not processing a template-argument-list. */
3252 parser->in_template_argument_list_p = false;
3254 /* We are not in an iteration statement. */
3255 parser->in_statement = 0;
3257 /* We are not in a switch statement. */
3258 parser->in_switch_statement_p = false;
3260 /* We are not parsing a type-id inside an expression. */
3261 parser->in_type_id_in_expr_p = false;
3263 /* Declarations aren't implicitly extern "C". */
3264 parser->implicit_extern_c = false;
3266 /* String literals should be translated to the execution character set. */
3267 parser->translate_strings_p = true;
3269 /* We are not parsing a function body. */
3270 parser->in_function_body = false;
3272 /* We can correct until told otherwise. */
3273 parser->colon_corrects_to_scope_p = true;
3275 /* The unparsed function queue is empty. */
3276 push_unparsed_function_queues (parser);
3278 /* There are no classes being defined. */
3279 parser->num_classes_being_defined = 0;
3281 /* No template parameters apply. */
3282 parser->num_template_parameter_lists = 0;
3287 /* Create a cp_lexer structure which will emit the tokens in CACHE
3288 and push it onto the parser's lexer stack. This is used for delayed
3289 parsing of in-class method bodies and default arguments, and should
3290 not be confused with tentative parsing. */
3292 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3294 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3295 lexer->next = parser->lexer;
3296 parser->lexer = lexer;
3298 /* Move the current source position to that of the first token in the
3300 cp_lexer_set_source_position_from_token (lexer->next_token);
3303 /* Pop the top lexer off the parser stack. This is never used for the
3304 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3306 cp_parser_pop_lexer (cp_parser *parser)
3308 cp_lexer *lexer = parser->lexer;
3309 parser->lexer = lexer->next;
3310 cp_lexer_destroy (lexer);
3312 /* Put the current source position back where it was before this
3313 lexer was pushed. */
3314 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3317 /* Lexical conventions [gram.lex] */
3319 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3323 cp_parser_identifier (cp_parser* parser)
3327 /* Look for the identifier. */
3328 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3329 /* Return the value. */
3330 return token ? token->u.value : error_mark_node;
3333 /* Parse a sequence of adjacent string constants. Returns a
3334 TREE_STRING representing the combined, nul-terminated string
3335 constant. If TRANSLATE is true, translate the string to the
3336 execution character set. If WIDE_OK is true, a wide string is
3339 C++98 [lex.string] says that if a narrow string literal token is
3340 adjacent to a wide string literal token, the behavior is undefined.
3341 However, C99 6.4.5p4 says that this results in a wide string literal.
3342 We follow C99 here, for consistency with the C front end.
3344 This code is largely lifted from lex_string() in c-lex.c.
3346 FUTURE: ObjC++ will need to handle @-strings here. */
3348 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3352 struct obstack str_ob;
3353 cpp_string str, istr, *strs;
3355 enum cpp_ttype type, curr_type;
3356 int have_suffix_p = 0;
3358 tree suffix_id = NULL_TREE;
3359 bool curr_tok_is_userdef_p = false;
3361 tok = cp_lexer_peek_token (parser->lexer);
3362 if (!cp_parser_is_string_literal (tok))
3364 cp_parser_error (parser, "expected string-literal");
3365 return error_mark_node;
3368 if (cpp_userdef_string_p (tok->type))
3370 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3371 curr_type = cpp_userdef_string_remove_type (tok->type);
3372 curr_tok_is_userdef_p = true;
3376 string_tree = tok->u.value;
3377 curr_type = tok->type;
3381 /* Try to avoid the overhead of creating and destroying an obstack
3382 for the common case of just one string. */
3383 if (!cp_parser_is_string_literal
3384 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3386 cp_lexer_consume_token (parser->lexer);
3388 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3389 str.len = TREE_STRING_LENGTH (string_tree);
3392 if (curr_tok_is_userdef_p)
3394 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3396 curr_type = cpp_userdef_string_remove_type (tok->type);
3399 curr_type = tok->type;
3405 gcc_obstack_init (&str_ob);
3410 cp_lexer_consume_token (parser->lexer);
3412 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3413 str.len = TREE_STRING_LENGTH (string_tree);
3415 if (curr_tok_is_userdef_p)
3417 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3418 if (have_suffix_p == 0)
3420 suffix_id = curr_suffix_id;
3423 else if (have_suffix_p == 1
3424 && curr_suffix_id != suffix_id)
3426 error ("inconsistent user-defined literal suffixes"
3427 " %qD and %qD in string literal",
3428 suffix_id, curr_suffix_id);
3431 curr_type = cpp_userdef_string_remove_type (tok->type);
3434 curr_type = tok->type;
3436 if (type != curr_type)
3438 if (type == CPP_STRING)
3440 else if (curr_type != CPP_STRING)
3441 error_at (tok->location,
3442 "unsupported non-standard concatenation "
3443 "of string literals");
3446 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3448 tok = cp_lexer_peek_token (parser->lexer);
3449 if (cpp_userdef_string_p (tok->type))
3451 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3452 curr_type = cpp_userdef_string_remove_type (tok->type);
3453 curr_tok_is_userdef_p = true;
3457 string_tree = tok->u.value;
3458 curr_type = tok->type;
3459 curr_tok_is_userdef_p = false;
3462 while (cp_parser_is_string_literal (tok));
3464 strs = (cpp_string *) obstack_finish (&str_ob);
3467 if (type != CPP_STRING && !wide_ok)
3469 cp_parser_error (parser, "a wide string is invalid in this context");
3473 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3474 (parse_in, strs, count, &istr, type))
3476 value = build_string (istr.len, (const char *)istr.text);
3477 free (CONST_CAST (unsigned char *, istr.text));
3483 case CPP_UTF8STRING:
3484 TREE_TYPE (value) = char_array_type_node;
3487 TREE_TYPE (value) = char16_array_type_node;
3490 TREE_TYPE (value) = char32_array_type_node;
3493 TREE_TYPE (value) = wchar_array_type_node;
3497 value = fix_string_type (value);
3501 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3502 tok->u.value = literal;
3503 return cp_parser_userdef_string_literal (tok);
3507 /* cpp_interpret_string has issued an error. */
3508 value = error_mark_node;
3511 obstack_free (&str_ob, 0);
3516 /* Look up a literal operator with the name and the exact arguments. */
3519 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3522 decl = lookup_name (name);
3523 if (!decl || !is_overloaded_fn (decl))
3524 return error_mark_node;
3526 for (fns = decl; fns; fns = OVL_NEXT (fns))
3530 tree fn = OVL_CURRENT (fns);
3531 tree argtypes = NULL_TREE;
3532 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3533 if (argtypes != NULL_TREE)
3535 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3536 ++ix, argtypes = TREE_CHAIN (argtypes))
3538 tree targ = TREE_VALUE (argtypes);
3539 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3540 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3541 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3542 if ((ptr || arr || !same_type_p (targ, tparm))
3544 || !same_type_p (TREE_TYPE (targ),
3545 TREE_TYPE (tparm))))
3549 && ix == VEC_length (tree, args)
3550 /* May be this should be sufficient_parms_p instead,
3551 depending on how exactly should user-defined literals
3552 work in presence of default arguments on the literal
3553 operator parameters. */
3554 && argtypes == void_list_node)
3559 return error_mark_node;
3562 /* Parse a user-defined char constant. Returns a call to a user-defined
3563 literal operator taking the character as an argument. */
3566 cp_parser_userdef_char_literal (cp_parser *parser)
3568 cp_token *token = cp_lexer_consume_token (parser->lexer);
3569 tree literal = token->u.value;
3570 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3571 tree value = USERDEF_LITERAL_VALUE (literal);
3572 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3575 /* Build up a call to the user-defined operator */
3576 /* Lookup the name we got back from the id-expression. */
3577 VEC(tree,gc) *args = make_tree_vector ();
3578 VEC_safe_push (tree, gc, args, value);
3579 decl = lookup_literal_operator (name, args);
3580 if (!decl || decl == error_mark_node)
3582 error ("unable to find character literal operator %qD with %qT argument",
3583 name, TREE_TYPE (value));
3584 release_tree_vector (args);
3585 return error_mark_node;
3587 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3588 release_tree_vector (args);
3589 if (result != error_mark_node)
3592 error ("unable to find character literal operator %qD with %qT argument",
3593 name, TREE_TYPE (value));
3594 return error_mark_node;
3597 /* A subroutine of cp_parser_userdef_numeric_literal to
3598 create a char... template parameter pack from a string node. */
3601 make_char_string_pack (tree value)
3604 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3605 const char *str = TREE_STRING_POINTER (value);
3606 int i, len = TREE_STRING_LENGTH (value) - 1;
3607 tree argvec = make_tree_vec (1);
3609 /* Fill in CHARVEC with all of the parameters. */
3610 charvec = make_tree_vec (len);
3611 for (i = 0; i < len; ++i)
3612 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3614 /* Build the argument packs. */
3615 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3616 TREE_TYPE (argpack) = char_type_node;
3618 TREE_VEC_ELT (argvec, 0) = argpack;
3623 /* Parse a user-defined numeric constant. returns a call to a user-defined
3624 literal operator. */
3627 cp_parser_userdef_numeric_literal (cp_parser *parser)
3629 cp_token *token = cp_lexer_consume_token (parser->lexer);
3630 tree literal = token->u.value;
3631 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3632 tree value = USERDEF_LITERAL_VALUE (literal);
3633 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3634 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3638 /* Look for a literal operator taking the exact type of numeric argument
3639 as the literal value. */
3640 args = make_tree_vector ();
3641 VEC_safe_push (tree, gc, args, value);
3642 decl = lookup_literal_operator (name, args);
3643 if (decl && decl != error_mark_node)
3645 result = finish_call_expr (decl, &args, false, true, tf_none);
3646 if (result != error_mark_node)
3648 release_tree_vector (args);
3652 release_tree_vector (args);
3654 /* If the numeric argument didn't work, look for a raw literal
3655 operator taking a const char* argument consisting of the number
3656 in string format. */
3657 args = make_tree_vector ();
3658 VEC_safe_push (tree, gc, args, num_string);
3659 decl = lookup_literal_operator (name, args);
3660 if (decl && decl != error_mark_node)
3662 result = finish_call_expr (decl, &args, false, true, tf_none);
3663 if (result != error_mark_node)
3665 release_tree_vector (args);
3669 release_tree_vector (args);
3671 /* If the raw literal didn't work, look for a non-type template
3672 function with parameter pack char.... Call the function with
3673 template parameter characters representing the number. */
3674 args = make_tree_vector ();
3675 decl = lookup_literal_operator (name, args);
3676 if (decl && decl != error_mark_node)
3678 tree tmpl_args = make_char_string_pack (num_string);
3679 decl = lookup_template_function (decl, tmpl_args);
3680 result = finish_call_expr (decl, &args, false, true, tf_none);
3681 if (result != error_mark_node)
3683 release_tree_vector (args);
3687 release_tree_vector (args);
3689 error ("unable to find numeric literal operator %qD", name);
3690 return error_mark_node;
3693 /* Parse a user-defined string constant. Returns a call to a user-defined
3694 literal operator taking a character pointer and the length of the string
3698 cp_parser_userdef_string_literal (cp_token *token)
3700 tree literal = token->u.value;
3701 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3702 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3703 tree value = USERDEF_LITERAL_VALUE (literal);
3704 int len = TREE_STRING_LENGTH (value)
3705 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3708 /* Build up a call to the user-defined operator */
3709 /* Lookup the name we got back from the id-expression. */
3710 VEC(tree,gc) *args = make_tree_vector ();
3711 VEC_safe_push (tree, gc, args, value);
3712 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3713 decl = lookup_name (name);
3714 if (!decl || decl == error_mark_node)
3716 error ("unable to find string literal operator %qD", name);
3717 release_tree_vector (args);
3718 return error_mark_node;
3720 result = finish_call_expr (decl, &args, false, true, tf_none);
3721 release_tree_vector (args);
3722 if (result != error_mark_node)
3725 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3726 name, TREE_TYPE (value), size_type_node);
3727 return error_mark_node;
3731 /* Basic concepts [gram.basic] */
3733 /* Parse a translation-unit.
3736 declaration-seq [opt]
3738 Returns TRUE if all went well. */
3741 cp_parser_translation_unit (cp_parser* parser)
3743 /* The address of the first non-permanent object on the declarator
3745 static void *declarator_obstack_base;
3749 /* Create the declarator obstack, if necessary. */
3750 if (!cp_error_declarator)
3752 gcc_obstack_init (&declarator_obstack);
3753 /* Create the error declarator. */
3754 cp_error_declarator = make_declarator (cdk_error);
3755 /* Create the empty parameter list. */
3756 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3757 /* Remember where the base of the declarator obstack lies. */
3758 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3761 cp_parser_declaration_seq_opt (parser);
3763 /* If there are no tokens left then all went well. */
3764 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3766 /* Get rid of the token array; we don't need it any more. */
3767 cp_lexer_destroy (parser->lexer);
3768 parser->lexer = NULL;
3770 /* This file might have been a context that's implicitly extern
3771 "C". If so, pop the lang context. (Only relevant for PCH.) */
3772 if (parser->implicit_extern_c)
3774 pop_lang_context ();
3775 parser->implicit_extern_c = false;
3779 finish_translation_unit ();
3785 cp_parser_error (parser, "expected declaration");
3789 /* Make sure the declarator obstack was fully cleaned up. */
3790 gcc_assert (obstack_next_free (&declarator_obstack)
3791 == declarator_obstack_base);
3793 /* All went well. */
3797 /* Expressions [gram.expr] */
3799 /* Parse a primary-expression.
3810 ( compound-statement )
3811 __builtin_va_arg ( assignment-expression , type-id )
3812 __builtin_offsetof ( type-id , offsetof-expression )
3815 __has_nothrow_assign ( type-id )
3816 __has_nothrow_constructor ( type-id )
3817 __has_nothrow_copy ( type-id )
3818 __has_trivial_assign ( type-id )
3819 __has_trivial_constructor ( type-id )
3820 __has_trivial_copy ( type-id )
3821 __has_trivial_destructor ( type-id )
3822 __has_virtual_destructor ( type-id )
3823 __is_abstract ( type-id )
3824 __is_base_of ( type-id , type-id )
3825 __is_class ( type-id )
3826 __is_convertible_to ( type-id , type-id )
3827 __is_empty ( type-id )
3828 __is_enum ( type-id )
3829 __is_final ( type-id )
3830 __is_literal_type ( type-id )
3831 __is_pod ( type-id )
3832 __is_polymorphic ( type-id )
3833 __is_std_layout ( type-id )
3834 __is_trivial ( type-id )
3835 __is_union ( type-id )
3837 Objective-C++ Extension:
3845 ADDRESS_P is true iff this expression was immediately preceded by
3846 "&" and therefore might denote a pointer-to-member. CAST_P is true
3847 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3848 true iff this expression is a template argument.
3850 Returns a representation of the expression. Upon return, *IDK
3851 indicates what kind of id-expression (if any) was present. */
3854 cp_parser_primary_expression (cp_parser *parser,
3857 bool template_arg_p,
3860 cp_token *token = NULL;
3862 /* Assume the primary expression is not an id-expression. */
3863 *idk = CP_ID_KIND_NONE;
3865 /* Peek at the next token. */
3866 token = cp_lexer_peek_token (parser->lexer);
3867 switch (token->type)
3876 user-defined-literal */
3882 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3883 return cp_parser_userdef_numeric_literal (parser);
3884 token = cp_lexer_consume_token (parser->lexer);
3885 if (TREE_CODE (token->u.value) == FIXED_CST)
3887 error_at (token->location,
3888 "fixed-point types not supported in C++");
3889 return error_mark_node;
3891 /* Floating-point literals are only allowed in an integral
3892 constant expression if they are cast to an integral or
3893 enumeration type. */
3894 if (TREE_CODE (token->u.value) == REAL_CST
3895 && parser->integral_constant_expression_p
3898 /* CAST_P will be set even in invalid code like "int(2.7 +
3899 ...)". Therefore, we have to check that the next token
3900 is sure to end the cast. */
3903 cp_token *next_token;
3905 next_token = cp_lexer_peek_token (parser->lexer);
3906 if (/* The comma at the end of an
3907 enumerator-definition. */
3908 next_token->type != CPP_COMMA
3909 /* The curly brace at the end of an enum-specifier. */
3910 && next_token->type != CPP_CLOSE_BRACE
3911 /* The end of a statement. */
3912 && next_token->type != CPP_SEMICOLON
3913 /* The end of the cast-expression. */
3914 && next_token->type != CPP_CLOSE_PAREN
3915 /* The end of an array bound. */
3916 && next_token->type != CPP_CLOSE_SQUARE
3917 /* The closing ">" in a template-argument-list. */
3918 && (next_token->type != CPP_GREATER
3919 || parser->greater_than_is_operator_p)
3920 /* C++0x only: A ">>" treated like two ">" tokens,
3921 in a template-argument-list. */
3922 && (next_token->type != CPP_RSHIFT
3923 || (cxx_dialect == cxx98)
3924 || parser->greater_than_is_operator_p))
3928 /* If we are within a cast, then the constraint that the
3929 cast is to an integral or enumeration type will be
3930 checked at that point. If we are not within a cast, then
3931 this code is invalid. */
3933 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3935 return token->u.value;
3937 case CPP_CHAR_USERDEF:
3938 case CPP_CHAR16_USERDEF:
3939 case CPP_CHAR32_USERDEF:
3940 case CPP_WCHAR_USERDEF:
3941 return cp_parser_userdef_char_literal (parser);
3947 case CPP_UTF8STRING:
3948 case CPP_STRING_USERDEF:
3949 case CPP_STRING16_USERDEF:
3950 case CPP_STRING32_USERDEF:
3951 case CPP_WSTRING_USERDEF:
3952 case CPP_UTF8STRING_USERDEF:
3953 /* ??? Should wide strings be allowed when parser->translate_strings_p
3954 is false (i.e. in attributes)? If not, we can kill the third
3955 argument to cp_parser_string_literal. */
3956 return cp_parser_string_literal (parser,
3957 parser->translate_strings_p,
3960 case CPP_OPEN_PAREN:
3963 bool saved_greater_than_is_operator_p;
3965 /* Consume the `('. */
3966 cp_lexer_consume_token (parser->lexer);
3967 /* Within a parenthesized expression, a `>' token is always
3968 the greater-than operator. */
3969 saved_greater_than_is_operator_p
3970 = parser->greater_than_is_operator_p;
3971 parser->greater_than_is_operator_p = true;
3972 /* If we see `( { ' then we are looking at the beginning of
3973 a GNU statement-expression. */
3974 if (cp_parser_allow_gnu_extensions_p (parser)
3975 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3977 /* Statement-expressions are not allowed by the standard. */
3978 pedwarn (token->location, OPT_Wpedantic,
3979 "ISO C++ forbids braced-groups within expressions");
3981 /* And they're not allowed outside of a function-body; you
3982 cannot, for example, write:
3984 int i = ({ int j = 3; j + 1; });
3986 at class or namespace scope. */
3987 if (!parser->in_function_body
3988 || parser->in_template_argument_list_p)
3990 error_at (token->location,
3991 "statement-expressions are not allowed outside "
3992 "functions nor in template-argument lists");
3993 cp_parser_skip_to_end_of_block_or_statement (parser);
3994 expr = error_mark_node;
3998 /* Start the statement-expression. */
3999 expr = begin_stmt_expr ();
4000 /* Parse the compound-statement. */
4001 cp_parser_compound_statement (parser, expr, false, false);
4003 expr = finish_stmt_expr (expr, false);
4008 /* Parse the parenthesized expression. */
4009 expr = cp_parser_expression (parser, cast_p, idk);
4010 /* Let the front end know that this expression was
4011 enclosed in parentheses. This matters in case, for
4012 example, the expression is of the form `A::B', since
4013 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4015 finish_parenthesized_expr (expr);
4016 /* DR 705: Wrapping an unqualified name in parentheses
4017 suppresses arg-dependent lookup. We want to pass back
4018 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4019 (c++/37862), but none of the others. */
4020 if (*idk != CP_ID_KIND_QUALIFIED)
4021 *idk = CP_ID_KIND_NONE;
4023 /* The `>' token might be the end of a template-id or
4024 template-parameter-list now. */
4025 parser->greater_than_is_operator_p
4026 = saved_greater_than_is_operator_p;
4027 /* Consume the `)'. */
4028 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4029 cp_parser_skip_to_end_of_statement (parser);
4034 case CPP_OPEN_SQUARE:
4035 if (c_dialect_objc ())
4036 /* We have an Objective-C++ message. */
4037 return cp_parser_objc_expression (parser);
4039 tree lam = cp_parser_lambda_expression (parser);
4040 /* Don't warn about a failed tentative parse. */
4041 if (cp_parser_error_occurred (parser))
4042 return error_mark_node;
4043 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4047 case CPP_OBJC_STRING:
4048 if (c_dialect_objc ())
4049 /* We have an Objective-C++ string literal. */
4050 return cp_parser_objc_expression (parser);
4051 cp_parser_error (parser, "expected primary-expression");
4052 return error_mark_node;
4055 switch (token->keyword)
4057 /* These two are the boolean literals. */
4059 cp_lexer_consume_token (parser->lexer);
4060 return boolean_true_node;
4062 cp_lexer_consume_token (parser->lexer);
4063 return boolean_false_node;
4065 /* The `__null' literal. */
4067 cp_lexer_consume_token (parser->lexer);
4070 /* The `nullptr' literal. */
4072 cp_lexer_consume_token (parser->lexer);
4073 return nullptr_node;
4075 /* Recognize the `this' keyword. */
4077 cp_lexer_consume_token (parser->lexer);
4078 if (parser->local_variables_forbidden_p)
4080 error_at (token->location,
4081 "%<this%> may not be used in this context");
4082 return error_mark_node;
4084 /* Pointers cannot appear in constant-expressions. */
4085 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4086 return error_mark_node;
4087 return finish_this_expr ();
4089 /* The `operator' keyword can be the beginning of an
4094 case RID_FUNCTION_NAME:
4095 case RID_PRETTY_FUNCTION_NAME:
4096 case RID_C99_FUNCTION_NAME:
4098 non_integral_constant name;
4100 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4101 __func__ are the names of variables -- but they are
4102 treated specially. Therefore, they are handled here,
4103 rather than relying on the generic id-expression logic
4104 below. Grammatically, these names are id-expressions.
4106 Consume the token. */
4107 token = cp_lexer_consume_token (parser->lexer);
4109 switch (token->keyword)
4111 case RID_FUNCTION_NAME:
4112 name = NIC_FUNC_NAME;
4114 case RID_PRETTY_FUNCTION_NAME:
4115 name = NIC_PRETTY_FUNC;
4117 case RID_C99_FUNCTION_NAME:
4118 name = NIC_C99_FUNC;
4124 if (cp_parser_non_integral_constant_expression (parser, name))
4125 return error_mark_node;
4127 /* Look up the name. */
4128 return finish_fname (token->u.value);
4135 source_location type_location;
4137 /* The `__builtin_va_arg' construct is used to handle
4138 `va_arg'. Consume the `__builtin_va_arg' token. */
4139 cp_lexer_consume_token (parser->lexer);
4140 /* Look for the opening `('. */
4141 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4142 /* Now, parse the assignment-expression. */
4143 expression = cp_parser_assignment_expression (parser,
4144 /*cast_p=*/false, NULL);
4145 /* Look for the `,'. */
4146 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4147 type_location = cp_lexer_peek_token (parser->lexer)->location;
4148 /* Parse the type-id. */
4149 type = cp_parser_type_id (parser);
4150 /* Look for the closing `)'. */
4151 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4152 /* Using `va_arg' in a constant-expression is not
4154 if (cp_parser_non_integral_constant_expression (parser,
4156 return error_mark_node;
4157 return build_x_va_arg (type_location, expression, type);
4161 return cp_parser_builtin_offsetof (parser);
4163 case RID_HAS_NOTHROW_ASSIGN:
4164 case RID_HAS_NOTHROW_CONSTRUCTOR:
4165 case RID_HAS_NOTHROW_COPY:
4166 case RID_HAS_TRIVIAL_ASSIGN:
4167 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4168 case RID_HAS_TRIVIAL_COPY:
4169 case RID_HAS_TRIVIAL_DESTRUCTOR:
4170 case RID_HAS_VIRTUAL_DESTRUCTOR:
4171 case RID_IS_ABSTRACT:
4172 case RID_IS_BASE_OF:
4174 case RID_IS_CONVERTIBLE_TO:
4178 case RID_IS_LITERAL_TYPE:
4180 case RID_IS_POLYMORPHIC:
4181 case RID_IS_STD_LAYOUT:
4182 case RID_IS_TRIVIAL:
4184 return cp_parser_trait_expr (parser, token->keyword);
4186 /* Objective-C++ expressions. */
4188 case RID_AT_PROTOCOL:
4189 case RID_AT_SELECTOR:
4190 return cp_parser_objc_expression (parser);
4193 if (parser->in_function_body
4194 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4197 error_at (token->location,
4198 "a template declaration cannot appear at block scope");
4199 cp_parser_skip_to_end_of_block_or_statement (parser);
4200 return error_mark_node;
4203 cp_parser_error (parser, "expected primary-expression");
4204 return error_mark_node;
4207 /* An id-expression can start with either an identifier, a
4208 `::' as the beginning of a qualified-id, or the "operator"
4212 case CPP_TEMPLATE_ID:
4213 case CPP_NESTED_NAME_SPECIFIER:
4217 const char *error_msg;
4220 cp_token *id_expr_token;
4223 /* Parse the id-expression. */
4225 = cp_parser_id_expression (parser,
4226 /*template_keyword_p=*/false,
4227 /*check_dependency_p=*/true,
4229 /*declarator_p=*/false,
4230 /*optional_p=*/false);
4231 if (id_expression == error_mark_node)
4232 return error_mark_node;
4233 id_expr_token = token;
4234 token = cp_lexer_peek_token (parser->lexer);
4235 done = (token->type != CPP_OPEN_SQUARE
4236 && token->type != CPP_OPEN_PAREN
4237 && token->type != CPP_DOT
4238 && token->type != CPP_DEREF
4239 && token->type != CPP_PLUS_PLUS
4240 && token->type != CPP_MINUS_MINUS);
4241 /* If we have a template-id, then no further lookup is
4242 required. If the template-id was for a template-class, we
4243 will sometimes have a TYPE_DECL at this point. */
4244 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4245 || TREE_CODE (id_expression) == TYPE_DECL)
4246 decl = id_expression;
4247 /* Look up the name. */
4250 tree ambiguous_decls;
4252 /* If we already know that this lookup is ambiguous, then
4253 we've already issued an error message; there's no reason
4255 if (id_expr_token->type == CPP_NAME
4256 && id_expr_token->ambiguous_p)
4258 cp_parser_simulate_error (parser);
4259 return error_mark_node;
4262 decl = cp_parser_lookup_name (parser, id_expression,
4265 /*is_namespace=*/false,
4266 /*check_dependency=*/true,
4268 id_expr_token->location);
4269 /* If the lookup was ambiguous, an error will already have
4271 if (ambiguous_decls)
4272 return error_mark_node;
4274 /* In Objective-C++, we may have an Objective-C 2.0
4275 dot-syntax for classes here. */
4276 if (c_dialect_objc ()
4277 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4278 && TREE_CODE (decl) == TYPE_DECL
4279 && objc_is_class_name (decl))
4282 cp_lexer_consume_token (parser->lexer);
4283 component = cp_parser_identifier (parser);
4284 if (component == error_mark_node)
4285 return error_mark_node;
4287 return objc_build_class_component_ref (id_expression, component);
4290 /* In Objective-C++, an instance variable (ivar) may be preferred
4291 to whatever cp_parser_lookup_name() found. */
4292 decl = objc_lookup_ivar (decl, id_expression);
4294 /* If name lookup gives us a SCOPE_REF, then the
4295 qualifying scope was dependent. */
4296 if (TREE_CODE (decl) == SCOPE_REF)
4298 /* At this point, we do not know if DECL is a valid
4299 integral constant expression. We assume that it is
4300 in fact such an expression, so that code like:
4302 template <int N> struct A {
4306 is accepted. At template-instantiation time, we
4307 will check that B<N>::i is actually a constant. */
4310 /* Check to see if DECL is a local variable in a context
4311 where that is forbidden. */
4312 if (parser->local_variables_forbidden_p
4313 && local_variable_p (decl))
4315 /* It might be that we only found DECL because we are
4316 trying to be generous with pre-ISO scoping rules.
4317 For example, consider:
4321 for (int i = 0; i < 10; ++i) {}
4322 extern void f(int j = i);
4325 Here, name look up will originally find the out
4326 of scope `i'. We need to issue a warning message,
4327 but then use the global `i'. */
4328 decl = check_for_out_of_scope_variable (decl);
4329 if (local_variable_p (decl))
4331 error_at (id_expr_token->location,
4332 "local variable %qD may not appear in this context",
4334 return error_mark_node;
4339 decl = (finish_id_expression
4340 (id_expression, decl, parser->scope,
4342 parser->integral_constant_expression_p,
4343 parser->allow_non_integral_constant_expression_p,
4344 &parser->non_integral_constant_expression_p,
4345 template_p, done, address_p,
4348 id_expr_token->location));
4350 cp_parser_error (parser, error_msg);
4354 /* Anything else is an error. */
4356 cp_parser_error (parser, "expected primary-expression");
4357 return error_mark_node;
4361 /* Parse an id-expression.
4368 :: [opt] nested-name-specifier template [opt] unqualified-id
4370 :: operator-function-id
4373 Return a representation of the unqualified portion of the
4374 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4375 a `::' or nested-name-specifier.
4377 Often, if the id-expression was a qualified-id, the caller will
4378 want to make a SCOPE_REF to represent the qualified-id. This
4379 function does not do this in order to avoid wastefully creating
4380 SCOPE_REFs when they are not required.
4382 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4385 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4386 uninstantiated templates.
4388 If *TEMPLATE_P is non-NULL, it is set to true iff the
4389 `template' keyword is used to explicitly indicate that the entity
4390 named is a template.
4392 If DECLARATOR_P is true, the id-expression is appearing as part of
4393 a declarator, rather than as part of an expression. */
4396 cp_parser_id_expression (cp_parser *parser,
4397 bool template_keyword_p,
4398 bool check_dependency_p,
4403 bool global_scope_p;
4404 bool nested_name_specifier_p;
4406 /* Assume the `template' keyword was not used. */
4408 *template_p = template_keyword_p;
4410 /* Look for the optional `::' operator. */
4412 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4414 /* Look for the optional nested-name-specifier. */
4415 nested_name_specifier_p
4416 = (cp_parser_nested_name_specifier_opt (parser,
4417 /*typename_keyword_p=*/false,
4422 /* If there is a nested-name-specifier, then we are looking at
4423 the first qualified-id production. */
4424 if (nested_name_specifier_p)
4427 tree saved_object_scope;
4428 tree saved_qualifying_scope;
4429 tree unqualified_id;
4432 /* See if the next token is the `template' keyword. */
4434 template_p = &is_template;
4435 *template_p = cp_parser_optional_template_keyword (parser);
4436 /* Name lookup we do during the processing of the
4437 unqualified-id might obliterate SCOPE. */
4438 saved_scope = parser->scope;
4439 saved_object_scope = parser->object_scope;
4440 saved_qualifying_scope = parser->qualifying_scope;
4441 /* Process the final unqualified-id. */
4442 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4445 /*optional_p=*/false);
4446 /* Restore the SAVED_SCOPE for our caller. */
4447 parser->scope = saved_scope;
4448 parser->object_scope = saved_object_scope;
4449 parser->qualifying_scope = saved_qualifying_scope;
4451 return unqualified_id;
4453 /* Otherwise, if we are in global scope, then we are looking at one
4454 of the other qualified-id productions. */
4455 else if (global_scope_p)
4460 /* Peek at the next token. */
4461 token = cp_lexer_peek_token (parser->lexer);
4463 /* If it's an identifier, and the next token is not a "<", then
4464 we can avoid the template-id case. This is an optimization
4465 for this common case. */
4466 if (token->type == CPP_NAME
4467 && !cp_parser_nth_token_starts_template_argument_list_p
4469 return cp_parser_identifier (parser);
4471 cp_parser_parse_tentatively (parser);
4472 /* Try a template-id. */
4473 id = cp_parser_template_id (parser,
4474 /*template_keyword_p=*/false,
4475 /*check_dependency_p=*/true,
4478 /* If that worked, we're done. */
4479 if (cp_parser_parse_definitely (parser))
4482 /* Peek at the next token. (Changes in the token buffer may
4483 have invalidated the pointer obtained above.) */
4484 token = cp_lexer_peek_token (parser->lexer);
4486 switch (token->type)
4489 return cp_parser_identifier (parser);
4492 if (token->keyword == RID_OPERATOR)
4493 return cp_parser_operator_function_id (parser);
4497 cp_parser_error (parser, "expected id-expression");
4498 return error_mark_node;
4502 return cp_parser_unqualified_id (parser, template_keyword_p,
4503 /*check_dependency_p=*/true,
4508 /* Parse an unqualified-id.
4512 operator-function-id
4513 conversion-function-id
4517 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4518 keyword, in a construct like `A::template ...'.
4520 Returns a representation of unqualified-id. For the `identifier'
4521 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4522 production a BIT_NOT_EXPR is returned; the operand of the
4523 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4524 other productions, see the documentation accompanying the
4525 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4526 names are looked up in uninstantiated templates. If DECLARATOR_P
4527 is true, the unqualified-id is appearing as part of a declarator,
4528 rather than as part of an expression. */
4531 cp_parser_unqualified_id (cp_parser* parser,
4532 bool template_keyword_p,
4533 bool check_dependency_p,
4539 /* Peek at the next token. */
4540 token = cp_lexer_peek_token (parser->lexer);
4542 switch (token->type)
4548 /* We don't know yet whether or not this will be a
4550 cp_parser_parse_tentatively (parser);
4551 /* Try a template-id. */
4552 id = cp_parser_template_id (parser, template_keyword_p,
4556 /* If it worked, we're done. */
4557 if (cp_parser_parse_definitely (parser))
4559 /* Otherwise, it's an ordinary identifier. */
4560 return cp_parser_identifier (parser);
4563 case CPP_TEMPLATE_ID:
4564 return cp_parser_template_id (parser, template_keyword_p,
4572 tree qualifying_scope;
4577 /* Consume the `~' token. */
4578 cp_lexer_consume_token (parser->lexer);
4579 /* Parse the class-name. The standard, as written, seems to
4582 template <typename T> struct S { ~S (); };
4583 template <typename T> S<T>::~S() {}
4585 is invalid, since `~' must be followed by a class-name, but
4586 `S<T>' is dependent, and so not known to be a class.
4587 That's not right; we need to look in uninstantiated
4588 templates. A further complication arises from:
4590 template <typename T> void f(T t) {
4594 Here, it is not possible to look up `T' in the scope of `T'
4595 itself. We must look in both the current scope, and the
4596 scope of the containing complete expression.
4598 Yet another issue is:
4607 The standard does not seem to say that the `S' in `~S'
4608 should refer to the type `S' and not the data member
4611 /* DR 244 says that we look up the name after the "~" in the
4612 same scope as we looked up the qualifying name. That idea
4613 isn't fully worked out; it's more complicated than that. */
4614 scope = parser->scope;
4615 object_scope = parser->object_scope;
4616 qualifying_scope = parser->qualifying_scope;
4618 /* Check for invalid scopes. */
4619 if (scope == error_mark_node)
4621 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4622 cp_lexer_consume_token (parser->lexer);
4623 return error_mark_node;
4625 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4627 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4628 error_at (token->location,
4629 "scope %qT before %<~%> is not a class-name",
4631 cp_parser_simulate_error (parser);
4632 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4633 cp_lexer_consume_token (parser->lexer);
4634 return error_mark_node;
4636 gcc_assert (!scope || TYPE_P (scope));
4638 /* If the name is of the form "X::~X" it's OK even if X is a
4640 token = cp_lexer_peek_token (parser->lexer);
4642 && token->type == CPP_NAME
4643 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4645 && (token->u.value == TYPE_IDENTIFIER (scope)
4646 || (CLASS_TYPE_P (scope)
4647 && constructor_name_p (token->u.value, scope))))
4649 cp_lexer_consume_token (parser->lexer);
4650 return build_nt (BIT_NOT_EXPR, scope);
4653 /* If there was an explicit qualification (S::~T), first look
4654 in the scope given by the qualification (i.e., S).
4656 Note: in the calls to cp_parser_class_name below we pass
4657 typename_type so that lookup finds the injected-class-name
4658 rather than the constructor. */
4660 type_decl = NULL_TREE;
4663 cp_parser_parse_tentatively (parser);
4664 type_decl = cp_parser_class_name (parser,
4665 /*typename_keyword_p=*/false,
4666 /*template_keyword_p=*/false,
4668 /*check_dependency=*/false,
4669 /*class_head_p=*/false,
4671 if (cp_parser_parse_definitely (parser))
4674 /* In "N::S::~S", look in "N" as well. */
4675 if (!done && scope && qualifying_scope)
4677 cp_parser_parse_tentatively (parser);
4678 parser->scope = qualifying_scope;
4679 parser->object_scope = NULL_TREE;
4680 parser->qualifying_scope = NULL_TREE;
4682 = cp_parser_class_name (parser,
4683 /*typename_keyword_p=*/false,
4684 /*template_keyword_p=*/false,
4686 /*check_dependency=*/false,
4687 /*class_head_p=*/false,
4689 if (cp_parser_parse_definitely (parser))
4692 /* In "p->S::~T", look in the scope given by "*p" as well. */
4693 else if (!done && object_scope)
4695 cp_parser_parse_tentatively (parser);
4696 parser->scope = object_scope;
4697 parser->object_scope = NULL_TREE;
4698 parser->qualifying_scope = NULL_TREE;
4700 = cp_parser_class_name (parser,
4701 /*typename_keyword_p=*/false,
4702 /*template_keyword_p=*/false,
4704 /*check_dependency=*/false,
4705 /*class_head_p=*/false,
4707 if (cp_parser_parse_definitely (parser))
4710 /* Look in the surrounding context. */
4713 parser->scope = NULL_TREE;
4714 parser->object_scope = NULL_TREE;
4715 parser->qualifying_scope = NULL_TREE;
4716 if (processing_template_decl)
4717 cp_parser_parse_tentatively (parser);
4719 = cp_parser_class_name (parser,
4720 /*typename_keyword_p=*/false,
4721 /*template_keyword_p=*/false,
4723 /*check_dependency=*/false,
4724 /*class_head_p=*/false,
4726 if (processing_template_decl
4727 && ! cp_parser_parse_definitely (parser))
4729 /* We couldn't find a type with this name, so just accept
4730 it and check for a match at instantiation time. */
4731 type_decl = cp_parser_identifier (parser);
4732 if (type_decl != error_mark_node)
4733 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4737 /* If an error occurred, assume that the name of the
4738 destructor is the same as the name of the qualifying
4739 class. That allows us to keep parsing after running
4740 into ill-formed destructor names. */
4741 if (type_decl == error_mark_node && scope)
4742 return build_nt (BIT_NOT_EXPR, scope);
4743 else if (type_decl == error_mark_node)
4744 return error_mark_node;
4746 /* Check that destructor name and scope match. */
4747 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4749 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4750 error_at (token->location,
4751 "declaration of %<~%T%> as member of %qT",
4753 cp_parser_simulate_error (parser);
4754 return error_mark_node;
4759 A typedef-name that names a class shall not be used as the
4760 identifier in the declarator for a destructor declaration. */
4762 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4763 && !DECL_SELF_REFERENCE_P (type_decl)
4764 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4765 error_at (token->location,
4766 "typedef-name %qD used as destructor declarator",
4769 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4773 if (token->keyword == RID_OPERATOR)
4777 /* This could be a template-id, so we try that first. */
4778 cp_parser_parse_tentatively (parser);
4779 /* Try a template-id. */
4780 id = cp_parser_template_id (parser, template_keyword_p,
4781 /*check_dependency_p=*/true,
4784 /* If that worked, we're done. */
4785 if (cp_parser_parse_definitely (parser))
4787 /* We still don't know whether we're looking at an
4788 operator-function-id or a conversion-function-id. */
4789 cp_parser_parse_tentatively (parser);
4790 /* Try an operator-function-id. */
4791 id = cp_parser_operator_function_id (parser);
4792 /* If that didn't work, try a conversion-function-id. */
4793 if (!cp_parser_parse_definitely (parser))
4794 id = cp_parser_conversion_function_id (parser);
4795 else if (UDLIT_OPER_P (id))
4798 const char *name = UDLIT_OP_SUFFIX (id);
4799 if (name[0] != '_' && !in_system_header)
4800 warning (0, "literal operator suffixes not preceded by %<_%>"
4801 " are reserved for future standardization");
4811 cp_parser_error (parser, "expected unqualified-id");
4812 return error_mark_node;
4816 /* Parse an (optional) nested-name-specifier.
4818 nested-name-specifier: [C++98]
4819 class-or-namespace-name :: nested-name-specifier [opt]
4820 class-or-namespace-name :: template nested-name-specifier [opt]
4822 nested-name-specifier: [C++0x]
4825 nested-name-specifier identifier ::
4826 nested-name-specifier template [opt] simple-template-id ::
4828 PARSER->SCOPE should be set appropriately before this function is
4829 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4830 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4833 Sets PARSER->SCOPE to the class (TYPE) or namespace
4834 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4835 it unchanged if there is no nested-name-specifier. Returns the new
4836 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4838 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4839 part of a declaration and/or decl-specifier. */
4842 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4843 bool typename_keyword_p,
4844 bool check_dependency_p,
4846 bool is_declaration)
4848 bool success = false;
4849 cp_token_position start = 0;
4852 /* Remember where the nested-name-specifier starts. */
4853 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4855 start = cp_lexer_token_position (parser->lexer, false);
4856 push_deferring_access_checks (dk_deferred);
4863 tree saved_qualifying_scope;
4864 bool template_keyword_p;
4866 /* Spot cases that cannot be the beginning of a
4867 nested-name-specifier. */
4868 token = cp_lexer_peek_token (parser->lexer);
4870 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4871 the already parsed nested-name-specifier. */
4872 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4874 /* Grab the nested-name-specifier and continue the loop. */
4875 cp_parser_pre_parsed_nested_name_specifier (parser);
4876 /* If we originally encountered this nested-name-specifier
4877 with IS_DECLARATION set to false, we will not have
4878 resolved TYPENAME_TYPEs, so we must do so here. */
4880 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4882 new_scope = resolve_typename_type (parser->scope,
4883 /*only_current_p=*/false);
4884 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4885 parser->scope = new_scope;
4891 /* Spot cases that cannot be the beginning of a
4892 nested-name-specifier. On the second and subsequent times
4893 through the loop, we look for the `template' keyword. */
4894 if (success && token->keyword == RID_TEMPLATE)
4896 /* A template-id can start a nested-name-specifier. */
4897 else if (token->type == CPP_TEMPLATE_ID)
4899 /* DR 743: decltype can be used in a nested-name-specifier. */
4900 else if (token_is_decltype (token))
4904 /* If the next token is not an identifier, then it is
4905 definitely not a type-name or namespace-name. */
4906 if (token->type != CPP_NAME)
4908 /* If the following token is neither a `<' (to begin a
4909 template-id), nor a `::', then we are not looking at a
4910 nested-name-specifier. */
4911 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4913 if (token->type == CPP_COLON
4914 && parser->colon_corrects_to_scope_p
4915 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4917 error_at (token->location,
4918 "found %<:%> in nested-name-specifier, expected %<::%>");
4919 token->type = CPP_SCOPE;
4922 if (token->type != CPP_SCOPE
4923 && !cp_parser_nth_token_starts_template_argument_list_p
4928 /* The nested-name-specifier is optional, so we parse
4930 cp_parser_parse_tentatively (parser);
4932 /* Look for the optional `template' keyword, if this isn't the
4933 first time through the loop. */
4935 template_keyword_p = cp_parser_optional_template_keyword (parser);
4937 template_keyword_p = false;
4939 /* Save the old scope since the name lookup we are about to do
4940 might destroy it. */
4941 old_scope = parser->scope;
4942 saved_qualifying_scope = parser->qualifying_scope;
4943 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4944 look up names in "X<T>::I" in order to determine that "Y" is
4945 a template. So, if we have a typename at this point, we make
4946 an effort to look through it. */
4948 && !typename_keyword_p
4950 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4951 parser->scope = resolve_typename_type (parser->scope,
4952 /*only_current_p=*/false);
4953 /* Parse the qualifying entity. */
4955 = cp_parser_qualifying_entity (parser,
4961 /* Look for the `::' token. */
4962 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4964 /* If we found what we wanted, we keep going; otherwise, we're
4966 if (!cp_parser_parse_definitely (parser))
4968 bool error_p = false;
4970 /* Restore the OLD_SCOPE since it was valid before the
4971 failed attempt at finding the last
4972 class-or-namespace-name. */
4973 parser->scope = old_scope;
4974 parser->qualifying_scope = saved_qualifying_scope;
4976 /* If the next token is a decltype, and the one after that is a
4977 `::', then the decltype has failed to resolve to a class or
4978 enumeration type. Give this error even when parsing
4979 tentatively since it can't possibly be valid--and we're going
4980 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4981 won't get another chance.*/
4982 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4983 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4986 token = cp_lexer_consume_token (parser->lexer);
4987 error_at (token->location, "decltype evaluates to %qT, "
4988 "which is not a class or enumeration type",
4990 parser->scope = error_mark_node;
4994 cp_lexer_consume_token (parser->lexer);
4997 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4999 /* If the next token is an identifier, and the one after
5000 that is a `::', then any valid interpretation would have
5001 found a class-or-namespace-name. */
5002 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5003 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5005 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5008 token = cp_lexer_consume_token (parser->lexer);
5011 if (!token->ambiguous_p)
5014 tree ambiguous_decls;
5016 decl = cp_parser_lookup_name (parser, token->u.value,
5018 /*is_template=*/false,
5019 /*is_namespace=*/false,
5020 /*check_dependency=*/true,
5023 if (TREE_CODE (decl) == TEMPLATE_DECL)
5024 error_at (token->location,
5025 "%qD used without template parameters",
5027 else if (ambiguous_decls)
5029 error_at (token->location,
5030 "reference to %qD is ambiguous",
5032 print_candidates (ambiguous_decls);
5033 decl = error_mark_node;
5037 if (cxx_dialect != cxx98)
5038 cp_parser_name_lookup_error
5039 (parser, token->u.value, decl, NLE_NOT_CXX98,
5042 cp_parser_name_lookup_error
5043 (parser, token->u.value, decl, NLE_CXX98,
5047 parser->scope = error_mark_node;
5049 /* Treat this as a successful nested-name-specifier
5054 If the name found is not a class-name (clause
5055 _class_) or namespace-name (_namespace.def_), the
5056 program is ill-formed. */
5059 cp_lexer_consume_token (parser->lexer);
5063 /* We've found one valid nested-name-specifier. */
5065 /* Name lookup always gives us a DECL. */
5066 if (TREE_CODE (new_scope) == TYPE_DECL)
5067 new_scope = TREE_TYPE (new_scope);
5068 /* Uses of "template" must be followed by actual templates. */
5069 if (template_keyword_p
5070 && !(CLASS_TYPE_P (new_scope)
5071 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5072 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5073 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5074 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5075 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5076 == TEMPLATE_ID_EXPR)))
5077 permerror (input_location, TYPE_P (new_scope)
5078 ? G_("%qT is not a template")
5079 : G_("%qD is not a template"),
5081 /* If it is a class scope, try to complete it; we are about to
5082 be looking up names inside the class. */
5083 if (TYPE_P (new_scope)
5084 /* Since checking types for dependency can be expensive,
5085 avoid doing it if the type is already complete. */
5086 && !COMPLETE_TYPE_P (new_scope)
5087 /* Do not try to complete dependent types. */
5088 && !dependent_type_p (new_scope))
5090 new_scope = complete_type (new_scope);
5091 /* If it is a typedef to current class, use the current
5092 class instead, as the typedef won't have any names inside
5094 if (!COMPLETE_TYPE_P (new_scope)
5095 && currently_open_class (new_scope))
5096 new_scope = TYPE_MAIN_VARIANT (new_scope);
5098 /* Make sure we look in the right scope the next time through
5100 parser->scope = new_scope;
5103 /* If parsing tentatively, replace the sequence of tokens that makes
5104 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5105 token. That way, should we re-parse the token stream, we will
5106 not have to repeat the effort required to do the parse, nor will
5107 we issue duplicate error messages. */
5108 if (success && start)
5112 token = cp_lexer_token_at (parser->lexer, start);
5113 /* Reset the contents of the START token. */
5114 token->type = CPP_NESTED_NAME_SPECIFIER;
5115 /* Retrieve any deferred checks. Do not pop this access checks yet
5116 so the memory will not be reclaimed during token replacing below. */
5117 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5118 token->u.tree_check_value->value = parser->scope;
5119 token->u.tree_check_value->checks = get_deferred_access_checks ();
5120 token->u.tree_check_value->qualifying_scope =
5121 parser->qualifying_scope;
5122 token->keyword = RID_MAX;
5124 /* Purge all subsequent tokens. */
5125 cp_lexer_purge_tokens_after (parser->lexer, start);
5129 pop_to_parent_deferring_access_checks ();
5131 return success ? parser->scope : NULL_TREE;
5134 /* Parse a nested-name-specifier. See
5135 cp_parser_nested_name_specifier_opt for details. This function
5136 behaves identically, except that it will an issue an error if no
5137 nested-name-specifier is present. */
5140 cp_parser_nested_name_specifier (cp_parser *parser,
5141 bool typename_keyword_p,
5142 bool check_dependency_p,
5144 bool is_declaration)
5148 /* Look for the nested-name-specifier. */
5149 scope = cp_parser_nested_name_specifier_opt (parser,
5154 /* If it was not present, issue an error message. */
5157 cp_parser_error (parser, "expected nested-name-specifier");
5158 parser->scope = NULL_TREE;
5164 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5165 this is either a class-name or a namespace-name (which corresponds
5166 to the class-or-namespace-name production in the grammar). For
5167 C++0x, it can also be a type-name that refers to an enumeration
5168 type or a simple-template-id.
5170 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5171 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5172 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5173 TYPE_P is TRUE iff the next name should be taken as a class-name,
5174 even the same name is declared to be another entity in the same
5177 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5178 specified by the class-or-namespace-name. If neither is found the
5179 ERROR_MARK_NODE is returned. */
5182 cp_parser_qualifying_entity (cp_parser *parser,
5183 bool typename_keyword_p,
5184 bool template_keyword_p,
5185 bool check_dependency_p,
5187 bool is_declaration)
5190 tree saved_qualifying_scope;
5191 tree saved_object_scope;
5194 bool successful_parse_p;
5196 /* DR 743: decltype can appear in a nested-name-specifier. */
5197 if (cp_lexer_next_token_is_decltype (parser->lexer))
5199 scope = cp_parser_decltype (parser);
5200 if (TREE_CODE (scope) != ENUMERAL_TYPE
5201 && !MAYBE_CLASS_TYPE_P (scope))
5203 cp_parser_simulate_error (parser);
5204 return error_mark_node;
5206 if (TYPE_NAME (scope))
5207 scope = TYPE_NAME (scope);
5211 /* Before we try to parse the class-name, we must save away the
5212 current PARSER->SCOPE since cp_parser_class_name will destroy
5214 saved_scope = parser->scope;
5215 saved_qualifying_scope = parser->qualifying_scope;
5216 saved_object_scope = parser->object_scope;
5217 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5218 there is no need to look for a namespace-name. */
5219 only_class_p = template_keyword_p
5220 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5222 cp_parser_parse_tentatively (parser);
5223 scope = cp_parser_class_name (parser,
5226 type_p ? class_type : none_type,
5228 /*class_head_p=*/false,
5230 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5231 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5233 && cxx_dialect != cxx98
5234 && !successful_parse_p)
5236 /* Restore the saved scope. */
5237 parser->scope = saved_scope;
5238 parser->qualifying_scope = saved_qualifying_scope;
5239 parser->object_scope = saved_object_scope;
5241 /* Parse tentatively. */
5242 cp_parser_parse_tentatively (parser);
5244 /* Parse a type-name */
5245 scope = cp_parser_type_name (parser);
5247 /* "If the name found does not designate a namespace or a class,
5248 enumeration, or dependent type, the program is ill-formed."
5250 We cover classes and dependent types above and namespaces below,
5251 so this code is only looking for enums. */
5252 if (!scope || TREE_CODE (scope) != TYPE_DECL
5253 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5254 cp_parser_simulate_error (parser);
5256 successful_parse_p = cp_parser_parse_definitely (parser);
5258 /* If that didn't work, try for a namespace-name. */
5259 if (!only_class_p && !successful_parse_p)
5261 /* Restore the saved scope. */
5262 parser->scope = saved_scope;
5263 parser->qualifying_scope = saved_qualifying_scope;
5264 parser->object_scope = saved_object_scope;
5265 /* If we are not looking at an identifier followed by the scope
5266 resolution operator, then this is not part of a
5267 nested-name-specifier. (Note that this function is only used
5268 to parse the components of a nested-name-specifier.) */
5269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5270 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5271 return error_mark_node;
5272 scope = cp_parser_namespace_name (parser);
5278 /* Parse a postfix-expression.
5282 postfix-expression [ expression ]
5283 postfix-expression ( expression-list [opt] )
5284 simple-type-specifier ( expression-list [opt] )
5285 typename :: [opt] nested-name-specifier identifier
5286 ( expression-list [opt] )
5287 typename :: [opt] nested-name-specifier template [opt] template-id
5288 ( expression-list [opt] )
5289 postfix-expression . template [opt] id-expression
5290 postfix-expression -> template [opt] id-expression
5291 postfix-expression . pseudo-destructor-name
5292 postfix-expression -> pseudo-destructor-name
5293 postfix-expression ++
5294 postfix-expression --
5295 dynamic_cast < type-id > ( expression )
5296 static_cast < type-id > ( expression )
5297 reinterpret_cast < type-id > ( expression )
5298 const_cast < type-id > ( expression )
5299 typeid ( expression )
5305 ( type-id ) { initializer-list , [opt] }
5307 This extension is a GNU version of the C99 compound-literal
5308 construct. (The C99 grammar uses `type-name' instead of `type-id',
5309 but they are essentially the same concept.)
5311 If ADDRESS_P is true, the postfix expression is the operand of the
5312 `&' operator. CAST_P is true if this expression is the target of a
5315 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5316 class member access expressions [expr.ref].
5318 Returns a representation of the expression. */
5321 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5322 bool member_access_only_p,
5323 cp_id_kind * pidk_return)
5327 cp_id_kind idk = CP_ID_KIND_NONE;
5328 tree postfix_expression = NULL_TREE;
5329 bool is_member_access = false;
5331 /* Peek at the next token. */
5332 token = cp_lexer_peek_token (parser->lexer);
5333 /* Some of the productions are determined by keywords. */
5334 keyword = token->keyword;
5344 const char *saved_message;
5346 /* All of these can be handled in the same way from the point
5347 of view of parsing. Begin by consuming the token
5348 identifying the cast. */
5349 cp_lexer_consume_token (parser->lexer);
5351 /* New types cannot be defined in the cast. */
5352 saved_message = parser->type_definition_forbidden_message;
5353 parser->type_definition_forbidden_message
5354 = G_("types may not be defined in casts");
5356 /* Look for the opening `<'. */
5357 cp_parser_require (parser, CPP_LESS, RT_LESS);
5358 /* Parse the type to which we are casting. */
5359 type = cp_parser_type_id (parser);
5360 /* Look for the closing `>'. */
5361 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5362 /* Restore the old message. */
5363 parser->type_definition_forbidden_message = saved_message;
5365 /* And the expression which is being cast. */
5366 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5367 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5368 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5370 /* Only type conversions to integral or enumeration types
5371 can be used in constant-expressions. */
5372 if (!cast_valid_in_integral_constant_expression_p (type)
5373 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5374 return error_mark_node;
5380 = build_dynamic_cast (type, expression, tf_warning_or_error);
5384 = build_static_cast (type, expression, tf_warning_or_error);
5388 = build_reinterpret_cast (type, expression,
5389 tf_warning_or_error);
5393 = build_const_cast (type, expression, tf_warning_or_error);
5404 const char *saved_message;
5405 bool saved_in_type_id_in_expr_p;
5407 /* Consume the `typeid' token. */
5408 cp_lexer_consume_token (parser->lexer);
5409 /* Look for the `(' token. */
5410 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5411 /* Types cannot be defined in a `typeid' expression. */
5412 saved_message = parser->type_definition_forbidden_message;
5413 parser->type_definition_forbidden_message
5414 = G_("types may not be defined in a %<typeid%> expression");
5415 /* We can't be sure yet whether we're looking at a type-id or an
5417 cp_parser_parse_tentatively (parser);
5418 /* Try a type-id first. */
5419 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5420 parser->in_type_id_in_expr_p = true;
5421 type = cp_parser_type_id (parser);
5422 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5423 /* Look for the `)' token. Otherwise, we can't be sure that
5424 we're not looking at an expression: consider `typeid (int
5425 (3))', for example. */
5426 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5427 /* If all went well, simply lookup the type-id. */
5428 if (cp_parser_parse_definitely (parser))
5429 postfix_expression = get_typeid (type);
5430 /* Otherwise, fall back to the expression variant. */
5435 /* Look for an expression. */
5436 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5437 /* Compute its typeid. */
5438 postfix_expression = build_typeid (expression);
5439 /* Look for the `)' token. */
5440 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5442 /* Restore the saved message. */
5443 parser->type_definition_forbidden_message = saved_message;
5444 /* `typeid' may not appear in an integral constant expression. */
5445 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5446 return error_mark_node;
5453 /* The syntax permitted here is the same permitted for an
5454 elaborated-type-specifier. */
5455 type = cp_parser_elaborated_type_specifier (parser,
5456 /*is_friend=*/false,
5457 /*is_declaration=*/false);
5458 postfix_expression = cp_parser_functional_cast (parser, type);
5462 case RID_BUILTIN_SHUFFLE:
5467 location_t loc = token->location;
5469 cp_lexer_consume_token (parser->lexer);
5470 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5471 /*cast_p=*/false, /*allow_expansion_p=*/true,
5472 /*non_constant_p=*/NULL);
5474 return error_mark_node;
5476 FOR_EACH_VEC_ELT (tree, vec, i, p)
5479 if (VEC_length (tree, vec) == 2)
5481 c_build_vec_perm_expr
5482 (loc, VEC_index (tree, vec, 0),
5483 NULL_TREE, VEC_index (tree, vec, 1));
5485 else if (VEC_length (tree, vec) == 3)
5487 c_build_vec_perm_expr
5488 (loc, VEC_index (tree, vec, 0),
5489 VEC_index (tree, vec, 1),
5490 VEC_index (tree, vec, 2));
5493 error_at (loc, "wrong number of arguments to "
5494 "%<__builtin_shuffle%>");
5495 return error_mark_node;
5504 /* If the next thing is a simple-type-specifier, we may be
5505 looking at a functional cast. We could also be looking at
5506 an id-expression. So, we try the functional cast, and if
5507 that doesn't work we fall back to the primary-expression. */
5508 cp_parser_parse_tentatively (parser);
5509 /* Look for the simple-type-specifier. */
5510 type = cp_parser_simple_type_specifier (parser,
5511 /*decl_specs=*/NULL,
5512 CP_PARSER_FLAGS_NONE);
5513 /* Parse the cast itself. */
5514 if (!cp_parser_error_occurred (parser))
5516 = cp_parser_functional_cast (parser, type);
5517 /* If that worked, we're done. */
5518 if (cp_parser_parse_definitely (parser))
5521 /* If the functional-cast didn't work out, try a
5522 compound-literal. */
5523 if (cp_parser_allow_gnu_extensions_p (parser)
5524 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5526 VEC(constructor_elt,gc) *initializer_list = NULL;
5527 bool saved_in_type_id_in_expr_p;
5529 cp_parser_parse_tentatively (parser);
5530 /* Consume the `('. */
5531 cp_lexer_consume_token (parser->lexer);
5532 /* Parse the type. */
5533 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5534 parser->in_type_id_in_expr_p = true;
5535 type = cp_parser_type_id (parser);
5536 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5537 /* Look for the `)'. */
5538 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5539 /* Look for the `{'. */
5540 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5541 /* If things aren't going well, there's no need to
5543 if (!cp_parser_error_occurred (parser))
5545 bool non_constant_p;
5546 /* Parse the initializer-list. */
5548 = cp_parser_initializer_list (parser, &non_constant_p);
5549 /* Allow a trailing `,'. */
5550 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5551 cp_lexer_consume_token (parser->lexer);
5552 /* Look for the final `}'. */
5553 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5555 /* If that worked, we're definitely looking at a
5556 compound-literal expression. */
5557 if (cp_parser_parse_definitely (parser))
5559 /* Warn the user that a compound literal is not
5560 allowed in standard C++. */
5561 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5562 /* For simplicity, we disallow compound literals in
5563 constant-expressions. We could
5564 allow compound literals of integer type, whose
5565 initializer was a constant, in constant
5566 expressions. Permitting that usage, as a further
5567 extension, would not change the meaning of any
5568 currently accepted programs. (Of course, as
5569 compound literals are not part of ISO C++, the
5570 standard has nothing to say.) */
5571 if (cp_parser_non_integral_constant_expression (parser,
5574 postfix_expression = error_mark_node;
5577 /* Form the representation of the compound-literal. */
5579 = (finish_compound_literal
5580 (type, build_constructor (init_list_type_node,
5582 tf_warning_or_error));
5587 /* It must be a primary-expression. */
5589 = cp_parser_primary_expression (parser, address_p, cast_p,
5590 /*template_arg_p=*/false,
5596 /* Keep looping until the postfix-expression is complete. */
5599 if (idk == CP_ID_KIND_UNQUALIFIED
5600 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5601 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5602 /* It is not a Koenig lookup function call. */
5604 = unqualified_name_lookup_error (postfix_expression);
5606 /* Peek at the next token. */
5607 token = cp_lexer_peek_token (parser->lexer);
5609 switch (token->type)
5611 case CPP_OPEN_SQUARE:
5613 = cp_parser_postfix_open_square_expression (parser,
5616 idk = CP_ID_KIND_NONE;
5617 is_member_access = false;
5620 case CPP_OPEN_PAREN:
5621 /* postfix-expression ( expression-list [opt] ) */
5624 bool is_builtin_constant_p;
5625 bool saved_integral_constant_expression_p = false;
5626 bool saved_non_integral_constant_expression_p = false;
5629 is_member_access = false;
5631 is_builtin_constant_p
5632 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5633 if (is_builtin_constant_p)
5635 /* The whole point of __builtin_constant_p is to allow
5636 non-constant expressions to appear as arguments. */
5637 saved_integral_constant_expression_p
5638 = parser->integral_constant_expression_p;
5639 saved_non_integral_constant_expression_p
5640 = parser->non_integral_constant_expression_p;
5641 parser->integral_constant_expression_p = false;
5643 args = (cp_parser_parenthesized_expression_list
5645 /*cast_p=*/false, /*allow_expansion_p=*/true,
5646 /*non_constant_p=*/NULL));
5647 if (is_builtin_constant_p)
5649 parser->integral_constant_expression_p
5650 = saved_integral_constant_expression_p;
5651 parser->non_integral_constant_expression_p
5652 = saved_non_integral_constant_expression_p;
5657 postfix_expression = error_mark_node;
5661 /* Function calls are not permitted in
5662 constant-expressions. */
5663 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5664 && cp_parser_non_integral_constant_expression (parser,
5667 postfix_expression = error_mark_node;
5668 release_tree_vector (args);
5673 if (idk == CP_ID_KIND_UNQUALIFIED
5674 || idk == CP_ID_KIND_TEMPLATE_ID)
5676 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5678 if (!VEC_empty (tree, args))
5681 if (!any_type_dependent_arguments_p (args))
5683 = perform_koenig_lookup (postfix_expression, args,
5684 /*include_std=*/false,
5685 tf_warning_or_error);
5689 = unqualified_fn_lookup_error (postfix_expression);
5691 /* We do not perform argument-dependent lookup if
5692 normal lookup finds a non-function, in accordance
5693 with the expected resolution of DR 218. */
5694 else if (!VEC_empty (tree, args)
5695 && is_overloaded_fn (postfix_expression))
5697 tree fn = get_first_fn (postfix_expression);
5698 fn = STRIP_TEMPLATE (fn);
5700 /* Do not do argument dependent lookup if regular
5701 lookup finds a member function or a block-scope
5702 function declaration. [basic.lookup.argdep]/3 */
5703 if (!DECL_FUNCTION_MEMBER_P (fn)
5704 && !DECL_LOCAL_FUNCTION_P (fn))
5707 if (!any_type_dependent_arguments_p (args))
5709 = perform_koenig_lookup (postfix_expression, args,
5710 /*include_std=*/false,
5711 tf_warning_or_error);
5716 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5718 tree instance = TREE_OPERAND (postfix_expression, 0);
5719 tree fn = TREE_OPERAND (postfix_expression, 1);
5721 if (processing_template_decl
5722 && (type_dependent_expression_p (instance)
5723 || (!BASELINK_P (fn)
5724 && TREE_CODE (fn) != FIELD_DECL)
5725 || type_dependent_expression_p (fn)
5726 || any_type_dependent_arguments_p (args)))
5729 = build_nt_call_vec (postfix_expression, args);
5730 release_tree_vector (args);
5734 if (BASELINK_P (fn))
5737 = (build_new_method_call
5738 (instance, fn, &args, NULL_TREE,
5739 (idk == CP_ID_KIND_QUALIFIED
5740 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5743 tf_warning_or_error));
5747 = finish_call_expr (postfix_expression, &args,
5748 /*disallow_virtual=*/false,
5750 tf_warning_or_error);
5752 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5753 || TREE_CODE (postfix_expression) == MEMBER_REF
5754 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5755 postfix_expression = (build_offset_ref_call_from_tree
5756 (postfix_expression, &args));
5757 else if (idk == CP_ID_KIND_QUALIFIED)
5758 /* A call to a static class member, or a namespace-scope
5761 = finish_call_expr (postfix_expression, &args,
5762 /*disallow_virtual=*/true,
5764 tf_warning_or_error);
5766 /* All other function calls. */
5768 = finish_call_expr (postfix_expression, &args,
5769 /*disallow_virtual=*/false,
5771 tf_warning_or_error);
5773 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5774 idk = CP_ID_KIND_NONE;
5776 release_tree_vector (args);
5782 /* postfix-expression . template [opt] id-expression
5783 postfix-expression . pseudo-destructor-name
5784 postfix-expression -> template [opt] id-expression
5785 postfix-expression -> pseudo-destructor-name */
5787 /* Consume the `.' or `->' operator. */
5788 cp_lexer_consume_token (parser->lexer);
5791 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5796 is_member_access = true;
5800 /* postfix-expression ++ */
5801 /* Consume the `++' token. */
5802 cp_lexer_consume_token (parser->lexer);
5803 /* Generate a representation for the complete expression. */
5805 = finish_increment_expr (postfix_expression,
5806 POSTINCREMENT_EXPR);
5807 /* Increments may not appear in constant-expressions. */
5808 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5809 postfix_expression = error_mark_node;
5810 idk = CP_ID_KIND_NONE;
5811 is_member_access = false;
5814 case CPP_MINUS_MINUS:
5815 /* postfix-expression -- */
5816 /* Consume the `--' token. */
5817 cp_lexer_consume_token (parser->lexer);
5818 /* Generate a representation for the complete expression. */
5820 = finish_increment_expr (postfix_expression,
5821 POSTDECREMENT_EXPR);
5822 /* Decrements may not appear in constant-expressions. */
5823 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5824 postfix_expression = error_mark_node;
5825 idk = CP_ID_KIND_NONE;
5826 is_member_access = false;
5830 if (pidk_return != NULL)
5831 * pidk_return = idk;
5832 if (member_access_only_p)
5833 return is_member_access? postfix_expression : error_mark_node;
5835 return postfix_expression;
5839 /* We should never get here. */
5841 return error_mark_node;
5844 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5845 by cp_parser_builtin_offsetof. We're looking for
5847 postfix-expression [ expression ]
5848 postfix-expression [ braced-init-list ] (C++11)
5850 FOR_OFFSETOF is set if we're being called in that context, which
5851 changes how we deal with integer constant expressions. */
5854 cp_parser_postfix_open_square_expression (cp_parser *parser,
5855 tree postfix_expression,
5859 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5861 /* Consume the `[' token. */
5862 cp_lexer_consume_token (parser->lexer);
5864 /* Parse the index expression. */
5865 /* ??? For offsetof, there is a question of what to allow here. If
5866 offsetof is not being used in an integral constant expression context,
5867 then we *could* get the right answer by computing the value at runtime.
5868 If we are in an integral constant expression context, then we might
5869 could accept any constant expression; hard to say without analysis.
5870 Rather than open the barn door too wide right away, allow only integer
5871 constant expressions here. */
5873 index = cp_parser_constant_expression (parser, false, NULL);
5876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5878 bool expr_nonconst_p;
5879 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5880 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5883 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5886 /* Look for the closing `]'. */
5887 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5889 /* Build the ARRAY_REF. */
5890 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5892 /* When not doing offsetof, array references are not permitted in
5893 constant-expressions. */
5895 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5896 postfix_expression = error_mark_node;
5898 return postfix_expression;
5901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5902 by cp_parser_builtin_offsetof. We're looking for
5904 postfix-expression . template [opt] id-expression
5905 postfix-expression . pseudo-destructor-name
5906 postfix-expression -> template [opt] id-expression
5907 postfix-expression -> pseudo-destructor-name
5909 FOR_OFFSETOF is set if we're being called in that context. That sorta
5910 limits what of the above we'll actually accept, but nevermind.
5911 TOKEN_TYPE is the "." or "->" token, which will already have been
5912 removed from the stream. */
5915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5916 enum cpp_ttype token_type,
5917 tree postfix_expression,
5918 bool for_offsetof, cp_id_kind *idk,
5919 location_t location)
5923 bool pseudo_destructor_p;
5924 tree scope = NULL_TREE;
5926 /* If this is a `->' operator, dereference the pointer. */
5927 if (token_type == CPP_DEREF)
5928 postfix_expression = build_x_arrow (location, postfix_expression,
5929 tf_warning_or_error);
5930 /* Check to see whether or not the expression is type-dependent. */
5931 dependent_p = type_dependent_expression_p (postfix_expression);
5932 /* The identifier following the `->' or `.' is not qualified. */
5933 parser->scope = NULL_TREE;
5934 parser->qualifying_scope = NULL_TREE;
5935 parser->object_scope = NULL_TREE;
5936 *idk = CP_ID_KIND_NONE;
5938 /* Enter the scope corresponding to the type of the object
5939 given by the POSTFIX_EXPRESSION. */
5940 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5942 scope = TREE_TYPE (postfix_expression);
5943 /* According to the standard, no expression should ever have
5944 reference type. Unfortunately, we do not currently match
5945 the standard in this respect in that our internal representation
5946 of an expression may have reference type even when the standard
5947 says it does not. Therefore, we have to manually obtain the
5948 underlying type here. */
5949 scope = non_reference (scope);
5950 /* The type of the POSTFIX_EXPRESSION must be complete. */
5951 if (scope == unknown_type_node)
5953 error_at (location, "%qE does not have class type",
5954 postfix_expression);
5957 /* Unlike the object expression in other contexts, *this is not
5958 required to be of complete type for purposes of class member
5959 access (5.2.5) outside the member function body. */
5960 else if (scope != current_class_ref
5961 && !(processing_template_decl && scope == current_class_type))
5962 scope = complete_type_or_else (scope, NULL_TREE);
5963 /* Let the name lookup machinery know that we are processing a
5964 class member access expression. */
5965 parser->context->object_type = scope;
5966 /* If something went wrong, we want to be able to discern that case,
5967 as opposed to the case where there was no SCOPE due to the type
5968 of expression being dependent. */
5970 scope = error_mark_node;
5971 /* If the SCOPE was erroneous, make the various semantic analysis
5972 functions exit quickly -- and without issuing additional error
5974 if (scope == error_mark_node)
5975 postfix_expression = error_mark_node;
5978 /* Assume this expression is not a pseudo-destructor access. */
5979 pseudo_destructor_p = false;
5981 /* If the SCOPE is a scalar type, then, if this is a valid program,
5982 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5983 is type dependent, it can be pseudo-destructor-name or something else.
5984 Try to parse it as pseudo-destructor-name first. */
5985 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5990 cp_parser_parse_tentatively (parser);
5991 /* Parse the pseudo-destructor-name. */
5993 cp_parser_pseudo_destructor_name (parser, &s, &type);
5995 && (cp_parser_error_occurred (parser)
5996 || TREE_CODE (type) != TYPE_DECL
5997 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5998 cp_parser_abort_tentative_parse (parser);
5999 else if (cp_parser_parse_definitely (parser))
6001 pseudo_destructor_p = true;
6003 = finish_pseudo_destructor_expr (postfix_expression,
6004 s, TREE_TYPE (type));
6008 if (!pseudo_destructor_p)
6010 /* If the SCOPE is not a scalar type, we are looking at an
6011 ordinary class member access expression, rather than a
6012 pseudo-destructor-name. */
6014 cp_token *token = cp_lexer_peek_token (parser->lexer);
6015 /* Parse the id-expression. */
6016 name = (cp_parser_id_expression
6018 cp_parser_optional_template_keyword (parser),
6019 /*check_dependency_p=*/true,
6021 /*declarator_p=*/false,
6022 /*optional_p=*/false));
6023 /* In general, build a SCOPE_REF if the member name is qualified.
6024 However, if the name was not dependent and has already been
6025 resolved; there is no need to build the SCOPE_REF. For example;
6027 struct X { void f(); };
6028 template <typename T> void f(T* t) { t->X::f(); }
6030 Even though "t" is dependent, "X::f" is not and has been resolved
6031 to a BASELINK; there is no need to include scope information. */
6033 /* But we do need to remember that there was an explicit scope for
6034 virtual function calls. */
6036 *idk = CP_ID_KIND_QUALIFIED;
6038 /* If the name is a template-id that names a type, we will get a
6039 TYPE_DECL here. That is invalid code. */
6040 if (TREE_CODE (name) == TYPE_DECL)
6042 error_at (token->location, "invalid use of %qD", name);
6043 postfix_expression = error_mark_node;
6047 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6049 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6051 error_at (token->location, "%<%D::%D%> is not a class member",
6052 parser->scope, name);
6053 postfix_expression = error_mark_node;
6056 name = build_qualified_name (/*type=*/NULL_TREE,
6060 parser->scope = NULL_TREE;
6061 parser->qualifying_scope = NULL_TREE;
6062 parser->object_scope = NULL_TREE;
6064 if (parser->scope && name && BASELINK_P (name))
6065 adjust_result_of_qualified_name_lookup
6066 (name, parser->scope, scope);
6068 = finish_class_member_access_expr (postfix_expression, name,
6070 tf_warning_or_error);
6074 /* We no longer need to look up names in the scope of the object on
6075 the left-hand side of the `.' or `->' operator. */
6076 parser->context->object_type = NULL_TREE;
6078 /* Outside of offsetof, these operators may not appear in
6079 constant-expressions. */
6081 && (cp_parser_non_integral_constant_expression
6082 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6083 postfix_expression = error_mark_node;
6085 return postfix_expression;
6088 /* Parse a parenthesized expression-list.
6091 assignment-expression
6092 expression-list, assignment-expression
6097 identifier, expression-list
6099 CAST_P is true if this expression is the target of a cast.
6101 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6104 Returns a vector of trees. Each element is a representation of an
6105 assignment-expression. NULL is returned if the ( and or ) are
6106 missing. An empty, but allocated, vector is returned on no
6107 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6108 if we are parsing an attribute list for an attribute that wants a
6109 plain identifier argument, normal_attr for an attribute that wants
6110 an expression, or non_attr if we aren't parsing an attribute list. If
6111 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6112 not all of the expressions in the list were constant. */
6114 static VEC(tree,gc) *
6115 cp_parser_parenthesized_expression_list (cp_parser* parser,
6116 int is_attribute_list,
6118 bool allow_expansion_p,
6119 bool *non_constant_p)
6121 VEC(tree,gc) *expression_list;
6122 bool fold_expr_p = is_attribute_list != non_attr;
6123 tree identifier = NULL_TREE;
6124 bool saved_greater_than_is_operator_p;
6126 /* Assume all the expressions will be constant. */
6128 *non_constant_p = false;
6130 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6133 expression_list = make_tree_vector ();
6135 /* Within a parenthesized expression, a `>' token is always
6136 the greater-than operator. */
6137 saved_greater_than_is_operator_p
6138 = parser->greater_than_is_operator_p;
6139 parser->greater_than_is_operator_p = true;
6141 /* Consume expressions until there are no more. */
6142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6147 /* At the beginning of attribute lists, check to see if the
6148 next token is an identifier. */
6149 if (is_attribute_list == id_attr
6150 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6154 /* Consume the identifier. */
6155 token = cp_lexer_consume_token (parser->lexer);
6156 /* Save the identifier. */
6157 identifier = token->u.value;
6161 bool expr_non_constant_p;
6163 /* Parse the next assignment-expression. */
6164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6166 /* A braced-init-list. */
6167 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6168 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6169 if (non_constant_p && expr_non_constant_p)
6170 *non_constant_p = true;
6172 else if (non_constant_p)
6174 expr = (cp_parser_constant_expression
6175 (parser, /*allow_non_constant_p=*/true,
6176 &expr_non_constant_p));
6177 if (expr_non_constant_p)
6178 *non_constant_p = true;
6181 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6184 expr = fold_non_dependent_expr (expr);
6186 /* If we have an ellipsis, then this is an expression
6188 if (allow_expansion_p
6189 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6191 /* Consume the `...'. */
6192 cp_lexer_consume_token (parser->lexer);
6194 /* Build the argument pack. */
6195 expr = make_pack_expansion (expr);
6198 /* Add it to the list. We add error_mark_node
6199 expressions to the list, so that we can still tell if
6200 the correct form for a parenthesized expression-list
6201 is found. That gives better errors. */
6202 VEC_safe_push (tree, gc, expression_list, expr);
6204 if (expr == error_mark_node)
6208 /* After the first item, attribute lists look the same as
6209 expression lists. */
6210 is_attribute_list = non_attr;
6213 /* If the next token isn't a `,', then we are done. */
6214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6217 /* Otherwise, consume the `,' and keep going. */
6218 cp_lexer_consume_token (parser->lexer);
6221 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6226 /* We try and resync to an unnested comma, as that will give the
6227 user better diagnostics. */
6228 ending = cp_parser_skip_to_closing_parenthesis (parser,
6229 /*recovering=*/true,
6231 /*consume_paren=*/true);
6236 parser->greater_than_is_operator_p
6237 = saved_greater_than_is_operator_p;
6242 parser->greater_than_is_operator_p
6243 = saved_greater_than_is_operator_p;
6246 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6248 return expression_list;
6251 /* Parse a pseudo-destructor-name.
6253 pseudo-destructor-name:
6254 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6255 :: [opt] nested-name-specifier template template-id :: ~ type-name
6256 :: [opt] nested-name-specifier [opt] ~ type-name
6258 If either of the first two productions is used, sets *SCOPE to the
6259 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6260 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6261 or ERROR_MARK_NODE if the parse fails. */
6264 cp_parser_pseudo_destructor_name (cp_parser* parser,
6268 bool nested_name_specifier_p;
6270 /* Assume that things will not work out. */
6271 *type = error_mark_node;
6273 /* Look for the optional `::' operator. */
6274 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6275 /* Look for the optional nested-name-specifier. */
6276 nested_name_specifier_p
6277 = (cp_parser_nested_name_specifier_opt (parser,
6278 /*typename_keyword_p=*/false,
6279 /*check_dependency_p=*/true,
6281 /*is_declaration=*/false)
6283 /* Now, if we saw a nested-name-specifier, we might be doing the
6284 second production. */
6285 if (nested_name_specifier_p
6286 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6288 /* Consume the `template' keyword. */
6289 cp_lexer_consume_token (parser->lexer);
6290 /* Parse the template-id. */
6291 cp_parser_template_id (parser,
6292 /*template_keyword_p=*/true,
6293 /*check_dependency_p=*/false,
6295 /*is_declaration=*/true);
6296 /* Look for the `::' token. */
6297 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6299 /* If the next token is not a `~', then there might be some
6300 additional qualification. */
6301 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6303 /* At this point, we're looking for "type-name :: ~". The type-name
6304 must not be a class-name, since this is a pseudo-destructor. So,
6305 it must be either an enum-name, or a typedef-name -- both of which
6306 are just identifiers. So, we peek ahead to check that the "::"
6307 and "~" tokens are present; if they are not, then we can avoid
6308 calling type_name. */
6309 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6310 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6311 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6313 cp_parser_error (parser, "non-scalar type");
6317 /* Look for the type-name. */
6318 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6319 if (*scope == error_mark_node)
6322 /* Look for the `::' token. */
6323 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6328 /* Look for the `~'. */
6329 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6331 /* Once we see the ~, this has to be a pseudo-destructor. */
6332 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6333 cp_parser_commit_to_tentative_parse (parser);
6335 /* Look for the type-name again. We are not responsible for
6336 checking that it matches the first type-name. */
6337 *type = cp_parser_nonclass_name (parser);
6340 /* Parse a unary-expression.
6346 unary-operator cast-expression
6347 sizeof unary-expression
6349 alignof ( type-id ) [C++0x]
6356 __extension__ cast-expression
6357 __alignof__ unary-expression
6358 __alignof__ ( type-id )
6359 alignof unary-expression [C++0x]
6360 __real__ cast-expression
6361 __imag__ cast-expression
6364 ADDRESS_P is true iff the unary-expression is appearing as the
6365 operand of the `&' operator. CAST_P is true if this expression is
6366 the target of a cast.
6368 Returns a representation of the expression. */
6371 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6375 enum tree_code unary_operator;
6377 /* Peek at the next token. */
6378 token = cp_lexer_peek_token (parser->lexer);
6379 /* Some keywords give away the kind of expression. */
6380 if (token->type == CPP_KEYWORD)
6382 enum rid keyword = token->keyword;
6392 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6393 /* Consume the token. */
6394 cp_lexer_consume_token (parser->lexer);
6395 /* Parse the operand. */
6396 operand = cp_parser_sizeof_operand (parser, keyword);
6398 if (TYPE_P (operand))
6399 return cxx_sizeof_or_alignof_type (operand, op, true);
6402 /* ISO C++ defines alignof only with types, not with
6403 expressions. So pedwarn if alignof is used with a non-
6404 type expression. However, __alignof__ is ok. */
6405 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6406 pedwarn (token->location, OPT_Wpedantic,
6407 "ISO C++ does not allow %<alignof%> "
6410 return cxx_sizeof_or_alignof_expr (operand, op, true);
6415 return cp_parser_new_expression (parser);
6418 return cp_parser_delete_expression (parser);
6422 /* The saved value of the PEDANTIC flag. */
6426 /* Save away the PEDANTIC flag. */
6427 cp_parser_extension_opt (parser, &saved_pedantic);
6428 /* Parse the cast-expression. */
6429 expr = cp_parser_simple_cast_expression (parser);
6430 /* Restore the PEDANTIC flag. */
6431 pedantic = saved_pedantic;
6441 /* Consume the `__real__' or `__imag__' token. */
6442 cp_lexer_consume_token (parser->lexer);
6443 /* Parse the cast-expression. */
6444 expression = cp_parser_simple_cast_expression (parser);
6445 /* Create the complete representation. */
6446 return build_x_unary_op (token->location,
6447 (keyword == RID_REALPART
6448 ? REALPART_EXPR : IMAGPART_EXPR),
6450 tf_warning_or_error);
6454 case RID_TRANSACTION_ATOMIC:
6455 case RID_TRANSACTION_RELAXED:
6456 return cp_parser_transaction_expression (parser, keyword);
6461 const char *saved_message;
6462 bool saved_integral_constant_expression_p;
6463 bool saved_non_integral_constant_expression_p;
6464 bool saved_greater_than_is_operator_p;
6466 cp_lexer_consume_token (parser->lexer);
6467 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6469 saved_message = parser->type_definition_forbidden_message;
6470 parser->type_definition_forbidden_message
6471 = G_("types may not be defined in %<noexcept%> expressions");
6473 saved_integral_constant_expression_p
6474 = parser->integral_constant_expression_p;
6475 saved_non_integral_constant_expression_p
6476 = parser->non_integral_constant_expression_p;
6477 parser->integral_constant_expression_p = false;
6479 saved_greater_than_is_operator_p
6480 = parser->greater_than_is_operator_p;
6481 parser->greater_than_is_operator_p = true;
6483 ++cp_unevaluated_operand;
6484 ++c_inhibit_evaluation_warnings;
6485 expr = cp_parser_expression (parser, false, NULL);
6486 --c_inhibit_evaluation_warnings;
6487 --cp_unevaluated_operand;
6489 parser->greater_than_is_operator_p
6490 = saved_greater_than_is_operator_p;
6492 parser->integral_constant_expression_p
6493 = saved_integral_constant_expression_p;
6494 parser->non_integral_constant_expression_p
6495 = saved_non_integral_constant_expression_p;
6497 parser->type_definition_forbidden_message = saved_message;
6499 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6500 return finish_noexcept_expr (expr, tf_warning_or_error);
6508 /* Look for the `:: new' and `:: delete', which also signal the
6509 beginning of a new-expression, or delete-expression,
6510 respectively. If the next token is `::', then it might be one of
6512 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6516 /* See if the token after the `::' is one of the keywords in
6517 which we're interested. */
6518 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6519 /* If it's `new', we have a new-expression. */
6520 if (keyword == RID_NEW)
6521 return cp_parser_new_expression (parser);
6522 /* Similarly, for `delete'. */
6523 else if (keyword == RID_DELETE)
6524 return cp_parser_delete_expression (parser);
6527 /* Look for a unary operator. */
6528 unary_operator = cp_parser_unary_operator (token);
6529 /* The `++' and `--' operators can be handled similarly, even though
6530 they are not technically unary-operators in the grammar. */
6531 if (unary_operator == ERROR_MARK)
6533 if (token->type == CPP_PLUS_PLUS)
6534 unary_operator = PREINCREMENT_EXPR;
6535 else if (token->type == CPP_MINUS_MINUS)
6536 unary_operator = PREDECREMENT_EXPR;
6537 /* Handle the GNU address-of-label extension. */
6538 else if (cp_parser_allow_gnu_extensions_p (parser)
6539 && token->type == CPP_AND_AND)
6543 location_t loc = token->location;
6545 /* Consume the '&&' token. */
6546 cp_lexer_consume_token (parser->lexer);
6547 /* Look for the identifier. */
6548 identifier = cp_parser_identifier (parser);
6549 /* Create an expression representing the address. */
6550 expression = finish_label_address_expr (identifier, loc);
6551 if (cp_parser_non_integral_constant_expression (parser,
6553 expression = error_mark_node;
6557 if (unary_operator != ERROR_MARK)
6559 tree cast_expression;
6560 tree expression = error_mark_node;
6561 non_integral_constant non_constant_p = NIC_NONE;
6562 location_t loc = token->location;
6564 /* Consume the operator token. */
6565 token = cp_lexer_consume_token (parser->lexer);
6566 /* Parse the cast-expression. */
6568 = cp_parser_cast_expression (parser,
6569 unary_operator == ADDR_EXPR,
6570 /*cast_p=*/false, pidk);
6571 /* Now, build an appropriate representation. */
6572 switch (unary_operator)
6575 non_constant_p = NIC_STAR;
6576 expression = build_x_indirect_ref (loc, cast_expression,
6578 tf_warning_or_error);
6582 non_constant_p = NIC_ADDR;
6585 expression = build_x_unary_op (loc, unary_operator,
6587 tf_warning_or_error);
6590 case PREINCREMENT_EXPR:
6591 case PREDECREMENT_EXPR:
6592 non_constant_p = unary_operator == PREINCREMENT_EXPR
6593 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6595 case UNARY_PLUS_EXPR:
6597 case TRUTH_NOT_EXPR:
6598 expression = finish_unary_op_expr (loc, unary_operator,
6606 if (non_constant_p != NIC_NONE
6607 && cp_parser_non_integral_constant_expression (parser,
6609 expression = error_mark_node;
6614 return cp_parser_postfix_expression (parser, address_p, cast_p,
6615 /*member_access_only_p=*/false,
6619 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6620 unary-operator, the corresponding tree code is returned. */
6622 static enum tree_code
6623 cp_parser_unary_operator (cp_token* token)
6625 switch (token->type)
6628 return INDIRECT_REF;
6634 return UNARY_PLUS_EXPR;
6640 return TRUTH_NOT_EXPR;
6643 return BIT_NOT_EXPR;
6650 /* Parse a new-expression.
6653 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6654 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6656 Returns a representation of the expression. */
6659 cp_parser_new_expression (cp_parser* parser)
6661 bool global_scope_p;
6662 VEC(tree,gc) *placement;
6664 VEC(tree,gc) *initializer;
6665 tree nelts = NULL_TREE;
6668 /* Look for the optional `::' operator. */
6670 = (cp_parser_global_scope_opt (parser,
6671 /*current_scope_valid_p=*/false)
6673 /* Look for the `new' operator. */
6674 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6675 /* There's no easy way to tell a new-placement from the
6676 `( type-id )' construct. */
6677 cp_parser_parse_tentatively (parser);
6678 /* Look for a new-placement. */
6679 placement = cp_parser_new_placement (parser);
6680 /* If that didn't work out, there's no new-placement. */
6681 if (!cp_parser_parse_definitely (parser))
6683 if (placement != NULL)
6684 release_tree_vector (placement);
6688 /* If the next token is a `(', then we have a parenthesized
6690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6693 const char *saved_message = parser->type_definition_forbidden_message;
6695 /* Consume the `('. */
6696 cp_lexer_consume_token (parser->lexer);
6698 /* Parse the type-id. */
6699 parser->type_definition_forbidden_message
6700 = G_("types may not be defined in a new-expression");
6701 type = cp_parser_type_id (parser);
6702 parser->type_definition_forbidden_message = saved_message;
6704 /* Look for the closing `)'. */
6705 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6706 token = cp_lexer_peek_token (parser->lexer);
6707 /* There should not be a direct-new-declarator in this production,
6708 but GCC used to allowed this, so we check and emit a sensible error
6709 message for this case. */
6710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6712 error_at (token->location,
6713 "array bound forbidden after parenthesized type-id");
6714 inform (token->location,
6715 "try removing the parentheses around the type-id");
6716 cp_parser_direct_new_declarator (parser);
6719 /* Otherwise, there must be a new-type-id. */
6721 type = cp_parser_new_type_id (parser, &nelts);
6723 /* If the next token is a `(' or '{', then we have a new-initializer. */
6724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6725 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6726 initializer = cp_parser_new_initializer (parser);
6730 /* A new-expression may not appear in an integral constant
6732 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6733 ret = error_mark_node;
6736 /* Create a representation of the new-expression. */
6737 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6738 tf_warning_or_error);
6741 if (placement != NULL)
6742 release_tree_vector (placement);
6743 if (initializer != NULL)
6744 release_tree_vector (initializer);
6749 /* Parse a new-placement.
6754 Returns the same representation as for an expression-list. */
6756 static VEC(tree,gc) *
6757 cp_parser_new_placement (cp_parser* parser)
6759 VEC(tree,gc) *expression_list;
6761 /* Parse the expression-list. */
6762 expression_list = (cp_parser_parenthesized_expression_list
6763 (parser, non_attr, /*cast_p=*/false,
6764 /*allow_expansion_p=*/true,
6765 /*non_constant_p=*/NULL));
6767 return expression_list;
6770 /* Parse a new-type-id.
6773 type-specifier-seq new-declarator [opt]
6775 Returns the TYPE allocated. If the new-type-id indicates an array
6776 type, *NELTS is set to the number of elements in the last array
6777 bound; the TYPE will not include the last array bound. */
6780 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6782 cp_decl_specifier_seq type_specifier_seq;
6783 cp_declarator *new_declarator;
6784 cp_declarator *declarator;
6785 cp_declarator *outer_declarator;
6786 const char *saved_message;
6788 /* The type-specifier sequence must not contain type definitions.
6789 (It cannot contain declarations of new types either, but if they
6790 are not definitions we will catch that because they are not
6792 saved_message = parser->type_definition_forbidden_message;
6793 parser->type_definition_forbidden_message
6794 = G_("types may not be defined in a new-type-id");
6795 /* Parse the type-specifier-seq. */
6796 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6797 /*is_trailing_return=*/false,
6798 &type_specifier_seq);
6799 /* Restore the old message. */
6800 parser->type_definition_forbidden_message = saved_message;
6802 if (type_specifier_seq.type == error_mark_node)
6803 return error_mark_node;
6805 /* Parse the new-declarator. */
6806 new_declarator = cp_parser_new_declarator_opt (parser);
6808 /* Determine the number of elements in the last array dimension, if
6811 /* Skip down to the last array dimension. */
6812 declarator = new_declarator;
6813 outer_declarator = NULL;
6814 while (declarator && (declarator->kind == cdk_pointer
6815 || declarator->kind == cdk_ptrmem))
6817 outer_declarator = declarator;
6818 declarator = declarator->declarator;
6821 && declarator->kind == cdk_array
6822 && declarator->declarator
6823 && declarator->declarator->kind == cdk_array)
6825 outer_declarator = declarator;
6826 declarator = declarator->declarator;
6829 if (declarator && declarator->kind == cdk_array)
6831 *nelts = declarator->u.array.bounds;
6832 if (*nelts == error_mark_node)
6833 *nelts = integer_one_node;
6835 if (outer_declarator)
6836 outer_declarator->declarator = declarator->declarator;
6838 new_declarator = NULL;
6841 return groktypename (&type_specifier_seq, new_declarator, false);
6844 /* Parse an (optional) new-declarator.
6847 ptr-operator new-declarator [opt]
6848 direct-new-declarator
6850 Returns the declarator. */
6852 static cp_declarator *
6853 cp_parser_new_declarator_opt (cp_parser* parser)
6855 enum tree_code code;
6857 cp_cv_quals cv_quals;
6859 /* We don't know if there's a ptr-operator next, or not. */
6860 cp_parser_parse_tentatively (parser);
6861 /* Look for a ptr-operator. */
6862 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6863 /* If that worked, look for more new-declarators. */
6864 if (cp_parser_parse_definitely (parser))
6866 cp_declarator *declarator;
6868 /* Parse another optional declarator. */
6869 declarator = cp_parser_new_declarator_opt (parser);
6871 return cp_parser_make_indirect_declarator
6872 (code, type, cv_quals, declarator);
6875 /* If the next token is a `[', there is a direct-new-declarator. */
6876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6877 return cp_parser_direct_new_declarator (parser);
6882 /* Parse a direct-new-declarator.
6884 direct-new-declarator:
6886 direct-new-declarator [constant-expression]
6890 static cp_declarator *
6891 cp_parser_direct_new_declarator (cp_parser* parser)
6893 cp_declarator *declarator = NULL;
6900 /* Look for the opening `['. */
6901 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6903 token = cp_lexer_peek_token (parser->lexer);
6904 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6905 /* The standard requires that the expression have integral
6906 type. DR 74 adds enumeration types. We believe that the
6907 real intent is that these expressions be handled like the
6908 expression in a `switch' condition, which also allows
6909 classes with a single conversion to integral or
6910 enumeration type. */
6911 if (!processing_template_decl)
6914 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6919 error_at (token->location,
6920 "expression in new-declarator must have integral "
6921 "or enumeration type");
6922 expression = error_mark_node;
6926 /* Look for the closing `]'. */
6927 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6929 /* Add this bound to the declarator. */
6930 declarator = make_array_declarator (declarator, expression);
6932 /* If the next token is not a `[', then there are no more
6934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6941 /* Parse a new-initializer.
6944 ( expression-list [opt] )
6947 Returns a representation of the expression-list. */
6949 static VEC(tree,gc) *
6950 cp_parser_new_initializer (cp_parser* parser)
6952 VEC(tree,gc) *expression_list;
6954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6957 bool expr_non_constant_p;
6958 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6959 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6960 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6961 expression_list = make_tree_vector_single (t);
6964 expression_list = (cp_parser_parenthesized_expression_list
6965 (parser, non_attr, /*cast_p=*/false,
6966 /*allow_expansion_p=*/true,
6967 /*non_constant_p=*/NULL));
6969 return expression_list;
6972 /* Parse a delete-expression.
6975 :: [opt] delete cast-expression
6976 :: [opt] delete [ ] cast-expression
6978 Returns a representation of the expression. */
6981 cp_parser_delete_expression (cp_parser* parser)
6983 bool global_scope_p;
6987 /* Look for the optional `::' operator. */
6989 = (cp_parser_global_scope_opt (parser,
6990 /*current_scope_valid_p=*/false)
6992 /* Look for the `delete' keyword. */
6993 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6994 /* See if the array syntax is in use. */
6995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6997 /* Consume the `[' token. */
6998 cp_lexer_consume_token (parser->lexer);
6999 /* Look for the `]' token. */
7000 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7001 /* Remember that this is the `[]' construct. */
7007 /* Parse the cast-expression. */
7008 expression = cp_parser_simple_cast_expression (parser);
7010 /* A delete-expression may not appear in an integral constant
7012 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7013 return error_mark_node;
7015 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7016 tf_warning_or_error);
7019 /* Returns true if TOKEN may start a cast-expression and false
7023 cp_parser_token_starts_cast_expression (cp_token *token)
7025 switch (token->type)
7031 case CPP_CLOSE_SQUARE:
7032 case CPP_CLOSE_PAREN:
7033 case CPP_CLOSE_BRACE:
7037 case CPP_DEREF_STAR:
7045 case CPP_GREATER_EQ:
7065 /* '[' may start a primary-expression in obj-c++. */
7066 case CPP_OPEN_SQUARE:
7067 return c_dialect_objc ();
7074 /* Parse a cast-expression.
7078 ( type-id ) cast-expression
7080 ADDRESS_P is true iff the unary-expression is appearing as the
7081 operand of the `&' operator. CAST_P is true if this expression is
7082 the target of a cast.
7084 Returns a representation of the expression. */
7087 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7090 /* If it's a `(', then we might be looking at a cast. */
7091 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7093 tree type = NULL_TREE;
7094 tree expr = NULL_TREE;
7095 bool compound_literal_p;
7096 const char *saved_message;
7098 /* There's no way to know yet whether or not this is a cast.
7099 For example, `(int (3))' is a unary-expression, while `(int)
7100 3' is a cast. So, we resort to parsing tentatively. */
7101 cp_parser_parse_tentatively (parser);
7102 /* Types may not be defined in a cast. */
7103 saved_message = parser->type_definition_forbidden_message;
7104 parser->type_definition_forbidden_message
7105 = G_("types may not be defined in casts");
7106 /* Consume the `('. */
7107 cp_lexer_consume_token (parser->lexer);
7108 /* A very tricky bit is that `(struct S) { 3 }' is a
7109 compound-literal (which we permit in C++ as an extension).
7110 But, that construct is not a cast-expression -- it is a
7111 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7112 is legal; if the compound-literal were a cast-expression,
7113 you'd need an extra set of parentheses.) But, if we parse
7114 the type-id, and it happens to be a class-specifier, then we
7115 will commit to the parse at that point, because we cannot
7116 undo the action that is done when creating a new class. So,
7117 then we cannot back up and do a postfix-expression.
7119 Therefore, we scan ahead to the closing `)', and check to see
7120 if the token after the `)' is a `{'. If so, we are not
7121 looking at a cast-expression.
7123 Save tokens so that we can put them back. */
7124 cp_lexer_save_tokens (parser->lexer);
7125 /* Skip tokens until the next token is a closing parenthesis.
7126 If we find the closing `)', and the next token is a `{', then
7127 we are looking at a compound-literal. */
7129 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7130 /*consume_paren=*/true)
7131 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7132 /* Roll back the tokens we skipped. */
7133 cp_lexer_rollback_tokens (parser->lexer);
7134 /* If we were looking at a compound-literal, simulate an error
7135 so that the call to cp_parser_parse_definitely below will
7137 if (compound_literal_p)
7138 cp_parser_simulate_error (parser);
7141 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7142 parser->in_type_id_in_expr_p = true;
7143 /* Look for the type-id. */
7144 type = cp_parser_type_id (parser);
7145 /* Look for the closing `)'. */
7146 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7147 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7150 /* Restore the saved message. */
7151 parser->type_definition_forbidden_message = saved_message;
7153 /* At this point this can only be either a cast or a
7154 parenthesized ctor such as `(T ())' that looks like a cast to
7155 function returning T. */
7156 if (!cp_parser_error_occurred (parser)
7157 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7160 cp_parser_parse_definitely (parser);
7161 expr = cp_parser_cast_expression (parser,
7162 /*address_p=*/false,
7163 /*cast_p=*/true, pidk);
7165 /* Warn about old-style casts, if so requested. */
7166 if (warn_old_style_cast
7167 && !in_system_header
7168 && !VOID_TYPE_P (type)
7169 && current_lang_name != lang_name_c)
7170 warning (OPT_Wold_style_cast, "use of old-style cast");
7172 /* Only type conversions to integral or enumeration types
7173 can be used in constant-expressions. */
7174 if (!cast_valid_in_integral_constant_expression_p (type)
7175 && cp_parser_non_integral_constant_expression (parser,
7177 return error_mark_node;
7179 /* Perform the cast. */
7180 expr = build_c_cast (input_location, type, expr);
7184 cp_parser_abort_tentative_parse (parser);
7187 /* If we get here, then it's not a cast, so it must be a
7188 unary-expression. */
7189 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7192 /* Parse a binary expression of the general form:
7196 pm-expression .* cast-expression
7197 pm-expression ->* cast-expression
7199 multiplicative-expression:
7201 multiplicative-expression * pm-expression
7202 multiplicative-expression / pm-expression
7203 multiplicative-expression % pm-expression
7205 additive-expression:
7206 multiplicative-expression
7207 additive-expression + multiplicative-expression
7208 additive-expression - multiplicative-expression
7212 shift-expression << additive-expression
7213 shift-expression >> additive-expression
7215 relational-expression:
7217 relational-expression < shift-expression
7218 relational-expression > shift-expression
7219 relational-expression <= shift-expression
7220 relational-expression >= shift-expression
7224 relational-expression:
7225 relational-expression <? shift-expression
7226 relational-expression >? shift-expression
7228 equality-expression:
7229 relational-expression
7230 equality-expression == relational-expression
7231 equality-expression != relational-expression
7235 and-expression & equality-expression
7237 exclusive-or-expression:
7239 exclusive-or-expression ^ and-expression
7241 inclusive-or-expression:
7242 exclusive-or-expression
7243 inclusive-or-expression | exclusive-or-expression
7245 logical-and-expression:
7246 inclusive-or-expression
7247 logical-and-expression && inclusive-or-expression
7249 logical-or-expression:
7250 logical-and-expression
7251 logical-or-expression || logical-and-expression
7253 All these are implemented with a single function like:
7256 simple-cast-expression
7257 binary-expression <token> binary-expression
7259 CAST_P is true if this expression is the target of a cast.
7261 The binops_by_token map is used to get the tree codes for each <token> type.
7262 binary-expressions are associated according to a precedence table. */
7264 #define TOKEN_PRECEDENCE(token) \
7265 (((token->type == CPP_GREATER \
7266 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7267 && !parser->greater_than_is_operator_p) \
7268 ? PREC_NOT_OPERATOR \
7269 : binops_by_token[token->type].prec)
7272 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7273 bool no_toplevel_fold_p,
7274 enum cp_parser_prec prec,
7277 cp_parser_expression_stack stack;
7278 cp_parser_expression_stack_entry *sp = &stack[0];
7279 cp_parser_expression_stack_entry current;
7282 enum tree_code rhs_type;
7283 enum cp_parser_prec new_prec, lookahead_prec;
7286 /* Parse the first expression. */
7287 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7289 current.lhs_type = ERROR_MARK;
7290 current.prec = prec;
7292 if (cp_parser_error_occurred (parser))
7293 return error_mark_node;
7297 /* Get an operator token. */
7298 token = cp_lexer_peek_token (parser->lexer);
7300 if (warn_cxx0x_compat
7301 && token->type == CPP_RSHIFT
7302 && !parser->greater_than_is_operator_p)
7304 if (warning_at (token->location, OPT_Wc__0x_compat,
7305 "%<>>%> operator is treated"
7306 " as two right angle brackets in C++11"))
7307 inform (token->location,
7308 "suggest parentheses around %<>>%> expression");
7311 new_prec = TOKEN_PRECEDENCE (token);
7313 /* Popping an entry off the stack means we completed a subexpression:
7314 - either we found a token which is not an operator (`>' where it is not
7315 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7316 will happen repeatedly;
7317 - or, we found an operator which has lower priority. This is the case
7318 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7320 if (new_prec <= current.prec)
7329 current.tree_type = binops_by_token[token->type].tree_type;
7330 current.loc = token->location;
7332 /* We used the operator token. */
7333 cp_lexer_consume_token (parser->lexer);
7335 /* For "false && x" or "true || x", x will never be executed;
7336 disable warnings while evaluating it. */
7337 if (current.tree_type == TRUTH_ANDIF_EXPR)
7338 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7339 else if (current.tree_type == TRUTH_ORIF_EXPR)
7340 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7342 /* Extract another operand. It may be the RHS of this expression
7343 or the LHS of a new, higher priority expression. */
7344 rhs = cp_parser_simple_cast_expression (parser);
7345 rhs_type = ERROR_MARK;
7347 /* Get another operator token. Look up its precedence to avoid
7348 building a useless (immediately popped) stack entry for common
7349 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7350 token = cp_lexer_peek_token (parser->lexer);
7351 lookahead_prec = TOKEN_PRECEDENCE (token);
7352 if (lookahead_prec > new_prec)
7354 /* ... and prepare to parse the RHS of the new, higher priority
7355 expression. Since precedence levels on the stack are
7356 monotonically increasing, we do not have to care about
7361 current.lhs_type = rhs_type;
7362 current.prec = new_prec;
7363 new_prec = lookahead_prec;
7367 lookahead_prec = new_prec;
7368 /* If the stack is not empty, we have parsed into LHS the right side
7369 (`4' in the example above) of an expression we had suspended.
7370 We can use the information on the stack to recover the LHS (`3')
7371 from the stack together with the tree code (`MULT_EXPR'), and
7372 the precedence of the higher level subexpression
7373 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7374 which will be used to actually build the additive expression. */
7376 rhs_type = current.lhs_type;
7381 /* Undo the disabling of warnings done above. */
7382 if (current.tree_type == TRUTH_ANDIF_EXPR)
7383 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7384 else if (current.tree_type == TRUTH_ORIF_EXPR)
7385 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7388 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7389 ERROR_MARK for everything that is not a binary expression.
7390 This makes warn_about_parentheses miss some warnings that
7391 involve unary operators. For unary expressions we should
7392 pass the correct tree_code unless the unary expression was
7393 surrounded by parentheses.
7395 if (no_toplevel_fold_p
7396 && lookahead_prec <= current.prec
7398 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7399 current.lhs = build2 (current.tree_type, boolean_type_node,
7402 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7403 current.lhs, current.lhs_type,
7404 rhs, rhs_type, &overload,
7405 tf_warning_or_error);
7406 current.lhs_type = current.tree_type;
7408 /* If the binary operator required the use of an overloaded operator,
7409 then this expression cannot be an integral constant-expression.
7410 An overloaded operator can be used even if both operands are
7411 otherwise permissible in an integral constant-expression if at
7412 least one of the operands is of enumeration type. */
7415 && cp_parser_non_integral_constant_expression (parser,
7417 return error_mark_node;
7424 /* Parse the `? expression : assignment-expression' part of a
7425 conditional-expression. The LOGICAL_OR_EXPR is the
7426 logical-or-expression that started the conditional-expression.
7427 Returns a representation of the entire conditional-expression.
7429 This routine is used by cp_parser_assignment_expression.
7431 ? expression : assignment-expression
7435 ? : assignment-expression */
7438 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7441 tree assignment_expr;
7442 struct cp_token *token;
7443 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7445 /* Consume the `?' token. */
7446 cp_lexer_consume_token (parser->lexer);
7447 token = cp_lexer_peek_token (parser->lexer);
7448 if (cp_parser_allow_gnu_extensions_p (parser)
7449 && token->type == CPP_COLON)
7451 pedwarn (token->location, OPT_Wpedantic,
7452 "ISO C++ does not allow ?: with omitted middle operand");
7453 /* Implicit true clause. */
7455 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7456 warn_for_omitted_condop (token->location, logical_or_expr);
7460 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7461 parser->colon_corrects_to_scope_p = false;
7462 /* Parse the expression. */
7463 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7464 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7465 c_inhibit_evaluation_warnings +=
7466 ((logical_or_expr == truthvalue_true_node)
7467 - (logical_or_expr == truthvalue_false_node));
7468 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7471 /* The next token should be a `:'. */
7472 cp_parser_require (parser, CPP_COLON, RT_COLON);
7473 /* Parse the assignment-expression. */
7474 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7475 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7477 /* Build the conditional-expression. */
7478 return build_x_conditional_expr (loc, logical_or_expr,
7481 tf_warning_or_error);
7484 /* Parse an assignment-expression.
7486 assignment-expression:
7487 conditional-expression
7488 logical-or-expression assignment-operator assignment_expression
7491 CAST_P is true if this expression is the target of a cast.
7493 Returns a representation for the expression. */
7496 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7501 /* If the next token is the `throw' keyword, then we're looking at
7502 a throw-expression. */
7503 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7504 expr = cp_parser_throw_expression (parser);
7505 /* Otherwise, it must be that we are looking at a
7506 logical-or-expression. */
7509 /* Parse the binary expressions (logical-or-expression). */
7510 expr = cp_parser_binary_expression (parser, cast_p, false,
7511 PREC_NOT_OPERATOR, pidk);
7512 /* If the next token is a `?' then we're actually looking at a
7513 conditional-expression. */
7514 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7515 return cp_parser_question_colon_clause (parser, expr);
7518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7520 /* If it's an assignment-operator, we're using the second
7522 enum tree_code assignment_operator
7523 = cp_parser_assignment_operator_opt (parser);
7524 if (assignment_operator != ERROR_MARK)
7526 bool non_constant_p;
7527 location_t saved_input_location;
7529 /* Parse the right-hand side of the assignment. */
7530 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7532 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7533 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7535 /* An assignment may not appear in a
7536 constant-expression. */
7537 if (cp_parser_non_integral_constant_expression (parser,
7539 return error_mark_node;
7540 /* Build the assignment expression. Its default
7541 location is the location of the '=' token. */
7542 saved_input_location = input_location;
7543 input_location = loc;
7544 expr = build_x_modify_expr (loc, expr,
7545 assignment_operator,
7547 tf_warning_or_error);
7548 input_location = saved_input_location;
7556 /* Parse an (optional) assignment-operator.
7558 assignment-operator: one of
7559 = *= /= %= += -= >>= <<= &= ^= |=
7563 assignment-operator: one of
7566 If the next token is an assignment operator, the corresponding tree
7567 code is returned, and the token is consumed. For example, for
7568 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7569 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7570 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7571 operator, ERROR_MARK is returned. */
7573 static enum tree_code
7574 cp_parser_assignment_operator_opt (cp_parser* parser)
7579 /* Peek at the next token. */
7580 token = cp_lexer_peek_token (parser->lexer);
7582 switch (token->type)
7593 op = TRUNC_DIV_EXPR;
7597 op = TRUNC_MOD_EXPR;
7629 /* Nothing else is an assignment operator. */
7633 /* If it was an assignment operator, consume it. */
7634 if (op != ERROR_MARK)
7635 cp_lexer_consume_token (parser->lexer);
7640 /* Parse an expression.
7643 assignment-expression
7644 expression , assignment-expression
7646 CAST_P is true if this expression is the target of a cast.
7648 Returns a representation of the expression. */
7651 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7653 tree expression = NULL_TREE;
7654 location_t loc = UNKNOWN_LOCATION;
7658 tree assignment_expression;
7660 /* Parse the next assignment-expression. */
7661 assignment_expression
7662 = cp_parser_assignment_expression (parser, cast_p, pidk);
7663 /* If this is the first assignment-expression, we can just
7666 expression = assignment_expression;
7668 expression = build_x_compound_expr (loc, expression,
7669 assignment_expression,
7670 tf_warning_or_error);
7671 /* If the next token is not a comma, then we are done with the
7673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7675 /* Consume the `,'. */
7676 loc = cp_lexer_peek_token (parser->lexer)->location;
7677 cp_lexer_consume_token (parser->lexer);
7678 /* A comma operator cannot appear in a constant-expression. */
7679 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7680 expression = error_mark_node;
7686 /* Parse a constant-expression.
7688 constant-expression:
7689 conditional-expression
7691 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7692 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7693 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7694 is false, NON_CONSTANT_P should be NULL. */
7697 cp_parser_constant_expression (cp_parser* parser,
7698 bool allow_non_constant_p,
7699 bool *non_constant_p)
7701 bool saved_integral_constant_expression_p;
7702 bool saved_allow_non_integral_constant_expression_p;
7703 bool saved_non_integral_constant_expression_p;
7706 /* It might seem that we could simply parse the
7707 conditional-expression, and then check to see if it were
7708 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7709 one that the compiler can figure out is constant, possibly after
7710 doing some simplifications or optimizations. The standard has a
7711 precise definition of constant-expression, and we must honor
7712 that, even though it is somewhat more restrictive.
7718 is not a legal declaration, because `(2, 3)' is not a
7719 constant-expression. The `,' operator is forbidden in a
7720 constant-expression. However, GCC's constant-folding machinery
7721 will fold this operation to an INTEGER_CST for `3'. */
7723 /* Save the old settings. */
7724 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7725 saved_allow_non_integral_constant_expression_p
7726 = parser->allow_non_integral_constant_expression_p;
7727 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7728 /* We are now parsing a constant-expression. */
7729 parser->integral_constant_expression_p = true;
7730 parser->allow_non_integral_constant_expression_p
7731 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7732 parser->non_integral_constant_expression_p = false;
7733 /* Although the grammar says "conditional-expression", we parse an
7734 "assignment-expression", which also permits "throw-expression"
7735 and the use of assignment operators. In the case that
7736 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7737 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7738 actually essential that we look for an assignment-expression.
7739 For example, cp_parser_initializer_clauses uses this function to
7740 determine whether a particular assignment-expression is in fact
7742 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7743 /* Restore the old settings. */
7744 parser->integral_constant_expression_p
7745 = saved_integral_constant_expression_p;
7746 parser->allow_non_integral_constant_expression_p
7747 = saved_allow_non_integral_constant_expression_p;
7748 if (cxx_dialect >= cxx0x)
7750 /* Require an rvalue constant expression here; that's what our
7751 callers expect. Reference constant expressions are handled
7752 separately in e.g. cp_parser_template_argument. */
7753 bool is_const = potential_rvalue_constant_expression (expression);
7754 parser->non_integral_constant_expression_p = !is_const;
7755 if (!is_const && !allow_non_constant_p)
7756 require_potential_rvalue_constant_expression (expression);
7758 if (allow_non_constant_p)
7759 *non_constant_p = parser->non_integral_constant_expression_p;
7760 parser->non_integral_constant_expression_p
7761 = saved_non_integral_constant_expression_p;
7766 /* Parse __builtin_offsetof.
7768 offsetof-expression:
7769 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7771 offsetof-member-designator:
7773 | offsetof-member-designator "." id-expression
7774 | offsetof-member-designator "[" expression "]"
7775 | offsetof-member-designator "->" id-expression */
7778 cp_parser_builtin_offsetof (cp_parser *parser)
7780 int save_ice_p, save_non_ice_p;
7785 /* We're about to accept non-integral-constant things, but will
7786 definitely yield an integral constant expression. Save and
7787 restore these values around our local parsing. */
7788 save_ice_p = parser->integral_constant_expression_p;
7789 save_non_ice_p = parser->non_integral_constant_expression_p;
7791 /* Consume the "__builtin_offsetof" token. */
7792 cp_lexer_consume_token (parser->lexer);
7793 /* Consume the opening `('. */
7794 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7795 /* Parse the type-id. */
7796 type = cp_parser_type_id (parser);
7797 /* Look for the `,'. */
7798 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7799 token = cp_lexer_peek_token (parser->lexer);
7801 /* Build the (type *)null that begins the traditional offsetof macro. */
7802 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7803 tf_warning_or_error);
7805 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7806 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7807 true, &dummy, token->location);
7810 token = cp_lexer_peek_token (parser->lexer);
7811 switch (token->type)
7813 case CPP_OPEN_SQUARE:
7814 /* offsetof-member-designator "[" expression "]" */
7815 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7819 /* offsetof-member-designator "->" identifier */
7820 expr = grok_array_decl (token->location, expr, integer_zero_node);
7824 /* offsetof-member-designator "." identifier */
7825 cp_lexer_consume_token (parser->lexer);
7826 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7831 case CPP_CLOSE_PAREN:
7832 /* Consume the ")" token. */
7833 cp_lexer_consume_token (parser->lexer);
7837 /* Error. We know the following require will fail, but
7838 that gives the proper error message. */
7839 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7840 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7841 expr = error_mark_node;
7847 /* If we're processing a template, we can't finish the semantics yet.
7848 Otherwise we can fold the entire expression now. */
7849 if (processing_template_decl)
7850 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7852 expr = finish_offsetof (expr);
7855 parser->integral_constant_expression_p = save_ice_p;
7856 parser->non_integral_constant_expression_p = save_non_ice_p;
7861 /* Parse a trait expression.
7863 Returns a representation of the expression, the underlying type
7864 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7867 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7870 tree type1, type2 = NULL_TREE;
7871 bool binary = false;
7872 cp_decl_specifier_seq decl_specs;
7876 case RID_HAS_NOTHROW_ASSIGN:
7877 kind = CPTK_HAS_NOTHROW_ASSIGN;
7879 case RID_HAS_NOTHROW_CONSTRUCTOR:
7880 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7882 case RID_HAS_NOTHROW_COPY:
7883 kind = CPTK_HAS_NOTHROW_COPY;
7885 case RID_HAS_TRIVIAL_ASSIGN:
7886 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7888 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7889 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7891 case RID_HAS_TRIVIAL_COPY:
7892 kind = CPTK_HAS_TRIVIAL_COPY;
7894 case RID_HAS_TRIVIAL_DESTRUCTOR:
7895 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7897 case RID_HAS_VIRTUAL_DESTRUCTOR:
7898 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7900 case RID_IS_ABSTRACT:
7901 kind = CPTK_IS_ABSTRACT;
7903 case RID_IS_BASE_OF:
7904 kind = CPTK_IS_BASE_OF;
7908 kind = CPTK_IS_CLASS;
7910 case RID_IS_CONVERTIBLE_TO:
7911 kind = CPTK_IS_CONVERTIBLE_TO;
7915 kind = CPTK_IS_EMPTY;
7918 kind = CPTK_IS_ENUM;
7921 kind = CPTK_IS_FINAL;
7923 case RID_IS_LITERAL_TYPE:
7924 kind = CPTK_IS_LITERAL_TYPE;
7929 case RID_IS_POLYMORPHIC:
7930 kind = CPTK_IS_POLYMORPHIC;
7932 case RID_IS_STD_LAYOUT:
7933 kind = CPTK_IS_STD_LAYOUT;
7935 case RID_IS_TRIVIAL:
7936 kind = CPTK_IS_TRIVIAL;
7939 kind = CPTK_IS_UNION;
7941 case RID_UNDERLYING_TYPE:
7942 kind = CPTK_UNDERLYING_TYPE;
7947 case RID_DIRECT_BASES:
7948 kind = CPTK_DIRECT_BASES;
7954 /* Consume the token. */
7955 cp_lexer_consume_token (parser->lexer);
7957 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7959 type1 = cp_parser_type_id (parser);
7961 if (type1 == error_mark_node)
7962 return error_mark_node;
7964 /* Build a trivial decl-specifier-seq. */
7965 clear_decl_specs (&decl_specs);
7966 decl_specs.type = type1;
7968 /* Call grokdeclarator to figure out what type this is. */
7969 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7970 /*initialized=*/0, /*attrlist=*/NULL);
7974 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7976 type2 = cp_parser_type_id (parser);
7978 if (type2 == error_mark_node)
7979 return error_mark_node;
7981 /* Build a trivial decl-specifier-seq. */
7982 clear_decl_specs (&decl_specs);
7983 decl_specs.type = type2;
7985 /* Call grokdeclarator to figure out what type this is. */
7986 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7987 /*initialized=*/0, /*attrlist=*/NULL);
7990 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7992 /* Complete the trait expression, which may mean either processing
7993 the trait expr now or saving it for template instantiation. */
7996 case CPTK_UNDERLYING_TYPE:
7997 return finish_underlying_type (type1);
7999 return finish_bases (type1, false);
8000 case CPTK_DIRECT_BASES:
8001 return finish_bases (type1, true);
8003 return finish_trait_expr (kind, type1, type2);
8007 /* Lambdas that appear in variable initializer or default argument scope
8008 get that in their mangling, so we need to record it. We might as well
8009 use the count for function and namespace scopes as well. */
8010 static GTY(()) tree lambda_scope;
8011 static GTY(()) int lambda_count;
8012 typedef struct GTY(()) tree_int
8017 DEF_VEC_O(tree_int);
8018 DEF_VEC_ALLOC_O(tree_int,gc);
8019 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8022 start_lambda_scope (tree decl)
8026 /* Once we're inside a function, we ignore other scopes and just push
8027 the function again so that popping works properly. */
8028 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8029 decl = current_function_decl;
8030 ti.t = lambda_scope;
8031 ti.i = lambda_count;
8032 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
8033 if (lambda_scope != decl)
8035 /* Don't reset the count if we're still in the same function. */
8036 lambda_scope = decl;
8042 record_lambda_scope (tree lambda)
8044 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8045 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8049 finish_lambda_scope (void)
8051 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8052 if (lambda_scope != p->t)
8054 lambda_scope = p->t;
8055 lambda_count = p->i;
8057 VEC_pop (tree_int, lambda_scope_stack);
8060 /* Parse a lambda expression.
8063 lambda-introducer lambda-declarator [opt] compound-statement
8065 Returns a representation of the expression. */
8068 cp_parser_lambda_expression (cp_parser* parser)
8070 tree lambda_expr = build_lambda_expr ();
8074 LAMBDA_EXPR_LOCATION (lambda_expr)
8075 = cp_lexer_peek_token (parser->lexer)->location;
8077 if (cp_unevaluated_operand)
8078 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8079 "lambda-expression in unevaluated context");
8081 /* We may be in the middle of deferred access check. Disable
8083 push_deferring_access_checks (dk_no_deferred);
8085 cp_parser_lambda_introducer (parser, lambda_expr);
8087 type = begin_lambda_type (lambda_expr);
8088 if (type == error_mark_node)
8089 return error_mark_node;
8091 record_lambda_scope (lambda_expr);
8093 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8094 determine_visibility (TYPE_NAME (type));
8096 /* Now that we've started the type, add the capture fields for any
8097 explicit captures. */
8098 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8101 /* Inside the class, surrounding template-parameter-lists do not apply. */
8102 unsigned int saved_num_template_parameter_lists
8103 = parser->num_template_parameter_lists;
8104 unsigned char in_statement = parser->in_statement;
8105 bool in_switch_statement_p = parser->in_switch_statement_p;
8107 parser->num_template_parameter_lists = 0;
8108 parser->in_statement = 0;
8109 parser->in_switch_statement_p = false;
8111 /* By virtue of defining a local class, a lambda expression has access to
8112 the private variables of enclosing classes. */
8114 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8117 cp_parser_lambda_body (parser, lambda_expr);
8118 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8119 cp_parser_skip_to_end_of_block_or_statement (parser);
8121 /* The capture list was built up in reverse order; fix that now. */
8123 tree newlist = NULL_TREE;
8126 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8129 next = TREE_CHAIN (elt);
8130 TREE_CHAIN (elt) = newlist;
8133 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8137 maybe_add_lambda_conv_op (type);
8139 type = finish_struct (type, /*attributes=*/NULL_TREE);
8141 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8142 parser->in_statement = in_statement;
8143 parser->in_switch_statement_p = in_switch_statement_p;
8146 pop_deferring_access_checks ();
8148 /* This field is only used during parsing of the lambda. */
8149 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8151 /* This lambda shouldn't have any proxies left at this point. */
8152 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8153 /* And now that we're done, push proxies for an enclosing lambda. */
8154 insert_pending_capture_proxies ();
8157 return build_lambda_object (lambda_expr);
8159 return error_mark_node;
8162 /* Parse the beginning of a lambda expression.
8165 [ lambda-capture [opt] ]
8167 LAMBDA_EXPR is the current representation of the lambda expression. */
8170 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8172 /* Need commas after the first capture. */
8175 /* Eat the leading `['. */
8176 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8178 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8179 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8180 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8181 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8182 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8183 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8185 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8187 cp_lexer_consume_token (parser->lexer);
8191 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8193 cp_token* capture_token;
8195 tree capture_init_expr;
8196 cp_id_kind idk = CP_ID_KIND_NONE;
8197 bool explicit_init_p = false;
8199 enum capture_kind_type
8204 enum capture_kind_type capture_kind = BY_COPY;
8206 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8208 error ("expected end of capture-list");
8215 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8217 /* Possibly capture `this'. */
8218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8220 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8221 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8222 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8223 "with by-copy capture default");
8224 cp_lexer_consume_token (parser->lexer);
8225 add_capture (lambda_expr,
8226 /*id=*/this_identifier,
8227 /*initializer=*/finish_this_expr(),
8228 /*by_reference_p=*/false,
8233 /* Remember whether we want to capture as a reference or not. */
8234 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8236 capture_kind = BY_REFERENCE;
8237 cp_lexer_consume_token (parser->lexer);
8240 /* Get the identifier. */
8241 capture_token = cp_lexer_peek_token (parser->lexer);
8242 capture_id = cp_parser_identifier (parser);
8244 if (capture_id == error_mark_node)
8245 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8246 delimiters, but I modified this to stop on unnested ']' as well. It
8247 was already changed to stop on unnested '}', so the
8248 "closing_parenthesis" name is no more misleading with my change. */
8250 cp_parser_skip_to_closing_parenthesis (parser,
8251 /*recovering=*/true,
8253 /*consume_paren=*/true);
8257 /* Find the initializer for this capture. */
8258 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8260 /* An explicit expression exists. */
8261 cp_lexer_consume_token (parser->lexer);
8262 pedwarn (input_location, OPT_Wpedantic,
8263 "ISO C++ does not allow initializers "
8264 "in lambda expression capture lists");
8265 capture_init_expr = cp_parser_assignment_expression (parser,
8268 explicit_init_p = true;
8272 const char* error_msg;
8274 /* Turn the identifier into an id-expression. */
8276 = cp_parser_lookup_name
8280 /*is_template=*/false,
8281 /*is_namespace=*/false,
8282 /*check_dependency=*/true,
8283 /*ambiguous_decls=*/NULL,
8284 capture_token->location);
8286 if (capture_init_expr == error_mark_node)
8288 unqualified_name_lookup_error (capture_id);
8291 else if (DECL_P (capture_init_expr)
8292 && (TREE_CODE (capture_init_expr) != VAR_DECL
8293 && TREE_CODE (capture_init_expr) != PARM_DECL))
8295 error_at (capture_token->location,
8296 "capture of non-variable %qD ",
8298 inform (0, "%q+#D declared here", capture_init_expr);
8301 if (TREE_CODE (capture_init_expr) == VAR_DECL
8302 && decl_storage_duration (capture_init_expr) != dk_auto)
8304 pedwarn (capture_token->location, 0, "capture of variable "
8305 "%qD with non-automatic storage duration",
8307 inform (0, "%q+#D declared here", capture_init_expr);
8312 = finish_id_expression
8317 /*integral_constant_expression_p=*/false,
8318 /*allow_non_integral_constant_expression_p=*/false,
8319 /*non_integral_constant_expression_p=*/NULL,
8320 /*template_p=*/false,
8322 /*address_p=*/false,
8323 /*template_arg_p=*/false,
8325 capture_token->location);
8328 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8329 && !explicit_init_p)
8331 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8332 && capture_kind == BY_COPY)
8333 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8334 "of %qD redundant with by-copy capture default",
8336 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8337 && capture_kind == BY_REFERENCE)
8338 pedwarn (capture_token->location, 0, "explicit by-reference "
8339 "capture of %qD redundant with by-reference capture "
8340 "default", capture_id);
8343 add_capture (lambda_expr,
8346 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8350 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8353 /* Parse the (optional) middle of a lambda expression.
8356 ( parameter-declaration-clause [opt] )
8357 attribute-specifier [opt]
8359 exception-specification [opt]
8360 lambda-return-type-clause [opt]
8362 LAMBDA_EXPR is the current representation of the lambda expression. */
8365 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8367 /* 5.1.1.4 of the standard says:
8368 If a lambda-expression does not include a lambda-declarator, it is as if
8369 the lambda-declarator were ().
8370 This means an empty parameter list, no attributes, and no exception
8372 tree param_list = void_list_node;
8373 tree attributes = NULL_TREE;
8374 tree exception_spec = NULL_TREE;
8377 /* The lambda-declarator is optional, but must begin with an opening
8378 parenthesis if present. */
8379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8381 cp_lexer_consume_token (parser->lexer);
8383 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8385 /* Parse parameters. */
8386 param_list = cp_parser_parameter_declaration_clause (parser);
8388 /* Default arguments shall not be specified in the
8389 parameter-declaration-clause of a lambda-declarator. */
8390 for (t = param_list; t; t = TREE_CHAIN (t))
8391 if (TREE_PURPOSE (t))
8392 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8393 "default argument specified for lambda parameter");
8395 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8397 attributes = cp_parser_attributes_opt (parser);
8399 /* Parse optional `mutable' keyword. */
8400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8402 cp_lexer_consume_token (parser->lexer);
8403 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8406 /* Parse optional exception specification. */
8407 exception_spec = cp_parser_exception_specification_opt (parser);
8409 /* Parse optional trailing return type. */
8410 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8412 cp_lexer_consume_token (parser->lexer);
8413 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8416 /* The function parameters must be in scope all the way until after the
8417 trailing-return-type in case of decltype. */
8418 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8419 pop_binding (DECL_NAME (t), t);
8424 /* Create the function call operator.
8426 Messing with declarators like this is no uglier than building up the
8427 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8430 cp_decl_specifier_seq return_type_specs;
8431 cp_declarator* declarator;
8436 clear_decl_specs (&return_type_specs);
8437 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8438 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8440 /* Maybe we will deduce the return type later. */
8441 return_type_specs.type = make_auto ();
8443 p = obstack_alloc (&declarator_obstack, 0);
8445 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8448 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8449 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8450 declarator = make_call_declarator (declarator, param_list, quals,
8451 VIRT_SPEC_UNSPECIFIED,
8453 /*late_return_type=*/NULL_TREE);
8454 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8456 fco = grokmethod (&return_type_specs,
8459 if (fco != error_mark_node)
8461 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8462 DECL_ARTIFICIAL (fco) = 1;
8463 /* Give the object parameter a different name. */
8464 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8467 finish_member_declaration (fco);
8469 obstack_free (&declarator_obstack, p);
8471 return (fco != error_mark_node);
8475 /* Parse the body of a lambda expression, which is simply
8479 but which requires special handling.
8480 LAMBDA_EXPR is the current representation of the lambda expression. */
8483 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8485 bool nested = (current_function_decl != NULL_TREE);
8486 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8488 push_function_context ();
8490 /* Still increment function_depth so that we don't GC in the
8491 middle of an expression. */
8493 /* Clear this in case we're in the middle of a default argument. */
8494 parser->local_variables_forbidden_p = false;
8496 /* Finish the function call operator
8498 + late_parsing_for_member
8499 + function_definition_after_declarator
8500 + ctor_initializer_opt_and_function_body */
8502 tree fco = lambda_function (lambda_expr);
8508 /* Let the front end know that we are going to be defining this
8510 start_preparsed_function (fco,
8512 SF_PRE_PARSED | SF_INCLASS_INLINE);
8514 start_lambda_scope (fco);
8515 body = begin_function_body ();
8517 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8520 /* Push the proxies for any explicit captures. */
8521 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8522 cap = TREE_CHAIN (cap))
8523 build_capture_proxy (TREE_PURPOSE (cap));
8525 compound_stmt = begin_compound_stmt (0);
8527 /* 5.1.1.4 of the standard says:
8528 If a lambda-expression does not include a trailing-return-type, it
8529 is as if the trailing-return-type denotes the following type:
8530 * if the compound-statement is of the form
8531 { return attribute-specifier [opt] expression ; }
8532 the type of the returned expression after lvalue-to-rvalue
8533 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8534 (_conv.array_ 4.2), and function-to-pointer conversion
8536 * otherwise, void. */
8538 /* In a lambda that has neither a lambda-return-type-clause
8539 nor a deducible form, errors should be reported for return statements
8540 in the body. Since we used void as the placeholder return type, parsing
8541 the body as usual will give such desired behavior. */
8542 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8543 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8544 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8546 tree expr = NULL_TREE;
8547 cp_id_kind idk = CP_ID_KIND_NONE;
8549 /* Parse tentatively in case there's more after the initial return
8551 cp_parser_parse_tentatively (parser);
8553 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8555 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8557 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8558 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8560 if (cp_parser_parse_definitely (parser))
8562 if (!processing_template_decl)
8563 apply_deduced_return_type (fco, lambda_return_type (expr));
8565 /* Will get error here if type not deduced yet. */
8566 finish_return_stmt (expr);
8574 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8575 cp_parser_label_declaration (parser);
8576 cp_parser_statement_seq_opt (parser, NULL_TREE);
8577 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8580 finish_compound_stmt (compound_stmt);
8583 finish_function_body (body);
8584 finish_lambda_scope ();
8586 /* Finish the function and generate code for it if necessary. */
8587 expand_or_defer_fn (finish_function (/*inline*/2));
8590 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8592 pop_function_context();
8597 /* Statements [gram.stmt.stmt] */
8599 /* Parse a statement.
8603 expression-statement
8608 declaration-statement
8616 IN_COMPOUND is true when the statement is nested inside a
8617 cp_parser_compound_statement; this matters for certain pragmas.
8619 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8620 is a (possibly labeled) if statement which is not enclosed in braces
8621 and has an else clause. This is used to implement -Wparentheses. */
8624 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8625 bool in_compound, bool *if_p)
8629 location_t statement_location;
8634 /* There is no statement yet. */
8635 statement = NULL_TREE;
8636 /* Peek at the next token. */
8637 token = cp_lexer_peek_token (parser->lexer);
8638 /* Remember the location of the first token in the statement. */
8639 statement_location = token->location;
8640 /* If this is a keyword, then that will often determine what kind of
8641 statement we have. */
8642 if (token->type == CPP_KEYWORD)
8644 enum rid keyword = token->keyword;
8650 /* Looks like a labeled-statement with a case label.
8651 Parse the label, and then use tail recursion to parse
8653 cp_parser_label_for_labeled_statement (parser);
8658 statement = cp_parser_selection_statement (parser, if_p);
8664 statement = cp_parser_iteration_statement (parser);
8671 statement = cp_parser_jump_statement (parser);
8674 /* Objective-C++ exception-handling constructs. */
8677 case RID_AT_FINALLY:
8678 case RID_AT_SYNCHRONIZED:
8680 statement = cp_parser_objc_statement (parser);
8684 statement = cp_parser_try_block (parser);
8688 /* This must be a namespace alias definition. */
8689 cp_parser_declaration_statement (parser);
8692 case RID_TRANSACTION_ATOMIC:
8693 case RID_TRANSACTION_RELAXED:
8694 statement = cp_parser_transaction (parser, keyword);
8696 case RID_TRANSACTION_CANCEL:
8697 statement = cp_parser_transaction_cancel (parser);
8701 /* It might be a keyword like `int' that can start a
8702 declaration-statement. */
8706 else if (token->type == CPP_NAME)
8708 /* If the next token is a `:', then we are looking at a
8709 labeled-statement. */
8710 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8711 if (token->type == CPP_COLON)
8713 /* Looks like a labeled-statement with an ordinary label.
8714 Parse the label, and then use tail recursion to parse
8716 cp_parser_label_for_labeled_statement (parser);
8720 /* Anything that starts with a `{' must be a compound-statement. */
8721 else if (token->type == CPP_OPEN_BRACE)
8722 statement = cp_parser_compound_statement (parser, NULL, false, false);
8723 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8724 a statement all its own. */
8725 else if (token->type == CPP_PRAGMA)
8727 /* Only certain OpenMP pragmas are attached to statements, and thus
8728 are considered statements themselves. All others are not. In
8729 the context of a compound, accept the pragma as a "statement" and
8730 return so that we can check for a close brace. Otherwise we
8731 require a real statement and must go back and read one. */
8733 cp_parser_pragma (parser, pragma_compound);
8734 else if (!cp_parser_pragma (parser, pragma_stmt))
8738 else if (token->type == CPP_EOF)
8740 cp_parser_error (parser, "expected statement");
8744 /* Everything else must be a declaration-statement or an
8745 expression-statement. Try for the declaration-statement
8746 first, unless we are looking at a `;', in which case we know that
8747 we have an expression-statement. */
8750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8752 cp_parser_parse_tentatively (parser);
8753 /* Try to parse the declaration-statement. */
8754 cp_parser_declaration_statement (parser);
8755 /* If that worked, we're done. */
8756 if (cp_parser_parse_definitely (parser))
8759 /* Look for an expression-statement instead. */
8760 statement = cp_parser_expression_statement (parser, in_statement_expr);
8763 /* Set the line number for the statement. */
8764 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8765 SET_EXPR_LOCATION (statement, statement_location);
8768 /* Parse the label for a labeled-statement, i.e.
8771 case constant-expression :
8775 case constant-expression ... constant-expression : statement
8777 When a label is parsed without errors, the label is added to the
8778 parse tree by the finish_* functions, so this function doesn't
8779 have to return the label. */
8782 cp_parser_label_for_labeled_statement (cp_parser* parser)
8785 tree label = NULL_TREE;
8786 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8788 /* The next token should be an identifier. */
8789 token = cp_lexer_peek_token (parser->lexer);
8790 if (token->type != CPP_NAME
8791 && token->type != CPP_KEYWORD)
8793 cp_parser_error (parser, "expected labeled-statement");
8797 parser->colon_corrects_to_scope_p = false;
8798 switch (token->keyword)
8805 /* Consume the `case' token. */
8806 cp_lexer_consume_token (parser->lexer);
8807 /* Parse the constant-expression. */
8808 expr = cp_parser_constant_expression (parser,
8809 /*allow_non_constant_p=*/false,
8812 ellipsis = cp_lexer_peek_token (parser->lexer);
8813 if (ellipsis->type == CPP_ELLIPSIS)
8815 /* Consume the `...' token. */
8816 cp_lexer_consume_token (parser->lexer);
8818 cp_parser_constant_expression (parser,
8819 /*allow_non_constant_p=*/false,
8821 /* We don't need to emit warnings here, as the common code
8822 will do this for us. */
8825 expr_hi = NULL_TREE;
8827 if (parser->in_switch_statement_p)
8828 finish_case_label (token->location, expr, expr_hi);
8830 error_at (token->location,
8831 "case label %qE not within a switch statement",
8837 /* Consume the `default' token. */
8838 cp_lexer_consume_token (parser->lexer);
8840 if (parser->in_switch_statement_p)
8841 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8843 error_at (token->location, "case label not within a switch statement");
8847 /* Anything else must be an ordinary label. */
8848 label = finish_label_stmt (cp_parser_identifier (parser));
8852 /* Require the `:' token. */
8853 cp_parser_require (parser, CPP_COLON, RT_COLON);
8855 /* An ordinary label may optionally be followed by attributes.
8856 However, this is only permitted if the attributes are then
8857 followed by a semicolon. This is because, for backward
8858 compatibility, when parsing
8859 lab: __attribute__ ((unused)) int i;
8860 we want the attribute to attach to "i", not "lab". */
8861 if (label != NULL_TREE
8862 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8866 cp_parser_parse_tentatively (parser);
8867 attrs = cp_parser_attributes_opt (parser);
8868 if (attrs == NULL_TREE
8869 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8870 cp_parser_abort_tentative_parse (parser);
8871 else if (!cp_parser_parse_definitely (parser))
8874 cplus_decl_attributes (&label, attrs, 0);
8877 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8880 /* Parse an expression-statement.
8882 expression-statement:
8885 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8886 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8887 indicates whether this expression-statement is part of an
8888 expression statement. */
8891 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8893 tree statement = NULL_TREE;
8894 cp_token *token = cp_lexer_peek_token (parser->lexer);
8896 /* If the next token is a ';', then there is no expression
8898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8899 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8901 /* Give a helpful message for "A<T>::type t;" and the like. */
8902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8903 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8905 if (TREE_CODE (statement) == SCOPE_REF)
8906 error_at (token->location, "need %<typename%> before %qE because "
8907 "%qT is a dependent scope",
8908 statement, TREE_OPERAND (statement, 0));
8909 else if (is_overloaded_fn (statement)
8910 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8913 tree fn = get_first_fn (statement);
8914 error_at (token->location,
8915 "%<%T::%D%> names the constructor, not the type",
8916 DECL_CONTEXT (fn), DECL_NAME (fn));
8920 /* Consume the final `;'. */
8921 cp_parser_consume_semicolon_at_end_of_statement (parser);
8923 if (in_statement_expr
8924 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8925 /* This is the final expression statement of a statement
8927 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8929 statement = finish_expr_stmt (statement);
8936 /* Parse a compound-statement.
8939 { statement-seq [opt] }
8944 { label-declaration-seq [opt] statement-seq [opt] }
8946 label-declaration-seq:
8948 label-declaration-seq label-declaration
8950 Returns a tree representing the statement. */
8953 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8954 bool in_try, bool function_body)
8958 /* Consume the `{'. */
8959 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8960 return error_mark_node;
8961 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8963 pedwarn (input_location, OPT_Wpedantic,
8964 "compound-statement in constexpr function");
8965 /* Begin the compound-statement. */
8966 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8967 /* If the next keyword is `__label__' we have a label declaration. */
8968 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8969 cp_parser_label_declaration (parser);
8970 /* Parse an (optional) statement-seq. */
8971 cp_parser_statement_seq_opt (parser, in_statement_expr);
8972 /* Finish the compound-statement. */
8973 finish_compound_stmt (compound_stmt);
8974 /* Consume the `}'. */
8975 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8977 return compound_stmt;
8980 /* Parse an (optional) statement-seq.
8984 statement-seq [opt] statement */
8987 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8989 /* Scan statements until there aren't any more. */
8992 cp_token *token = cp_lexer_peek_token (parser->lexer);
8994 /* If we are looking at a `}', then we have run out of
8995 statements; the same is true if we have reached the end
8996 of file, or have stumbled upon a stray '@end'. */
8997 if (token->type == CPP_CLOSE_BRACE
8998 || token->type == CPP_EOF
8999 || token->type == CPP_PRAGMA_EOL
9000 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9003 /* If we are in a compound statement and find 'else' then
9004 something went wrong. */
9005 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9007 if (parser->in_statement & IN_IF_STMT)
9011 token = cp_lexer_consume_token (parser->lexer);
9012 error_at (token->location, "%<else%> without a previous %<if%>");
9016 /* Parse the statement. */
9017 cp_parser_statement (parser, in_statement_expr, true, NULL);
9021 /* Parse a selection-statement.
9023 selection-statement:
9024 if ( condition ) statement
9025 if ( condition ) statement else statement
9026 switch ( condition ) statement
9028 Returns the new IF_STMT or SWITCH_STMT.
9030 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9031 is a (possibly labeled) if statement which is not enclosed in
9032 braces and has an else clause. This is used to implement
9036 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9044 /* Peek at the next token. */
9045 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9047 /* See what kind of keyword it is. */
9048 keyword = token->keyword;
9057 /* Look for the `('. */
9058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9060 cp_parser_skip_to_end_of_statement (parser);
9061 return error_mark_node;
9064 /* Begin the selection-statement. */
9065 if (keyword == RID_IF)
9066 statement = begin_if_stmt ();
9068 statement = begin_switch_stmt ();
9070 /* Parse the condition. */
9071 condition = cp_parser_condition (parser);
9072 /* Look for the `)'. */
9073 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9074 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9075 /*consume_paren=*/true);
9077 if (keyword == RID_IF)
9080 unsigned char in_statement;
9082 /* Add the condition. */
9083 finish_if_stmt_cond (condition, statement);
9085 /* Parse the then-clause. */
9086 in_statement = parser->in_statement;
9087 parser->in_statement |= IN_IF_STMT;
9088 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9090 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9091 add_stmt (build_empty_stmt (loc));
9092 cp_lexer_consume_token (parser->lexer);
9093 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9094 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9095 "empty body in an %<if%> statement");
9099 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9100 parser->in_statement = in_statement;
9102 finish_then_clause (statement);
9104 /* If the next token is `else', parse the else-clause. */
9105 if (cp_lexer_next_token_is_keyword (parser->lexer,
9108 /* Consume the `else' keyword. */
9109 cp_lexer_consume_token (parser->lexer);
9110 begin_else_clause (statement);
9111 /* Parse the else-clause. */
9112 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9115 loc = cp_lexer_peek_token (parser->lexer)->location;
9117 OPT_Wempty_body, "suggest braces around "
9118 "empty body in an %<else%> statement");
9119 add_stmt (build_empty_stmt (loc));
9120 cp_lexer_consume_token (parser->lexer);
9123 cp_parser_implicitly_scoped_statement (parser, NULL);
9125 finish_else_clause (statement);
9127 /* If we are currently parsing a then-clause, then
9128 IF_P will not be NULL. We set it to true to
9129 indicate that this if statement has an else clause.
9130 This may trigger the Wparentheses warning below
9131 when we get back up to the parent if statement. */
9137 /* This if statement does not have an else clause. If
9138 NESTED_IF is true, then the then-clause is an if
9139 statement which does have an else clause. We warn
9140 about the potential ambiguity. */
9142 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9143 "suggest explicit braces to avoid ambiguous"
9147 /* Now we're all done with the if-statement. */
9148 finish_if_stmt (statement);
9152 bool in_switch_statement_p;
9153 unsigned char in_statement;
9155 /* Add the condition. */
9156 finish_switch_cond (condition, statement);
9158 /* Parse the body of the switch-statement. */
9159 in_switch_statement_p = parser->in_switch_statement_p;
9160 in_statement = parser->in_statement;
9161 parser->in_switch_statement_p = true;
9162 parser->in_statement |= IN_SWITCH_STMT;
9163 cp_parser_implicitly_scoped_statement (parser, NULL);
9164 parser->in_switch_statement_p = in_switch_statement_p;
9165 parser->in_statement = in_statement;
9167 /* Now we're all done with the switch-statement. */
9168 finish_switch_stmt (statement);
9176 cp_parser_error (parser, "expected selection-statement");
9177 return error_mark_node;
9181 /* Parse a condition.
9185 type-specifier-seq declarator = initializer-clause
9186 type-specifier-seq declarator braced-init-list
9191 type-specifier-seq declarator asm-specification [opt]
9192 attributes [opt] = assignment-expression
9194 Returns the expression that should be tested. */
9197 cp_parser_condition (cp_parser* parser)
9199 cp_decl_specifier_seq type_specifiers;
9200 const char *saved_message;
9201 int declares_class_or_enum;
9203 /* Try the declaration first. */
9204 cp_parser_parse_tentatively (parser);
9205 /* New types are not allowed in the type-specifier-seq for a
9207 saved_message = parser->type_definition_forbidden_message;
9208 parser->type_definition_forbidden_message
9209 = G_("types may not be defined in conditions");
9210 /* Parse the type-specifier-seq. */
9211 cp_parser_decl_specifier_seq (parser,
9212 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9214 &declares_class_or_enum);
9215 /* Restore the saved message. */
9216 parser->type_definition_forbidden_message = saved_message;
9217 /* If all is well, we might be looking at a declaration. */
9218 if (!cp_parser_error_occurred (parser))
9221 tree asm_specification;
9223 cp_declarator *declarator;
9224 tree initializer = NULL_TREE;
9226 /* Parse the declarator. */
9227 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9228 /*ctor_dtor_or_conv_p=*/NULL,
9229 /*parenthesized_p=*/NULL,
9230 /*member_p=*/false);
9231 /* Parse the attributes. */
9232 attributes = cp_parser_attributes_opt (parser);
9233 /* Parse the asm-specification. */
9234 asm_specification = cp_parser_asm_specification_opt (parser);
9235 /* If the next token is not an `=' or '{', then we might still be
9236 looking at an expression. For example:
9240 looks like a decl-specifier-seq and a declarator -- but then
9241 there is no `=', so this is an expression. */
9242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9243 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9244 cp_parser_simulate_error (parser);
9246 /* If we did see an `=' or '{', then we are looking at a declaration
9248 if (cp_parser_parse_definitely (parser))
9251 bool non_constant_p;
9252 bool flags = LOOKUP_ONLYCONVERTING;
9254 /* Create the declaration. */
9255 decl = start_decl (declarator, &type_specifiers,
9256 /*initialized_p=*/true,
9257 attributes, /*prefix_attributes=*/NULL_TREE,
9260 /* Parse the initializer. */
9261 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9263 initializer = cp_parser_braced_list (parser, &non_constant_p);
9264 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9269 /* Consume the `='. */
9270 cp_parser_require (parser, CPP_EQ, RT_EQ);
9271 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9273 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9274 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9276 /* Process the initializer. */
9277 cp_finish_decl (decl,
9278 initializer, !non_constant_p,
9283 pop_scope (pushed_scope);
9285 return convert_from_reference (decl);
9288 /* If we didn't even get past the declarator successfully, we are
9289 definitely not looking at a declaration. */
9291 cp_parser_abort_tentative_parse (parser);
9293 /* Otherwise, we are looking at an expression. */
9294 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9297 /* Parses a for-statement or range-for-statement until the closing ')',
9301 cp_parser_for (cp_parser *parser)
9303 tree init, scope, decl;
9306 /* Begin the for-statement. */
9307 scope = begin_for_scope (&init);
9309 /* Parse the initialization. */
9310 is_range_for = cp_parser_for_init_statement (parser, &decl);
9313 return cp_parser_range_for (parser, scope, init, decl);
9315 return cp_parser_c_for (parser, scope, init);
9319 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9321 /* Normal for loop */
9322 tree condition = NULL_TREE;
9323 tree expression = NULL_TREE;
9326 stmt = begin_for_stmt (scope, init);
9327 /* The for-init-statement has already been parsed in
9328 cp_parser_for_init_statement, so no work is needed here. */
9329 finish_for_init_stmt (stmt);
9331 /* If there's a condition, process it. */
9332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9333 condition = cp_parser_condition (parser);
9334 finish_for_cond (condition, stmt);
9335 /* Look for the `;'. */
9336 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9338 /* If there's an expression, process it. */
9339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9340 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9341 finish_for_expr (expression, stmt);
9346 /* Tries to parse a range-based for-statement:
9349 decl-specifier-seq declarator : expression
9351 The decl-specifier-seq declarator and the `:' are already parsed by
9352 cp_parser_for_init_statement. If processing_template_decl it returns a
9353 newly created RANGE_FOR_STMT; if not, it is converted to a
9354 regular FOR_STMT. */
9357 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9359 tree stmt, range_expr;
9361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9363 bool expr_non_constant_p;
9364 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9367 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9369 /* If in template, STMT is converted to a normal for-statement
9370 at instantiation. If not, it is done just ahead. */
9371 if (processing_template_decl)
9373 if (check_for_bare_parameter_packs (range_expr))
9374 range_expr = error_mark_node;
9375 stmt = begin_range_for_stmt (scope, init);
9376 finish_range_for_decl (stmt, range_decl, range_expr);
9377 if (!type_dependent_expression_p (range_expr)
9378 /* do_auto_deduction doesn't mess with template init-lists. */
9379 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9380 do_range_for_auto_deduction (range_decl, range_expr);
9384 stmt = begin_for_stmt (scope, init);
9385 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9390 /* Subroutine of cp_convert_range_for: given the initializer expression,
9391 builds up the range temporary. */
9394 build_range_temp (tree range_expr)
9396 tree range_type, range_temp;
9398 /* Find out the type deduced by the declaration
9399 `auto &&__range = range_expr'. */
9400 range_type = cp_build_reference_type (make_auto (), true);
9401 range_type = do_auto_deduction (range_type, range_expr,
9402 type_uses_auto (range_type));
9404 /* Create the __range variable. */
9405 range_temp = build_decl (input_location, VAR_DECL,
9406 get_identifier ("__for_range"), range_type);
9407 TREE_USED (range_temp) = 1;
9408 DECL_ARTIFICIAL (range_temp) = 1;
9413 /* Used by cp_parser_range_for in template context: we aren't going to
9414 do a full conversion yet, but we still need to resolve auto in the
9415 type of the for-range-declaration if present. This is basically
9416 a shortcut version of cp_convert_range_for. */
9419 do_range_for_auto_deduction (tree decl, tree range_expr)
9421 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9424 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9425 range_temp = convert_from_reference (build_range_temp (range_expr));
9426 iter_type = (cp_parser_perform_range_for_lookup
9427 (range_temp, &begin_dummy, &end_dummy));
9428 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9429 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9430 tf_warning_or_error);
9431 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9432 iter_decl, auto_node);
9436 /* Converts a range-based for-statement into a normal
9437 for-statement, as per the definition.
9439 for (RANGE_DECL : RANGE_EXPR)
9442 should be equivalent to:
9445 auto &&__range = RANGE_EXPR;
9446 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9450 RANGE_DECL = *__begin;
9455 If RANGE_EXPR is an array:
9456 BEGIN_EXPR = __range
9457 END_EXPR = __range + ARRAY_SIZE(__range)
9458 Else if RANGE_EXPR has a member 'begin' or 'end':
9459 BEGIN_EXPR = __range.begin()
9460 END_EXPR = __range.end()
9462 BEGIN_EXPR = begin(__range)
9463 END_EXPR = end(__range);
9465 If __range has a member 'begin' but not 'end', or vice versa, we must
9466 still use the second alternative (it will surely fail, however).
9467 When calling begin()/end() in the third alternative we must use
9468 argument dependent lookup, but always considering 'std' as an associated
9472 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9475 tree iter_type, begin_expr, end_expr;
9476 tree condition, expression;
9478 if (range_decl == error_mark_node || range_expr == error_mark_node)
9479 /* If an error happened previously do nothing or else a lot of
9480 unhelpful errors would be issued. */
9481 begin_expr = end_expr = iter_type = error_mark_node;
9484 tree range_temp = build_range_temp (range_expr);
9485 pushdecl (range_temp);
9486 cp_finish_decl (range_temp, range_expr,
9487 /*is_constant_init*/false, NULL_TREE,
9488 LOOKUP_ONLYCONVERTING);
9490 range_temp = convert_from_reference (range_temp);
9491 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9492 &begin_expr, &end_expr);
9495 /* The new for initialization statement. */
9496 begin = build_decl (input_location, VAR_DECL,
9497 get_identifier ("__for_begin"), iter_type);
9498 TREE_USED (begin) = 1;
9499 DECL_ARTIFICIAL (begin) = 1;
9501 cp_finish_decl (begin, begin_expr,
9502 /*is_constant_init*/false, NULL_TREE,
9503 LOOKUP_ONLYCONVERTING);
9505 end = build_decl (input_location, VAR_DECL,
9506 get_identifier ("__for_end"), iter_type);
9507 TREE_USED (end) = 1;
9508 DECL_ARTIFICIAL (end) = 1;
9510 cp_finish_decl (end, end_expr,
9511 /*is_constant_init*/false, NULL_TREE,
9512 LOOKUP_ONLYCONVERTING);
9514 finish_for_init_stmt (statement);
9516 /* The new for condition. */
9517 condition = build_x_binary_op (input_location, NE_EXPR,
9520 NULL, tf_warning_or_error);
9521 finish_for_cond (condition, statement);
9523 /* The new increment expression. */
9524 expression = finish_unary_op_expr (input_location,
9525 PREINCREMENT_EXPR, begin);
9526 finish_for_expr (expression, statement);
9528 /* The declaration is initialized with *__begin inside the loop body. */
9529 cp_finish_decl (range_decl,
9530 build_x_indirect_ref (input_location, begin, RO_NULL,
9531 tf_warning_or_error),
9532 /*is_constant_init*/false, NULL_TREE,
9533 LOOKUP_ONLYCONVERTING);
9538 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9539 We need to solve both at the same time because the method used
9540 depends on the existence of members begin or end.
9541 Returns the type deduced for the iterator expression. */
9544 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9546 if (error_operand_p (range))
9548 *begin = *end = error_mark_node;
9549 return error_mark_node;
9552 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9554 error ("range-based %<for%> expression of type %qT "
9555 "has incomplete type", TREE_TYPE (range));
9556 *begin = *end = error_mark_node;
9557 return error_mark_node;
9559 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9561 /* If RANGE is an array, we will use pointer arithmetic. */
9563 *end = build_binary_op (input_location, PLUS_EXPR,
9565 array_type_nelts_top (TREE_TYPE (range)),
9567 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9571 /* If it is not an array, we must do a bit of magic. */
9572 tree id_begin, id_end;
9573 tree member_begin, member_end;
9575 *begin = *end = error_mark_node;
9577 id_begin = get_identifier ("begin");
9578 id_end = get_identifier ("end");
9579 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9580 /*protect=*/2, /*want_type=*/false,
9581 tf_warning_or_error);
9582 member_end = lookup_member (TREE_TYPE (range), id_end,
9583 /*protect=*/2, /*want_type=*/false,
9584 tf_warning_or_error);
9586 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9588 /* Use the member functions. */
9589 if (member_begin != NULL_TREE)
9590 *begin = cp_parser_range_for_member_function (range, id_begin);
9592 error ("range-based %<for%> expression of type %qT has an "
9593 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9595 if (member_end != NULL_TREE)
9596 *end = cp_parser_range_for_member_function (range, id_end);
9598 error ("range-based %<for%> expression of type %qT has a "
9599 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9603 /* Use global functions with ADL. */
9605 vec = make_tree_vector ();
9607 VEC_safe_push (tree, gc, vec, range);
9609 member_begin = perform_koenig_lookup (id_begin, vec,
9610 /*include_std=*/true,
9611 tf_warning_or_error);
9612 *begin = finish_call_expr (member_begin, &vec, false, true,
9613 tf_warning_or_error);
9614 member_end = perform_koenig_lookup (id_end, vec,
9615 /*include_std=*/true,
9616 tf_warning_or_error);
9617 *end = finish_call_expr (member_end, &vec, false, true,
9618 tf_warning_or_error);
9620 release_tree_vector (vec);
9623 /* Last common checks. */
9624 if (*begin == error_mark_node || *end == error_mark_node)
9626 /* If one of the expressions is an error do no more checks. */
9627 *begin = *end = error_mark_node;
9628 return error_mark_node;
9632 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9633 /* The unqualified type of the __begin and __end temporaries should
9634 be the same, as required by the multiple auto declaration. */
9635 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9636 error ("inconsistent begin/end types in range-based %<for%> "
9637 "statement: %qT and %qT",
9638 TREE_TYPE (*begin), TREE_TYPE (*end));
9644 /* Helper function for cp_parser_perform_range_for_lookup.
9645 Builds a tree for RANGE.IDENTIFIER(). */
9648 cp_parser_range_for_member_function (tree range, tree identifier)
9653 member = finish_class_member_access_expr (range, identifier,
9654 false, tf_warning_or_error);
9655 if (member == error_mark_node)
9656 return error_mark_node;
9658 vec = make_tree_vector ();
9659 res = finish_call_expr (member, &vec,
9660 /*disallow_virtual=*/false,
9662 tf_warning_or_error);
9663 release_tree_vector (vec);
9667 /* Parse an iteration-statement.
9669 iteration-statement:
9670 while ( condition ) statement
9671 do statement while ( expression ) ;
9672 for ( for-init-statement condition [opt] ; expression [opt] )
9675 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9678 cp_parser_iteration_statement (cp_parser* parser)
9683 unsigned char in_statement;
9685 /* Peek at the next token. */
9686 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9688 return error_mark_node;
9690 /* Remember whether or not we are already within an iteration
9692 in_statement = parser->in_statement;
9694 /* See what kind of keyword it is. */
9695 keyword = token->keyword;
9702 /* Begin the while-statement. */
9703 statement = begin_while_stmt ();
9704 /* Look for the `('. */
9705 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9706 /* Parse the condition. */
9707 condition = cp_parser_condition (parser);
9708 finish_while_stmt_cond (condition, statement);
9709 /* Look for the `)'. */
9710 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9711 /* Parse the dependent statement. */
9712 parser->in_statement = IN_ITERATION_STMT;
9713 cp_parser_already_scoped_statement (parser);
9714 parser->in_statement = in_statement;
9715 /* We're done with the while-statement. */
9716 finish_while_stmt (statement);
9724 /* Begin the do-statement. */
9725 statement = begin_do_stmt ();
9726 /* Parse the body of the do-statement. */
9727 parser->in_statement = IN_ITERATION_STMT;
9728 cp_parser_implicitly_scoped_statement (parser, NULL);
9729 parser->in_statement = in_statement;
9730 finish_do_body (statement);
9731 /* Look for the `while' keyword. */
9732 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9733 /* Look for the `('. */
9734 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9735 /* Parse the expression. */
9736 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9737 /* We're done with the do-statement. */
9738 finish_do_stmt (expression, statement);
9739 /* Look for the `)'. */
9740 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9741 /* Look for the `;'. */
9742 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9748 /* Look for the `('. */
9749 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9751 statement = cp_parser_for (parser);
9753 /* Look for the `)'. */
9754 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9756 /* Parse the body of the for-statement. */
9757 parser->in_statement = IN_ITERATION_STMT;
9758 cp_parser_already_scoped_statement (parser);
9759 parser->in_statement = in_statement;
9761 /* We're done with the for-statement. */
9762 finish_for_stmt (statement);
9767 cp_parser_error (parser, "expected iteration-statement");
9768 statement = error_mark_node;
9775 /* Parse a for-init-statement or the declarator of a range-based-for.
9776 Returns true if a range-based-for declaration is seen.
9779 expression-statement
9780 simple-declaration */
9783 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9785 /* If the next token is a `;', then we have an empty
9786 expression-statement. Grammatically, this is also a
9787 simple-declaration, but an invalid one, because it does not
9788 declare anything. Therefore, if we did not handle this case
9789 specially, we would issue an error message about an invalid
9791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9793 bool is_range_for = false;
9794 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9796 parser->colon_corrects_to_scope_p = false;
9798 /* We're going to speculatively look for a declaration, falling back
9799 to an expression, if necessary. */
9800 cp_parser_parse_tentatively (parser);
9801 /* Parse the declaration. */
9802 cp_parser_simple_declaration (parser,
9803 /*function_definition_allowed_p=*/false,
9805 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9806 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9808 /* It is a range-for, consume the ':' */
9809 cp_lexer_consume_token (parser->lexer);
9810 is_range_for = true;
9811 if (cxx_dialect < cxx0x)
9813 error_at (cp_lexer_peek_token (parser->lexer)->location,
9814 "range-based %<for%> loops are not allowed "
9816 *decl = error_mark_node;
9820 /* The ';' is not consumed yet because we told
9821 cp_parser_simple_declaration not to. */
9822 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9824 if (cp_parser_parse_definitely (parser))
9825 return is_range_for;
9826 /* If the tentative parse failed, then we shall need to look for an
9827 expression-statement. */
9829 /* If we are here, it is an expression-statement. */
9830 cp_parser_expression_statement (parser, NULL_TREE);
9834 /* Parse a jump-statement.
9839 return expression [opt] ;
9840 return braced-init-list ;
9848 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9851 cp_parser_jump_statement (cp_parser* parser)
9853 tree statement = error_mark_node;
9856 unsigned char in_statement;
9858 /* Peek at the next token. */
9859 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9861 return error_mark_node;
9863 /* See what kind of keyword it is. */
9864 keyword = token->keyword;
9868 in_statement = parser->in_statement & ~IN_IF_STMT;
9869 switch (in_statement)
9872 error_at (token->location, "break statement not within loop or switch");
9875 gcc_assert ((in_statement & IN_SWITCH_STMT)
9876 || in_statement == IN_ITERATION_STMT);
9877 statement = finish_break_stmt ();
9880 error_at (token->location, "invalid exit from OpenMP structured block");
9883 error_at (token->location, "break statement used with OpenMP for loop");
9886 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9890 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9893 error_at (token->location, "continue statement not within a loop");
9895 case IN_ITERATION_STMT:
9897 statement = finish_continue_stmt ();
9900 error_at (token->location, "invalid exit from OpenMP structured block");
9905 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9911 bool expr_non_constant_p;
9913 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9915 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9916 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9918 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9919 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9921 /* If the next token is a `;', then there is no
9924 /* Build the return-statement. */
9925 statement = finish_return_stmt (expr);
9926 /* Look for the final `;'. */
9927 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9932 /* Create the goto-statement. */
9933 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9935 /* Issue a warning about this use of a GNU extension. */
9936 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
9937 /* Consume the '*' token. */
9938 cp_lexer_consume_token (parser->lexer);
9939 /* Parse the dependent expression. */
9940 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9943 finish_goto_stmt (cp_parser_identifier (parser));
9944 /* Look for the final `;'. */
9945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9949 cp_parser_error (parser, "expected jump-statement");
9956 /* Parse a declaration-statement.
9958 declaration-statement:
9959 block-declaration */
9962 cp_parser_declaration_statement (cp_parser* parser)
9966 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9967 p = obstack_alloc (&declarator_obstack, 0);
9969 /* Parse the block-declaration. */
9970 cp_parser_block_declaration (parser, /*statement_p=*/true);
9972 /* Free any declarators allocated. */
9973 obstack_free (&declarator_obstack, p);
9975 /* Finish off the statement. */
9979 /* Some dependent statements (like `if (cond) statement'), are
9980 implicitly in their own scope. In other words, if the statement is
9981 a single statement (as opposed to a compound-statement), it is
9982 none-the-less treated as if it were enclosed in braces. Any
9983 declarations appearing in the dependent statement are out of scope
9984 after control passes that point. This function parses a statement,
9985 but ensures that is in its own scope, even if it is not a
9988 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9989 is a (possibly labeled) if statement which is not enclosed in
9990 braces and has an else clause. This is used to implement
9993 Returns the new statement. */
9996 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10003 /* Mark if () ; with a special NOP_EXPR. */
10004 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10006 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10007 cp_lexer_consume_token (parser->lexer);
10008 statement = add_stmt (build_empty_stmt (loc));
10010 /* if a compound is opened, we simply parse the statement directly. */
10011 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10012 statement = cp_parser_compound_statement (parser, NULL, false, false);
10013 /* If the token is not a `{', then we must take special action. */
10016 /* Create a compound-statement. */
10017 statement = begin_compound_stmt (0);
10018 /* Parse the dependent-statement. */
10019 cp_parser_statement (parser, NULL_TREE, false, if_p);
10020 /* Finish the dummy compound-statement. */
10021 finish_compound_stmt (statement);
10024 /* Return the statement. */
10028 /* For some dependent statements (like `while (cond) statement'), we
10029 have already created a scope. Therefore, even if the dependent
10030 statement is a compound-statement, we do not want to create another
10034 cp_parser_already_scoped_statement (cp_parser* parser)
10036 /* If the token is a `{', then we must take special action. */
10037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10038 cp_parser_statement (parser, NULL_TREE, false, NULL);
10041 /* Avoid calling cp_parser_compound_statement, so that we
10042 don't create a new scope. Do everything else by hand. */
10043 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10044 /* If the next keyword is `__label__' we have a label declaration. */
10045 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10046 cp_parser_label_declaration (parser);
10047 /* Parse an (optional) statement-seq. */
10048 cp_parser_statement_seq_opt (parser, NULL_TREE);
10049 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10053 /* Declarations [gram.dcl.dcl] */
10055 /* Parse an optional declaration-sequence.
10059 declaration-seq declaration */
10062 cp_parser_declaration_seq_opt (cp_parser* parser)
10068 token = cp_lexer_peek_token (parser->lexer);
10070 if (token->type == CPP_CLOSE_BRACE
10071 || token->type == CPP_EOF
10072 || token->type == CPP_PRAGMA_EOL)
10075 if (token->type == CPP_SEMICOLON)
10077 /* A declaration consisting of a single semicolon is
10078 invalid. Allow it unless we're being pedantic. */
10079 cp_lexer_consume_token (parser->lexer);
10080 if (!in_system_header)
10081 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10085 /* If we're entering or exiting a region that's implicitly
10086 extern "C", modify the lang context appropriately. */
10087 if (!parser->implicit_extern_c && token->implicit_extern_c)
10089 push_lang_context (lang_name_c);
10090 parser->implicit_extern_c = true;
10092 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10094 pop_lang_context ();
10095 parser->implicit_extern_c = false;
10098 if (token->type == CPP_PRAGMA)
10100 /* A top-level declaration can consist solely of a #pragma.
10101 A nested declaration cannot, so this is done here and not
10102 in cp_parser_declaration. (A #pragma at block scope is
10103 handled in cp_parser_statement.) */
10104 cp_parser_pragma (parser, pragma_external);
10108 /* Parse the declaration itself. */
10109 cp_parser_declaration (parser);
10113 /* Parse a declaration.
10117 function-definition
10118 template-declaration
10119 explicit-instantiation
10120 explicit-specialization
10121 linkage-specification
10122 namespace-definition
10127 __extension__ declaration */
10130 cp_parser_declaration (cp_parser* parser)
10134 int saved_pedantic;
10136 tree attributes = NULL_TREE;
10138 /* Check for the `__extension__' keyword. */
10139 if (cp_parser_extension_opt (parser, &saved_pedantic))
10141 /* Parse the qualified declaration. */
10142 cp_parser_declaration (parser);
10143 /* Restore the PEDANTIC flag. */
10144 pedantic = saved_pedantic;
10149 /* Try to figure out what kind of declaration is present. */
10150 token1 = *cp_lexer_peek_token (parser->lexer);
10152 if (token1.type != CPP_EOF)
10153 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10156 token2.type = CPP_EOF;
10157 token2.keyword = RID_MAX;
10160 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10161 p = obstack_alloc (&declarator_obstack, 0);
10163 /* If the next token is `extern' and the following token is a string
10164 literal, then we have a linkage specification. */
10165 if (token1.keyword == RID_EXTERN
10166 && cp_parser_is_pure_string_literal (&token2))
10167 cp_parser_linkage_specification (parser);
10168 /* If the next token is `template', then we have either a template
10169 declaration, an explicit instantiation, or an explicit
10171 else if (token1.keyword == RID_TEMPLATE)
10173 /* `template <>' indicates a template specialization. */
10174 if (token2.type == CPP_LESS
10175 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10176 cp_parser_explicit_specialization (parser);
10177 /* `template <' indicates a template declaration. */
10178 else if (token2.type == CPP_LESS)
10179 cp_parser_template_declaration (parser, /*member_p=*/false);
10180 /* Anything else must be an explicit instantiation. */
10182 cp_parser_explicit_instantiation (parser);
10184 /* If the next token is `export', then we have a template
10186 else if (token1.keyword == RID_EXPORT)
10187 cp_parser_template_declaration (parser, /*member_p=*/false);
10188 /* If the next token is `extern', 'static' or 'inline' and the one
10189 after that is `template', we have a GNU extended explicit
10190 instantiation directive. */
10191 else if (cp_parser_allow_gnu_extensions_p (parser)
10192 && (token1.keyword == RID_EXTERN
10193 || token1.keyword == RID_STATIC
10194 || token1.keyword == RID_INLINE)
10195 && token2.keyword == RID_TEMPLATE)
10196 cp_parser_explicit_instantiation (parser);
10197 /* If the next token is `namespace', check for a named or unnamed
10198 namespace definition. */
10199 else if (token1.keyword == RID_NAMESPACE
10200 && (/* A named namespace definition. */
10201 (token2.type == CPP_NAME
10202 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10204 /* An unnamed namespace definition. */
10205 || token2.type == CPP_OPEN_BRACE
10206 || token2.keyword == RID_ATTRIBUTE))
10207 cp_parser_namespace_definition (parser);
10208 /* An inline (associated) namespace definition. */
10209 else if (token1.keyword == RID_INLINE
10210 && token2.keyword == RID_NAMESPACE)
10211 cp_parser_namespace_definition (parser);
10212 /* Objective-C++ declaration/definition. */
10213 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10214 cp_parser_objc_declaration (parser, NULL_TREE);
10215 else if (c_dialect_objc ()
10216 && token1.keyword == RID_ATTRIBUTE
10217 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10218 cp_parser_objc_declaration (parser, attributes);
10219 /* We must have either a block declaration or a function
10222 /* Try to parse a block-declaration, or a function-definition. */
10223 cp_parser_block_declaration (parser, /*statement_p=*/false);
10225 /* Free any declarators allocated. */
10226 obstack_free (&declarator_obstack, p);
10229 /* Parse a block-declaration.
10234 namespace-alias-definition
10241 __extension__ block-declaration
10246 static_assert-declaration
10248 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10249 part of a declaration-statement. */
10252 cp_parser_block_declaration (cp_parser *parser,
10256 int saved_pedantic;
10258 /* Check for the `__extension__' keyword. */
10259 if (cp_parser_extension_opt (parser, &saved_pedantic))
10261 /* Parse the qualified declaration. */
10262 cp_parser_block_declaration (parser, statement_p);
10263 /* Restore the PEDANTIC flag. */
10264 pedantic = saved_pedantic;
10269 /* Peek at the next token to figure out which kind of declaration is
10271 token1 = cp_lexer_peek_token (parser->lexer);
10273 /* If the next keyword is `asm', we have an asm-definition. */
10274 if (token1->keyword == RID_ASM)
10277 cp_parser_commit_to_tentative_parse (parser);
10278 cp_parser_asm_definition (parser);
10280 /* If the next keyword is `namespace', we have a
10281 namespace-alias-definition. */
10282 else if (token1->keyword == RID_NAMESPACE)
10283 cp_parser_namespace_alias_definition (parser);
10284 /* If the next keyword is `using', we have a
10285 using-declaration, a using-directive, or an alias-declaration. */
10286 else if (token1->keyword == RID_USING)
10291 cp_parser_commit_to_tentative_parse (parser);
10292 /* If the token after `using' is `namespace', then we have a
10293 using-directive. */
10294 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10295 if (token2->keyword == RID_NAMESPACE)
10296 cp_parser_using_directive (parser);
10297 /* If the second token after 'using' is '=', then we have an
10298 alias-declaration. */
10299 else if (cxx_dialect >= cxx0x
10300 && token2->type == CPP_NAME
10301 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10302 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10303 == RID_ATTRIBUTE)))
10304 cp_parser_alias_declaration (parser);
10305 /* Otherwise, it's a using-declaration. */
10307 cp_parser_using_declaration (parser,
10308 /*access_declaration_p=*/false);
10310 /* If the next keyword is `__label__' we have a misplaced label
10312 else if (token1->keyword == RID_LABEL)
10314 cp_lexer_consume_token (parser->lexer);
10315 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10316 cp_parser_skip_to_end_of_statement (parser);
10317 /* If the next token is now a `;', consume it. */
10318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10319 cp_lexer_consume_token (parser->lexer);
10321 /* If the next token is `static_assert' we have a static assertion. */
10322 else if (token1->keyword == RID_STATIC_ASSERT)
10323 cp_parser_static_assert (parser, /*member_p=*/false);
10324 /* Anything else must be a simple-declaration. */
10326 cp_parser_simple_declaration (parser, !statement_p,
10327 /*maybe_range_for_decl*/NULL);
10330 /* Parse a simple-declaration.
10332 simple-declaration:
10333 decl-specifier-seq [opt] init-declarator-list [opt] ;
10335 init-declarator-list:
10337 init-declarator-list , init-declarator
10339 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10340 function-definition as a simple-declaration.
10342 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10343 parsed declaration if it is an uninitialized single declarator not followed
10344 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10345 if present, will not be consumed. */
10348 cp_parser_simple_declaration (cp_parser* parser,
10349 bool function_definition_allowed_p,
10350 tree *maybe_range_for_decl)
10352 cp_decl_specifier_seq decl_specifiers;
10353 int declares_class_or_enum;
10354 bool saw_declarator;
10356 if (maybe_range_for_decl)
10357 *maybe_range_for_decl = NULL_TREE;
10359 /* Defer access checks until we know what is being declared; the
10360 checks for names appearing in the decl-specifier-seq should be
10361 done as if we were in the scope of the thing being declared. */
10362 push_deferring_access_checks (dk_deferred);
10364 /* Parse the decl-specifier-seq. We have to keep track of whether
10365 or not the decl-specifier-seq declares a named class or
10366 enumeration type, since that is the only case in which the
10367 init-declarator-list is allowed to be empty.
10371 In a simple-declaration, the optional init-declarator-list can be
10372 omitted only when declaring a class or enumeration, that is when
10373 the decl-specifier-seq contains either a class-specifier, an
10374 elaborated-type-specifier, or an enum-specifier. */
10375 cp_parser_decl_specifier_seq (parser,
10376 CP_PARSER_FLAGS_OPTIONAL,
10378 &declares_class_or_enum);
10379 /* We no longer need to defer access checks. */
10380 stop_deferring_access_checks ();
10382 /* In a block scope, a valid declaration must always have a
10383 decl-specifier-seq. By not trying to parse declarators, we can
10384 resolve the declaration/expression ambiguity more quickly. */
10385 if (!function_definition_allowed_p
10386 && !decl_specifiers.any_specifiers_p)
10388 cp_parser_error (parser, "expected declaration");
10392 /* If the next two tokens are both identifiers, the code is
10393 erroneous. The usual cause of this situation is code like:
10397 where "T" should name a type -- but does not. */
10398 if (!decl_specifiers.any_type_specifiers_p
10399 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10401 /* If parsing tentatively, we should commit; we really are
10402 looking at a declaration. */
10403 cp_parser_commit_to_tentative_parse (parser);
10408 /* If we have seen at least one decl-specifier, and the next token
10409 is not a parenthesis, then we must be looking at a declaration.
10410 (After "int (" we might be looking at a functional cast.) */
10411 if (decl_specifiers.any_specifiers_p
10412 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10413 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10414 && !cp_parser_error_occurred (parser))
10415 cp_parser_commit_to_tentative_parse (parser);
10417 /* Keep going until we hit the `;' at the end of the simple
10419 saw_declarator = false;
10420 while (cp_lexer_next_token_is_not (parser->lexer,
10424 bool function_definition_p;
10427 if (saw_declarator)
10429 /* If we are processing next declarator, coma is expected */
10430 token = cp_lexer_peek_token (parser->lexer);
10431 gcc_assert (token->type == CPP_COMMA);
10432 cp_lexer_consume_token (parser->lexer);
10433 if (maybe_range_for_decl)
10434 *maybe_range_for_decl = error_mark_node;
10437 saw_declarator = true;
10439 /* Parse the init-declarator. */
10440 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10442 function_definition_allowed_p,
10443 /*member_p=*/false,
10444 declares_class_or_enum,
10445 &function_definition_p,
10446 maybe_range_for_decl);
10447 /* If an error occurred while parsing tentatively, exit quickly.
10448 (That usually happens when in the body of a function; each
10449 statement is treated as a declaration-statement until proven
10451 if (cp_parser_error_occurred (parser))
10453 /* Handle function definitions specially. */
10454 if (function_definition_p)
10456 /* If the next token is a `,', then we are probably
10457 processing something like:
10461 which is erroneous. */
10462 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10464 cp_token *token = cp_lexer_peek_token (parser->lexer);
10465 error_at (token->location,
10467 " declarations and function-definitions is forbidden");
10469 /* Otherwise, we're done with the list of declarators. */
10472 pop_deferring_access_checks ();
10476 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10477 *maybe_range_for_decl = decl;
10478 /* The next token should be either a `,' or a `;'. */
10479 token = cp_lexer_peek_token (parser->lexer);
10480 /* If it's a `,', there are more declarators to come. */
10481 if (token->type == CPP_COMMA)
10482 /* will be consumed next time around */;
10483 /* If it's a `;', we are done. */
10484 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10486 /* Anything else is an error. */
10489 /* If we have already issued an error message we don't need
10490 to issue another one. */
10491 if (decl != error_mark_node
10492 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10493 cp_parser_error (parser, "expected %<,%> or %<;%>");
10494 /* Skip tokens until we reach the end of the statement. */
10495 cp_parser_skip_to_end_of_statement (parser);
10496 /* If the next token is now a `;', consume it. */
10497 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10498 cp_lexer_consume_token (parser->lexer);
10501 /* After the first time around, a function-definition is not
10502 allowed -- even if it was OK at first. For example:
10507 function_definition_allowed_p = false;
10510 /* Issue an error message if no declarators are present, and the
10511 decl-specifier-seq does not itself declare a class or
10513 if (!saw_declarator)
10515 if (cp_parser_declares_only_class_p (parser))
10516 shadow_tag (&decl_specifiers);
10517 /* Perform any deferred access checks. */
10518 perform_deferred_access_checks ();
10521 /* Consume the `;'. */
10522 if (!maybe_range_for_decl)
10523 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10526 pop_deferring_access_checks ();
10529 /* Parse a decl-specifier-seq.
10531 decl-specifier-seq:
10532 decl-specifier-seq [opt] decl-specifier
10535 storage-class-specifier
10546 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10548 The parser flags FLAGS is used to control type-specifier parsing.
10550 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10553 1: one of the decl-specifiers is an elaborated-type-specifier
10554 (i.e., a type declaration)
10555 2: one of the decl-specifiers is an enum-specifier or a
10556 class-specifier (i.e., a type definition)
10561 cp_parser_decl_specifier_seq (cp_parser* parser,
10562 cp_parser_flags flags,
10563 cp_decl_specifier_seq *decl_specs,
10564 int* declares_class_or_enum)
10566 bool constructor_possible_p = !parser->in_declarator_p;
10567 cp_token *start_token = NULL;
10570 /* Clear DECL_SPECS. */
10571 clear_decl_specs (decl_specs);
10573 /* Assume no class or enumeration type is declared. */
10574 *declares_class_or_enum = 0;
10576 /* Keep reading specifiers until there are no more to read. */
10579 bool constructor_p;
10580 bool found_decl_spec;
10584 /* Peek at the next token. */
10585 token = cp_lexer_peek_token (parser->lexer);
10587 /* Save the first token of the decl spec list for error
10590 start_token = token;
10591 /* Handle attributes. */
10592 if (token->keyword == RID_ATTRIBUTE)
10594 /* Parse the attributes. */
10595 decl_specs->attributes
10596 = chainon (decl_specs->attributes,
10597 cp_parser_attributes_opt (parser));
10598 if (decl_specs->locations[ds_attribute] == 0)
10599 decl_specs->locations[ds_attribute] = token->location;
10602 /* Assume we will find a decl-specifier keyword. */
10603 found_decl_spec = true;
10604 /* If the next token is an appropriate keyword, we can simply
10605 add it to the list. */
10606 switch (token->keyword)
10612 if (!at_class_scope_p ())
10614 error_at (token->location, "%<friend%> used outside of class");
10615 cp_lexer_purge_token (parser->lexer);
10620 /* Consume the token. */
10621 cp_lexer_consume_token (parser->lexer);
10625 case RID_CONSTEXPR:
10627 cp_lexer_consume_token (parser->lexer);
10630 /* function-specifier:
10637 cp_parser_function_specifier_opt (parser, decl_specs);
10644 /* Consume the token. */
10645 cp_lexer_consume_token (parser->lexer);
10646 /* A constructor declarator cannot appear in a typedef. */
10647 constructor_possible_p = false;
10648 /* The "typedef" keyword can only occur in a declaration; we
10649 may as well commit at this point. */
10650 cp_parser_commit_to_tentative_parse (parser);
10652 if (decl_specs->storage_class != sc_none)
10653 decl_specs->conflicting_specifiers_p = true;
10656 /* storage-class-specifier:
10666 if (cxx_dialect == cxx98)
10668 /* Consume the token. */
10669 cp_lexer_consume_token (parser->lexer);
10671 /* Complain about `auto' as a storage specifier, if
10672 we're complaining about C++0x compatibility. */
10673 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10674 " changes meaning in C++11; please remove it");
10676 /* Set the storage class anyway. */
10677 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10681 /* C++0x auto type-specifier. */
10682 found_decl_spec = false;
10689 /* Consume the token. */
10690 cp_lexer_consume_token (parser->lexer);
10691 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10695 /* Consume the token. */
10697 cp_lexer_consume_token (parser->lexer);
10701 /* We did not yet find a decl-specifier yet. */
10702 found_decl_spec = false;
10706 if (found_decl_spec
10707 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10708 && token->keyword != RID_CONSTEXPR)
10709 error ("decl-specifier invalid in condition");
10712 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
10714 /* Constructors are a special case. The `S' in `S()' is not a
10715 decl-specifier; it is the beginning of the declarator. */
10717 = (!found_decl_spec
10718 && constructor_possible_p
10719 && (cp_parser_constructor_declarator_p
10720 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10722 /* If we don't have a DECL_SPEC yet, then we must be looking at
10723 a type-specifier. */
10724 if (!found_decl_spec && !constructor_p)
10726 int decl_spec_declares_class_or_enum;
10727 bool is_cv_qualifier;
10731 = cp_parser_type_specifier (parser, flags,
10733 /*is_declaration=*/true,
10734 &decl_spec_declares_class_or_enum,
10736 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10738 /* If this type-specifier referenced a user-defined type
10739 (a typedef, class-name, etc.), then we can't allow any
10740 more such type-specifiers henceforth.
10744 The longest sequence of decl-specifiers that could
10745 possibly be a type name is taken as the
10746 decl-specifier-seq of a declaration. The sequence shall
10747 be self-consistent as described below.
10751 As a general rule, at most one type-specifier is allowed
10752 in the complete decl-specifier-seq of a declaration. The
10753 only exceptions are the following:
10755 -- const or volatile can be combined with any other
10758 -- signed or unsigned can be combined with char, long,
10766 void g (const int Pc);
10768 Here, Pc is *not* part of the decl-specifier seq; it's
10769 the declarator. Therefore, once we see a type-specifier
10770 (other than a cv-qualifier), we forbid any additional
10771 user-defined types. We *do* still allow things like `int
10772 int' to be considered a decl-specifier-seq, and issue the
10773 error message later. */
10774 if (type_spec && !is_cv_qualifier)
10775 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10776 /* A constructor declarator cannot follow a type-specifier. */
10779 constructor_possible_p = false;
10780 found_decl_spec = true;
10781 if (!is_cv_qualifier)
10782 decl_specs->any_type_specifiers_p = true;
10786 /* If we still do not have a DECL_SPEC, then there are no more
10787 decl-specifiers. */
10788 if (!found_decl_spec)
10791 decl_specs->any_specifiers_p = true;
10792 /* After we see one decl-specifier, further decl-specifiers are
10793 always optional. */
10794 flags |= CP_PARSER_FLAGS_OPTIONAL;
10797 /* Don't allow a friend specifier with a class definition. */
10798 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10799 && (*declares_class_or_enum & 2))
10800 error_at (decl_specs->locations[ds_friend],
10801 "class definition may not be declared a friend");
10804 /* Parse an (optional) storage-class-specifier.
10806 storage-class-specifier:
10815 storage-class-specifier:
10818 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10821 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10823 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10826 if (cxx_dialect != cxx98)
10828 /* Fall through for C++98. */
10835 /* Consume the token. */
10836 return cp_lexer_consume_token (parser->lexer)->u.value;
10843 /* Parse an (optional) function-specifier.
10845 function-specifier:
10850 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10851 Updates DECL_SPECS, if it is non-NULL. */
10854 cp_parser_function_specifier_opt (cp_parser* parser,
10855 cp_decl_specifier_seq *decl_specs)
10857 cp_token *token = cp_lexer_peek_token (parser->lexer);
10858 switch (token->keyword)
10861 set_and_check_decl_spec_loc (decl_specs, ds_inline, token->location);
10865 /* 14.5.2.3 [temp.mem]
10867 A member function template shall not be virtual. */
10868 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10869 error_at (token->location, "templates may not be %<virtual%>");
10870 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token->location);
10874 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token->location);
10881 /* Consume the token. */
10882 return cp_lexer_consume_token (parser->lexer)->u.value;
10885 /* Parse a linkage-specification.
10887 linkage-specification:
10888 extern string-literal { declaration-seq [opt] }
10889 extern string-literal declaration */
10892 cp_parser_linkage_specification (cp_parser* parser)
10896 /* Look for the `extern' keyword. */
10897 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10899 /* Look for the string-literal. */
10900 linkage = cp_parser_string_literal (parser, false, false);
10902 /* Transform the literal into an identifier. If the literal is a
10903 wide-character string, or contains embedded NULs, then we can't
10904 handle it as the user wants. */
10905 if (strlen (TREE_STRING_POINTER (linkage))
10906 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10908 cp_parser_error (parser, "invalid linkage-specification");
10909 /* Assume C++ linkage. */
10910 linkage = lang_name_cplusplus;
10913 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10915 /* We're now using the new linkage. */
10916 push_lang_context (linkage);
10918 /* If the next token is a `{', then we're using the first
10920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10922 /* Consume the `{' token. */
10923 cp_lexer_consume_token (parser->lexer);
10924 /* Parse the declarations. */
10925 cp_parser_declaration_seq_opt (parser);
10926 /* Look for the closing `}'. */
10927 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10929 /* Otherwise, there's just one declaration. */
10932 bool saved_in_unbraced_linkage_specification_p;
10934 saved_in_unbraced_linkage_specification_p
10935 = parser->in_unbraced_linkage_specification_p;
10936 parser->in_unbraced_linkage_specification_p = true;
10937 cp_parser_declaration (parser);
10938 parser->in_unbraced_linkage_specification_p
10939 = saved_in_unbraced_linkage_specification_p;
10942 /* We're done with the linkage-specification. */
10943 pop_lang_context ();
10946 /* Parse a static_assert-declaration.
10948 static_assert-declaration:
10949 static_assert ( constant-expression , string-literal ) ;
10951 If MEMBER_P, this static_assert is a class member. */
10954 cp_parser_static_assert(cp_parser *parser, bool member_p)
10959 location_t saved_loc;
10962 /* Peek at the `static_assert' token so we can keep track of exactly
10963 where the static assertion started. */
10964 token = cp_lexer_peek_token (parser->lexer);
10965 saved_loc = token->location;
10967 /* Look for the `static_assert' keyword. */
10968 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10972 /* We know we are in a static assertion; commit to any tentative
10974 if (cp_parser_parsing_tentatively (parser))
10975 cp_parser_commit_to_tentative_parse (parser);
10977 /* Parse the `(' starting the static assertion condition. */
10978 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10980 /* Parse the constant-expression. Allow a non-constant expression
10981 here in order to give better diagnostics in finish_static_assert. */
10983 cp_parser_constant_expression (parser,
10984 /*allow_non_constant_p=*/true,
10985 /*non_constant_p=*/&dummy);
10987 /* Parse the separating `,'. */
10988 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10990 /* Parse the string-literal message. */
10991 message = cp_parser_string_literal (parser,
10992 /*translate=*/false,
10995 /* A `)' completes the static assertion. */
10996 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10997 cp_parser_skip_to_closing_parenthesis (parser,
10998 /*recovering=*/true,
10999 /*or_comma=*/false,
11000 /*consume_paren=*/true);
11002 /* A semicolon terminates the declaration. */
11003 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11005 /* Complete the static assertion, which may mean either processing
11006 the static assert now or saving it for template instantiation. */
11007 finish_static_assert (condition, message, saved_loc, member_p);
11010 /* Parse a `decltype' type. Returns the type.
11012 simple-type-specifier:
11013 decltype ( expression ) */
11016 cp_parser_decltype (cp_parser *parser)
11019 bool id_expression_or_member_access_p = false;
11020 const char *saved_message;
11021 bool saved_integral_constant_expression_p;
11022 bool saved_non_integral_constant_expression_p;
11023 cp_token *id_expr_start_token;
11024 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11026 if (start_token->type == CPP_DECLTYPE)
11028 /* Already parsed. */
11029 cp_lexer_consume_token (parser->lexer);
11030 return start_token->u.value;
11033 /* Look for the `decltype' token. */
11034 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11035 return error_mark_node;
11037 /* Types cannot be defined in a `decltype' expression. Save away the
11039 saved_message = parser->type_definition_forbidden_message;
11041 /* And create the new one. */
11042 parser->type_definition_forbidden_message
11043 = G_("types may not be defined in %<decltype%> expressions");
11045 /* The restrictions on constant-expressions do not apply inside
11046 decltype expressions. */
11047 saved_integral_constant_expression_p
11048 = parser->integral_constant_expression_p;
11049 saved_non_integral_constant_expression_p
11050 = parser->non_integral_constant_expression_p;
11051 parser->integral_constant_expression_p = false;
11053 /* Do not actually evaluate the expression. */
11054 ++cp_unevaluated_operand;
11056 /* Do not warn about problems with the expression. */
11057 ++c_inhibit_evaluation_warnings;
11059 /* Parse the opening `('. */
11060 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11061 return error_mark_node;
11063 /* First, try parsing an id-expression. */
11064 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11065 cp_parser_parse_tentatively (parser);
11066 expr = cp_parser_id_expression (parser,
11067 /*template_keyword_p=*/false,
11068 /*check_dependency_p=*/true,
11069 /*template_p=*/NULL,
11070 /*declarator_p=*/false,
11071 /*optional_p=*/false);
11073 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11075 bool non_integral_constant_expression_p = false;
11076 tree id_expression = expr;
11078 const char *error_msg;
11080 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11081 /* Lookup the name we got back from the id-expression. */
11082 expr = cp_parser_lookup_name (parser, expr,
11084 /*is_template=*/false,
11085 /*is_namespace=*/false,
11086 /*check_dependency=*/true,
11087 /*ambiguous_decls=*/NULL,
11088 id_expr_start_token->location);
11091 && expr != error_mark_node
11092 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11093 && TREE_CODE (expr) != TYPE_DECL
11094 && (TREE_CODE (expr) != BIT_NOT_EXPR
11095 || !TYPE_P (TREE_OPERAND (expr, 0)))
11096 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11098 /* Complete lookup of the id-expression. */
11099 expr = (finish_id_expression
11100 (id_expression, expr, parser->scope, &idk,
11101 /*integral_constant_expression_p=*/false,
11102 /*allow_non_integral_constant_expression_p=*/true,
11103 &non_integral_constant_expression_p,
11104 /*template_p=*/false,
11106 /*address_p=*/false,
11107 /*template_arg_p=*/false,
11109 id_expr_start_token->location));
11111 if (expr == error_mark_node)
11112 /* We found an id-expression, but it was something that we
11113 should not have found. This is an error, not something
11114 we can recover from, so note that we found an
11115 id-expression and we'll recover as gracefully as
11117 id_expression_or_member_access_p = true;
11121 && expr != error_mark_node
11122 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11123 /* We have an id-expression. */
11124 id_expression_or_member_access_p = true;
11127 if (!id_expression_or_member_access_p)
11129 /* Abort the id-expression parse. */
11130 cp_parser_abort_tentative_parse (parser);
11132 /* Parsing tentatively, again. */
11133 cp_parser_parse_tentatively (parser);
11135 /* Parse a class member access. */
11136 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11138 /*member_access_only_p=*/true, NULL);
11141 && expr != error_mark_node
11142 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11143 /* We have an id-expression. */
11144 id_expression_or_member_access_p = true;
11147 if (id_expression_or_member_access_p)
11148 /* We have parsed the complete id-expression or member access. */
11149 cp_parser_parse_definitely (parser);
11152 bool saved_greater_than_is_operator_p;
11154 /* Abort our attempt to parse an id-expression or member access
11156 cp_parser_abort_tentative_parse (parser);
11158 /* Within a parenthesized expression, a `>' token is always
11159 the greater-than operator. */
11160 saved_greater_than_is_operator_p
11161 = parser->greater_than_is_operator_p;
11162 parser->greater_than_is_operator_p = true;
11164 /* Parse a full expression. */
11165 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11167 /* The `>' token might be the end of a template-id or
11168 template-parameter-list now. */
11169 parser->greater_than_is_operator_p
11170 = saved_greater_than_is_operator_p;
11173 /* Go back to evaluating expressions. */
11174 --cp_unevaluated_operand;
11175 --c_inhibit_evaluation_warnings;
11177 /* Restore the old message and the integral constant expression
11179 parser->type_definition_forbidden_message = saved_message;
11180 parser->integral_constant_expression_p
11181 = saved_integral_constant_expression_p;
11182 parser->non_integral_constant_expression_p
11183 = saved_non_integral_constant_expression_p;
11185 /* Parse to the closing `)'. */
11186 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11188 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11189 /*consume_paren=*/true);
11190 return error_mark_node;
11193 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11194 tf_warning_or_error);
11196 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11198 start_token->type = CPP_DECLTYPE;
11199 start_token->u.value = expr;
11200 start_token->keyword = RID_MAX;
11201 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11206 /* Special member functions [gram.special] */
11208 /* Parse a conversion-function-id.
11210 conversion-function-id:
11211 operator conversion-type-id
11213 Returns an IDENTIFIER_NODE representing the operator. */
11216 cp_parser_conversion_function_id (cp_parser* parser)
11220 tree saved_qualifying_scope;
11221 tree saved_object_scope;
11222 tree pushed_scope = NULL_TREE;
11224 /* Look for the `operator' token. */
11225 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11226 return error_mark_node;
11227 /* When we parse the conversion-type-id, the current scope will be
11228 reset. However, we need that information in able to look up the
11229 conversion function later, so we save it here. */
11230 saved_scope = parser->scope;
11231 saved_qualifying_scope = parser->qualifying_scope;
11232 saved_object_scope = parser->object_scope;
11233 /* We must enter the scope of the class so that the names of
11234 entities declared within the class are available in the
11235 conversion-type-id. For example, consider:
11242 S::operator I() { ... }
11244 In order to see that `I' is a type-name in the definition, we
11245 must be in the scope of `S'. */
11247 pushed_scope = push_scope (saved_scope);
11248 /* Parse the conversion-type-id. */
11249 type = cp_parser_conversion_type_id (parser);
11250 /* Leave the scope of the class, if any. */
11252 pop_scope (pushed_scope);
11253 /* Restore the saved scope. */
11254 parser->scope = saved_scope;
11255 parser->qualifying_scope = saved_qualifying_scope;
11256 parser->object_scope = saved_object_scope;
11257 /* If the TYPE is invalid, indicate failure. */
11258 if (type == error_mark_node)
11259 return error_mark_node;
11260 return mangle_conv_op_name_for_type (type);
11263 /* Parse a conversion-type-id:
11265 conversion-type-id:
11266 type-specifier-seq conversion-declarator [opt]
11268 Returns the TYPE specified. */
11271 cp_parser_conversion_type_id (cp_parser* parser)
11274 cp_decl_specifier_seq type_specifiers;
11275 cp_declarator *declarator;
11276 tree type_specified;
11278 /* Parse the attributes. */
11279 attributes = cp_parser_attributes_opt (parser);
11280 /* Parse the type-specifiers. */
11281 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11282 /*is_trailing_return=*/false,
11284 /* If that didn't work, stop. */
11285 if (type_specifiers.type == error_mark_node)
11286 return error_mark_node;
11287 /* Parse the conversion-declarator. */
11288 declarator = cp_parser_conversion_declarator_opt (parser);
11290 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11291 /*initialized=*/0, &attributes);
11293 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11295 /* Don't give this error when parsing tentatively. This happens to
11296 work because we always parse this definitively once. */
11297 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11298 && type_uses_auto (type_specified))
11300 if (cxx_dialect < cxx1y)
11302 error ("invalid use of %<auto%> in conversion operator");
11303 return error_mark_node;
11305 else if (template_parm_scope_p ())
11306 warning (0, "use of %<auto%> in member template "
11307 "conversion operator can never be deduced");
11310 return type_specified;
11313 /* Parse an (optional) conversion-declarator.
11315 conversion-declarator:
11316 ptr-operator conversion-declarator [opt]
11320 static cp_declarator *
11321 cp_parser_conversion_declarator_opt (cp_parser* parser)
11323 enum tree_code code;
11325 cp_cv_quals cv_quals;
11327 /* We don't know if there's a ptr-operator next, or not. */
11328 cp_parser_parse_tentatively (parser);
11329 /* Try the ptr-operator. */
11330 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11331 /* If it worked, look for more conversion-declarators. */
11332 if (cp_parser_parse_definitely (parser))
11334 cp_declarator *declarator;
11336 /* Parse another optional declarator. */
11337 declarator = cp_parser_conversion_declarator_opt (parser);
11339 return cp_parser_make_indirect_declarator
11340 (code, class_type, cv_quals, declarator);
11346 /* Parse an (optional) ctor-initializer.
11349 : mem-initializer-list
11351 Returns TRUE iff the ctor-initializer was actually present. */
11354 cp_parser_ctor_initializer_opt (cp_parser* parser)
11356 /* If the next token is not a `:', then there is no
11357 ctor-initializer. */
11358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11360 /* Do default initialization of any bases and members. */
11361 if (DECL_CONSTRUCTOR_P (current_function_decl))
11362 finish_mem_initializers (NULL_TREE);
11367 /* Consume the `:' token. */
11368 cp_lexer_consume_token (parser->lexer);
11369 /* And the mem-initializer-list. */
11370 cp_parser_mem_initializer_list (parser);
11375 /* Parse a mem-initializer-list.
11377 mem-initializer-list:
11378 mem-initializer ... [opt]
11379 mem-initializer ... [opt] , mem-initializer-list */
11382 cp_parser_mem_initializer_list (cp_parser* parser)
11384 tree mem_initializer_list = NULL_TREE;
11385 tree target_ctor = error_mark_node;
11386 cp_token *token = cp_lexer_peek_token (parser->lexer);
11388 /* Let the semantic analysis code know that we are starting the
11389 mem-initializer-list. */
11390 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11391 error_at (token->location,
11392 "only constructors take member initializers");
11394 /* Loop through the list. */
11397 tree mem_initializer;
11399 token = cp_lexer_peek_token (parser->lexer);
11400 /* Parse the mem-initializer. */
11401 mem_initializer = cp_parser_mem_initializer (parser);
11402 /* If the next token is a `...', we're expanding member initializers. */
11403 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11405 /* Consume the `...'. */
11406 cp_lexer_consume_token (parser->lexer);
11408 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11409 can be expanded but members cannot. */
11410 if (mem_initializer != error_mark_node
11411 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11413 error_at (token->location,
11414 "cannot expand initializer for member %<%D%>",
11415 TREE_PURPOSE (mem_initializer));
11416 mem_initializer = error_mark_node;
11419 /* Construct the pack expansion type. */
11420 if (mem_initializer != error_mark_node)
11421 mem_initializer = make_pack_expansion (mem_initializer);
11423 if (target_ctor != error_mark_node
11424 && mem_initializer != error_mark_node)
11426 error ("mem-initializer for %qD follows constructor delegation",
11427 TREE_PURPOSE (mem_initializer));
11428 mem_initializer = error_mark_node;
11430 /* Look for a target constructor. */
11431 if (mem_initializer != error_mark_node
11432 && TYPE_P (TREE_PURPOSE (mem_initializer))
11433 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11435 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11436 if (mem_initializer_list)
11438 error ("constructor delegation follows mem-initializer for %qD",
11439 TREE_PURPOSE (mem_initializer_list));
11440 mem_initializer = error_mark_node;
11442 target_ctor = mem_initializer;
11444 /* Add it to the list, unless it was erroneous. */
11445 if (mem_initializer != error_mark_node)
11447 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11448 mem_initializer_list = mem_initializer;
11450 /* If the next token is not a `,', we're done. */
11451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11453 /* Consume the `,' token. */
11454 cp_lexer_consume_token (parser->lexer);
11457 /* Perform semantic analysis. */
11458 if (DECL_CONSTRUCTOR_P (current_function_decl))
11459 finish_mem_initializers (mem_initializer_list);
11462 /* Parse a mem-initializer.
11465 mem-initializer-id ( expression-list [opt] )
11466 mem-initializer-id braced-init-list
11471 ( expression-list [opt] )
11473 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11474 class) or FIELD_DECL (for a non-static data member) to initialize;
11475 the TREE_VALUE is the expression-list. An empty initialization
11476 list is represented by void_list_node. */
11479 cp_parser_mem_initializer (cp_parser* parser)
11481 tree mem_initializer_id;
11482 tree expression_list;
11484 cp_token *token = cp_lexer_peek_token (parser->lexer);
11486 /* Find out what is being initialized. */
11487 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11489 permerror (token->location,
11490 "anachronistic old-style base class initializer");
11491 mem_initializer_id = NULL_TREE;
11495 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11496 if (mem_initializer_id == error_mark_node)
11497 return mem_initializer_id;
11499 member = expand_member_init (mem_initializer_id);
11500 if (member && !DECL_P (member))
11501 in_base_initializer = 1;
11503 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11505 bool expr_non_constant_p;
11506 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11507 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11508 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11509 expression_list = build_tree_list (NULL_TREE, expression_list);
11514 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11516 /*allow_expansion_p=*/true,
11517 /*non_constant_p=*/NULL);
11519 return error_mark_node;
11520 expression_list = build_tree_list_vec (vec);
11521 release_tree_vector (vec);
11524 if (expression_list == error_mark_node)
11525 return error_mark_node;
11526 if (!expression_list)
11527 expression_list = void_type_node;
11529 in_base_initializer = 0;
11531 return member ? build_tree_list (member, expression_list) : error_mark_node;
11534 /* Parse a mem-initializer-id.
11536 mem-initializer-id:
11537 :: [opt] nested-name-specifier [opt] class-name
11540 Returns a TYPE indicating the class to be initializer for the first
11541 production. Returns an IDENTIFIER_NODE indicating the data member
11542 to be initialized for the second production. */
11545 cp_parser_mem_initializer_id (cp_parser* parser)
11547 bool global_scope_p;
11548 bool nested_name_specifier_p;
11549 bool template_p = false;
11552 cp_token *token = cp_lexer_peek_token (parser->lexer);
11554 /* `typename' is not allowed in this context ([temp.res]). */
11555 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11557 error_at (token->location,
11558 "keyword %<typename%> not allowed in this context (a qualified "
11559 "member initializer is implicitly a type)");
11560 cp_lexer_consume_token (parser->lexer);
11562 /* Look for the optional `::' operator. */
11564 = (cp_parser_global_scope_opt (parser,
11565 /*current_scope_valid_p=*/false)
11567 /* Look for the optional nested-name-specifier. The simplest way to
11572 The keyword `typename' is not permitted in a base-specifier or
11573 mem-initializer; in these contexts a qualified name that
11574 depends on a template-parameter is implicitly assumed to be a
11577 is to assume that we have seen the `typename' keyword at this
11579 nested_name_specifier_p
11580 = (cp_parser_nested_name_specifier_opt (parser,
11581 /*typename_keyword_p=*/true,
11582 /*check_dependency_p=*/true,
11584 /*is_declaration=*/true)
11586 if (nested_name_specifier_p)
11587 template_p = cp_parser_optional_template_keyword (parser);
11588 /* If there is a `::' operator or a nested-name-specifier, then we
11589 are definitely looking for a class-name. */
11590 if (global_scope_p || nested_name_specifier_p)
11591 return cp_parser_class_name (parser,
11592 /*typename_keyword_p=*/true,
11593 /*template_keyword_p=*/template_p,
11595 /*check_dependency_p=*/true,
11596 /*class_head_p=*/false,
11597 /*is_declaration=*/true);
11598 /* Otherwise, we could also be looking for an ordinary identifier. */
11599 cp_parser_parse_tentatively (parser);
11600 /* Try a class-name. */
11601 id = cp_parser_class_name (parser,
11602 /*typename_keyword_p=*/true,
11603 /*template_keyword_p=*/false,
11605 /*check_dependency_p=*/true,
11606 /*class_head_p=*/false,
11607 /*is_declaration=*/true);
11608 /* If we found one, we're done. */
11609 if (cp_parser_parse_definitely (parser))
11611 /* Otherwise, look for an ordinary identifier. */
11612 return cp_parser_identifier (parser);
11615 /* Overloading [gram.over] */
11617 /* Parse an operator-function-id.
11619 operator-function-id:
11622 Returns an IDENTIFIER_NODE for the operator which is a
11623 human-readable spelling of the identifier, e.g., `operator +'. */
11626 cp_parser_operator_function_id (cp_parser* parser)
11628 /* Look for the `operator' keyword. */
11629 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11630 return error_mark_node;
11631 /* And then the name of the operator itself. */
11632 return cp_parser_operator (parser);
11635 /* Return an identifier node for a user-defined literal operator.
11636 The suffix identifier is chained to the operator name identifier. */
11639 cp_literal_operator_id (const char* name)
11642 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11643 + strlen (name) + 10);
11644 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11645 identifier = get_identifier (buffer);
11646 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11651 /* Parse an operator.
11654 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11655 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11656 || ++ -- , ->* -> () []
11663 Returns an IDENTIFIER_NODE for the operator which is a
11664 human-readable spelling of the identifier, e.g., `operator +'. */
11667 cp_parser_operator (cp_parser* parser)
11669 tree id = NULL_TREE;
11672 /* Peek at the next token. */
11673 token = cp_lexer_peek_token (parser->lexer);
11674 /* Figure out which operator we have. */
11675 switch (token->type)
11681 /* The keyword should be either `new' or `delete'. */
11682 if (token->keyword == RID_NEW)
11684 else if (token->keyword == RID_DELETE)
11689 /* Consume the `new' or `delete' token. */
11690 cp_lexer_consume_token (parser->lexer);
11692 /* Peek at the next token. */
11693 token = cp_lexer_peek_token (parser->lexer);
11694 /* If it's a `[' token then this is the array variant of the
11696 if (token->type == CPP_OPEN_SQUARE)
11698 /* Consume the `[' token. */
11699 cp_lexer_consume_token (parser->lexer);
11700 /* Look for the `]' token. */
11701 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11702 id = ansi_opname (op == NEW_EXPR
11703 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11705 /* Otherwise, we have the non-array variant. */
11707 id = ansi_opname (op);
11713 id = ansi_opname (PLUS_EXPR);
11717 id = ansi_opname (MINUS_EXPR);
11721 id = ansi_opname (MULT_EXPR);
11725 id = ansi_opname (TRUNC_DIV_EXPR);
11729 id = ansi_opname (TRUNC_MOD_EXPR);
11733 id = ansi_opname (BIT_XOR_EXPR);
11737 id = ansi_opname (BIT_AND_EXPR);
11741 id = ansi_opname (BIT_IOR_EXPR);
11745 id = ansi_opname (BIT_NOT_EXPR);
11749 id = ansi_opname (TRUTH_NOT_EXPR);
11753 id = ansi_assopname (NOP_EXPR);
11757 id = ansi_opname (LT_EXPR);
11761 id = ansi_opname (GT_EXPR);
11765 id = ansi_assopname (PLUS_EXPR);
11769 id = ansi_assopname (MINUS_EXPR);
11773 id = ansi_assopname (MULT_EXPR);
11777 id = ansi_assopname (TRUNC_DIV_EXPR);
11781 id = ansi_assopname (TRUNC_MOD_EXPR);
11785 id = ansi_assopname (BIT_XOR_EXPR);
11789 id = ansi_assopname (BIT_AND_EXPR);
11793 id = ansi_assopname (BIT_IOR_EXPR);
11797 id = ansi_opname (LSHIFT_EXPR);
11801 id = ansi_opname (RSHIFT_EXPR);
11804 case CPP_LSHIFT_EQ:
11805 id = ansi_assopname (LSHIFT_EXPR);
11808 case CPP_RSHIFT_EQ:
11809 id = ansi_assopname (RSHIFT_EXPR);
11813 id = ansi_opname (EQ_EXPR);
11817 id = ansi_opname (NE_EXPR);
11821 id = ansi_opname (LE_EXPR);
11824 case CPP_GREATER_EQ:
11825 id = ansi_opname (GE_EXPR);
11829 id = ansi_opname (TRUTH_ANDIF_EXPR);
11833 id = ansi_opname (TRUTH_ORIF_EXPR);
11836 case CPP_PLUS_PLUS:
11837 id = ansi_opname (POSTINCREMENT_EXPR);
11840 case CPP_MINUS_MINUS:
11841 id = ansi_opname (PREDECREMENT_EXPR);
11845 id = ansi_opname (COMPOUND_EXPR);
11848 case CPP_DEREF_STAR:
11849 id = ansi_opname (MEMBER_REF);
11853 id = ansi_opname (COMPONENT_REF);
11856 case CPP_OPEN_PAREN:
11857 /* Consume the `('. */
11858 cp_lexer_consume_token (parser->lexer);
11859 /* Look for the matching `)'. */
11860 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11861 return ansi_opname (CALL_EXPR);
11863 case CPP_OPEN_SQUARE:
11864 /* Consume the `['. */
11865 cp_lexer_consume_token (parser->lexer);
11866 /* Look for the matching `]'. */
11867 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11868 return ansi_opname (ARRAY_REF);
11871 if (cxx_dialect == cxx98)
11872 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11873 if (TREE_STRING_LENGTH (token->u.value) > 2)
11875 error ("expected empty string after %<operator%> keyword");
11876 return error_mark_node;
11878 /* Consume the string. */
11879 cp_lexer_consume_token (parser->lexer);
11880 /* Look for the suffix identifier. */
11881 token = cp_lexer_peek_token (parser->lexer);
11882 if (token->type == CPP_NAME)
11884 id = cp_parser_identifier (parser);
11885 if (id != error_mark_node)
11887 const char *name = IDENTIFIER_POINTER (id);
11888 return cp_literal_operator_id (name);
11893 error ("expected suffix identifier");
11894 return error_mark_node;
11897 case CPP_STRING_USERDEF:
11898 error ("missing space between %<\"\"%> and suffix identifier");
11899 return error_mark_node;
11902 /* Anything else is an error. */
11906 /* If we have selected an identifier, we need to consume the
11909 cp_lexer_consume_token (parser->lexer);
11910 /* Otherwise, no valid operator name was present. */
11913 cp_parser_error (parser, "expected operator");
11914 id = error_mark_node;
11920 /* Parse a template-declaration.
11922 template-declaration:
11923 export [opt] template < template-parameter-list > declaration
11925 If MEMBER_P is TRUE, this template-declaration occurs within a
11928 The grammar rule given by the standard isn't correct. What
11929 is really meant is:
11931 template-declaration:
11932 export [opt] template-parameter-list-seq
11933 decl-specifier-seq [opt] init-declarator [opt] ;
11934 export [opt] template-parameter-list-seq
11935 function-definition
11937 template-parameter-list-seq:
11938 template-parameter-list-seq [opt]
11939 template < template-parameter-list > */
11942 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11944 /* Check for `export'. */
11945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11947 /* Consume the `export' token. */
11948 cp_lexer_consume_token (parser->lexer);
11949 /* Warn that we do not support `export'. */
11950 warning (0, "keyword %<export%> not implemented, and will be ignored");
11953 cp_parser_template_declaration_after_export (parser, member_p);
11956 /* Parse a template-parameter-list.
11958 template-parameter-list:
11960 template-parameter-list , template-parameter
11962 Returns a TREE_LIST. Each node represents a template parameter.
11963 The nodes are connected via their TREE_CHAINs. */
11966 cp_parser_template_parameter_list (cp_parser* parser)
11968 tree parameter_list = NULL_TREE;
11970 begin_template_parm_list ();
11972 /* The loop below parses the template parms. We first need to know
11973 the total number of template parms to be able to compute proper
11974 canonical types of each dependent type. So after the loop, when
11975 we know the total number of template parms,
11976 end_template_parm_list computes the proper canonical types and
11977 fixes up the dependent types accordingly. */
11982 bool is_parameter_pack;
11983 location_t parm_loc;
11985 /* Parse the template-parameter. */
11986 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11987 parameter = cp_parser_template_parameter (parser,
11989 &is_parameter_pack);
11990 /* Add it to the list. */
11991 if (parameter != error_mark_node)
11992 parameter_list = process_template_parm (parameter_list,
11996 is_parameter_pack);
11999 tree err_parm = build_tree_list (parameter, parameter);
12000 parameter_list = chainon (parameter_list, err_parm);
12003 /* If the next token is not a `,', we're done. */
12004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12006 /* Otherwise, consume the `,' token. */
12007 cp_lexer_consume_token (parser->lexer);
12010 return end_template_parm_list (parameter_list);
12013 /* Parse a template-parameter.
12015 template-parameter:
12017 parameter-declaration
12019 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12020 the parameter. The TREE_PURPOSE is the default value, if any.
12021 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12022 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12023 set to true iff this parameter is a parameter pack. */
12026 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12027 bool *is_parameter_pack)
12030 cp_parameter_declarator *parameter_declarator;
12031 cp_declarator *id_declarator;
12034 /* Assume it is a type parameter or a template parameter. */
12035 *is_non_type = false;
12036 /* Assume it not a parameter pack. */
12037 *is_parameter_pack = false;
12038 /* Peek at the next token. */
12039 token = cp_lexer_peek_token (parser->lexer);
12040 /* If it is `class' or `template', we have a type-parameter. */
12041 if (token->keyword == RID_TEMPLATE)
12042 return cp_parser_type_parameter (parser, is_parameter_pack);
12043 /* If it is `class' or `typename' we do not know yet whether it is a
12044 type parameter or a non-type parameter. Consider:
12046 template <typename T, typename T::X X> ...
12050 template <class C, class D*> ...
12052 Here, the first parameter is a type parameter, and the second is
12053 a non-type parameter. We can tell by looking at the token after
12054 the identifier -- if it is a `,', `=', or `>' then we have a type
12056 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12058 /* Peek at the token after `class' or `typename'. */
12059 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12060 /* If it's an ellipsis, we have a template type parameter
12062 if (token->type == CPP_ELLIPSIS)
12063 return cp_parser_type_parameter (parser, is_parameter_pack);
12064 /* If it's an identifier, skip it. */
12065 if (token->type == CPP_NAME)
12066 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12067 /* Now, see if the token looks like the end of a template
12069 if (token->type == CPP_COMMA
12070 || token->type == CPP_EQ
12071 || token->type == CPP_GREATER)
12072 return cp_parser_type_parameter (parser, is_parameter_pack);
12075 /* Otherwise, it is a non-type parameter.
12079 When parsing a default template-argument for a non-type
12080 template-parameter, the first non-nested `>' is taken as the end
12081 of the template parameter-list rather than a greater-than
12083 *is_non_type = true;
12084 parameter_declarator
12085 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12086 /*parenthesized_p=*/NULL);
12088 /* If the parameter declaration is marked as a parameter pack, set
12089 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12090 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12092 if (parameter_declarator
12093 && parameter_declarator->declarator
12094 && parameter_declarator->declarator->parameter_pack_p)
12096 *is_parameter_pack = true;
12097 parameter_declarator->declarator->parameter_pack_p = false;
12100 /* If the next token is an ellipsis, and we don't already have it
12101 marked as a parameter pack, then we have a parameter pack (that
12102 has no declarator). */
12103 if (!*is_parameter_pack
12104 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12105 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12107 /* Consume the `...'. */
12108 cp_lexer_consume_token (parser->lexer);
12109 maybe_warn_variadic_templates ();
12111 *is_parameter_pack = true;
12113 /* We might end up with a pack expansion as the type of the non-type
12114 template parameter, in which case this is a non-type template
12116 else if (parameter_declarator
12117 && parameter_declarator->decl_specifiers.type
12118 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12120 *is_parameter_pack = true;
12121 parameter_declarator->decl_specifiers.type =
12122 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12125 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12127 /* Parameter packs cannot have default arguments. However, a
12128 user may try to do so, so we'll parse them and give an
12129 appropriate diagnostic here. */
12131 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12133 /* Find the name of the parameter pack. */
12134 id_declarator = parameter_declarator->declarator;
12135 while (id_declarator && id_declarator->kind != cdk_id)
12136 id_declarator = id_declarator->declarator;
12138 if (id_declarator && id_declarator->kind == cdk_id)
12139 error_at (start_token->location,
12140 "template parameter pack %qD cannot have a default argument",
12141 id_declarator->u.id.unqualified_name);
12143 error_at (start_token->location,
12144 "template parameter pack cannot have a default argument");
12146 /* Parse the default argument, but throw away the result. */
12147 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12150 parm = grokdeclarator (parameter_declarator->declarator,
12151 ¶meter_declarator->decl_specifiers,
12152 TPARM, /*initialized=*/0,
12153 /*attrlist=*/NULL);
12154 if (parm == error_mark_node)
12155 return error_mark_node;
12157 return build_tree_list (parameter_declarator->default_argument, parm);
12160 /* Parse a type-parameter.
12163 class identifier [opt]
12164 class identifier [opt] = type-id
12165 typename identifier [opt]
12166 typename identifier [opt] = type-id
12167 template < template-parameter-list > class identifier [opt]
12168 template < template-parameter-list > class identifier [opt]
12171 GNU Extension (variadic templates):
12174 class ... identifier [opt]
12175 typename ... identifier [opt]
12177 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12178 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12179 the declaration of the parameter.
12181 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12184 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12189 /* Look for a keyword to tell us what kind of parameter this is. */
12190 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12192 return error_mark_node;
12194 switch (token->keyword)
12200 tree default_argument;
12202 /* If the next token is an ellipsis, we have a template
12204 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12206 /* Consume the `...' token. */
12207 cp_lexer_consume_token (parser->lexer);
12208 maybe_warn_variadic_templates ();
12210 *is_parameter_pack = true;
12213 /* If the next token is an identifier, then it names the
12215 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12216 identifier = cp_parser_identifier (parser);
12218 identifier = NULL_TREE;
12220 /* Create the parameter. */
12221 parameter = finish_template_type_parm (class_type_node, identifier);
12223 /* If the next token is an `=', we have a default argument. */
12224 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12226 /* Consume the `=' token. */
12227 cp_lexer_consume_token (parser->lexer);
12228 /* Parse the default-argument. */
12229 push_deferring_access_checks (dk_no_deferred);
12230 default_argument = cp_parser_type_id (parser);
12232 /* Template parameter packs cannot have default
12234 if (*is_parameter_pack)
12237 error_at (token->location,
12238 "template parameter pack %qD cannot have a "
12239 "default argument", identifier);
12241 error_at (token->location,
12242 "template parameter packs cannot have "
12243 "default arguments");
12244 default_argument = NULL_TREE;
12246 pop_deferring_access_checks ();
12249 default_argument = NULL_TREE;
12251 /* Create the combined representation of the parameter and the
12252 default argument. */
12253 parameter = build_tree_list (default_argument, parameter);
12260 tree default_argument;
12262 /* Look for the `<'. */
12263 cp_parser_require (parser, CPP_LESS, RT_LESS);
12264 /* Parse the template-parameter-list. */
12265 cp_parser_template_parameter_list (parser);
12266 /* Look for the `>'. */
12267 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12268 /* Look for the `class' keyword. */
12269 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12270 /* If the next token is an ellipsis, we have a template
12272 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12274 /* Consume the `...' token. */
12275 cp_lexer_consume_token (parser->lexer);
12276 maybe_warn_variadic_templates ();
12278 *is_parameter_pack = true;
12280 /* If the next token is an `=', then there is a
12281 default-argument. If the next token is a `>', we are at
12282 the end of the parameter-list. If the next token is a `,',
12283 then we are at the end of this parameter. */
12284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12285 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12286 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12288 identifier = cp_parser_identifier (parser);
12289 /* Treat invalid names as if the parameter were nameless. */
12290 if (identifier == error_mark_node)
12291 identifier = NULL_TREE;
12294 identifier = NULL_TREE;
12296 /* Create the template parameter. */
12297 parameter = finish_template_template_parm (class_type_node,
12300 /* If the next token is an `=', then there is a
12301 default-argument. */
12302 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12306 /* Consume the `='. */
12307 cp_lexer_consume_token (parser->lexer);
12308 /* Parse the id-expression. */
12309 push_deferring_access_checks (dk_no_deferred);
12310 /* save token before parsing the id-expression, for error
12312 token = cp_lexer_peek_token (parser->lexer);
12314 = cp_parser_id_expression (parser,
12315 /*template_keyword_p=*/false,
12316 /*check_dependency_p=*/true,
12317 /*template_p=*/&is_template,
12318 /*declarator_p=*/false,
12319 /*optional_p=*/false);
12320 if (TREE_CODE (default_argument) == TYPE_DECL)
12321 /* If the id-expression was a template-id that refers to
12322 a template-class, we already have the declaration here,
12323 so no further lookup is needed. */
12326 /* Look up the name. */
12328 = cp_parser_lookup_name (parser, default_argument,
12330 /*is_template=*/is_template,
12331 /*is_namespace=*/false,
12332 /*check_dependency=*/true,
12333 /*ambiguous_decls=*/NULL,
12335 /* See if the default argument is valid. */
12337 = check_template_template_default_arg (default_argument);
12339 /* Template parameter packs cannot have default
12341 if (*is_parameter_pack)
12344 error_at (token->location,
12345 "template parameter pack %qD cannot "
12346 "have a default argument",
12349 error_at (token->location, "template parameter packs cannot "
12350 "have default arguments");
12351 default_argument = NULL_TREE;
12353 pop_deferring_access_checks ();
12356 default_argument = NULL_TREE;
12358 /* Create the combined representation of the parameter and the
12359 default argument. */
12360 parameter = build_tree_list (default_argument, parameter);
12365 gcc_unreachable ();
12372 /* Parse a template-id.
12375 template-name < template-argument-list [opt] >
12377 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12378 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12379 returned. Otherwise, if the template-name names a function, or set
12380 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12381 names a class, returns a TYPE_DECL for the specialization.
12383 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12384 uninstantiated templates. */
12387 cp_parser_template_id (cp_parser *parser,
12388 bool template_keyword_p,
12389 bool check_dependency_p,
12390 enum tag_types tag_type,
12391 bool is_declaration)
12397 cp_token_position start_of_id = 0;
12398 deferred_access_check *chk;
12399 VEC (deferred_access_check,gc) *access_check;
12400 cp_token *next_token = NULL, *next_token_2 = NULL;
12401 bool is_identifier;
12403 /* If the next token corresponds to a template-id, there is no need
12405 next_token = cp_lexer_peek_token (parser->lexer);
12406 if (next_token->type == CPP_TEMPLATE_ID)
12408 struct tree_check *check_value;
12410 /* Get the stored value. */
12411 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12412 /* Perform any access checks that were deferred. */
12413 access_check = check_value->checks;
12416 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12417 perform_or_defer_access_check (chk->binfo,
12421 /* Return the stored value. */
12422 return check_value->value;
12425 /* Avoid performing name lookup if there is no possibility of
12426 finding a template-id. */
12427 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12428 || (next_token->type == CPP_NAME
12429 && !cp_parser_nth_token_starts_template_argument_list_p
12432 cp_parser_error (parser, "expected template-id");
12433 return error_mark_node;
12436 /* Remember where the template-id starts. */
12437 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12438 start_of_id = cp_lexer_token_position (parser->lexer, false);
12440 push_deferring_access_checks (dk_deferred);
12442 /* Parse the template-name. */
12443 is_identifier = false;
12444 templ = cp_parser_template_name (parser, template_keyword_p,
12445 check_dependency_p,
12449 if (templ == error_mark_node || is_identifier)
12451 pop_deferring_access_checks ();
12455 /* If we find the sequence `[:' after a template-name, it's probably
12456 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12457 parse correctly the argument list. */
12458 next_token = cp_lexer_peek_token (parser->lexer);
12459 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12460 if (next_token->type == CPP_OPEN_SQUARE
12461 && next_token->flags & DIGRAPH
12462 && next_token_2->type == CPP_COLON
12463 && !(next_token_2->flags & PREV_WHITE))
12465 cp_parser_parse_tentatively (parser);
12466 /* Change `:' into `::'. */
12467 next_token_2->type = CPP_SCOPE;
12468 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12470 cp_lexer_consume_token (parser->lexer);
12472 /* Parse the arguments. */
12473 arguments = cp_parser_enclosed_template_argument_list (parser);
12474 if (!cp_parser_parse_definitely (parser))
12476 /* If we couldn't parse an argument list, then we revert our changes
12477 and return simply an error. Maybe this is not a template-id
12479 next_token_2->type = CPP_COLON;
12480 cp_parser_error (parser, "expected %<<%>");
12481 pop_deferring_access_checks ();
12482 return error_mark_node;
12484 /* Otherwise, emit an error about the invalid digraph, but continue
12485 parsing because we got our argument list. */
12486 if (permerror (next_token->location,
12487 "%<<::%> cannot begin a template-argument list"))
12489 static bool hint = false;
12490 inform (next_token->location,
12491 "%<<:%> is an alternate spelling for %<[%>."
12492 " Insert whitespace between %<<%> and %<::%>");
12493 if (!hint && !flag_permissive)
12495 inform (next_token->location, "(if you use %<-fpermissive%>"
12496 " G++ will accept your code)");
12503 /* Look for the `<' that starts the template-argument-list. */
12504 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12506 pop_deferring_access_checks ();
12507 return error_mark_node;
12509 /* Parse the arguments. */
12510 arguments = cp_parser_enclosed_template_argument_list (parser);
12513 /* Build a representation of the specialization. */
12514 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12515 template_id = build_min_nt_loc (next_token->location,
12518 else if (DECL_TYPE_TEMPLATE_P (templ)
12519 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12521 bool entering_scope;
12522 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12523 template (rather than some instantiation thereof) only if
12524 is not nested within some other construct. For example, in
12525 "template <typename T> void f(T) { A<T>::", A<T> is just an
12526 instantiation of A. */
12527 entering_scope = (template_parm_scope_p ()
12528 && cp_lexer_next_token_is (parser->lexer,
12531 = finish_template_type (templ, arguments, entering_scope);
12535 /* If it's not a class-template or a template-template, it should be
12536 a function-template. */
12537 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12538 || TREE_CODE (templ) == OVERLOAD
12539 || BASELINK_P (templ)));
12541 template_id = lookup_template_function (templ, arguments);
12544 /* If parsing tentatively, replace the sequence of tokens that makes
12545 up the template-id with a CPP_TEMPLATE_ID token. That way,
12546 should we re-parse the token stream, we will not have to repeat
12547 the effort required to do the parse, nor will we issue duplicate
12548 error messages about problems during instantiation of the
12552 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12554 /* Reset the contents of the START_OF_ID token. */
12555 token->type = CPP_TEMPLATE_ID;
12556 /* Retrieve any deferred checks. Do not pop this access checks yet
12557 so the memory will not be reclaimed during token replacing below. */
12558 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12559 token->u.tree_check_value->value = template_id;
12560 token->u.tree_check_value->checks = get_deferred_access_checks ();
12561 token->keyword = RID_MAX;
12563 /* Purge all subsequent tokens. */
12564 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12566 /* ??? Can we actually assume that, if template_id ==
12567 error_mark_node, we will have issued a diagnostic to the
12568 user, as opposed to simply marking the tentative parse as
12570 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12571 error_at (token->location, "parse error in template argument list");
12574 pop_deferring_access_checks ();
12575 return template_id;
12578 /* Parse a template-name.
12583 The standard should actually say:
12587 operator-function-id
12589 A defect report has been filed about this issue.
12591 A conversion-function-id cannot be a template name because they cannot
12592 be part of a template-id. In fact, looking at this code:
12594 a.operator K<int>()
12596 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12597 It is impossible to call a templated conversion-function-id with an
12598 explicit argument list, since the only allowed template parameter is
12599 the type to which it is converting.
12601 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12602 `template' keyword, in a construction like:
12606 In that case `f' is taken to be a template-name, even though there
12607 is no way of knowing for sure.
12609 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12610 name refers to a set of overloaded functions, at least one of which
12611 is a template, or an IDENTIFIER_NODE with the name of the template,
12612 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12613 names are looked up inside uninstantiated templates. */
12616 cp_parser_template_name (cp_parser* parser,
12617 bool template_keyword_p,
12618 bool check_dependency_p,
12619 bool is_declaration,
12620 enum tag_types tag_type,
12621 bool *is_identifier)
12626 cp_token *token = cp_lexer_peek_token (parser->lexer);
12628 /* If the next token is `operator', then we have either an
12629 operator-function-id or a conversion-function-id. */
12630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12632 /* We don't know whether we're looking at an
12633 operator-function-id or a conversion-function-id. */
12634 cp_parser_parse_tentatively (parser);
12635 /* Try an operator-function-id. */
12636 identifier = cp_parser_operator_function_id (parser);
12637 /* If that didn't work, try a conversion-function-id. */
12638 if (!cp_parser_parse_definitely (parser))
12640 cp_parser_error (parser, "expected template-name");
12641 return error_mark_node;
12644 /* Look for the identifier. */
12646 identifier = cp_parser_identifier (parser);
12648 /* If we didn't find an identifier, we don't have a template-id. */
12649 if (identifier == error_mark_node)
12650 return error_mark_node;
12652 /* If the name immediately followed the `template' keyword, then it
12653 is a template-name. However, if the next token is not `<', then
12654 we do not treat it as a template-name, since it is not being used
12655 as part of a template-id. This enables us to handle constructs
12658 template <typename T> struct S { S(); };
12659 template <typename T> S<T>::S();
12661 correctly. We would treat `S' as a template -- if it were `S<T>'
12662 -- but we do not if there is no `<'. */
12664 if (processing_template_decl
12665 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12667 /* In a declaration, in a dependent context, we pretend that the
12668 "template" keyword was present in order to improve error
12669 recovery. For example, given:
12671 template <typename T> void f(T::X<int>);
12673 we want to treat "X<int>" as a template-id. */
12675 && !template_keyword_p
12676 && parser->scope && TYPE_P (parser->scope)
12677 && check_dependency_p
12678 && dependent_scope_p (parser->scope)
12679 /* Do not do this for dtors (or ctors), since they never
12680 need the template keyword before their name. */
12681 && !constructor_name_p (identifier, parser->scope))
12683 cp_token_position start = 0;
12685 /* Explain what went wrong. */
12686 error_at (token->location, "non-template %qD used as template",
12688 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12689 parser->scope, identifier);
12690 /* If parsing tentatively, find the location of the "<" token. */
12691 if (cp_parser_simulate_error (parser))
12692 start = cp_lexer_token_position (parser->lexer, true);
12693 /* Parse the template arguments so that we can issue error
12694 messages about them. */
12695 cp_lexer_consume_token (parser->lexer);
12696 cp_parser_enclosed_template_argument_list (parser);
12697 /* Skip tokens until we find a good place from which to
12698 continue parsing. */
12699 cp_parser_skip_to_closing_parenthesis (parser,
12700 /*recovering=*/true,
12702 /*consume_paren=*/false);
12703 /* If parsing tentatively, permanently remove the
12704 template argument list. That will prevent duplicate
12705 error messages from being issued about the missing
12706 "template" keyword. */
12708 cp_lexer_purge_tokens_after (parser->lexer, start);
12710 *is_identifier = true;
12714 /* If the "template" keyword is present, then there is generally
12715 no point in doing name-lookup, so we just return IDENTIFIER.
12716 But, if the qualifying scope is non-dependent then we can
12717 (and must) do name-lookup normally. */
12718 if (template_keyword_p
12720 || (TYPE_P (parser->scope)
12721 && dependent_type_p (parser->scope))))
12725 /* Look up the name. */
12726 decl = cp_parser_lookup_name (parser, identifier,
12728 /*is_template=*/true,
12729 /*is_namespace=*/false,
12730 check_dependency_p,
12731 /*ambiguous_decls=*/NULL,
12734 /* If DECL is a template, then the name was a template-name. */
12735 if (TREE_CODE (decl) == TEMPLATE_DECL)
12739 tree fn = NULL_TREE;
12741 /* The standard does not explicitly indicate whether a name that
12742 names a set of overloaded declarations, some of which are
12743 templates, is a template-name. However, such a name should
12744 be a template-name; otherwise, there is no way to form a
12745 template-id for the overloaded templates. */
12746 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12747 if (TREE_CODE (fns) == OVERLOAD)
12748 for (fn = fns; fn; fn = OVL_NEXT (fn))
12749 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12754 /* The name does not name a template. */
12755 cp_parser_error (parser, "expected template-name");
12756 return error_mark_node;
12760 /* If DECL is dependent, and refers to a function, then just return
12761 its name; we will look it up again during template instantiation. */
12762 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12764 tree scope = ovl_scope (decl);
12765 if (TYPE_P (scope) && dependent_type_p (scope))
12772 /* Parse a template-argument-list.
12774 template-argument-list:
12775 template-argument ... [opt]
12776 template-argument-list , template-argument ... [opt]
12778 Returns a TREE_VEC containing the arguments. */
12781 cp_parser_template_argument_list (cp_parser* parser)
12783 tree fixed_args[10];
12784 unsigned n_args = 0;
12785 unsigned alloced = 10;
12786 tree *arg_ary = fixed_args;
12788 bool saved_in_template_argument_list_p;
12790 bool saved_non_ice_p;
12792 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12793 parser->in_template_argument_list_p = true;
12794 /* Even if the template-id appears in an integral
12795 constant-expression, the contents of the argument list do
12797 saved_ice_p = parser->integral_constant_expression_p;
12798 parser->integral_constant_expression_p = false;
12799 saved_non_ice_p = parser->non_integral_constant_expression_p;
12800 parser->non_integral_constant_expression_p = false;
12802 /* Parse the arguments. */
12808 /* Consume the comma. */
12809 cp_lexer_consume_token (parser->lexer);
12811 /* Parse the template-argument. */
12812 argument = cp_parser_template_argument (parser);
12814 /* If the next token is an ellipsis, we're expanding a template
12816 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12818 if (argument == error_mark_node)
12820 cp_token *token = cp_lexer_peek_token (parser->lexer);
12821 error_at (token->location,
12822 "expected parameter pack before %<...%>");
12824 /* Consume the `...' token. */
12825 cp_lexer_consume_token (parser->lexer);
12827 /* Make the argument into a TYPE_PACK_EXPANSION or
12828 EXPR_PACK_EXPANSION. */
12829 argument = make_pack_expansion (argument);
12832 if (n_args == alloced)
12836 if (arg_ary == fixed_args)
12838 arg_ary = XNEWVEC (tree, alloced);
12839 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12842 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12844 arg_ary[n_args++] = argument;
12846 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12848 vec = make_tree_vec (n_args);
12851 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12853 if (arg_ary != fixed_args)
12855 parser->non_integral_constant_expression_p = saved_non_ice_p;
12856 parser->integral_constant_expression_p = saved_ice_p;
12857 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12858 #ifdef ENABLE_CHECKING
12859 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12864 /* Parse a template-argument.
12867 assignment-expression
12871 The representation is that of an assignment-expression, type-id, or
12872 id-expression -- except that the qualified id-expression is
12873 evaluated, so that the value returned is either a DECL or an
12876 Although the standard says "assignment-expression", it forbids
12877 throw-expressions or assignments in the template argument.
12878 Therefore, we use "conditional-expression" instead. */
12881 cp_parser_template_argument (cp_parser* parser)
12886 bool maybe_type_id = false;
12887 cp_token *token = NULL, *argument_start_token = NULL;
12888 location_t loc = 0;
12891 /* There's really no way to know what we're looking at, so we just
12892 try each alternative in order.
12896 In a template-argument, an ambiguity between a type-id and an
12897 expression is resolved to a type-id, regardless of the form of
12898 the corresponding template-parameter.
12900 Therefore, we try a type-id first. */
12901 cp_parser_parse_tentatively (parser);
12902 argument = cp_parser_template_type_arg (parser);
12903 /* If there was no error parsing the type-id but the next token is a
12904 '>>', our behavior depends on which dialect of C++ we're
12905 parsing. In C++98, we probably found a typo for '> >'. But there
12906 are type-id which are also valid expressions. For instance:
12908 struct X { int operator >> (int); };
12909 template <int V> struct Foo {};
12912 Here 'X()' is a valid type-id of a function type, but the user just
12913 wanted to write the expression "X() >> 5". Thus, we remember that we
12914 found a valid type-id, but we still try to parse the argument as an
12915 expression to see what happens.
12917 In C++0x, the '>>' will be considered two separate '>'
12919 if (!cp_parser_error_occurred (parser)
12920 && cxx_dialect == cxx98
12921 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12923 maybe_type_id = true;
12924 cp_parser_abort_tentative_parse (parser);
12928 /* If the next token isn't a `,' or a `>', then this argument wasn't
12929 really finished. This means that the argument is not a valid
12931 if (!cp_parser_next_token_ends_template_argument_p (parser))
12932 cp_parser_error (parser, "expected template-argument");
12933 /* If that worked, we're done. */
12934 if (cp_parser_parse_definitely (parser))
12937 /* We're still not sure what the argument will be. */
12938 cp_parser_parse_tentatively (parser);
12939 /* Try a template. */
12940 argument_start_token = cp_lexer_peek_token (parser->lexer);
12941 argument = cp_parser_id_expression (parser,
12942 /*template_keyword_p=*/false,
12943 /*check_dependency_p=*/true,
12945 /*declarator_p=*/false,
12946 /*optional_p=*/false);
12947 /* If the next token isn't a `,' or a `>', then this argument wasn't
12948 really finished. */
12949 if (!cp_parser_next_token_ends_template_argument_p (parser))
12950 cp_parser_error (parser, "expected template-argument");
12951 if (!cp_parser_error_occurred (parser))
12953 /* Figure out what is being referred to. If the id-expression
12954 was for a class template specialization, then we will have a
12955 TYPE_DECL at this point. There is no need to do name lookup
12956 at this point in that case. */
12957 if (TREE_CODE (argument) != TYPE_DECL)
12958 argument = cp_parser_lookup_name (parser, argument,
12960 /*is_template=*/template_p,
12961 /*is_namespace=*/false,
12962 /*check_dependency=*/true,
12963 /*ambiguous_decls=*/NULL,
12964 argument_start_token->location);
12965 if (TREE_CODE (argument) != TEMPLATE_DECL
12966 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12967 cp_parser_error (parser, "expected template-name");
12969 if (cp_parser_parse_definitely (parser))
12971 /* It must be a non-type argument. There permitted cases are given
12972 in [temp.arg.nontype]:
12974 -- an integral constant-expression of integral or enumeration
12977 -- the name of a non-type template-parameter; or
12979 -- the name of an object or function with external linkage...
12981 -- the address of an object or function with external linkage...
12983 -- a pointer to member... */
12984 /* Look for a non-type template parameter. */
12985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12987 cp_parser_parse_tentatively (parser);
12988 argument = cp_parser_primary_expression (parser,
12989 /*address_p=*/false,
12991 /*template_arg_p=*/true,
12993 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12994 || !cp_parser_next_token_ends_template_argument_p (parser))
12995 cp_parser_simulate_error (parser);
12996 if (cp_parser_parse_definitely (parser))
13000 /* If the next token is "&", the argument must be the address of an
13001 object or function with external linkage. */
13002 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13005 loc = cp_lexer_peek_token (parser->lexer)->location;
13006 cp_lexer_consume_token (parser->lexer);
13008 /* See if we might have an id-expression. */
13009 token = cp_lexer_peek_token (parser->lexer);
13010 if (token->type == CPP_NAME
13011 || token->keyword == RID_OPERATOR
13012 || token->type == CPP_SCOPE
13013 || token->type == CPP_TEMPLATE_ID
13014 || token->type == CPP_NESTED_NAME_SPECIFIER)
13016 cp_parser_parse_tentatively (parser);
13017 argument = cp_parser_primary_expression (parser,
13020 /*template_arg_p=*/true,
13022 if (cp_parser_error_occurred (parser)
13023 || !cp_parser_next_token_ends_template_argument_p (parser))
13024 cp_parser_abort_tentative_parse (parser);
13029 if (TREE_CODE (argument) == INDIRECT_REF)
13031 gcc_assert (REFERENCE_REF_P (argument));
13032 argument = TREE_OPERAND (argument, 0);
13035 /* If we're in a template, we represent a qualified-id referring
13036 to a static data member as a SCOPE_REF even if the scope isn't
13037 dependent so that we can check access control later. */
13039 if (TREE_CODE (probe) == SCOPE_REF)
13040 probe = TREE_OPERAND (probe, 1);
13041 if (TREE_CODE (probe) == VAR_DECL)
13043 /* A variable without external linkage might still be a
13044 valid constant-expression, so no error is issued here
13045 if the external-linkage check fails. */
13046 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13047 cp_parser_simulate_error (parser);
13049 else if (is_overloaded_fn (argument))
13050 /* All overloaded functions are allowed; if the external
13051 linkage test does not pass, an error will be issued
13055 && (TREE_CODE (argument) == OFFSET_REF
13056 || TREE_CODE (argument) == SCOPE_REF))
13057 /* A pointer-to-member. */
13059 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13062 cp_parser_simulate_error (parser);
13064 if (cp_parser_parse_definitely (parser))
13067 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13068 tf_warning_or_error);
13073 /* If the argument started with "&", there are no other valid
13074 alternatives at this point. */
13077 cp_parser_error (parser, "invalid non-type template argument");
13078 return error_mark_node;
13081 /* If the argument wasn't successfully parsed as a type-id followed
13082 by '>>', the argument can only be a constant expression now.
13083 Otherwise, we try parsing the constant-expression tentatively,
13084 because the argument could really be a type-id. */
13086 cp_parser_parse_tentatively (parser);
13087 argument = cp_parser_constant_expression (parser,
13088 /*allow_non_constant_p=*/false,
13089 /*non_constant_p=*/NULL);
13090 argument = fold_non_dependent_expr (argument);
13091 if (!maybe_type_id)
13093 if (!cp_parser_next_token_ends_template_argument_p (parser))
13094 cp_parser_error (parser, "expected template-argument");
13095 if (cp_parser_parse_definitely (parser))
13097 /* We did our best to parse the argument as a non type-id, but that
13098 was the only alternative that matched (albeit with a '>' after
13099 it). We can assume it's just a typo from the user, and a
13100 diagnostic will then be issued. */
13101 return cp_parser_template_type_arg (parser);
13104 /* Parse an explicit-instantiation.
13106 explicit-instantiation:
13107 template declaration
13109 Although the standard says `declaration', what it really means is:
13111 explicit-instantiation:
13112 template decl-specifier-seq [opt] declarator [opt] ;
13114 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13115 supposed to be allowed. A defect report has been filed about this
13120 explicit-instantiation:
13121 storage-class-specifier template
13122 decl-specifier-seq [opt] declarator [opt] ;
13123 function-specifier template
13124 decl-specifier-seq [opt] declarator [opt] ; */
13127 cp_parser_explicit_instantiation (cp_parser* parser)
13129 int declares_class_or_enum;
13130 cp_decl_specifier_seq decl_specifiers;
13131 tree extension_specifier = NULL_TREE;
13133 timevar_push (TV_TEMPLATE_INST);
13135 /* Look for an (optional) storage-class-specifier or
13136 function-specifier. */
13137 if (cp_parser_allow_gnu_extensions_p (parser))
13139 extension_specifier
13140 = cp_parser_storage_class_specifier_opt (parser);
13141 if (!extension_specifier)
13142 extension_specifier
13143 = cp_parser_function_specifier_opt (parser,
13144 /*decl_specs=*/NULL);
13147 /* Look for the `template' keyword. */
13148 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13149 /* Let the front end know that we are processing an explicit
13151 begin_explicit_instantiation ();
13152 /* [temp.explicit] says that we are supposed to ignore access
13153 control while processing explicit instantiation directives. */
13154 push_deferring_access_checks (dk_no_check);
13155 /* Parse a decl-specifier-seq. */
13156 cp_parser_decl_specifier_seq (parser,
13157 CP_PARSER_FLAGS_OPTIONAL,
13159 &declares_class_or_enum);
13160 /* If there was exactly one decl-specifier, and it declared a class,
13161 and there's no declarator, then we have an explicit type
13163 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13167 type = check_tag_decl (&decl_specifiers);
13168 /* Turn access control back on for names used during
13169 template instantiation. */
13170 pop_deferring_access_checks ();
13172 do_type_instantiation (type, extension_specifier,
13173 /*complain=*/tf_error);
13177 cp_declarator *declarator;
13180 /* Parse the declarator. */
13182 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13183 /*ctor_dtor_or_conv_p=*/NULL,
13184 /*parenthesized_p=*/NULL,
13185 /*member_p=*/false);
13186 if (declares_class_or_enum & 2)
13187 cp_parser_check_for_definition_in_return_type (declarator,
13188 decl_specifiers.type,
13189 decl_specifiers.locations[ds_type_spec]);
13190 if (declarator != cp_error_declarator)
13192 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13193 permerror (decl_specifiers.locations[ds_inline],
13194 "explicit instantiation shall not use"
13195 " %<inline%> specifier");
13196 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13197 permerror (decl_specifiers.locations[ds_constexpr],
13198 "explicit instantiation shall not use"
13199 " %<constexpr%> specifier");
13201 decl = grokdeclarator (declarator, &decl_specifiers,
13202 NORMAL, 0, &decl_specifiers.attributes);
13203 /* Turn access control back on for names used during
13204 template instantiation. */
13205 pop_deferring_access_checks ();
13206 /* Do the explicit instantiation. */
13207 do_decl_instantiation (decl, extension_specifier);
13211 pop_deferring_access_checks ();
13212 /* Skip the body of the explicit instantiation. */
13213 cp_parser_skip_to_end_of_statement (parser);
13216 /* We're done with the instantiation. */
13217 end_explicit_instantiation ();
13219 cp_parser_consume_semicolon_at_end_of_statement (parser);
13221 timevar_pop (TV_TEMPLATE_INST);
13224 /* Parse an explicit-specialization.
13226 explicit-specialization:
13227 template < > declaration
13229 Although the standard says `declaration', what it really means is:
13231 explicit-specialization:
13232 template <> decl-specifier [opt] init-declarator [opt] ;
13233 template <> function-definition
13234 template <> explicit-specialization
13235 template <> template-declaration */
13238 cp_parser_explicit_specialization (cp_parser* parser)
13240 bool need_lang_pop;
13241 cp_token *token = cp_lexer_peek_token (parser->lexer);
13243 /* Look for the `template' keyword. */
13244 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13245 /* Look for the `<'. */
13246 cp_parser_require (parser, CPP_LESS, RT_LESS);
13247 /* Look for the `>'. */
13248 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13249 /* We have processed another parameter list. */
13250 ++parser->num_template_parameter_lists;
13253 A template ... explicit specialization ... shall not have C
13255 if (current_lang_name == lang_name_c)
13257 error_at (token->location, "template specialization with C linkage");
13258 /* Give it C++ linkage to avoid confusing other parts of the
13260 push_lang_context (lang_name_cplusplus);
13261 need_lang_pop = true;
13264 need_lang_pop = false;
13265 /* Let the front end know that we are beginning a specialization. */
13266 if (!begin_specialization ())
13268 end_specialization ();
13272 /* If the next keyword is `template', we need to figure out whether
13273 or not we're looking a template-declaration. */
13274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13276 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13277 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13278 cp_parser_template_declaration_after_export (parser,
13279 /*member_p=*/false);
13281 cp_parser_explicit_specialization (parser);
13284 /* Parse the dependent declaration. */
13285 cp_parser_single_declaration (parser,
13287 /*member_p=*/false,
13288 /*explicit_specialization_p=*/true,
13289 /*friend_p=*/NULL);
13290 /* We're done with the specialization. */
13291 end_specialization ();
13292 /* For the erroneous case of a template with C linkage, we pushed an
13293 implicit C++ linkage scope; exit that scope now. */
13295 pop_lang_context ();
13296 /* We're done with this parameter list. */
13297 --parser->num_template_parameter_lists;
13300 /* Parse a type-specifier.
13303 simple-type-specifier
13306 elaborated-type-specifier
13314 Returns a representation of the type-specifier. For a
13315 class-specifier, enum-specifier, or elaborated-type-specifier, a
13316 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13318 The parser flags FLAGS is used to control type-specifier parsing.
13320 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13321 in a decl-specifier-seq.
13323 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13324 class-specifier, enum-specifier, or elaborated-type-specifier, then
13325 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13326 if a type is declared; 2 if it is defined. Otherwise, it is set to
13329 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13330 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13331 is set to FALSE. */
13334 cp_parser_type_specifier (cp_parser* parser,
13335 cp_parser_flags flags,
13336 cp_decl_specifier_seq *decl_specs,
13337 bool is_declaration,
13338 int* declares_class_or_enum,
13339 bool* is_cv_qualifier)
13341 tree type_spec = NULL_TREE;
13344 cp_decl_spec ds = ds_last;
13346 /* Assume this type-specifier does not declare a new type. */
13347 if (declares_class_or_enum)
13348 *declares_class_or_enum = 0;
13349 /* And that it does not specify a cv-qualifier. */
13350 if (is_cv_qualifier)
13351 *is_cv_qualifier = false;
13352 /* Peek at the next token. */
13353 token = cp_lexer_peek_token (parser->lexer);
13355 /* If we're looking at a keyword, we can use that to guide the
13356 production we choose. */
13357 keyword = token->keyword;
13361 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13362 goto elaborated_type_specifier;
13364 /* Look for the enum-specifier. */
13365 type_spec = cp_parser_enum_specifier (parser);
13366 /* If that worked, we're done. */
13369 if (declares_class_or_enum)
13370 *declares_class_or_enum = 2;
13372 cp_parser_set_decl_spec_type (decl_specs,
13375 /*type_definition_p=*/true);
13379 goto elaborated_type_specifier;
13381 /* Any of these indicate either a class-specifier, or an
13382 elaborated-type-specifier. */
13386 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13387 goto elaborated_type_specifier;
13389 /* Parse tentatively so that we can back up if we don't find a
13390 class-specifier. */
13391 cp_parser_parse_tentatively (parser);
13392 /* Look for the class-specifier. */
13393 type_spec = cp_parser_class_specifier (parser);
13394 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13395 /* If that worked, we're done. */
13396 if (cp_parser_parse_definitely (parser))
13398 if (declares_class_or_enum)
13399 *declares_class_or_enum = 2;
13401 cp_parser_set_decl_spec_type (decl_specs,
13404 /*type_definition_p=*/true);
13408 /* Fall through. */
13409 elaborated_type_specifier:
13410 /* We're declaring (not defining) a class or enum. */
13411 if (declares_class_or_enum)
13412 *declares_class_or_enum = 1;
13414 /* Fall through. */
13416 /* Look for an elaborated-type-specifier. */
13418 = (cp_parser_elaborated_type_specifier
13420 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13423 cp_parser_set_decl_spec_type (decl_specs,
13426 /*type_definition_p=*/false);
13431 if (is_cv_qualifier)
13432 *is_cv_qualifier = true;
13437 if (is_cv_qualifier)
13438 *is_cv_qualifier = true;
13443 if (is_cv_qualifier)
13444 *is_cv_qualifier = true;
13448 /* The `__complex__' keyword is a GNU extension. */
13456 /* Handle simple keywords. */
13461 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
13462 decl_specs->any_specifiers_p = true;
13464 return cp_lexer_consume_token (parser->lexer)->u.value;
13467 /* If we do not already have a type-specifier, assume we are looking
13468 at a simple-type-specifier. */
13469 type_spec = cp_parser_simple_type_specifier (parser,
13473 /* If we didn't find a type-specifier, and a type-specifier was not
13474 optional in this context, issue an error message. */
13475 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13477 cp_parser_error (parser, "expected type specifier");
13478 return error_mark_node;
13484 /* Parse a simple-type-specifier.
13486 simple-type-specifier:
13487 :: [opt] nested-name-specifier [opt] type-name
13488 :: [opt] nested-name-specifier template template-id
13503 simple-type-specifier:
13505 decltype ( expression )
13508 __underlying_type ( type-id )
13512 simple-type-specifier:
13514 __typeof__ unary-expression
13515 __typeof__ ( type-id )
13517 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13518 appropriately updated. */
13521 cp_parser_simple_type_specifier (cp_parser* parser,
13522 cp_decl_specifier_seq *decl_specs,
13523 cp_parser_flags flags)
13525 tree type = NULL_TREE;
13528 /* Peek at the next token. */
13529 token = cp_lexer_peek_token (parser->lexer);
13531 /* If we're looking at a keyword, things are easy. */
13532 switch (token->keyword)
13536 decl_specs->explicit_char_p = true;
13537 type = char_type_node;
13540 type = char16_type_node;
13543 type = char32_type_node;
13546 type = wchar_type_node;
13549 type = boolean_type_node;
13552 set_and_check_decl_spec_loc (decl_specs, ds_short, token->location);
13553 type = short_integer_type_node;
13557 decl_specs->explicit_int_p = true;
13558 type = integer_type_node;
13561 if (!int128_integer_type_node)
13564 decl_specs->explicit_int128_p = true;
13565 type = int128_integer_type_node;
13569 set_and_check_decl_spec_loc (decl_specs, ds_long, token->location);
13570 type = long_integer_type_node;
13573 set_and_check_decl_spec_loc (decl_specs, ds_signed, token->location);
13574 type = integer_type_node;
13577 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token->location);
13578 type = unsigned_type_node;
13581 type = float_type_node;
13584 type = double_type_node;
13587 type = void_type_node;
13591 maybe_warn_cpp0x (CPP0X_AUTO);
13592 type = make_auto ();
13596 /* Since DR 743, decltype can either be a simple-type-specifier by
13597 itself or begin a nested-name-specifier. Parsing it will replace
13598 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13599 handling below decide what to do. */
13600 cp_parser_decltype (parser);
13601 cp_lexer_set_token_position (parser->lexer, token);
13605 /* Consume the `typeof' token. */
13606 cp_lexer_consume_token (parser->lexer);
13607 /* Parse the operand to `typeof'. */
13608 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13609 /* If it is not already a TYPE, take its type. */
13610 if (!TYPE_P (type))
13611 type = finish_typeof (type);
13614 cp_parser_set_decl_spec_type (decl_specs, type,
13616 /*type_definition_p=*/false);
13620 case RID_UNDERLYING_TYPE:
13621 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13623 cp_parser_set_decl_spec_type (decl_specs, type,
13625 /*type_definition_p=*/false);
13630 case RID_DIRECT_BASES:
13631 type = cp_parser_trait_expr (parser, token->keyword);
13633 cp_parser_set_decl_spec_type (decl_specs, type,
13635 /*type_definition_p=*/false);
13641 /* If token is an already-parsed decltype not followed by ::,
13642 it's a simple-type-specifier. */
13643 if (token->type == CPP_DECLTYPE
13644 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13646 type = token->u.value;
13648 cp_parser_set_decl_spec_type (decl_specs, type,
13650 /*type_definition_p=*/false);
13651 cp_lexer_consume_token (parser->lexer);
13655 /* If the type-specifier was for a built-in type, we're done. */
13658 /* Record the type. */
13660 && (token->keyword != RID_SIGNED
13661 && token->keyword != RID_UNSIGNED
13662 && token->keyword != RID_SHORT
13663 && token->keyword != RID_LONG))
13664 cp_parser_set_decl_spec_type (decl_specs,
13667 /*type_definition_p=*/false);
13669 decl_specs->any_specifiers_p = true;
13671 /* Consume the token. */
13672 cp_lexer_consume_token (parser->lexer);
13674 /* There is no valid C++ program where a non-template type is
13675 followed by a "<". That usually indicates that the user thought
13676 that the type was a template. */
13677 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13680 return TYPE_NAME (type);
13683 /* The type-specifier must be a user-defined type. */
13684 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13689 /* Don't gobble tokens or issue error messages if this is an
13690 optional type-specifier. */
13691 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13692 cp_parser_parse_tentatively (parser);
13694 /* Look for the optional `::' operator. */
13696 = (cp_parser_global_scope_opt (parser,
13697 /*current_scope_valid_p=*/false)
13699 /* Look for the nested-name specifier. */
13701 = (cp_parser_nested_name_specifier_opt (parser,
13702 /*typename_keyword_p=*/false,
13703 /*check_dependency_p=*/true,
13705 /*is_declaration=*/false)
13707 token = cp_lexer_peek_token (parser->lexer);
13708 /* If we have seen a nested-name-specifier, and the next token
13709 is `template', then we are using the template-id production. */
13711 && cp_parser_optional_template_keyword (parser))
13713 /* Look for the template-id. */
13714 type = cp_parser_template_id (parser,
13715 /*template_keyword_p=*/true,
13716 /*check_dependency_p=*/true,
13718 /*is_declaration=*/false);
13719 /* If the template-id did not name a type, we are out of
13721 if (TREE_CODE (type) != TYPE_DECL)
13723 cp_parser_error (parser, "expected template-id for type");
13727 /* Otherwise, look for a type-name. */
13729 type = cp_parser_type_name (parser);
13730 /* Keep track of all name-lookups performed in class scopes. */
13734 && TREE_CODE (type) == TYPE_DECL
13735 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13736 maybe_note_name_used_in_class (DECL_NAME (type), type);
13737 /* If it didn't work out, we don't have a TYPE. */
13738 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13739 && !cp_parser_parse_definitely (parser))
13741 if (type && decl_specs)
13742 cp_parser_set_decl_spec_type (decl_specs, type,
13744 /*type_definition_p=*/false);
13747 /* If we didn't get a type-name, issue an error message. */
13748 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13750 cp_parser_error (parser, "expected type-name");
13751 return error_mark_node;
13754 if (type && type != error_mark_node)
13756 /* See if TYPE is an Objective-C type, and if so, parse and
13757 accept any protocol references following it. Do this before
13758 the cp_parser_check_for_invalid_template_id() call, because
13759 Objective-C types can be followed by '<...>' which would
13760 enclose protocol names rather than template arguments, and so
13761 everything is fine. */
13762 if (c_dialect_objc () && !parser->scope
13763 && (objc_is_id (type) || objc_is_class_name (type)))
13765 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13766 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13768 /* Clobber the "unqualified" type previously entered into
13769 DECL_SPECS with the new, improved protocol-qualified version. */
13771 decl_specs->type = qual_type;
13776 /* There is no valid C++ program where a non-template type is
13777 followed by a "<". That usually indicates that the user
13778 thought that the type was a template. */
13779 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13787 /* Parse a type-name.
13793 simple-template-id [in c++0x]
13801 Returns a TYPE_DECL for the type. */
13804 cp_parser_type_name (cp_parser* parser)
13808 /* We can't know yet whether it is a class-name or not. */
13809 cp_parser_parse_tentatively (parser);
13810 /* Try a class-name. */
13811 type_decl = cp_parser_class_name (parser,
13812 /*typename_keyword_p=*/false,
13813 /*template_keyword_p=*/false,
13815 /*check_dependency_p=*/true,
13816 /*class_head_p=*/false,
13817 /*is_declaration=*/false);
13818 /* If it's not a class-name, keep looking. */
13819 if (!cp_parser_parse_definitely (parser))
13821 if (cxx_dialect < cxx0x)
13822 /* It must be a typedef-name or an enum-name. */
13823 return cp_parser_nonclass_name (parser);
13825 cp_parser_parse_tentatively (parser);
13826 /* It is either a simple-template-id representing an
13827 instantiation of an alias template... */
13828 type_decl = cp_parser_template_id (parser,
13829 /*template_keyword_p=*/false,
13830 /*check_dependency_p=*/false,
13832 /*is_declaration=*/false);
13833 /* Note that this must be an instantiation of an alias template
13834 because [temp.names]/6 says:
13836 A template-id that names an alias template specialization
13839 Whereas [temp.names]/7 says:
13841 A simple-template-id that names a class template
13842 specialization is a class-name. */
13843 if (type_decl != NULL_TREE
13844 && TREE_CODE (type_decl) == TYPE_DECL
13845 && TYPE_DECL_ALIAS_P (type_decl))
13846 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13848 cp_parser_simulate_error (parser);
13850 if (!cp_parser_parse_definitely (parser))
13851 /* ... Or a typedef-name or an enum-name. */
13852 return cp_parser_nonclass_name (parser);
13858 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13866 Returns a TYPE_DECL for the type. */
13869 cp_parser_nonclass_name (cp_parser* parser)
13874 cp_token *token = cp_lexer_peek_token (parser->lexer);
13875 identifier = cp_parser_identifier (parser);
13876 if (identifier == error_mark_node)
13877 return error_mark_node;
13879 /* Look up the type-name. */
13880 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13882 if (TREE_CODE (type_decl) == USING_DECL)
13884 if (!DECL_DEPENDENT_P (type_decl))
13885 type_decl = strip_using_decl (type_decl);
13886 else if (USING_DECL_TYPENAME_P (type_decl))
13888 /* We have found a type introduced by a using
13889 declaration at class scope that refers to a dependent
13892 using typename :: [opt] nested-name-specifier unqualified-id ;
13894 type_decl = make_typename_type (TREE_TYPE (type_decl),
13895 DECL_NAME (type_decl),
13896 typename_type, tf_error);
13897 if (type_decl != error_mark_node)
13898 type_decl = TYPE_NAME (type_decl);
13902 if (TREE_CODE (type_decl) != TYPE_DECL
13903 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13905 /* See if this is an Objective-C type. */
13906 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13907 tree type = objc_get_protocol_qualified_type (identifier, protos);
13909 type_decl = TYPE_NAME (type);
13912 /* Issue an error if we did not find a type-name. */
13913 if (TREE_CODE (type_decl) != TYPE_DECL
13914 /* In Objective-C, we have the complication that class names are
13915 normally type names and start declarations (eg, the
13916 "NSObject" in "NSObject *object;"), but can be used in an
13917 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13918 is an expression. So, a classname followed by a dot is not a
13919 valid type-name. */
13920 || (objc_is_class_name (TREE_TYPE (type_decl))
13921 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13923 if (!cp_parser_simulate_error (parser))
13924 cp_parser_name_lookup_error (parser, identifier, type_decl,
13925 NLE_TYPE, token->location);
13926 return error_mark_node;
13928 /* Remember that the name was used in the definition of the
13929 current class so that we can check later to see if the
13930 meaning would have been different after the class was
13931 entirely defined. */
13932 else if (type_decl != error_mark_node
13934 maybe_note_name_used_in_class (identifier, type_decl);
13939 /* Parse an elaborated-type-specifier. Note that the grammar given
13940 here incorporates the resolution to DR68.
13942 elaborated-type-specifier:
13943 class-key :: [opt] nested-name-specifier [opt] identifier
13944 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13945 enum-key :: [opt] nested-name-specifier [opt] identifier
13946 typename :: [opt] nested-name-specifier identifier
13947 typename :: [opt] nested-name-specifier template [opt]
13952 elaborated-type-specifier:
13953 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13954 class-key attributes :: [opt] nested-name-specifier [opt]
13955 template [opt] template-id
13956 enum attributes :: [opt] nested-name-specifier [opt] identifier
13958 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13959 declared `friend'. If IS_DECLARATION is TRUE, then this
13960 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13961 something is being declared.
13963 Returns the TYPE specified. */
13966 cp_parser_elaborated_type_specifier (cp_parser* parser,
13968 bool is_declaration)
13970 enum tag_types tag_type;
13972 tree type = NULL_TREE;
13973 tree attributes = NULL_TREE;
13975 cp_token *token = NULL;
13977 /* See if we're looking at the `enum' keyword. */
13978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13980 /* Consume the `enum' token. */
13981 cp_lexer_consume_token (parser->lexer);
13982 /* Remember that it's an enumeration type. */
13983 tag_type = enum_type;
13984 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13985 enums) is used here. */
13986 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13987 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13989 pedwarn (input_location, 0, "elaborated-type-specifier "
13990 "for a scoped enum must not use the %<%D%> keyword",
13991 cp_lexer_peek_token (parser->lexer)->u.value);
13992 /* Consume the `struct' or `class' and parse it anyway. */
13993 cp_lexer_consume_token (parser->lexer);
13995 /* Parse the attributes. */
13996 attributes = cp_parser_attributes_opt (parser);
13998 /* Or, it might be `typename'. */
13999 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14002 /* Consume the `typename' token. */
14003 cp_lexer_consume_token (parser->lexer);
14004 /* Remember that it's a `typename' type. */
14005 tag_type = typename_type;
14007 /* Otherwise it must be a class-key. */
14010 tag_type = cp_parser_class_key (parser);
14011 if (tag_type == none_type)
14012 return error_mark_node;
14013 /* Parse the attributes. */
14014 attributes = cp_parser_attributes_opt (parser);
14017 /* Look for the `::' operator. */
14018 globalscope = cp_parser_global_scope_opt (parser,
14019 /*current_scope_valid_p=*/false);
14020 /* Look for the nested-name-specifier. */
14021 if (tag_type == typename_type && !globalscope)
14023 if (!cp_parser_nested_name_specifier (parser,
14024 /*typename_keyword_p=*/true,
14025 /*check_dependency_p=*/true,
14028 return error_mark_node;
14031 /* Even though `typename' is not present, the proposed resolution
14032 to Core Issue 180 says that in `class A<T>::B', `B' should be
14033 considered a type-name, even if `A<T>' is dependent. */
14034 cp_parser_nested_name_specifier_opt (parser,
14035 /*typename_keyword_p=*/true,
14036 /*check_dependency_p=*/true,
14039 /* For everything but enumeration types, consider a template-id.
14040 For an enumeration type, consider only a plain identifier. */
14041 if (tag_type != enum_type)
14043 bool template_p = false;
14046 /* Allow the `template' keyword. */
14047 template_p = cp_parser_optional_template_keyword (parser);
14048 /* If we didn't see `template', we don't know if there's a
14049 template-id or not. */
14051 cp_parser_parse_tentatively (parser);
14052 /* Parse the template-id. */
14053 token = cp_lexer_peek_token (parser->lexer);
14054 decl = cp_parser_template_id (parser, template_p,
14055 /*check_dependency_p=*/true,
14058 /* If we didn't find a template-id, look for an ordinary
14060 if (!template_p && !cp_parser_parse_definitely (parser))
14062 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14063 in effect, then we must assume that, upon instantiation, the
14064 template will correspond to a class. */
14065 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14066 && tag_type == typename_type)
14067 type = make_typename_type (parser->scope, decl,
14069 /*complain=*/tf_error);
14070 /* If the `typename' keyword is in effect and DECL is not a type
14071 decl. Then type is non existant. */
14072 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14075 type = check_elaborated_type_specifier (tag_type, decl,
14076 /*allow_template_p=*/true);
14081 token = cp_lexer_peek_token (parser->lexer);
14082 identifier = cp_parser_identifier (parser);
14084 if (identifier == error_mark_node)
14086 parser->scope = NULL_TREE;
14087 return error_mark_node;
14090 /* For a `typename', we needn't call xref_tag. */
14091 if (tag_type == typename_type
14092 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14093 return cp_parser_make_typename_type (parser, parser->scope,
14096 /* Look up a qualified name in the usual way. */
14100 tree ambiguous_decls;
14102 decl = cp_parser_lookup_name (parser, identifier,
14104 /*is_template=*/false,
14105 /*is_namespace=*/false,
14106 /*check_dependency=*/true,
14110 /* If the lookup was ambiguous, an error will already have been
14112 if (ambiguous_decls)
14113 return error_mark_node;
14115 /* If we are parsing friend declaration, DECL may be a
14116 TEMPLATE_DECL tree node here. However, we need to check
14117 whether this TEMPLATE_DECL results in valid code. Consider
14118 the following example:
14121 template <class T> class C {};
14124 template <class T> friend class N::C; // #1, valid code
14126 template <class T> class Y {
14127 friend class N::C; // #2, invalid code
14130 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14131 name lookup of `N::C'. We see that friend declaration must
14132 be template for the code to be valid. Note that
14133 processing_template_decl does not work here since it is
14134 always 1 for the above two cases. */
14136 decl = (cp_parser_maybe_treat_template_as_class
14137 (decl, /*tag_name_p=*/is_friend
14138 && parser->num_template_parameter_lists));
14140 if (TREE_CODE (decl) != TYPE_DECL)
14142 cp_parser_diagnose_invalid_type_name (parser,
14146 return error_mark_node;
14149 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14151 bool allow_template = (parser->num_template_parameter_lists
14152 || DECL_SELF_REFERENCE_P (decl));
14153 type = check_elaborated_type_specifier (tag_type, decl,
14156 if (type == error_mark_node)
14157 return error_mark_node;
14160 /* Forward declarations of nested types, such as
14165 are invalid unless all components preceding the final '::'
14166 are complete. If all enclosing types are complete, these
14167 declarations become merely pointless.
14169 Invalid forward declarations of nested types are errors
14170 caught elsewhere in parsing. Those that are pointless arrive
14173 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14174 && !is_friend && !processing_explicit_instantiation)
14175 warning (0, "declaration %qD does not declare anything", decl);
14177 type = TREE_TYPE (decl);
14181 /* An elaborated-type-specifier sometimes introduces a new type and
14182 sometimes names an existing type. Normally, the rule is that it
14183 introduces a new type only if there is not an existing type of
14184 the same name already in scope. For example, given:
14187 void f() { struct S s; }
14189 the `struct S' in the body of `f' is the same `struct S' as in
14190 the global scope; the existing definition is used. However, if
14191 there were no global declaration, this would introduce a new
14192 local class named `S'.
14194 An exception to this rule applies to the following code:
14196 namespace N { struct S; }
14198 Here, the elaborated-type-specifier names a new type
14199 unconditionally; even if there is already an `S' in the
14200 containing scope this declaration names a new type.
14201 This exception only applies if the elaborated-type-specifier
14202 forms the complete declaration:
14206 A declaration consisting solely of `class-key identifier ;' is
14207 either a redeclaration of the name in the current scope or a
14208 forward declaration of the identifier as a class name. It
14209 introduces the name into the current scope.
14211 We are in this situation precisely when the next token is a `;'.
14213 An exception to the exception is that a `friend' declaration does
14214 *not* name a new type; i.e., given:
14216 struct S { friend struct T; };
14218 `T' is not a new type in the scope of `S'.
14220 Also, `new struct S' or `sizeof (struct S)' never results in the
14221 definition of a new type; a new type can only be declared in a
14222 declaration context. */
14228 /* Friends have special name lookup rules. */
14229 ts = ts_within_enclosing_non_class;
14230 else if (is_declaration
14231 && cp_lexer_next_token_is (parser->lexer,
14233 /* This is a `class-key identifier ;' */
14239 (parser->num_template_parameter_lists
14240 && (cp_parser_next_token_starts_class_definition_p (parser)
14241 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14242 /* An unqualified name was used to reference this type, so
14243 there were no qualifying templates. */
14244 if (!cp_parser_check_template_parameters (parser,
14245 /*num_templates=*/0,
14247 /*declarator=*/NULL))
14248 return error_mark_node;
14249 type = xref_tag (tag_type, identifier, ts, template_p);
14253 if (type == error_mark_node)
14254 return error_mark_node;
14256 /* Allow attributes on forward declarations of classes. */
14259 if (TREE_CODE (type) == TYPENAME_TYPE)
14260 warning (OPT_Wattributes,
14261 "attributes ignored on uninstantiated type");
14262 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14263 && ! processing_explicit_instantiation)
14264 warning (OPT_Wattributes,
14265 "attributes ignored on template instantiation");
14266 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14267 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14269 warning (OPT_Wattributes,
14270 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14273 if (tag_type != enum_type)
14275 /* Indicate whether this class was declared as a `class' or as a
14277 if (TREE_CODE (type) == RECORD_TYPE)
14278 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14279 cp_parser_check_class_key (tag_type, type);
14282 /* A "<" cannot follow an elaborated type specifier. If that
14283 happens, the user was probably trying to form a template-id. */
14284 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14290 /* Parse an enum-specifier.
14293 enum-head { enumerator-list [opt] }
14294 enum-head { enumerator-list , } [C++0x]
14297 enum-key identifier [opt] enum-base [opt]
14298 enum-key nested-name-specifier identifier enum-base [opt]
14303 enum struct [C++0x]
14306 : type-specifier-seq
14308 opaque-enum-specifier:
14309 enum-key identifier enum-base [opt] ;
14312 enum-key attributes[opt] identifier [opt] enum-base [opt]
14313 { enumerator-list [opt] }attributes[opt]
14314 enum-key attributes[opt] identifier [opt] enum-base [opt]
14315 { enumerator-list, }attributes[opt] [C++0x]
14317 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14318 if the token stream isn't an enum-specifier after all. */
14321 cp_parser_enum_specifier (cp_parser* parser)
14324 tree type = NULL_TREE;
14326 tree nested_name_specifier = NULL_TREE;
14328 bool scoped_enum_p = false;
14329 bool has_underlying_type = false;
14330 bool nested_being_defined = false;
14331 bool new_value_list = false;
14332 bool is_new_type = false;
14333 bool is_anonymous = false;
14334 tree underlying_type = NULL_TREE;
14335 cp_token *type_start_token = NULL;
14336 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14338 parser->colon_corrects_to_scope_p = false;
14340 /* Parse tentatively so that we can back up if we don't find a
14342 cp_parser_parse_tentatively (parser);
14344 /* Caller guarantees that the current token is 'enum', an identifier
14345 possibly follows, and the token after that is an opening brace.
14346 If we don't have an identifier, fabricate an anonymous name for
14347 the enumeration being defined. */
14348 cp_lexer_consume_token (parser->lexer);
14350 /* Parse the "class" or "struct", which indicates a scoped
14351 enumeration type in C++0x. */
14352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14353 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14355 if (cxx_dialect < cxx0x)
14356 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14358 /* Consume the `struct' or `class' token. */
14359 cp_lexer_consume_token (parser->lexer);
14361 scoped_enum_p = true;
14364 attributes = cp_parser_attributes_opt (parser);
14366 /* Clear the qualification. */
14367 parser->scope = NULL_TREE;
14368 parser->qualifying_scope = NULL_TREE;
14369 parser->object_scope = NULL_TREE;
14371 /* Figure out in what scope the declaration is being placed. */
14372 prev_scope = current_scope ();
14374 type_start_token = cp_lexer_peek_token (parser->lexer);
14376 push_deferring_access_checks (dk_no_check);
14377 nested_name_specifier
14378 = cp_parser_nested_name_specifier_opt (parser,
14379 /*typename_keyword_p=*/true,
14380 /*check_dependency_p=*/false,
14382 /*is_declaration=*/false);
14384 if (nested_name_specifier)
14388 identifier = cp_parser_identifier (parser);
14389 name = cp_parser_lookup_name (parser, identifier,
14391 /*is_template=*/false,
14392 /*is_namespace=*/false,
14393 /*check_dependency=*/true,
14394 /*ambiguous_decls=*/NULL,
14398 type = TREE_TYPE (name);
14399 if (TREE_CODE (type) == TYPENAME_TYPE)
14401 /* Are template enums allowed in ISO? */
14402 if (template_parm_scope_p ())
14403 pedwarn (type_start_token->location, OPT_Wpedantic,
14404 "%qD is an enumeration template", name);
14405 /* ignore a typename reference, for it will be solved by name
14411 error_at (type_start_token->location,
14412 "%qD is not an enumerator-name", identifier);
14416 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14417 identifier = cp_parser_identifier (parser);
14420 identifier = make_anon_name ();
14421 is_anonymous = true;
14424 pop_deferring_access_checks ();
14426 /* Check for the `:' that denotes a specified underlying type in C++0x.
14427 Note that a ':' could also indicate a bitfield width, however. */
14428 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14430 cp_decl_specifier_seq type_specifiers;
14432 /* Consume the `:'. */
14433 cp_lexer_consume_token (parser->lexer);
14435 /* Parse the type-specifier-seq. */
14436 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14437 /*is_trailing_return=*/false,
14440 /* At this point this is surely not elaborated type specifier. */
14441 if (!cp_parser_parse_definitely (parser))
14444 if (cxx_dialect < cxx0x)
14445 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14447 has_underlying_type = true;
14449 /* If that didn't work, stop. */
14450 if (type_specifiers.type != error_mark_node)
14452 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14453 /*initialized=*/0, NULL);
14454 if (underlying_type == error_mark_node)
14455 underlying_type = NULL_TREE;
14459 /* Look for the `{' but don't consume it yet. */
14460 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14462 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14464 cp_parser_error (parser, "expected %<{%>");
14465 if (has_underlying_type)
14471 /* An opaque-enum-specifier must have a ';' here. */
14472 if ((scoped_enum_p || underlying_type)
14473 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14475 cp_parser_error (parser, "expected %<;%> or %<{%>");
14476 if (has_underlying_type)
14484 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14487 if (nested_name_specifier)
14489 if (CLASS_TYPE_P (nested_name_specifier))
14491 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14492 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14493 push_scope (nested_name_specifier);
14495 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14497 push_nested_namespace (nested_name_specifier);
14501 /* Issue an error message if type-definitions are forbidden here. */
14502 if (!cp_parser_check_type_definition (parser))
14503 type = error_mark_node;
14505 /* Create the new type. We do this before consuming the opening
14506 brace so the enum will be recorded as being on the line of its
14507 tag (or the 'enum' keyword, if there is no tag). */
14508 type = start_enum (identifier, type, underlying_type,
14509 scoped_enum_p, &is_new_type);
14511 /* If the next token is not '{' it is an opaque-enum-specifier or an
14512 elaborated-type-specifier. */
14513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14515 timevar_push (TV_PARSE_ENUM);
14516 if (nested_name_specifier)
14518 /* The following catches invalid code such as:
14519 enum class S<int>::E { A, B, C }; */
14520 if (!processing_specialization
14521 && CLASS_TYPE_P (nested_name_specifier)
14522 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14523 error_at (type_start_token->location, "cannot add an enumerator "
14524 "list to a template instantiation");
14526 /* If that scope does not contain the scope in which the
14527 class was originally declared, the program is invalid. */
14528 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14530 if (at_namespace_scope_p ())
14531 error_at (type_start_token->location,
14532 "declaration of %qD in namespace %qD which does not "
14534 type, prev_scope, nested_name_specifier);
14536 error_at (type_start_token->location,
14537 "declaration of %qD in %qD which does not enclose %qD",
14538 type, prev_scope, nested_name_specifier);
14539 type = error_mark_node;
14544 begin_scope (sk_scoped_enum, type);
14546 /* Consume the opening brace. */
14547 cp_lexer_consume_token (parser->lexer);
14549 if (type == error_mark_node)
14550 ; /* Nothing to add */
14551 else if (OPAQUE_ENUM_P (type)
14552 || (cxx_dialect > cxx98 && processing_specialization))
14554 new_value_list = true;
14555 SET_OPAQUE_ENUM_P (type, false);
14556 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14560 error_at (type_start_token->location, "multiple definition of %q#T", type);
14561 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14562 "previous definition here");
14563 type = error_mark_node;
14566 if (type == error_mark_node)
14567 cp_parser_skip_to_end_of_block_or_statement (parser);
14568 /* If the next token is not '}', then there are some enumerators. */
14569 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14570 cp_parser_enumerator_list (parser, type);
14572 /* Consume the final '}'. */
14573 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14577 timevar_pop (TV_PARSE_ENUM);
14581 /* If a ';' follows, then it is an opaque-enum-specifier
14582 and additional restrictions apply. */
14583 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14586 error_at (type_start_token->location,
14587 "opaque-enum-specifier without name");
14588 else if (nested_name_specifier)
14589 error_at (type_start_token->location,
14590 "opaque-enum-specifier must use a simple identifier");
14594 /* Look for trailing attributes to apply to this enumeration, and
14595 apply them if appropriate. */
14596 if (cp_parser_allow_gnu_extensions_p (parser))
14598 tree trailing_attr = cp_parser_attributes_opt (parser);
14599 trailing_attr = chainon (trailing_attr, attributes);
14600 cplus_decl_attributes (&type,
14602 (int) ATTR_FLAG_TYPE_IN_PLACE);
14605 /* Finish up the enumeration. */
14606 if (type != error_mark_node)
14608 if (new_value_list)
14609 finish_enum_value_list (type);
14611 finish_enum (type);
14614 if (nested_name_specifier)
14616 if (CLASS_TYPE_P (nested_name_specifier))
14618 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14619 pop_scope (nested_name_specifier);
14621 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14623 pop_nested_namespace (nested_name_specifier);
14627 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14631 /* Parse an enumerator-list. The enumerators all have the indicated
14635 enumerator-definition
14636 enumerator-list , enumerator-definition */
14639 cp_parser_enumerator_list (cp_parser* parser, tree type)
14643 /* Parse an enumerator-definition. */
14644 cp_parser_enumerator_definition (parser, type);
14646 /* If the next token is not a ',', we've reached the end of
14648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14650 /* Otherwise, consume the `,' and keep going. */
14651 cp_lexer_consume_token (parser->lexer);
14652 /* If the next token is a `}', there is a trailing comma. */
14653 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14655 if (cxx_dialect < cxx0x && !in_system_header)
14656 pedwarn (input_location, OPT_Wpedantic,
14657 "comma at end of enumerator list");
14663 /* Parse an enumerator-definition. The enumerator has the indicated
14666 enumerator-definition:
14668 enumerator = constant-expression
14674 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14680 /* Save the input location because we are interested in the location
14681 of the identifier and not the location of the explicit value. */
14682 loc = cp_lexer_peek_token (parser->lexer)->location;
14684 /* Look for the identifier. */
14685 identifier = cp_parser_identifier (parser);
14686 if (identifier == error_mark_node)
14689 /* If the next token is an '=', then there is an explicit value. */
14690 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14692 /* Consume the `=' token. */
14693 cp_lexer_consume_token (parser->lexer);
14694 /* Parse the value. */
14695 value = cp_parser_constant_expression (parser,
14696 /*allow_non_constant_p=*/false,
14702 /* If we are processing a template, make sure the initializer of the
14703 enumerator doesn't contain any bare template parameter pack. */
14704 if (check_for_bare_parameter_packs (value))
14705 value = error_mark_node;
14707 /* integral_constant_value will pull out this expression, so make sure
14708 it's folded as appropriate. */
14709 value = fold_non_dependent_expr (value);
14711 /* Create the enumerator. */
14712 build_enumerator (identifier, value, type, loc);
14715 /* Parse a namespace-name.
14718 original-namespace-name
14721 Returns the NAMESPACE_DECL for the namespace. */
14724 cp_parser_namespace_name (cp_parser* parser)
14727 tree namespace_decl;
14729 cp_token *token = cp_lexer_peek_token (parser->lexer);
14731 /* Get the name of the namespace. */
14732 identifier = cp_parser_identifier (parser);
14733 if (identifier == error_mark_node)
14734 return error_mark_node;
14736 /* Look up the identifier in the currently active scope. Look only
14737 for namespaces, due to:
14739 [basic.lookup.udir]
14741 When looking up a namespace-name in a using-directive or alias
14742 definition, only namespace names are considered.
14746 [basic.lookup.qual]
14748 During the lookup of a name preceding the :: scope resolution
14749 operator, object, function, and enumerator names are ignored.
14751 (Note that cp_parser_qualifying_entity only calls this
14752 function if the token after the name is the scope resolution
14754 namespace_decl = cp_parser_lookup_name (parser, identifier,
14756 /*is_template=*/false,
14757 /*is_namespace=*/true,
14758 /*check_dependency=*/true,
14759 /*ambiguous_decls=*/NULL,
14761 /* If it's not a namespace, issue an error. */
14762 if (namespace_decl == error_mark_node
14763 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14765 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14766 error_at (token->location, "%qD is not a namespace-name", identifier);
14767 cp_parser_error (parser, "expected namespace-name");
14768 namespace_decl = error_mark_node;
14771 return namespace_decl;
14774 /* Parse a namespace-definition.
14776 namespace-definition:
14777 named-namespace-definition
14778 unnamed-namespace-definition
14780 named-namespace-definition:
14781 original-namespace-definition
14782 extension-namespace-definition
14784 original-namespace-definition:
14785 namespace identifier { namespace-body }
14787 extension-namespace-definition:
14788 namespace original-namespace-name { namespace-body }
14790 unnamed-namespace-definition:
14791 namespace { namespace-body } */
14794 cp_parser_namespace_definition (cp_parser* parser)
14796 tree identifier, attribs;
14797 bool has_visibility;
14800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14802 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14804 cp_lexer_consume_token (parser->lexer);
14809 /* Look for the `namespace' keyword. */
14810 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14812 /* Get the name of the namespace. We do not attempt to distinguish
14813 between an original-namespace-definition and an
14814 extension-namespace-definition at this point. The semantic
14815 analysis routines are responsible for that. */
14816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14817 identifier = cp_parser_identifier (parser);
14819 identifier = NULL_TREE;
14821 /* Parse any specified attributes. */
14822 attribs = cp_parser_attributes_opt (parser);
14824 /* Look for the `{' to start the namespace. */
14825 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14826 /* Start the namespace. */
14827 push_namespace (identifier);
14829 /* "inline namespace" is equivalent to a stub namespace definition
14830 followed by a strong using directive. */
14833 tree name_space = current_namespace;
14834 /* Set up namespace association. */
14835 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14836 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14837 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14838 /* Import the contents of the inline namespace. */
14840 do_using_directive (name_space);
14841 push_namespace (identifier);
14844 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14846 /* Parse the body of the namespace. */
14847 cp_parser_namespace_body (parser);
14849 if (has_visibility)
14850 pop_visibility (1);
14852 /* Finish the namespace. */
14854 /* Look for the final `}'. */
14855 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14858 /* Parse a namespace-body.
14861 declaration-seq [opt] */
14864 cp_parser_namespace_body (cp_parser* parser)
14866 cp_parser_declaration_seq_opt (parser);
14869 /* Parse a namespace-alias-definition.
14871 namespace-alias-definition:
14872 namespace identifier = qualified-namespace-specifier ; */
14875 cp_parser_namespace_alias_definition (cp_parser* parser)
14878 tree namespace_specifier;
14880 cp_token *token = cp_lexer_peek_token (parser->lexer);
14882 /* Look for the `namespace' keyword. */
14883 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14884 /* Look for the identifier. */
14885 identifier = cp_parser_identifier (parser);
14886 if (identifier == error_mark_node)
14888 /* Look for the `=' token. */
14889 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14890 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14892 error_at (token->location, "%<namespace%> definition is not allowed here");
14893 /* Skip the definition. */
14894 cp_lexer_consume_token (parser->lexer);
14895 if (cp_parser_skip_to_closing_brace (parser))
14896 cp_lexer_consume_token (parser->lexer);
14899 cp_parser_require (parser, CPP_EQ, RT_EQ);
14900 /* Look for the qualified-namespace-specifier. */
14901 namespace_specifier
14902 = cp_parser_qualified_namespace_specifier (parser);
14903 /* Look for the `;' token. */
14904 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14906 /* Register the alias in the symbol table. */
14907 do_namespace_alias (identifier, namespace_specifier);
14910 /* Parse a qualified-namespace-specifier.
14912 qualified-namespace-specifier:
14913 :: [opt] nested-name-specifier [opt] namespace-name
14915 Returns a NAMESPACE_DECL corresponding to the specified
14919 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14921 /* Look for the optional `::'. */
14922 cp_parser_global_scope_opt (parser,
14923 /*current_scope_valid_p=*/false);
14925 /* Look for the optional nested-name-specifier. */
14926 cp_parser_nested_name_specifier_opt (parser,
14927 /*typename_keyword_p=*/false,
14928 /*check_dependency_p=*/true,
14930 /*is_declaration=*/true);
14932 return cp_parser_namespace_name (parser);
14935 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14936 access declaration.
14939 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14940 using :: unqualified-id ;
14942 access-declaration:
14948 cp_parser_using_declaration (cp_parser* parser,
14949 bool access_declaration_p)
14952 bool typename_p = false;
14953 bool global_scope_p;
14957 int oldcount = errorcount;
14958 cp_token *diag_token = NULL;
14960 if (access_declaration_p)
14962 diag_token = cp_lexer_peek_token (parser->lexer);
14963 cp_parser_parse_tentatively (parser);
14967 /* Look for the `using' keyword. */
14968 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14970 /* Peek at the next token. */
14971 token = cp_lexer_peek_token (parser->lexer);
14972 /* See if it's `typename'. */
14973 if (token->keyword == RID_TYPENAME)
14975 /* Remember that we've seen it. */
14977 /* Consume the `typename' token. */
14978 cp_lexer_consume_token (parser->lexer);
14982 /* Look for the optional global scope qualification. */
14984 = (cp_parser_global_scope_opt (parser,
14985 /*current_scope_valid_p=*/false)
14988 /* If we saw `typename', or didn't see `::', then there must be a
14989 nested-name-specifier present. */
14990 if (typename_p || !global_scope_p)
14991 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14992 /*check_dependency_p=*/true,
14994 /*is_declaration=*/true);
14995 /* Otherwise, we could be in either of the two productions. In that
14996 case, treat the nested-name-specifier as optional. */
14998 qscope = cp_parser_nested_name_specifier_opt (parser,
14999 /*typename_keyword_p=*/false,
15000 /*check_dependency_p=*/true,
15002 /*is_declaration=*/true);
15004 qscope = global_namespace;
15006 if (access_declaration_p && cp_parser_error_occurred (parser))
15007 /* Something has already gone wrong; there's no need to parse
15008 further. Since an error has occurred, the return value of
15009 cp_parser_parse_definitely will be false, as required. */
15010 return cp_parser_parse_definitely (parser);
15012 token = cp_lexer_peek_token (parser->lexer);
15013 /* Parse the unqualified-id. */
15014 identifier = cp_parser_unqualified_id (parser,
15015 /*template_keyword_p=*/false,
15016 /*check_dependency_p=*/true,
15017 /*declarator_p=*/true,
15018 /*optional_p=*/false);
15020 if (access_declaration_p)
15022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15023 cp_parser_simulate_error (parser);
15024 if (!cp_parser_parse_definitely (parser))
15028 /* The function we call to handle a using-declaration is different
15029 depending on what scope we are in. */
15030 if (qscope == error_mark_node || identifier == error_mark_node)
15032 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15033 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15034 /* [namespace.udecl]
15036 A using declaration shall not name a template-id. */
15037 error_at (token->location,
15038 "a template-id may not appear in a using-declaration");
15041 if (at_class_scope_p ())
15043 /* Create the USING_DECL. */
15044 decl = do_class_using_decl (parser->scope, identifier);
15046 if (decl && typename_p)
15047 USING_DECL_TYPENAME_P (decl) = 1;
15049 if (check_for_bare_parameter_packs (decl))
15052 /* Add it to the list of members in this class. */
15053 finish_member_declaration (decl);
15057 decl = cp_parser_lookup_name_simple (parser,
15060 if (decl == error_mark_node)
15061 cp_parser_name_lookup_error (parser, identifier,
15064 else if (check_for_bare_parameter_packs (decl))
15066 else if (!at_namespace_scope_p ())
15067 do_local_using_decl (decl, qscope, identifier);
15069 do_toplevel_using_decl (decl, qscope, identifier);
15073 /* Look for the final `;'. */
15074 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15076 if (access_declaration_p && errorcount == oldcount)
15077 warning_at (diag_token->location, OPT_Wdeprecated,
15078 "access declarations are deprecated "
15079 "in favour of using-declarations; "
15080 "suggestion: add the %<using%> keyword");
15085 /* Parse an alias-declaration.
15088 using identifier attribute-specifier-seq [opt] = type-id */
15091 cp_parser_alias_declaration (cp_parser* parser)
15093 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15094 location_t id_location, using_location, attrs_location = 0;
15095 cp_declarator *declarator;
15096 cp_decl_specifier_seq decl_specs;
15098 const char *saved_message = NULL;
15100 /* Look for the `using' keyword. */
15101 using_location = cp_lexer_peek_token (parser->lexer)->location;
15102 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15103 id_location = cp_lexer_peek_token (parser->lexer)->location;
15104 id = cp_parser_identifier (parser);
15105 if (id == error_mark_node)
15106 return error_mark_node;
15108 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
15109 attributes = cp_parser_attributes_opt (parser);
15110 if (attributes == error_mark_node)
15111 return error_mark_node;
15113 cp_parser_require (parser, CPP_EQ, RT_EQ);
15115 if (cp_parser_error_occurred (parser))
15116 return error_mark_node;
15118 /* Now we are going to parse the type-id of the declaration. */
15123 "A type-specifier-seq shall not define a class or enumeration
15124 unless it appears in the type-id of an alias-declaration (7.1.3) that
15125 is not the declaration of a template-declaration."
15127 In other words, if we currently are in an alias template, the
15128 type-id should not define a type.
15130 So let's set parser->type_definition_forbidden_message in that
15131 case; cp_parser_check_type_definition (called by
15132 cp_parser_class_specifier) will then emit an error if a type is
15133 defined in the type-id. */
15134 if (parser->num_template_parameter_lists)
15136 saved_message = parser->type_definition_forbidden_message;
15137 parser->type_definition_forbidden_message =
15138 G_("types may not be defined in alias template declarations");
15141 type = cp_parser_type_id (parser);
15143 /* Restore the error message if need be. */
15144 if (parser->num_template_parameter_lists)
15145 parser->type_definition_forbidden_message = saved_message;
15147 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15149 if (cp_parser_error_occurred (parser))
15150 return error_mark_node;
15152 /* A typedef-name can also be introduced by an alias-declaration. The
15153 identifier following the using keyword becomes a typedef-name. It has
15154 the same semantics as if it were introduced by the typedef
15155 specifier. In particular, it does not define a new type and it shall
15156 not appear in the type-id. */
15158 clear_decl_specs (&decl_specs);
15159 decl_specs.type = type;
15160 if (attributes != NULL_TREE)
15162 decl_specs.attributes = attributes;
15163 set_and_check_decl_spec_loc (&decl_specs,
15167 set_and_check_decl_spec_loc (&decl_specs,
15170 set_and_check_decl_spec_loc (&decl_specs,
15174 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15175 declarator->id_loc = id_location;
15177 member_p = at_class_scope_p ();
15179 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15180 NULL_TREE, attributes);
15182 decl = start_decl (declarator, &decl_specs, 0,
15183 attributes, NULL_TREE, &pushed_scope);
15184 if (decl == error_mark_node)
15187 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15190 pop_scope (pushed_scope);
15192 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15193 added into the symbol table; otherwise, return the TYPE_DECL. */
15194 if (DECL_LANG_SPECIFIC (decl)
15195 && DECL_TEMPLATE_INFO (decl)
15196 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15198 decl = DECL_TI_TEMPLATE (decl);
15200 check_member_template (decl);
15206 /* Parse a using-directive.
15209 using namespace :: [opt] nested-name-specifier [opt]
15210 namespace-name ; */
15213 cp_parser_using_directive (cp_parser* parser)
15215 tree namespace_decl;
15218 /* Look for the `using' keyword. */
15219 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15220 /* And the `namespace' keyword. */
15221 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15222 /* Look for the optional `::' operator. */
15223 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15224 /* And the optional nested-name-specifier. */
15225 cp_parser_nested_name_specifier_opt (parser,
15226 /*typename_keyword_p=*/false,
15227 /*check_dependency_p=*/true,
15229 /*is_declaration=*/true);
15230 /* Get the namespace being used. */
15231 namespace_decl = cp_parser_namespace_name (parser);
15232 /* And any specified attributes. */
15233 attribs = cp_parser_attributes_opt (parser);
15234 /* Update the symbol table. */
15235 parse_using_directive (namespace_decl, attribs);
15236 /* Look for the final `;'. */
15237 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15240 /* Parse an asm-definition.
15243 asm ( string-literal ) ;
15248 asm volatile [opt] ( string-literal ) ;
15249 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15250 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15251 : asm-operand-list [opt] ) ;
15252 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15253 : asm-operand-list [opt]
15254 : asm-clobber-list [opt] ) ;
15255 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15256 : asm-clobber-list [opt]
15257 : asm-goto-list ) ; */
15260 cp_parser_asm_definition (cp_parser* parser)
15263 tree outputs = NULL_TREE;
15264 tree inputs = NULL_TREE;
15265 tree clobbers = NULL_TREE;
15266 tree labels = NULL_TREE;
15268 bool volatile_p = false;
15269 bool extended_p = false;
15270 bool invalid_inputs_p = false;
15271 bool invalid_outputs_p = false;
15272 bool goto_p = false;
15273 required_token missing = RT_NONE;
15275 /* Look for the `asm' keyword. */
15276 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15277 /* See if the next token is `volatile'. */
15278 if (cp_parser_allow_gnu_extensions_p (parser)
15279 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15281 /* Remember that we saw the `volatile' keyword. */
15283 /* Consume the token. */
15284 cp_lexer_consume_token (parser->lexer);
15286 if (cp_parser_allow_gnu_extensions_p (parser)
15287 && parser->in_function_body
15288 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15290 /* Remember that we saw the `goto' keyword. */
15292 /* Consume the token. */
15293 cp_lexer_consume_token (parser->lexer);
15295 /* Look for the opening `('. */
15296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15298 /* Look for the string. */
15299 string = cp_parser_string_literal (parser, false, false);
15300 if (string == error_mark_node)
15302 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15303 /*consume_paren=*/true);
15307 /* If we're allowing GNU extensions, check for the extended assembly
15308 syntax. Unfortunately, the `:' tokens need not be separated by
15309 a space in C, and so, for compatibility, we tolerate that here
15310 too. Doing that means that we have to treat the `::' operator as
15312 if (cp_parser_allow_gnu_extensions_p (parser)
15313 && parser->in_function_body
15314 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15315 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15317 bool inputs_p = false;
15318 bool clobbers_p = false;
15319 bool labels_p = false;
15321 /* The extended syntax was used. */
15324 /* Look for outputs. */
15325 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15327 /* Consume the `:'. */
15328 cp_lexer_consume_token (parser->lexer);
15329 /* Parse the output-operands. */
15330 if (cp_lexer_next_token_is_not (parser->lexer,
15332 && cp_lexer_next_token_is_not (parser->lexer,
15334 && cp_lexer_next_token_is_not (parser->lexer,
15337 outputs = cp_parser_asm_operand_list (parser);
15339 if (outputs == error_mark_node)
15340 invalid_outputs_p = true;
15342 /* If the next token is `::', there are no outputs, and the
15343 next token is the beginning of the inputs. */
15344 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15345 /* The inputs are coming next. */
15348 /* Look for inputs. */
15350 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15352 /* Consume the `:' or `::'. */
15353 cp_lexer_consume_token (parser->lexer);
15354 /* Parse the output-operands. */
15355 if (cp_lexer_next_token_is_not (parser->lexer,
15357 && cp_lexer_next_token_is_not (parser->lexer,
15359 && cp_lexer_next_token_is_not (parser->lexer,
15361 inputs = cp_parser_asm_operand_list (parser);
15363 if (inputs == error_mark_node)
15364 invalid_inputs_p = true;
15366 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15367 /* The clobbers are coming next. */
15370 /* Look for clobbers. */
15372 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15375 /* Consume the `:' or `::'. */
15376 cp_lexer_consume_token (parser->lexer);
15377 /* Parse the clobbers. */
15378 if (cp_lexer_next_token_is_not (parser->lexer,
15380 && cp_lexer_next_token_is_not (parser->lexer,
15382 clobbers = cp_parser_asm_clobber_list (parser);
15385 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15386 /* The labels are coming next. */
15389 /* Look for labels. */
15391 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15394 /* Consume the `:' or `::'. */
15395 cp_lexer_consume_token (parser->lexer);
15396 /* Parse the labels. */
15397 labels = cp_parser_asm_label_list (parser);
15400 if (goto_p && !labels_p)
15401 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15404 missing = RT_COLON_SCOPE;
15406 /* Look for the closing `)'. */
15407 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15408 missing ? missing : RT_CLOSE_PAREN))
15409 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15410 /*consume_paren=*/true);
15411 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15413 if (!invalid_inputs_p && !invalid_outputs_p)
15415 /* Create the ASM_EXPR. */
15416 if (parser->in_function_body)
15418 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15419 inputs, clobbers, labels);
15420 /* If the extended syntax was not used, mark the ASM_EXPR. */
15423 tree temp = asm_stmt;
15424 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15425 temp = TREE_OPERAND (temp, 0);
15427 ASM_INPUT_P (temp) = 1;
15431 add_asm_node (string);
15435 /* Declarators [gram.dcl.decl] */
15437 /* Parse an init-declarator.
15440 declarator initializer [opt]
15445 declarator asm-specification [opt] attributes [opt] initializer [opt]
15447 function-definition:
15448 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15450 decl-specifier-seq [opt] declarator function-try-block
15454 function-definition:
15455 __extension__ function-definition
15459 function-definition:
15460 decl-specifier-seq [opt] declarator function-transaction-block
15462 The DECL_SPECIFIERS apply to this declarator. Returns a
15463 representation of the entity declared. If MEMBER_P is TRUE, then
15464 this declarator appears in a class scope. The new DECL created by
15465 this declarator is returned.
15467 The CHECKS are access checks that should be performed once we know
15468 what entity is being declared (and, therefore, what classes have
15471 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15472 for a function-definition here as well. If the declarator is a
15473 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15474 be TRUE upon return. By that point, the function-definition will
15475 have been completely parsed.
15477 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15480 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15481 parsed declaration if it is an uninitialized single declarator not followed
15482 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15483 if present, will not be consumed. If returned, this declarator will be
15484 created with SD_INITIALIZED but will not call cp_finish_decl. */
15487 cp_parser_init_declarator (cp_parser* parser,
15488 cp_decl_specifier_seq *decl_specifiers,
15489 VEC (deferred_access_check,gc)* checks,
15490 bool function_definition_allowed_p,
15492 int declares_class_or_enum,
15493 bool* function_definition_p,
15494 tree* maybe_range_for_decl)
15496 cp_token *token = NULL, *asm_spec_start_token = NULL,
15497 *attributes_start_token = NULL;
15498 cp_declarator *declarator;
15499 tree prefix_attributes;
15501 tree asm_specification;
15503 tree decl = NULL_TREE;
15505 int is_initialized;
15506 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15507 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15509 enum cpp_ttype initialization_kind;
15510 bool is_direct_init = false;
15511 bool is_non_constant_init;
15512 int ctor_dtor_or_conv_p;
15514 tree pushed_scope = NULL_TREE;
15515 bool range_for_decl_p = false;
15517 /* Gather the attributes that were provided with the
15518 decl-specifiers. */
15519 prefix_attributes = decl_specifiers->attributes;
15521 /* Assume that this is not the declarator for a function
15523 if (function_definition_p)
15524 *function_definition_p = false;
15526 /* Defer access checks while parsing the declarator; we cannot know
15527 what names are accessible until we know what is being
15529 resume_deferring_access_checks ();
15531 /* Parse the declarator. */
15532 token = cp_lexer_peek_token (parser->lexer);
15534 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15535 &ctor_dtor_or_conv_p,
15536 /*parenthesized_p=*/NULL,
15538 /* Gather up the deferred checks. */
15539 stop_deferring_access_checks ();
15541 /* If the DECLARATOR was erroneous, there's no need to go
15543 if (declarator == cp_error_declarator)
15544 return error_mark_node;
15546 /* Check that the number of template-parameter-lists is OK. */
15547 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15549 return error_mark_node;
15551 if (declares_class_or_enum & 2)
15552 cp_parser_check_for_definition_in_return_type (declarator,
15553 decl_specifiers->type,
15554 decl_specifiers->locations[ds_type_spec]);
15556 /* Figure out what scope the entity declared by the DECLARATOR is
15557 located in. `grokdeclarator' sometimes changes the scope, so
15558 we compute it now. */
15559 scope = get_scope_of_declarator (declarator);
15561 /* Perform any lookups in the declared type which were thought to be
15562 dependent, but are not in the scope of the declarator. */
15563 decl_specifiers->type
15564 = maybe_update_decl_type (decl_specifiers->type, scope);
15566 /* If we're allowing GNU extensions, look for an asm-specification
15568 if (cp_parser_allow_gnu_extensions_p (parser))
15570 /* Look for an asm-specification. */
15571 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15572 asm_specification = cp_parser_asm_specification_opt (parser);
15573 /* And attributes. */
15574 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15575 attributes = cp_parser_attributes_opt (parser);
15579 asm_specification = NULL_TREE;
15580 attributes = NULL_TREE;
15583 /* Peek at the next token. */
15584 token = cp_lexer_peek_token (parser->lexer);
15585 /* Check to see if the token indicates the start of a
15586 function-definition. */
15587 if (function_declarator_p (declarator)
15588 && cp_parser_token_starts_function_definition_p (token))
15590 if (!function_definition_allowed_p)
15592 /* If a function-definition should not appear here, issue an
15594 cp_parser_error (parser,
15595 "a function-definition is not allowed here");
15596 return error_mark_node;
15600 location_t func_brace_location
15601 = cp_lexer_peek_token (parser->lexer)->location;
15603 /* Neither attributes nor an asm-specification are allowed
15604 on a function-definition. */
15605 if (asm_specification)
15606 error_at (asm_spec_start_token->location,
15607 "an asm-specification is not allowed "
15608 "on a function-definition");
15610 error_at (attributes_start_token->location,
15611 "attributes are not allowed on a function-definition");
15612 /* This is a function-definition. */
15613 *function_definition_p = true;
15615 /* Parse the function definition. */
15617 decl = cp_parser_save_member_function_body (parser,
15620 prefix_attributes);
15623 = (cp_parser_function_definition_from_specifiers_and_declarator
15624 (parser, decl_specifiers, prefix_attributes, declarator));
15626 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15628 /* This is where the prologue starts... */
15629 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15630 = func_brace_location;
15639 Only in function declarations for constructors, destructors, and
15640 type conversions can the decl-specifier-seq be omitted.
15642 We explicitly postpone this check past the point where we handle
15643 function-definitions because we tolerate function-definitions
15644 that are missing their return types in some modes. */
15645 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15647 cp_parser_error (parser,
15648 "expected constructor, destructor, or type conversion");
15649 return error_mark_node;
15652 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15653 if (token->type == CPP_EQ
15654 || token->type == CPP_OPEN_PAREN
15655 || token->type == CPP_OPEN_BRACE)
15657 is_initialized = SD_INITIALIZED;
15658 initialization_kind = token->type;
15659 if (maybe_range_for_decl)
15660 *maybe_range_for_decl = error_mark_node;
15662 if (token->type == CPP_EQ
15663 && function_declarator_p (declarator))
15665 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15666 if (t2->keyword == RID_DEFAULT)
15667 is_initialized = SD_DEFAULTED;
15668 else if (t2->keyword == RID_DELETE)
15669 is_initialized = SD_DELETED;
15674 /* If the init-declarator isn't initialized and isn't followed by a
15675 `,' or `;', it's not a valid init-declarator. */
15676 if (token->type != CPP_COMMA
15677 && token->type != CPP_SEMICOLON)
15679 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15680 range_for_decl_p = true;
15683 cp_parser_error (parser, "expected initializer");
15684 return error_mark_node;
15687 is_initialized = SD_UNINITIALIZED;
15688 initialization_kind = CPP_EOF;
15691 /* Because start_decl has side-effects, we should only call it if we
15692 know we're going ahead. By this point, we know that we cannot
15693 possibly be looking at any other construct. */
15694 cp_parser_commit_to_tentative_parse (parser);
15696 /* If the decl specifiers were bad, issue an error now that we're
15697 sure this was intended to be a declarator. Then continue
15698 declaring the variable(s), as int, to try to cut down on further
15700 if (decl_specifiers->any_specifiers_p
15701 && decl_specifiers->type == error_mark_node)
15703 cp_parser_error (parser, "invalid type in declaration");
15704 decl_specifiers->type = integer_type_node;
15707 /* Check to see whether or not this declaration is a friend. */
15708 friend_p = cp_parser_friend_p (decl_specifiers);
15710 /* Enter the newly declared entry in the symbol table. If we're
15711 processing a declaration in a class-specifier, we wait until
15712 after processing the initializer. */
15715 if (parser->in_unbraced_linkage_specification_p)
15716 decl_specifiers->storage_class = sc_extern;
15717 decl = start_decl (declarator, decl_specifiers,
15718 range_for_decl_p? SD_INITIALIZED : is_initialized,
15719 attributes, prefix_attributes,
15721 /* Adjust location of decl if declarator->id_loc is more appropriate:
15722 set, and decl wasn't merged with another decl, in which case its
15723 location would be different from input_location, and more accurate. */
15725 && declarator->id_loc != UNKNOWN_LOCATION
15726 && DECL_SOURCE_LOCATION (decl) == input_location)
15727 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15730 /* Enter the SCOPE. That way unqualified names appearing in the
15731 initializer will be looked up in SCOPE. */
15732 pushed_scope = push_scope (scope);
15734 /* Perform deferred access control checks, now that we know in which
15735 SCOPE the declared entity resides. */
15736 if (!member_p && decl)
15738 tree saved_current_function_decl = NULL_TREE;
15740 /* If the entity being declared is a function, pretend that we
15741 are in its scope. If it is a `friend', it may have access to
15742 things that would not otherwise be accessible. */
15743 if (TREE_CODE (decl) == FUNCTION_DECL)
15745 saved_current_function_decl = current_function_decl;
15746 current_function_decl = decl;
15749 /* Perform access checks for template parameters. */
15750 cp_parser_perform_template_parameter_access_checks (checks);
15752 /* Perform the access control checks for the declarator and the
15753 decl-specifiers. */
15754 perform_deferred_access_checks ();
15756 /* Restore the saved value. */
15757 if (TREE_CODE (decl) == FUNCTION_DECL)
15758 current_function_decl = saved_current_function_decl;
15761 /* Parse the initializer. */
15762 initializer = NULL_TREE;
15763 is_direct_init = false;
15764 is_non_constant_init = true;
15765 if (is_initialized)
15767 if (function_declarator_p (declarator))
15769 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15770 if (initialization_kind == CPP_EQ)
15771 initializer = cp_parser_pure_specifier (parser);
15774 /* If the declaration was erroneous, we don't really
15775 know what the user intended, so just silently
15776 consume the initializer. */
15777 if (decl != error_mark_node)
15778 error_at (initializer_start_token->location,
15779 "initializer provided for function");
15780 cp_parser_skip_to_closing_parenthesis (parser,
15781 /*recovering=*/true,
15782 /*or_comma=*/false,
15783 /*consume_paren=*/true);
15788 /* We want to record the extra mangling scope for in-class
15789 initializers of class members and initializers of static data
15790 member templates. The former involves deferring
15791 parsing of the initializer until end of class as with default
15792 arguments. So right here we only handle the latter. */
15793 if (!member_p && processing_template_decl)
15794 start_lambda_scope (decl);
15795 initializer = cp_parser_initializer (parser,
15797 &is_non_constant_init);
15798 if (!member_p && processing_template_decl)
15799 finish_lambda_scope ();
15800 if (initializer == error_mark_node)
15801 cp_parser_skip_to_end_of_statement (parser);
15805 /* The old parser allows attributes to appear after a parenthesized
15806 initializer. Mark Mitchell proposed removing this functionality
15807 on the GCC mailing lists on 2002-08-13. This parser accepts the
15808 attributes -- but ignores them. */
15809 if (cp_parser_allow_gnu_extensions_p (parser)
15810 && initialization_kind == CPP_OPEN_PAREN)
15811 if (cp_parser_attributes_opt (parser))
15812 warning (OPT_Wattributes,
15813 "attributes after parenthesized initializer ignored");
15815 /* For an in-class declaration, use `grokfield' to create the
15821 pop_scope (pushed_scope);
15822 pushed_scope = NULL_TREE;
15824 decl = grokfield (declarator, decl_specifiers,
15825 initializer, !is_non_constant_init,
15826 /*asmspec=*/NULL_TREE,
15827 prefix_attributes);
15828 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15829 cp_parser_save_default_args (parser, decl);
15832 /* Finish processing the declaration. But, skip member
15834 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15836 cp_finish_decl (decl,
15837 initializer, !is_non_constant_init,
15839 /* If the initializer is in parentheses, then this is
15840 a direct-initialization, which means that an
15841 `explicit' constructor is OK. Otherwise, an
15842 `explicit' constructor cannot be used. */
15843 ((is_direct_init || !is_initialized)
15844 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15846 else if ((cxx_dialect != cxx98) && friend_p
15847 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15848 /* Core issue #226 (C++0x only): A default template-argument
15849 shall not be specified in a friend class template
15851 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15852 /*is_partial=*/0, /*is_friend_decl=*/1);
15854 if (!friend_p && pushed_scope)
15855 pop_scope (pushed_scope);
15860 /* Parse a declarator.
15864 ptr-operator declarator
15866 abstract-declarator:
15867 ptr-operator abstract-declarator [opt]
15868 direct-abstract-declarator
15873 attributes [opt] direct-declarator
15874 attributes [opt] ptr-operator declarator
15876 abstract-declarator:
15877 attributes [opt] ptr-operator abstract-declarator [opt]
15878 attributes [opt] direct-abstract-declarator
15880 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15881 detect constructor, destructor or conversion operators. It is set
15882 to -1 if the declarator is a name, and +1 if it is a
15883 function. Otherwise it is set to zero. Usually you just want to
15884 test for >0, but internally the negative value is used.
15886 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15887 a decl-specifier-seq unless it declares a constructor, destructor,
15888 or conversion. It might seem that we could check this condition in
15889 semantic analysis, rather than parsing, but that makes it difficult
15890 to handle something like `f()'. We want to notice that there are
15891 no decl-specifiers, and therefore realize that this is an
15892 expression, not a declaration.)
15894 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15895 the declarator is a direct-declarator of the form "(...)".
15897 MEMBER_P is true iff this declarator is a member-declarator. */
15899 static cp_declarator *
15900 cp_parser_declarator (cp_parser* parser,
15901 cp_parser_declarator_kind dcl_kind,
15902 int* ctor_dtor_or_conv_p,
15903 bool* parenthesized_p,
15906 cp_declarator *declarator;
15907 enum tree_code code;
15908 cp_cv_quals cv_quals;
15910 tree attributes = NULL_TREE;
15912 /* Assume this is not a constructor, destructor, or type-conversion
15914 if (ctor_dtor_or_conv_p)
15915 *ctor_dtor_or_conv_p = 0;
15917 if (cp_parser_allow_gnu_extensions_p (parser))
15918 attributes = cp_parser_attributes_opt (parser);
15920 /* Check for the ptr-operator production. */
15921 cp_parser_parse_tentatively (parser);
15922 /* Parse the ptr-operator. */
15923 code = cp_parser_ptr_operator (parser,
15926 /* If that worked, then we have a ptr-operator. */
15927 if (cp_parser_parse_definitely (parser))
15929 /* If a ptr-operator was found, then this declarator was not
15931 if (parenthesized_p)
15932 *parenthesized_p = true;
15933 /* The dependent declarator is optional if we are parsing an
15934 abstract-declarator. */
15935 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15936 cp_parser_parse_tentatively (parser);
15938 /* Parse the dependent declarator. */
15939 declarator = cp_parser_declarator (parser, dcl_kind,
15940 /*ctor_dtor_or_conv_p=*/NULL,
15941 /*parenthesized_p=*/NULL,
15942 /*member_p=*/false);
15944 /* If we are parsing an abstract-declarator, we must handle the
15945 case where the dependent declarator is absent. */
15946 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15947 && !cp_parser_parse_definitely (parser))
15950 declarator = cp_parser_make_indirect_declarator
15951 (code, class_type, cv_quals, declarator);
15953 /* Everything else is a direct-declarator. */
15956 if (parenthesized_p)
15957 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15959 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15960 ctor_dtor_or_conv_p,
15964 if (attributes && declarator && declarator != cp_error_declarator)
15965 declarator->attributes = attributes;
15970 /* Parse a direct-declarator or direct-abstract-declarator.
15974 direct-declarator ( parameter-declaration-clause )
15975 cv-qualifier-seq [opt]
15976 exception-specification [opt]
15977 direct-declarator [ constant-expression [opt] ]
15980 direct-abstract-declarator:
15981 direct-abstract-declarator [opt]
15982 ( parameter-declaration-clause )
15983 cv-qualifier-seq [opt]
15984 exception-specification [opt]
15985 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15986 ( abstract-declarator )
15988 Returns a representation of the declarator. DCL_KIND is
15989 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15990 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15991 we are parsing a direct-declarator. It is
15992 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15993 of ambiguity we prefer an abstract declarator, as per
15994 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15995 cp_parser_declarator. */
15997 static cp_declarator *
15998 cp_parser_direct_declarator (cp_parser* parser,
15999 cp_parser_declarator_kind dcl_kind,
16000 int* ctor_dtor_or_conv_p,
16004 cp_declarator *declarator = NULL;
16005 tree scope = NULL_TREE;
16006 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16007 bool saved_in_declarator_p = parser->in_declarator_p;
16009 tree pushed_scope = NULL_TREE;
16013 /* Peek at the next token. */
16014 token = cp_lexer_peek_token (parser->lexer);
16015 if (token->type == CPP_OPEN_PAREN)
16017 /* This is either a parameter-declaration-clause, or a
16018 parenthesized declarator. When we know we are parsing a
16019 named declarator, it must be a parenthesized declarator
16020 if FIRST is true. For instance, `(int)' is a
16021 parameter-declaration-clause, with an omitted
16022 direct-abstract-declarator. But `((*))', is a
16023 parenthesized abstract declarator. Finally, when T is a
16024 template parameter `(T)' is a
16025 parameter-declaration-clause, and not a parenthesized
16028 We first try and parse a parameter-declaration-clause,
16029 and then try a nested declarator (if FIRST is true).
16031 It is not an error for it not to be a
16032 parameter-declaration-clause, even when FIRST is
16038 The first is the declaration of a function while the
16039 second is the definition of a variable, including its
16042 Having seen only the parenthesis, we cannot know which of
16043 these two alternatives should be selected. Even more
16044 complex are examples like:
16049 The former is a function-declaration; the latter is a
16050 variable initialization.
16052 Thus again, we try a parameter-declaration-clause, and if
16053 that fails, we back out and return. */
16055 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16058 unsigned saved_num_template_parameter_lists;
16059 bool is_declarator = false;
16062 /* In a member-declarator, the only valid interpretation
16063 of a parenthesis is the start of a
16064 parameter-declaration-clause. (It is invalid to
16065 initialize a static data member with a parenthesized
16066 initializer; only the "=" form of initialization is
16069 cp_parser_parse_tentatively (parser);
16071 /* Consume the `('. */
16072 cp_lexer_consume_token (parser->lexer);
16075 /* If this is going to be an abstract declarator, we're
16076 in a declarator and we can't have default args. */
16077 parser->default_arg_ok_p = false;
16078 parser->in_declarator_p = true;
16081 /* Inside the function parameter list, surrounding
16082 template-parameter-lists do not apply. */
16083 saved_num_template_parameter_lists
16084 = parser->num_template_parameter_lists;
16085 parser->num_template_parameter_lists = 0;
16087 begin_scope (sk_function_parms, NULL_TREE);
16089 /* Parse the parameter-declaration-clause. */
16090 params = cp_parser_parameter_declaration_clause (parser);
16092 parser->num_template_parameter_lists
16093 = saved_num_template_parameter_lists;
16095 /* Consume the `)'. */
16096 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16098 /* If all went well, parse the cv-qualifier-seq and the
16099 exception-specification. */
16100 if (member_p || cp_parser_parse_definitely (parser))
16102 cp_cv_quals cv_quals;
16103 cp_virt_specifiers virt_specifiers;
16104 tree exception_specification;
16107 is_declarator = true;
16109 if (ctor_dtor_or_conv_p)
16110 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16113 /* Parse the cv-qualifier-seq. */
16114 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16115 /* And the exception-specification. */
16116 exception_specification
16117 = cp_parser_exception_specification_opt (parser);
16119 late_return = (cp_parser_late_return_type_opt
16120 (parser, member_p ? cv_quals : -1));
16122 /* Parse the virt-specifier-seq. */
16123 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16125 /* Create the function-declarator. */
16126 declarator = make_call_declarator (declarator,
16130 exception_specification,
16132 /* Any subsequent parameter lists are to do with
16133 return type, so are not those of the declared
16135 parser->default_arg_ok_p = false;
16138 /* Remove the function parms from scope. */
16139 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16140 pop_binding (DECL_NAME (t), t);
16144 /* Repeat the main loop. */
16148 /* If this is the first, we can try a parenthesized
16152 bool saved_in_type_id_in_expr_p;
16154 parser->default_arg_ok_p = saved_default_arg_ok_p;
16155 parser->in_declarator_p = saved_in_declarator_p;
16157 /* Consume the `('. */
16158 cp_lexer_consume_token (parser->lexer);
16159 /* Parse the nested declarator. */
16160 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16161 parser->in_type_id_in_expr_p = true;
16163 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16164 /*parenthesized_p=*/NULL,
16166 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16168 /* Expect a `)'. */
16169 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16170 declarator = cp_error_declarator;
16171 if (declarator == cp_error_declarator)
16174 goto handle_declarator;
16176 /* Otherwise, we must be done. */
16180 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16181 && token->type == CPP_OPEN_SQUARE)
16183 /* Parse an array-declarator. */
16186 if (ctor_dtor_or_conv_p)
16187 *ctor_dtor_or_conv_p = 0;
16190 parser->default_arg_ok_p = false;
16191 parser->in_declarator_p = true;
16192 /* Consume the `['. */
16193 cp_lexer_consume_token (parser->lexer);
16194 /* Peek at the next token. */
16195 token = cp_lexer_peek_token (parser->lexer);
16196 /* If the next token is `]', then there is no
16197 constant-expression. */
16198 if (token->type != CPP_CLOSE_SQUARE)
16200 bool non_constant_p;
16203 = cp_parser_constant_expression (parser,
16204 /*allow_non_constant=*/true,
16206 if (!non_constant_p)
16208 else if (error_operand_p (bounds))
16209 /* Already gave an error. */;
16210 else if (!parser->in_function_body
16211 || current_binding_level->kind == sk_function_parms)
16213 /* Normally, the array bound must be an integral constant
16214 expression. However, as an extension, we allow VLAs
16215 in function scopes as long as they aren't part of a
16216 parameter declaration. */
16217 cp_parser_error (parser,
16218 "array bound is not an integer constant");
16219 bounds = error_mark_node;
16221 else if (processing_template_decl)
16223 /* Remember this wasn't a constant-expression. */
16224 bounds = build_nop (TREE_TYPE (bounds), bounds);
16225 TREE_SIDE_EFFECTS (bounds) = 1;
16229 bounds = NULL_TREE;
16230 /* Look for the closing `]'. */
16231 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16233 declarator = cp_error_declarator;
16237 declarator = make_array_declarator (declarator, bounds);
16239 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16242 tree qualifying_scope;
16243 tree unqualified_name;
16244 special_function_kind sfk;
16246 bool pack_expansion_p = false;
16247 cp_token *declarator_id_start_token;
16249 /* Parse a declarator-id */
16250 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16253 cp_parser_parse_tentatively (parser);
16255 /* If we see an ellipsis, we should be looking at a
16257 if (token->type == CPP_ELLIPSIS)
16259 /* Consume the `...' */
16260 cp_lexer_consume_token (parser->lexer);
16262 pack_expansion_p = true;
16266 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16268 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16269 qualifying_scope = parser->scope;
16274 if (!unqualified_name && pack_expansion_p)
16276 /* Check whether an error occurred. */
16277 okay = !cp_parser_error_occurred (parser);
16279 /* We already consumed the ellipsis to mark a
16280 parameter pack, but we have no way to report it,
16281 so abort the tentative parse. We will be exiting
16282 immediately anyway. */
16283 cp_parser_abort_tentative_parse (parser);
16286 okay = cp_parser_parse_definitely (parser);
16289 unqualified_name = error_mark_node;
16290 else if (unqualified_name
16291 && (qualifying_scope
16292 || (TREE_CODE (unqualified_name)
16293 != IDENTIFIER_NODE)))
16295 cp_parser_error (parser, "expected unqualified-id");
16296 unqualified_name = error_mark_node;
16300 if (!unqualified_name)
16302 if (unqualified_name == error_mark_node)
16304 declarator = cp_error_declarator;
16305 pack_expansion_p = false;
16306 declarator->parameter_pack_p = false;
16310 if (qualifying_scope && at_namespace_scope_p ()
16311 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16313 /* In the declaration of a member of a template class
16314 outside of the class itself, the SCOPE will sometimes
16315 be a TYPENAME_TYPE. For example, given:
16317 template <typename T>
16318 int S<T>::R::i = 3;
16320 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16321 this context, we must resolve S<T>::R to an ordinary
16322 type, rather than a typename type.
16324 The reason we normally avoid resolving TYPENAME_TYPEs
16325 is that a specialization of `S' might render
16326 `S<T>::R' not a type. However, if `S' is
16327 specialized, then this `i' will not be used, so there
16328 is no harm in resolving the types here. */
16331 /* Resolve the TYPENAME_TYPE. */
16332 type = resolve_typename_type (qualifying_scope,
16333 /*only_current_p=*/false);
16334 /* If that failed, the declarator is invalid. */
16335 if (TREE_CODE (type) == TYPENAME_TYPE)
16337 if (typedef_variant_p (type))
16338 error_at (declarator_id_start_token->location,
16339 "cannot define member of dependent typedef "
16342 error_at (declarator_id_start_token->location,
16343 "%<%T::%E%> is not a type",
16344 TYPE_CONTEXT (qualifying_scope),
16345 TYPE_IDENTIFIER (qualifying_scope));
16347 qualifying_scope = type;
16352 if (unqualified_name)
16356 if (qualifying_scope
16357 && CLASS_TYPE_P (qualifying_scope))
16358 class_type = qualifying_scope;
16360 class_type = current_class_type;
16362 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16364 tree name_type = TREE_TYPE (unqualified_name);
16365 if (class_type && same_type_p (name_type, class_type))
16367 if (qualifying_scope
16368 && CLASSTYPE_USE_TEMPLATE (name_type))
16370 error_at (declarator_id_start_token->location,
16371 "invalid use of constructor as a template");
16372 inform (declarator_id_start_token->location,
16373 "use %<%T::%D%> instead of %<%T::%D%> to "
16374 "name the constructor in a qualified name",
16376 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16377 class_type, name_type);
16378 declarator = cp_error_declarator;
16382 unqualified_name = constructor_name (class_type);
16386 /* We do not attempt to print the declarator
16387 here because we do not have enough
16388 information about its original syntactic
16390 cp_parser_error (parser, "invalid declarator");
16391 declarator = cp_error_declarator;
16398 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16399 sfk = sfk_destructor;
16400 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16401 sfk = sfk_conversion;
16402 else if (/* There's no way to declare a constructor
16403 for an anonymous type, even if the type
16404 got a name for linkage purposes. */
16405 !TYPE_WAS_ANONYMOUS (class_type)
16406 && constructor_name_p (unqualified_name,
16409 unqualified_name = constructor_name (class_type);
16410 sfk = sfk_constructor;
16412 else if (is_overloaded_fn (unqualified_name)
16413 && DECL_CONSTRUCTOR_P (get_first_fn
16414 (unqualified_name)))
16415 sfk = sfk_constructor;
16417 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16418 *ctor_dtor_or_conv_p = -1;
16421 declarator = make_id_declarator (qualifying_scope,
16424 declarator->id_loc = token->location;
16425 declarator->parameter_pack_p = pack_expansion_p;
16427 if (pack_expansion_p)
16428 maybe_warn_variadic_templates ();
16431 handle_declarator:;
16432 scope = get_scope_of_declarator (declarator);
16434 /* Any names that appear after the declarator-id for a
16435 member are looked up in the containing scope. */
16436 pushed_scope = push_scope (scope);
16437 parser->in_declarator_p = true;
16438 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16439 || (declarator && declarator->kind == cdk_id))
16440 /* Default args are only allowed on function
16442 parser->default_arg_ok_p = saved_default_arg_ok_p;
16444 parser->default_arg_ok_p = false;
16453 /* For an abstract declarator, we might wind up with nothing at this
16454 point. That's an error; the declarator is not optional. */
16456 cp_parser_error (parser, "expected declarator");
16458 /* If we entered a scope, we must exit it now. */
16460 pop_scope (pushed_scope);
16462 parser->default_arg_ok_p = saved_default_arg_ok_p;
16463 parser->in_declarator_p = saved_in_declarator_p;
16468 /* Parse a ptr-operator.
16471 * cv-qualifier-seq [opt]
16473 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16478 & cv-qualifier-seq [opt]
16480 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16481 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16482 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16483 filled in with the TYPE containing the member. *CV_QUALS is
16484 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16485 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16486 Note that the tree codes returned by this function have nothing
16487 to do with the types of trees that will be eventually be created
16488 to represent the pointer or reference type being parsed. They are
16489 just constants with suggestive names. */
16490 static enum tree_code
16491 cp_parser_ptr_operator (cp_parser* parser,
16493 cp_cv_quals *cv_quals)
16495 enum tree_code code = ERROR_MARK;
16498 /* Assume that it's not a pointer-to-member. */
16500 /* And that there are no cv-qualifiers. */
16501 *cv_quals = TYPE_UNQUALIFIED;
16503 /* Peek at the next token. */
16504 token = cp_lexer_peek_token (parser->lexer);
16506 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16507 if (token->type == CPP_MULT)
16508 code = INDIRECT_REF;
16509 else if (token->type == CPP_AND)
16511 else if ((cxx_dialect != cxx98) &&
16512 token->type == CPP_AND_AND) /* C++0x only */
16513 code = NON_LVALUE_EXPR;
16515 if (code != ERROR_MARK)
16517 /* Consume the `*', `&' or `&&'. */
16518 cp_lexer_consume_token (parser->lexer);
16520 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16521 `&', if we are allowing GNU extensions. (The only qualifier
16522 that can legally appear after `&' is `restrict', but that is
16523 enforced during semantic analysis. */
16524 if (code == INDIRECT_REF
16525 || cp_parser_allow_gnu_extensions_p (parser))
16526 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16530 /* Try the pointer-to-member case. */
16531 cp_parser_parse_tentatively (parser);
16532 /* Look for the optional `::' operator. */
16533 cp_parser_global_scope_opt (parser,
16534 /*current_scope_valid_p=*/false);
16535 /* Look for the nested-name specifier. */
16536 token = cp_lexer_peek_token (parser->lexer);
16537 cp_parser_nested_name_specifier (parser,
16538 /*typename_keyword_p=*/false,
16539 /*check_dependency_p=*/true,
16541 /*is_declaration=*/false);
16542 /* If we found it, and the next token is a `*', then we are
16543 indeed looking at a pointer-to-member operator. */
16544 if (!cp_parser_error_occurred (parser)
16545 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16547 /* Indicate that the `*' operator was used. */
16548 code = INDIRECT_REF;
16550 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16551 error_at (token->location, "%qD is a namespace", parser->scope);
16552 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16553 error_at (token->location, "cannot form pointer to member of "
16554 "non-class %q#T", parser->scope);
16557 /* The type of which the member is a member is given by the
16559 *type = parser->scope;
16560 /* The next name will not be qualified. */
16561 parser->scope = NULL_TREE;
16562 parser->qualifying_scope = NULL_TREE;
16563 parser->object_scope = NULL_TREE;
16564 /* Look for the optional cv-qualifier-seq. */
16565 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16568 /* If that didn't work we don't have a ptr-operator. */
16569 if (!cp_parser_parse_definitely (parser))
16570 cp_parser_error (parser, "expected ptr-operator");
16576 /* Parse an (optional) cv-qualifier-seq.
16579 cv-qualifier cv-qualifier-seq [opt]
16590 Returns a bitmask representing the cv-qualifiers. */
16593 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16595 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16600 cp_cv_quals cv_qualifier;
16602 /* Peek at the next token. */
16603 token = cp_lexer_peek_token (parser->lexer);
16604 /* See if it's a cv-qualifier. */
16605 switch (token->keyword)
16608 cv_qualifier = TYPE_QUAL_CONST;
16612 cv_qualifier = TYPE_QUAL_VOLATILE;
16616 cv_qualifier = TYPE_QUAL_RESTRICT;
16620 cv_qualifier = TYPE_UNQUALIFIED;
16627 if (cv_quals & cv_qualifier)
16629 error_at (token->location, "duplicate cv-qualifier");
16630 cp_lexer_purge_token (parser->lexer);
16634 cp_lexer_consume_token (parser->lexer);
16635 cv_quals |= cv_qualifier;
16642 /* Parse an (optional) virt-specifier-seq.
16644 virt-specifier-seq:
16645 virt-specifier virt-specifier-seq [opt]
16651 Returns a bitmask representing the virt-specifiers. */
16653 static cp_virt_specifiers
16654 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16656 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16661 cp_virt_specifiers virt_specifier;
16663 /* Peek at the next token. */
16664 token = cp_lexer_peek_token (parser->lexer);
16665 /* See if it's a virt-specifier-qualifier. */
16666 if (token->type != CPP_NAME)
16668 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16670 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16671 virt_specifier = VIRT_SPEC_OVERRIDE;
16673 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16675 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16676 virt_specifier = VIRT_SPEC_FINAL;
16678 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16680 virt_specifier = VIRT_SPEC_FINAL;
16685 if (virt_specifiers & virt_specifier)
16687 error_at (token->location, "duplicate virt-specifier");
16688 cp_lexer_purge_token (parser->lexer);
16692 cp_lexer_consume_token (parser->lexer);
16693 virt_specifiers |= virt_specifier;
16696 return virt_specifiers;
16699 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16700 is in scope even though it isn't real. */
16703 inject_this_parameter (tree ctype, cp_cv_quals quals)
16707 if (current_class_ptr)
16709 /* We don't clear this between NSDMIs. Is it already what we want? */
16710 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16711 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16712 && cp_type_quals (type) == quals)
16716 this_parm = build_this_parm (ctype, quals);
16717 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16718 current_class_ptr = NULL_TREE;
16720 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16721 current_class_ptr = this_parm;
16724 /* Parse a late-specified return type, if any. This is not a separate
16725 non-terminal, but part of a function declarator, which looks like
16727 -> trailing-type-specifier-seq abstract-declarator(opt)
16729 Returns the type indicated by the type-id.
16731 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16735 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16740 /* Peek at the next token. */
16741 token = cp_lexer_peek_token (parser->lexer);
16742 /* A late-specified return type is indicated by an initial '->'. */
16743 if (token->type != CPP_DEREF)
16746 /* Consume the ->. */
16747 cp_lexer_consume_token (parser->lexer);
16751 /* DR 1207: 'this' is in scope in the trailing return type. */
16752 gcc_assert (current_class_ptr == NULL_TREE);
16753 inject_this_parameter (current_class_type, quals);
16756 type = cp_parser_trailing_type_id (parser);
16759 current_class_ptr = current_class_ref = NULL_TREE;
16764 /* Parse a declarator-id.
16768 :: [opt] nested-name-specifier [opt] type-name
16770 In the `id-expression' case, the value returned is as for
16771 cp_parser_id_expression if the id-expression was an unqualified-id.
16772 If the id-expression was a qualified-id, then a SCOPE_REF is
16773 returned. The first operand is the scope (either a NAMESPACE_DECL
16774 or TREE_TYPE), but the second is still just a representation of an
16778 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16781 /* The expression must be an id-expression. Assume that qualified
16782 names are the names of types so that:
16785 int S<T>::R::i = 3;
16787 will work; we must treat `S<T>::R' as the name of a type.
16788 Similarly, assume that qualified names are templates, where
16792 int S<T>::R<T>::i = 3;
16795 id = cp_parser_id_expression (parser,
16796 /*template_keyword_p=*/false,
16797 /*check_dependency_p=*/false,
16798 /*template_p=*/NULL,
16799 /*declarator_p=*/true,
16801 if (id && BASELINK_P (id))
16802 id = BASELINK_FUNCTIONS (id);
16806 /* Parse a type-id.
16809 type-specifier-seq abstract-declarator [opt]
16811 Returns the TYPE specified. */
16814 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16815 bool is_trailing_return)
16817 cp_decl_specifier_seq type_specifier_seq;
16818 cp_declarator *abstract_declarator;
16820 /* Parse the type-specifier-seq. */
16821 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16822 is_trailing_return,
16823 &type_specifier_seq);
16824 if (type_specifier_seq.type == error_mark_node)
16825 return error_mark_node;
16827 /* There might or might not be an abstract declarator. */
16828 cp_parser_parse_tentatively (parser);
16829 /* Look for the declarator. */
16830 abstract_declarator
16831 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16832 /*parenthesized_p=*/NULL,
16833 /*member_p=*/false);
16834 /* Check to see if there really was a declarator. */
16835 if (!cp_parser_parse_definitely (parser))
16836 abstract_declarator = NULL;
16838 if (type_specifier_seq.type
16839 && type_uses_auto (type_specifier_seq.type))
16841 /* A type-id with type 'auto' is only ok if the abstract declarator
16842 is a function declarator with a late-specified return type. */
16843 if (abstract_declarator
16844 && abstract_declarator->kind == cdk_function
16845 && abstract_declarator->u.function.late_return_type)
16849 error ("invalid use of %<auto%>");
16850 return error_mark_node;
16854 return groktypename (&type_specifier_seq, abstract_declarator,
16858 static tree cp_parser_type_id (cp_parser *parser)
16860 return cp_parser_type_id_1 (parser, false, false);
16863 static tree cp_parser_template_type_arg (cp_parser *parser)
16866 const char *saved_message = parser->type_definition_forbidden_message;
16867 parser->type_definition_forbidden_message
16868 = G_("types may not be defined in template arguments");
16869 r = cp_parser_type_id_1 (parser, true, false);
16870 parser->type_definition_forbidden_message = saved_message;
16874 static tree cp_parser_trailing_type_id (cp_parser *parser)
16876 return cp_parser_type_id_1 (parser, false, true);
16879 /* Parse a type-specifier-seq.
16881 type-specifier-seq:
16882 type-specifier type-specifier-seq [opt]
16886 type-specifier-seq:
16887 attributes type-specifier-seq [opt]
16889 If IS_DECLARATION is true, we are at the start of a "condition" or
16890 exception-declaration, so we might be followed by a declarator-id.
16892 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16893 i.e. we've just seen "->".
16895 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16898 cp_parser_type_specifier_seq (cp_parser* parser,
16899 bool is_declaration,
16900 bool is_trailing_return,
16901 cp_decl_specifier_seq *type_specifier_seq)
16903 bool seen_type_specifier = false;
16904 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16905 cp_token *start_token = NULL;
16907 /* Clear the TYPE_SPECIFIER_SEQ. */
16908 clear_decl_specs (type_specifier_seq);
16910 /* In the context of a trailing return type, enum E { } is an
16911 elaborated-type-specifier followed by a function-body, not an
16913 if (is_trailing_return)
16914 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16916 /* Parse the type-specifiers and attributes. */
16919 tree type_specifier;
16920 bool is_cv_qualifier;
16922 /* Check for attributes first. */
16923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16925 type_specifier_seq->attributes =
16926 chainon (type_specifier_seq->attributes,
16927 cp_parser_attributes_opt (parser));
16931 /* record the token of the beginning of the type specifier seq,
16932 for error reporting purposes*/
16934 start_token = cp_lexer_peek_token (parser->lexer);
16936 /* Look for the type-specifier. */
16937 type_specifier = cp_parser_type_specifier (parser,
16939 type_specifier_seq,
16940 /*is_declaration=*/false,
16943 if (!type_specifier)
16945 /* If the first type-specifier could not be found, this is not a
16946 type-specifier-seq at all. */
16947 if (!seen_type_specifier)
16949 cp_parser_error (parser, "expected type-specifier");
16950 type_specifier_seq->type = error_mark_node;
16953 /* If subsequent type-specifiers could not be found, the
16954 type-specifier-seq is complete. */
16958 seen_type_specifier = true;
16959 /* The standard says that a condition can be:
16961 type-specifier-seq declarator = assignment-expression
16968 we should treat the "S" as a declarator, not as a
16969 type-specifier. The standard doesn't say that explicitly for
16970 type-specifier-seq, but it does say that for
16971 decl-specifier-seq in an ordinary declaration. Perhaps it
16972 would be clearer just to allow a decl-specifier-seq here, and
16973 then add a semantic restriction that if any decl-specifiers
16974 that are not type-specifiers appear, the program is invalid. */
16975 if (is_declaration && !is_cv_qualifier)
16976 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16980 /* Parse a parameter-declaration-clause.
16982 parameter-declaration-clause:
16983 parameter-declaration-list [opt] ... [opt]
16984 parameter-declaration-list , ...
16986 Returns a representation for the parameter declarations. A return
16987 value of NULL indicates a parameter-declaration-clause consisting
16988 only of an ellipsis. */
16991 cp_parser_parameter_declaration_clause (cp_parser* parser)
16998 /* Peek at the next token. */
16999 token = cp_lexer_peek_token (parser->lexer);
17000 /* Check for trivial parameter-declaration-clauses. */
17001 if (token->type == CPP_ELLIPSIS)
17003 /* Consume the `...' token. */
17004 cp_lexer_consume_token (parser->lexer);
17007 else if (token->type == CPP_CLOSE_PAREN)
17008 /* There are no parameters. */
17010 #ifndef NO_IMPLICIT_EXTERN_C
17011 if (in_system_header && current_class_type == NULL
17012 && current_lang_name == lang_name_c)
17016 return void_list_node;
17018 /* Check for `(void)', too, which is a special case. */
17019 else if (token->keyword == RID_VOID
17020 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17021 == CPP_CLOSE_PAREN))
17023 /* Consume the `void' token. */
17024 cp_lexer_consume_token (parser->lexer);
17025 /* There are no parameters. */
17026 return void_list_node;
17029 /* Parse the parameter-declaration-list. */
17030 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17031 /* If a parse error occurred while parsing the
17032 parameter-declaration-list, then the entire
17033 parameter-declaration-clause is erroneous. */
17037 /* Peek at the next token. */
17038 token = cp_lexer_peek_token (parser->lexer);
17039 /* If it's a `,', the clause should terminate with an ellipsis. */
17040 if (token->type == CPP_COMMA)
17042 /* Consume the `,'. */
17043 cp_lexer_consume_token (parser->lexer);
17044 /* Expect an ellipsis. */
17046 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17048 /* It might also be `...' if the optional trailing `,' was
17050 else if (token->type == CPP_ELLIPSIS)
17052 /* Consume the `...' token. */
17053 cp_lexer_consume_token (parser->lexer);
17054 /* And remember that we saw it. */
17058 ellipsis_p = false;
17060 /* Finish the parameter list. */
17062 parameters = chainon (parameters, void_list_node);
17067 /* Parse a parameter-declaration-list.
17069 parameter-declaration-list:
17070 parameter-declaration
17071 parameter-declaration-list , parameter-declaration
17073 Returns a representation of the parameter-declaration-list, as for
17074 cp_parser_parameter_declaration_clause. However, the
17075 `void_list_node' is never appended to the list. Upon return,
17076 *IS_ERROR will be true iff an error occurred. */
17079 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17081 tree parameters = NULL_TREE;
17082 tree *tail = ¶meters;
17083 bool saved_in_unbraced_linkage_specification_p;
17086 /* Assume all will go well. */
17088 /* The special considerations that apply to a function within an
17089 unbraced linkage specifications do not apply to the parameters
17090 to the function. */
17091 saved_in_unbraced_linkage_specification_p
17092 = parser->in_unbraced_linkage_specification_p;
17093 parser->in_unbraced_linkage_specification_p = false;
17095 /* Look for more parameters. */
17098 cp_parameter_declarator *parameter;
17099 tree decl = error_mark_node;
17100 bool parenthesized_p = false;
17101 /* Parse the parameter. */
17103 = cp_parser_parameter_declaration (parser,
17104 /*template_parm_p=*/false,
17107 /* We don't know yet if the enclosing context is deprecated, so wait
17108 and warn in grokparms if appropriate. */
17109 deprecated_state = DEPRECATED_SUPPRESS;
17112 decl = grokdeclarator (parameter->declarator,
17113 ¶meter->decl_specifiers,
17115 parameter->default_argument != NULL_TREE,
17116 ¶meter->decl_specifiers.attributes);
17118 deprecated_state = DEPRECATED_NORMAL;
17120 /* If a parse error occurred parsing the parameter declaration,
17121 then the entire parameter-declaration-list is erroneous. */
17122 if (decl == error_mark_node)
17125 parameters = error_mark_node;
17129 if (parameter->decl_specifiers.attributes)
17130 cplus_decl_attributes (&decl,
17131 parameter->decl_specifiers.attributes,
17133 if (DECL_NAME (decl))
17134 decl = pushdecl (decl);
17136 if (decl != error_mark_node)
17138 retrofit_lang_decl (decl);
17139 DECL_PARM_INDEX (decl) = ++index;
17140 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17143 /* Add the new parameter to the list. */
17144 *tail = build_tree_list (parameter->default_argument, decl);
17145 tail = &TREE_CHAIN (*tail);
17147 /* Peek at the next token. */
17148 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17149 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17150 /* These are for Objective-C++ */
17151 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17152 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17153 /* The parameter-declaration-list is complete. */
17155 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17159 /* Peek at the next token. */
17160 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17161 /* If it's an ellipsis, then the list is complete. */
17162 if (token->type == CPP_ELLIPSIS)
17164 /* Otherwise, there must be more parameters. Consume the
17166 cp_lexer_consume_token (parser->lexer);
17167 /* When parsing something like:
17169 int i(float f, double d)
17171 we can tell after seeing the declaration for "f" that we
17172 are not looking at an initialization of a variable "i",
17173 but rather at the declaration of a function "i".
17175 Due to the fact that the parsing of template arguments
17176 (as specified to a template-id) requires backtracking we
17177 cannot use this technique when inside a template argument
17179 if (!parser->in_template_argument_list_p
17180 && !parser->in_type_id_in_expr_p
17181 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17182 /* However, a parameter-declaration of the form
17183 "foat(f)" (which is a valid declaration of a
17184 parameter "f") can also be interpreted as an
17185 expression (the conversion of "f" to "float"). */
17186 && !parenthesized_p)
17187 cp_parser_commit_to_tentative_parse (parser);
17191 cp_parser_error (parser, "expected %<,%> or %<...%>");
17192 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17193 cp_parser_skip_to_closing_parenthesis (parser,
17194 /*recovering=*/true,
17195 /*or_comma=*/false,
17196 /*consume_paren=*/false);
17201 parser->in_unbraced_linkage_specification_p
17202 = saved_in_unbraced_linkage_specification_p;
17207 /* Parse a parameter declaration.
17209 parameter-declaration:
17210 decl-specifier-seq ... [opt] declarator
17211 decl-specifier-seq declarator = assignment-expression
17212 decl-specifier-seq ... [opt] abstract-declarator [opt]
17213 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17215 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17216 declares a template parameter. (In that case, a non-nested `>'
17217 token encountered during the parsing of the assignment-expression
17218 is not interpreted as a greater-than operator.)
17220 Returns a representation of the parameter, or NULL if an error
17221 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17222 true iff the declarator is of the form "(p)". */
17224 static cp_parameter_declarator *
17225 cp_parser_parameter_declaration (cp_parser *parser,
17226 bool template_parm_p,
17227 bool *parenthesized_p)
17229 int declares_class_or_enum;
17230 cp_decl_specifier_seq decl_specifiers;
17231 cp_declarator *declarator;
17232 tree default_argument;
17233 cp_token *token = NULL, *declarator_token_start = NULL;
17234 const char *saved_message;
17236 /* In a template parameter, `>' is not an operator.
17240 When parsing a default template-argument for a non-type
17241 template-parameter, the first non-nested `>' is taken as the end
17242 of the template parameter-list rather than a greater-than
17245 /* Type definitions may not appear in parameter types. */
17246 saved_message = parser->type_definition_forbidden_message;
17247 parser->type_definition_forbidden_message
17248 = G_("types may not be defined in parameter types");
17250 /* Parse the declaration-specifiers. */
17251 cp_parser_decl_specifier_seq (parser,
17252 CP_PARSER_FLAGS_NONE,
17254 &declares_class_or_enum);
17256 /* Complain about missing 'typename' or other invalid type names. */
17257 if (!decl_specifiers.any_type_specifiers_p)
17258 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17260 /* If an error occurred, there's no reason to attempt to parse the
17261 rest of the declaration. */
17262 if (cp_parser_error_occurred (parser))
17264 parser->type_definition_forbidden_message = saved_message;
17268 /* Peek at the next token. */
17269 token = cp_lexer_peek_token (parser->lexer);
17271 /* If the next token is a `)', `,', `=', `>', or `...', then there
17272 is no declarator. However, when variadic templates are enabled,
17273 there may be a declarator following `...'. */
17274 if (token->type == CPP_CLOSE_PAREN
17275 || token->type == CPP_COMMA
17276 || token->type == CPP_EQ
17277 || token->type == CPP_GREATER)
17280 if (parenthesized_p)
17281 *parenthesized_p = false;
17283 /* Otherwise, there should be a declarator. */
17286 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17287 parser->default_arg_ok_p = false;
17289 /* After seeing a decl-specifier-seq, if the next token is not a
17290 "(", there is no possibility that the code is a valid
17291 expression. Therefore, if parsing tentatively, we commit at
17293 if (!parser->in_template_argument_list_p
17294 /* In an expression context, having seen:
17298 we cannot be sure whether we are looking at a
17299 function-type (taking a "char" as a parameter) or a cast
17300 of some object of type "char" to "int". */
17301 && !parser->in_type_id_in_expr_p
17302 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17303 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17304 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17305 cp_parser_commit_to_tentative_parse (parser);
17306 /* Parse the declarator. */
17307 declarator_token_start = token;
17308 declarator = cp_parser_declarator (parser,
17309 CP_PARSER_DECLARATOR_EITHER,
17310 /*ctor_dtor_or_conv_p=*/NULL,
17312 /*member_p=*/false);
17313 parser->default_arg_ok_p = saved_default_arg_ok_p;
17314 /* After the declarator, allow more attributes. */
17315 decl_specifiers.attributes
17316 = chainon (decl_specifiers.attributes,
17317 cp_parser_attributes_opt (parser));
17320 /* If the next token is an ellipsis, and we have not seen a
17321 declarator name, and the type of the declarator contains parameter
17322 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17323 a parameter pack expansion expression. Otherwise, leave the
17324 ellipsis for a C-style variadic function. */
17325 token = cp_lexer_peek_token (parser->lexer);
17326 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17328 tree type = decl_specifiers.type;
17330 if (type && DECL_P (type))
17331 type = TREE_TYPE (type);
17334 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17335 && declarator_can_be_parameter_pack (declarator)
17336 && (!declarator || !declarator->parameter_pack_p)
17337 && uses_parameter_packs (type))
17339 /* Consume the `...'. */
17340 cp_lexer_consume_token (parser->lexer);
17341 maybe_warn_variadic_templates ();
17343 /* Build a pack expansion type */
17345 declarator->parameter_pack_p = true;
17347 decl_specifiers.type = make_pack_expansion (type);
17351 /* The restriction on defining new types applies only to the type
17352 of the parameter, not to the default argument. */
17353 parser->type_definition_forbidden_message = saved_message;
17355 /* If the next token is `=', then process a default argument. */
17356 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17358 token = cp_lexer_peek_token (parser->lexer);
17359 /* If we are defining a class, then the tokens that make up the
17360 default argument must be saved and processed later. */
17361 if (!template_parm_p && at_class_scope_p ()
17362 && TYPE_BEING_DEFINED (current_class_type)
17363 && !LAMBDA_TYPE_P (current_class_type))
17364 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17365 /* Outside of a class definition, we can just parse the
17366 assignment-expression. */
17369 = cp_parser_default_argument (parser, template_parm_p);
17371 if (!parser->default_arg_ok_p)
17373 if (flag_permissive)
17374 warning (0, "deprecated use of default argument for parameter of non-function");
17377 error_at (token->location,
17378 "default arguments are only "
17379 "permitted for function parameters");
17380 default_argument = NULL_TREE;
17383 else if ((declarator && declarator->parameter_pack_p)
17384 || (decl_specifiers.type
17385 && PACK_EXPANSION_P (decl_specifiers.type)))
17387 /* Find the name of the parameter pack. */
17388 cp_declarator *id_declarator = declarator;
17389 while (id_declarator && id_declarator->kind != cdk_id)
17390 id_declarator = id_declarator->declarator;
17392 if (id_declarator && id_declarator->kind == cdk_id)
17393 error_at (declarator_token_start->location,
17395 ? G_("template parameter pack %qD "
17396 "cannot have a default argument")
17397 : G_("parameter pack %qD cannot have "
17398 "a default argument"),
17399 id_declarator->u.id.unqualified_name);
17401 error_at (declarator_token_start->location,
17403 ? G_("template parameter pack cannot have "
17404 "a default argument")
17405 : G_("parameter pack cannot have a "
17406 "default argument"));
17408 default_argument = NULL_TREE;
17412 default_argument = NULL_TREE;
17414 return make_parameter_declarator (&decl_specifiers,
17419 /* Parse a default argument and return it.
17421 TEMPLATE_PARM_P is true if this is a default argument for a
17422 non-type template parameter. */
17424 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17426 tree default_argument = NULL_TREE;
17427 bool saved_greater_than_is_operator_p;
17428 bool saved_local_variables_forbidden_p;
17429 bool non_constant_p, is_direct_init;
17431 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17433 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17434 parser->greater_than_is_operator_p = !template_parm_p;
17435 /* Local variable names (and the `this' keyword) may not
17436 appear in a default argument. */
17437 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17438 parser->local_variables_forbidden_p = true;
17439 /* Parse the assignment-expression. */
17440 if (template_parm_p)
17441 push_deferring_access_checks (dk_no_deferred);
17443 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17444 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17445 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17446 if (template_parm_p)
17447 pop_deferring_access_checks ();
17448 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17449 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17451 return default_argument;
17454 /* Parse a function-body.
17457 compound_statement */
17460 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17462 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17465 /* Parse a ctor-initializer-opt followed by a function-body. Return
17466 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17467 is true we are parsing a function-try-block. */
17470 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17471 bool in_function_try_block)
17474 bool ctor_initializer_p;
17475 const bool check_body_p =
17476 DECL_CONSTRUCTOR_P (current_function_decl)
17477 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17480 /* Begin the function body. */
17481 body = begin_function_body ();
17482 /* Parse the optional ctor-initializer. */
17483 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17485 /* If we're parsing a constexpr constructor definition, we need
17486 to check that the constructor body is indeed empty. However,
17487 before we get to cp_parser_function_body lot of junk has been
17488 generated, so we can't just check that we have an empty block.
17489 Rather we take a snapshot of the outermost block, and check whether
17490 cp_parser_function_body changed its state. */
17493 list = cur_stmt_list;
17494 if (STATEMENT_LIST_TAIL (list))
17495 last = STATEMENT_LIST_TAIL (list)->stmt;
17497 /* Parse the function-body. */
17498 cp_parser_function_body (parser, in_function_try_block);
17500 check_constexpr_ctor_body (last, list);
17501 /* Finish the function body. */
17502 finish_function_body (body);
17504 return ctor_initializer_p;
17507 /* Parse an initializer.
17510 = initializer-clause
17511 ( expression-list )
17513 Returns an expression representing the initializer. If no
17514 initializer is present, NULL_TREE is returned.
17516 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17517 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17518 set to TRUE if there is no initializer present. If there is an
17519 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17520 is set to true; otherwise it is set to false. */
17523 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17524 bool* non_constant_p)
17529 /* Peek at the next token. */
17530 token = cp_lexer_peek_token (parser->lexer);
17532 /* Let our caller know whether or not this initializer was
17534 *is_direct_init = (token->type != CPP_EQ);
17535 /* Assume that the initializer is constant. */
17536 *non_constant_p = false;
17538 if (token->type == CPP_EQ)
17540 /* Consume the `='. */
17541 cp_lexer_consume_token (parser->lexer);
17542 /* Parse the initializer-clause. */
17543 init = cp_parser_initializer_clause (parser, non_constant_p);
17545 else if (token->type == CPP_OPEN_PAREN)
17548 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17550 /*allow_expansion_p=*/true,
17553 return error_mark_node;
17554 init = build_tree_list_vec (vec);
17555 release_tree_vector (vec);
17557 else if (token->type == CPP_OPEN_BRACE)
17559 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17560 init = cp_parser_braced_list (parser, non_constant_p);
17561 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17565 /* Anything else is an error. */
17566 cp_parser_error (parser, "expected initializer");
17567 init = error_mark_node;
17573 /* Parse an initializer-clause.
17575 initializer-clause:
17576 assignment-expression
17579 Returns an expression representing the initializer.
17581 If the `assignment-expression' production is used the value
17582 returned is simply a representation for the expression.
17584 Otherwise, calls cp_parser_braced_list. */
17587 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17591 /* Assume the expression is constant. */
17592 *non_constant_p = false;
17594 /* If it is not a `{', then we are looking at an
17595 assignment-expression. */
17596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17599 = cp_parser_constant_expression (parser,
17600 /*allow_non_constant_p=*/true,
17604 initializer = cp_parser_braced_list (parser, non_constant_p);
17606 return initializer;
17609 /* Parse a brace-enclosed initializer list.
17612 { initializer-list , [opt] }
17615 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17616 the elements of the initializer-list (or NULL, if the last
17617 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17618 NULL_TREE. There is no way to detect whether or not the optional
17619 trailing `,' was provided. NON_CONSTANT_P is as for
17620 cp_parser_initializer. */
17623 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17627 /* Consume the `{' token. */
17628 cp_lexer_consume_token (parser->lexer);
17629 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17630 initializer = make_node (CONSTRUCTOR);
17631 /* If it's not a `}', then there is a non-trivial initializer. */
17632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17634 /* Parse the initializer list. */
17635 CONSTRUCTOR_ELTS (initializer)
17636 = cp_parser_initializer_list (parser, non_constant_p);
17637 /* A trailing `,' token is allowed. */
17638 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17639 cp_lexer_consume_token (parser->lexer);
17641 /* Now, there should be a trailing `}'. */
17642 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17643 TREE_TYPE (initializer) = init_list_type_node;
17644 return initializer;
17647 /* Parse an initializer-list.
17650 initializer-clause ... [opt]
17651 initializer-list , initializer-clause ... [opt]
17656 designation initializer-clause ...[opt]
17657 initializer-list , designation initializer-clause ...[opt]
17662 [ constant-expression ] =
17664 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17665 for the initializer. If the INDEX of the elt is non-NULL, it is the
17666 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17667 as for cp_parser_initializer. */
17669 static VEC(constructor_elt,gc) *
17670 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17672 VEC(constructor_elt,gc) *v = NULL;
17674 /* Assume all of the expressions are constant. */
17675 *non_constant_p = false;
17677 /* Parse the rest of the list. */
17683 bool clause_non_constant_p;
17685 /* If the next token is an identifier and the following one is a
17686 colon, we are looking at the GNU designated-initializer
17688 if (cp_parser_allow_gnu_extensions_p (parser)
17689 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17690 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17692 /* Warn the user that they are using an extension. */
17693 pedwarn (input_location, OPT_Wpedantic,
17694 "ISO C++ does not allow designated initializers");
17695 /* Consume the identifier. */
17696 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17697 /* Consume the `:'. */
17698 cp_lexer_consume_token (parser->lexer);
17700 /* Also handle the C99 syntax, '. id ='. */
17701 else if (cp_parser_allow_gnu_extensions_p (parser)
17702 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17703 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17704 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17706 /* Warn the user that they are using an extension. */
17707 pedwarn (input_location, OPT_Wpedantic,
17708 "ISO C++ does not allow C99 designated initializers");
17709 /* Consume the `.'. */
17710 cp_lexer_consume_token (parser->lexer);
17711 /* Consume the identifier. */
17712 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17713 /* Consume the `='. */
17714 cp_lexer_consume_token (parser->lexer);
17716 /* Also handle C99 array designators, '[ const ] ='. */
17717 else if (cp_parser_allow_gnu_extensions_p (parser)
17718 && !c_dialect_objc ()
17719 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17721 /* In C++11, [ could start a lambda-introducer. */
17722 cp_parser_parse_tentatively (parser);
17723 cp_lexer_consume_token (parser->lexer);
17724 designator = cp_parser_constant_expression (parser, false, NULL);
17725 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17726 cp_parser_require (parser, CPP_EQ, RT_EQ);
17727 if (!cp_parser_parse_definitely (parser))
17728 designator = NULL_TREE;
17731 designator = NULL_TREE;
17733 /* Parse the initializer. */
17734 initializer = cp_parser_initializer_clause (parser,
17735 &clause_non_constant_p);
17736 /* If any clause is non-constant, so is the entire initializer. */
17737 if (clause_non_constant_p)
17738 *non_constant_p = true;
17740 /* If we have an ellipsis, this is an initializer pack
17742 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17744 /* Consume the `...'. */
17745 cp_lexer_consume_token (parser->lexer);
17747 /* Turn the initializer into an initializer expansion. */
17748 initializer = make_pack_expansion (initializer);
17751 /* Add it to the vector. */
17752 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17754 /* If the next token is not a comma, we have reached the end of
17756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17759 /* Peek at the next token. */
17760 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17761 /* If the next token is a `}', then we're still done. An
17762 initializer-clause can have a trailing `,' after the
17763 initializer-list and before the closing `}'. */
17764 if (token->type == CPP_CLOSE_BRACE)
17767 /* Consume the `,' token. */
17768 cp_lexer_consume_token (parser->lexer);
17774 /* Classes [gram.class] */
17776 /* Parse a class-name.
17782 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17783 to indicate that names looked up in dependent types should be
17784 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17785 keyword has been used to indicate that the name that appears next
17786 is a template. TAG_TYPE indicates the explicit tag given before
17787 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17788 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17789 is the class being defined in a class-head.
17791 Returns the TYPE_DECL representing the class. */
17794 cp_parser_class_name (cp_parser *parser,
17795 bool typename_keyword_p,
17796 bool template_keyword_p,
17797 enum tag_types tag_type,
17798 bool check_dependency_p,
17800 bool is_declaration)
17806 tree identifier = NULL_TREE;
17808 /* All class-names start with an identifier. */
17809 token = cp_lexer_peek_token (parser->lexer);
17810 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17812 cp_parser_error (parser, "expected class-name");
17813 return error_mark_node;
17816 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17817 to a template-id, so we save it here. */
17818 scope = parser->scope;
17819 if (scope == error_mark_node)
17820 return error_mark_node;
17822 /* Any name names a type if we're following the `typename' keyword
17823 in a qualified name where the enclosing scope is type-dependent. */
17824 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17825 && dependent_type_p (scope));
17826 /* Handle the common case (an identifier, but not a template-id)
17828 if (token->type == CPP_NAME
17829 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17831 cp_token *identifier_token;
17834 /* Look for the identifier. */
17835 identifier_token = cp_lexer_peek_token (parser->lexer);
17836 ambiguous_p = identifier_token->ambiguous_p;
17837 identifier = cp_parser_identifier (parser);
17838 /* If the next token isn't an identifier, we are certainly not
17839 looking at a class-name. */
17840 if (identifier == error_mark_node)
17841 decl = error_mark_node;
17842 /* If we know this is a type-name, there's no need to look it
17844 else if (typename_p)
17848 tree ambiguous_decls;
17849 /* If we already know that this lookup is ambiguous, then
17850 we've already issued an error message; there's no reason
17854 cp_parser_simulate_error (parser);
17855 return error_mark_node;
17857 /* If the next token is a `::', then the name must be a type
17860 [basic.lookup.qual]
17862 During the lookup for a name preceding the :: scope
17863 resolution operator, object, function, and enumerator
17864 names are ignored. */
17865 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17866 tag_type = typename_type;
17867 /* Look up the name. */
17868 decl = cp_parser_lookup_name (parser, identifier,
17870 /*is_template=*/false,
17871 /*is_namespace=*/false,
17872 check_dependency_p,
17874 identifier_token->location);
17875 if (ambiguous_decls)
17877 if (cp_parser_parsing_tentatively (parser))
17878 cp_parser_simulate_error (parser);
17879 return error_mark_node;
17885 /* Try a template-id. */
17886 decl = cp_parser_template_id (parser, template_keyword_p,
17887 check_dependency_p,
17890 if (decl == error_mark_node)
17891 return error_mark_node;
17894 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17896 /* If this is a typename, create a TYPENAME_TYPE. */
17897 if (typename_p && decl != error_mark_node)
17899 decl = make_typename_type (scope, decl, typename_type,
17900 /*complain=*/tf_error);
17901 if (decl != error_mark_node)
17902 decl = TYPE_NAME (decl);
17905 decl = strip_using_decl (decl);
17907 /* Check to see that it is really the name of a class. */
17908 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17909 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17910 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17911 /* Situations like this:
17913 template <typename T> struct A {
17914 typename T::template X<int>::I i;
17917 are problematic. Is `T::template X<int>' a class-name? The
17918 standard does not seem to be definitive, but there is no other
17919 valid interpretation of the following `::'. Therefore, those
17920 names are considered class-names. */
17922 decl = make_typename_type (scope, decl, tag_type, tf_error);
17923 if (decl != error_mark_node)
17924 decl = TYPE_NAME (decl);
17926 else if (TREE_CODE (decl) != TYPE_DECL
17927 || TREE_TYPE (decl) == error_mark_node
17928 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17929 /* In Objective-C 2.0, a classname followed by '.' starts a
17930 dot-syntax expression, and it's not a type-name. */
17931 || (c_dialect_objc ()
17932 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17933 && objc_is_class_name (decl)))
17934 decl = error_mark_node;
17936 if (decl == error_mark_node)
17937 cp_parser_error (parser, "expected class-name");
17938 else if (identifier && !parser->scope)
17939 maybe_note_name_used_in_class (identifier, decl);
17944 /* Parse a class-specifier.
17947 class-head { member-specification [opt] }
17949 Returns the TREE_TYPE representing the class. */
17952 cp_parser_class_specifier_1 (cp_parser* parser)
17955 tree attributes = NULL_TREE;
17956 bool nested_name_specifier_p;
17957 unsigned saved_num_template_parameter_lists;
17958 bool saved_in_function_body;
17959 unsigned char in_statement;
17960 bool in_switch_statement_p;
17961 bool saved_in_unbraced_linkage_specification_p;
17962 tree old_scope = NULL_TREE;
17963 tree scope = NULL_TREE;
17964 cp_token *closing_brace;
17966 push_deferring_access_checks (dk_no_deferred);
17968 /* Parse the class-head. */
17969 type = cp_parser_class_head (parser,
17970 &nested_name_specifier_p);
17971 /* If the class-head was a semantic disaster, skip the entire body
17975 cp_parser_skip_to_end_of_block_or_statement (parser);
17976 pop_deferring_access_checks ();
17977 return error_mark_node;
17980 /* Look for the `{'. */
17981 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17983 pop_deferring_access_checks ();
17984 return error_mark_node;
17987 /* Issue an error message if type-definitions are forbidden here. */
17988 cp_parser_check_type_definition (parser);
17989 /* Remember that we are defining one more class. */
17990 ++parser->num_classes_being_defined;
17991 /* Inside the class, surrounding template-parameter-lists do not
17993 saved_num_template_parameter_lists
17994 = parser->num_template_parameter_lists;
17995 parser->num_template_parameter_lists = 0;
17996 /* We are not in a function body. */
17997 saved_in_function_body = parser->in_function_body;
17998 parser->in_function_body = false;
17999 /* Or in a loop. */
18000 in_statement = parser->in_statement;
18001 parser->in_statement = 0;
18002 /* Or in a switch. */
18003 in_switch_statement_p = parser->in_switch_statement_p;
18004 parser->in_switch_statement_p = false;
18005 /* We are not immediately inside an extern "lang" block. */
18006 saved_in_unbraced_linkage_specification_p
18007 = parser->in_unbraced_linkage_specification_p;
18008 parser->in_unbraced_linkage_specification_p = false;
18010 /* Start the class. */
18011 if (nested_name_specifier_p)
18013 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18014 old_scope = push_inner_scope (scope);
18016 type = begin_class_definition (type);
18018 if (type == error_mark_node)
18019 /* If the type is erroneous, skip the entire body of the class. */
18020 cp_parser_skip_to_closing_brace (parser);
18022 /* Parse the member-specification. */
18023 cp_parser_member_specification_opt (parser);
18025 /* Look for the trailing `}'. */
18026 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18027 /* Look for trailing attributes to apply to this class. */
18028 if (cp_parser_allow_gnu_extensions_p (parser))
18029 attributes = cp_parser_attributes_opt (parser);
18030 if (type != error_mark_node)
18031 type = finish_struct (type, attributes);
18032 if (nested_name_specifier_p)
18033 pop_inner_scope (old_scope, scope);
18035 /* We've finished a type definition. Check for the common syntax
18036 error of forgetting a semicolon after the definition. We need to
18037 be careful, as we can't just check for not-a-semicolon and be done
18038 with it; the user might have typed:
18040 class X { } c = ...;
18041 class X { } *p = ...;
18043 and so forth. Instead, enumerate all the possible tokens that
18044 might follow this production; if we don't see one of them, then
18045 complain and silently insert the semicolon. */
18047 cp_token *token = cp_lexer_peek_token (parser->lexer);
18048 bool want_semicolon = true;
18050 switch (token->type)
18053 case CPP_SEMICOLON:
18056 case CPP_OPEN_PAREN:
18057 case CPP_CLOSE_PAREN:
18059 want_semicolon = false;
18062 /* While it's legal for type qualifiers and storage class
18063 specifiers to follow type definitions in the grammar, only
18064 compiler testsuites contain code like that. Assume that if
18065 we see such code, then what we're really seeing is a case
18069 const <type> var = ...;
18074 static <type> func (...) ...
18076 i.e. the qualifier or specifier applies to the next
18077 declaration. To do so, however, we need to look ahead one
18078 more token to see if *that* token is a type specifier.
18080 This code could be improved to handle:
18083 static const <type> var = ...; */
18085 if (keyword_is_decl_specifier (token->keyword))
18087 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18089 /* Handling user-defined types here would be nice, but very
18092 = (lookahead->type == CPP_KEYWORD
18093 && keyword_begins_type_specifier (lookahead->keyword));
18100 /* If we don't have a type, then something is very wrong and we
18101 shouldn't try to do anything clever. Likewise for not seeing the
18103 if (closing_brace && TYPE_P (type) && want_semicolon)
18105 cp_token_position prev
18106 = cp_lexer_previous_token_position (parser->lexer);
18107 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18108 location_t loc = prev_token->location;
18110 if (CLASSTYPE_DECLARED_CLASS (type))
18111 error_at (loc, "expected %<;%> after class definition");
18112 else if (TREE_CODE (type) == RECORD_TYPE)
18113 error_at (loc, "expected %<;%> after struct definition");
18114 else if (TREE_CODE (type) == UNION_TYPE)
18115 error_at (loc, "expected %<;%> after union definition");
18117 gcc_unreachable ();
18119 /* Unget one token and smash it to look as though we encountered
18120 a semicolon in the input stream. */
18121 cp_lexer_set_token_position (parser->lexer, prev);
18122 token = cp_lexer_peek_token (parser->lexer);
18123 token->type = CPP_SEMICOLON;
18124 token->keyword = RID_MAX;
18128 /* If this class is not itself within the scope of another class,
18129 then we need to parse the bodies of all of the queued function
18130 definitions. Note that the queued functions defined in a class
18131 are not always processed immediately following the
18132 class-specifier for that class. Consider:
18135 struct B { void f() { sizeof (A); } };
18138 If `f' were processed before the processing of `A' were
18139 completed, there would be no way to compute the size of `A'.
18140 Note that the nesting we are interested in here is lexical --
18141 not the semantic nesting given by TYPE_CONTEXT. In particular,
18144 struct A { struct B; };
18145 struct A::B { void f() { } };
18147 there is no need to delay the parsing of `A::B::f'. */
18148 if (--parser->num_classes_being_defined == 0)
18151 tree class_type = NULL_TREE;
18152 tree pushed_scope = NULL_TREE;
18154 cp_default_arg_entry *e;
18155 tree save_ccp, save_ccr;
18157 /* In a first pass, parse default arguments to the functions.
18158 Then, in a second pass, parse the bodies of the functions.
18159 This two-phased approach handles cases like:
18167 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18171 /* If there are default arguments that have not yet been processed,
18172 take care of them now. */
18173 if (class_type != e->class_type)
18176 pop_scope (pushed_scope);
18177 class_type = e->class_type;
18178 pushed_scope = push_scope (class_type);
18180 /* Make sure that any template parameters are in scope. */
18181 maybe_begin_member_template_processing (decl);
18182 /* Parse the default argument expressions. */
18183 cp_parser_late_parsing_default_args (parser, decl);
18184 /* Remove any template parameters from the symbol table. */
18185 maybe_end_member_template_processing ();
18187 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18188 /* Now parse any NSDMIs. */
18189 save_ccp = current_class_ptr;
18190 save_ccr = current_class_ref;
18191 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18193 if (class_type != DECL_CONTEXT (decl))
18196 pop_scope (pushed_scope);
18197 class_type = DECL_CONTEXT (decl);
18198 pushed_scope = push_scope (class_type);
18200 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18201 cp_parser_late_parsing_nsdmi (parser, decl);
18203 VEC_truncate (tree, unparsed_nsdmis, 0);
18204 current_class_ptr = save_ccp;
18205 current_class_ref = save_ccr;
18207 pop_scope (pushed_scope);
18208 /* Now parse the body of the functions. */
18209 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18210 cp_parser_late_parsing_for_member (parser, decl);
18211 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18214 /* Put back any saved access checks. */
18215 pop_deferring_access_checks ();
18217 /* Restore saved state. */
18218 parser->in_switch_statement_p = in_switch_statement_p;
18219 parser->in_statement = in_statement;
18220 parser->in_function_body = saved_in_function_body;
18221 parser->num_template_parameter_lists
18222 = saved_num_template_parameter_lists;
18223 parser->in_unbraced_linkage_specification_p
18224 = saved_in_unbraced_linkage_specification_p;
18230 cp_parser_class_specifier (cp_parser* parser)
18233 timevar_push (TV_PARSE_STRUCT);
18234 ret = cp_parser_class_specifier_1 (parser);
18235 timevar_pop (TV_PARSE_STRUCT);
18239 /* Parse a class-head.
18242 class-key identifier [opt] base-clause [opt]
18243 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18244 class-key nested-name-specifier [opt] template-id
18247 class-virt-specifier:
18251 class-key attributes identifier [opt] base-clause [opt]
18252 class-key attributes nested-name-specifier identifier base-clause [opt]
18253 class-key attributes nested-name-specifier [opt] template-id
18256 Upon return BASES is initialized to the list of base classes (or
18257 NULL, if there are none) in the same form returned by
18258 cp_parser_base_clause.
18260 Returns the TYPE of the indicated class. Sets
18261 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18262 involving a nested-name-specifier was used, and FALSE otherwise.
18264 Returns error_mark_node if this is not a class-head.
18266 Returns NULL_TREE if the class-head is syntactically valid, but
18267 semantically invalid in a way that means we should skip the entire
18268 body of the class. */
18271 cp_parser_class_head (cp_parser* parser,
18272 bool* nested_name_specifier_p)
18274 tree nested_name_specifier;
18275 enum tag_types class_key;
18276 tree id = NULL_TREE;
18277 tree type = NULL_TREE;
18280 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18281 bool template_id_p = false;
18282 bool qualified_p = false;
18283 bool invalid_nested_name_p = false;
18284 bool invalid_explicit_specialization_p = false;
18285 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18286 tree pushed_scope = NULL_TREE;
18287 unsigned num_templates;
18288 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18289 /* Assume no nested-name-specifier will be present. */
18290 *nested_name_specifier_p = false;
18291 /* Assume no template parameter lists will be used in defining the
18294 parser->colon_corrects_to_scope_p = false;
18296 /* Look for the class-key. */
18297 class_key = cp_parser_class_key (parser);
18298 if (class_key == none_type)
18299 return error_mark_node;
18301 /* Parse the attributes. */
18302 attributes = cp_parser_attributes_opt (parser);
18304 /* If the next token is `::', that is invalid -- but sometimes
18305 people do try to write:
18309 Handle this gracefully by accepting the extra qualifier, and then
18310 issuing an error about it later if this really is a
18311 class-head. If it turns out just to be an elaborated type
18312 specifier, remain silent. */
18313 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18314 qualified_p = true;
18316 push_deferring_access_checks (dk_no_check);
18318 /* Determine the name of the class. Begin by looking for an
18319 optional nested-name-specifier. */
18320 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18321 nested_name_specifier
18322 = cp_parser_nested_name_specifier_opt (parser,
18323 /*typename_keyword_p=*/false,
18324 /*check_dependency_p=*/false,
18326 /*is_declaration=*/false);
18327 /* If there was a nested-name-specifier, then there *must* be an
18329 if (nested_name_specifier)
18331 type_start_token = cp_lexer_peek_token (parser->lexer);
18332 /* Although the grammar says `identifier', it really means
18333 `class-name' or `template-name'. You are only allowed to
18334 define a class that has already been declared with this
18337 The proposed resolution for Core Issue 180 says that wherever
18338 you see `class T::X' you should treat `X' as a type-name.
18340 It is OK to define an inaccessible class; for example:
18342 class A { class B; };
18345 We do not know if we will see a class-name, or a
18346 template-name. We look for a class-name first, in case the
18347 class-name is a template-id; if we looked for the
18348 template-name first we would stop after the template-name. */
18349 cp_parser_parse_tentatively (parser);
18350 type = cp_parser_class_name (parser,
18351 /*typename_keyword_p=*/false,
18352 /*template_keyword_p=*/false,
18354 /*check_dependency_p=*/false,
18355 /*class_head_p=*/true,
18356 /*is_declaration=*/false);
18357 /* If that didn't work, ignore the nested-name-specifier. */
18358 if (!cp_parser_parse_definitely (parser))
18360 invalid_nested_name_p = true;
18361 type_start_token = cp_lexer_peek_token (parser->lexer);
18362 id = cp_parser_identifier (parser);
18363 if (id == error_mark_node)
18366 /* If we could not find a corresponding TYPE, treat this
18367 declaration like an unqualified declaration. */
18368 if (type == error_mark_node)
18369 nested_name_specifier = NULL_TREE;
18370 /* Otherwise, count the number of templates used in TYPE and its
18371 containing scopes. */
18376 for (scope = TREE_TYPE (type);
18377 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18378 scope = (TYPE_P (scope)
18379 ? TYPE_CONTEXT (scope)
18380 : DECL_CONTEXT (scope)))
18382 && CLASS_TYPE_P (scope)
18383 && CLASSTYPE_TEMPLATE_INFO (scope)
18384 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18385 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18389 /* Otherwise, the identifier is optional. */
18392 /* We don't know whether what comes next is a template-id,
18393 an identifier, or nothing at all. */
18394 cp_parser_parse_tentatively (parser);
18395 /* Check for a template-id. */
18396 type_start_token = cp_lexer_peek_token (parser->lexer);
18397 id = cp_parser_template_id (parser,
18398 /*template_keyword_p=*/false,
18399 /*check_dependency_p=*/true,
18401 /*is_declaration=*/true);
18402 /* If that didn't work, it could still be an identifier. */
18403 if (!cp_parser_parse_definitely (parser))
18405 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18407 type_start_token = cp_lexer_peek_token (parser->lexer);
18408 id = cp_parser_identifier (parser);
18415 template_id_p = true;
18420 pop_deferring_access_checks ();
18424 cp_parser_check_for_invalid_template_id (parser, id,
18426 type_start_token->location);
18428 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18430 /* If it's not a `:' or a `{' then we can't really be looking at a
18431 class-head, since a class-head only appears as part of a
18432 class-specifier. We have to detect this situation before calling
18433 xref_tag, since that has irreversible side-effects. */
18434 if (!cp_parser_next_token_starts_class_definition_p (parser))
18436 cp_parser_error (parser, "expected %<{%> or %<:%>");
18437 type = error_mark_node;
18441 /* At this point, we're going ahead with the class-specifier, even
18442 if some other problem occurs. */
18443 cp_parser_commit_to_tentative_parse (parser);
18444 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18446 cp_parser_error (parser,
18447 "cannot specify %<override%> for a class");
18448 type = error_mark_node;
18451 /* Issue the error about the overly-qualified name now. */
18454 cp_parser_error (parser,
18455 "global qualification of class name is invalid");
18456 type = error_mark_node;
18459 else if (invalid_nested_name_p)
18461 cp_parser_error (parser,
18462 "qualified name does not name a class");
18463 type = error_mark_node;
18466 else if (nested_name_specifier)
18470 /* Reject typedef-names in class heads. */
18471 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18473 error_at (type_start_token->location,
18474 "invalid class name in declaration of %qD",
18480 /* Figure out in what scope the declaration is being placed. */
18481 scope = current_scope ();
18482 /* If that scope does not contain the scope in which the
18483 class was originally declared, the program is invalid. */
18484 if (scope && !is_ancestor (scope, nested_name_specifier))
18486 if (at_namespace_scope_p ())
18487 error_at (type_start_token->location,
18488 "declaration of %qD in namespace %qD which does not "
18490 type, scope, nested_name_specifier);
18492 error_at (type_start_token->location,
18493 "declaration of %qD in %qD which does not enclose %qD",
18494 type, scope, nested_name_specifier);
18500 A declarator-id shall not be qualified except for the
18501 definition of a ... nested class outside of its class
18502 ... [or] the definition or explicit instantiation of a
18503 class member of a namespace outside of its namespace. */
18504 if (scope == nested_name_specifier)
18506 permerror (nested_name_specifier_token_start->location,
18507 "extra qualification not allowed");
18508 nested_name_specifier = NULL_TREE;
18512 /* An explicit-specialization must be preceded by "template <>". If
18513 it is not, try to recover gracefully. */
18514 if (at_namespace_scope_p ()
18515 && parser->num_template_parameter_lists == 0
18518 error_at (type_start_token->location,
18519 "an explicit specialization must be preceded by %<template <>%>");
18520 invalid_explicit_specialization_p = true;
18521 /* Take the same action that would have been taken by
18522 cp_parser_explicit_specialization. */
18523 ++parser->num_template_parameter_lists;
18524 begin_specialization ();
18526 /* There must be no "return" statements between this point and the
18527 end of this function; set "type "to the correct return value and
18528 use "goto done;" to return. */
18529 /* Make sure that the right number of template parameters were
18531 if (!cp_parser_check_template_parameters (parser, num_templates,
18532 type_start_token->location,
18533 /*declarator=*/NULL))
18535 /* If something went wrong, there is no point in even trying to
18536 process the class-definition. */
18541 /* Look up the type. */
18544 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18545 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18546 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18548 error_at (type_start_token->location,
18549 "function template %qD redeclared as a class template", id);
18550 type = error_mark_node;
18554 type = TREE_TYPE (id);
18555 type = maybe_process_partial_specialization (type);
18557 if (nested_name_specifier)
18558 pushed_scope = push_scope (nested_name_specifier);
18560 else if (nested_name_specifier)
18566 template <typename T> struct S { struct T };
18567 template <typename T> struct S<T>::T { };
18569 we will get a TYPENAME_TYPE when processing the definition of
18570 `S::T'. We need to resolve it to the actual type before we
18571 try to define it. */
18572 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18574 class_type = resolve_typename_type (TREE_TYPE (type),
18575 /*only_current_p=*/false);
18576 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18577 type = TYPE_NAME (class_type);
18580 cp_parser_error (parser, "could not resolve typename type");
18581 type = error_mark_node;
18585 if (maybe_process_partial_specialization (TREE_TYPE (type))
18586 == error_mark_node)
18592 class_type = current_class_type;
18593 /* Enter the scope indicated by the nested-name-specifier. */
18594 pushed_scope = push_scope (nested_name_specifier);
18595 /* Get the canonical version of this type. */
18596 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18597 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18598 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18600 type = push_template_decl (type);
18601 if (type == error_mark_node)
18608 type = TREE_TYPE (type);
18609 *nested_name_specifier_p = true;
18611 else /* The name is not a nested name. */
18613 /* If the class was unnamed, create a dummy name. */
18615 id = make_anon_name ();
18616 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18617 parser->num_template_parameter_lists);
18620 /* Indicate whether this class was declared as a `class' or as a
18622 if (TREE_CODE (type) == RECORD_TYPE)
18623 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18624 cp_parser_check_class_key (class_key, type);
18626 /* If this type was already complete, and we see another definition,
18627 that's an error. */
18628 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18630 error_at (type_start_token->location, "redefinition of %q#T",
18632 error_at (type_start_token->location, "previous definition of %q+#T",
18637 else if (type == error_mark_node)
18642 /* Apply attributes now, before any use of the class as a template
18643 argument in its base list. */
18644 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18645 fixup_attribute_variants (type);
18648 /* We will have entered the scope containing the class; the names of
18649 base classes should be looked up in that context. For example:
18651 struct A { struct B {}; struct C; };
18652 struct A::C : B {};
18656 /* Get the list of base-classes, if there is one. */
18657 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18658 bases = cp_parser_base_clause (parser);
18662 /* If we're really defining a class, process the base classes.
18663 If they're invalid, fail. */
18664 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18665 && !xref_basetypes (type, bases))
18669 /* Leave the scope given by the nested-name-specifier. We will
18670 enter the class scope itself while processing the members. */
18672 pop_scope (pushed_scope);
18674 if (invalid_explicit_specialization_p)
18676 end_specialization ();
18677 --parser->num_template_parameter_lists;
18681 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18682 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18683 CLASSTYPE_FINAL (type) = 1;
18685 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18689 /* Parse a class-key.
18696 Returns the kind of class-key specified, or none_type to indicate
18699 static enum tag_types
18700 cp_parser_class_key (cp_parser* parser)
18703 enum tag_types tag_type;
18705 /* Look for the class-key. */
18706 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18710 /* Check to see if the TOKEN is a class-key. */
18711 tag_type = cp_parser_token_is_class_key (token);
18713 cp_parser_error (parser, "expected class-key");
18717 /* Parse an (optional) member-specification.
18719 member-specification:
18720 member-declaration member-specification [opt]
18721 access-specifier : member-specification [opt] */
18724 cp_parser_member_specification_opt (cp_parser* parser)
18731 /* Peek at the next token. */
18732 token = cp_lexer_peek_token (parser->lexer);
18733 /* If it's a `}', or EOF then we've seen all the members. */
18734 if (token->type == CPP_CLOSE_BRACE
18735 || token->type == CPP_EOF
18736 || token->type == CPP_PRAGMA_EOL)
18739 /* See if this token is a keyword. */
18740 keyword = token->keyword;
18744 case RID_PROTECTED:
18746 /* Consume the access-specifier. */
18747 cp_lexer_consume_token (parser->lexer);
18748 /* Remember which access-specifier is active. */
18749 current_access_specifier = token->u.value;
18750 /* Look for the `:'. */
18751 cp_parser_require (parser, CPP_COLON, RT_COLON);
18755 /* Accept #pragmas at class scope. */
18756 if (token->type == CPP_PRAGMA)
18758 cp_parser_pragma (parser, pragma_external);
18762 /* Otherwise, the next construction must be a
18763 member-declaration. */
18764 cp_parser_member_declaration (parser);
18769 /* Parse a member-declaration.
18771 member-declaration:
18772 decl-specifier-seq [opt] member-declarator-list [opt] ;
18773 function-definition ; [opt]
18774 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18776 template-declaration
18779 member-declarator-list:
18781 member-declarator-list , member-declarator
18784 declarator pure-specifier [opt]
18785 declarator constant-initializer [opt]
18786 identifier [opt] : constant-expression
18790 member-declaration:
18791 __extension__ member-declaration
18794 declarator attributes [opt] pure-specifier [opt]
18795 declarator attributes [opt] constant-initializer [opt]
18796 identifier [opt] attributes [opt] : constant-expression
18800 member-declaration:
18801 static_assert-declaration */
18804 cp_parser_member_declaration (cp_parser* parser)
18806 cp_decl_specifier_seq decl_specifiers;
18807 tree prefix_attributes;
18809 int declares_class_or_enum;
18811 cp_token *token = NULL;
18812 cp_token *decl_spec_token_start = NULL;
18813 cp_token *initializer_token_start = NULL;
18814 int saved_pedantic;
18815 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18817 /* Check for the `__extension__' keyword. */
18818 if (cp_parser_extension_opt (parser, &saved_pedantic))
18821 cp_parser_member_declaration (parser);
18822 /* Restore the old value of the PEDANTIC flag. */
18823 pedantic = saved_pedantic;
18828 /* Check for a template-declaration. */
18829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18831 /* An explicit specialization here is an error condition, and we
18832 expect the specialization handler to detect and report this. */
18833 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18834 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18835 cp_parser_explicit_specialization (parser);
18837 cp_parser_template_declaration (parser, /*member_p=*/true);
18842 /* Check for a using-declaration. */
18843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18845 if (cxx_dialect < cxx0x)
18847 /* Parse the using-declaration. */
18848 cp_parser_using_declaration (parser,
18849 /*access_declaration_p=*/false);
18855 cp_parser_parse_tentatively (parser);
18856 decl = cp_parser_alias_declaration (parser);
18857 if (cp_parser_parse_definitely (parser))
18858 finish_member_declaration (decl);
18860 cp_parser_using_declaration (parser,
18861 /*access_declaration_p=*/false);
18866 /* Check for @defs. */
18867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18870 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18871 ivar = ivar_chains;
18875 ivar = TREE_CHAIN (member);
18876 TREE_CHAIN (member) = NULL_TREE;
18877 finish_member_declaration (member);
18882 /* If the next token is `static_assert' we have a static assertion. */
18883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18885 cp_parser_static_assert (parser, /*member_p=*/true);
18889 parser->colon_corrects_to_scope_p = false;
18891 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18894 /* Parse the decl-specifier-seq. */
18895 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18896 cp_parser_decl_specifier_seq (parser,
18897 CP_PARSER_FLAGS_OPTIONAL,
18899 &declares_class_or_enum);
18900 prefix_attributes = decl_specifiers.attributes;
18901 decl_specifiers.attributes = NULL_TREE;
18902 /* Check for an invalid type-name. */
18903 if (!decl_specifiers.any_type_specifiers_p
18904 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18906 /* If there is no declarator, then the decl-specifier-seq should
18908 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18910 /* If there was no decl-specifier-seq, and the next token is a
18911 `;', then we have something like:
18917 Each member-declaration shall declare at least one member
18918 name of the class. */
18919 if (!decl_specifiers.any_specifiers_p)
18921 cp_token *token = cp_lexer_peek_token (parser->lexer);
18922 if (!in_system_header_at (token->location))
18923 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
18929 /* See if this declaration is a friend. */
18930 friend_p = cp_parser_friend_p (&decl_specifiers);
18931 /* If there were decl-specifiers, check to see if there was
18932 a class-declaration. */
18933 type = check_tag_decl (&decl_specifiers);
18934 /* Nested classes have already been added to the class, but
18935 a `friend' needs to be explicitly registered. */
18938 /* If the `friend' keyword was present, the friend must
18939 be introduced with a class-key. */
18940 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18941 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
18942 "in C++03 a class-key must be used "
18943 "when declaring a friend");
18946 template <typename T> struct A {
18947 friend struct A<T>::B;
18950 A<T>::B will be represented by a TYPENAME_TYPE, and
18951 therefore not recognized by check_tag_decl. */
18954 type = decl_specifiers.type;
18955 if (type && TREE_CODE (type) == TYPE_DECL)
18956 type = TREE_TYPE (type);
18958 if (!type || !TYPE_P (type))
18959 error_at (decl_spec_token_start->location,
18960 "friend declaration does not name a class or "
18963 make_friend_class (current_class_type, type,
18964 /*complain=*/true);
18966 /* If there is no TYPE, an error message will already have
18968 else if (!type || type == error_mark_node)
18970 /* An anonymous aggregate has to be handled specially; such
18971 a declaration really declares a data member (with a
18972 particular type), as opposed to a nested class. */
18973 else if (ANON_AGGR_TYPE_P (type))
18976 if (decl_specifiers.storage_class != sc_none)
18977 error_at (decl_spec_token_start->location,
18978 "a storage class on an anonymous aggregate "
18979 "in class scope is not allowed");
18981 /* Remove constructors and such from TYPE, now that we
18982 know it is an anonymous aggregate. */
18983 fixup_anonymous_aggr (type);
18984 /* And make the corresponding data member. */
18985 decl = build_decl (decl_spec_token_start->location,
18986 FIELD_DECL, NULL_TREE, type);
18987 /* Add it to the class. */
18988 finish_member_declaration (decl);
18991 cp_parser_check_access_in_redeclaration
18993 decl_spec_token_start->location);
18998 bool assume_semicolon = false;
19000 /* See if these declarations will be friends. */
19001 friend_p = cp_parser_friend_p (&decl_specifiers);
19003 /* Keep going until we hit the `;' at the end of the
19005 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19007 tree attributes = NULL_TREE;
19008 tree first_attribute;
19010 /* Peek at the next token. */
19011 token = cp_lexer_peek_token (parser->lexer);
19013 /* Check for a bitfield declaration. */
19014 if (token->type == CPP_COLON
19015 || (token->type == CPP_NAME
19016 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19022 /* Get the name of the bitfield. Note that we cannot just
19023 check TOKEN here because it may have been invalidated by
19024 the call to cp_lexer_peek_nth_token above. */
19025 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19026 identifier = cp_parser_identifier (parser);
19028 identifier = NULL_TREE;
19030 /* Consume the `:' token. */
19031 cp_lexer_consume_token (parser->lexer);
19032 /* Get the width of the bitfield. */
19034 = cp_parser_constant_expression (parser,
19035 /*allow_non_constant=*/false,
19038 /* Look for attributes that apply to the bitfield. */
19039 attributes = cp_parser_attributes_opt (parser);
19040 /* Remember which attributes are prefix attributes and
19042 first_attribute = attributes;
19043 /* Combine the attributes. */
19044 attributes = chainon (prefix_attributes, attributes);
19046 /* Create the bitfield declaration. */
19047 decl = grokbitfield (identifier
19048 ? make_id_declarator (NULL_TREE,
19058 cp_declarator *declarator;
19060 tree asm_specification;
19061 int ctor_dtor_or_conv_p;
19063 /* Parse the declarator. */
19065 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19066 &ctor_dtor_or_conv_p,
19067 /*parenthesized_p=*/NULL,
19068 /*member_p=*/true);
19070 /* If something went wrong parsing the declarator, make sure
19071 that we at least consume some tokens. */
19072 if (declarator == cp_error_declarator)
19074 /* Skip to the end of the statement. */
19075 cp_parser_skip_to_end_of_statement (parser);
19076 /* If the next token is not a semicolon, that is
19077 probably because we just skipped over the body of
19078 a function. So, we consume a semicolon if
19079 present, but do not issue an error message if it
19081 if (cp_lexer_next_token_is (parser->lexer,
19083 cp_lexer_consume_token (parser->lexer);
19087 if (declares_class_or_enum & 2)
19088 cp_parser_check_for_definition_in_return_type
19089 (declarator, decl_specifiers.type,
19090 decl_specifiers.locations[ds_type_spec]);
19092 /* Look for an asm-specification. */
19093 asm_specification = cp_parser_asm_specification_opt (parser);
19094 /* Look for attributes that apply to the declaration. */
19095 attributes = cp_parser_attributes_opt (parser);
19096 /* Remember which attributes are prefix attributes and
19098 first_attribute = attributes;
19099 /* Combine the attributes. */
19100 attributes = chainon (prefix_attributes, attributes);
19102 /* If it's an `=', then we have a constant-initializer or a
19103 pure-specifier. It is not correct to parse the
19104 initializer before registering the member declaration
19105 since the member declaration should be in scope while
19106 its initializer is processed. However, the rest of the
19107 front end does not yet provide an interface that allows
19108 us to handle this correctly. */
19109 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19113 A pure-specifier shall be used only in the declaration of
19114 a virtual function.
19116 A member-declarator can contain a constant-initializer
19117 only if it declares a static member of integral or
19120 Therefore, if the DECLARATOR is for a function, we look
19121 for a pure-specifier; otherwise, we look for a
19122 constant-initializer. When we call `grokfield', it will
19123 perform more stringent semantics checks. */
19124 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19125 if (function_declarator_p (declarator)
19126 || (decl_specifiers.type
19127 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19128 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19129 == FUNCTION_TYPE)))
19130 initializer = cp_parser_pure_specifier (parser);
19131 else if (decl_specifiers.storage_class != sc_static)
19132 initializer = cp_parser_save_nsdmi (parser);
19133 else if (cxx_dialect >= cxx0x)
19136 /* Don't require a constant rvalue in C++11, since we
19137 might want a reference constant. We'll enforce
19138 constancy later. */
19139 cp_lexer_consume_token (parser->lexer);
19140 /* Parse the initializer. */
19141 initializer = cp_parser_initializer_clause (parser,
19145 /* Parse the initializer. */
19146 initializer = cp_parser_constant_initializer (parser);
19148 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19149 && !function_declarator_p (declarator))
19152 if (decl_specifiers.storage_class != sc_static)
19153 initializer = cp_parser_save_nsdmi (parser);
19155 initializer = cp_parser_initializer (parser, &x, &x);
19157 /* Otherwise, there is no initializer. */
19159 initializer = NULL_TREE;
19161 /* See if we are probably looking at a function
19162 definition. We are certainly not looking at a
19163 member-declarator. Calling `grokfield' has
19164 side-effects, so we must not do it unless we are sure
19165 that we are looking at a member-declarator. */
19166 if (cp_parser_token_starts_function_definition_p
19167 (cp_lexer_peek_token (parser->lexer)))
19169 /* The grammar does not allow a pure-specifier to be
19170 used when a member function is defined. (It is
19171 possible that this fact is an oversight in the
19172 standard, since a pure function may be defined
19173 outside of the class-specifier. */
19174 if (initializer && initializer_token_start)
19175 error_at (initializer_token_start->location,
19176 "pure-specifier on function-definition");
19177 decl = cp_parser_save_member_function_body (parser,
19181 /* If the member was not a friend, declare it here. */
19183 finish_member_declaration (decl);
19184 /* Peek at the next token. */
19185 token = cp_lexer_peek_token (parser->lexer);
19186 /* If the next token is a semicolon, consume it. */
19187 if (token->type == CPP_SEMICOLON)
19188 cp_lexer_consume_token (parser->lexer);
19192 if (declarator->kind == cdk_function)
19193 declarator->id_loc = token->location;
19194 /* Create the declaration. */
19195 decl = grokfield (declarator, &decl_specifiers,
19196 initializer, /*init_const_expr_p=*/true,
19201 /* Reset PREFIX_ATTRIBUTES. */
19202 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19203 attributes = TREE_CHAIN (attributes);
19205 TREE_CHAIN (attributes) = NULL_TREE;
19207 /* If there is any qualification still in effect, clear it
19208 now; we will be starting fresh with the next declarator. */
19209 parser->scope = NULL_TREE;
19210 parser->qualifying_scope = NULL_TREE;
19211 parser->object_scope = NULL_TREE;
19212 /* If it's a `,', then there are more declarators. */
19213 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19214 cp_lexer_consume_token (parser->lexer);
19215 /* If the next token isn't a `;', then we have a parse error. */
19216 else if (cp_lexer_next_token_is_not (parser->lexer,
19219 /* The next token might be a ways away from where the
19220 actual semicolon is missing. Find the previous token
19221 and use that for our error position. */
19222 cp_token *token = cp_lexer_previous_token (parser->lexer);
19223 error_at (token->location,
19224 "expected %<;%> at end of member declaration");
19226 /* Assume that the user meant to provide a semicolon. If
19227 we were to cp_parser_skip_to_end_of_statement, we might
19228 skip to a semicolon inside a member function definition
19229 and issue nonsensical error messages. */
19230 assume_semicolon = true;
19235 /* Add DECL to the list of members. */
19237 finish_member_declaration (decl);
19239 if (TREE_CODE (decl) == FUNCTION_DECL)
19240 cp_parser_save_default_args (parser, decl);
19241 else if (TREE_CODE (decl) == FIELD_DECL
19242 && !DECL_C_BIT_FIELD (decl)
19243 && DECL_INITIAL (decl))
19244 /* Add DECL to the queue of NSDMI to be parsed later. */
19245 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19248 if (assume_semicolon)
19253 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19255 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19258 /* Parse a pure-specifier.
19263 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19264 Otherwise, ERROR_MARK_NODE is returned. */
19267 cp_parser_pure_specifier (cp_parser* parser)
19271 /* Look for the `=' token. */
19272 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19273 return error_mark_node;
19274 /* Look for the `0' token. */
19275 token = cp_lexer_peek_token (parser->lexer);
19277 if (token->type == CPP_EOF
19278 || token->type == CPP_PRAGMA_EOL)
19279 return error_mark_node;
19281 cp_lexer_consume_token (parser->lexer);
19283 /* Accept = default or = delete in c++0x mode. */
19284 if (token->keyword == RID_DEFAULT
19285 || token->keyword == RID_DELETE)
19287 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19288 return token->u.value;
19291 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19292 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19294 cp_parser_error (parser,
19295 "invalid pure specifier (only %<= 0%> is allowed)");
19296 cp_parser_skip_to_end_of_statement (parser);
19297 return error_mark_node;
19299 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19301 error_at (token->location, "templates may not be %<virtual%>");
19302 return error_mark_node;
19305 return integer_zero_node;
19308 /* Parse a constant-initializer.
19310 constant-initializer:
19311 = constant-expression
19313 Returns a representation of the constant-expression. */
19316 cp_parser_constant_initializer (cp_parser* parser)
19318 /* Look for the `=' token. */
19319 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19320 return error_mark_node;
19322 /* It is invalid to write:
19324 struct S { static const int i = { 7 }; };
19327 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19329 cp_parser_error (parser,
19330 "a brace-enclosed initializer is not allowed here");
19331 /* Consume the opening brace. */
19332 cp_lexer_consume_token (parser->lexer);
19333 /* Skip the initializer. */
19334 cp_parser_skip_to_closing_brace (parser);
19335 /* Look for the trailing `}'. */
19336 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19338 return error_mark_node;
19341 return cp_parser_constant_expression (parser,
19342 /*allow_non_constant=*/false,
19346 /* Derived classes [gram.class.derived] */
19348 /* Parse a base-clause.
19351 : base-specifier-list
19353 base-specifier-list:
19354 base-specifier ... [opt]
19355 base-specifier-list , base-specifier ... [opt]
19357 Returns a TREE_LIST representing the base-classes, in the order in
19358 which they were declared. The representation of each node is as
19359 described by cp_parser_base_specifier.
19361 In the case that no bases are specified, this function will return
19362 NULL_TREE, not ERROR_MARK_NODE. */
19365 cp_parser_base_clause (cp_parser* parser)
19367 tree bases = NULL_TREE;
19369 /* Look for the `:' that begins the list. */
19370 cp_parser_require (parser, CPP_COLON, RT_COLON);
19372 /* Scan the base-specifier-list. */
19377 bool pack_expansion_p = false;
19379 /* Look for the base-specifier. */
19380 base = cp_parser_base_specifier (parser);
19381 /* Look for the (optional) ellipsis. */
19382 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19384 /* Consume the `...'. */
19385 cp_lexer_consume_token (parser->lexer);
19387 pack_expansion_p = true;
19390 /* Add BASE to the front of the list. */
19391 if (base && base != error_mark_node)
19393 if (pack_expansion_p)
19394 /* Make this a pack expansion type. */
19395 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19397 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19399 TREE_CHAIN (base) = bases;
19403 /* Peek at the next token. */
19404 token = cp_lexer_peek_token (parser->lexer);
19405 /* If it's not a comma, then the list is complete. */
19406 if (token->type != CPP_COMMA)
19408 /* Consume the `,'. */
19409 cp_lexer_consume_token (parser->lexer);
19412 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19413 base class had a qualified name. However, the next name that
19414 appears is certainly not qualified. */
19415 parser->scope = NULL_TREE;
19416 parser->qualifying_scope = NULL_TREE;
19417 parser->object_scope = NULL_TREE;
19419 return nreverse (bases);
19422 /* Parse a base-specifier.
19425 :: [opt] nested-name-specifier [opt] class-name
19426 virtual access-specifier [opt] :: [opt] nested-name-specifier
19428 access-specifier virtual [opt] :: [opt] nested-name-specifier
19431 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19432 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19433 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19434 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19437 cp_parser_base_specifier (cp_parser* parser)
19441 bool virtual_p = false;
19442 bool duplicate_virtual_error_issued_p = false;
19443 bool duplicate_access_error_issued_p = false;
19444 bool class_scope_p, template_p;
19445 tree access = access_default_node;
19448 /* Process the optional `virtual' and `access-specifier'. */
19451 /* Peek at the next token. */
19452 token = cp_lexer_peek_token (parser->lexer);
19453 /* Process `virtual'. */
19454 switch (token->keyword)
19457 /* If `virtual' appears more than once, issue an error. */
19458 if (virtual_p && !duplicate_virtual_error_issued_p)
19460 cp_parser_error (parser,
19461 "%<virtual%> specified more than once in base-specified");
19462 duplicate_virtual_error_issued_p = true;
19467 /* Consume the `virtual' token. */
19468 cp_lexer_consume_token (parser->lexer);
19473 case RID_PROTECTED:
19475 /* If more than one access specifier appears, issue an
19477 if (access != access_default_node
19478 && !duplicate_access_error_issued_p)
19480 cp_parser_error (parser,
19481 "more than one access specifier in base-specified");
19482 duplicate_access_error_issued_p = true;
19485 access = ridpointers[(int) token->keyword];
19487 /* Consume the access-specifier. */
19488 cp_lexer_consume_token (parser->lexer);
19497 /* It is not uncommon to see programs mechanically, erroneously, use
19498 the 'typename' keyword to denote (dependent) qualified types
19499 as base classes. */
19500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19502 token = cp_lexer_peek_token (parser->lexer);
19503 if (!processing_template_decl)
19504 error_at (token->location,
19505 "keyword %<typename%> not allowed outside of templates");
19507 error_at (token->location,
19508 "keyword %<typename%> not allowed in this context "
19509 "(the base class is implicitly a type)");
19510 cp_lexer_consume_token (parser->lexer);
19513 /* Look for the optional `::' operator. */
19514 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19515 /* Look for the nested-name-specifier. The simplest way to
19520 The keyword `typename' is not permitted in a base-specifier or
19521 mem-initializer; in these contexts a qualified name that
19522 depends on a template-parameter is implicitly assumed to be a
19525 is to pretend that we have seen the `typename' keyword at this
19527 cp_parser_nested_name_specifier_opt (parser,
19528 /*typename_keyword_p=*/true,
19529 /*check_dependency_p=*/true,
19531 /*is_declaration=*/true);
19532 /* If the base class is given by a qualified name, assume that names
19533 we see are type names or templates, as appropriate. */
19534 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19535 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19538 && cp_lexer_next_token_is_decltype (parser->lexer))
19539 /* DR 950 allows decltype as a base-specifier. */
19540 type = cp_parser_decltype (parser);
19543 /* Otherwise, look for the class-name. */
19544 type = cp_parser_class_name (parser,
19548 /*check_dependency_p=*/true,
19549 /*class_head_p=*/false,
19550 /*is_declaration=*/true);
19551 type = TREE_TYPE (type);
19554 if (type == error_mark_node)
19555 return error_mark_node;
19557 return finish_base_specifier (type, access, virtual_p);
19560 /* Exception handling [gram.exception] */
19562 /* Parse an (optional) noexcept-specification.
19564 noexcept-specification:
19565 noexcept ( constant-expression ) [opt]
19567 If no noexcept-specification is present, returns NULL_TREE.
19568 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19569 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19570 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19571 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19572 in which case a boolean condition is returned instead. */
19575 cp_parser_noexcept_specification_opt (cp_parser* parser,
19576 bool require_constexpr,
19577 bool* consumed_expr,
19581 const char *saved_message;
19583 /* Peek at the next token. */
19584 token = cp_lexer_peek_token (parser->lexer);
19586 /* Is it a noexcept-specification? */
19587 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19590 cp_lexer_consume_token (parser->lexer);
19592 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19594 cp_lexer_consume_token (parser->lexer);
19596 if (require_constexpr)
19598 /* Types may not be defined in an exception-specification. */
19599 saved_message = parser->type_definition_forbidden_message;
19600 parser->type_definition_forbidden_message
19601 = G_("types may not be defined in an exception-specification");
19603 expr = cp_parser_constant_expression (parser, false, NULL);
19605 /* Restore the saved message. */
19606 parser->type_definition_forbidden_message = saved_message;
19610 expr = cp_parser_expression (parser, false, NULL);
19611 *consumed_expr = true;
19614 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19618 expr = boolean_true_node;
19619 if (!require_constexpr)
19620 *consumed_expr = false;
19623 /* We cannot build a noexcept-spec right away because this will check
19624 that expr is a constexpr. */
19626 return build_noexcept_spec (expr, tf_warning_or_error);
19634 /* Parse an (optional) exception-specification.
19636 exception-specification:
19637 throw ( type-id-list [opt] )
19639 Returns a TREE_LIST representing the exception-specification. The
19640 TREE_VALUE of each node is a type. */
19643 cp_parser_exception_specification_opt (cp_parser* parser)
19647 const char *saved_message;
19649 /* Peek at the next token. */
19650 token = cp_lexer_peek_token (parser->lexer);
19652 /* Is it a noexcept-specification? */
19653 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19655 if (type_id_list != NULL_TREE)
19656 return type_id_list;
19658 /* If it's not `throw', then there's no exception-specification. */
19659 if (!cp_parser_is_keyword (token, RID_THROW))
19663 /* Enable this once a lot of code has transitioned to noexcept? */
19664 if (cxx_dialect >= cxx0x && !in_system_header)
19665 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19666 "deprecated in C++0x; use %<noexcept%> instead");
19669 /* Consume the `throw'. */
19670 cp_lexer_consume_token (parser->lexer);
19672 /* Look for the `('. */
19673 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19675 /* Peek at the next token. */
19676 token = cp_lexer_peek_token (parser->lexer);
19677 /* If it's not a `)', then there is a type-id-list. */
19678 if (token->type != CPP_CLOSE_PAREN)
19680 /* Types may not be defined in an exception-specification. */
19681 saved_message = parser->type_definition_forbidden_message;
19682 parser->type_definition_forbidden_message
19683 = G_("types may not be defined in an exception-specification");
19684 /* Parse the type-id-list. */
19685 type_id_list = cp_parser_type_id_list (parser);
19686 /* Restore the saved message. */
19687 parser->type_definition_forbidden_message = saved_message;
19690 type_id_list = empty_except_spec;
19692 /* Look for the `)'. */
19693 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19695 return type_id_list;
19698 /* Parse an (optional) type-id-list.
19702 type-id-list , type-id ... [opt]
19704 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19705 in the order that the types were presented. */
19708 cp_parser_type_id_list (cp_parser* parser)
19710 tree types = NULL_TREE;
19717 /* Get the next type-id. */
19718 type = cp_parser_type_id (parser);
19719 /* Parse the optional ellipsis. */
19720 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19722 /* Consume the `...'. */
19723 cp_lexer_consume_token (parser->lexer);
19725 /* Turn the type into a pack expansion expression. */
19726 type = make_pack_expansion (type);
19728 /* Add it to the list. */
19729 types = add_exception_specifier (types, type, /*complain=*/1);
19730 /* Peek at the next token. */
19731 token = cp_lexer_peek_token (parser->lexer);
19732 /* If it is not a `,', we are done. */
19733 if (token->type != CPP_COMMA)
19735 /* Consume the `,'. */
19736 cp_lexer_consume_token (parser->lexer);
19739 return nreverse (types);
19742 /* Parse a try-block.
19745 try compound-statement handler-seq */
19748 cp_parser_try_block (cp_parser* parser)
19752 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19753 try_block = begin_try_block ();
19754 cp_parser_compound_statement (parser, NULL, true, false);
19755 finish_try_block (try_block);
19756 cp_parser_handler_seq (parser);
19757 finish_handler_sequence (try_block);
19762 /* Parse a function-try-block.
19764 function-try-block:
19765 try ctor-initializer [opt] function-body handler-seq */
19768 cp_parser_function_try_block (cp_parser* parser)
19770 tree compound_stmt;
19772 bool ctor_initializer_p;
19774 /* Look for the `try' keyword. */
19775 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19777 /* Let the rest of the front end know where we are. */
19778 try_block = begin_function_try_block (&compound_stmt);
19779 /* Parse the function-body. */
19780 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19781 (parser, /*in_function_try_block=*/true);
19782 /* We're done with the `try' part. */
19783 finish_function_try_block (try_block);
19784 /* Parse the handlers. */
19785 cp_parser_handler_seq (parser);
19786 /* We're done with the handlers. */
19787 finish_function_handler_sequence (try_block, compound_stmt);
19789 return ctor_initializer_p;
19792 /* Parse a handler-seq.
19795 handler handler-seq [opt] */
19798 cp_parser_handler_seq (cp_parser* parser)
19804 /* Parse the handler. */
19805 cp_parser_handler (parser);
19806 /* Peek at the next token. */
19807 token = cp_lexer_peek_token (parser->lexer);
19808 /* If it's not `catch' then there are no more handlers. */
19809 if (!cp_parser_is_keyword (token, RID_CATCH))
19814 /* Parse a handler.
19817 catch ( exception-declaration ) compound-statement */
19820 cp_parser_handler (cp_parser* parser)
19825 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19826 handler = begin_handler ();
19827 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19828 declaration = cp_parser_exception_declaration (parser);
19829 finish_handler_parms (declaration, handler);
19830 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19831 cp_parser_compound_statement (parser, NULL, false, false);
19832 finish_handler (handler);
19835 /* Parse an exception-declaration.
19837 exception-declaration:
19838 type-specifier-seq declarator
19839 type-specifier-seq abstract-declarator
19843 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19844 ellipsis variant is used. */
19847 cp_parser_exception_declaration (cp_parser* parser)
19849 cp_decl_specifier_seq type_specifiers;
19850 cp_declarator *declarator;
19851 const char *saved_message;
19853 /* If it's an ellipsis, it's easy to handle. */
19854 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19856 /* Consume the `...' token. */
19857 cp_lexer_consume_token (parser->lexer);
19861 /* Types may not be defined in exception-declarations. */
19862 saved_message = parser->type_definition_forbidden_message;
19863 parser->type_definition_forbidden_message
19864 = G_("types may not be defined in exception-declarations");
19866 /* Parse the type-specifier-seq. */
19867 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19868 /*is_trailing_return=*/false,
19870 /* If it's a `)', then there is no declarator. */
19871 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19874 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19875 /*ctor_dtor_or_conv_p=*/NULL,
19876 /*parenthesized_p=*/NULL,
19877 /*member_p=*/false);
19879 /* Restore the saved message. */
19880 parser->type_definition_forbidden_message = saved_message;
19882 if (!type_specifiers.any_specifiers_p)
19883 return error_mark_node;
19885 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19888 /* Parse a throw-expression.
19891 throw assignment-expression [opt]
19893 Returns a THROW_EXPR representing the throw-expression. */
19896 cp_parser_throw_expression (cp_parser* parser)
19901 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19902 token = cp_lexer_peek_token (parser->lexer);
19903 /* Figure out whether or not there is an assignment-expression
19904 following the "throw" keyword. */
19905 if (token->type == CPP_COMMA
19906 || token->type == CPP_SEMICOLON
19907 || token->type == CPP_CLOSE_PAREN
19908 || token->type == CPP_CLOSE_SQUARE
19909 || token->type == CPP_CLOSE_BRACE
19910 || token->type == CPP_COLON)
19911 expression = NULL_TREE;
19913 expression = cp_parser_assignment_expression (parser,
19914 /*cast_p=*/false, NULL);
19916 return build_throw (expression);
19919 /* GNU Extensions */
19921 /* Parse an (optional) asm-specification.
19924 asm ( string-literal )
19926 If the asm-specification is present, returns a STRING_CST
19927 corresponding to the string-literal. Otherwise, returns
19931 cp_parser_asm_specification_opt (cp_parser* parser)
19934 tree asm_specification;
19936 /* Peek at the next token. */
19937 token = cp_lexer_peek_token (parser->lexer);
19938 /* If the next token isn't the `asm' keyword, then there's no
19939 asm-specification. */
19940 if (!cp_parser_is_keyword (token, RID_ASM))
19943 /* Consume the `asm' token. */
19944 cp_lexer_consume_token (parser->lexer);
19945 /* Look for the `('. */
19946 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19948 /* Look for the string-literal. */
19949 asm_specification = cp_parser_string_literal (parser, false, false);
19951 /* Look for the `)'. */
19952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19954 return asm_specification;
19957 /* Parse an asm-operand-list.
19961 asm-operand-list , asm-operand
19964 string-literal ( expression )
19965 [ string-literal ] string-literal ( expression )
19967 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19968 each node is the expression. The TREE_PURPOSE is itself a
19969 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19970 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19971 is a STRING_CST for the string literal before the parenthesis. Returns
19972 ERROR_MARK_NODE if any of the operands are invalid. */
19975 cp_parser_asm_operand_list (cp_parser* parser)
19977 tree asm_operands = NULL_TREE;
19978 bool invalid_operands = false;
19982 tree string_literal;
19986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19988 /* Consume the `[' token. */
19989 cp_lexer_consume_token (parser->lexer);
19990 /* Read the operand name. */
19991 name = cp_parser_identifier (parser);
19992 if (name != error_mark_node)
19993 name = build_string (IDENTIFIER_LENGTH (name),
19994 IDENTIFIER_POINTER (name));
19995 /* Look for the closing `]'. */
19996 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20000 /* Look for the string-literal. */
20001 string_literal = cp_parser_string_literal (parser, false, false);
20003 /* Look for the `('. */
20004 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20005 /* Parse the expression. */
20006 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20007 /* Look for the `)'. */
20008 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20010 if (name == error_mark_node
20011 || string_literal == error_mark_node
20012 || expression == error_mark_node)
20013 invalid_operands = true;
20015 /* Add this operand to the list. */
20016 asm_operands = tree_cons (build_tree_list (name, string_literal),
20019 /* If the next token is not a `,', there are no more
20021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20023 /* Consume the `,'. */
20024 cp_lexer_consume_token (parser->lexer);
20027 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20030 /* Parse an asm-clobber-list.
20034 asm-clobber-list , string-literal
20036 Returns a TREE_LIST, indicating the clobbers in the order that they
20037 appeared. The TREE_VALUE of each node is a STRING_CST. */
20040 cp_parser_asm_clobber_list (cp_parser* parser)
20042 tree clobbers = NULL_TREE;
20046 tree string_literal;
20048 /* Look for the string literal. */
20049 string_literal = cp_parser_string_literal (parser, false, false);
20050 /* Add it to the list. */
20051 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20052 /* If the next token is not a `,', then the list is
20054 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20056 /* Consume the `,' token. */
20057 cp_lexer_consume_token (parser->lexer);
20063 /* Parse an asm-label-list.
20067 asm-label-list , identifier
20069 Returns a TREE_LIST, indicating the labels in the order that they
20070 appeared. The TREE_VALUE of each node is a label. */
20073 cp_parser_asm_label_list (cp_parser* parser)
20075 tree labels = NULL_TREE;
20079 tree identifier, label, name;
20081 /* Look for the identifier. */
20082 identifier = cp_parser_identifier (parser);
20083 if (!error_operand_p (identifier))
20085 label = lookup_label (identifier);
20086 if (TREE_CODE (label) == LABEL_DECL)
20088 TREE_USED (label) = 1;
20089 check_goto (label);
20090 name = build_string (IDENTIFIER_LENGTH (identifier),
20091 IDENTIFIER_POINTER (identifier));
20092 labels = tree_cons (name, label, labels);
20095 /* If the next token is not a `,', then the list is
20097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20099 /* Consume the `,' token. */
20100 cp_lexer_consume_token (parser->lexer);
20103 return nreverse (labels);
20106 /* Parse an (optional) series of attributes.
20109 attributes attribute
20112 __attribute__ (( attribute-list [opt] ))
20114 The return value is as for cp_parser_attribute_list. */
20117 cp_parser_attributes_opt (cp_parser* parser)
20119 tree attributes = NULL_TREE;
20124 tree attribute_list;
20127 /* Peek at the next token. */
20128 token = cp_lexer_peek_token (parser->lexer);
20129 /* If it's not `__attribute__', then we're done. */
20130 if (token->keyword != RID_ATTRIBUTE)
20133 /* Consume the `__attribute__' keyword. */
20134 cp_lexer_consume_token (parser->lexer);
20135 /* Look for the two `(' tokens. */
20136 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20139 /* Peek at the next token. */
20140 token = cp_lexer_peek_token (parser->lexer);
20141 if (token->type != CPP_CLOSE_PAREN)
20142 /* Parse the attribute-list. */
20143 attribute_list = cp_parser_attribute_list (parser);
20145 /* If the next token is a `)', then there is no attribute
20147 attribute_list = NULL;
20149 /* Look for the two `)' tokens. */
20150 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20152 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20155 cp_parser_skip_to_end_of_statement (parser);
20157 /* Add these new attributes to the list. */
20158 attributes = chainon (attributes, attribute_list);
20164 /* Parse an attribute-list.
20168 attribute-list , attribute
20172 identifier ( identifier )
20173 identifier ( identifier , expression-list )
20174 identifier ( expression-list )
20176 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20177 to an attribute. The TREE_PURPOSE of each node is the identifier
20178 indicating which attribute is in use. The TREE_VALUE represents
20179 the arguments, if any. */
20182 cp_parser_attribute_list (cp_parser* parser)
20184 tree attribute_list = NULL_TREE;
20185 bool save_translate_strings_p = parser->translate_strings_p;
20187 parser->translate_strings_p = false;
20194 /* Look for the identifier. We also allow keywords here; for
20195 example `__attribute__ ((const))' is legal. */
20196 token = cp_lexer_peek_token (parser->lexer);
20197 if (token->type == CPP_NAME
20198 || token->type == CPP_KEYWORD)
20200 tree arguments = NULL_TREE;
20202 /* Consume the token. */
20203 token = cp_lexer_consume_token (parser->lexer);
20205 /* Save away the identifier that indicates which attribute
20207 identifier = (token->type == CPP_KEYWORD)
20208 /* For keywords, use the canonical spelling, not the
20209 parsed identifier. */
20210 ? ridpointers[(int) token->keyword]
20213 attribute = build_tree_list (identifier, NULL_TREE);
20215 /* Peek at the next token. */
20216 token = cp_lexer_peek_token (parser->lexer);
20217 /* If it's an `(', then parse the attribute arguments. */
20218 if (token->type == CPP_OPEN_PAREN)
20221 int attr_flag = (attribute_takes_identifier_p (identifier)
20222 ? id_attr : normal_attr);
20223 vec = cp_parser_parenthesized_expression_list
20224 (parser, attr_flag, /*cast_p=*/false,
20225 /*allow_expansion_p=*/false,
20226 /*non_constant_p=*/NULL);
20228 arguments = error_mark_node;
20231 arguments = build_tree_list_vec (vec);
20232 release_tree_vector (vec);
20234 /* Save the arguments away. */
20235 TREE_VALUE (attribute) = arguments;
20238 if (arguments != error_mark_node)
20240 /* Add this attribute to the list. */
20241 TREE_CHAIN (attribute) = attribute_list;
20242 attribute_list = attribute;
20245 token = cp_lexer_peek_token (parser->lexer);
20247 /* Now, look for more attributes. If the next token isn't a
20248 `,', we're done. */
20249 if (token->type != CPP_COMMA)
20252 /* Consume the comma and keep going. */
20253 cp_lexer_consume_token (parser->lexer);
20255 parser->translate_strings_p = save_translate_strings_p;
20257 /* We built up the list in reverse order. */
20258 return nreverse (attribute_list);
20261 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20262 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20263 current value of the PEDANTIC flag, regardless of whether or not
20264 the `__extension__' keyword is present. The caller is responsible
20265 for restoring the value of the PEDANTIC flag. */
20268 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20270 /* Save the old value of the PEDANTIC flag. */
20271 *saved_pedantic = pedantic;
20273 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20275 /* Consume the `__extension__' token. */
20276 cp_lexer_consume_token (parser->lexer);
20277 /* We're not being pedantic while the `__extension__' keyword is
20287 /* Parse a label declaration.
20290 __label__ label-declarator-seq ;
20292 label-declarator-seq:
20293 identifier , label-declarator-seq
20297 cp_parser_label_declaration (cp_parser* parser)
20299 /* Look for the `__label__' keyword. */
20300 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20306 /* Look for an identifier. */
20307 identifier = cp_parser_identifier (parser);
20308 /* If we failed, stop. */
20309 if (identifier == error_mark_node)
20311 /* Declare it as a label. */
20312 finish_label_decl (identifier);
20313 /* If the next token is a `;', stop. */
20314 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20316 /* Look for the `,' separating the label declarations. */
20317 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20320 /* Look for the final `;'. */
20321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20324 /* Support Functions */
20326 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20327 NAME should have one of the representations used for an
20328 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20329 is returned. If PARSER->SCOPE is a dependent type, then a
20330 SCOPE_REF is returned.
20332 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20333 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20334 was formed. Abstractly, such entities should not be passed to this
20335 function, because they do not need to be looked up, but it is
20336 simpler to check for this special case here, rather than at the
20339 In cases not explicitly covered above, this function returns a
20340 DECL, OVERLOAD, or baselink representing the result of the lookup.
20341 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20344 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20345 (e.g., "struct") that was used. In that case bindings that do not
20346 refer to types are ignored.
20348 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20351 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20354 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20357 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20358 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20359 NULL_TREE otherwise. */
20362 cp_parser_lookup_name (cp_parser *parser, tree name,
20363 enum tag_types tag_type,
20366 bool check_dependency,
20367 tree *ambiguous_decls,
20368 location_t name_location)
20371 tree object_type = parser->context->object_type;
20373 /* Assume that the lookup will be unambiguous. */
20374 if (ambiguous_decls)
20375 *ambiguous_decls = NULL_TREE;
20377 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20378 no longer valid. Note that if we are parsing tentatively, and
20379 the parse fails, OBJECT_TYPE will be automatically restored. */
20380 parser->context->object_type = NULL_TREE;
20382 if (name == error_mark_node)
20383 return error_mark_node;
20385 /* A template-id has already been resolved; there is no lookup to
20387 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20389 if (BASELINK_P (name))
20391 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20392 == TEMPLATE_ID_EXPR);
20396 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20397 it should already have been checked to make sure that the name
20398 used matches the type being destroyed. */
20399 if (TREE_CODE (name) == BIT_NOT_EXPR)
20403 /* Figure out to which type this destructor applies. */
20405 type = parser->scope;
20406 else if (object_type)
20407 type = object_type;
20409 type = current_class_type;
20410 /* If that's not a class type, there is no destructor. */
20411 if (!type || !CLASS_TYPE_P (type))
20412 return error_mark_node;
20413 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20414 lazily_declare_fn (sfk_destructor, type);
20415 if (!CLASSTYPE_DESTRUCTORS (type))
20416 return error_mark_node;
20417 /* If it was a class type, return the destructor. */
20418 return CLASSTYPE_DESTRUCTORS (type);
20421 /* By this point, the NAME should be an ordinary identifier. If
20422 the id-expression was a qualified name, the qualifying scope is
20423 stored in PARSER->SCOPE at this point. */
20424 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20426 /* Perform the lookup. */
20431 if (parser->scope == error_mark_node)
20432 return error_mark_node;
20434 /* If the SCOPE is dependent, the lookup must be deferred until
20435 the template is instantiated -- unless we are explicitly
20436 looking up names in uninstantiated templates. Even then, we
20437 cannot look up the name if the scope is not a class type; it
20438 might, for example, be a template type parameter. */
20439 dependent_p = (TYPE_P (parser->scope)
20440 && dependent_scope_p (parser->scope));
20441 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20443 /* Defer lookup. */
20444 decl = error_mark_node;
20447 tree pushed_scope = NULL_TREE;
20449 /* If PARSER->SCOPE is a dependent type, then it must be a
20450 class type, and we must not be checking dependencies;
20451 otherwise, we would have processed this lookup above. So
20452 that PARSER->SCOPE is not considered a dependent base by
20453 lookup_member, we must enter the scope here. */
20455 pushed_scope = push_scope (parser->scope);
20457 /* If the PARSER->SCOPE is a template specialization, it
20458 may be instantiated during name lookup. In that case,
20459 errors may be issued. Even if we rollback the current
20460 tentative parse, those errors are valid. */
20461 decl = lookup_qualified_name (parser->scope, name,
20462 tag_type != none_type,
20463 /*complain=*/true);
20465 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20466 lookup result and the nested-name-specifier nominates a class C:
20467 * if the name specified after the nested-name-specifier, when
20468 looked up in C, is the injected-class-name of C (Clause 9), or
20469 * if the name specified after the nested-name-specifier is the
20470 same as the identifier or the simple-template-id's template-
20471 name in the last component of the nested-name-specifier,
20472 the name is instead considered to name the constructor of
20473 class C. [ Note: for example, the constructor is not an
20474 acceptable lookup result in an elaborated-type-specifier so
20475 the constructor would not be used in place of the
20476 injected-class-name. --end note ] Such a constructor name
20477 shall be used only in the declarator-id of a declaration that
20478 names a constructor or in a using-declaration. */
20479 if (tag_type == none_type
20480 && DECL_SELF_REFERENCE_P (decl)
20481 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20482 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20483 tag_type != none_type,
20484 /*complain=*/true);
20486 /* If we have a single function from a using decl, pull it out. */
20487 if (TREE_CODE (decl) == OVERLOAD
20488 && !really_overloaded_fn (decl))
20489 decl = OVL_FUNCTION (decl);
20492 pop_scope (pushed_scope);
20495 /* If the scope is a dependent type and either we deferred lookup or
20496 we did lookup but didn't find the name, rememeber the name. */
20497 if (decl == error_mark_node && TYPE_P (parser->scope)
20498 && dependent_type_p (parser->scope))
20504 /* The resolution to Core Issue 180 says that `struct
20505 A::B' should be considered a type-name, even if `A'
20507 type = make_typename_type (parser->scope, name, tag_type,
20508 /*complain=*/tf_error);
20509 decl = TYPE_NAME (type);
20511 else if (is_template
20512 && (cp_parser_next_token_ends_template_argument_p (parser)
20513 || cp_lexer_next_token_is (parser->lexer,
20515 decl = make_unbound_class_template (parser->scope,
20517 /*complain=*/tf_error);
20519 decl = build_qualified_name (/*type=*/NULL_TREE,
20520 parser->scope, name,
20523 parser->qualifying_scope = parser->scope;
20524 parser->object_scope = NULL_TREE;
20526 else if (object_type)
20528 tree object_decl = NULL_TREE;
20529 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20530 OBJECT_TYPE is not a class. */
20531 if (CLASS_TYPE_P (object_type))
20532 /* If the OBJECT_TYPE is a template specialization, it may
20533 be instantiated during name lookup. In that case, errors
20534 may be issued. Even if we rollback the current tentative
20535 parse, those errors are valid. */
20536 object_decl = lookup_member (object_type,
20539 tag_type != none_type,
20540 tf_warning_or_error);
20541 /* Look it up in the enclosing context, too. */
20542 decl = lookup_name_real (name, tag_type != none_type,
20544 /*block_p=*/true, is_namespace, 0);
20545 parser->object_scope = object_type;
20546 parser->qualifying_scope = NULL_TREE;
20548 decl = object_decl;
20552 decl = lookup_name_real (name, tag_type != none_type,
20554 /*block_p=*/true, is_namespace, 0);
20555 parser->qualifying_scope = NULL_TREE;
20556 parser->object_scope = NULL_TREE;
20559 /* If the lookup failed, let our caller know. */
20560 if (!decl || decl == error_mark_node)
20561 return error_mark_node;
20563 /* Pull out the template from an injected-class-name (or multiple). */
20565 decl = maybe_get_template_decl_from_type_decl (decl);
20567 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20568 if (TREE_CODE (decl) == TREE_LIST)
20570 if (ambiguous_decls)
20571 *ambiguous_decls = decl;
20572 /* The error message we have to print is too complicated for
20573 cp_parser_error, so we incorporate its actions directly. */
20574 if (!cp_parser_simulate_error (parser))
20576 error_at (name_location, "reference to %qD is ambiguous",
20578 print_candidates (decl);
20580 return error_mark_node;
20583 gcc_assert (DECL_P (decl)
20584 || TREE_CODE (decl) == OVERLOAD
20585 || TREE_CODE (decl) == SCOPE_REF
20586 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20587 || BASELINK_P (decl));
20589 /* If we have resolved the name of a member declaration, check to
20590 see if the declaration is accessible. When the name resolves to
20591 set of overloaded functions, accessibility is checked when
20592 overload resolution is done.
20594 During an explicit instantiation, access is not checked at all,
20595 as per [temp.explicit]. */
20597 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20599 maybe_record_typedef_use (decl);
20604 /* Like cp_parser_lookup_name, but for use in the typical case where
20605 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20606 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20609 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20611 return cp_parser_lookup_name (parser, name,
20613 /*is_template=*/false,
20614 /*is_namespace=*/false,
20615 /*check_dependency=*/true,
20616 /*ambiguous_decls=*/NULL,
20620 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20621 the current context, return the TYPE_DECL. If TAG_NAME_P is
20622 true, the DECL indicates the class being defined in a class-head,
20623 or declared in an elaborated-type-specifier.
20625 Otherwise, return DECL. */
20628 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20630 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20631 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20634 template <typename T> struct B;
20637 template <typename T> struct A::B {};
20639 Similarly, in an elaborated-type-specifier:
20641 namespace N { struct X{}; }
20644 template <typename T> friend struct N::X;
20647 However, if the DECL refers to a class type, and we are in
20648 the scope of the class, then the name lookup automatically
20649 finds the TYPE_DECL created by build_self_reference rather
20650 than a TEMPLATE_DECL. For example, in:
20652 template <class T> struct S {
20656 there is no need to handle such case. */
20658 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20659 return DECL_TEMPLATE_RESULT (decl);
20664 /* If too many, or too few, template-parameter lists apply to the
20665 declarator, issue an error message. Returns TRUE if all went well,
20666 and FALSE otherwise. */
20669 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20670 cp_declarator *declarator,
20671 location_t declarator_location)
20673 unsigned num_templates;
20675 /* We haven't seen any classes that involve template parameters yet. */
20678 switch (declarator->kind)
20681 if (declarator->u.id.qualifying_scope)
20685 scope = declarator->u.id.qualifying_scope;
20687 while (scope && CLASS_TYPE_P (scope))
20689 /* You're supposed to have one `template <...>'
20690 for every template class, but you don't need one
20691 for a full specialization. For example:
20693 template <class T> struct S{};
20694 template <> struct S<int> { void f(); };
20695 void S<int>::f () {}
20697 is correct; there shouldn't be a `template <>' for
20698 the definition of `S<int>::f'. */
20699 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20700 /* If SCOPE does not have template information of any
20701 kind, then it is not a template, nor is it nested
20702 within a template. */
20704 if (explicit_class_specialization_p (scope))
20706 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20709 scope = TYPE_CONTEXT (scope);
20712 else if (TREE_CODE (declarator->u.id.unqualified_name)
20713 == TEMPLATE_ID_EXPR)
20714 /* If the DECLARATOR has the form `X<y>' then it uses one
20715 additional level of template parameters. */
20718 return cp_parser_check_template_parameters
20719 (parser, num_templates, declarator_location, declarator);
20725 case cdk_reference:
20727 return (cp_parser_check_declarator_template_parameters
20728 (parser, declarator->declarator, declarator_location));
20734 gcc_unreachable ();
20739 /* NUM_TEMPLATES were used in the current declaration. If that is
20740 invalid, return FALSE and issue an error messages. Otherwise,
20741 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20742 declarator and we can print more accurate diagnostics. */
20745 cp_parser_check_template_parameters (cp_parser* parser,
20746 unsigned num_templates,
20747 location_t location,
20748 cp_declarator *declarator)
20750 /* If there are the same number of template classes and parameter
20751 lists, that's OK. */
20752 if (parser->num_template_parameter_lists == num_templates)
20754 /* If there are more, but only one more, then we are referring to a
20755 member template. That's OK too. */
20756 if (parser->num_template_parameter_lists == num_templates + 1)
20758 /* If there are more template classes than parameter lists, we have
20761 template <class T> void S<T>::R<T>::f (); */
20762 if (parser->num_template_parameter_lists < num_templates)
20764 if (declarator && !current_function_decl)
20765 error_at (location, "specializing member %<%T::%E%> "
20766 "requires %<template<>%> syntax",
20767 declarator->u.id.qualifying_scope,
20768 declarator->u.id.unqualified_name);
20769 else if (declarator)
20770 error_at (location, "invalid declaration of %<%T::%E%>",
20771 declarator->u.id.qualifying_scope,
20772 declarator->u.id.unqualified_name);
20774 error_at (location, "too few template-parameter-lists");
20777 /* Otherwise, there are too many template parameter lists. We have
20780 template <class T> template <class U> void S::f(); */
20781 error_at (location, "too many template-parameter-lists");
20785 /* Parse an optional `::' token indicating that the following name is
20786 from the global namespace. If so, PARSER->SCOPE is set to the
20787 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20788 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20789 Returns the new value of PARSER->SCOPE, if the `::' token is
20790 present, and NULL_TREE otherwise. */
20793 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20797 /* Peek at the next token. */
20798 token = cp_lexer_peek_token (parser->lexer);
20799 /* If we're looking at a `::' token then we're starting from the
20800 global namespace, not our current location. */
20801 if (token->type == CPP_SCOPE)
20803 /* Consume the `::' token. */
20804 cp_lexer_consume_token (parser->lexer);
20805 /* Set the SCOPE so that we know where to start the lookup. */
20806 parser->scope = global_namespace;
20807 parser->qualifying_scope = global_namespace;
20808 parser->object_scope = NULL_TREE;
20810 return parser->scope;
20812 else if (!current_scope_valid_p)
20814 parser->scope = NULL_TREE;
20815 parser->qualifying_scope = NULL_TREE;
20816 parser->object_scope = NULL_TREE;
20822 /* Returns TRUE if the upcoming token sequence is the start of a
20823 constructor declarator. If FRIEND_P is true, the declarator is
20824 preceded by the `friend' specifier. */
20827 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20829 bool constructor_p;
20830 tree nested_name_specifier;
20831 cp_token *next_token;
20833 /* The common case is that this is not a constructor declarator, so
20834 try to avoid doing lots of work if at all possible. It's not
20835 valid declare a constructor at function scope. */
20836 if (parser->in_function_body)
20838 /* And only certain tokens can begin a constructor declarator. */
20839 next_token = cp_lexer_peek_token (parser->lexer);
20840 if (next_token->type != CPP_NAME
20841 && next_token->type != CPP_SCOPE
20842 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20843 && next_token->type != CPP_TEMPLATE_ID)
20846 /* Parse tentatively; we are going to roll back all of the tokens
20848 cp_parser_parse_tentatively (parser);
20849 /* Assume that we are looking at a constructor declarator. */
20850 constructor_p = true;
20852 /* Look for the optional `::' operator. */
20853 cp_parser_global_scope_opt (parser,
20854 /*current_scope_valid_p=*/false);
20855 /* Look for the nested-name-specifier. */
20856 nested_name_specifier
20857 = (cp_parser_nested_name_specifier_opt (parser,
20858 /*typename_keyword_p=*/false,
20859 /*check_dependency_p=*/false,
20861 /*is_declaration=*/false));
20862 /* Outside of a class-specifier, there must be a
20863 nested-name-specifier. */
20864 if (!nested_name_specifier &&
20865 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20867 constructor_p = false;
20868 else if (nested_name_specifier == error_mark_node)
20869 constructor_p = false;
20871 /* If we have a class scope, this is easy; DR 147 says that S::S always
20872 names the constructor, and no other qualified name could. */
20873 if (constructor_p && nested_name_specifier
20874 && CLASS_TYPE_P (nested_name_specifier))
20876 tree id = cp_parser_unqualified_id (parser,
20877 /*template_keyword_p=*/false,
20878 /*check_dependency_p=*/false,
20879 /*declarator_p=*/true,
20880 /*optional_p=*/false);
20881 if (is_overloaded_fn (id))
20882 id = DECL_NAME (get_first_fn (id));
20883 if (!constructor_name_p (id, nested_name_specifier))
20884 constructor_p = false;
20886 /* If we still think that this might be a constructor-declarator,
20887 look for a class-name. */
20888 else if (constructor_p)
20892 template <typename T> struct S {
20896 we must recognize that the nested `S' names a class. */
20898 type_decl = cp_parser_class_name (parser,
20899 /*typename_keyword_p=*/false,
20900 /*template_keyword_p=*/false,
20902 /*check_dependency_p=*/false,
20903 /*class_head_p=*/false,
20904 /*is_declaration=*/false);
20905 /* If there was no class-name, then this is not a constructor. */
20906 constructor_p = !cp_parser_error_occurred (parser);
20908 /* If we're still considering a constructor, we have to see a `(',
20909 to begin the parameter-declaration-clause, followed by either a
20910 `)', an `...', or a decl-specifier. We need to check for a
20911 type-specifier to avoid being fooled into thinking that:
20915 is a constructor. (It is actually a function named `f' that
20916 takes one parameter (of type `int') and returns a value of type
20919 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20920 constructor_p = false;
20923 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20924 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20925 /* A parameter declaration begins with a decl-specifier,
20926 which is either the "attribute" keyword, a storage class
20927 specifier, or (usually) a type-specifier. */
20928 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20931 tree pushed_scope = NULL_TREE;
20932 unsigned saved_num_template_parameter_lists;
20934 /* Names appearing in the type-specifier should be looked up
20935 in the scope of the class. */
20936 if (current_class_type)
20940 type = TREE_TYPE (type_decl);
20941 if (TREE_CODE (type) == TYPENAME_TYPE)
20943 type = resolve_typename_type (type,
20944 /*only_current_p=*/false);
20945 if (TREE_CODE (type) == TYPENAME_TYPE)
20947 cp_parser_abort_tentative_parse (parser);
20951 pushed_scope = push_scope (type);
20954 /* Inside the constructor parameter list, surrounding
20955 template-parameter-lists do not apply. */
20956 saved_num_template_parameter_lists
20957 = parser->num_template_parameter_lists;
20958 parser->num_template_parameter_lists = 0;
20960 /* Look for the type-specifier. */
20961 cp_parser_type_specifier (parser,
20962 CP_PARSER_FLAGS_NONE,
20963 /*decl_specs=*/NULL,
20964 /*is_declarator=*/true,
20965 /*declares_class_or_enum=*/NULL,
20966 /*is_cv_qualifier=*/NULL);
20968 parser->num_template_parameter_lists
20969 = saved_num_template_parameter_lists;
20971 /* Leave the scope of the class. */
20973 pop_scope (pushed_scope);
20975 constructor_p = !cp_parser_error_occurred (parser);
20979 /* We did not really want to consume any tokens. */
20980 cp_parser_abort_tentative_parse (parser);
20982 return constructor_p;
20985 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20986 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20987 they must be performed once we are in the scope of the function.
20989 Returns the function defined. */
20992 cp_parser_function_definition_from_specifiers_and_declarator
20993 (cp_parser* parser,
20994 cp_decl_specifier_seq *decl_specifiers,
20996 const cp_declarator *declarator)
21001 /* Begin the function-definition. */
21002 success_p = start_function (decl_specifiers, declarator, attributes);
21004 /* The things we're about to see are not directly qualified by any
21005 template headers we've seen thus far. */
21006 reset_specialization ();
21008 /* If there were names looked up in the decl-specifier-seq that we
21009 did not check, check them now. We must wait until we are in the
21010 scope of the function to perform the checks, since the function
21011 might be a friend. */
21012 perform_deferred_access_checks ();
21016 /* Skip the entire function. */
21017 cp_parser_skip_to_end_of_block_or_statement (parser);
21018 fn = error_mark_node;
21020 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21022 /* Seen already, skip it. An error message has already been output. */
21023 cp_parser_skip_to_end_of_block_or_statement (parser);
21024 fn = current_function_decl;
21025 current_function_decl = NULL_TREE;
21026 /* If this is a function from a class, pop the nested class. */
21027 if (current_class_name)
21028 pop_nested_class ();
21033 if (DECL_DECLARED_INLINE_P (current_function_decl))
21034 tv = TV_PARSE_INLINE;
21036 tv = TV_PARSE_FUNC;
21038 fn = cp_parser_function_definition_after_declarator (parser,
21039 /*inline_p=*/false);
21046 /* Parse the part of a function-definition that follows the
21047 declarator. INLINE_P is TRUE iff this function is an inline
21048 function defined within a class-specifier.
21050 Returns the function defined. */
21053 cp_parser_function_definition_after_declarator (cp_parser* parser,
21057 bool ctor_initializer_p = false;
21058 bool saved_in_unbraced_linkage_specification_p;
21059 bool saved_in_function_body;
21060 unsigned saved_num_template_parameter_lists;
21063 saved_in_function_body = parser->in_function_body;
21064 parser->in_function_body = true;
21065 /* If the next token is `return', then the code may be trying to
21066 make use of the "named return value" extension that G++ used to
21068 token = cp_lexer_peek_token (parser->lexer);
21069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21071 /* Consume the `return' keyword. */
21072 cp_lexer_consume_token (parser->lexer);
21073 /* Look for the identifier that indicates what value is to be
21075 cp_parser_identifier (parser);
21076 /* Issue an error message. */
21077 error_at (token->location,
21078 "named return values are no longer supported");
21079 /* Skip tokens until we reach the start of the function body. */
21082 cp_token *token = cp_lexer_peek_token (parser->lexer);
21083 if (token->type == CPP_OPEN_BRACE
21084 || token->type == CPP_EOF
21085 || token->type == CPP_PRAGMA_EOL)
21087 cp_lexer_consume_token (parser->lexer);
21090 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21091 anything declared inside `f'. */
21092 saved_in_unbraced_linkage_specification_p
21093 = parser->in_unbraced_linkage_specification_p;
21094 parser->in_unbraced_linkage_specification_p = false;
21095 /* Inside the function, surrounding template-parameter-lists do not
21097 saved_num_template_parameter_lists
21098 = parser->num_template_parameter_lists;
21099 parser->num_template_parameter_lists = 0;
21101 start_lambda_scope (current_function_decl);
21103 /* If the next token is `try', `__transaction_atomic', or
21104 `__transaction_relaxed`, then we are looking at either function-try-block
21105 or function-transaction-block. Note that all of these include the
21107 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21108 ctor_initializer_p = cp_parser_function_transaction (parser,
21109 RID_TRANSACTION_ATOMIC);
21110 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21111 RID_TRANSACTION_RELAXED))
21112 ctor_initializer_p = cp_parser_function_transaction (parser,
21113 RID_TRANSACTION_RELAXED);
21114 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21115 ctor_initializer_p = cp_parser_function_try_block (parser);
21117 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21118 (parser, /*in_function_try_block=*/false);
21120 finish_lambda_scope ();
21122 /* Finish the function. */
21123 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21124 (inline_p ? 2 : 0));
21125 /* Generate code for it, if necessary. */
21126 expand_or_defer_fn (fn);
21127 /* Restore the saved values. */
21128 parser->in_unbraced_linkage_specification_p
21129 = saved_in_unbraced_linkage_specification_p;
21130 parser->num_template_parameter_lists
21131 = saved_num_template_parameter_lists;
21132 parser->in_function_body = saved_in_function_body;
21137 /* Parse a template-declaration, assuming that the `export' (and
21138 `extern') keywords, if present, has already been scanned. MEMBER_P
21139 is as for cp_parser_template_declaration. */
21142 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21144 tree decl = NULL_TREE;
21145 VEC (deferred_access_check,gc) *checks;
21146 tree parameter_list;
21147 bool friend_p = false;
21148 bool need_lang_pop;
21151 /* Look for the `template' keyword. */
21152 token = cp_lexer_peek_token (parser->lexer);
21153 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21157 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21159 if (at_class_scope_p () && current_function_decl)
21161 /* 14.5.2.2 [temp.mem]
21163 A local class shall not have member templates. */
21164 error_at (token->location,
21165 "invalid declaration of member template in local class");
21166 cp_parser_skip_to_end_of_block_or_statement (parser);
21171 A template ... shall not have C linkage. */
21172 if (current_lang_name == lang_name_c)
21174 error_at (token->location, "template with C linkage");
21175 /* Give it C++ linkage to avoid confusing other parts of the
21177 push_lang_context (lang_name_cplusplus);
21178 need_lang_pop = true;
21181 need_lang_pop = false;
21183 /* We cannot perform access checks on the template parameter
21184 declarations until we know what is being declared, just as we
21185 cannot check the decl-specifier list. */
21186 push_deferring_access_checks (dk_deferred);
21188 /* If the next token is `>', then we have an invalid
21189 specialization. Rather than complain about an invalid template
21190 parameter, issue an error message here. */
21191 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21193 cp_parser_error (parser, "invalid explicit specialization");
21194 begin_specialization ();
21195 parameter_list = NULL_TREE;
21199 /* Parse the template parameters. */
21200 parameter_list = cp_parser_template_parameter_list (parser);
21203 /* Get the deferred access checks from the parameter list. These
21204 will be checked once we know what is being declared, as for a
21205 member template the checks must be performed in the scope of the
21206 class containing the member. */
21207 checks = get_deferred_access_checks ();
21209 /* Look for the `>'. */
21210 cp_parser_skip_to_end_of_template_parameter_list (parser);
21211 /* We just processed one more parameter list. */
21212 ++parser->num_template_parameter_lists;
21213 /* If the next token is `template', there are more template
21215 if (cp_lexer_next_token_is_keyword (parser->lexer,
21217 cp_parser_template_declaration_after_export (parser, member_p);
21218 else if (cxx_dialect >= cxx0x
21219 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21220 decl = cp_parser_alias_declaration (parser);
21223 /* There are no access checks when parsing a template, as we do not
21224 know if a specialization will be a friend. */
21225 push_deferring_access_checks (dk_no_check);
21226 token = cp_lexer_peek_token (parser->lexer);
21227 decl = cp_parser_single_declaration (parser,
21230 /*explicit_specialization_p=*/false,
21232 pop_deferring_access_checks ();
21234 /* If this is a member template declaration, let the front
21236 if (member_p && !friend_p && decl)
21238 if (TREE_CODE (decl) == TYPE_DECL)
21239 cp_parser_check_access_in_redeclaration (decl, token->location);
21241 decl = finish_member_template_decl (decl);
21243 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21244 make_friend_class (current_class_type, TREE_TYPE (decl),
21245 /*complain=*/true);
21247 /* We are done with the current parameter list. */
21248 --parser->num_template_parameter_lists;
21250 pop_deferring_access_checks ();
21253 finish_template_decl (parameter_list);
21255 /* Check the template arguments for a literal operator template. */
21257 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21258 && UDLIT_OPER_P (DECL_NAME (decl)))
21261 if (parameter_list == NULL_TREE)
21265 int num_parms = TREE_VEC_LENGTH (parameter_list);
21266 if (num_parms != 1)
21270 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21271 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21272 if (TREE_TYPE (parm) != char_type_node
21273 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21278 error ("literal operator template %qD has invalid parameter list."
21279 " Expected non-type template argument pack <char...>",
21282 /* Register member declarations. */
21283 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21284 finish_member_declaration (decl);
21285 /* For the erroneous case of a template with C linkage, we pushed an
21286 implicit C++ linkage scope; exit that scope now. */
21288 pop_lang_context ();
21289 /* If DECL is a function template, we must return to parse it later.
21290 (Even though there is no definition, there might be default
21291 arguments that need handling.) */
21292 if (member_p && decl
21293 && (TREE_CODE (decl) == FUNCTION_DECL
21294 || DECL_FUNCTION_TEMPLATE_P (decl)))
21295 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21298 /* Perform the deferred access checks from a template-parameter-list.
21299 CHECKS is a TREE_LIST of access checks, as returned by
21300 get_deferred_access_checks. */
21303 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21305 ++processing_template_parmlist;
21306 perform_access_checks (checks);
21307 --processing_template_parmlist;
21310 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21311 `function-definition' sequence. MEMBER_P is true, this declaration
21312 appears in a class scope.
21314 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21315 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21318 cp_parser_single_declaration (cp_parser* parser,
21319 VEC (deferred_access_check,gc)* checks,
21321 bool explicit_specialization_p,
21324 int declares_class_or_enum;
21325 tree decl = NULL_TREE;
21326 cp_decl_specifier_seq decl_specifiers;
21327 bool function_definition_p = false;
21328 cp_token *decl_spec_token_start;
21330 /* This function is only used when processing a template
21332 gcc_assert (innermost_scope_kind () == sk_template_parms
21333 || innermost_scope_kind () == sk_template_spec);
21335 /* Defer access checks until we know what is being declared. */
21336 push_deferring_access_checks (dk_deferred);
21338 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21340 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21341 cp_parser_decl_specifier_seq (parser,
21342 CP_PARSER_FLAGS_OPTIONAL,
21344 &declares_class_or_enum);
21346 *friend_p = cp_parser_friend_p (&decl_specifiers);
21348 /* There are no template typedefs. */
21349 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21351 error_at (decl_spec_token_start->location,
21352 "template declaration of %<typedef%>");
21353 decl = error_mark_node;
21356 /* Gather up the access checks that occurred the
21357 decl-specifier-seq. */
21358 stop_deferring_access_checks ();
21360 /* Check for the declaration of a template class. */
21361 if (declares_class_or_enum)
21363 if (cp_parser_declares_only_class_p (parser))
21365 decl = shadow_tag (&decl_specifiers);
21370 friend template <typename T> struct A<T>::B;
21373 A<T>::B will be represented by a TYPENAME_TYPE, and
21374 therefore not recognized by shadow_tag. */
21375 if (friend_p && *friend_p
21377 && decl_specifiers.type
21378 && TYPE_P (decl_specifiers.type))
21379 decl = decl_specifiers.type;
21381 if (decl && decl != error_mark_node)
21382 decl = TYPE_NAME (decl);
21384 decl = error_mark_node;
21386 /* Perform access checks for template parameters. */
21387 cp_parser_perform_template_parameter_access_checks (checks);
21391 /* Complain about missing 'typename' or other invalid type names. */
21392 if (!decl_specifiers.any_type_specifiers_p
21393 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21395 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21396 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21397 the rest of this declaration. */
21398 decl = error_mark_node;
21402 /* If it's not a template class, try for a template function. If
21403 the next token is a `;', then this declaration does not declare
21404 anything. But, if there were errors in the decl-specifiers, then
21405 the error might well have come from an attempted class-specifier.
21406 In that case, there's no need to warn about a missing declarator. */
21408 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21409 || decl_specifiers.type != error_mark_node))
21411 decl = cp_parser_init_declarator (parser,
21414 /*function_definition_allowed_p=*/true,
21416 declares_class_or_enum,
21417 &function_definition_p,
21420 /* 7.1.1-1 [dcl.stc]
21422 A storage-class-specifier shall not be specified in an explicit
21423 specialization... */
21425 && explicit_specialization_p
21426 && decl_specifiers.storage_class != sc_none)
21428 error_at (decl_spec_token_start->location,
21429 "explicit template specialization cannot have a storage class");
21430 decl = error_mark_node;
21434 /* Look for a trailing `;' after the declaration. */
21435 if (!function_definition_p
21436 && (decl == error_mark_node
21437 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21438 cp_parser_skip_to_end_of_block_or_statement (parser);
21441 pop_deferring_access_checks ();
21443 /* Clear any current qualification; whatever comes next is the start
21444 of something new. */
21445 parser->scope = NULL_TREE;
21446 parser->qualifying_scope = NULL_TREE;
21447 parser->object_scope = NULL_TREE;
21452 /* Parse a cast-expression that is not the operand of a unary "&". */
21455 cp_parser_simple_cast_expression (cp_parser *parser)
21457 return cp_parser_cast_expression (parser, /*address_p=*/false,
21458 /*cast_p=*/false, NULL);
21461 /* Parse a functional cast to TYPE. Returns an expression
21462 representing the cast. */
21465 cp_parser_functional_cast (cp_parser* parser, tree type)
21468 tree expression_list;
21472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21474 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21475 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21476 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21477 if (TREE_CODE (type) == TYPE_DECL)
21478 type = TREE_TYPE (type);
21479 return finish_compound_literal (type, expression_list,
21480 tf_warning_or_error);
21484 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21486 /*allow_expansion_p=*/true,
21487 /*non_constant_p=*/NULL);
21489 expression_list = error_mark_node;
21492 expression_list = build_tree_list_vec (vec);
21493 release_tree_vector (vec);
21496 cast = build_functional_cast (type, expression_list,
21497 tf_warning_or_error);
21498 /* [expr.const]/1: In an integral constant expression "only type
21499 conversions to integral or enumeration type can be used". */
21500 if (TREE_CODE (type) == TYPE_DECL)
21501 type = TREE_TYPE (type);
21502 if (cast != error_mark_node
21503 && !cast_valid_in_integral_constant_expression_p (type)
21504 && cp_parser_non_integral_constant_expression (parser,
21506 return error_mark_node;
21510 /* Save the tokens that make up the body of a member function defined
21511 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21512 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21513 specifiers applied to the declaration. Returns the FUNCTION_DECL
21514 for the member function. */
21517 cp_parser_save_member_function_body (cp_parser* parser,
21518 cp_decl_specifier_seq *decl_specifiers,
21519 cp_declarator *declarator,
21526 /* Create the FUNCTION_DECL. */
21527 fn = grokmethod (decl_specifiers, declarator, attributes);
21528 /* If something went badly wrong, bail out now. */
21529 if (fn == error_mark_node)
21531 /* If there's a function-body, skip it. */
21532 if (cp_parser_token_starts_function_definition_p
21533 (cp_lexer_peek_token (parser->lexer)))
21534 cp_parser_skip_to_end_of_block_or_statement (parser);
21535 return error_mark_node;
21538 /* Remember it, if there default args to post process. */
21539 cp_parser_save_default_args (parser, fn);
21541 /* Save away the tokens that make up the body of the
21543 first = parser->lexer->next_token;
21544 /* We can have braced-init-list mem-initializers before the fn body. */
21545 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21547 cp_lexer_consume_token (parser->lexer);
21548 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21549 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21551 /* cache_group will stop after an un-nested { } pair, too. */
21552 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21555 /* variadic mem-inits have ... after the ')'. */
21556 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21557 cp_lexer_consume_token (parser->lexer);
21560 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21561 /* Handle function try blocks. */
21562 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21563 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21564 last = parser->lexer->next_token;
21566 /* Save away the inline definition; we will process it when the
21567 class is complete. */
21568 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21569 DECL_PENDING_INLINE_P (fn) = 1;
21571 /* We need to know that this was defined in the class, so that
21572 friend templates are handled correctly. */
21573 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21575 /* Add FN to the queue of functions to be parsed later. */
21576 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21581 /* Save the tokens that make up the in-class initializer for a non-static
21582 data member. Returns a DEFAULT_ARG. */
21585 cp_parser_save_nsdmi (cp_parser* parser)
21587 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21590 /* Parse a template-argument-list, as well as the trailing ">" (but
21591 not the opening "<"). See cp_parser_template_argument_list for the
21595 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21599 tree saved_qualifying_scope;
21600 tree saved_object_scope;
21601 bool saved_greater_than_is_operator_p;
21602 int saved_unevaluated_operand;
21603 int saved_inhibit_evaluation_warnings;
21607 When parsing a template-id, the first non-nested `>' is taken as
21608 the end of the template-argument-list rather than a greater-than
21610 saved_greater_than_is_operator_p
21611 = parser->greater_than_is_operator_p;
21612 parser->greater_than_is_operator_p = false;
21613 /* Parsing the argument list may modify SCOPE, so we save it
21615 saved_scope = parser->scope;
21616 saved_qualifying_scope = parser->qualifying_scope;
21617 saved_object_scope = parser->object_scope;
21618 /* We need to evaluate the template arguments, even though this
21619 template-id may be nested within a "sizeof". */
21620 saved_unevaluated_operand = cp_unevaluated_operand;
21621 cp_unevaluated_operand = 0;
21622 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21623 c_inhibit_evaluation_warnings = 0;
21624 /* Parse the template-argument-list itself. */
21625 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21626 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21627 arguments = NULL_TREE;
21629 arguments = cp_parser_template_argument_list (parser);
21630 /* Look for the `>' that ends the template-argument-list. If we find
21631 a '>>' instead, it's probably just a typo. */
21632 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21634 if (cxx_dialect != cxx98)
21636 /* In C++0x, a `>>' in a template argument list or cast
21637 expression is considered to be two separate `>'
21638 tokens. So, change the current token to a `>', but don't
21639 consume it: it will be consumed later when the outer
21640 template argument list (or cast expression) is parsed.
21641 Note that this replacement of `>' for `>>' is necessary
21642 even if we are parsing tentatively: in the tentative
21643 case, after calling
21644 cp_parser_enclosed_template_argument_list we will always
21645 throw away all of the template arguments and the first
21646 closing `>', either because the template argument list
21647 was erroneous or because we are replacing those tokens
21648 with a CPP_TEMPLATE_ID token. The second `>' (which will
21649 not have been thrown away) is needed either to close an
21650 outer template argument list or to complete a new-style
21652 cp_token *token = cp_lexer_peek_token (parser->lexer);
21653 token->type = CPP_GREATER;
21655 else if (!saved_greater_than_is_operator_p)
21657 /* If we're in a nested template argument list, the '>>' has
21658 to be a typo for '> >'. We emit the error message, but we
21659 continue parsing and we push a '>' as next token, so that
21660 the argument list will be parsed correctly. Note that the
21661 global source location is still on the token before the
21662 '>>', so we need to say explicitly where we want it. */
21663 cp_token *token = cp_lexer_peek_token (parser->lexer);
21664 error_at (token->location, "%<>>%> should be %<> >%> "
21665 "within a nested template argument list");
21667 token->type = CPP_GREATER;
21671 /* If this is not a nested template argument list, the '>>'
21672 is a typo for '>'. Emit an error message and continue.
21673 Same deal about the token location, but here we can get it
21674 right by consuming the '>>' before issuing the diagnostic. */
21675 cp_token *token = cp_lexer_consume_token (parser->lexer);
21676 error_at (token->location,
21677 "spurious %<>>%>, use %<>%> to terminate "
21678 "a template argument list");
21682 cp_parser_skip_to_end_of_template_parameter_list (parser);
21683 /* The `>' token might be a greater-than operator again now. */
21684 parser->greater_than_is_operator_p
21685 = saved_greater_than_is_operator_p;
21686 /* Restore the SAVED_SCOPE. */
21687 parser->scope = saved_scope;
21688 parser->qualifying_scope = saved_qualifying_scope;
21689 parser->object_scope = saved_object_scope;
21690 cp_unevaluated_operand = saved_unevaluated_operand;
21691 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21696 /* MEMBER_FUNCTION is a member function, or a friend. If default
21697 arguments, or the body of the function have not yet been parsed,
21701 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21703 timevar_push (TV_PARSE_INMETH);
21704 /* If this member is a template, get the underlying
21706 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21707 member_function = DECL_TEMPLATE_RESULT (member_function);
21709 /* There should not be any class definitions in progress at this
21710 point; the bodies of members are only parsed outside of all class
21712 gcc_assert (parser->num_classes_being_defined == 0);
21713 /* While we're parsing the member functions we might encounter more
21714 classes. We want to handle them right away, but we don't want
21715 them getting mixed up with functions that are currently in the
21717 push_unparsed_function_queues (parser);
21719 /* Make sure that any template parameters are in scope. */
21720 maybe_begin_member_template_processing (member_function);
21722 /* If the body of the function has not yet been parsed, parse it
21724 if (DECL_PENDING_INLINE_P (member_function))
21726 tree function_scope;
21727 cp_token_cache *tokens;
21729 /* The function is no longer pending; we are processing it. */
21730 tokens = DECL_PENDING_INLINE_INFO (member_function);
21731 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21732 DECL_PENDING_INLINE_P (member_function) = 0;
21734 /* If this is a local class, enter the scope of the containing
21736 function_scope = current_function_decl;
21737 if (function_scope)
21738 push_function_context ();
21740 /* Push the body of the function onto the lexer stack. */
21741 cp_parser_push_lexer_for_tokens (parser, tokens);
21743 /* Let the front end know that we going to be defining this
21745 start_preparsed_function (member_function, NULL_TREE,
21746 SF_PRE_PARSED | SF_INCLASS_INLINE);
21748 /* Don't do access checking if it is a templated function. */
21749 if (processing_template_decl)
21750 push_deferring_access_checks (dk_no_check);
21752 /* Now, parse the body of the function. */
21753 cp_parser_function_definition_after_declarator (parser,
21754 /*inline_p=*/true);
21756 if (processing_template_decl)
21757 pop_deferring_access_checks ();
21759 /* Leave the scope of the containing function. */
21760 if (function_scope)
21761 pop_function_context ();
21762 cp_parser_pop_lexer (parser);
21765 /* Remove any template parameters from the symbol table. */
21766 maybe_end_member_template_processing ();
21768 /* Restore the queue. */
21769 pop_unparsed_function_queues (parser);
21770 timevar_pop (TV_PARSE_INMETH);
21773 /* If DECL contains any default args, remember it on the unparsed
21774 functions queue. */
21777 cp_parser_save_default_args (cp_parser* parser, tree decl)
21781 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21783 probe = TREE_CHAIN (probe))
21784 if (TREE_PURPOSE (probe))
21786 cp_default_arg_entry *entry
21787 = VEC_safe_push (cp_default_arg_entry, gc,
21788 unparsed_funs_with_default_args, NULL);
21789 entry->class_type = current_class_type;
21790 entry->decl = decl;
21795 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21796 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21797 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21798 from the parameter-type-list. */
21801 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21802 tree default_arg, tree parmtype)
21804 cp_token_cache *tokens;
21808 if (default_arg == error_mark_node)
21809 return error_mark_node;
21811 /* Push the saved tokens for the default argument onto the parser's
21813 tokens = DEFARG_TOKENS (default_arg);
21814 cp_parser_push_lexer_for_tokens (parser, tokens);
21816 start_lambda_scope (decl);
21818 /* Parse the default argument. */
21819 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21820 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21821 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21823 finish_lambda_scope ();
21825 if (parsed_arg == error_mark_node)
21826 cp_parser_skip_to_end_of_statement (parser);
21828 if (!processing_template_decl)
21830 /* In a non-template class, check conversions now. In a template,
21831 we'll wait and instantiate these as needed. */
21832 if (TREE_CODE (decl) == PARM_DECL)
21833 parsed_arg = check_default_argument (parmtype, parsed_arg);
21836 int flags = LOOKUP_IMPLICIT;
21837 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21838 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21839 flags = LOOKUP_NORMAL;
21840 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21844 /* If the token stream has not been completely used up, then
21845 there was extra junk after the end of the default
21847 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21849 if (TREE_CODE (decl) == PARM_DECL)
21850 cp_parser_error (parser, "expected %<,%>");
21852 cp_parser_error (parser, "expected %<;%>");
21855 /* Revert to the main lexer. */
21856 cp_parser_pop_lexer (parser);
21861 /* FIELD is a non-static data member with an initializer which we saved for
21862 later; parse it now. */
21865 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21869 push_unparsed_function_queues (parser);
21870 def = cp_parser_late_parse_one_default_arg (parser, field,
21871 DECL_INITIAL (field),
21873 pop_unparsed_function_queues (parser);
21875 DECL_INITIAL (field) = def;
21878 /* FN is a FUNCTION_DECL which may contains a parameter with an
21879 unparsed DEFAULT_ARG. Parse the default args now. This function
21880 assumes that the current scope is the scope in which the default
21881 argument should be processed. */
21884 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21886 bool saved_local_variables_forbidden_p;
21887 tree parm, parmdecl;
21889 /* While we're parsing the default args, we might (due to the
21890 statement expression extension) encounter more classes. We want
21891 to handle them right away, but we don't want them getting mixed
21892 up with default args that are currently in the queue. */
21893 push_unparsed_function_queues (parser);
21895 /* Local variable names (and the `this' keyword) may not appear
21896 in a default argument. */
21897 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21898 parser->local_variables_forbidden_p = true;
21900 push_defarg_context (fn);
21902 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21903 parmdecl = DECL_ARGUMENTS (fn);
21904 parm && parm != void_list_node;
21905 parm = TREE_CHAIN (parm),
21906 parmdecl = DECL_CHAIN (parmdecl))
21908 tree default_arg = TREE_PURPOSE (parm);
21910 VEC(tree,gc) *insts;
21917 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21918 /* This can happen for a friend declaration for a function
21919 already declared with default arguments. */
21923 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21925 TREE_VALUE (parm));
21926 if (parsed_arg == error_mark_node)
21931 TREE_PURPOSE (parm) = parsed_arg;
21933 /* Update any instantiations we've already created. */
21934 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21935 VEC_iterate (tree, insts, ix, copy); ix++)
21936 TREE_PURPOSE (copy) = parsed_arg;
21939 pop_defarg_context ();
21941 /* Make sure no default arg is missing. */
21942 check_default_args (fn);
21944 /* Restore the state of local_variables_forbidden_p. */
21945 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21947 /* Restore the queue. */
21948 pop_unparsed_function_queues (parser);
21951 /* Parse the operand of `sizeof' (or a similar operator). Returns
21952 either a TYPE or an expression, depending on the form of the
21953 input. The KEYWORD indicates which kind of expression we have
21957 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21959 tree expr = NULL_TREE;
21960 const char *saved_message;
21962 bool saved_integral_constant_expression_p;
21963 bool saved_non_integral_constant_expression_p;
21964 bool pack_expansion_p = false;
21966 /* Types cannot be defined in a `sizeof' expression. Save away the
21968 saved_message = parser->type_definition_forbidden_message;
21969 /* And create the new one. */
21970 tmp = concat ("types may not be defined in %<",
21971 IDENTIFIER_POINTER (ridpointers[keyword]),
21972 "%> expressions", NULL);
21973 parser->type_definition_forbidden_message = tmp;
21975 /* The restrictions on constant-expressions do not apply inside
21976 sizeof expressions. */
21977 saved_integral_constant_expression_p
21978 = parser->integral_constant_expression_p;
21979 saved_non_integral_constant_expression_p
21980 = parser->non_integral_constant_expression_p;
21981 parser->integral_constant_expression_p = false;
21983 /* If it's a `...', then we are computing the length of a parameter
21985 if (keyword == RID_SIZEOF
21986 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21988 /* Consume the `...'. */
21989 cp_lexer_consume_token (parser->lexer);
21990 maybe_warn_variadic_templates ();
21992 /* Note that this is an expansion. */
21993 pack_expansion_p = true;
21996 /* Do not actually evaluate the expression. */
21997 ++cp_unevaluated_operand;
21998 ++c_inhibit_evaluation_warnings;
21999 /* If it's a `(', then we might be looking at the type-id
22001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22004 bool saved_in_type_id_in_expr_p;
22006 /* We can't be sure yet whether we're looking at a type-id or an
22008 cp_parser_parse_tentatively (parser);
22009 /* Consume the `('. */
22010 cp_lexer_consume_token (parser->lexer);
22011 /* Parse the type-id. */
22012 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22013 parser->in_type_id_in_expr_p = true;
22014 type = cp_parser_type_id (parser);
22015 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22016 /* Now, look for the trailing `)'. */
22017 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22018 /* If all went well, then we're done. */
22019 if (cp_parser_parse_definitely (parser))
22021 cp_decl_specifier_seq decl_specs;
22023 /* Build a trivial decl-specifier-seq. */
22024 clear_decl_specs (&decl_specs);
22025 decl_specs.type = type;
22027 /* Call grokdeclarator to figure out what type this is. */
22028 expr = grokdeclarator (NULL,
22032 /*attrlist=*/NULL);
22035 else if (pack_expansion_p)
22036 permerror (cp_lexer_peek_token (parser->lexer)->location,
22037 "%<sizeof...%> argument must be surrounded by parentheses");
22039 /* If the type-id production did not work out, then we must be
22040 looking at the unary-expression production. */
22042 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22043 /*cast_p=*/false, NULL);
22045 if (pack_expansion_p)
22046 /* Build a pack expansion. */
22047 expr = make_pack_expansion (expr);
22049 /* Go back to evaluating expressions. */
22050 --cp_unevaluated_operand;
22051 --c_inhibit_evaluation_warnings;
22053 /* Free the message we created. */
22055 /* And restore the old one. */
22056 parser->type_definition_forbidden_message = saved_message;
22057 parser->integral_constant_expression_p
22058 = saved_integral_constant_expression_p;
22059 parser->non_integral_constant_expression_p
22060 = saved_non_integral_constant_expression_p;
22065 /* If the current declaration has no declarator, return true. */
22068 cp_parser_declares_only_class_p (cp_parser *parser)
22070 /* If the next token is a `;' or a `,' then there is no
22072 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22073 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22076 /* Update the DECL_SPECS to reflect the storage class indicated by
22080 cp_parser_set_storage_class (cp_parser *parser,
22081 cp_decl_specifier_seq *decl_specs,
22083 location_t location)
22085 cp_storage_class storage_class;
22087 if (parser->in_unbraced_linkage_specification_p)
22089 error_at (location, "invalid use of %qD in linkage specification",
22090 ridpointers[keyword]);
22093 else if (decl_specs->storage_class != sc_none)
22095 decl_specs->conflicting_specifiers_p = true;
22099 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22100 && decl_spec_seq_has_spec_p (decl_specs, ds_thread))
22102 error_at (decl_specs->locations[ds_thread],
22103 "%<__thread%> before %qD", ridpointers[keyword]);
22104 decl_specs->locations[ds_thread] = 0;
22110 storage_class = sc_auto;
22113 storage_class = sc_register;
22116 storage_class = sc_static;
22119 storage_class = sc_extern;
22122 storage_class = sc_mutable;
22125 gcc_unreachable ();
22127 decl_specs->storage_class = storage_class;
22128 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, location);
22130 /* A storage class specifier cannot be applied alongside a typedef
22131 specifier. If there is a typedef specifier present then set
22132 conflicting_specifiers_p which will trigger an error later
22133 on in grokdeclarator. */
22134 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22135 decl_specs->conflicting_specifiers_p = true;
22138 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22139 is true, the type is a class or enum definition. */
22142 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22144 location_t location,
22145 bool type_definition_p)
22147 decl_specs->any_specifiers_p = true;
22149 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22150 (with, for example, in "typedef int wchar_t;") we remember that
22151 this is what happened. In system headers, we ignore these
22152 declarations so that G++ can work with system headers that are not
22154 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22155 && !type_definition_p
22156 && (type_spec == boolean_type_node
22157 || type_spec == char16_type_node
22158 || type_spec == char32_type_node
22159 || type_spec == wchar_type_node)
22160 && (decl_specs->type
22161 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22162 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22163 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22164 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22166 decl_specs->redefined_builtin_type = type_spec;
22167 set_and_check_decl_spec_loc (decl_specs,
22168 ds_redefined_builtin_type_spec,
22170 if (!decl_specs->type)
22172 decl_specs->type = type_spec;
22173 decl_specs->type_definition_p = false;
22174 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, location);
22177 else if (decl_specs->type)
22178 decl_specs->multiple_types_p = true;
22181 decl_specs->type = type_spec;
22182 decl_specs->type_definition_p = type_definition_p;
22183 decl_specs->redefined_builtin_type = NULL_TREE;
22184 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, location);
22188 /* Set the location for a declarator specifier and check if it is
22191 DECL_SPECS is the sequence of declarator specifiers onto which to
22194 DS is the single declarator specifier to set which location is to
22195 be set onto the existing sequence of declarators.
22197 LOCATION is the location for the declarator specifier to
22201 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22202 cp_decl_spec ds, source_location location)
22204 gcc_assert (ds < ds_last);
22206 if (decl_specs == NULL)
22209 if (decl_specs->locations[ds] == 0)
22210 decl_specs->locations[ds] = location;
22215 if (decl_specs->locations[ds_long_long] != 0)
22216 error_at (location,
22217 "%<long long long%> is too long for GCC");
22220 decl_specs->locations[ds_long_long] = location;
22221 pedwarn_cxx98 (location,
22223 "ISO C++ 1998 does not support %<long long%>");
22228 static const char *const decl_spec_names[] = {
22246 error_at (location,
22247 "duplicate %qs", decl_spec_names[ds]);
22252 /* Return true iff the declarator specifier DS is present in the
22253 sequence of declarator specifiers DECL_SPECS. */
22256 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22259 gcc_assert (ds < ds_last);
22261 if (decl_specs == NULL)
22264 return decl_specs->locations[ds] != 0;
22267 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22268 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22271 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22273 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22276 /* Issue an error message indicating that TOKEN_DESC was expected.
22277 If KEYWORD is true, it indicated this function is called by
22278 cp_parser_require_keword and the required token can only be
22279 a indicated keyword. */
22282 cp_parser_required_error (cp_parser *parser,
22283 required_token token_desc,
22286 switch (token_desc)
22289 cp_parser_error (parser, "expected %<new%>");
22292 cp_parser_error (parser, "expected %<delete%>");
22295 cp_parser_error (parser, "expected %<return%>");
22298 cp_parser_error (parser, "expected %<while%>");
22301 cp_parser_error (parser, "expected %<extern%>");
22303 case RT_STATIC_ASSERT:
22304 cp_parser_error (parser, "expected %<static_assert%>");
22307 cp_parser_error (parser, "expected %<decltype%>");
22310 cp_parser_error (parser, "expected %<operator%>");
22313 cp_parser_error (parser, "expected %<class%>");
22316 cp_parser_error (parser, "expected %<template%>");
22319 cp_parser_error (parser, "expected %<namespace%>");
22322 cp_parser_error (parser, "expected %<using%>");
22325 cp_parser_error (parser, "expected %<asm%>");
22328 cp_parser_error (parser, "expected %<try%>");
22331 cp_parser_error (parser, "expected %<catch%>");
22334 cp_parser_error (parser, "expected %<throw%>");
22337 cp_parser_error (parser, "expected %<__label__%>");
22340 cp_parser_error (parser, "expected %<@try%>");
22342 case RT_AT_SYNCHRONIZED:
22343 cp_parser_error (parser, "expected %<@synchronized%>");
22346 cp_parser_error (parser, "expected %<@throw%>");
22348 case RT_TRANSACTION_ATOMIC:
22349 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22351 case RT_TRANSACTION_RELAXED:
22352 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22359 switch (token_desc)
22362 cp_parser_error (parser, "expected %<;%>");
22364 case RT_OPEN_PAREN:
22365 cp_parser_error (parser, "expected %<(%>");
22367 case RT_CLOSE_BRACE:
22368 cp_parser_error (parser, "expected %<}%>");
22370 case RT_OPEN_BRACE:
22371 cp_parser_error (parser, "expected %<{%>");
22373 case RT_CLOSE_SQUARE:
22374 cp_parser_error (parser, "expected %<]%>");
22376 case RT_OPEN_SQUARE:
22377 cp_parser_error (parser, "expected %<[%>");
22380 cp_parser_error (parser, "expected %<,%>");
22383 cp_parser_error (parser, "expected %<::%>");
22386 cp_parser_error (parser, "expected %<<%>");
22389 cp_parser_error (parser, "expected %<>%>");
22392 cp_parser_error (parser, "expected %<=%>");
22395 cp_parser_error (parser, "expected %<...%>");
22398 cp_parser_error (parser, "expected %<*%>");
22401 cp_parser_error (parser, "expected %<~%>");
22404 cp_parser_error (parser, "expected %<:%>");
22406 case RT_COLON_SCOPE:
22407 cp_parser_error (parser, "expected %<:%> or %<::%>");
22409 case RT_CLOSE_PAREN:
22410 cp_parser_error (parser, "expected %<)%>");
22412 case RT_COMMA_CLOSE_PAREN:
22413 cp_parser_error (parser, "expected %<,%> or %<)%>");
22415 case RT_PRAGMA_EOL:
22416 cp_parser_error (parser, "expected end of line");
22419 cp_parser_error (parser, "expected identifier");
22422 cp_parser_error (parser, "expected selection-statement");
22424 case RT_INTERATION:
22425 cp_parser_error (parser, "expected iteration-statement");
22428 cp_parser_error (parser, "expected jump-statement");
22431 cp_parser_error (parser, "expected class-key");
22433 case RT_CLASS_TYPENAME_TEMPLATE:
22434 cp_parser_error (parser,
22435 "expected %<class%>, %<typename%>, or %<template%>");
22438 gcc_unreachable ();
22442 gcc_unreachable ();
22447 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22448 issue an error message indicating that TOKEN_DESC was expected.
22450 Returns the token consumed, if the token had the appropriate type.
22451 Otherwise, returns NULL. */
22454 cp_parser_require (cp_parser* parser,
22455 enum cpp_ttype type,
22456 required_token token_desc)
22458 if (cp_lexer_next_token_is (parser->lexer, type))
22459 return cp_lexer_consume_token (parser->lexer);
22462 /* Output the MESSAGE -- unless we're parsing tentatively. */
22463 if (!cp_parser_simulate_error (parser))
22464 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22469 /* An error message is produced if the next token is not '>'.
22470 All further tokens are skipped until the desired token is
22471 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22474 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22476 /* Current level of '< ... >'. */
22477 unsigned level = 0;
22478 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22479 unsigned nesting_depth = 0;
22481 /* Are we ready, yet? If not, issue error message. */
22482 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22485 /* Skip tokens until the desired token is found. */
22488 /* Peek at the next token. */
22489 switch (cp_lexer_peek_token (parser->lexer)->type)
22492 if (!nesting_depth)
22497 if (cxx_dialect == cxx98)
22498 /* C++0x views the `>>' operator as two `>' tokens, but
22501 else if (!nesting_depth && level-- == 0)
22503 /* We've hit a `>>' where the first `>' closes the
22504 template argument list, and the second `>' is
22505 spurious. Just consume the `>>' and stop; we've
22506 already produced at least one error. */
22507 cp_lexer_consume_token (parser->lexer);
22510 /* Fall through for C++0x, so we handle the second `>' in
22514 if (!nesting_depth && level-- == 0)
22516 /* We've reached the token we want, consume it and stop. */
22517 cp_lexer_consume_token (parser->lexer);
22522 case CPP_OPEN_PAREN:
22523 case CPP_OPEN_SQUARE:
22527 case CPP_CLOSE_PAREN:
22528 case CPP_CLOSE_SQUARE:
22529 if (nesting_depth-- == 0)
22534 case CPP_PRAGMA_EOL:
22535 case CPP_SEMICOLON:
22536 case CPP_OPEN_BRACE:
22537 case CPP_CLOSE_BRACE:
22538 /* The '>' was probably forgotten, don't look further. */
22545 /* Consume this token. */
22546 cp_lexer_consume_token (parser->lexer);
22550 /* If the next token is the indicated keyword, consume it. Otherwise,
22551 issue an error message indicating that TOKEN_DESC was expected.
22553 Returns the token consumed, if the token had the appropriate type.
22554 Otherwise, returns NULL. */
22557 cp_parser_require_keyword (cp_parser* parser,
22559 required_token token_desc)
22561 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22563 if (token && token->keyword != keyword)
22565 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22572 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22573 function-definition. */
22576 cp_parser_token_starts_function_definition_p (cp_token* token)
22578 return (/* An ordinary function-body begins with an `{'. */
22579 token->type == CPP_OPEN_BRACE
22580 /* A ctor-initializer begins with a `:'. */
22581 || token->type == CPP_COLON
22582 /* A function-try-block begins with `try'. */
22583 || token->keyword == RID_TRY
22584 /* A function-transaction-block begins with `__transaction_atomic'
22585 or `__transaction_relaxed'. */
22586 || token->keyword == RID_TRANSACTION_ATOMIC
22587 || token->keyword == RID_TRANSACTION_RELAXED
22588 /* The named return value extension begins with `return'. */
22589 || token->keyword == RID_RETURN);
22592 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22596 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22600 token = cp_lexer_peek_token (parser->lexer);
22601 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22604 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22605 C++0x) ending a template-argument. */
22608 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22612 token = cp_lexer_peek_token (parser->lexer);
22613 return (token->type == CPP_COMMA
22614 || token->type == CPP_GREATER
22615 || token->type == CPP_ELLIPSIS
22616 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22619 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22620 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22623 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22628 token = cp_lexer_peek_nth_token (parser->lexer, n);
22629 if (token->type == CPP_LESS)
22631 /* Check for the sequence `<::' in the original code. It would be lexed as
22632 `[:', where `[' is a digraph, and there is no whitespace before
22634 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22637 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22638 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22644 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22645 or none_type otherwise. */
22647 static enum tag_types
22648 cp_parser_token_is_class_key (cp_token* token)
22650 switch (token->keyword)
22655 return record_type;
22664 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22667 cp_parser_check_class_key (enum tag_types class_key, tree type)
22669 if (type == error_mark_node)
22671 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22673 permerror (input_location, "%qs tag used in naming %q#T",
22674 class_key == union_type ? "union"
22675 : class_key == record_type ? "struct" : "class",
22677 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22678 "%q#T was previously declared here", type);
22682 /* Issue an error message if DECL is redeclared with different
22683 access than its original declaration [class.access.spec/3].
22684 This applies to nested classes and nested class templates.
22688 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22690 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22693 if ((TREE_PRIVATE (decl)
22694 != (current_access_specifier == access_private_node))
22695 || (TREE_PROTECTED (decl)
22696 != (current_access_specifier == access_protected_node)))
22697 error_at (location, "%qD redeclared with different access", decl);
22700 /* Look for the `template' keyword, as a syntactic disambiguator.
22701 Return TRUE iff it is present, in which case it will be
22705 cp_parser_optional_template_keyword (cp_parser *parser)
22707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22709 /* The `template' keyword can only be used within templates;
22710 outside templates the parser can always figure out what is a
22711 template and what is not. */
22712 if (!processing_template_decl)
22714 cp_token *token = cp_lexer_peek_token (parser->lexer);
22715 error_at (token->location,
22716 "%<template%> (as a disambiguator) is only allowed "
22717 "within templates");
22718 /* If this part of the token stream is rescanned, the same
22719 error message would be generated. So, we purge the token
22720 from the stream. */
22721 cp_lexer_purge_token (parser->lexer);
22726 /* Consume the `template' keyword. */
22727 cp_lexer_consume_token (parser->lexer);
22735 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22736 set PARSER->SCOPE, and perform other related actions. */
22739 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22742 struct tree_check *check_value;
22743 deferred_access_check *chk;
22744 VEC (deferred_access_check,gc) *checks;
22746 /* Get the stored value. */
22747 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22748 /* Perform any access checks that were deferred. */
22749 checks = check_value->checks;
22752 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22753 perform_or_defer_access_check (chk->binfo,
22757 /* Set the scope from the stored value. */
22758 parser->scope = check_value->value;
22759 parser->qualifying_scope = check_value->qualifying_scope;
22760 parser->object_scope = NULL_TREE;
22763 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22764 encounter the end of a block before what we were looking for. */
22767 cp_parser_cache_group (cp_parser *parser,
22768 enum cpp_ttype end,
22773 cp_token *token = cp_lexer_peek_token (parser->lexer);
22775 /* Abort a parenthesized expression if we encounter a semicolon. */
22776 if ((end == CPP_CLOSE_PAREN || depth == 0)
22777 && token->type == CPP_SEMICOLON)
22779 /* If we've reached the end of the file, stop. */
22780 if (token->type == CPP_EOF
22781 || (end != CPP_PRAGMA_EOL
22782 && token->type == CPP_PRAGMA_EOL))
22784 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22785 /* We've hit the end of an enclosing block, so there's been some
22786 kind of syntax error. */
22789 /* Consume the token. */
22790 cp_lexer_consume_token (parser->lexer);
22791 /* See if it starts a new group. */
22792 if (token->type == CPP_OPEN_BRACE)
22794 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22795 /* In theory this should probably check end == '}', but
22796 cp_parser_save_member_function_body needs it to exit
22797 after either '}' or ')' when called with ')'. */
22801 else if (token->type == CPP_OPEN_PAREN)
22803 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22804 if (depth == 0 && end == CPP_CLOSE_PAREN)
22807 else if (token->type == CPP_PRAGMA)
22808 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22809 else if (token->type == end)
22814 /* Like above, for caching a default argument or NSDMI. Both of these are
22815 terminated by a non-nested comma, but it can be unclear whether or not a
22816 comma is nested in a template argument list unless we do more parsing.
22817 In order to handle this ambiguity, when we encounter a ',' after a '<'
22818 we try to parse what follows as a parameter-declaration-list (in the
22819 case of a default argument) or a member-declarator (in the case of an
22820 NSDMI). If that succeeds, then we stop caching. */
22823 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22825 unsigned depth = 0;
22826 int maybe_template_id = 0;
22827 cp_token *first_token;
22829 tree default_argument;
22831 /* Add tokens until we have processed the entire default
22832 argument. We add the range [first_token, token). */
22833 first_token = cp_lexer_peek_token (parser->lexer);
22834 if (first_token->type == CPP_OPEN_BRACE)
22836 /* For list-initialization, this is straightforward. */
22837 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22838 token = cp_lexer_peek_token (parser->lexer);
22844 /* Peek at the next token. */
22845 token = cp_lexer_peek_token (parser->lexer);
22846 /* What we do depends on what token we have. */
22847 switch (token->type)
22849 /* In valid code, a default argument must be
22850 immediately followed by a `,' `)', or `...'. */
22852 if (depth == 0 && maybe_template_id)
22854 /* If we've seen a '<', we might be in a
22855 template-argument-list. Until Core issue 325 is
22856 resolved, we don't know how this situation ought
22857 to be handled, so try to DTRT. We check whether
22858 what comes after the comma is a valid parameter
22859 declaration list. If it is, then the comma ends
22860 the default argument; otherwise the default
22861 argument continues. */
22862 bool error = false;
22865 /* Set ITALP so cp_parser_parameter_declaration_list
22866 doesn't decide to commit to this parse. */
22867 bool saved_italp = parser->in_template_argument_list_p;
22868 parser->in_template_argument_list_p = true;
22870 cp_parser_parse_tentatively (parser);
22871 cp_lexer_consume_token (parser->lexer);
22875 int ctor_dtor_or_conv_p;
22876 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22877 &ctor_dtor_or_conv_p,
22878 /*parenthesized_p=*/NULL,
22879 /*member_p=*/true);
22883 begin_scope (sk_function_parms, NULL_TREE);
22884 cp_parser_parameter_declaration_list (parser, &error);
22885 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22886 pop_binding (DECL_NAME (t), t);
22889 if (!cp_parser_error_occurred (parser) && !error)
22891 cp_parser_abort_tentative_parse (parser);
22893 parser->in_template_argument_list_p = saved_italp;
22896 case CPP_CLOSE_PAREN:
22898 /* If we run into a non-nested `;', `}', or `]',
22899 then the code is invalid -- but the default
22900 argument is certainly over. */
22901 case CPP_SEMICOLON:
22902 case CPP_CLOSE_BRACE:
22903 case CPP_CLOSE_SQUARE:
22906 /* Update DEPTH, if necessary. */
22907 else if (token->type == CPP_CLOSE_PAREN
22908 || token->type == CPP_CLOSE_BRACE
22909 || token->type == CPP_CLOSE_SQUARE)
22913 case CPP_OPEN_PAREN:
22914 case CPP_OPEN_SQUARE:
22915 case CPP_OPEN_BRACE:
22921 /* This might be the comparison operator, or it might
22922 start a template argument list. */
22923 ++maybe_template_id;
22927 if (cxx_dialect == cxx98)
22929 /* Fall through for C++0x, which treats the `>>'
22930 operator like two `>' tokens in certain
22936 /* This might be an operator, or it might close a
22937 template argument list. But if a previous '<'
22938 started a template argument list, this will have
22939 closed it, so we can't be in one anymore. */
22940 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22941 if (maybe_template_id < 0)
22942 maybe_template_id = 0;
22946 /* If we run out of tokens, issue an error message. */
22948 case CPP_PRAGMA_EOL:
22949 error_at (token->location, "file ends in default argument");
22955 /* In these cases, we should look for template-ids.
22956 For example, if the default argument is
22957 `X<int, double>()', we need to do name lookup to
22958 figure out whether or not `X' is a template; if
22959 so, the `,' does not end the default argument.
22961 That is not yet done. */
22968 /* If we've reached the end, stop. */
22972 /* Add the token to the token block. */
22973 token = cp_lexer_consume_token (parser->lexer);
22976 /* Create a DEFAULT_ARG to represent the unparsed default
22978 default_argument = make_node (DEFAULT_ARG);
22979 DEFARG_TOKENS (default_argument)
22980 = cp_token_cache_new (first_token, token);
22981 DEFARG_INSTANTIATIONS (default_argument) = NULL;
22983 return default_argument;
22986 /* Begin parsing tentatively. We always save tokens while parsing
22987 tentatively so that if the tentative parsing fails we can restore the
22991 cp_parser_parse_tentatively (cp_parser* parser)
22993 /* Enter a new parsing context. */
22994 parser->context = cp_parser_context_new (parser->context);
22995 /* Begin saving tokens. */
22996 cp_lexer_save_tokens (parser->lexer);
22997 /* In order to avoid repetitive access control error messages,
22998 access checks are queued up until we are no longer parsing
23000 push_deferring_access_checks (dk_deferred);
23003 /* Commit to the currently active tentative parse. */
23006 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23008 cp_parser_context *context;
23011 /* Mark all of the levels as committed. */
23012 lexer = parser->lexer;
23013 for (context = parser->context; context->next; context = context->next)
23015 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23017 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23018 while (!cp_lexer_saving_tokens (lexer))
23019 lexer = lexer->next;
23020 cp_lexer_commit_tokens (lexer);
23024 /* Abort the currently active tentative parse. All consumed tokens
23025 will be rolled back, and no diagnostics will be issued. */
23028 cp_parser_abort_tentative_parse (cp_parser* parser)
23030 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23031 || errorcount > 0);
23032 cp_parser_simulate_error (parser);
23033 /* Now, pretend that we want to see if the construct was
23034 successfully parsed. */
23035 cp_parser_parse_definitely (parser);
23038 /* Stop parsing tentatively. If a parse error has occurred, restore the
23039 token stream. Otherwise, commit to the tokens we have consumed.
23040 Returns true if no error occurred; false otherwise. */
23043 cp_parser_parse_definitely (cp_parser* parser)
23045 bool error_occurred;
23046 cp_parser_context *context;
23048 /* Remember whether or not an error occurred, since we are about to
23049 destroy that information. */
23050 error_occurred = cp_parser_error_occurred (parser);
23051 /* Remove the topmost context from the stack. */
23052 context = parser->context;
23053 parser->context = context->next;
23054 /* If no parse errors occurred, commit to the tentative parse. */
23055 if (!error_occurred)
23057 /* Commit to the tokens read tentatively, unless that was
23059 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23060 cp_lexer_commit_tokens (parser->lexer);
23062 pop_to_parent_deferring_access_checks ();
23064 /* Otherwise, if errors occurred, roll back our state so that things
23065 are just as they were before we began the tentative parse. */
23068 cp_lexer_rollback_tokens (parser->lexer);
23069 pop_deferring_access_checks ();
23071 /* Add the context to the front of the free list. */
23072 context->next = cp_parser_context_free_list;
23073 cp_parser_context_free_list = context;
23075 return !error_occurred;
23078 /* Returns true if we are parsing tentatively and are not committed to
23079 this tentative parse. */
23082 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23084 return (cp_parser_parsing_tentatively (parser)
23085 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23088 /* Returns nonzero iff an error has occurred during the most recent
23089 tentative parse. */
23092 cp_parser_error_occurred (cp_parser* parser)
23094 return (cp_parser_parsing_tentatively (parser)
23095 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23098 /* Returns nonzero if GNU extensions are allowed. */
23101 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23103 return parser->allow_gnu_extensions_p;
23106 /* Objective-C++ Productions */
23109 /* Parse an Objective-C expression, which feeds into a primary-expression
23113 objc-message-expression
23114 objc-string-literal
23115 objc-encode-expression
23116 objc-protocol-expression
23117 objc-selector-expression
23119 Returns a tree representation of the expression. */
23122 cp_parser_objc_expression (cp_parser* parser)
23124 /* Try to figure out what kind of declaration is present. */
23125 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23129 case CPP_OPEN_SQUARE:
23130 return cp_parser_objc_message_expression (parser);
23132 case CPP_OBJC_STRING:
23133 kwd = cp_lexer_consume_token (parser->lexer);
23134 return objc_build_string_object (kwd->u.value);
23137 switch (kwd->keyword)
23139 case RID_AT_ENCODE:
23140 return cp_parser_objc_encode_expression (parser);
23142 case RID_AT_PROTOCOL:
23143 return cp_parser_objc_protocol_expression (parser);
23145 case RID_AT_SELECTOR:
23146 return cp_parser_objc_selector_expression (parser);
23152 error_at (kwd->location,
23153 "misplaced %<@%D%> Objective-C++ construct",
23155 cp_parser_skip_to_end_of_block_or_statement (parser);
23158 return error_mark_node;
23161 /* Parse an Objective-C message expression.
23163 objc-message-expression:
23164 [ objc-message-receiver objc-message-args ]
23166 Returns a representation of an Objective-C message. */
23169 cp_parser_objc_message_expression (cp_parser* parser)
23171 tree receiver, messageargs;
23173 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23174 receiver = cp_parser_objc_message_receiver (parser);
23175 messageargs = cp_parser_objc_message_args (parser);
23176 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23178 return objc_build_message_expr (receiver, messageargs);
23181 /* Parse an objc-message-receiver.
23183 objc-message-receiver:
23185 simple-type-specifier
23187 Returns a representation of the type or expression. */
23190 cp_parser_objc_message_receiver (cp_parser* parser)
23194 /* An Objective-C message receiver may be either (1) a type
23195 or (2) an expression. */
23196 cp_parser_parse_tentatively (parser);
23197 rcv = cp_parser_expression (parser, false, NULL);
23199 if (cp_parser_parse_definitely (parser))
23202 rcv = cp_parser_simple_type_specifier (parser,
23203 /*decl_specs=*/NULL,
23204 CP_PARSER_FLAGS_NONE);
23206 return objc_get_class_reference (rcv);
23209 /* Parse the arguments and selectors comprising an Objective-C message.
23214 objc-selector-args , objc-comma-args
23216 objc-selector-args:
23217 objc-selector [opt] : assignment-expression
23218 objc-selector-args objc-selector [opt] : assignment-expression
23221 assignment-expression
23222 objc-comma-args , assignment-expression
23224 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23225 selector arguments and TREE_VALUE containing a list of comma
23229 cp_parser_objc_message_args (cp_parser* parser)
23231 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23232 bool maybe_unary_selector_p = true;
23233 cp_token *token = cp_lexer_peek_token (parser->lexer);
23235 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23237 tree selector = NULL_TREE, arg;
23239 if (token->type != CPP_COLON)
23240 selector = cp_parser_objc_selector (parser);
23242 /* Detect if we have a unary selector. */
23243 if (maybe_unary_selector_p
23244 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23245 return build_tree_list (selector, NULL_TREE);
23247 maybe_unary_selector_p = false;
23248 cp_parser_require (parser, CPP_COLON, RT_COLON);
23249 arg = cp_parser_assignment_expression (parser, false, NULL);
23252 = chainon (sel_args,
23253 build_tree_list (selector, arg));
23255 token = cp_lexer_peek_token (parser->lexer);
23258 /* Handle non-selector arguments, if any. */
23259 while (token->type == CPP_COMMA)
23263 cp_lexer_consume_token (parser->lexer);
23264 arg = cp_parser_assignment_expression (parser, false, NULL);
23267 = chainon (addl_args,
23268 build_tree_list (NULL_TREE, arg));
23270 token = cp_lexer_peek_token (parser->lexer);
23273 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23275 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23276 return build_tree_list (error_mark_node, error_mark_node);
23279 return build_tree_list (sel_args, addl_args);
23282 /* Parse an Objective-C encode expression.
23284 objc-encode-expression:
23285 @encode objc-typename
23287 Returns an encoded representation of the type argument. */
23290 cp_parser_objc_encode_expression (cp_parser* parser)
23295 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23296 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23297 token = cp_lexer_peek_token (parser->lexer);
23298 type = complete_type (cp_parser_type_id (parser));
23299 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23303 error_at (token->location,
23304 "%<@encode%> must specify a type as an argument");
23305 return error_mark_node;
23308 /* This happens if we find @encode(T) (where T is a template
23309 typename or something dependent on a template typename) when
23310 parsing a template. In that case, we can't compile it
23311 immediately, but we rather create an AT_ENCODE_EXPR which will
23312 need to be instantiated when the template is used.
23314 if (dependent_type_p (type))
23316 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23317 TREE_READONLY (value) = 1;
23321 return objc_build_encode_expr (type);
23324 /* Parse an Objective-C @defs expression. */
23327 cp_parser_objc_defs_expression (cp_parser *parser)
23331 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23332 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23333 name = cp_parser_identifier (parser);
23334 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23336 return objc_get_class_ivars (name);
23339 /* Parse an Objective-C protocol expression.
23341 objc-protocol-expression:
23342 @protocol ( identifier )
23344 Returns a representation of the protocol expression. */
23347 cp_parser_objc_protocol_expression (cp_parser* parser)
23351 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23352 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23353 proto = cp_parser_identifier (parser);
23354 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23356 return objc_build_protocol_expr (proto);
23359 /* Parse an Objective-C selector expression.
23361 objc-selector-expression:
23362 @selector ( objc-method-signature )
23364 objc-method-signature:
23370 objc-selector-seq objc-selector :
23372 Returns a representation of the method selector. */
23375 cp_parser_objc_selector_expression (cp_parser* parser)
23377 tree sel_seq = NULL_TREE;
23378 bool maybe_unary_selector_p = true;
23380 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23382 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23383 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23384 token = cp_lexer_peek_token (parser->lexer);
23386 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23387 || token->type == CPP_SCOPE)
23389 tree selector = NULL_TREE;
23391 if (token->type != CPP_COLON
23392 || token->type == CPP_SCOPE)
23393 selector = cp_parser_objc_selector (parser);
23395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23396 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23398 /* Detect if we have a unary selector. */
23399 if (maybe_unary_selector_p)
23401 sel_seq = selector;
23402 goto finish_selector;
23406 cp_parser_error (parser, "expected %<:%>");
23409 maybe_unary_selector_p = false;
23410 token = cp_lexer_consume_token (parser->lexer);
23412 if (token->type == CPP_SCOPE)
23415 = chainon (sel_seq,
23416 build_tree_list (selector, NULL_TREE));
23418 = chainon (sel_seq,
23419 build_tree_list (NULL_TREE, NULL_TREE));
23423 = chainon (sel_seq,
23424 build_tree_list (selector, NULL_TREE));
23426 token = cp_lexer_peek_token (parser->lexer);
23430 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23432 return objc_build_selector_expr (loc, sel_seq);
23435 /* Parse a list of identifiers.
23437 objc-identifier-list:
23439 objc-identifier-list , identifier
23441 Returns a TREE_LIST of identifier nodes. */
23444 cp_parser_objc_identifier_list (cp_parser* parser)
23450 identifier = cp_parser_identifier (parser);
23451 if (identifier == error_mark_node)
23452 return error_mark_node;
23454 list = build_tree_list (NULL_TREE, identifier);
23455 sep = cp_lexer_peek_token (parser->lexer);
23457 while (sep->type == CPP_COMMA)
23459 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23460 identifier = cp_parser_identifier (parser);
23461 if (identifier == error_mark_node)
23464 list = chainon (list, build_tree_list (NULL_TREE,
23466 sep = cp_lexer_peek_token (parser->lexer);
23472 /* Parse an Objective-C alias declaration.
23474 objc-alias-declaration:
23475 @compatibility_alias identifier identifier ;
23477 This function registers the alias mapping with the Objective-C front end.
23478 It returns nothing. */
23481 cp_parser_objc_alias_declaration (cp_parser* parser)
23485 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23486 alias = cp_parser_identifier (parser);
23487 orig = cp_parser_identifier (parser);
23488 objc_declare_alias (alias, orig);
23489 cp_parser_consume_semicolon_at_end_of_statement (parser);
23492 /* Parse an Objective-C class forward-declaration.
23494 objc-class-declaration:
23495 @class objc-identifier-list ;
23497 The function registers the forward declarations with the Objective-C
23498 front end. It returns nothing. */
23501 cp_parser_objc_class_declaration (cp_parser* parser)
23503 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23508 id = cp_parser_identifier (parser);
23509 if (id == error_mark_node)
23512 objc_declare_class (id);
23514 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23515 cp_lexer_consume_token (parser->lexer);
23519 cp_parser_consume_semicolon_at_end_of_statement (parser);
23522 /* Parse a list of Objective-C protocol references.
23524 objc-protocol-refs-opt:
23525 objc-protocol-refs [opt]
23527 objc-protocol-refs:
23528 < objc-identifier-list >
23530 Returns a TREE_LIST of identifiers, if any. */
23533 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23535 tree protorefs = NULL_TREE;
23537 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23539 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23540 protorefs = cp_parser_objc_identifier_list (parser);
23541 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23547 /* Parse a Objective-C visibility specification. */
23550 cp_parser_objc_visibility_spec (cp_parser* parser)
23552 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23554 switch (vis->keyword)
23556 case RID_AT_PRIVATE:
23557 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23559 case RID_AT_PROTECTED:
23560 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23562 case RID_AT_PUBLIC:
23563 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23565 case RID_AT_PACKAGE:
23566 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23572 /* Eat '@private'/'@protected'/'@public'. */
23573 cp_lexer_consume_token (parser->lexer);
23576 /* Parse an Objective-C method type. Return 'true' if it is a class
23577 (+) method, and 'false' if it is an instance (-) method. */
23580 cp_parser_objc_method_type (cp_parser* parser)
23582 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23588 /* Parse an Objective-C protocol qualifier. */
23591 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23593 tree quals = NULL_TREE, node;
23594 cp_token *token = cp_lexer_peek_token (parser->lexer);
23596 node = token->u.value;
23598 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23599 && (node == ridpointers [(int) RID_IN]
23600 || node == ridpointers [(int) RID_OUT]
23601 || node == ridpointers [(int) RID_INOUT]
23602 || node == ridpointers [(int) RID_BYCOPY]
23603 || node == ridpointers [(int) RID_BYREF]
23604 || node == ridpointers [(int) RID_ONEWAY]))
23606 quals = tree_cons (NULL_TREE, node, quals);
23607 cp_lexer_consume_token (parser->lexer);
23608 token = cp_lexer_peek_token (parser->lexer);
23609 node = token->u.value;
23615 /* Parse an Objective-C typename. */
23618 cp_parser_objc_typename (cp_parser* parser)
23620 tree type_name = NULL_TREE;
23622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23624 tree proto_quals, cp_type = NULL_TREE;
23626 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23627 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23629 /* An ObjC type name may consist of just protocol qualifiers, in which
23630 case the type shall default to 'id'. */
23631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23633 cp_type = cp_parser_type_id (parser);
23635 /* If the type could not be parsed, an error has already
23636 been produced. For error recovery, behave as if it had
23637 not been specified, which will use the default type
23639 if (cp_type == error_mark_node)
23641 cp_type = NULL_TREE;
23642 /* We need to skip to the closing parenthesis as
23643 cp_parser_type_id() does not seem to do it for
23645 cp_parser_skip_to_closing_parenthesis (parser,
23646 /*recovering=*/true,
23647 /*or_comma=*/false,
23648 /*consume_paren=*/false);
23652 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23653 type_name = build_tree_list (proto_quals, cp_type);
23659 /* Check to see if TYPE refers to an Objective-C selector name. */
23662 cp_parser_objc_selector_p (enum cpp_ttype type)
23664 return (type == CPP_NAME || type == CPP_KEYWORD
23665 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23666 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23667 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23668 || type == CPP_XOR || type == CPP_XOR_EQ);
23671 /* Parse an Objective-C selector. */
23674 cp_parser_objc_selector (cp_parser* parser)
23676 cp_token *token = cp_lexer_consume_token (parser->lexer);
23678 if (!cp_parser_objc_selector_p (token->type))
23680 error_at (token->location, "invalid Objective-C++ selector name");
23681 return error_mark_node;
23684 /* C++ operator names are allowed to appear in ObjC selectors. */
23685 switch (token->type)
23687 case CPP_AND_AND: return get_identifier ("and");
23688 case CPP_AND_EQ: return get_identifier ("and_eq");
23689 case CPP_AND: return get_identifier ("bitand");
23690 case CPP_OR: return get_identifier ("bitor");
23691 case CPP_COMPL: return get_identifier ("compl");
23692 case CPP_NOT: return get_identifier ("not");
23693 case CPP_NOT_EQ: return get_identifier ("not_eq");
23694 case CPP_OR_OR: return get_identifier ("or");
23695 case CPP_OR_EQ: return get_identifier ("or_eq");
23696 case CPP_XOR: return get_identifier ("xor");
23697 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23698 default: return token->u.value;
23702 /* Parse an Objective-C params list. */
23705 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23707 tree params = NULL_TREE;
23708 bool maybe_unary_selector_p = true;
23709 cp_token *token = cp_lexer_peek_token (parser->lexer);
23711 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23713 tree selector = NULL_TREE, type_name, identifier;
23714 tree parm_attr = NULL_TREE;
23716 if (token->keyword == RID_ATTRIBUTE)
23719 if (token->type != CPP_COLON)
23720 selector = cp_parser_objc_selector (parser);
23722 /* Detect if we have a unary selector. */
23723 if (maybe_unary_selector_p
23724 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23726 params = selector; /* Might be followed by attributes. */
23730 maybe_unary_selector_p = false;
23731 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23733 /* Something went quite wrong. There should be a colon
23734 here, but there is not. Stop parsing parameters. */
23737 type_name = cp_parser_objc_typename (parser);
23738 /* New ObjC allows attributes on parameters too. */
23739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23740 parm_attr = cp_parser_attributes_opt (parser);
23741 identifier = cp_parser_identifier (parser);
23745 objc_build_keyword_decl (selector,
23750 token = cp_lexer_peek_token (parser->lexer);
23753 if (params == NULL_TREE)
23755 cp_parser_error (parser, "objective-c++ method declaration is expected");
23756 return error_mark_node;
23759 /* We allow tail attributes for the method. */
23760 if (token->keyword == RID_ATTRIBUTE)
23762 *attributes = cp_parser_attributes_opt (parser);
23763 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23764 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23766 cp_parser_error (parser,
23767 "method attributes must be specified at the end");
23768 return error_mark_node;
23771 if (params == NULL_TREE)
23773 cp_parser_error (parser, "objective-c++ method declaration is expected");
23774 return error_mark_node;
23779 /* Parse the non-keyword Objective-C params. */
23782 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23785 tree params = make_node (TREE_LIST);
23786 cp_token *token = cp_lexer_peek_token (parser->lexer);
23787 *ellipsisp = false; /* Initially, assume no ellipsis. */
23789 while (token->type == CPP_COMMA)
23791 cp_parameter_declarator *parmdecl;
23794 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23795 token = cp_lexer_peek_token (parser->lexer);
23797 if (token->type == CPP_ELLIPSIS)
23799 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23801 token = cp_lexer_peek_token (parser->lexer);
23805 /* TODO: parse attributes for tail parameters. */
23806 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23807 parm = grokdeclarator (parmdecl->declarator,
23808 &parmdecl->decl_specifiers,
23809 PARM, /*initialized=*/0,
23810 /*attrlist=*/NULL);
23812 chainon (params, build_tree_list (NULL_TREE, parm));
23813 token = cp_lexer_peek_token (parser->lexer);
23816 /* We allow tail attributes for the method. */
23817 if (token->keyword == RID_ATTRIBUTE)
23819 if (*attributes == NULL_TREE)
23821 *attributes = cp_parser_attributes_opt (parser);
23822 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23823 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23827 /* We have an error, but parse the attributes, so that we can
23829 *attributes = cp_parser_attributes_opt (parser);
23831 cp_parser_error (parser,
23832 "method attributes must be specified at the end");
23833 return error_mark_node;
23839 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23842 cp_parser_objc_interstitial_code (cp_parser* parser)
23844 cp_token *token = cp_lexer_peek_token (parser->lexer);
23846 /* If the next token is `extern' and the following token is a string
23847 literal, then we have a linkage specification. */
23848 if (token->keyword == RID_EXTERN
23849 && cp_parser_is_pure_string_literal
23850 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23851 cp_parser_linkage_specification (parser);
23852 /* Handle #pragma, if any. */
23853 else if (token->type == CPP_PRAGMA)
23854 cp_parser_pragma (parser, pragma_external);
23855 /* Allow stray semicolons. */
23856 else if (token->type == CPP_SEMICOLON)
23857 cp_lexer_consume_token (parser->lexer);
23858 /* Mark methods as optional or required, when building protocols. */
23859 else if (token->keyword == RID_AT_OPTIONAL)
23861 cp_lexer_consume_token (parser->lexer);
23862 objc_set_method_opt (true);
23864 else if (token->keyword == RID_AT_REQUIRED)
23866 cp_lexer_consume_token (parser->lexer);
23867 objc_set_method_opt (false);
23869 else if (token->keyword == RID_NAMESPACE)
23870 cp_parser_namespace_definition (parser);
23871 /* Other stray characters must generate errors. */
23872 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23874 cp_lexer_consume_token (parser->lexer);
23875 error ("stray %qs between Objective-C++ methods",
23876 token->type == CPP_OPEN_BRACE ? "{" : "}");
23878 /* Finally, try to parse a block-declaration, or a function-definition. */
23880 cp_parser_block_declaration (parser, /*statement_p=*/false);
23883 /* Parse a method signature. */
23886 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23888 tree rettype, kwdparms, optparms;
23889 bool ellipsis = false;
23890 bool is_class_method;
23892 is_class_method = cp_parser_objc_method_type (parser);
23893 rettype = cp_parser_objc_typename (parser);
23894 *attributes = NULL_TREE;
23895 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23896 if (kwdparms == error_mark_node)
23897 return error_mark_node;
23898 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23899 if (optparms == error_mark_node)
23900 return error_mark_node;
23902 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23906 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23909 cp_lexer_save_tokens (parser->lexer);
23910 tattr = cp_parser_attributes_opt (parser);
23911 gcc_assert (tattr) ;
23913 /* If the attributes are followed by a method introducer, this is not allowed.
23914 Dump the attributes and flag the situation. */
23915 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23916 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23919 /* Otherwise, the attributes introduce some interstitial code, possibly so
23920 rewind to allow that check. */
23921 cp_lexer_rollback_tokens (parser->lexer);
23925 /* Parse an Objective-C method prototype list. */
23928 cp_parser_objc_method_prototype_list (cp_parser* parser)
23930 cp_token *token = cp_lexer_peek_token (parser->lexer);
23932 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23934 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23936 tree attributes, sig;
23937 bool is_class_method;
23938 if (token->type == CPP_PLUS)
23939 is_class_method = true;
23941 is_class_method = false;
23942 sig = cp_parser_objc_method_signature (parser, &attributes);
23943 if (sig == error_mark_node)
23945 cp_parser_skip_to_end_of_block_or_statement (parser);
23946 token = cp_lexer_peek_token (parser->lexer);
23949 objc_add_method_declaration (is_class_method, sig, attributes);
23950 cp_parser_consume_semicolon_at_end_of_statement (parser);
23952 else if (token->keyword == RID_AT_PROPERTY)
23953 cp_parser_objc_at_property_declaration (parser);
23954 else if (token->keyword == RID_ATTRIBUTE
23955 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23956 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23958 "prefix attributes are ignored for methods");
23960 /* Allow for interspersed non-ObjC++ code. */
23961 cp_parser_objc_interstitial_code (parser);
23963 token = cp_lexer_peek_token (parser->lexer);
23966 if (token->type != CPP_EOF)
23967 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23969 cp_parser_error (parser, "expected %<@end%>");
23971 objc_finish_interface ();
23974 /* Parse an Objective-C method definition list. */
23977 cp_parser_objc_method_definition_list (cp_parser* parser)
23979 cp_token *token = cp_lexer_peek_token (parser->lexer);
23981 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23985 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23988 tree sig, attribute;
23989 bool is_class_method;
23990 if (token->type == CPP_PLUS)
23991 is_class_method = true;
23993 is_class_method = false;
23994 push_deferring_access_checks (dk_deferred);
23995 sig = cp_parser_objc_method_signature (parser, &attribute);
23996 if (sig == error_mark_node)
23998 cp_parser_skip_to_end_of_block_or_statement (parser);
23999 token = cp_lexer_peek_token (parser->lexer);
24002 objc_start_method_definition (is_class_method, sig, attribute,
24005 /* For historical reasons, we accept an optional semicolon. */
24006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24007 cp_lexer_consume_token (parser->lexer);
24009 ptk = cp_lexer_peek_token (parser->lexer);
24010 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24011 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24013 perform_deferred_access_checks ();
24014 stop_deferring_access_checks ();
24015 meth = cp_parser_function_definition_after_declarator (parser,
24017 pop_deferring_access_checks ();
24018 objc_finish_method_definition (meth);
24021 /* The following case will be removed once @synthesize is
24022 completely implemented. */
24023 else if (token->keyword == RID_AT_PROPERTY)
24024 cp_parser_objc_at_property_declaration (parser);
24025 else if (token->keyword == RID_AT_SYNTHESIZE)
24026 cp_parser_objc_at_synthesize_declaration (parser);
24027 else if (token->keyword == RID_AT_DYNAMIC)
24028 cp_parser_objc_at_dynamic_declaration (parser);
24029 else if (token->keyword == RID_ATTRIBUTE
24030 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24031 warning_at (token->location, OPT_Wattributes,
24032 "prefix attributes are ignored for methods");
24034 /* Allow for interspersed non-ObjC++ code. */
24035 cp_parser_objc_interstitial_code (parser);
24037 token = cp_lexer_peek_token (parser->lexer);
24040 if (token->type != CPP_EOF)
24041 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24043 cp_parser_error (parser, "expected %<@end%>");
24045 objc_finish_implementation ();
24048 /* Parse Objective-C ivars. */
24051 cp_parser_objc_class_ivars (cp_parser* parser)
24053 cp_token *token = cp_lexer_peek_token (parser->lexer);
24055 if (token->type != CPP_OPEN_BRACE)
24056 return; /* No ivars specified. */
24058 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24059 token = cp_lexer_peek_token (parser->lexer);
24061 while (token->type != CPP_CLOSE_BRACE
24062 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24064 cp_decl_specifier_seq declspecs;
24065 int decl_class_or_enum_p;
24066 tree prefix_attributes;
24068 cp_parser_objc_visibility_spec (parser);
24070 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24073 cp_parser_decl_specifier_seq (parser,
24074 CP_PARSER_FLAGS_OPTIONAL,
24076 &decl_class_or_enum_p);
24078 /* auto, register, static, extern, mutable. */
24079 if (declspecs.storage_class != sc_none)
24081 cp_parser_error (parser, "invalid type for instance variable");
24082 declspecs.storage_class = sc_none;
24086 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24088 cp_parser_error (parser, "invalid type for instance variable");
24089 declspecs.locations[ds_thread] = 0;
24093 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24095 cp_parser_error (parser, "invalid type for instance variable");
24096 declspecs.locations[ds_thread] = 0;
24099 prefix_attributes = declspecs.attributes;
24100 declspecs.attributes = NULL_TREE;
24102 /* Keep going until we hit the `;' at the end of the
24104 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24106 tree width = NULL_TREE, attributes, first_attribute, decl;
24107 cp_declarator *declarator = NULL;
24108 int ctor_dtor_or_conv_p;
24110 /* Check for a (possibly unnamed) bitfield declaration. */
24111 token = cp_lexer_peek_token (parser->lexer);
24112 if (token->type == CPP_COLON)
24115 if (token->type == CPP_NAME
24116 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24119 /* Get the name of the bitfield. */
24120 declarator = make_id_declarator (NULL_TREE,
24121 cp_parser_identifier (parser),
24125 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24126 /* Get the width of the bitfield. */
24128 = cp_parser_constant_expression (parser,
24129 /*allow_non_constant=*/false,
24134 /* Parse the declarator. */
24136 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24137 &ctor_dtor_or_conv_p,
24138 /*parenthesized_p=*/NULL,
24139 /*member_p=*/false);
24142 /* Look for attributes that apply to the ivar. */
24143 attributes = cp_parser_attributes_opt (parser);
24144 /* Remember which attributes are prefix attributes and
24146 first_attribute = attributes;
24147 /* Combine the attributes. */
24148 attributes = chainon (prefix_attributes, attributes);
24151 /* Create the bitfield declaration. */
24152 decl = grokbitfield (declarator, &declspecs,
24156 decl = grokfield (declarator, &declspecs,
24157 NULL_TREE, /*init_const_expr_p=*/false,
24158 NULL_TREE, attributes);
24160 /* Add the instance variable. */
24161 if (decl != error_mark_node && decl != NULL_TREE)
24162 objc_add_instance_variable (decl);
24164 /* Reset PREFIX_ATTRIBUTES. */
24165 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24166 attributes = TREE_CHAIN (attributes);
24168 TREE_CHAIN (attributes) = NULL_TREE;
24170 token = cp_lexer_peek_token (parser->lexer);
24172 if (token->type == CPP_COMMA)
24174 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24180 cp_parser_consume_semicolon_at_end_of_statement (parser);
24181 token = cp_lexer_peek_token (parser->lexer);
24184 if (token->keyword == RID_AT_END)
24185 cp_parser_error (parser, "expected %<}%>");
24187 /* Do not consume the RID_AT_END, so it will be read again as terminating
24188 the @interface of @implementation. */
24189 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24190 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24192 /* For historical reasons, we accept an optional semicolon. */
24193 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24194 cp_lexer_consume_token (parser->lexer);
24197 /* Parse an Objective-C protocol declaration. */
24200 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24202 tree proto, protorefs;
24205 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24208 tok = cp_lexer_peek_token (parser->lexer);
24209 error_at (tok->location, "identifier expected after %<@protocol%>");
24210 cp_parser_consume_semicolon_at_end_of_statement (parser);
24214 /* See if we have a forward declaration or a definition. */
24215 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24217 /* Try a forward declaration first. */
24218 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24224 id = cp_parser_identifier (parser);
24225 if (id == error_mark_node)
24228 objc_declare_protocol (id, attributes);
24230 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24231 cp_lexer_consume_token (parser->lexer);
24235 cp_parser_consume_semicolon_at_end_of_statement (parser);
24238 /* Ok, we got a full-fledged definition (or at least should). */
24241 proto = cp_parser_identifier (parser);
24242 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24243 objc_start_protocol (proto, protorefs, attributes);
24244 cp_parser_objc_method_prototype_list (parser);
24248 /* Parse an Objective-C superclass or category. */
24251 cp_parser_objc_superclass_or_category (cp_parser *parser,
24254 tree *categ, bool *is_class_extension)
24256 cp_token *next = cp_lexer_peek_token (parser->lexer);
24258 *super = *categ = NULL_TREE;
24259 *is_class_extension = false;
24260 if (next->type == CPP_COLON)
24262 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24263 *super = cp_parser_identifier (parser);
24265 else if (next->type == CPP_OPEN_PAREN)
24267 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24269 /* If there is no category name, and this is an @interface, we
24270 have a class extension. */
24271 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24273 *categ = NULL_TREE;
24274 *is_class_extension = true;
24277 *categ = cp_parser_identifier (parser);
24279 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24283 /* Parse an Objective-C class interface. */
24286 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24288 tree name, super, categ, protos;
24289 bool is_class_extension;
24291 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24292 name = cp_parser_identifier (parser);
24293 if (name == error_mark_node)
24295 /* It's hard to recover because even if valid @interface stuff
24296 is to follow, we can't compile it (or validate it) if we
24297 don't even know which class it refers to. Let's assume this
24298 was a stray '@interface' token in the stream and skip it.
24302 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24303 &is_class_extension);
24304 protos = cp_parser_objc_protocol_refs_opt (parser);
24306 /* We have either a class or a category on our hands. */
24307 if (categ || is_class_extension)
24308 objc_start_category_interface (name, categ, protos, attributes);
24311 objc_start_class_interface (name, super, protos, attributes);
24312 /* Handle instance variable declarations, if any. */
24313 cp_parser_objc_class_ivars (parser);
24314 objc_continue_interface ();
24317 cp_parser_objc_method_prototype_list (parser);
24320 /* Parse an Objective-C class implementation. */
24323 cp_parser_objc_class_implementation (cp_parser* parser)
24325 tree name, super, categ;
24326 bool is_class_extension;
24328 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24329 name = cp_parser_identifier (parser);
24330 if (name == error_mark_node)
24332 /* It's hard to recover because even if valid @implementation
24333 stuff is to follow, we can't compile it (or validate it) if
24334 we don't even know which class it refers to. Let's assume
24335 this was a stray '@implementation' token in the stream and
24340 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24341 &is_class_extension);
24343 /* We have either a class or a category on our hands. */
24345 objc_start_category_implementation (name, categ);
24348 objc_start_class_implementation (name, super);
24349 /* Handle instance variable declarations, if any. */
24350 cp_parser_objc_class_ivars (parser);
24351 objc_continue_implementation ();
24354 cp_parser_objc_method_definition_list (parser);
24357 /* Consume the @end token and finish off the implementation. */
24360 cp_parser_objc_end_implementation (cp_parser* parser)
24362 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24363 objc_finish_implementation ();
24366 /* Parse an Objective-C declaration. */
24369 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24371 /* Try to figure out what kind of declaration is present. */
24372 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24375 switch (kwd->keyword)
24380 error_at (kwd->location, "attributes may not be specified before"
24381 " the %<@%D%> Objective-C++ keyword",
24385 case RID_AT_IMPLEMENTATION:
24386 warning_at (kwd->location, OPT_Wattributes,
24387 "prefix attributes are ignored before %<@%D%>",
24394 switch (kwd->keyword)
24397 cp_parser_objc_alias_declaration (parser);
24400 cp_parser_objc_class_declaration (parser);
24402 case RID_AT_PROTOCOL:
24403 cp_parser_objc_protocol_declaration (parser, attributes);
24405 case RID_AT_INTERFACE:
24406 cp_parser_objc_class_interface (parser, attributes);
24408 case RID_AT_IMPLEMENTATION:
24409 cp_parser_objc_class_implementation (parser);
24412 cp_parser_objc_end_implementation (parser);
24415 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24417 cp_parser_skip_to_end_of_block_or_statement (parser);
24421 /* Parse an Objective-C try-catch-finally statement.
24423 objc-try-catch-finally-stmt:
24424 @try compound-statement objc-catch-clause-seq [opt]
24425 objc-finally-clause [opt]
24427 objc-catch-clause-seq:
24428 objc-catch-clause objc-catch-clause-seq [opt]
24431 @catch ( objc-exception-declaration ) compound-statement
24433 objc-finally-clause:
24434 @finally compound-statement
24436 objc-exception-declaration:
24437 parameter-declaration
24440 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24444 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24445 for C. Keep them in sync. */
24448 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24450 location_t location;
24453 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24454 location = cp_lexer_peek_token (parser->lexer)->location;
24455 objc_maybe_warn_exceptions (location);
24456 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24457 node, lest it get absorbed into the surrounding block. */
24458 stmt = push_stmt_list ();
24459 cp_parser_compound_statement (parser, NULL, false, false);
24460 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24462 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24464 cp_parameter_declarator *parm;
24465 tree parameter_declaration = error_mark_node;
24466 bool seen_open_paren = false;
24468 cp_lexer_consume_token (parser->lexer);
24469 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24470 seen_open_paren = true;
24471 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24473 /* We have "@catch (...)" (where the '...' are literally
24474 what is in the code). Skip the '...'.
24475 parameter_declaration is set to NULL_TREE, and
24476 objc_being_catch_clauses() knows that that means
24478 cp_lexer_consume_token (parser->lexer);
24479 parameter_declaration = NULL_TREE;
24483 /* We have "@catch (NSException *exception)" or something
24484 like that. Parse the parameter declaration. */
24485 parm = cp_parser_parameter_declaration (parser, false, NULL);
24487 parameter_declaration = error_mark_node;
24489 parameter_declaration = grokdeclarator (parm->declarator,
24490 &parm->decl_specifiers,
24491 PARM, /*initialized=*/0,
24492 /*attrlist=*/NULL);
24494 if (seen_open_paren)
24495 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24498 /* If there was no open parenthesis, we are recovering from
24499 an error, and we are trying to figure out what mistake
24500 the user has made. */
24502 /* If there is an immediate closing parenthesis, the user
24503 probably forgot the opening one (ie, they typed "@catch
24504 NSException *e)". Parse the closing parenthesis and keep
24506 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24507 cp_lexer_consume_token (parser->lexer);
24509 /* If these is no immediate closing parenthesis, the user
24510 probably doesn't know that parenthesis are required at
24511 all (ie, they typed "@catch NSException *e"). So, just
24512 forget about the closing parenthesis and keep going. */
24514 objc_begin_catch_clause (parameter_declaration);
24515 cp_parser_compound_statement (parser, NULL, false, false);
24516 objc_finish_catch_clause ();
24518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24520 cp_lexer_consume_token (parser->lexer);
24521 location = cp_lexer_peek_token (parser->lexer)->location;
24522 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24523 node, lest it get absorbed into the surrounding block. */
24524 stmt = push_stmt_list ();
24525 cp_parser_compound_statement (parser, NULL, false, false);
24526 objc_build_finally_clause (location, pop_stmt_list (stmt));
24529 return objc_finish_try_stmt ();
24532 /* Parse an Objective-C synchronized statement.
24534 objc-synchronized-stmt:
24535 @synchronized ( expression ) compound-statement
24537 Returns NULL_TREE. */
24540 cp_parser_objc_synchronized_statement (cp_parser *parser)
24542 location_t location;
24545 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24547 location = cp_lexer_peek_token (parser->lexer)->location;
24548 objc_maybe_warn_exceptions (location);
24549 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24550 lock = cp_parser_expression (parser, false, NULL);
24551 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24553 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24554 node, lest it get absorbed into the surrounding block. */
24555 stmt = push_stmt_list ();
24556 cp_parser_compound_statement (parser, NULL, false, false);
24558 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24561 /* Parse an Objective-C throw statement.
24564 @throw assignment-expression [opt] ;
24566 Returns a constructed '@throw' statement. */
24569 cp_parser_objc_throw_statement (cp_parser *parser)
24571 tree expr = NULL_TREE;
24572 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24574 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24577 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24579 cp_parser_consume_semicolon_at_end_of_statement (parser);
24581 return objc_build_throw_stmt (loc, expr);
24584 /* Parse an Objective-C statement. */
24587 cp_parser_objc_statement (cp_parser * parser)
24589 /* Try to figure out what kind of declaration is present. */
24590 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24592 switch (kwd->keyword)
24595 return cp_parser_objc_try_catch_finally_statement (parser);
24596 case RID_AT_SYNCHRONIZED:
24597 return cp_parser_objc_synchronized_statement (parser);
24599 return cp_parser_objc_throw_statement (parser);
24601 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24603 cp_parser_skip_to_end_of_block_or_statement (parser);
24606 return error_mark_node;
24609 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24610 look ahead to see if an objc keyword follows the attributes. This
24611 is to detect the use of prefix attributes on ObjC @interface and
24615 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24617 cp_lexer_save_tokens (parser->lexer);
24618 *attrib = cp_parser_attributes_opt (parser);
24619 gcc_assert (*attrib);
24620 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24622 cp_lexer_commit_tokens (parser->lexer);
24625 cp_lexer_rollback_tokens (parser->lexer);
24629 /* This routine is a minimal replacement for
24630 c_parser_struct_declaration () used when parsing the list of
24631 types/names or ObjC++ properties. For example, when parsing the
24634 @property (readonly) int a, b, c;
24636 this function is responsible for parsing "int a, int b, int c" and
24637 returning the declarations as CHAIN of DECLs.
24639 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24640 similar parsing. */
24642 cp_parser_objc_struct_declaration (cp_parser *parser)
24644 tree decls = NULL_TREE;
24645 cp_decl_specifier_seq declspecs;
24646 int decl_class_or_enum_p;
24647 tree prefix_attributes;
24649 cp_parser_decl_specifier_seq (parser,
24650 CP_PARSER_FLAGS_NONE,
24652 &decl_class_or_enum_p);
24654 if (declspecs.type == error_mark_node)
24655 return error_mark_node;
24657 /* auto, register, static, extern, mutable. */
24658 if (declspecs.storage_class != sc_none)
24660 cp_parser_error (parser, "invalid type for property");
24661 declspecs.storage_class = sc_none;
24665 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24667 cp_parser_error (parser, "invalid type for property");
24668 declspecs.locations[ds_thread] = 0;
24672 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24674 cp_parser_error (parser, "invalid type for property");
24675 declspecs.locations[ds_typedef] = 0;
24678 prefix_attributes = declspecs.attributes;
24679 declspecs.attributes = NULL_TREE;
24681 /* Keep going until we hit the `;' at the end of the declaration. */
24682 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24684 tree attributes, first_attribute, decl;
24685 cp_declarator *declarator;
24688 /* Parse the declarator. */
24689 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24690 NULL, NULL, false);
24692 /* Look for attributes that apply to the ivar. */
24693 attributes = cp_parser_attributes_opt (parser);
24694 /* Remember which attributes are prefix attributes and
24696 first_attribute = attributes;
24697 /* Combine the attributes. */
24698 attributes = chainon (prefix_attributes, attributes);
24700 decl = grokfield (declarator, &declspecs,
24701 NULL_TREE, /*init_const_expr_p=*/false,
24702 NULL_TREE, attributes);
24704 if (decl == error_mark_node || decl == NULL_TREE)
24705 return error_mark_node;
24707 /* Reset PREFIX_ATTRIBUTES. */
24708 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24709 attributes = TREE_CHAIN (attributes);
24711 TREE_CHAIN (attributes) = NULL_TREE;
24713 DECL_CHAIN (decl) = decls;
24716 token = cp_lexer_peek_token (parser->lexer);
24717 if (token->type == CPP_COMMA)
24719 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24728 /* Parse an Objective-C @property declaration. The syntax is:
24730 objc-property-declaration:
24731 '@property' objc-property-attributes[opt] struct-declaration ;
24733 objc-property-attributes:
24734 '(' objc-property-attribute-list ')'
24736 objc-property-attribute-list:
24737 objc-property-attribute
24738 objc-property-attribute-list, objc-property-attribute
24740 objc-property-attribute
24741 'getter' = identifier
24742 'setter' = identifier
24751 @property NSString *name;
24752 @property (readonly) id object;
24753 @property (retain, nonatomic, getter=getTheName) id name;
24754 @property int a, b, c;
24756 PS: This function is identical to
24757 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24759 cp_parser_objc_at_property_declaration (cp_parser *parser)
24761 /* The following variables hold the attributes of the properties as
24762 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24763 seen. When we see an attribute, we set them to 'true' (if they
24764 are boolean properties) or to the identifier (if they have an
24765 argument, ie, for getter and setter). Note that here we only
24766 parse the list of attributes, check the syntax and accumulate the
24767 attributes that we find. objc_add_property_declaration() will
24768 then process the information. */
24769 bool property_assign = false;
24770 bool property_copy = false;
24771 tree property_getter_ident = NULL_TREE;
24772 bool property_nonatomic = false;
24773 bool property_readonly = false;
24774 bool property_readwrite = false;
24775 bool property_retain = false;
24776 tree property_setter_ident = NULL_TREE;
24778 /* 'properties' is the list of properties that we read. Usually a
24779 single one, but maybe more (eg, in "@property int a, b, c;" there
24784 loc = cp_lexer_peek_token (parser->lexer)->location;
24786 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24788 /* Parse the optional attribute list... */
24789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24792 cp_lexer_consume_token (parser->lexer);
24796 bool syntax_error = false;
24797 cp_token *token = cp_lexer_peek_token (parser->lexer);
24800 if (token->type != CPP_NAME)
24802 cp_parser_error (parser, "expected identifier");
24805 keyword = C_RID_CODE (token->u.value);
24806 cp_lexer_consume_token (parser->lexer);
24809 case RID_ASSIGN: property_assign = true; break;
24810 case RID_COPY: property_copy = true; break;
24811 case RID_NONATOMIC: property_nonatomic = true; break;
24812 case RID_READONLY: property_readonly = true; break;
24813 case RID_READWRITE: property_readwrite = true; break;
24814 case RID_RETAIN: property_retain = true; break;
24818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24820 if (keyword == RID_GETTER)
24821 cp_parser_error (parser,
24822 "missing %<=%> (after %<getter%> attribute)");
24824 cp_parser_error (parser,
24825 "missing %<=%> (after %<setter%> attribute)");
24826 syntax_error = true;
24829 cp_lexer_consume_token (parser->lexer); /* eat the = */
24830 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24832 cp_parser_error (parser, "expected identifier");
24833 syntax_error = true;
24836 if (keyword == RID_SETTER)
24838 if (property_setter_ident != NULL_TREE)
24840 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24841 cp_lexer_consume_token (parser->lexer);
24844 property_setter_ident = cp_parser_objc_selector (parser);
24845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24846 cp_parser_error (parser, "setter name must terminate with %<:%>");
24848 cp_lexer_consume_token (parser->lexer);
24852 if (property_getter_ident != NULL_TREE)
24854 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24855 cp_lexer_consume_token (parser->lexer);
24858 property_getter_ident = cp_parser_objc_selector (parser);
24862 cp_parser_error (parser, "unknown property attribute");
24863 syntax_error = true;
24870 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24871 cp_lexer_consume_token (parser->lexer);
24876 /* FIXME: "@property (setter, assign);" will generate a spurious
24877 "error: expected ‘)’ before ‘,’ token". This is because
24878 cp_parser_require, unlike the C counterpart, will produce an
24879 error even if we are in error recovery. */
24880 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24882 cp_parser_skip_to_closing_parenthesis (parser,
24883 /*recovering=*/true,
24884 /*or_comma=*/false,
24885 /*consume_paren=*/true);
24889 /* ... and the property declaration(s). */
24890 properties = cp_parser_objc_struct_declaration (parser);
24892 if (properties == error_mark_node)
24894 cp_parser_skip_to_end_of_statement (parser);
24895 /* If the next token is now a `;', consume it. */
24896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24897 cp_lexer_consume_token (parser->lexer);
24901 if (properties == NULL_TREE)
24902 cp_parser_error (parser, "expected identifier");
24905 /* Comma-separated properties are chained together in
24906 reverse order; add them one by one. */
24907 properties = nreverse (properties);
24909 for (; properties; properties = TREE_CHAIN (properties))
24910 objc_add_property_declaration (loc, copy_node (properties),
24911 property_readonly, property_readwrite,
24912 property_assign, property_retain,
24913 property_copy, property_nonatomic,
24914 property_getter_ident, property_setter_ident);
24917 cp_parser_consume_semicolon_at_end_of_statement (parser);
24920 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24922 objc-synthesize-declaration:
24923 @synthesize objc-synthesize-identifier-list ;
24925 objc-synthesize-identifier-list:
24926 objc-synthesize-identifier
24927 objc-synthesize-identifier-list, objc-synthesize-identifier
24929 objc-synthesize-identifier
24931 identifier = identifier
24934 @synthesize MyProperty;
24935 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24937 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24938 for C. Keep them in sync.
24941 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24943 tree list = NULL_TREE;
24945 loc = cp_lexer_peek_token (parser->lexer)->location;
24947 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24950 tree property, ivar;
24951 property = cp_parser_identifier (parser);
24952 if (property == error_mark_node)
24954 cp_parser_consume_semicolon_at_end_of_statement (parser);
24957 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24959 cp_lexer_consume_token (parser->lexer);
24960 ivar = cp_parser_identifier (parser);
24961 if (ivar == error_mark_node)
24963 cp_parser_consume_semicolon_at_end_of_statement (parser);
24969 list = chainon (list, build_tree_list (ivar, property));
24970 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24971 cp_lexer_consume_token (parser->lexer);
24975 cp_parser_consume_semicolon_at_end_of_statement (parser);
24976 objc_add_synthesize_declaration (loc, list);
24979 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24981 objc-dynamic-declaration:
24982 @dynamic identifier-list ;
24985 @dynamic MyProperty;
24986 @dynamic MyProperty, AnotherProperty;
24988 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24989 for C. Keep them in sync.
24992 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24994 tree list = NULL_TREE;
24996 loc = cp_lexer_peek_token (parser->lexer)->location;
24998 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25002 property = cp_parser_identifier (parser);
25003 if (property == error_mark_node)
25005 cp_parser_consume_semicolon_at_end_of_statement (parser);
25008 list = chainon (list, build_tree_list (NULL, property));
25009 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25010 cp_lexer_consume_token (parser->lexer);
25014 cp_parser_consume_semicolon_at_end_of_statement (parser);
25015 objc_add_dynamic_declaration (loc, list);
25019 /* OpenMP 2.5 parsing routines. */
25021 /* Returns name of the next clause.
25022 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25023 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25024 returned and the token is consumed. */
25026 static pragma_omp_clause
25027 cp_parser_omp_clause_name (cp_parser *parser)
25029 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25032 result = PRAGMA_OMP_CLAUSE_IF;
25033 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25034 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25035 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25036 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25037 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25039 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25040 const char *p = IDENTIFIER_POINTER (id);
25045 if (!strcmp ("collapse", p))
25046 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25047 else if (!strcmp ("copyin", p))
25048 result = PRAGMA_OMP_CLAUSE_COPYIN;
25049 else if (!strcmp ("copyprivate", p))
25050 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25053 if (!strcmp ("final", p))
25054 result = PRAGMA_OMP_CLAUSE_FINAL;
25055 else if (!strcmp ("firstprivate", p))
25056 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25059 if (!strcmp ("lastprivate", p))
25060 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25063 if (!strcmp ("mergeable", p))
25064 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25067 if (!strcmp ("nowait", p))
25068 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25069 else if (!strcmp ("num_threads", p))
25070 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25073 if (!strcmp ("ordered", p))
25074 result = PRAGMA_OMP_CLAUSE_ORDERED;
25077 if (!strcmp ("reduction", p))
25078 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25081 if (!strcmp ("schedule", p))
25082 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25083 else if (!strcmp ("shared", p))
25084 result = PRAGMA_OMP_CLAUSE_SHARED;
25087 if (!strcmp ("untied", p))
25088 result = PRAGMA_OMP_CLAUSE_UNTIED;
25093 if (result != PRAGMA_OMP_CLAUSE_NONE)
25094 cp_lexer_consume_token (parser->lexer);
25099 /* Validate that a clause of the given type does not already exist. */
25102 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25103 const char *name, location_t location)
25107 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25108 if (OMP_CLAUSE_CODE (c) == code)
25110 error_at (location, "too many %qs clauses", name);
25118 variable-list , identifier
25120 In addition, we match a closing parenthesis. An opening parenthesis
25121 will have been consumed by the caller.
25123 If KIND is nonzero, create the appropriate node and install the decl
25124 in OMP_CLAUSE_DECL and add the node to the head of the list.
25126 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25127 return the list created. */
25130 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25138 token = cp_lexer_peek_token (parser->lexer);
25139 name = cp_parser_id_expression (parser, /*template_p=*/false,
25140 /*check_dependency_p=*/true,
25141 /*template_p=*/NULL,
25142 /*declarator_p=*/false,
25143 /*optional_p=*/false);
25144 if (name == error_mark_node)
25147 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25148 if (decl == error_mark_node)
25149 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25151 else if (kind != 0)
25153 tree u = build_omp_clause (token->location, kind);
25154 OMP_CLAUSE_DECL (u) = decl;
25155 OMP_CLAUSE_CHAIN (u) = list;
25159 list = tree_cons (decl, NULL_TREE, list);
25162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25164 cp_lexer_consume_token (parser->lexer);
25167 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25171 /* Try to resync to an unnested comma. Copied from
25172 cp_parser_parenthesized_expression_list. */
25174 ending = cp_parser_skip_to_closing_parenthesis (parser,
25175 /*recovering=*/true,
25177 /*consume_paren=*/true);
25185 /* Similarly, but expect leading and trailing parenthesis. This is a very
25186 common case for omp clauses. */
25189 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25191 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25192 return cp_parser_omp_var_list_no_open (parser, kind, list);
25197 collapse ( constant-expression ) */
25200 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25206 loc = cp_lexer_peek_token (parser->lexer)->location;
25207 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25210 num = cp_parser_constant_expression (parser, false, NULL);
25212 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25213 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25214 /*or_comma=*/false,
25215 /*consume_paren=*/true);
25217 if (num == error_mark_node)
25219 num = fold_non_dependent_expr (num);
25220 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25221 || !host_integerp (num, 0)
25222 || (n = tree_low_cst (num, 0)) <= 0
25225 error_at (loc, "collapse argument needs positive constant integer expression");
25229 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25230 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25231 OMP_CLAUSE_CHAIN (c) = list;
25232 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25238 default ( shared | none ) */
25241 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25243 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25246 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25248 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25250 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25251 const char *p = IDENTIFIER_POINTER (id);
25256 if (strcmp ("none", p) != 0)
25258 kind = OMP_CLAUSE_DEFAULT_NONE;
25262 if (strcmp ("shared", p) != 0)
25264 kind = OMP_CLAUSE_DEFAULT_SHARED;
25271 cp_lexer_consume_token (parser->lexer);
25276 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25279 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25280 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25281 /*or_comma=*/false,
25282 /*consume_paren=*/true);
25284 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25287 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25288 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25289 OMP_CLAUSE_CHAIN (c) = list;
25290 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25296 final ( expression ) */
25299 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25306 t = cp_parser_condition (parser);
25308 if (t == error_mark_node
25309 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25310 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25311 /*or_comma=*/false,
25312 /*consume_paren=*/true);
25314 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25316 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25317 OMP_CLAUSE_FINAL_EXPR (c) = t;
25318 OMP_CLAUSE_CHAIN (c) = list;
25324 if ( expression ) */
25327 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25331 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25334 t = cp_parser_condition (parser);
25336 if (t == error_mark_node
25337 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25338 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25339 /*or_comma=*/false,
25340 /*consume_paren=*/true);
25342 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25344 c = build_omp_clause (location, OMP_CLAUSE_IF);
25345 OMP_CLAUSE_IF_EXPR (c) = t;
25346 OMP_CLAUSE_CHAIN (c) = list;
25355 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25356 tree list, location_t location)
25360 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25363 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25364 OMP_CLAUSE_CHAIN (c) = list;
25372 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25373 tree list, location_t location)
25377 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25379 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25380 OMP_CLAUSE_CHAIN (c) = list;
25385 num_threads ( expression ) */
25388 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25389 location_t location)
25393 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25396 t = cp_parser_expression (parser, false, NULL);
25398 if (t == error_mark_node
25399 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25400 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25401 /*or_comma=*/false,
25402 /*consume_paren=*/true);
25404 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25405 "num_threads", location);
25407 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25408 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25409 OMP_CLAUSE_CHAIN (c) = list;
25418 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25419 tree list, location_t location)
25423 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25424 "ordered", location);
25426 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25427 OMP_CLAUSE_CHAIN (c) = list;
25432 reduction ( reduction-operator : variable-list )
25434 reduction-operator:
25435 One of: + * - & ^ | && ||
25439 reduction-operator:
25440 One of: + * - & ^ | && || min max */
25443 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25445 enum tree_code code;
25448 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25451 switch (cp_lexer_peek_token (parser->lexer)->type)
25463 code = BIT_AND_EXPR;
25466 code = BIT_XOR_EXPR;
25469 code = BIT_IOR_EXPR;
25472 code = TRUTH_ANDIF_EXPR;
25475 code = TRUTH_ORIF_EXPR;
25479 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25480 const char *p = IDENTIFIER_POINTER (id);
25482 if (strcmp (p, "min") == 0)
25487 if (strcmp (p, "max") == 0)
25495 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25496 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25498 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25499 /*or_comma=*/false,
25500 /*consume_paren=*/true);
25503 cp_lexer_consume_token (parser->lexer);
25505 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25508 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25509 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25510 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25516 schedule ( schedule-kind )
25517 schedule ( schedule-kind , expression )
25520 static | dynamic | guided | runtime | auto */
25523 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25527 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25530 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25535 const char *p = IDENTIFIER_POINTER (id);
25540 if (strcmp ("dynamic", p) != 0)
25542 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25546 if (strcmp ("guided", p) != 0)
25548 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25552 if (strcmp ("runtime", p) != 0)
25554 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25561 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25562 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25563 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25564 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25567 cp_lexer_consume_token (parser->lexer);
25569 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25572 cp_lexer_consume_token (parser->lexer);
25574 token = cp_lexer_peek_token (parser->lexer);
25575 t = cp_parser_assignment_expression (parser, false, NULL);
25577 if (t == error_mark_node)
25579 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25580 error_at (token->location, "schedule %<runtime%> does not take "
25581 "a %<chunk_size%> parameter");
25582 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25583 error_at (token->location, "schedule %<auto%> does not take "
25584 "a %<chunk_size%> parameter");
25586 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25588 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25591 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25594 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25595 OMP_CLAUSE_CHAIN (c) = list;
25599 cp_parser_error (parser, "invalid schedule kind");
25601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25602 /*or_comma=*/false,
25603 /*consume_paren=*/true);
25611 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25612 tree list, location_t location)
25616 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25618 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25619 OMP_CLAUSE_CHAIN (c) = list;
25623 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25624 is a bitmask in MASK. Return the list of clauses found; the result
25625 of clause default goes in *pdefault. */
25628 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25629 const char *where, cp_token *pragma_tok)
25631 tree clauses = NULL;
25633 cp_token *token = NULL;
25635 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25637 pragma_omp_clause c_kind;
25638 const char *c_name;
25639 tree prev = clauses;
25641 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25642 cp_lexer_consume_token (parser->lexer);
25644 token = cp_lexer_peek_token (parser->lexer);
25645 c_kind = cp_parser_omp_clause_name (parser);
25650 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25651 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25653 c_name = "collapse";
25655 case PRAGMA_OMP_CLAUSE_COPYIN:
25656 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25659 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25660 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25662 c_name = "copyprivate";
25664 case PRAGMA_OMP_CLAUSE_DEFAULT:
25665 clauses = cp_parser_omp_clause_default (parser, clauses,
25667 c_name = "default";
25669 case PRAGMA_OMP_CLAUSE_FINAL:
25670 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25673 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25674 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25676 c_name = "firstprivate";
25678 case PRAGMA_OMP_CLAUSE_IF:
25679 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25682 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25683 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25685 c_name = "lastprivate";
25687 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25688 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25690 c_name = "mergeable";
25692 case PRAGMA_OMP_CLAUSE_NOWAIT:
25693 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25696 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25697 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25699 c_name = "num_threads";
25701 case PRAGMA_OMP_CLAUSE_ORDERED:
25702 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25704 c_name = "ordered";
25706 case PRAGMA_OMP_CLAUSE_PRIVATE:
25707 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25709 c_name = "private";
25711 case PRAGMA_OMP_CLAUSE_REDUCTION:
25712 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25713 c_name = "reduction";
25715 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25716 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25718 c_name = "schedule";
25720 case PRAGMA_OMP_CLAUSE_SHARED:
25721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25725 case PRAGMA_OMP_CLAUSE_UNTIED:
25726 clauses = cp_parser_omp_clause_untied (parser, clauses,
25731 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25735 if (((mask >> c_kind) & 1) == 0)
25737 /* Remove the invalid clause(s) from the list to avoid
25738 confusing the rest of the compiler. */
25740 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25744 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25745 return finish_omp_clauses (clauses);
25752 In practice, we're also interested in adding the statement to an
25753 outer node. So it is convenient if we work around the fact that
25754 cp_parser_statement calls add_stmt. */
25757 cp_parser_begin_omp_structured_block (cp_parser *parser)
25759 unsigned save = parser->in_statement;
25761 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25762 This preserves the "not within loop or switch" style error messages
25763 for nonsense cases like
25769 if (parser->in_statement)
25770 parser->in_statement = IN_OMP_BLOCK;
25776 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25778 parser->in_statement = save;
25782 cp_parser_omp_structured_block (cp_parser *parser)
25784 tree stmt = begin_omp_structured_block ();
25785 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25787 cp_parser_statement (parser, NULL_TREE, false, NULL);
25789 cp_parser_end_omp_structured_block (parser, save);
25790 return finish_omp_structured_block (stmt);
25794 # pragma omp atomic new-line
25798 x binop= expr | x++ | ++x | x-- | --x
25800 +, *, -, /, &, ^, |, <<, >>
25802 where x is an lvalue expression with scalar type.
25805 # pragma omp atomic new-line
25808 # pragma omp atomic read new-line
25811 # pragma omp atomic write new-line
25814 # pragma omp atomic update new-line
25817 # pragma omp atomic capture new-line
25820 # pragma omp atomic capture new-line
25828 expression-stmt | x = x binop expr
25830 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25832 { v = x; update-stmt; } | { update-stmt; v = x; }
25834 where x and v are lvalue expressions with scalar type. */
25837 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25839 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25840 tree rhs1 = NULL_TREE, orig_lhs;
25841 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25842 bool structured_block = false;
25844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25846 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25847 const char *p = IDENTIFIER_POINTER (id);
25849 if (!strcmp (p, "read"))
25850 code = OMP_ATOMIC_READ;
25851 else if (!strcmp (p, "write"))
25853 else if (!strcmp (p, "update"))
25855 else if (!strcmp (p, "capture"))
25856 code = OMP_ATOMIC_CAPTURE_NEW;
25860 cp_lexer_consume_token (parser->lexer);
25862 cp_parser_require_pragma_eol (parser, pragma_tok);
25866 case OMP_ATOMIC_READ:
25867 case NOP_EXPR: /* atomic write */
25868 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25869 /*cast_p=*/false, NULL);
25870 if (v == error_mark_node)
25872 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25874 if (code == NOP_EXPR)
25875 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25877 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25878 /*cast_p=*/false, NULL);
25879 if (lhs == error_mark_node)
25881 if (code == NOP_EXPR)
25883 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25891 case OMP_ATOMIC_CAPTURE_NEW:
25892 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25894 cp_lexer_consume_token (parser->lexer);
25895 structured_block = true;
25899 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25900 /*cast_p=*/false, NULL);
25901 if (v == error_mark_node)
25903 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25911 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25912 /*cast_p=*/false, NULL);
25914 switch (TREE_CODE (lhs))
25919 case POSTINCREMENT_EXPR:
25920 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25921 code = OMP_ATOMIC_CAPTURE_OLD;
25923 case PREINCREMENT_EXPR:
25924 lhs = TREE_OPERAND (lhs, 0);
25925 opcode = PLUS_EXPR;
25926 rhs = integer_one_node;
25929 case POSTDECREMENT_EXPR:
25930 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25931 code = OMP_ATOMIC_CAPTURE_OLD;
25933 case PREDECREMENT_EXPR:
25934 lhs = TREE_OPERAND (lhs, 0);
25935 opcode = MINUS_EXPR;
25936 rhs = integer_one_node;
25939 case COMPOUND_EXPR:
25940 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25941 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25942 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25943 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25944 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25945 (TREE_OPERAND (lhs, 1), 0), 0)))
25947 /* Undo effects of boolean_increment for post {in,de}crement. */
25948 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25951 if (TREE_CODE (lhs) == MODIFY_EXPR
25952 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25954 /* Undo effects of boolean_increment. */
25955 if (integer_onep (TREE_OPERAND (lhs, 1)))
25957 /* This is pre or post increment. */
25958 rhs = TREE_OPERAND (lhs, 1);
25959 lhs = TREE_OPERAND (lhs, 0);
25961 if (code == OMP_ATOMIC_CAPTURE_NEW
25962 && !structured_block
25963 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25964 code = OMP_ATOMIC_CAPTURE_OLD;
25970 switch (cp_lexer_peek_token (parser->lexer)->type)
25973 opcode = MULT_EXPR;
25976 opcode = TRUNC_DIV_EXPR;
25979 opcode = PLUS_EXPR;
25982 opcode = MINUS_EXPR;
25984 case CPP_LSHIFT_EQ:
25985 opcode = LSHIFT_EXPR;
25987 case CPP_RSHIFT_EQ:
25988 opcode = RSHIFT_EXPR;
25991 opcode = BIT_AND_EXPR;
25994 opcode = BIT_IOR_EXPR;
25997 opcode = BIT_XOR_EXPR;
26000 if (structured_block || code == OMP_ATOMIC)
26002 enum cp_parser_prec oprec;
26004 cp_lexer_consume_token (parser->lexer);
26005 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26006 /*cast_p=*/false, NULL);
26007 if (rhs1 == error_mark_node)
26009 token = cp_lexer_peek_token (parser->lexer);
26010 switch (token->type)
26012 case CPP_SEMICOLON:
26013 if (code == OMP_ATOMIC_CAPTURE_NEW)
26015 code = OMP_ATOMIC_CAPTURE_OLD;
26020 cp_lexer_consume_token (parser->lexer);
26023 cp_parser_error (parser,
26024 "invalid form of %<#pragma omp atomic%>");
26027 opcode = MULT_EXPR;
26030 opcode = TRUNC_DIV_EXPR;
26033 opcode = PLUS_EXPR;
26036 opcode = MINUS_EXPR;
26039 opcode = LSHIFT_EXPR;
26042 opcode = RSHIFT_EXPR;
26045 opcode = BIT_AND_EXPR;
26048 opcode = BIT_IOR_EXPR;
26051 opcode = BIT_XOR_EXPR;
26054 cp_parser_error (parser,
26055 "invalid operator for %<#pragma omp atomic%>");
26058 oprec = TOKEN_PRECEDENCE (token);
26059 gcc_assert (oprec != PREC_NOT_OPERATOR);
26060 if (commutative_tree_code (opcode))
26061 oprec = (enum cp_parser_prec) (oprec - 1);
26062 cp_lexer_consume_token (parser->lexer);
26063 rhs = cp_parser_binary_expression (parser, false, false,
26065 if (rhs == error_mark_node)
26071 cp_parser_error (parser,
26072 "invalid operator for %<#pragma omp atomic%>");
26075 cp_lexer_consume_token (parser->lexer);
26077 rhs = cp_parser_expression (parser, false, NULL);
26078 if (rhs == error_mark_node)
26083 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26085 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26087 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26088 /*cast_p=*/false, NULL);
26089 if (v == error_mark_node)
26091 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26093 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26094 /*cast_p=*/false, NULL);
26095 if (lhs1 == error_mark_node)
26098 if (structured_block)
26100 cp_parser_consume_semicolon_at_end_of_statement (parser);
26101 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26104 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26105 if (!structured_block)
26106 cp_parser_consume_semicolon_at_end_of_statement (parser);
26110 cp_parser_skip_to_end_of_block_or_statement (parser);
26111 if (structured_block)
26113 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26114 cp_lexer_consume_token (parser->lexer);
26115 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26117 cp_parser_skip_to_end_of_block_or_statement (parser);
26118 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26119 cp_lexer_consume_token (parser->lexer);
26126 # pragma omp barrier new-line */
26129 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26131 cp_parser_require_pragma_eol (parser, pragma_tok);
26132 finish_omp_barrier ();
26136 # pragma omp critical [(name)] new-line
26137 structured-block */
26140 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26142 tree stmt, name = NULL;
26144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26146 cp_lexer_consume_token (parser->lexer);
26148 name = cp_parser_identifier (parser);
26150 if (name == error_mark_node
26151 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26152 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26153 /*or_comma=*/false,
26154 /*consume_paren=*/true);
26155 if (name == error_mark_node)
26158 cp_parser_require_pragma_eol (parser, pragma_tok);
26160 stmt = cp_parser_omp_structured_block (parser);
26161 return c_finish_omp_critical (input_location, stmt, name);
26165 # pragma omp flush flush-vars[opt] new-line
26168 ( variable-list ) */
26171 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26174 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26175 cp_parser_require_pragma_eol (parser, pragma_tok);
26177 finish_omp_flush ();
26180 /* Helper function, to parse omp for increment expression. */
26183 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26185 tree cond = cp_parser_binary_expression (parser, false, true,
26186 PREC_NOT_OPERATOR, NULL);
26187 if (cond == error_mark_node
26188 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26190 cp_parser_skip_to_end_of_statement (parser);
26191 return error_mark_node;
26194 switch (TREE_CODE (cond))
26202 return error_mark_node;
26205 /* If decl is an iterator, preserve LHS and RHS of the relational
26206 expr until finish_omp_for. */
26208 && (type_dependent_expression_p (decl)
26209 || CLASS_TYPE_P (TREE_TYPE (decl))))
26212 return build_x_binary_op (input_location, TREE_CODE (cond),
26213 TREE_OPERAND (cond, 0), ERROR_MARK,
26214 TREE_OPERAND (cond, 1), ERROR_MARK,
26215 /*overload=*/NULL, tf_warning_or_error);
26218 /* Helper function, to parse omp for increment expression. */
26221 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26223 cp_token *token = cp_lexer_peek_token (parser->lexer);
26229 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26231 op = (token->type == CPP_PLUS_PLUS
26232 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26233 cp_lexer_consume_token (parser->lexer);
26234 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26236 return error_mark_node;
26237 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26240 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26242 return error_mark_node;
26244 token = cp_lexer_peek_token (parser->lexer);
26245 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26247 op = (token->type == CPP_PLUS_PLUS
26248 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26249 cp_lexer_consume_token (parser->lexer);
26250 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26253 op = cp_parser_assignment_operator_opt (parser);
26254 if (op == ERROR_MARK)
26255 return error_mark_node;
26257 if (op != NOP_EXPR)
26259 rhs = cp_parser_assignment_expression (parser, false, NULL);
26260 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26261 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26264 lhs = cp_parser_binary_expression (parser, false, false,
26265 PREC_ADDITIVE_EXPRESSION, NULL);
26266 token = cp_lexer_peek_token (parser->lexer);
26267 decl_first = lhs == decl;
26270 if (token->type != CPP_PLUS
26271 && token->type != CPP_MINUS)
26272 return error_mark_node;
26276 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26277 cp_lexer_consume_token (parser->lexer);
26278 rhs = cp_parser_binary_expression (parser, false, false,
26279 PREC_ADDITIVE_EXPRESSION, NULL);
26280 token = cp_lexer_peek_token (parser->lexer);
26281 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26283 if (lhs == NULL_TREE)
26285 if (op == PLUS_EXPR)
26288 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26289 tf_warning_or_error);
26292 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26293 ERROR_MARK, NULL, tf_warning_or_error);
26296 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26300 if (rhs != decl || op == MINUS_EXPR)
26301 return error_mark_node;
26302 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26305 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26307 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26310 /* Parse the restricted form of the for statement allowed by OpenMP. */
26313 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26315 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26316 tree real_decl, initv, condv, incrv, declv;
26317 tree this_pre_body, cl;
26318 location_t loc_first;
26319 bool collapse_err = false;
26320 int i, collapse = 1, nbraces = 0;
26321 VEC(tree,gc) *for_block = make_tree_vector ();
26323 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26324 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26325 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26327 gcc_assert (collapse >= 1);
26329 declv = make_tree_vec (collapse);
26330 initv = make_tree_vec (collapse);
26331 condv = make_tree_vec (collapse);
26332 incrv = make_tree_vec (collapse);
26334 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26336 for (i = 0; i < collapse; i++)
26338 int bracecount = 0;
26339 bool add_private_clause = false;
26342 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26344 cp_parser_error (parser, "for statement expected");
26347 loc = cp_lexer_consume_token (parser->lexer)->location;
26349 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26352 init = decl = real_decl = NULL;
26353 this_pre_body = push_stmt_list ();
26354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26356 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26360 integer-type var = lb
26361 random-access-iterator-type var = lb
26362 pointer-type var = lb
26364 cp_decl_specifier_seq type_specifiers;
26366 /* First, try to parse as an initialized declaration. See
26367 cp_parser_condition, from whence the bulk of this is copied. */
26369 cp_parser_parse_tentatively (parser);
26370 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26371 /*is_trailing_return=*/false,
26373 if (cp_parser_parse_definitely (parser))
26375 /* If parsing a type specifier seq succeeded, then this
26376 MUST be a initialized declaration. */
26377 tree asm_specification, attributes;
26378 cp_declarator *declarator;
26380 declarator = cp_parser_declarator (parser,
26381 CP_PARSER_DECLARATOR_NAMED,
26382 /*ctor_dtor_or_conv_p=*/NULL,
26383 /*parenthesized_p=*/NULL,
26384 /*member_p=*/false);
26385 attributes = cp_parser_attributes_opt (parser);
26386 asm_specification = cp_parser_asm_specification_opt (parser);
26388 if (declarator == cp_error_declarator)
26389 cp_parser_skip_to_end_of_statement (parser);
26393 tree pushed_scope, auto_node;
26395 decl = start_decl (declarator, &type_specifiers,
26396 SD_INITIALIZED, attributes,
26397 /*prefix_attributes=*/NULL_TREE,
26400 auto_node = type_uses_auto (TREE_TYPE (decl));
26401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26403 if (cp_lexer_next_token_is (parser->lexer,
26405 error ("parenthesized initialization is not allowed in "
26406 "OpenMP %<for%> loop");
26408 /* Trigger an error. */
26409 cp_parser_require (parser, CPP_EQ, RT_EQ);
26411 init = error_mark_node;
26412 cp_parser_skip_to_end_of_statement (parser);
26414 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26415 || type_dependent_expression_p (decl)
26418 bool is_direct_init, is_non_constant_init;
26420 init = cp_parser_initializer (parser,
26422 &is_non_constant_init);
26427 = do_auto_deduction (TREE_TYPE (decl), init,
26430 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26431 && !type_dependent_expression_p (decl))
26435 cp_finish_decl (decl, init, !is_non_constant_init,
26437 LOOKUP_ONLYCONVERTING);
26438 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26440 VEC_safe_push (tree, gc, for_block, this_pre_body);
26444 init = pop_stmt_list (this_pre_body);
26445 this_pre_body = NULL_TREE;
26450 cp_lexer_consume_token (parser->lexer);
26451 init = cp_parser_assignment_expression (parser, false, NULL);
26454 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26455 init = error_mark_node;
26457 cp_finish_decl (decl, NULL_TREE,
26458 /*init_const_expr_p=*/false,
26460 LOOKUP_ONLYCONVERTING);
26464 pop_scope (pushed_scope);
26470 /* If parsing a type specifier sequence failed, then
26471 this MUST be a simple expression. */
26472 cp_parser_parse_tentatively (parser);
26473 decl = cp_parser_primary_expression (parser, false, false,
26475 if (!cp_parser_error_occurred (parser)
26478 && CLASS_TYPE_P (TREE_TYPE (decl)))
26482 cp_parser_parse_definitely (parser);
26483 cp_parser_require (parser, CPP_EQ, RT_EQ);
26484 rhs = cp_parser_assignment_expression (parser, false, NULL);
26485 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
26488 tf_warning_or_error));
26489 add_private_clause = true;
26494 cp_parser_abort_tentative_parse (parser);
26495 init = cp_parser_expression (parser, false, NULL);
26498 if (TREE_CODE (init) == MODIFY_EXPR
26499 || TREE_CODE (init) == MODOP_EXPR)
26500 real_decl = TREE_OPERAND (init, 0);
26505 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26508 this_pre_body = pop_stmt_list (this_pre_body);
26512 pre_body = push_stmt_list ();
26514 add_stmt (this_pre_body);
26515 pre_body = pop_stmt_list (pre_body);
26518 pre_body = this_pre_body;
26523 if (par_clauses != NULL && real_decl != NULL_TREE)
26526 for (c = par_clauses; *c ; )
26527 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26528 && OMP_CLAUSE_DECL (*c) == real_decl)
26530 error_at (loc, "iteration variable %qD"
26531 " should not be firstprivate", real_decl);
26532 *c = OMP_CLAUSE_CHAIN (*c);
26534 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26535 && OMP_CLAUSE_DECL (*c) == real_decl)
26537 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26538 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26539 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26540 OMP_CLAUSE_DECL (l) = real_decl;
26541 OMP_CLAUSE_CHAIN (l) = clauses;
26542 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26544 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26545 CP_OMP_CLAUSE_INFO (*c) = NULL;
26546 add_private_clause = false;
26550 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26551 && OMP_CLAUSE_DECL (*c) == real_decl)
26552 add_private_clause = false;
26553 c = &OMP_CLAUSE_CHAIN (*c);
26557 if (add_private_clause)
26560 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26562 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26563 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26564 && OMP_CLAUSE_DECL (c) == decl)
26566 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26567 && OMP_CLAUSE_DECL (c) == decl)
26568 error_at (loc, "iteration variable %qD "
26569 "should not be firstprivate",
26571 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26572 && OMP_CLAUSE_DECL (c) == decl)
26573 error_at (loc, "iteration variable %qD should not be reduction",
26578 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26579 OMP_CLAUSE_DECL (c) = decl;
26580 c = finish_omp_clauses (c);
26583 OMP_CLAUSE_CHAIN (c) = clauses;
26590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26591 cond = cp_parser_omp_for_cond (parser, decl);
26592 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26597 /* If decl is an iterator, preserve the operator on decl
26598 until finish_omp_for. */
26600 && ((processing_template_decl
26601 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26602 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26603 incr = cp_parser_omp_for_incr (parser, real_decl);
26605 incr = cp_parser_expression (parser, false, NULL);
26608 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26609 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26610 /*or_comma=*/false,
26611 /*consume_paren=*/true);
26613 TREE_VEC_ELT (declv, i) = decl;
26614 TREE_VEC_ELT (initv, i) = init;
26615 TREE_VEC_ELT (condv, i) = cond;
26616 TREE_VEC_ELT (incrv, i) = incr;
26618 if (i == collapse - 1)
26621 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26622 in between the collapsed for loops to be still considered perfectly
26623 nested. Hopefully the final version clarifies this.
26624 For now handle (multiple) {'s and empty statements. */
26625 cp_parser_parse_tentatively (parser);
26628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26630 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26632 cp_lexer_consume_token (parser->lexer);
26635 else if (bracecount
26636 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26637 cp_lexer_consume_token (parser->lexer);
26640 loc = cp_lexer_peek_token (parser->lexer)->location;
26641 error_at (loc, "not enough collapsed for loops");
26642 collapse_err = true;
26643 cp_parser_abort_tentative_parse (parser);
26652 cp_parser_parse_definitely (parser);
26653 nbraces += bracecount;
26657 /* Note that we saved the original contents of this flag when we entered
26658 the structured block, and so we don't need to re-save it here. */
26659 parser->in_statement = IN_OMP_FOR;
26661 /* Note that the grammar doesn't call for a structured block here,
26662 though the loop as a whole is a structured block. */
26663 body = push_stmt_list ();
26664 cp_parser_statement (parser, NULL_TREE, false, NULL);
26665 body = pop_stmt_list (body);
26667 if (declv == NULL_TREE)
26670 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26671 pre_body, clauses);
26675 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26677 cp_lexer_consume_token (parser->lexer);
26680 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26681 cp_lexer_consume_token (parser->lexer);
26686 error_at (cp_lexer_peek_token (parser->lexer)->location,
26687 "collapsed loops not perfectly nested");
26689 collapse_err = true;
26690 cp_parser_statement_seq_opt (parser, NULL);
26691 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26696 while (!VEC_empty (tree, for_block))
26697 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26698 release_tree_vector (for_block);
26704 #pragma omp for for-clause[optseq] new-line
26707 #define OMP_FOR_CLAUSE_MASK \
26708 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26709 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26710 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26711 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26712 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26713 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26714 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26715 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26718 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26720 tree clauses, sb, ret;
26723 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26724 "#pragma omp for", pragma_tok);
26726 sb = begin_omp_structured_block ();
26727 save = cp_parser_begin_omp_structured_block (parser);
26729 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26731 cp_parser_end_omp_structured_block (parser, save);
26732 add_stmt (finish_omp_structured_block (sb));
26738 # pragma omp master new-line
26739 structured-block */
26742 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26744 cp_parser_require_pragma_eol (parser, pragma_tok);
26745 return c_finish_omp_master (input_location,
26746 cp_parser_omp_structured_block (parser));
26750 # pragma omp ordered new-line
26751 structured-block */
26754 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26756 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26757 cp_parser_require_pragma_eol (parser, pragma_tok);
26758 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26764 { section-sequence }
26767 section-directive[opt] structured-block
26768 section-sequence section-directive structured-block */
26771 cp_parser_omp_sections_scope (cp_parser *parser)
26773 tree stmt, substmt;
26774 bool error_suppress = false;
26777 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26780 stmt = push_stmt_list ();
26782 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26786 substmt = begin_omp_structured_block ();
26787 save = cp_parser_begin_omp_structured_block (parser);
26791 cp_parser_statement (parser, NULL_TREE, false, NULL);
26793 tok = cp_lexer_peek_token (parser->lexer);
26794 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26796 if (tok->type == CPP_CLOSE_BRACE)
26798 if (tok->type == CPP_EOF)
26802 cp_parser_end_omp_structured_block (parser, save);
26803 substmt = finish_omp_structured_block (substmt);
26804 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26805 add_stmt (substmt);
26810 tok = cp_lexer_peek_token (parser->lexer);
26811 if (tok->type == CPP_CLOSE_BRACE)
26813 if (tok->type == CPP_EOF)
26816 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26818 cp_lexer_consume_token (parser->lexer);
26819 cp_parser_require_pragma_eol (parser, tok);
26820 error_suppress = false;
26822 else if (!error_suppress)
26824 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26825 error_suppress = true;
26828 substmt = cp_parser_omp_structured_block (parser);
26829 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26830 add_stmt (substmt);
26832 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26834 substmt = pop_stmt_list (stmt);
26836 stmt = make_node (OMP_SECTIONS);
26837 TREE_TYPE (stmt) = void_type_node;
26838 OMP_SECTIONS_BODY (stmt) = substmt;
26845 # pragma omp sections sections-clause[optseq] newline
26848 #define OMP_SECTIONS_CLAUSE_MASK \
26849 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26850 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26851 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26852 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26853 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26856 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26860 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26861 "#pragma omp sections", pragma_tok);
26863 ret = cp_parser_omp_sections_scope (parser);
26865 OMP_SECTIONS_CLAUSES (ret) = clauses;
26871 # pragma parallel parallel-clause new-line
26872 # pragma parallel for parallel-for-clause new-line
26873 # pragma parallel sections parallel-sections-clause new-line */
26875 #define OMP_PARALLEL_CLAUSE_MASK \
26876 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26877 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26878 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26879 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26880 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26881 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26882 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26883 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26886 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26888 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26889 const char *p_name = "#pragma omp parallel";
26890 tree stmt, clauses, par_clause, ws_clause, block;
26891 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26893 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26895 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26897 cp_lexer_consume_token (parser->lexer);
26898 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26899 p_name = "#pragma omp parallel for";
26900 mask |= OMP_FOR_CLAUSE_MASK;
26901 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26903 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26905 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26906 const char *p = IDENTIFIER_POINTER (id);
26907 if (strcmp (p, "sections") == 0)
26909 cp_lexer_consume_token (parser->lexer);
26910 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26911 p_name = "#pragma omp parallel sections";
26912 mask |= OMP_SECTIONS_CLAUSE_MASK;
26913 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26917 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26918 block = begin_omp_parallel ();
26919 save = cp_parser_begin_omp_structured_block (parser);
26923 case PRAGMA_OMP_PARALLEL:
26924 cp_parser_statement (parser, NULL_TREE, false, NULL);
26925 par_clause = clauses;
26928 case PRAGMA_OMP_PARALLEL_FOR:
26929 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26930 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26933 case PRAGMA_OMP_PARALLEL_SECTIONS:
26934 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26935 stmt = cp_parser_omp_sections_scope (parser);
26937 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26941 gcc_unreachable ();
26944 cp_parser_end_omp_structured_block (parser, save);
26945 stmt = finish_omp_parallel (par_clause, block);
26946 if (p_kind != PRAGMA_OMP_PARALLEL)
26947 OMP_PARALLEL_COMBINED (stmt) = 1;
26952 # pragma omp single single-clause[optseq] new-line
26953 structured-block */
26955 #define OMP_SINGLE_CLAUSE_MASK \
26956 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26957 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26958 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26959 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26962 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26964 tree stmt = make_node (OMP_SINGLE);
26965 TREE_TYPE (stmt) = void_type_node;
26967 OMP_SINGLE_CLAUSES (stmt)
26968 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26969 "#pragma omp single", pragma_tok);
26970 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26972 return add_stmt (stmt);
26976 # pragma omp task task-clause[optseq] new-line
26977 structured-block */
26979 #define OMP_TASK_CLAUSE_MASK \
26980 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26981 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26982 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26983 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26984 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26985 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26986 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26987 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26990 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26992 tree clauses, block;
26995 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26996 "#pragma omp task", pragma_tok);
26997 block = begin_omp_task ();
26998 save = cp_parser_begin_omp_structured_block (parser);
26999 cp_parser_statement (parser, NULL_TREE, false, NULL);
27000 cp_parser_end_omp_structured_block (parser, save);
27001 return finish_omp_task (clauses, block);
27005 # pragma omp taskwait new-line */
27008 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27010 cp_parser_require_pragma_eol (parser, pragma_tok);
27011 finish_omp_taskwait ();
27015 # pragma omp taskyield new-line */
27018 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27020 cp_parser_require_pragma_eol (parser, pragma_tok);
27021 finish_omp_taskyield ();
27025 # pragma omp threadprivate (variable-list) */
27028 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27032 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27033 cp_parser_require_pragma_eol (parser, pragma_tok);
27035 finish_omp_threadprivate (vars);
27038 /* Main entry point to OpenMP statement pragmas. */
27041 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27045 switch (pragma_tok->pragma_kind)
27047 case PRAGMA_OMP_ATOMIC:
27048 cp_parser_omp_atomic (parser, pragma_tok);
27050 case PRAGMA_OMP_CRITICAL:
27051 stmt = cp_parser_omp_critical (parser, pragma_tok);
27053 case PRAGMA_OMP_FOR:
27054 stmt = cp_parser_omp_for (parser, pragma_tok);
27056 case PRAGMA_OMP_MASTER:
27057 stmt = cp_parser_omp_master (parser, pragma_tok);
27059 case PRAGMA_OMP_ORDERED:
27060 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27062 case PRAGMA_OMP_PARALLEL:
27063 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27065 case PRAGMA_OMP_SECTIONS:
27066 stmt = cp_parser_omp_sections (parser, pragma_tok);
27068 case PRAGMA_OMP_SINGLE:
27069 stmt = cp_parser_omp_single (parser, pragma_tok);
27071 case PRAGMA_OMP_TASK:
27072 stmt = cp_parser_omp_task (parser, pragma_tok);
27075 gcc_unreachable ();
27079 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27082 /* Transactional Memory parsing routines. */
27084 /* Parse a transaction attribute.
27090 ??? Simplify this when C++0x bracket attributes are
27091 implemented properly. */
27094 cp_parser_txn_attribute_opt (cp_parser *parser)
27097 tree attr_name, attr = NULL;
27099 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27100 return cp_parser_attributes_opt (parser);
27102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27104 cp_lexer_consume_token (parser->lexer);
27105 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27108 token = cp_lexer_peek_token (parser->lexer);
27109 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27111 token = cp_lexer_consume_token (parser->lexer);
27113 attr_name = (token->type == CPP_KEYWORD
27114 /* For keywords, use the canonical spelling,
27115 not the parsed identifier. */
27116 ? ridpointers[(int) token->keyword]
27118 attr = build_tree_list (attr_name, NULL_TREE);
27121 cp_parser_error (parser, "expected identifier");
27123 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27125 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27129 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27131 transaction-statement:
27132 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27134 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27138 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27140 unsigned char old_in = parser->in_transaction;
27141 unsigned char this_in = 1, new_in;
27143 tree stmt, attrs, noex;
27145 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27146 || keyword == RID_TRANSACTION_RELAXED);
27147 token = cp_parser_require_keyword (parser, keyword,
27148 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27149 : RT_TRANSACTION_RELAXED));
27150 gcc_assert (token != NULL);
27152 if (keyword == RID_TRANSACTION_RELAXED)
27153 this_in |= TM_STMT_ATTR_RELAXED;
27156 attrs = cp_parser_txn_attribute_opt (parser);
27158 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27161 /* Parse a noexcept specification. */
27162 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27164 /* Keep track if we're in the lexical scope of an outer transaction. */
27165 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27167 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27169 parser->in_transaction = new_in;
27170 cp_parser_compound_statement (parser, NULL, false, false);
27171 parser->in_transaction = old_in;
27173 finish_transaction_stmt (stmt, NULL, this_in, noex);
27178 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27180 transaction-expression:
27181 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27182 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27186 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27188 unsigned char old_in = parser->in_transaction;
27189 unsigned char this_in = 1;
27194 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27195 || keyword == RID_TRANSACTION_RELAXED);
27198 error (keyword == RID_TRANSACTION_RELAXED
27199 ? G_("%<__transaction_relaxed%> without transactional memory "
27201 : G_("%<__transaction_atomic%> without transactional memory "
27202 "support enabled"));
27204 token = cp_parser_require_keyword (parser, keyword,
27205 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27206 : RT_TRANSACTION_RELAXED));
27207 gcc_assert (token != NULL);
27209 if (keyword == RID_TRANSACTION_RELAXED)
27210 this_in |= TM_STMT_ATTR_RELAXED;
27212 /* Set this early. This might mean that we allow transaction_cancel in
27213 an expression that we find out later actually has to be a constexpr.
27214 However, we expect that cxx_constant_value will be able to deal with
27215 this; also, if the noexcept has no constexpr, then what we parse next
27216 really is a transaction's body. */
27217 parser->in_transaction = this_in;
27219 /* Parse a noexcept specification. */
27220 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27223 if (!noex || !noex_expr
27224 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27226 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27228 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27229 finish_parenthesized_expr (expr);
27231 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27235 /* The only expression that is available got parsed for the noexcept
27236 already. noexcept is true then. */
27238 noex = boolean_true_node;
27241 expr = build_transaction_expr (token->location, expr, this_in, noex);
27242 parser->in_transaction = old_in;
27244 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27245 return error_mark_node;
27247 return (flag_tm ? expr : error_mark_node);
27250 /* Parse a function-transaction-block.
27252 function-transaction-block:
27253 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27255 __transaction_atomic txn-attribute[opt] function-try-block
27256 __transaction_relaxed ctor-initializer[opt] function-body
27257 __transaction_relaxed function-try-block
27261 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27263 unsigned char old_in = parser->in_transaction;
27264 unsigned char new_in = 1;
27265 tree compound_stmt, stmt, attrs;
27266 bool ctor_initializer_p;
27269 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27270 || keyword == RID_TRANSACTION_RELAXED);
27271 token = cp_parser_require_keyword (parser, keyword,
27272 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27273 : RT_TRANSACTION_RELAXED));
27274 gcc_assert (token != NULL);
27276 if (keyword == RID_TRANSACTION_RELAXED)
27277 new_in |= TM_STMT_ATTR_RELAXED;
27280 attrs = cp_parser_txn_attribute_opt (parser);
27282 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27285 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27287 parser->in_transaction = new_in;
27289 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27290 ctor_initializer_p = cp_parser_function_try_block (parser);
27292 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27293 (parser, /*in_function_try_block=*/false);
27295 parser->in_transaction = old_in;
27297 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27299 return ctor_initializer_p;
27302 /* Parse a __transaction_cancel statement.
27305 __transaction_cancel txn-attribute[opt] ;
27306 __transaction_cancel txn-attribute[opt] throw-expression ;
27308 ??? Cancel and throw is not yet implemented. */
27311 cp_parser_transaction_cancel (cp_parser *parser)
27314 bool is_outer = false;
27317 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27318 RT_TRANSACTION_CANCEL);
27319 gcc_assert (token != NULL);
27321 attrs = cp_parser_txn_attribute_opt (parser);
27323 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27325 /* ??? Parse cancel-and-throw here. */
27327 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27331 error_at (token->location, "%<__transaction_cancel%> without "
27332 "transactional memory support enabled");
27333 return error_mark_node;
27335 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27337 error_at (token->location, "%<__transaction_cancel%> within a "
27338 "%<__transaction_relaxed%>");
27339 return error_mark_node;
27343 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27344 && !is_tm_may_cancel_outer (current_function_decl))
27346 error_at (token->location, "outer %<__transaction_cancel%> not "
27347 "within outer %<__transaction_atomic%>");
27348 error_at (token->location,
27349 " or a %<transaction_may_cancel_outer%> function");
27350 return error_mark_node;
27353 else if (parser->in_transaction == 0)
27355 error_at (token->location, "%<__transaction_cancel%> not within "
27356 "%<__transaction_atomic%>");
27357 return error_mark_node;
27360 stmt = build_tm_abort_call (token->location, is_outer);
27369 static GTY (()) cp_parser *the_parser;
27372 /* Special handling for the first token or line in the file. The first
27373 thing in the file might be #pragma GCC pch_preprocess, which loads a
27374 PCH file, which is a GC collection point. So we need to handle this
27375 first pragma without benefit of an existing lexer structure.
27377 Always returns one token to the caller in *FIRST_TOKEN. This is
27378 either the true first token of the file, or the first token after
27379 the initial pragma. */
27382 cp_parser_initial_pragma (cp_token *first_token)
27386 cp_lexer_get_preprocessor_token (NULL, first_token);
27387 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27390 cp_lexer_get_preprocessor_token (NULL, first_token);
27391 if (first_token->type == CPP_STRING)
27393 name = first_token->u.value;
27395 cp_lexer_get_preprocessor_token (NULL, first_token);
27396 if (first_token->type != CPP_PRAGMA_EOL)
27397 error_at (first_token->location,
27398 "junk at end of %<#pragma GCC pch_preprocess%>");
27401 error_at (first_token->location, "expected string literal");
27403 /* Skip to the end of the pragma. */
27404 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27405 cp_lexer_get_preprocessor_token (NULL, first_token);
27407 /* Now actually load the PCH file. */
27409 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27411 /* Read one more token to return to our caller. We have to do this
27412 after reading the PCH file in, since its pointers have to be
27414 cp_lexer_get_preprocessor_token (NULL, first_token);
27417 /* Normal parsing of a pragma token. Here we can (and must) use the
27421 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27423 cp_token *pragma_tok;
27426 pragma_tok = cp_lexer_consume_token (parser->lexer);
27427 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27428 parser->lexer->in_pragma = true;
27430 id = pragma_tok->pragma_kind;
27433 case PRAGMA_GCC_PCH_PREPROCESS:
27434 error_at (pragma_tok->location,
27435 "%<#pragma GCC pch_preprocess%> must be first");
27438 case PRAGMA_OMP_BARRIER:
27441 case pragma_compound:
27442 cp_parser_omp_barrier (parser, pragma_tok);
27445 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27446 "used in compound statements");
27453 case PRAGMA_OMP_FLUSH:
27456 case pragma_compound:
27457 cp_parser_omp_flush (parser, pragma_tok);
27460 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27461 "used in compound statements");
27468 case PRAGMA_OMP_TASKWAIT:
27471 case pragma_compound:
27472 cp_parser_omp_taskwait (parser, pragma_tok);
27475 error_at (pragma_tok->location,
27476 "%<#pragma omp taskwait%> may only be "
27477 "used in compound statements");
27484 case PRAGMA_OMP_TASKYIELD:
27487 case pragma_compound:
27488 cp_parser_omp_taskyield (parser, pragma_tok);
27491 error_at (pragma_tok->location,
27492 "%<#pragma omp taskyield%> may only be "
27493 "used in compound statements");
27500 case PRAGMA_OMP_THREADPRIVATE:
27501 cp_parser_omp_threadprivate (parser, pragma_tok);
27504 case PRAGMA_OMP_ATOMIC:
27505 case PRAGMA_OMP_CRITICAL:
27506 case PRAGMA_OMP_FOR:
27507 case PRAGMA_OMP_MASTER:
27508 case PRAGMA_OMP_ORDERED:
27509 case PRAGMA_OMP_PARALLEL:
27510 case PRAGMA_OMP_SECTIONS:
27511 case PRAGMA_OMP_SINGLE:
27512 case PRAGMA_OMP_TASK:
27513 if (context == pragma_external)
27515 cp_parser_omp_construct (parser, pragma_tok);
27518 case PRAGMA_OMP_SECTION:
27519 error_at (pragma_tok->location,
27520 "%<#pragma omp section%> may only be used in "
27521 "%<#pragma omp sections%> construct");
27525 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27526 c_invoke_pragma_handler (id);
27530 cp_parser_error (parser, "expected declaration specifiers");
27534 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27538 /* The interface the pragma parsers have to the lexer. */
27541 pragma_lex (tree *value)
27544 enum cpp_ttype ret;
27546 tok = cp_lexer_peek_token (the_parser->lexer);
27549 *value = tok->u.value;
27551 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27553 else if (ret == CPP_STRING)
27554 *value = cp_parser_string_literal (the_parser, false, false);
27557 cp_lexer_consume_token (the_parser->lexer);
27558 if (ret == CPP_KEYWORD)
27566 /* External interface. */
27568 /* Parse one entire translation unit. */
27571 c_parse_file (void)
27573 static bool already_called = false;
27575 if (already_called)
27577 sorry ("inter-module optimizations not implemented for C++");
27580 already_called = true;
27582 the_parser = cp_parser_new ();
27583 push_deferring_access_checks (flag_access_control
27584 ? dk_no_deferred : dk_no_check);
27585 cp_parser_translation_unit (the_parser);
27589 #include "gt-cp-parser.h"