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"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
41 #include "tree-pretty-print.h"
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 static cp_token eof_token =
52 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
58 /* floating-point literal */
62 /* %<__FUNCTION__%> */
64 /* %<__PRETTY_FUNCTION__%> */
72 /* %<typeid%> operator */
74 /* non-constant compound literals */
82 /* an array reference */
88 /* the address of a label */
102 /* calls to overloaded operators */
106 /* a comma operator */
108 /* a call to a constructor */
110 /* a transaction expression */
112 } non_integral_constant;
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error {
120 /* is not a class or namespace */
122 /* is not a class, namespace, or enumeration */
126 /* The various kinds of required token */
127 typedef enum required_token {
129 RT_SEMICOLON, /* ';' */
130 RT_OPEN_PAREN, /* '(' */
131 RT_CLOSE_BRACE, /* '}' */
132 RT_OPEN_BRACE, /* '{' */
133 RT_CLOSE_SQUARE, /* ']' */
134 RT_OPEN_SQUARE, /* '[' */
138 RT_GREATER, /* '>' */
140 RT_ELLIPSIS, /* '...' */
144 RT_COLON_SCOPE, /* ':' or '::' */
145 RT_CLOSE_PAREN, /* ')' */
146 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147 RT_PRAGMA_EOL, /* end of line */
148 RT_NAME, /* identifier */
150 /* The type is CPP_KEYWORD */
152 RT_DELETE, /* delete */
153 RT_RETURN, /* return */
154 RT_WHILE, /* while */
155 RT_EXTERN, /* extern */
156 RT_STATIC_ASSERT, /* static_assert */
157 RT_DECLTYPE, /* decltype */
158 RT_OPERATOR, /* operator */
159 RT_CLASS, /* class */
160 RT_TEMPLATE, /* template */
161 RT_NAMESPACE, /* namespace */
162 RT_USING, /* using */
165 RT_CATCH, /* catch */
166 RT_THROW, /* throw */
167 RT_LABEL, /* __label__ */
168 RT_AT_TRY, /* @try */
169 RT_AT_SYNCHRONIZED, /* @synchronized */
170 RT_AT_THROW, /* @throw */
172 RT_SELECT, /* selection-statement */
173 RT_INTERATION, /* iteration-statement */
174 RT_JUMP, /* jump-statement */
175 RT_CLASS_KEY, /* class-key */
176 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 static cp_lexer *cp_lexer_new_main
186 static cp_lexer *cp_lexer_new_from_tokens
187 (cp_token_cache *tokens);
188 static void cp_lexer_destroy
190 static int cp_lexer_saving_tokens
192 static cp_token *cp_lexer_token_at
193 (cp_lexer *, cp_token_position);
194 static void cp_lexer_get_preprocessor_token
195 (cp_lexer *, cp_token *);
196 static inline cp_token *cp_lexer_peek_token
198 static cp_token *cp_lexer_peek_nth_token
199 (cp_lexer *, size_t);
200 static inline bool cp_lexer_next_token_is
201 (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_not
203 (cp_lexer *, enum cpp_ttype);
204 static bool cp_lexer_next_token_is_keyword
205 (cp_lexer *, enum rid);
206 static cp_token *cp_lexer_consume_token
208 static void cp_lexer_purge_token
210 static void cp_lexer_purge_tokens_after
211 (cp_lexer *, cp_token_position);
212 static void cp_lexer_save_tokens
214 static void cp_lexer_commit_tokens
216 static void cp_lexer_rollback_tokens
218 static void cp_lexer_print_token
219 (FILE *, cp_token *);
220 static inline bool cp_lexer_debugging_p
222 static void cp_lexer_start_debugging
223 (cp_lexer *) ATTRIBUTE_UNUSED;
224 static void cp_lexer_stop_debugging
225 (cp_lexer *) ATTRIBUTE_UNUSED;
227 static cp_token_cache *cp_token_cache_new
228 (cp_token *, cp_token *);
230 static void cp_parser_initial_pragma
233 static tree cp_literal_operator_id
236 /* Manifest constants. */
237 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
238 #define CP_SAVED_TOKEN_STACK 5
242 /* The stream to which debugging output should be written. */
243 static FILE *cp_lexer_debug_stream;
245 /* Nonzero if we are parsing an unevaluated operand: an operand to
246 sizeof, typeof, or alignof. */
247 int cp_unevaluated_operand;
249 /* Dump up to NUM tokens in BUFFER to FILE starting with token
250 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
251 first token in BUFFER. If NUM is 0, dump all the tokens. If
252 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
253 highlighted by surrounding it in [[ ]]. */
256 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
257 cp_token *start_token, unsigned num,
258 cp_token *curr_token)
260 unsigned i, nprinted;
264 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
270 num = VEC_length (cp_token, buffer);
272 if (start_token == NULL)
273 start_token = VEC_address (cp_token, buffer);
275 if (start_token > VEC_address (cp_token, buffer))
277 cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
278 fprintf (file, " ... ");
283 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
285 if (token == start_token)
292 if (token == curr_token)
293 fprintf (file, "[[");
295 cp_lexer_print_token (file, token);
297 if (token == curr_token)
298 fprintf (file, "]]");
304 case CPP_CLOSE_BRACE:
314 if (i == num && i < VEC_length (cp_token, buffer))
316 fprintf (file, " ... ");
317 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
318 VEC_length (cp_token, buffer) - 1));
321 fprintf (file, "\n");
325 /* Dump all tokens in BUFFER to stderr. */
328 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
330 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
334 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
335 description for T. */
338 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
342 fprintf (file, "%s: ", desc);
343 print_node_brief (file, "", t, 0);
348 /* Dump parser context C to FILE. */
351 cp_debug_print_context (FILE *file, cp_parser_context *c)
353 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
354 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
355 print_node_brief (file, "", c->object_type, 0);
356 fprintf (file, "}\n");
360 /* Print the stack of parsing contexts to FILE starting with FIRST. */
363 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
366 cp_parser_context *c;
368 fprintf (file, "Parsing context stack:\n");
369 for (i = 0, c = first; c; c = c->next, i++)
371 fprintf (file, "\t#%u: ", i);
372 cp_debug_print_context (file, c);
377 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
380 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
383 fprintf (file, "%s: true\n", desc);
387 /* Print an unparsed function entry UF to FILE. */
390 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
393 cp_default_arg_entry *default_arg_fn;
396 fprintf (file, "\tFunctions with default args:\n");
398 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
402 fprintf (file, "\t\tClass type: ");
403 print_node_brief (file, "", default_arg_fn->class_type, 0);
404 fprintf (file, "\t\tDeclaration: ");
405 print_node_brief (file, "", default_arg_fn->decl, 0);
406 fprintf (file, "\n");
409 fprintf (file, "\n\tFunctions with definitions that require "
410 "post-processing\n\t\t");
411 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
413 print_node_brief (file, "", fn, 0);
416 fprintf (file, "\n");
418 fprintf (file, "\n\tNon-static data members with initializers that require "
419 "post-processing\n\t\t");
420 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
422 print_node_brief (file, "", fn, 0);
425 fprintf (file, "\n");
429 /* Print the stack of unparsed member functions S to FILE. */
432 cp_debug_print_unparsed_queues (FILE *file,
433 VEC(cp_unparsed_functions_entry, gc) *s)
436 cp_unparsed_functions_entry *uf;
438 fprintf (file, "Unparsed functions\n");
439 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
441 fprintf (file, "#%u:\n", i);
442 cp_debug_print_unparsed_function (file, uf);
447 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
448 the given PARSER. If FILE is NULL, the output is printed on stderr. */
451 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
453 cp_token *next_token, *first_token, *start_token;
458 next_token = parser->lexer->next_token;
459 first_token = VEC_address (cp_token, parser->lexer->buffer);
460 start_token = (next_token > first_token + window_size / 2)
461 ? next_token - window_size / 2
463 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
468 /* Dump debugging information for the given PARSER. If FILE is NULL,
469 the output is printed on stderr. */
472 cp_debug_parser (FILE *file, cp_parser *parser)
474 const size_t window_size = 20;
476 expanded_location eloc;
481 fprintf (file, "Parser state\n\n");
482 fprintf (file, "Number of tokens: %u\n",
483 VEC_length (cp_token, parser->lexer->buffer));
484 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
485 cp_debug_print_tree_if_set (file, "Object scope",
486 parser->object_scope);
487 cp_debug_print_tree_if_set (file, "Qualifying scope",
488 parser->qualifying_scope);
489 cp_debug_print_context_stack (file, parser->context);
490 cp_debug_print_flag (file, "Allow GNU extensions",
491 parser->allow_gnu_extensions_p);
492 cp_debug_print_flag (file, "'>' token is greater-than",
493 parser->greater_than_is_operator_p);
494 cp_debug_print_flag (file, "Default args allowed in current "
495 "parameter list", parser->default_arg_ok_p);
496 cp_debug_print_flag (file, "Parsing integral constant-expression",
497 parser->integral_constant_expression_p);
498 cp_debug_print_flag (file, "Allow non-constant expression in current "
499 "constant-expression",
500 parser->allow_non_integral_constant_expression_p);
501 cp_debug_print_flag (file, "Seen non-constant expression",
502 parser->non_integral_constant_expression_p);
503 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
505 parser->local_variables_forbidden_p);
506 cp_debug_print_flag (file, "In unbraced linkage specification",
507 parser->in_unbraced_linkage_specification_p);
508 cp_debug_print_flag (file, "Parsing a declarator",
509 parser->in_declarator_p);
510 cp_debug_print_flag (file, "In template argument list",
511 parser->in_template_argument_list_p);
512 cp_debug_print_flag (file, "Parsing an iteration statement",
513 parser->in_statement & IN_ITERATION_STMT);
514 cp_debug_print_flag (file, "Parsing a switch statement",
515 parser->in_statement & IN_SWITCH_STMT);
516 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
517 parser->in_statement & IN_OMP_BLOCK);
518 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
519 parser->in_statement & IN_OMP_FOR);
520 cp_debug_print_flag (file, "Parsing an if statement",
521 parser->in_statement & IN_IF_STMT);
522 cp_debug_print_flag (file, "Parsing a type-id in an expression "
523 "context", parser->in_type_id_in_expr_p);
524 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
525 parser->implicit_extern_c);
526 cp_debug_print_flag (file, "String expressions should be translated "
527 "to execution character set",
528 parser->translate_strings_p);
529 cp_debug_print_flag (file, "Parsing function body outside of a "
530 "local class", parser->in_function_body);
531 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
532 parser->colon_corrects_to_scope_p);
533 if (parser->type_definition_forbidden_message)
534 fprintf (file, "Error message for forbidden type definitions: %s\n",
535 parser->type_definition_forbidden_message);
536 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
537 fprintf (file, "Number of class definitions in progress: %u\n",
538 parser->num_classes_being_defined);
539 fprintf (file, "Number of template parameter lists for the current "
540 "declaration: %u\n", parser->num_template_parameter_lists);
541 cp_debug_parser_tokens (file, parser, window_size);
542 token = parser->lexer->next_token;
543 fprintf (file, "Next token to parse:\n");
544 fprintf (file, "\tToken: ");
545 cp_lexer_print_token (file, token);
546 eloc = expand_location (token->location);
547 fprintf (file, "\n\tFile: %s\n", eloc.file);
548 fprintf (file, "\tLine: %d\n", eloc.line);
549 fprintf (file, "\tColumn: %d\n", eloc.column);
553 /* Allocate memory for a new lexer object and return it. */
556 cp_lexer_alloc (void)
560 c_common_no_more_pch ();
562 /* Allocate the memory. */
563 lexer = ggc_alloc_cleared_cp_lexer ();
565 /* Initially we are not debugging. */
566 lexer->debugging_p = false;
568 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
569 CP_SAVED_TOKEN_STACK);
571 /* Create the buffer. */
572 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
578 /* Create a new main C++ lexer, the lexer that gets tokens from the
582 cp_lexer_new_main (void)
587 /* It's possible that parsing the first pragma will load a PCH file,
588 which is a GC collection point. So we have to do that before
589 allocating any memory. */
590 cp_parser_initial_pragma (&token);
592 lexer = cp_lexer_alloc ();
594 /* Put the first token in the buffer. */
595 VEC_quick_push (cp_token, lexer->buffer, &token);
597 /* Get the remaining tokens from the preprocessor. */
598 while (token.type != CPP_EOF)
600 cp_lexer_get_preprocessor_token (lexer, &token);
601 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
604 lexer->last_token = VEC_address (cp_token, lexer->buffer)
605 + VEC_length (cp_token, lexer->buffer)
607 lexer->next_token = VEC_length (cp_token, lexer->buffer)
608 ? VEC_address (cp_token, lexer->buffer)
611 /* Subsequent preprocessor diagnostics should use compiler
612 diagnostic functions to get the compiler source location. */
615 gcc_assert (!lexer->next_token->purged_p);
619 /* Create a new lexer whose token stream is primed with the tokens in
620 CACHE. When these tokens are exhausted, no new tokens will be read. */
623 cp_lexer_new_from_tokens (cp_token_cache *cache)
625 cp_token *first = cache->first;
626 cp_token *last = cache->last;
627 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
629 /* We do not own the buffer. */
630 lexer->buffer = NULL;
631 lexer->next_token = first == last ? &eof_token : first;
632 lexer->last_token = last;
634 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
635 CP_SAVED_TOKEN_STACK);
637 /* Initially we are not debugging. */
638 lexer->debugging_p = false;
640 gcc_assert (!lexer->next_token->purged_p);
644 /* Frees all resources associated with LEXER. */
647 cp_lexer_destroy (cp_lexer *lexer)
649 VEC_free (cp_token, gc, lexer->buffer);
650 VEC_free (cp_token_position, heap, lexer->saved_tokens);
654 /* Returns nonzero if debugging information should be output. */
657 cp_lexer_debugging_p (cp_lexer *lexer)
659 return lexer->debugging_p;
663 static inline cp_token_position
664 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
666 gcc_assert (!previous_p || lexer->next_token != &eof_token);
668 return lexer->next_token - previous_p;
671 static inline cp_token *
672 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
678 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
680 lexer->next_token = cp_lexer_token_at (lexer, pos);
683 static inline cp_token_position
684 cp_lexer_previous_token_position (cp_lexer *lexer)
686 if (lexer->next_token == &eof_token)
687 return lexer->last_token - 1;
689 return cp_lexer_token_position (lexer, true);
692 static inline cp_token *
693 cp_lexer_previous_token (cp_lexer *lexer)
695 cp_token_position tp = cp_lexer_previous_token_position (lexer);
697 return cp_lexer_token_at (lexer, tp);
700 /* nonzero if we are presently saving tokens. */
703 cp_lexer_saving_tokens (const cp_lexer* lexer)
705 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
708 /* Store the next token from the preprocessor in *TOKEN. Return true
709 if we reach EOF. If LEXER is NULL, assume we are handling an
710 initial #pragma pch_preprocess, and thus want the lexer to return
711 processed strings. */
714 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
716 static int is_extern_c = 0;
718 /* Get a new token from the preprocessor. */
720 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
721 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
722 token->keyword = RID_MAX;
723 token->pragma_kind = PRAGMA_NONE;
724 token->purged_p = false;
726 /* On some systems, some header files are surrounded by an
727 implicit extern "C" block. Set a flag in the token if it
728 comes from such a header. */
729 is_extern_c += pending_lang_change;
730 pending_lang_change = 0;
731 token->implicit_extern_c = is_extern_c > 0;
733 /* Check to see if this token is a keyword. */
734 if (token->type == CPP_NAME)
736 if (C_IS_RESERVED_WORD (token->u.value))
738 /* Mark this token as a keyword. */
739 token->type = CPP_KEYWORD;
740 /* Record which keyword. */
741 token->keyword = C_RID_CODE (token->u.value);
745 if (warn_cxx0x_compat
746 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
747 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
749 /* Warn about the C++0x keyword (but still treat it as
751 warning (OPT_Wc__0x_compat,
752 "identifier %qE is a keyword in C++11",
755 /* Clear out the C_RID_CODE so we don't warn about this
756 particular identifier-turned-keyword again. */
757 C_SET_RID_CODE (token->u.value, RID_MAX);
760 token->ambiguous_p = false;
761 token->keyword = RID_MAX;
764 else if (token->type == CPP_AT_NAME)
766 /* This only happens in Objective-C++; it must be a keyword. */
767 token->type = CPP_KEYWORD;
768 switch (C_RID_CODE (token->u.value))
770 /* Replace 'class' with '@class', 'private' with '@private',
771 etc. This prevents confusion with the C++ keyword
772 'class', and makes the tokens consistent with other
773 Objective-C 'AT' keywords. For example '@class' is
774 reported as RID_AT_CLASS which is consistent with
775 '@synchronized', which is reported as
778 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
779 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
780 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
781 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
782 case RID_THROW: token->keyword = RID_AT_THROW; break;
783 case RID_TRY: token->keyword = RID_AT_TRY; break;
784 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
785 default: token->keyword = C_RID_CODE (token->u.value);
788 else if (token->type == CPP_PRAGMA)
790 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
791 token->pragma_kind = ((enum pragma_kind)
792 TREE_INT_CST_LOW (token->u.value));
793 token->u.value = NULL_TREE;
797 /* Update the globals input_location and the input file stack from TOKEN. */
799 cp_lexer_set_source_position_from_token (cp_token *token)
801 if (token->type != CPP_EOF)
803 input_location = token->location;
807 /* Return a pointer to the next token in the token stream, but do not
810 static inline cp_token *
811 cp_lexer_peek_token (cp_lexer *lexer)
813 if (cp_lexer_debugging_p (lexer))
815 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
816 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
817 putc ('\n', cp_lexer_debug_stream);
819 return lexer->next_token;
822 /* Return true if the next token has the indicated TYPE. */
825 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
827 return cp_lexer_peek_token (lexer)->type == type;
830 /* Return true if the next token does not have the indicated TYPE. */
833 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
835 return !cp_lexer_next_token_is (lexer, type);
838 /* Return true if the next token is the indicated KEYWORD. */
841 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
843 return cp_lexer_peek_token (lexer)->keyword == keyword;
846 /* Return true if the next token is not the indicated KEYWORD. */
849 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
851 return cp_lexer_peek_token (lexer)->keyword != keyword;
854 /* Return true if the next token is a keyword for a decl-specifier. */
857 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
861 token = cp_lexer_peek_token (lexer);
862 switch (token->keyword)
864 /* auto specifier: storage-class-specifier in C++,
865 simple-type-specifier in C++0x. */
867 /* Storage classes. */
873 /* Elaborated type specifiers. */
879 /* Simple type specifiers. */
894 /* GNU extensions. */
897 /* C++0x extensions. */
899 case RID_UNDERLYING_TYPE:
907 /* Returns TRUE iff the token T begins a decltype type. */
910 token_is_decltype (cp_token *t)
912 return (t->keyword == RID_DECLTYPE
913 || t->type == CPP_DECLTYPE);
916 /* Returns TRUE iff the next token begins a decltype type. */
919 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
921 cp_token *t = cp_lexer_peek_token (lexer);
922 return token_is_decltype (t);
925 /* Return a pointer to the Nth token in the token stream. If N is 1,
926 then this is precisely equivalent to cp_lexer_peek_token (except
927 that it is not inline). One would like to disallow that case, but
928 there is one case (cp_parser_nth_token_starts_template_id) where
929 the caller passes a variable for N and it might be 1. */
932 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
936 /* N is 1-based, not zero-based. */
939 if (cp_lexer_debugging_p (lexer))
940 fprintf (cp_lexer_debug_stream,
941 "cp_lexer: peeking ahead %ld at token: ", (long)n);
944 token = lexer->next_token;
945 gcc_assert (!n || token != &eof_token);
949 if (token == lexer->last_token)
955 if (!token->purged_p)
959 if (cp_lexer_debugging_p (lexer))
961 cp_lexer_print_token (cp_lexer_debug_stream, token);
962 putc ('\n', cp_lexer_debug_stream);
968 /* Return the next token, and advance the lexer's next_token pointer
969 to point to the next non-purged token. */
972 cp_lexer_consume_token (cp_lexer* lexer)
974 cp_token *token = lexer->next_token;
976 gcc_assert (token != &eof_token);
977 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
982 if (lexer->next_token == lexer->last_token)
984 lexer->next_token = &eof_token;
989 while (lexer->next_token->purged_p);
991 cp_lexer_set_source_position_from_token (token);
993 /* Provide debugging output. */
994 if (cp_lexer_debugging_p (lexer))
996 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
997 cp_lexer_print_token (cp_lexer_debug_stream, token);
998 putc ('\n', cp_lexer_debug_stream);
1004 /* Permanently remove the next token from the token stream, and
1005 advance the next_token pointer to refer to the next non-purged
1009 cp_lexer_purge_token (cp_lexer *lexer)
1011 cp_token *tok = lexer->next_token;
1013 gcc_assert (tok != &eof_token);
1014 tok->purged_p = true;
1015 tok->location = UNKNOWN_LOCATION;
1016 tok->u.value = NULL_TREE;
1017 tok->keyword = RID_MAX;
1022 if (tok == lexer->last_token)
1028 while (tok->purged_p);
1029 lexer->next_token = tok;
1032 /* Permanently remove all tokens after TOK, up to, but not
1033 including, the token that will be returned next by
1034 cp_lexer_peek_token. */
1037 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1039 cp_token *peek = lexer->next_token;
1041 if (peek == &eof_token)
1042 peek = lexer->last_token;
1044 gcc_assert (tok < peek);
1046 for ( tok += 1; tok != peek; tok += 1)
1048 tok->purged_p = true;
1049 tok->location = UNKNOWN_LOCATION;
1050 tok->u.value = NULL_TREE;
1051 tok->keyword = RID_MAX;
1055 /* Begin saving tokens. All tokens consumed after this point will be
1059 cp_lexer_save_tokens (cp_lexer* lexer)
1061 /* Provide debugging output. */
1062 if (cp_lexer_debugging_p (lexer))
1063 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1065 VEC_safe_push (cp_token_position, heap,
1066 lexer->saved_tokens, lexer->next_token);
1069 /* Commit to the portion of the token stream most recently saved. */
1072 cp_lexer_commit_tokens (cp_lexer* lexer)
1074 /* Provide debugging output. */
1075 if (cp_lexer_debugging_p (lexer))
1076 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1078 VEC_pop (cp_token_position, lexer->saved_tokens);
1081 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1082 to the token stream. Stop saving tokens. */
1085 cp_lexer_rollback_tokens (cp_lexer* lexer)
1087 /* Provide debugging output. */
1088 if (cp_lexer_debugging_p (lexer))
1089 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1091 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1094 /* Print a representation of the TOKEN on the STREAM. */
1097 cp_lexer_print_token (FILE * stream, cp_token *token)
1099 /* We don't use cpp_type2name here because the parser defines
1100 a few tokens of its own. */
1101 static const char *const token_names[] = {
1102 /* cpplib-defined token types */
1103 #define OP(e, s) #e,
1104 #define TK(e, s) #e,
1108 /* C++ parser token types - see "Manifest constants", above. */
1111 "NESTED_NAME_SPECIFIER",
1114 /* For some tokens, print the associated data. */
1115 switch (token->type)
1118 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1119 For example, `struct' is mapped to an INTEGER_CST. */
1120 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1122 /* else fall through */
1124 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1131 case CPP_UTF8STRING:
1132 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1136 print_generic_expr (stream, token->u.value, 0);
1140 /* If we have a name for the token, print it out. Otherwise, we
1141 simply give the numeric code. */
1142 if (token->type < ARRAY_SIZE(token_names))
1143 fputs (token_names[token->type], stream);
1145 fprintf (stream, "[%d]", token->type);
1150 /* Start emitting debugging information. */
1153 cp_lexer_start_debugging (cp_lexer* lexer)
1155 lexer->debugging_p = true;
1156 cp_lexer_debug_stream = stderr;
1159 /* Stop emitting debugging information. */
1162 cp_lexer_stop_debugging (cp_lexer* lexer)
1164 lexer->debugging_p = false;
1165 cp_lexer_debug_stream = NULL;
1168 /* Create a new cp_token_cache, representing a range of tokens. */
1170 static cp_token_cache *
1171 cp_token_cache_new (cp_token *first, cp_token *last)
1173 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1174 cache->first = first;
1180 /* Decl-specifiers. */
1182 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1185 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1187 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1192 /* Nothing other than the parser should be creating declarators;
1193 declarators are a semi-syntactic representation of C++ entities.
1194 Other parts of the front end that need to create entities (like
1195 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1197 static cp_declarator *make_call_declarator
1198 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1199 static cp_declarator *make_array_declarator
1200 (cp_declarator *, tree);
1201 static cp_declarator *make_pointer_declarator
1202 (cp_cv_quals, cp_declarator *);
1203 static cp_declarator *make_reference_declarator
1204 (cp_cv_quals, cp_declarator *, bool);
1205 static cp_parameter_declarator *make_parameter_declarator
1206 (cp_decl_specifier_seq *, cp_declarator *, tree);
1207 static cp_declarator *make_ptrmem_declarator
1208 (cp_cv_quals, tree, cp_declarator *);
1210 /* An erroneous declarator. */
1211 static cp_declarator *cp_error_declarator;
1213 /* The obstack on which declarators and related data structures are
1215 static struct obstack declarator_obstack;
1217 /* Alloc BYTES from the declarator memory pool. */
1219 static inline void *
1220 alloc_declarator (size_t bytes)
1222 return obstack_alloc (&declarator_obstack, bytes);
1225 /* Allocate a declarator of the indicated KIND. Clear fields that are
1226 common to all declarators. */
1228 static cp_declarator *
1229 make_declarator (cp_declarator_kind kind)
1231 cp_declarator *declarator;
1233 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1234 declarator->kind = kind;
1235 declarator->attributes = NULL_TREE;
1236 declarator->declarator = NULL;
1237 declarator->parameter_pack_p = false;
1238 declarator->id_loc = UNKNOWN_LOCATION;
1243 /* Make a declarator for a generalized identifier. If
1244 QUALIFYING_SCOPE is non-NULL, the identifier is
1245 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1246 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1249 static cp_declarator *
1250 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1251 special_function_kind sfk)
1253 cp_declarator *declarator;
1255 /* It is valid to write:
1257 class C { void f(); };
1261 The standard is not clear about whether `typedef const C D' is
1262 legal; as of 2002-09-15 the committee is considering that
1263 question. EDG 3.0 allows that syntax. Therefore, we do as
1265 if (qualifying_scope && TYPE_P (qualifying_scope))
1266 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1268 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1269 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1270 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1272 declarator = make_declarator (cdk_id);
1273 declarator->u.id.qualifying_scope = qualifying_scope;
1274 declarator->u.id.unqualified_name = unqualified_name;
1275 declarator->u.id.sfk = sfk;
1280 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1281 of modifiers such as const or volatile to apply to the pointer
1282 type, represented as identifiers. */
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1287 cp_declarator *declarator;
1289 declarator = make_declarator (cdk_pointer);
1290 declarator->declarator = target;
1291 declarator->u.pointer.qualifiers = cv_qualifiers;
1292 declarator->u.pointer.class_type = NULL_TREE;
1295 declarator->id_loc = target->id_loc;
1296 declarator->parameter_pack_p = target->parameter_pack_p;
1297 target->parameter_pack_p = false;
1300 declarator->parameter_pack_p = false;
1305 /* Like make_pointer_declarator -- but for references. */
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1311 cp_declarator *declarator;
1313 declarator = make_declarator (cdk_reference);
1314 declarator->declarator = target;
1315 declarator->u.reference.qualifiers = cv_qualifiers;
1316 declarator->u.reference.rvalue_ref = rvalue_ref;
1319 declarator->id_loc = target->id_loc;
1320 declarator->parameter_pack_p = target->parameter_pack_p;
1321 target->parameter_pack_p = false;
1324 declarator->parameter_pack_p = false;
1329 /* Like make_pointer_declarator -- but for a pointer to a non-static
1330 member of CLASS_TYPE. */
1333 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1334 cp_declarator *pointee)
1336 cp_declarator *declarator;
1338 declarator = make_declarator (cdk_ptrmem);
1339 declarator->declarator = pointee;
1340 declarator->u.pointer.qualifiers = cv_qualifiers;
1341 declarator->u.pointer.class_type = class_type;
1345 declarator->parameter_pack_p = pointee->parameter_pack_p;
1346 pointee->parameter_pack_p = false;
1349 declarator->parameter_pack_p = false;
1354 /* Make a declarator for the function given by TARGET, with the
1355 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1356 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1357 indicates what exceptions can be thrown. */
1360 make_call_declarator (cp_declarator *target,
1362 cp_cv_quals cv_qualifiers,
1363 cp_virt_specifiers virt_specifiers,
1364 tree exception_specification,
1365 tree late_return_type)
1367 cp_declarator *declarator;
1369 declarator = make_declarator (cdk_function);
1370 declarator->declarator = target;
1371 declarator->u.function.parameters = parms;
1372 declarator->u.function.qualifiers = cv_qualifiers;
1373 declarator->u.function.virt_specifiers = virt_specifiers;
1374 declarator->u.function.exception_specification = exception_specification;
1375 declarator->u.function.late_return_type = late_return_type;
1378 declarator->id_loc = target->id_loc;
1379 declarator->parameter_pack_p = target->parameter_pack_p;
1380 target->parameter_pack_p = false;
1383 declarator->parameter_pack_p = false;
1388 /* Make a declarator for an array of BOUNDS elements, each of which is
1389 defined by ELEMENT. */
1392 make_array_declarator (cp_declarator *element, tree bounds)
1394 cp_declarator *declarator;
1396 declarator = make_declarator (cdk_array);
1397 declarator->declarator = element;
1398 declarator->u.array.bounds = bounds;
1401 declarator->id_loc = element->id_loc;
1402 declarator->parameter_pack_p = element->parameter_pack_p;
1403 element->parameter_pack_p = false;
1406 declarator->parameter_pack_p = false;
1411 /* Determine whether the declarator we've seen so far can be a
1412 parameter pack, when followed by an ellipsis. */
1414 declarator_can_be_parameter_pack (cp_declarator *declarator)
1416 /* Search for a declarator name, or any other declarator that goes
1417 after the point where the ellipsis could appear in a parameter
1418 pack. If we find any of these, then this declarator can not be
1419 made into a parameter pack. */
1421 while (declarator && !found)
1423 switch ((int)declarator->kind)
1434 declarator = declarator->declarator;
1442 cp_parameter_declarator *no_parameters;
1444 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1445 DECLARATOR and DEFAULT_ARGUMENT. */
1447 cp_parameter_declarator *
1448 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1449 cp_declarator *declarator,
1450 tree default_argument)
1452 cp_parameter_declarator *parameter;
1454 parameter = ((cp_parameter_declarator *)
1455 alloc_declarator (sizeof (cp_parameter_declarator)));
1456 parameter->next = NULL;
1457 if (decl_specifiers)
1458 parameter->decl_specifiers = *decl_specifiers;
1460 clear_decl_specs (¶meter->decl_specifiers);
1461 parameter->declarator = declarator;
1462 parameter->default_argument = default_argument;
1463 parameter->ellipsis_p = false;
1468 /* Returns true iff DECLARATOR is a declaration for a function. */
1471 function_declarator_p (const cp_declarator *declarator)
1475 if (declarator->kind == cdk_function
1476 && declarator->declarator->kind == cdk_id)
1478 if (declarator->kind == cdk_id
1479 || declarator->kind == cdk_error)
1481 declarator = declarator->declarator;
1491 A cp_parser parses the token stream as specified by the C++
1492 grammar. Its job is purely parsing, not semantic analysis. For
1493 example, the parser breaks the token stream into declarators,
1494 expressions, statements, and other similar syntactic constructs.
1495 It does not check that the types of the expressions on either side
1496 of an assignment-statement are compatible, or that a function is
1497 not declared with a parameter of type `void'.
1499 The parser invokes routines elsewhere in the compiler to perform
1500 semantic analysis and to build up the abstract syntax tree for the
1503 The parser (and the template instantiation code, which is, in a
1504 way, a close relative of parsing) are the only parts of the
1505 compiler that should be calling push_scope and pop_scope, or
1506 related functions. The parser (and template instantiation code)
1507 keeps track of what scope is presently active; everything else
1508 should simply honor that. (The code that generates static
1509 initializers may also need to set the scope, in order to check
1510 access control correctly when emitting the initializers.)
1515 The parser is of the standard recursive-descent variety. Upcoming
1516 tokens in the token stream are examined in order to determine which
1517 production to use when parsing a non-terminal. Some C++ constructs
1518 require arbitrary look ahead to disambiguate. For example, it is
1519 impossible, in the general case, to tell whether a statement is an
1520 expression or declaration without scanning the entire statement.
1521 Therefore, the parser is capable of "parsing tentatively." When the
1522 parser is not sure what construct comes next, it enters this mode.
1523 Then, while we attempt to parse the construct, the parser queues up
1524 error messages, rather than issuing them immediately, and saves the
1525 tokens it consumes. If the construct is parsed successfully, the
1526 parser "commits", i.e., it issues any queued error messages and
1527 the tokens that were being preserved are permanently discarded.
1528 If, however, the construct is not parsed successfully, the parser
1529 rolls back its state completely so that it can resume parsing using
1530 a different alternative.
1535 The performance of the parser could probably be improved substantially.
1536 We could often eliminate the need to parse tentatively by looking ahead
1537 a little bit. In some places, this approach might not entirely eliminate
1538 the need to parse tentatively, but it might still speed up the average
1541 /* Flags that are passed to some parsing functions. These values can
1542 be bitwise-ored together. */
1547 CP_PARSER_FLAGS_NONE = 0x0,
1548 /* The construct is optional. If it is not present, then no error
1549 should be issued. */
1550 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1551 /* When parsing a type-specifier, treat user-defined type-names
1552 as non-type identifiers. */
1553 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1554 /* When parsing a type-specifier, do not try to parse a class-specifier
1555 or enum-specifier. */
1556 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1557 /* When parsing a decl-specifier-seq, only allow type-specifier or
1559 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1562 /* This type is used for parameters and variables which hold
1563 combinations of the above flags. */
1564 typedef int cp_parser_flags;
1566 /* The different kinds of declarators we want to parse. */
1568 typedef enum cp_parser_declarator_kind
1570 /* We want an abstract declarator. */
1571 CP_PARSER_DECLARATOR_ABSTRACT,
1572 /* We want a named declarator. */
1573 CP_PARSER_DECLARATOR_NAMED,
1574 /* We don't mind, but the name must be an unqualified-id. */
1575 CP_PARSER_DECLARATOR_EITHER
1576 } cp_parser_declarator_kind;
1578 /* The precedence values used to parse binary expressions. The minimum value
1579 of PREC must be 1, because zero is reserved to quickly discriminate
1580 binary operators from other tokens. */
1585 PREC_LOGICAL_OR_EXPRESSION,
1586 PREC_LOGICAL_AND_EXPRESSION,
1587 PREC_INCLUSIVE_OR_EXPRESSION,
1588 PREC_EXCLUSIVE_OR_EXPRESSION,
1589 PREC_AND_EXPRESSION,
1590 PREC_EQUALITY_EXPRESSION,
1591 PREC_RELATIONAL_EXPRESSION,
1592 PREC_SHIFT_EXPRESSION,
1593 PREC_ADDITIVE_EXPRESSION,
1594 PREC_MULTIPLICATIVE_EXPRESSION,
1596 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1599 /* A mapping from a token type to a corresponding tree node type, with a
1600 precedence value. */
1602 typedef struct cp_parser_binary_operations_map_node
1604 /* The token type. */
1605 enum cpp_ttype token_type;
1606 /* The corresponding tree code. */
1607 enum tree_code tree_type;
1608 /* The precedence of this operator. */
1609 enum cp_parser_prec prec;
1610 } cp_parser_binary_operations_map_node;
1612 typedef struct cp_parser_expression_stack_entry
1614 /* Left hand side of the binary operation we are currently
1617 /* Original tree code for left hand side, if it was a binary
1618 expression itself (used for -Wparentheses). */
1619 enum tree_code lhs_type;
1620 /* Tree code for the binary operation we are parsing. */
1621 enum tree_code tree_type;
1622 /* Precedence of the binary operation we are parsing. */
1623 enum cp_parser_prec prec;
1624 } cp_parser_expression_stack_entry;
1626 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1627 entries because precedence levels on the stack are monotonically
1629 typedef struct cp_parser_expression_stack_entry
1630 cp_parser_expression_stack[NUM_PREC_VALUES];
1634 /* Constructors and destructors. */
1636 static cp_parser_context *cp_parser_context_new
1637 (cp_parser_context *);
1639 /* Class variables. */
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644 Transformed into an associative array (binops_by_token) by
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1651 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1655 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1658 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1661 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1666 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1669 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1671 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1673 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1675 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1677 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1680 /* The same as binops, but initialized by cp_parser_new so that
1681 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1685 /* Constructors and destructors. */
1687 /* Construct a new context. The context below this one on the stack
1688 is given by NEXT. */
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1693 cp_parser_context *context;
1695 /* Allocate the storage. */
1696 if (cp_parser_context_free_list != NULL)
1698 /* Pull the first entry from the free list. */
1699 context = cp_parser_context_free_list;
1700 cp_parser_context_free_list = context->next;
1701 memset (context, 0, sizeof (*context));
1704 context = ggc_alloc_cleared_cp_parser_context ();
1706 /* No errors have occurred yet in this context. */
1707 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708 /* If this is not the bottommost context, copy information that we
1709 need from the previous context. */
1712 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713 expression, then we are parsing one in this context, too. */
1714 context->object_type = next->object_type;
1715 /* Thread the stack. */
1716 context->next = next;
1722 /* Managing the unparsed function queues. */
1724 #define unparsed_funs_with_default_args \
1725 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728 #define unparsed_nsdmis \
1729 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1732 push_unparsed_function_queues (cp_parser *parser)
1734 VEC_safe_push (cp_unparsed_functions_entry, gc,
1735 parser->unparsed_queues, NULL);
1736 unparsed_funs_with_default_args = NULL;
1737 unparsed_funs_with_definitions = make_tree_vector ();
1738 unparsed_nsdmis = NULL;
1742 pop_unparsed_function_queues (cp_parser *parser)
1744 release_tree_vector (unparsed_funs_with_definitions);
1745 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1750 /* Constructors and destructors. */
1752 static cp_parser *cp_parser_new
1755 /* Routines to parse various constructs.
1757 Those that return `tree' will return the error_mark_node (rather
1758 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759 Sometimes, they will return an ordinary node if error-recovery was
1760 attempted, even though a parse error occurred. So, to check
1761 whether or not a parse error occurred, you should always use
1762 cp_parser_error_occurred. If the construct is optional (indicated
1763 either by an `_opt' in the name of the function that does the
1764 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765 the construct is not present. */
1767 /* Lexical conventions [gram.lex] */
1769 static tree cp_parser_identifier
1771 static tree cp_parser_string_literal
1772 (cp_parser *, bool, bool);
1773 static tree cp_parser_userdef_char_literal
1775 static tree cp_parser_userdef_string_literal
1777 static tree cp_parser_userdef_numeric_literal
1780 /* Basic concepts [gram.basic] */
1782 static bool cp_parser_translation_unit
1785 /* Expressions [gram.expr] */
1787 static tree cp_parser_primary_expression
1788 (cp_parser *, bool, bool, bool, cp_id_kind *);
1789 static tree cp_parser_id_expression
1790 (cp_parser *, bool, bool, bool *, bool, bool);
1791 static tree cp_parser_unqualified_id
1792 (cp_parser *, bool, bool, bool, bool);
1793 static tree cp_parser_nested_name_specifier_opt
1794 (cp_parser *, bool, bool, bool, bool);
1795 static tree cp_parser_nested_name_specifier
1796 (cp_parser *, bool, bool, bool, bool);
1797 static tree cp_parser_qualifying_entity
1798 (cp_parser *, bool, bool, bool, bool, bool);
1799 static tree cp_parser_postfix_expression
1800 (cp_parser *, bool, bool, bool, cp_id_kind *);
1801 static tree cp_parser_postfix_open_square_expression
1802 (cp_parser *, tree, bool);
1803 static tree cp_parser_postfix_dot_deref_expression
1804 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1806 (cp_parser *, int, bool, bool, bool *);
1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1809 static void cp_parser_pseudo_destructor_name
1810 (cp_parser *, tree *, tree *);
1811 static tree cp_parser_unary_expression
1812 (cp_parser *, bool, bool, cp_id_kind *);
1813 static enum tree_code cp_parser_unary_operator
1815 static tree cp_parser_new_expression
1817 static VEC(tree,gc) *cp_parser_new_placement
1819 static tree cp_parser_new_type_id
1820 (cp_parser *, tree *);
1821 static cp_declarator *cp_parser_new_declarator_opt
1823 static cp_declarator *cp_parser_direct_new_declarator
1825 static VEC(tree,gc) *cp_parser_new_initializer
1827 static tree cp_parser_delete_expression
1829 static tree cp_parser_cast_expression
1830 (cp_parser *, bool, bool, cp_id_kind *);
1831 static tree cp_parser_binary_expression
1832 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1833 static tree cp_parser_question_colon_clause
1834 (cp_parser *, tree);
1835 static tree cp_parser_assignment_expression
1836 (cp_parser *, bool, cp_id_kind *);
1837 static enum tree_code cp_parser_assignment_operator_opt
1839 static tree cp_parser_expression
1840 (cp_parser *, bool, cp_id_kind *);
1841 static tree cp_parser_constant_expression
1842 (cp_parser *, bool, bool *);
1843 static tree cp_parser_builtin_offsetof
1845 static tree cp_parser_lambda_expression
1847 static void cp_parser_lambda_introducer
1848 (cp_parser *, tree);
1849 static bool cp_parser_lambda_declarator_opt
1850 (cp_parser *, tree);
1851 static void cp_parser_lambda_body
1852 (cp_parser *, tree);
1854 /* Statements [gram.stmt.stmt] */
1856 static void cp_parser_statement
1857 (cp_parser *, tree, bool, bool *);
1858 static void cp_parser_label_for_labeled_statement
1860 static tree cp_parser_expression_statement
1861 (cp_parser *, tree);
1862 static tree cp_parser_compound_statement
1863 (cp_parser *, tree, bool, bool);
1864 static void cp_parser_statement_seq_opt
1865 (cp_parser *, tree);
1866 static tree cp_parser_selection_statement
1867 (cp_parser *, bool *);
1868 static tree cp_parser_condition
1870 static tree cp_parser_iteration_statement
1872 static bool cp_parser_for_init_statement
1873 (cp_parser *, tree *decl);
1874 static tree cp_parser_for
1876 static tree cp_parser_c_for
1877 (cp_parser *, tree, tree);
1878 static tree cp_parser_range_for
1879 (cp_parser *, tree, tree, tree);
1880 static void do_range_for_auto_deduction
1882 static tree cp_parser_perform_range_for_lookup
1883 (tree, tree *, tree *);
1884 static tree cp_parser_range_for_member_function
1886 static tree cp_parser_jump_statement
1888 static void cp_parser_declaration_statement
1891 static tree cp_parser_implicitly_scoped_statement
1892 (cp_parser *, bool *);
1893 static void cp_parser_already_scoped_statement
1896 /* Declarations [gram.dcl.dcl] */
1898 static void cp_parser_declaration_seq_opt
1900 static void cp_parser_declaration
1902 static void cp_parser_block_declaration
1903 (cp_parser *, bool);
1904 static void cp_parser_simple_declaration
1905 (cp_parser *, bool, tree *);
1906 static void cp_parser_decl_specifier_seq
1907 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1908 static tree cp_parser_storage_class_specifier_opt
1910 static tree cp_parser_function_specifier_opt
1911 (cp_parser *, cp_decl_specifier_seq *);
1912 static tree cp_parser_type_specifier
1913 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1915 static tree cp_parser_simple_type_specifier
1916 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1917 static tree cp_parser_type_name
1919 static tree cp_parser_nonclass_name
1920 (cp_parser* parser);
1921 static tree cp_parser_elaborated_type_specifier
1922 (cp_parser *, bool, bool);
1923 static tree cp_parser_enum_specifier
1925 static void cp_parser_enumerator_list
1926 (cp_parser *, tree);
1927 static void cp_parser_enumerator_definition
1928 (cp_parser *, tree);
1929 static tree cp_parser_namespace_name
1931 static void cp_parser_namespace_definition
1933 static void cp_parser_namespace_body
1935 static tree cp_parser_qualified_namespace_specifier
1937 static void cp_parser_namespace_alias_definition
1939 static bool cp_parser_using_declaration
1940 (cp_parser *, bool);
1941 static void cp_parser_using_directive
1943 static tree cp_parser_alias_declaration
1945 static void cp_parser_asm_definition
1947 static void cp_parser_linkage_specification
1949 static void cp_parser_static_assert
1950 (cp_parser *, bool);
1951 static tree cp_parser_decltype
1954 /* Declarators [gram.dcl.decl] */
1956 static tree cp_parser_init_declarator
1957 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1958 static cp_declarator *cp_parser_declarator
1959 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1960 static cp_declarator *cp_parser_direct_declarator
1961 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1962 static enum tree_code cp_parser_ptr_operator
1963 (cp_parser *, tree *, cp_cv_quals *);
1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1968 static tree cp_parser_late_return_type_opt
1969 (cp_parser *, cp_cv_quals);
1970 static tree cp_parser_declarator_id
1971 (cp_parser *, bool);
1972 static tree cp_parser_type_id
1974 static tree cp_parser_template_type_arg
1976 static tree cp_parser_trailing_type_id (cp_parser *);
1977 static tree cp_parser_type_id_1
1978 (cp_parser *, bool, bool);
1979 static void cp_parser_type_specifier_seq
1980 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1981 static tree cp_parser_parameter_declaration_clause
1983 static tree cp_parser_parameter_declaration_list
1984 (cp_parser *, bool *);
1985 static cp_parameter_declarator *cp_parser_parameter_declaration
1986 (cp_parser *, bool, bool *);
1987 static tree cp_parser_default_argument
1988 (cp_parser *, bool);
1989 static void cp_parser_function_body
1991 static tree cp_parser_initializer
1992 (cp_parser *, bool *, bool *);
1993 static tree cp_parser_initializer_clause
1994 (cp_parser *, bool *);
1995 static tree cp_parser_braced_list
1996 (cp_parser*, bool*);
1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1998 (cp_parser *, bool *);
2000 static bool cp_parser_ctor_initializer_opt_and_function_body
2003 /* Classes [gram.class] */
2005 static tree cp_parser_class_name
2006 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2007 static tree cp_parser_class_specifier
2009 static tree cp_parser_class_head
2010 (cp_parser *, bool *, tree *, tree *);
2011 static enum tag_types cp_parser_class_key
2013 static void cp_parser_member_specification_opt
2015 static void cp_parser_member_declaration
2017 static tree cp_parser_pure_specifier
2019 static tree cp_parser_constant_initializer
2022 /* Derived classes [gram.class.derived] */
2024 static tree cp_parser_base_clause
2026 static tree cp_parser_base_specifier
2029 /* Special member functions [gram.special] */
2031 static tree cp_parser_conversion_function_id
2033 static tree cp_parser_conversion_type_id
2035 static cp_declarator *cp_parser_conversion_declarator_opt
2037 static bool cp_parser_ctor_initializer_opt
2039 static void cp_parser_mem_initializer_list
2041 static tree cp_parser_mem_initializer
2043 static tree cp_parser_mem_initializer_id
2046 /* Overloading [gram.over] */
2048 static tree cp_parser_operator_function_id
2050 static tree cp_parser_operator
2053 /* Templates [gram.temp] */
2055 static void cp_parser_template_declaration
2056 (cp_parser *, bool);
2057 static tree cp_parser_template_parameter_list
2059 static tree cp_parser_template_parameter
2060 (cp_parser *, bool *, bool *);
2061 static tree cp_parser_type_parameter
2062 (cp_parser *, bool *);
2063 static tree cp_parser_template_id
2064 (cp_parser *, bool, bool, bool);
2065 static tree cp_parser_template_name
2066 (cp_parser *, bool, bool, bool, bool *);
2067 static tree cp_parser_template_argument_list
2069 static tree cp_parser_template_argument
2071 static void cp_parser_explicit_instantiation
2073 static void cp_parser_explicit_specialization
2076 /* Exception handling [gram.exception] */
2078 static tree cp_parser_try_block
2080 static bool cp_parser_function_try_block
2082 static void cp_parser_handler_seq
2084 static void cp_parser_handler
2086 static tree cp_parser_exception_declaration
2088 static tree cp_parser_throw_expression
2090 static tree cp_parser_exception_specification_opt
2092 static tree cp_parser_type_id_list
2095 /* GNU Extensions */
2097 static tree cp_parser_asm_specification_opt
2099 static tree cp_parser_asm_operand_list
2101 static tree cp_parser_asm_clobber_list
2103 static tree cp_parser_asm_label_list
2105 static tree cp_parser_attributes_opt
2107 static tree cp_parser_attribute_list
2109 static bool cp_parser_extension_opt
2110 (cp_parser *, int *);
2111 static void cp_parser_label_declaration
2114 /* Transactional Memory Extensions */
2116 static tree cp_parser_transaction
2117 (cp_parser *, enum rid);
2118 static tree cp_parser_transaction_expression
2119 (cp_parser *, enum rid);
2120 static bool cp_parser_function_transaction
2121 (cp_parser *, enum rid);
2122 static tree cp_parser_transaction_cancel
2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2126 static bool cp_parser_pragma
2127 (cp_parser *, enum pragma_context);
2129 /* Objective-C++ Productions */
2131 static tree cp_parser_objc_message_receiver
2133 static tree cp_parser_objc_message_args
2135 static tree cp_parser_objc_message_expression
2137 static tree cp_parser_objc_encode_expression
2139 static tree cp_parser_objc_defs_expression
2141 static tree cp_parser_objc_protocol_expression
2143 static tree cp_parser_objc_selector_expression
2145 static tree cp_parser_objc_expression
2147 static bool cp_parser_objc_selector_p
2149 static tree cp_parser_objc_selector
2151 static tree cp_parser_objc_protocol_refs_opt
2153 static void cp_parser_objc_declaration
2154 (cp_parser *, tree);
2155 static tree cp_parser_objc_statement
2157 static bool cp_parser_objc_valid_prefix_attributes
2158 (cp_parser *, tree *);
2159 static void cp_parser_objc_at_property_declaration
2161 static void cp_parser_objc_at_synthesize_declaration
2163 static void cp_parser_objc_at_dynamic_declaration
2165 static tree cp_parser_objc_struct_declaration
2168 /* Utility Routines */
2170 static tree cp_parser_lookup_name
2171 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2172 static tree cp_parser_lookup_name_simple
2173 (cp_parser *, tree, location_t);
2174 static tree cp_parser_maybe_treat_template_as_class
2176 static bool cp_parser_check_declarator_template_parameters
2177 (cp_parser *, cp_declarator *, location_t);
2178 static bool cp_parser_check_template_parameters
2179 (cp_parser *, unsigned, location_t, cp_declarator *);
2180 static tree cp_parser_simple_cast_expression
2182 static tree cp_parser_global_scope_opt
2183 (cp_parser *, bool);
2184 static bool cp_parser_constructor_declarator_p
2185 (cp_parser *, bool);
2186 static tree cp_parser_function_definition_from_specifiers_and_declarator
2187 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2188 static tree cp_parser_function_definition_after_declarator
2189 (cp_parser *, bool);
2190 static void cp_parser_template_declaration_after_export
2191 (cp_parser *, bool);
2192 static void cp_parser_perform_template_parameter_access_checks
2193 (VEC (deferred_access_check,gc)*);
2194 static tree cp_parser_single_declaration
2195 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2196 static tree cp_parser_functional_cast
2197 (cp_parser *, tree);
2198 static tree cp_parser_save_member_function_body
2199 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2200 static tree cp_parser_save_nsdmi
2202 static tree cp_parser_enclosed_template_argument_list
2204 static void cp_parser_save_default_args
2205 (cp_parser *, tree);
2206 static void cp_parser_late_parsing_for_member
2207 (cp_parser *, tree);
2208 static tree cp_parser_late_parse_one_default_arg
2209 (cp_parser *, tree, tree, tree);
2210 static void cp_parser_late_parsing_nsdmi
2211 (cp_parser *, tree);
2212 static void cp_parser_late_parsing_default_args
2213 (cp_parser *, tree);
2214 static tree cp_parser_sizeof_operand
2215 (cp_parser *, enum rid);
2216 static tree cp_parser_trait_expr
2217 (cp_parser *, enum rid);
2218 static bool cp_parser_declares_only_class_p
2220 static void cp_parser_set_storage_class
2221 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2222 static void cp_parser_set_decl_spec_type
2223 (cp_decl_specifier_seq *, tree, location_t, bool);
2224 static bool cp_parser_friend_p
2225 (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227 (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229 (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231 (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2234 static bool cp_parser_next_token_starts_class_definition_p
2236 static bool cp_parser_next_token_ends_template_argument_p
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239 (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2242 static void cp_parser_check_class_key
2243 (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245 (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2248 static void cp_parser_pre_parsed_nested_name_specifier
2250 static bool cp_parser_cache_group
2251 (cp_parser *, enum cpp_ttype, unsigned);
2252 static tree cp_parser_cache_defarg
2253 (cp_parser *parser, bool nsdmi);
2254 static void cp_parser_parse_tentatively
2256 static void cp_parser_commit_to_tentative_parse
2258 static void cp_parser_abort_tentative_parse
2260 static bool cp_parser_parse_definitely
2262 static inline bool cp_parser_parsing_tentatively
2264 static bool cp_parser_uncommitted_to_tentative_parse_p
2266 static void cp_parser_error
2267 (cp_parser *, const char *);
2268 static void cp_parser_name_lookup_error
2269 (cp_parser *, tree, tree, name_lookup_error, location_t);
2270 static bool cp_parser_simulate_error
2272 static bool cp_parser_check_type_definition
2274 static void cp_parser_check_for_definition_in_return_type
2275 (cp_declarator *, tree, location_t type_location);
2276 static void cp_parser_check_for_invalid_template_id
2277 (cp_parser *, tree, location_t location);
2278 static bool cp_parser_non_integral_constant_expression
2279 (cp_parser *, non_integral_constant);
2280 static void cp_parser_diagnose_invalid_type_name
2281 (cp_parser *, tree, tree, location_t);
2282 static bool cp_parser_parse_and_diagnose_invalid_type_name
2284 static int cp_parser_skip_to_closing_parenthesis
2285 (cp_parser *, bool, bool, bool);
2286 static void cp_parser_skip_to_end_of_statement
2288 static void cp_parser_consume_semicolon_at_end_of_statement
2290 static void cp_parser_skip_to_end_of_block_or_statement
2292 static bool cp_parser_skip_to_closing_brace
2294 static void cp_parser_skip_to_end_of_template_parameter_list
2296 static void cp_parser_skip_to_pragma_eol
2297 (cp_parser*, cp_token *);
2298 static bool cp_parser_error_occurred
2300 static bool cp_parser_allow_gnu_extensions_p
2302 static bool cp_parser_is_pure_string_literal
2304 static bool cp_parser_is_string_literal
2306 static bool cp_parser_is_keyword
2307 (cp_token *, enum rid);
2308 static tree cp_parser_make_typename_type
2309 (cp_parser *, tree, tree, location_t location);
2310 static cp_declarator * cp_parser_make_indirect_declarator
2311 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2313 /* Returns nonzero if we are parsing tentatively. */
2316 cp_parser_parsing_tentatively (cp_parser* parser)
2318 return parser->context->next != NULL;
2321 /* Returns nonzero if TOKEN is a string literal. */
2324 cp_parser_is_pure_string_literal (cp_token* token)
2326 return (token->type == CPP_STRING ||
2327 token->type == CPP_STRING16 ||
2328 token->type == CPP_STRING32 ||
2329 token->type == CPP_WSTRING ||
2330 token->type == CPP_UTF8STRING);
2333 /* Returns nonzero if TOKEN is a string literal
2334 of a user-defined string literal. */
2337 cp_parser_is_string_literal (cp_token* token)
2339 return (cp_parser_is_pure_string_literal (token) ||
2340 token->type == CPP_STRING_USERDEF ||
2341 token->type == CPP_STRING16_USERDEF ||
2342 token->type == CPP_STRING32_USERDEF ||
2343 token->type == CPP_WSTRING_USERDEF ||
2344 token->type == CPP_UTF8STRING_USERDEF);
2347 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2350 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2352 return token->keyword == keyword;
2355 /* If not parsing tentatively, issue a diagnostic of the form
2356 FILE:LINE: MESSAGE before TOKEN
2357 where TOKEN is the next token in the input stream. MESSAGE
2358 (specified by the caller) is usually of the form "expected
2362 cp_parser_error (cp_parser* parser, const char* gmsgid)
2364 if (!cp_parser_simulate_error (parser))
2366 cp_token *token = cp_lexer_peek_token (parser->lexer);
2367 /* This diagnostic makes more sense if it is tagged to the line
2368 of the token we just peeked at. */
2369 cp_lexer_set_source_position_from_token (token);
2371 if (token->type == CPP_PRAGMA)
2373 error_at (token->location,
2374 "%<#pragma%> is not allowed here");
2375 cp_parser_skip_to_pragma_eol (parser, token);
2379 c_parse_error (gmsgid,
2380 /* Because c_parser_error does not understand
2381 CPP_KEYWORD, keywords are treated like
2383 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2384 token->u.value, token->flags);
2388 /* Issue an error about name-lookup failing. NAME is the
2389 IDENTIFIER_NODE DECL is the result of
2390 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2391 the thing that we hoped to find. */
2394 cp_parser_name_lookup_error (cp_parser* parser,
2397 name_lookup_error desired,
2398 location_t location)
2400 /* If name lookup completely failed, tell the user that NAME was not
2402 if (decl == error_mark_node)
2404 if (parser->scope && parser->scope != global_namespace)
2405 error_at (location, "%<%E::%E%> has not been declared",
2406 parser->scope, name);
2407 else if (parser->scope == global_namespace)
2408 error_at (location, "%<::%E%> has not been declared", name);
2409 else if (parser->object_scope
2410 && !CLASS_TYPE_P (parser->object_scope))
2411 error_at (location, "request for member %qE in non-class type %qT",
2412 name, parser->object_scope);
2413 else if (parser->object_scope)
2414 error_at (location, "%<%T::%E%> has not been declared",
2415 parser->object_scope, name);
2417 error_at (location, "%qE has not been declared", name);
2419 else if (parser->scope && parser->scope != global_namespace)
2424 error_at (location, "%<%E::%E%> is not a type",
2425 parser->scope, name);
2428 error_at (location, "%<%E::%E%> is not a class or namespace",
2429 parser->scope, name);
2433 "%<%E::%E%> is not a class, namespace, or enumeration",
2434 parser->scope, name);
2441 else if (parser->scope == global_namespace)
2446 error_at (location, "%<::%E%> is not a type", name);
2449 error_at (location, "%<::%E%> is not a class or namespace", name);
2453 "%<::%E%> is not a class, namespace, or enumeration",
2465 error_at (location, "%qE is not a type", name);
2468 error_at (location, "%qE is not a class or namespace", name);
2472 "%qE is not a class, namespace, or enumeration", name);
2480 /* If we are parsing tentatively, remember that an error has occurred
2481 during this tentative parse. Returns true if the error was
2482 simulated; false if a message should be issued by the caller. */
2485 cp_parser_simulate_error (cp_parser* parser)
2487 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2489 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2495 /* Check for repeated decl-specifiers. */
2498 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2499 location_t location)
2503 for (ds = ds_first; ds != ds_last; ++ds)
2505 unsigned count = decl_specs->specs[ds];
2508 /* The "long" specifier is a special case because of "long long". */
2512 error_at (location, "%<long long long%> is too long for GCC");
2514 pedwarn_cxx98 (location, OPT_Wlong_long,
2515 "ISO C++ 1998 does not support %<long long%>");
2519 static const char *const decl_spec_names[] = {
2537 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2542 /* This function is called when a type is defined. If type
2543 definitions are forbidden at this point, an error message is
2547 cp_parser_check_type_definition (cp_parser* parser)
2549 /* If types are forbidden here, issue a message. */
2550 if (parser->type_definition_forbidden_message)
2552 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2553 in the message need to be interpreted. */
2554 error (parser->type_definition_forbidden_message);
2560 /* This function is called when the DECLARATOR is processed. The TYPE
2561 was a type defined in the decl-specifiers. If it is invalid to
2562 define a type in the decl-specifiers for DECLARATOR, an error is
2563 issued. TYPE_LOCATION is the location of TYPE and is used
2564 for error reporting. */
2567 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2568 tree type, location_t type_location)
2570 /* [dcl.fct] forbids type definitions in return types.
2571 Unfortunately, it's not easy to know whether or not we are
2572 processing a return type until after the fact. */
2574 && (declarator->kind == cdk_pointer
2575 || declarator->kind == cdk_reference
2576 || declarator->kind == cdk_ptrmem))
2577 declarator = declarator->declarator;
2579 && declarator->kind == cdk_function)
2581 error_at (type_location,
2582 "new types may not be defined in a return type");
2583 inform (type_location,
2584 "(perhaps a semicolon is missing after the definition of %qT)",
2589 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2590 "<" in any valid C++ program. If the next token is indeed "<",
2591 issue a message warning the user about what appears to be an
2592 invalid attempt to form a template-id. LOCATION is the location
2593 of the type-specifier (TYPE) */
2596 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2597 tree type, location_t location)
2599 cp_token_position start = 0;
2601 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2604 error_at (location, "%qT is not a template", type);
2605 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2606 error_at (location, "%qE is not a template", type);
2608 error_at (location, "invalid template-id");
2609 /* Remember the location of the invalid "<". */
2610 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2611 start = cp_lexer_token_position (parser->lexer, true);
2612 /* Consume the "<". */
2613 cp_lexer_consume_token (parser->lexer);
2614 /* Parse the template arguments. */
2615 cp_parser_enclosed_template_argument_list (parser);
2616 /* Permanently remove the invalid template arguments so that
2617 this error message is not issued again. */
2619 cp_lexer_purge_tokens_after (parser->lexer, start);
2623 /* If parsing an integral constant-expression, issue an error message
2624 about the fact that THING appeared and return true. Otherwise,
2625 return false. In either case, set
2626 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2629 cp_parser_non_integral_constant_expression (cp_parser *parser,
2630 non_integral_constant thing)
2632 parser->non_integral_constant_expression_p = true;
2633 if (parser->integral_constant_expression_p)
2635 if (!parser->allow_non_integral_constant_expression_p)
2637 const char *msg = NULL;
2641 error ("floating-point literal "
2642 "cannot appear in a constant-expression");
2645 error ("a cast to a type other than an integral or "
2646 "enumeration type cannot appear in a "
2647 "constant-expression");
2650 error ("%<typeid%> operator "
2651 "cannot appear in a constant-expression");
2654 error ("non-constant compound literals "
2655 "cannot appear in a constant-expression");
2658 error ("a function call "
2659 "cannot appear in a constant-expression");
2662 error ("an increment "
2663 "cannot appear in a constant-expression");
2666 error ("an decrement "
2667 "cannot appear in a constant-expression");
2670 error ("an array reference "
2671 "cannot appear in a constant-expression");
2673 case NIC_ADDR_LABEL:
2674 error ("the address of a label "
2675 "cannot appear in a constant-expression");
2677 case NIC_OVERLOADED:
2678 error ("calls to overloaded operators "
2679 "cannot appear in a constant-expression");
2681 case NIC_ASSIGNMENT:
2682 error ("an assignment cannot appear in a constant-expression");
2685 error ("a comma operator "
2686 "cannot appear in a constant-expression");
2688 case NIC_CONSTRUCTOR:
2689 error ("a call to a constructor "
2690 "cannot appear in a constant-expression");
2692 case NIC_TRANSACTION:
2693 error ("a transaction expression "
2694 "cannot appear in a constant-expression");
2700 msg = "__FUNCTION__";
2702 case NIC_PRETTY_FUNC:
2703 msg = "__PRETTY_FUNCTION__";
2723 case NIC_PREINCREMENT:
2726 case NIC_PREDECREMENT:
2739 error ("%qs cannot appear in a constant-expression", msg);
2746 /* Emit a diagnostic for an invalid type name. SCOPE is the
2747 qualifying scope (or NULL, if none) for ID. This function commits
2748 to the current active tentative parse, if any. (Otherwise, the
2749 problematic construct might be encountered again later, resulting
2750 in duplicate error messages.) LOCATION is the location of ID. */
2753 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2754 tree scope, tree id,
2755 location_t location)
2757 tree decl, old_scope;
2758 cp_parser_commit_to_tentative_parse (parser);
2759 /* Try to lookup the identifier. */
2760 old_scope = parser->scope;
2761 parser->scope = scope;
2762 decl = cp_parser_lookup_name_simple (parser, id, location);
2763 parser->scope = old_scope;
2764 /* If the lookup found a template-name, it means that the user forgot
2765 to specify an argument list. Emit a useful error message. */
2766 if (TREE_CODE (decl) == TEMPLATE_DECL)
2768 "invalid use of template-name %qE without an argument list",
2770 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2771 error_at (location, "invalid use of destructor %qD as a type", id);
2772 else if (TREE_CODE (decl) == TYPE_DECL)
2773 /* Something like 'unsigned A a;' */
2774 error_at (location, "invalid combination of multiple type-specifiers");
2775 else if (!parser->scope)
2777 /* Issue an error message. */
2778 error_at (location, "%qE does not name a type", id);
2779 /* If we're in a template class, it's possible that the user was
2780 referring to a type from a base class. For example:
2782 template <typename T> struct A { typedef T X; };
2783 template <typename T> struct B : public A<T> { X x; };
2785 The user should have said "typename A<T>::X". */
2786 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2787 inform (location, "C++11 %<constexpr%> only available with "
2788 "-std=c++11 or -std=gnu++11");
2789 else if (processing_template_decl && current_class_type
2790 && TYPE_BINFO (current_class_type))
2794 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2798 tree base_type = BINFO_TYPE (b);
2799 if (CLASS_TYPE_P (base_type)
2800 && dependent_type_p (base_type))
2803 /* Go from a particular instantiation of the
2804 template (which will have an empty TYPE_FIELDs),
2805 to the main version. */
2806 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2807 for (field = TYPE_FIELDS (base_type);
2809 field = DECL_CHAIN (field))
2810 if (TREE_CODE (field) == TYPE_DECL
2811 && DECL_NAME (field) == id)
2814 "(perhaps %<typename %T::%E%> was intended)",
2815 BINFO_TYPE (b), id);
2824 /* Here we diagnose qualified-ids where the scope is actually correct,
2825 but the identifier does not resolve to a valid type name. */
2826 else if (parser->scope != error_mark_node)
2828 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2829 error_at (location, "%qE in namespace %qE does not name a type",
2831 else if (CLASS_TYPE_P (parser->scope)
2832 && constructor_name_p (id, parser->scope))
2835 error_at (location, "%<%T::%E%> names the constructor, not"
2836 " the type", parser->scope, id);
2837 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2838 error_at (location, "and %qT has no template constructors",
2841 else if (TYPE_P (parser->scope)
2842 && dependent_scope_p (parser->scope))
2843 error_at (location, "need %<typename%> before %<%T::%E%> because "
2844 "%qT is a dependent scope",
2845 parser->scope, id, parser->scope);
2846 else if (TYPE_P (parser->scope))
2847 error_at (location, "%qE in %q#T does not name a type",
2854 /* Check for a common situation where a type-name should be present,
2855 but is not, and issue a sensible error message. Returns true if an
2856 invalid type-name was detected.
2858 The situation handled by this function are variable declarations of the
2859 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2860 Usually, `ID' should name a type, but if we got here it means that it
2861 does not. We try to emit the best possible error message depending on
2862 how exactly the id-expression looks like. */
2865 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2868 cp_token *token = cp_lexer_peek_token (parser->lexer);
2870 /* Avoid duplicate error about ambiguous lookup. */
2871 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2873 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2874 if (next->type == CPP_NAME && next->ambiguous_p)
2878 cp_parser_parse_tentatively (parser);
2879 id = cp_parser_id_expression (parser,
2880 /*template_keyword_p=*/false,
2881 /*check_dependency_p=*/true,
2882 /*template_p=*/NULL,
2883 /*declarator_p=*/true,
2884 /*optional_p=*/false);
2885 /* If the next token is a (, this is a function with no explicit return
2886 type, i.e. constructor, destructor or conversion op. */
2887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2888 || TREE_CODE (id) == TYPE_DECL)
2890 cp_parser_abort_tentative_parse (parser);
2893 if (!cp_parser_parse_definitely (parser))
2896 /* Emit a diagnostic for the invalid type. */
2897 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2898 id, token->location);
2900 /* If we aren't in the middle of a declarator (i.e. in a
2901 parameter-declaration-clause), skip to the end of the declaration;
2902 there's no point in trying to process it. */
2903 if (!parser->in_declarator_p)
2904 cp_parser_skip_to_end_of_block_or_statement (parser);
2908 /* Consume tokens up to, and including, the next non-nested closing `)'.
2909 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2910 are doing error recovery. Returns -1 if OR_COMMA is true and we
2911 found an unnested comma. */
2914 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2919 unsigned paren_depth = 0;
2920 unsigned brace_depth = 0;
2921 unsigned square_depth = 0;
2923 if (recovering && !or_comma
2924 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2929 cp_token * token = cp_lexer_peek_token (parser->lexer);
2931 switch (token->type)
2934 case CPP_PRAGMA_EOL:
2935 /* If we've run out of tokens, then there is no closing `)'. */
2938 /* This is good for lambda expression capture-lists. */
2939 case CPP_OPEN_SQUARE:
2942 case CPP_CLOSE_SQUARE:
2943 if (!square_depth--)
2948 /* This matches the processing in skip_to_end_of_statement. */
2953 case CPP_OPEN_BRACE:
2956 case CPP_CLOSE_BRACE:
2962 if (recovering && or_comma && !brace_depth && !paren_depth
2967 case CPP_OPEN_PAREN:
2972 case CPP_CLOSE_PAREN:
2973 if (!brace_depth && !paren_depth--)
2976 cp_lexer_consume_token (parser->lexer);
2985 /* Consume the token. */
2986 cp_lexer_consume_token (parser->lexer);
2990 /* Consume tokens until we reach the end of the current statement.
2991 Normally, that will be just before consuming a `;'. However, if a
2992 non-nested `}' comes first, then we stop before consuming that. */
2995 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2997 unsigned nesting_depth = 0;
3001 cp_token *token = cp_lexer_peek_token (parser->lexer);
3003 switch (token->type)
3006 case CPP_PRAGMA_EOL:
3007 /* If we've run out of tokens, stop. */
3011 /* If the next token is a `;', we have reached the end of the
3017 case CPP_CLOSE_BRACE:
3018 /* If this is a non-nested '}', stop before consuming it.
3019 That way, when confronted with something like:
3023 we stop before consuming the closing '}', even though we
3024 have not yet reached a `;'. */
3025 if (nesting_depth == 0)
3028 /* If it is the closing '}' for a block that we have
3029 scanned, stop -- but only after consuming the token.
3035 we will stop after the body of the erroneously declared
3036 function, but before consuming the following `typedef'
3038 if (--nesting_depth == 0)
3040 cp_lexer_consume_token (parser->lexer);
3044 case CPP_OPEN_BRACE:
3052 /* Consume the token. */
3053 cp_lexer_consume_token (parser->lexer);
3057 /* This function is called at the end of a statement or declaration.
3058 If the next token is a semicolon, it is consumed; otherwise, error
3059 recovery is attempted. */
3062 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3064 /* Look for the trailing `;'. */
3065 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3067 /* If there is additional (erroneous) input, skip to the end of
3069 cp_parser_skip_to_end_of_statement (parser);
3070 /* If the next token is now a `;', consume it. */
3071 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3072 cp_lexer_consume_token (parser->lexer);
3076 /* Skip tokens until we have consumed an entire block, or until we
3077 have consumed a non-nested `;'. */
3080 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3082 int nesting_depth = 0;
3084 while (nesting_depth >= 0)
3086 cp_token *token = cp_lexer_peek_token (parser->lexer);
3088 switch (token->type)
3091 case CPP_PRAGMA_EOL:
3092 /* If we've run out of tokens, stop. */
3096 /* Stop if this is an unnested ';'. */
3101 case CPP_CLOSE_BRACE:
3102 /* Stop if this is an unnested '}', or closes the outermost
3105 if (nesting_depth < 0)
3111 case CPP_OPEN_BRACE:
3120 /* Consume the token. */
3121 cp_lexer_consume_token (parser->lexer);
3125 /* Skip tokens until a non-nested closing curly brace is the next
3126 token, or there are no more tokens. Return true in the first case,
3130 cp_parser_skip_to_closing_brace (cp_parser *parser)
3132 unsigned nesting_depth = 0;
3136 cp_token *token = cp_lexer_peek_token (parser->lexer);
3138 switch (token->type)
3141 case CPP_PRAGMA_EOL:
3142 /* If we've run out of tokens, stop. */
3145 case CPP_CLOSE_BRACE:
3146 /* If the next token is a non-nested `}', then we have reached
3147 the end of the current block. */
3148 if (nesting_depth-- == 0)
3152 case CPP_OPEN_BRACE:
3153 /* If it the next token is a `{', then we are entering a new
3154 block. Consume the entire block. */
3162 /* Consume the token. */
3163 cp_lexer_consume_token (parser->lexer);
3167 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3168 parameter is the PRAGMA token, allowing us to purge the entire pragma
3172 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3176 parser->lexer->in_pragma = false;
3179 token = cp_lexer_consume_token (parser->lexer);
3180 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3182 /* Ensure that the pragma is not parsed again. */
3183 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3186 /* Require pragma end of line, resyncing with it as necessary. The
3187 arguments are as for cp_parser_skip_to_pragma_eol. */
3190 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3192 parser->lexer->in_pragma = false;
3193 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3197 /* This is a simple wrapper around make_typename_type. When the id is
3198 an unresolved identifier node, we can provide a superior diagnostic
3199 using cp_parser_diagnose_invalid_type_name. */
3202 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3203 tree id, location_t id_location)
3206 if (TREE_CODE (id) == IDENTIFIER_NODE)
3208 result = make_typename_type (scope, id, typename_type,
3209 /*complain=*/tf_none);
3210 if (result == error_mark_node)
3211 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3214 return make_typename_type (scope, id, typename_type, tf_error);
3217 /* This is a wrapper around the
3218 make_{pointer,ptrmem,reference}_declarator functions that decides
3219 which one to call based on the CODE and CLASS_TYPE arguments. The
3220 CODE argument should be one of the values returned by
3221 cp_parser_ptr_operator. */
3222 static cp_declarator *
3223 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3224 cp_cv_quals cv_qualifiers,
3225 cp_declarator *target)
3227 if (code == ERROR_MARK)
3228 return cp_error_declarator;
3230 if (code == INDIRECT_REF)
3231 if (class_type == NULL_TREE)
3232 return make_pointer_declarator (cv_qualifiers, target);
3234 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3235 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3236 return make_reference_declarator (cv_qualifiers, target, false);
3237 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3238 return make_reference_declarator (cv_qualifiers, target, true);
3242 /* Create a new C++ parser. */
3245 cp_parser_new (void)
3251 /* cp_lexer_new_main is called before doing GC allocation because
3252 cp_lexer_new_main might load a PCH file. */
3253 lexer = cp_lexer_new_main ();
3255 /* Initialize the binops_by_token so that we can get the tree
3256 directly from the token. */
3257 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3258 binops_by_token[binops[i].token_type] = binops[i];
3260 parser = ggc_alloc_cleared_cp_parser ();
3261 parser->lexer = lexer;
3262 parser->context = cp_parser_context_new (NULL);
3264 /* For now, we always accept GNU extensions. */
3265 parser->allow_gnu_extensions_p = 1;
3267 /* The `>' token is a greater-than operator, not the end of a
3269 parser->greater_than_is_operator_p = true;
3271 parser->default_arg_ok_p = true;
3273 /* We are not parsing a constant-expression. */
3274 parser->integral_constant_expression_p = false;
3275 parser->allow_non_integral_constant_expression_p = false;
3276 parser->non_integral_constant_expression_p = false;
3278 /* Local variable names are not forbidden. */
3279 parser->local_variables_forbidden_p = false;
3281 /* We are not processing an `extern "C"' declaration. */
3282 parser->in_unbraced_linkage_specification_p = false;
3284 /* We are not processing a declarator. */
3285 parser->in_declarator_p = false;
3287 /* We are not processing a template-argument-list. */
3288 parser->in_template_argument_list_p = false;
3290 /* We are not in an iteration statement. */
3291 parser->in_statement = 0;
3293 /* We are not in a switch statement. */
3294 parser->in_switch_statement_p = false;
3296 /* We are not parsing a type-id inside an expression. */
3297 parser->in_type_id_in_expr_p = false;
3299 /* Declarations aren't implicitly extern "C". */
3300 parser->implicit_extern_c = false;
3302 /* String literals should be translated to the execution character set. */
3303 parser->translate_strings_p = true;
3305 /* We are not parsing a function body. */
3306 parser->in_function_body = false;
3308 /* We can correct until told otherwise. */
3309 parser->colon_corrects_to_scope_p = true;
3311 /* The unparsed function queue is empty. */
3312 push_unparsed_function_queues (parser);
3314 /* There are no classes being defined. */
3315 parser->num_classes_being_defined = 0;
3317 /* No template parameters apply. */
3318 parser->num_template_parameter_lists = 0;
3323 /* Create a cp_lexer structure which will emit the tokens in CACHE
3324 and push it onto the parser's lexer stack. This is used for delayed
3325 parsing of in-class method bodies and default arguments, and should
3326 not be confused with tentative parsing. */
3328 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3330 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3331 lexer->next = parser->lexer;
3332 parser->lexer = lexer;
3334 /* Move the current source position to that of the first token in the
3336 cp_lexer_set_source_position_from_token (lexer->next_token);
3339 /* Pop the top lexer off the parser stack. This is never used for the
3340 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3342 cp_parser_pop_lexer (cp_parser *parser)
3344 cp_lexer *lexer = parser->lexer;
3345 parser->lexer = lexer->next;
3346 cp_lexer_destroy (lexer);
3348 /* Put the current source position back where it was before this
3349 lexer was pushed. */
3350 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3353 /* Lexical conventions [gram.lex] */
3355 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3359 cp_parser_identifier (cp_parser* parser)
3363 /* Look for the identifier. */
3364 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3365 /* Return the value. */
3366 return token ? token->u.value : error_mark_node;
3369 /* Parse a sequence of adjacent string constants. Returns a
3370 TREE_STRING representing the combined, nul-terminated string
3371 constant. If TRANSLATE is true, translate the string to the
3372 execution character set. If WIDE_OK is true, a wide string is
3375 C++98 [lex.string] says that if a narrow string literal token is
3376 adjacent to a wide string literal token, the behavior is undefined.
3377 However, C99 6.4.5p4 says that this results in a wide string literal.
3378 We follow C99 here, for consistency with the C front end.
3380 This code is largely lifted from lex_string() in c-lex.c.
3382 FUTURE: ObjC++ will need to handle @-strings here. */
3384 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3388 struct obstack str_ob;
3389 cpp_string str, istr, *strs;
3391 enum cpp_ttype type, curr_type;
3392 int have_suffix_p = 0;
3394 tree suffix_id = NULL_TREE;
3395 bool curr_tok_is_userdef_p = false;
3397 tok = cp_lexer_peek_token (parser->lexer);
3398 if (!cp_parser_is_string_literal (tok))
3400 cp_parser_error (parser, "expected string-literal");
3401 return error_mark_node;
3404 if (cpp_userdef_string_p (tok->type))
3406 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3407 curr_type = cpp_userdef_string_remove_type (tok->type);
3408 curr_tok_is_userdef_p = true;
3412 string_tree = tok->u.value;
3413 curr_type = tok->type;
3417 /* Try to avoid the overhead of creating and destroying an obstack
3418 for the common case of just one string. */
3419 if (!cp_parser_is_string_literal
3420 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3422 cp_lexer_consume_token (parser->lexer);
3424 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3425 str.len = TREE_STRING_LENGTH (string_tree);
3428 if (curr_tok_is_userdef_p)
3430 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3432 curr_type = cpp_userdef_string_remove_type (tok->type);
3435 curr_type = tok->type;
3441 gcc_obstack_init (&str_ob);
3446 cp_lexer_consume_token (parser->lexer);
3448 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3449 str.len = TREE_STRING_LENGTH (string_tree);
3451 if (curr_tok_is_userdef_p)
3453 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3454 if (have_suffix_p == 0)
3456 suffix_id = curr_suffix_id;
3459 else if (have_suffix_p == 1
3460 && curr_suffix_id != suffix_id)
3462 error ("inconsistent user-defined literal suffixes"
3463 " %qD and %qD in string literal",
3464 suffix_id, curr_suffix_id);
3467 curr_type = cpp_userdef_string_remove_type (tok->type);
3470 curr_type = tok->type;
3472 if (type != curr_type)
3474 if (type == CPP_STRING)
3476 else if (curr_type != CPP_STRING)
3477 error_at (tok->location,
3478 "unsupported non-standard concatenation "
3479 "of string literals");
3482 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3484 tok = cp_lexer_peek_token (parser->lexer);
3485 if (cpp_userdef_string_p (tok->type))
3487 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3488 curr_type = cpp_userdef_string_remove_type (tok->type);
3489 curr_tok_is_userdef_p = true;
3493 string_tree = tok->u.value;
3494 curr_type = tok->type;
3495 curr_tok_is_userdef_p = false;
3498 while (cp_parser_is_string_literal (tok));
3500 strs = (cpp_string *) obstack_finish (&str_ob);
3503 if (type != CPP_STRING && !wide_ok)
3505 cp_parser_error (parser, "a wide string is invalid in this context");
3509 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3510 (parse_in, strs, count, &istr, type))
3512 value = build_string (istr.len, (const char *)istr.text);
3513 free (CONST_CAST (unsigned char *, istr.text));
3519 case CPP_UTF8STRING:
3520 TREE_TYPE (value) = char_array_type_node;
3523 TREE_TYPE (value) = char16_array_type_node;
3526 TREE_TYPE (value) = char32_array_type_node;
3529 TREE_TYPE (value) = wchar_array_type_node;
3533 value = fix_string_type (value);
3537 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3538 tok->u.value = literal;
3539 return cp_parser_userdef_string_literal (tok);
3543 /* cpp_interpret_string has issued an error. */
3544 value = error_mark_node;
3547 obstack_free (&str_ob, 0);
3552 /* Look up a literal operator with the name and the exact arguments. */
3555 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3558 decl = lookup_name (name);
3559 if (!decl || !is_overloaded_fn (decl))
3560 return error_mark_node;
3562 for (fns = decl; fns; fns = OVL_NEXT (fns))
3566 tree fn = OVL_CURRENT (fns);
3567 tree argtypes = NULL_TREE;
3568 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3569 if (argtypes != NULL_TREE)
3571 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3572 ++ix, argtypes = TREE_CHAIN (argtypes))
3574 tree targ = TREE_VALUE (argtypes);
3575 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3576 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3577 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3578 if ((ptr || arr || !same_type_p (targ, tparm))
3580 || !same_type_p (TREE_TYPE (targ),
3581 TREE_TYPE (tparm))))
3585 && ix == VEC_length (tree, args)
3586 /* May be this should be sufficient_parms_p instead,
3587 depending on how exactly should user-defined literals
3588 work in presence of default arguments on the literal
3589 operator parameters. */
3590 && argtypes == void_list_node)
3595 return error_mark_node;
3598 /* Parse a user-defined char constant. Returns a call to a user-defined
3599 literal operator taking the character as an argument. */
3602 cp_parser_userdef_char_literal (cp_parser *parser)
3604 cp_token *token = cp_lexer_consume_token (parser->lexer);
3605 tree literal = token->u.value;
3606 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3607 tree value = USERDEF_LITERAL_VALUE (literal);
3608 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3611 /* Build up a call to the user-defined operator */
3612 /* Lookup the name we got back from the id-expression. */
3613 VEC(tree,gc) *args = make_tree_vector ();
3614 VEC_safe_push (tree, gc, args, value);
3615 decl = lookup_literal_operator (name, args);
3616 if (!decl || decl == error_mark_node)
3618 error ("unable to find character literal operator %qD with %qT argument",
3619 name, TREE_TYPE (value));
3620 release_tree_vector (args);
3621 return error_mark_node;
3623 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3624 release_tree_vector (args);
3625 if (result != error_mark_node)
3628 error ("unable to find character literal operator %qD with %qT argument",
3629 name, TREE_TYPE (value));
3630 return error_mark_node;
3633 /* A subroutine of cp_parser_userdef_numeric_literal to
3634 create a char... template parameter pack from a string node. */
3637 make_char_string_pack (tree value)
3640 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3641 const char *str = TREE_STRING_POINTER (value);
3642 int i, len = TREE_STRING_LENGTH (value) - 1;
3643 tree argvec = make_tree_vec (1);
3645 /* Fill in CHARVEC with all of the parameters. */
3646 charvec = make_tree_vec (len);
3647 for (i = 0; i < len; ++i)
3648 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3650 /* Build the argument packs. */
3651 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3652 TREE_TYPE (argpack) = char_type_node;
3654 TREE_VEC_ELT (argvec, 0) = argpack;
3659 /* Parse a user-defined numeric constant. returns a call to a user-defined
3660 literal operator. */
3663 cp_parser_userdef_numeric_literal (cp_parser *parser)
3665 cp_token *token = cp_lexer_consume_token (parser->lexer);
3666 tree literal = token->u.value;
3667 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3668 tree value = USERDEF_LITERAL_VALUE (literal);
3669 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3670 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3674 /* Look for a literal operator taking the exact type of numeric argument
3675 as the literal value. */
3676 args = make_tree_vector ();
3677 VEC_safe_push (tree, gc, args, value);
3678 decl = lookup_literal_operator (name, args);
3679 if (decl && decl != error_mark_node)
3681 result = finish_call_expr (decl, &args, false, true, tf_none);
3682 if (result != error_mark_node)
3684 release_tree_vector (args);
3688 release_tree_vector (args);
3690 /* If the numeric argument didn't work, look for a raw literal
3691 operator taking a const char* argument consisting of the number
3692 in string format. */
3693 args = make_tree_vector ();
3694 VEC_safe_push (tree, gc, args, num_string);
3695 decl = lookup_literal_operator (name, args);
3696 if (decl && decl != error_mark_node)
3698 result = finish_call_expr (decl, &args, false, true, tf_none);
3699 if (result != error_mark_node)
3701 release_tree_vector (args);
3705 release_tree_vector (args);
3707 /* If the raw literal didn't work, look for a non-type template
3708 function with parameter pack char.... Call the function with
3709 template parameter characters representing the number. */
3710 args = make_tree_vector ();
3711 decl = lookup_literal_operator (name, args);
3712 if (decl && decl != error_mark_node)
3714 tree tmpl_args = make_char_string_pack (num_string);
3715 decl = lookup_template_function (decl, tmpl_args);
3716 result = finish_call_expr (decl, &args, false, true, tf_none);
3717 if (result != error_mark_node)
3719 release_tree_vector (args);
3723 release_tree_vector (args);
3725 error ("unable to find numeric literal operator %qD", name);
3726 return error_mark_node;
3729 /* Parse a user-defined string constant. Returns a call to a user-defined
3730 literal operator taking a character pointer and the length of the string
3734 cp_parser_userdef_string_literal (cp_token *token)
3736 tree literal = token->u.value;
3737 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3738 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3739 tree value = USERDEF_LITERAL_VALUE (literal);
3740 int len = TREE_STRING_LENGTH (value)
3741 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3744 /* Build up a call to the user-defined operator */
3745 /* Lookup the name we got back from the id-expression. */
3746 VEC(tree,gc) *args = make_tree_vector ();
3747 VEC_safe_push (tree, gc, args, value);
3748 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3749 decl = lookup_name (name);
3750 if (!decl || decl == error_mark_node)
3752 error ("unable to find string literal operator %qD", name);
3753 release_tree_vector (args);
3754 return error_mark_node;
3756 result = finish_call_expr (decl, &args, false, true, tf_none);
3757 release_tree_vector (args);
3758 if (result != error_mark_node)
3761 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3762 name, TREE_TYPE (value), size_type_node);
3763 return error_mark_node;
3767 /* Basic concepts [gram.basic] */
3769 /* Parse a translation-unit.
3772 declaration-seq [opt]
3774 Returns TRUE if all went well. */
3777 cp_parser_translation_unit (cp_parser* parser)
3779 /* The address of the first non-permanent object on the declarator
3781 static void *declarator_obstack_base;
3785 /* Create the declarator obstack, if necessary. */
3786 if (!cp_error_declarator)
3788 gcc_obstack_init (&declarator_obstack);
3789 /* Create the error declarator. */
3790 cp_error_declarator = make_declarator (cdk_error);
3791 /* Create the empty parameter list. */
3792 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3793 /* Remember where the base of the declarator obstack lies. */
3794 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3797 cp_parser_declaration_seq_opt (parser);
3799 /* If there are no tokens left then all went well. */
3800 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3802 /* Get rid of the token array; we don't need it any more. */
3803 cp_lexer_destroy (parser->lexer);
3804 parser->lexer = NULL;
3806 /* This file might have been a context that's implicitly extern
3807 "C". If so, pop the lang context. (Only relevant for PCH.) */
3808 if (parser->implicit_extern_c)
3810 pop_lang_context ();
3811 parser->implicit_extern_c = false;
3815 finish_translation_unit ();
3821 cp_parser_error (parser, "expected declaration");
3825 /* Make sure the declarator obstack was fully cleaned up. */
3826 gcc_assert (obstack_next_free (&declarator_obstack)
3827 == declarator_obstack_base);
3829 /* All went well. */
3833 /* Expressions [gram.expr] */
3835 /* Parse a primary-expression.
3846 ( compound-statement )
3847 __builtin_va_arg ( assignment-expression , type-id )
3848 __builtin_offsetof ( type-id , offsetof-expression )
3851 __has_nothrow_assign ( type-id )
3852 __has_nothrow_constructor ( type-id )
3853 __has_nothrow_copy ( type-id )
3854 __has_trivial_assign ( type-id )
3855 __has_trivial_constructor ( type-id )
3856 __has_trivial_copy ( type-id )
3857 __has_trivial_destructor ( type-id )
3858 __has_virtual_destructor ( type-id )
3859 __is_abstract ( type-id )
3860 __is_base_of ( type-id , type-id )
3861 __is_class ( type-id )
3862 __is_convertible_to ( type-id , type-id )
3863 __is_empty ( type-id )
3864 __is_enum ( type-id )
3865 __is_final ( type-id )
3866 __is_literal_type ( type-id )
3867 __is_pod ( type-id )
3868 __is_polymorphic ( type-id )
3869 __is_std_layout ( type-id )
3870 __is_trivial ( type-id )
3871 __is_union ( type-id )
3873 Objective-C++ Extension:
3881 ADDRESS_P is true iff this expression was immediately preceded by
3882 "&" and therefore might denote a pointer-to-member. CAST_P is true
3883 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3884 true iff this expression is a template argument.
3886 Returns a representation of the expression. Upon return, *IDK
3887 indicates what kind of id-expression (if any) was present. */
3890 cp_parser_primary_expression (cp_parser *parser,
3893 bool template_arg_p,
3896 cp_token *token = NULL;
3898 /* Assume the primary expression is not an id-expression. */
3899 *idk = CP_ID_KIND_NONE;
3901 /* Peek at the next token. */
3902 token = cp_lexer_peek_token (parser->lexer);
3903 switch (token->type)
3912 user-defined-literal */
3918 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3919 return cp_parser_userdef_numeric_literal (parser);
3920 token = cp_lexer_consume_token (parser->lexer);
3921 if (TREE_CODE (token->u.value) == FIXED_CST)
3923 error_at (token->location,
3924 "fixed-point types not supported in C++");
3925 return error_mark_node;
3927 /* Floating-point literals are only allowed in an integral
3928 constant expression if they are cast to an integral or
3929 enumeration type. */
3930 if (TREE_CODE (token->u.value) == REAL_CST
3931 && parser->integral_constant_expression_p
3934 /* CAST_P will be set even in invalid code like "int(2.7 +
3935 ...)". Therefore, we have to check that the next token
3936 is sure to end the cast. */
3939 cp_token *next_token;
3941 next_token = cp_lexer_peek_token (parser->lexer);
3942 if (/* The comma at the end of an
3943 enumerator-definition. */
3944 next_token->type != CPP_COMMA
3945 /* The curly brace at the end of an enum-specifier. */
3946 && next_token->type != CPP_CLOSE_BRACE
3947 /* The end of a statement. */
3948 && next_token->type != CPP_SEMICOLON
3949 /* The end of the cast-expression. */
3950 && next_token->type != CPP_CLOSE_PAREN
3951 /* The end of an array bound. */
3952 && next_token->type != CPP_CLOSE_SQUARE
3953 /* The closing ">" in a template-argument-list. */
3954 && (next_token->type != CPP_GREATER
3955 || parser->greater_than_is_operator_p)
3956 /* C++0x only: A ">>" treated like two ">" tokens,
3957 in a template-argument-list. */
3958 && (next_token->type != CPP_RSHIFT
3959 || (cxx_dialect == cxx98)
3960 || parser->greater_than_is_operator_p))
3964 /* If we are within a cast, then the constraint that the
3965 cast is to an integral or enumeration type will be
3966 checked at that point. If we are not within a cast, then
3967 this code is invalid. */
3969 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3971 return token->u.value;
3973 case CPP_CHAR_USERDEF:
3974 case CPP_CHAR16_USERDEF:
3975 case CPP_CHAR32_USERDEF:
3976 case CPP_WCHAR_USERDEF:
3977 return cp_parser_userdef_char_literal (parser);
3983 case CPP_UTF8STRING:
3984 case CPP_STRING_USERDEF:
3985 case CPP_STRING16_USERDEF:
3986 case CPP_STRING32_USERDEF:
3987 case CPP_WSTRING_USERDEF:
3988 case CPP_UTF8STRING_USERDEF:
3989 /* ??? Should wide strings be allowed when parser->translate_strings_p
3990 is false (i.e. in attributes)? If not, we can kill the third
3991 argument to cp_parser_string_literal. */
3992 return cp_parser_string_literal (parser,
3993 parser->translate_strings_p,
3996 case CPP_OPEN_PAREN:
3999 bool saved_greater_than_is_operator_p;
4001 /* Consume the `('. */
4002 cp_lexer_consume_token (parser->lexer);
4003 /* Within a parenthesized expression, a `>' token is always
4004 the greater-than operator. */
4005 saved_greater_than_is_operator_p
4006 = parser->greater_than_is_operator_p;
4007 parser->greater_than_is_operator_p = true;
4008 /* If we see `( { ' then we are looking at the beginning of
4009 a GNU statement-expression. */
4010 if (cp_parser_allow_gnu_extensions_p (parser)
4011 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4013 /* Statement-expressions are not allowed by the standard. */
4014 pedwarn (token->location, OPT_pedantic,
4015 "ISO C++ forbids braced-groups within expressions");
4017 /* And they're not allowed outside of a function-body; you
4018 cannot, for example, write:
4020 int i = ({ int j = 3; j + 1; });
4022 at class or namespace scope. */
4023 if (!parser->in_function_body
4024 || parser->in_template_argument_list_p)
4026 error_at (token->location,
4027 "statement-expressions are not allowed outside "
4028 "functions nor in template-argument lists");
4029 cp_parser_skip_to_end_of_block_or_statement (parser);
4030 expr = error_mark_node;
4034 /* Start the statement-expression. */
4035 expr = begin_stmt_expr ();
4036 /* Parse the compound-statement. */
4037 cp_parser_compound_statement (parser, expr, false, false);
4039 expr = finish_stmt_expr (expr, false);
4044 /* Parse the parenthesized expression. */
4045 expr = cp_parser_expression (parser, cast_p, idk);
4046 /* Let the front end know that this expression was
4047 enclosed in parentheses. This matters in case, for
4048 example, the expression is of the form `A::B', since
4049 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4051 finish_parenthesized_expr (expr);
4052 /* DR 705: Wrapping an unqualified name in parentheses
4053 suppresses arg-dependent lookup. We want to pass back
4054 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4055 (c++/37862), but none of the others. */
4056 if (*idk != CP_ID_KIND_QUALIFIED)
4057 *idk = CP_ID_KIND_NONE;
4059 /* The `>' token might be the end of a template-id or
4060 template-parameter-list now. */
4061 parser->greater_than_is_operator_p
4062 = saved_greater_than_is_operator_p;
4063 /* Consume the `)'. */
4064 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4065 cp_parser_skip_to_end_of_statement (parser);
4070 case CPP_OPEN_SQUARE:
4071 if (c_dialect_objc ())
4072 /* We have an Objective-C++ message. */
4073 return cp_parser_objc_expression (parser);
4075 tree lam = cp_parser_lambda_expression (parser);
4076 /* Don't warn about a failed tentative parse. */
4077 if (cp_parser_error_occurred (parser))
4078 return error_mark_node;
4079 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4083 case CPP_OBJC_STRING:
4084 if (c_dialect_objc ())
4085 /* We have an Objective-C++ string literal. */
4086 return cp_parser_objc_expression (parser);
4087 cp_parser_error (parser, "expected primary-expression");
4088 return error_mark_node;
4091 switch (token->keyword)
4093 /* These two are the boolean literals. */
4095 cp_lexer_consume_token (parser->lexer);
4096 return boolean_true_node;
4098 cp_lexer_consume_token (parser->lexer);
4099 return boolean_false_node;
4101 /* The `__null' literal. */
4103 cp_lexer_consume_token (parser->lexer);
4106 /* The `nullptr' literal. */
4108 cp_lexer_consume_token (parser->lexer);
4109 return nullptr_node;
4111 /* Recognize the `this' keyword. */
4113 cp_lexer_consume_token (parser->lexer);
4114 if (parser->local_variables_forbidden_p)
4116 error_at (token->location,
4117 "%<this%> may not be used in this context");
4118 return error_mark_node;
4120 /* Pointers cannot appear in constant-expressions. */
4121 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4122 return error_mark_node;
4123 return finish_this_expr ();
4125 /* The `operator' keyword can be the beginning of an
4130 case RID_FUNCTION_NAME:
4131 case RID_PRETTY_FUNCTION_NAME:
4132 case RID_C99_FUNCTION_NAME:
4134 non_integral_constant name;
4136 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4137 __func__ are the names of variables -- but they are
4138 treated specially. Therefore, they are handled here,
4139 rather than relying on the generic id-expression logic
4140 below. Grammatically, these names are id-expressions.
4142 Consume the token. */
4143 token = cp_lexer_consume_token (parser->lexer);
4145 switch (token->keyword)
4147 case RID_FUNCTION_NAME:
4148 name = NIC_FUNC_NAME;
4150 case RID_PRETTY_FUNCTION_NAME:
4151 name = NIC_PRETTY_FUNC;
4153 case RID_C99_FUNCTION_NAME:
4154 name = NIC_C99_FUNC;
4160 if (cp_parser_non_integral_constant_expression (parser, name))
4161 return error_mark_node;
4163 /* Look up the name. */
4164 return finish_fname (token->u.value);
4172 /* The `__builtin_va_arg' construct is used to handle
4173 `va_arg'. Consume the `__builtin_va_arg' token. */
4174 cp_lexer_consume_token (parser->lexer);
4175 /* Look for the opening `('. */
4176 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4177 /* Now, parse the assignment-expression. */
4178 expression = cp_parser_assignment_expression (parser,
4179 /*cast_p=*/false, NULL);
4180 /* Look for the `,'. */
4181 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4182 /* Parse the type-id. */
4183 type = cp_parser_type_id (parser);
4184 /* Look for the closing `)'. */
4185 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4186 /* Using `va_arg' in a constant-expression is not
4188 if (cp_parser_non_integral_constant_expression (parser,
4190 return error_mark_node;
4191 return build_x_va_arg (expression, type);
4195 return cp_parser_builtin_offsetof (parser);
4197 case RID_HAS_NOTHROW_ASSIGN:
4198 case RID_HAS_NOTHROW_CONSTRUCTOR:
4199 case RID_HAS_NOTHROW_COPY:
4200 case RID_HAS_TRIVIAL_ASSIGN:
4201 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4202 case RID_HAS_TRIVIAL_COPY:
4203 case RID_HAS_TRIVIAL_DESTRUCTOR:
4204 case RID_HAS_VIRTUAL_DESTRUCTOR:
4205 case RID_IS_ABSTRACT:
4206 case RID_IS_BASE_OF:
4208 case RID_IS_CONVERTIBLE_TO:
4212 case RID_IS_LITERAL_TYPE:
4214 case RID_IS_POLYMORPHIC:
4215 case RID_IS_STD_LAYOUT:
4216 case RID_IS_TRIVIAL:
4218 return cp_parser_trait_expr (parser, token->keyword);
4220 /* Objective-C++ expressions. */
4222 case RID_AT_PROTOCOL:
4223 case RID_AT_SELECTOR:
4224 return cp_parser_objc_expression (parser);
4227 if (parser->in_function_body
4228 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4231 error_at (token->location,
4232 "a template declaration cannot appear at block scope");
4233 cp_parser_skip_to_end_of_block_or_statement (parser);
4234 return error_mark_node;
4237 cp_parser_error (parser, "expected primary-expression");
4238 return error_mark_node;
4241 /* An id-expression can start with either an identifier, a
4242 `::' as the beginning of a qualified-id, or the "operator"
4246 case CPP_TEMPLATE_ID:
4247 case CPP_NESTED_NAME_SPECIFIER:
4251 const char *error_msg;
4254 cp_token *id_expr_token;
4257 /* Parse the id-expression. */
4259 = cp_parser_id_expression (parser,
4260 /*template_keyword_p=*/false,
4261 /*check_dependency_p=*/true,
4263 /*declarator_p=*/false,
4264 /*optional_p=*/false);
4265 if (id_expression == error_mark_node)
4266 return error_mark_node;
4267 id_expr_token = token;
4268 token = cp_lexer_peek_token (parser->lexer);
4269 done = (token->type != CPP_OPEN_SQUARE
4270 && token->type != CPP_OPEN_PAREN
4271 && token->type != CPP_DOT
4272 && token->type != CPP_DEREF
4273 && token->type != CPP_PLUS_PLUS
4274 && token->type != CPP_MINUS_MINUS);
4275 /* If we have a template-id, then no further lookup is
4276 required. If the template-id was for a template-class, we
4277 will sometimes have a TYPE_DECL at this point. */
4278 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4279 || TREE_CODE (id_expression) == TYPE_DECL)
4280 decl = id_expression;
4281 /* Look up the name. */
4284 tree ambiguous_decls;
4286 /* If we already know that this lookup is ambiguous, then
4287 we've already issued an error message; there's no reason
4289 if (id_expr_token->type == CPP_NAME
4290 && id_expr_token->ambiguous_p)
4292 cp_parser_simulate_error (parser);
4293 return error_mark_node;
4296 decl = cp_parser_lookup_name (parser, id_expression,
4299 /*is_namespace=*/false,
4300 /*check_dependency=*/true,
4302 id_expr_token->location);
4303 /* If the lookup was ambiguous, an error will already have
4305 if (ambiguous_decls)
4306 return error_mark_node;
4308 /* In Objective-C++, we may have an Objective-C 2.0
4309 dot-syntax for classes here. */
4310 if (c_dialect_objc ()
4311 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4312 && TREE_CODE (decl) == TYPE_DECL
4313 && objc_is_class_name (decl))
4316 cp_lexer_consume_token (parser->lexer);
4317 component = cp_parser_identifier (parser);
4318 if (component == error_mark_node)
4319 return error_mark_node;
4321 return objc_build_class_component_ref (id_expression, component);
4324 /* In Objective-C++, an instance variable (ivar) may be preferred
4325 to whatever cp_parser_lookup_name() found. */
4326 decl = objc_lookup_ivar (decl, id_expression);
4328 /* If name lookup gives us a SCOPE_REF, then the
4329 qualifying scope was dependent. */
4330 if (TREE_CODE (decl) == SCOPE_REF)
4332 /* At this point, we do not know if DECL is a valid
4333 integral constant expression. We assume that it is
4334 in fact such an expression, so that code like:
4336 template <int N> struct A {
4340 is accepted. At template-instantiation time, we
4341 will check that B<N>::i is actually a constant. */
4344 /* Check to see if DECL is a local variable in a context
4345 where that is forbidden. */
4346 if (parser->local_variables_forbidden_p
4347 && local_variable_p (decl))
4349 /* It might be that we only found DECL because we are
4350 trying to be generous with pre-ISO scoping rules.
4351 For example, consider:
4355 for (int i = 0; i < 10; ++i) {}
4356 extern void f(int j = i);
4359 Here, name look up will originally find the out
4360 of scope `i'. We need to issue a warning message,
4361 but then use the global `i'. */
4362 decl = check_for_out_of_scope_variable (decl);
4363 if (local_variable_p (decl))
4365 error_at (id_expr_token->location,
4366 "local variable %qD may not appear in this context",
4368 return error_mark_node;
4373 decl = (finish_id_expression
4374 (id_expression, decl, parser->scope,
4376 parser->integral_constant_expression_p,
4377 parser->allow_non_integral_constant_expression_p,
4378 &parser->non_integral_constant_expression_p,
4379 template_p, done, address_p,
4382 id_expr_token->location));
4384 cp_parser_error (parser, error_msg);
4388 /* Anything else is an error. */
4390 cp_parser_error (parser, "expected primary-expression");
4391 return error_mark_node;
4395 /* Parse an id-expression.
4402 :: [opt] nested-name-specifier template [opt] unqualified-id
4404 :: operator-function-id
4407 Return a representation of the unqualified portion of the
4408 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4409 a `::' or nested-name-specifier.
4411 Often, if the id-expression was a qualified-id, the caller will
4412 want to make a SCOPE_REF to represent the qualified-id. This
4413 function does not do this in order to avoid wastefully creating
4414 SCOPE_REFs when they are not required.
4416 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4419 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4420 uninstantiated templates.
4422 If *TEMPLATE_P is non-NULL, it is set to true iff the
4423 `template' keyword is used to explicitly indicate that the entity
4424 named is a template.
4426 If DECLARATOR_P is true, the id-expression is appearing as part of
4427 a declarator, rather than as part of an expression. */
4430 cp_parser_id_expression (cp_parser *parser,
4431 bool template_keyword_p,
4432 bool check_dependency_p,
4437 bool global_scope_p;
4438 bool nested_name_specifier_p;
4440 /* Assume the `template' keyword was not used. */
4442 *template_p = template_keyword_p;
4444 /* Look for the optional `::' operator. */
4446 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4448 /* Look for the optional nested-name-specifier. */
4449 nested_name_specifier_p
4450 = (cp_parser_nested_name_specifier_opt (parser,
4451 /*typename_keyword_p=*/false,
4456 /* If there is a nested-name-specifier, then we are looking at
4457 the first qualified-id production. */
4458 if (nested_name_specifier_p)
4461 tree saved_object_scope;
4462 tree saved_qualifying_scope;
4463 tree unqualified_id;
4466 /* See if the next token is the `template' keyword. */
4468 template_p = &is_template;
4469 *template_p = cp_parser_optional_template_keyword (parser);
4470 /* Name lookup we do during the processing of the
4471 unqualified-id might obliterate SCOPE. */
4472 saved_scope = parser->scope;
4473 saved_object_scope = parser->object_scope;
4474 saved_qualifying_scope = parser->qualifying_scope;
4475 /* Process the final unqualified-id. */
4476 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4479 /*optional_p=*/false);
4480 /* Restore the SAVED_SCOPE for our caller. */
4481 parser->scope = saved_scope;
4482 parser->object_scope = saved_object_scope;
4483 parser->qualifying_scope = saved_qualifying_scope;
4485 return unqualified_id;
4487 /* Otherwise, if we are in global scope, then we are looking at one
4488 of the other qualified-id productions. */
4489 else if (global_scope_p)
4494 /* Peek at the next token. */
4495 token = cp_lexer_peek_token (parser->lexer);
4497 /* If it's an identifier, and the next token is not a "<", then
4498 we can avoid the template-id case. This is an optimization
4499 for this common case. */
4500 if (token->type == CPP_NAME
4501 && !cp_parser_nth_token_starts_template_argument_list_p
4503 return cp_parser_identifier (parser);
4505 cp_parser_parse_tentatively (parser);
4506 /* Try a template-id. */
4507 id = cp_parser_template_id (parser,
4508 /*template_keyword_p=*/false,
4509 /*check_dependency_p=*/true,
4511 /* If that worked, we're done. */
4512 if (cp_parser_parse_definitely (parser))
4515 /* Peek at the next token. (Changes in the token buffer may
4516 have invalidated the pointer obtained above.) */
4517 token = cp_lexer_peek_token (parser->lexer);
4519 switch (token->type)
4522 return cp_parser_identifier (parser);
4525 if (token->keyword == RID_OPERATOR)
4526 return cp_parser_operator_function_id (parser);
4530 cp_parser_error (parser, "expected id-expression");
4531 return error_mark_node;
4535 return cp_parser_unqualified_id (parser, template_keyword_p,
4536 /*check_dependency_p=*/true,
4541 /* Parse an unqualified-id.
4545 operator-function-id
4546 conversion-function-id
4550 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4551 keyword, in a construct like `A::template ...'.
4553 Returns a representation of unqualified-id. For the `identifier'
4554 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4555 production a BIT_NOT_EXPR is returned; the operand of the
4556 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4557 other productions, see the documentation accompanying the
4558 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4559 names are looked up in uninstantiated templates. If DECLARATOR_P
4560 is true, the unqualified-id is appearing as part of a declarator,
4561 rather than as part of an expression. */
4564 cp_parser_unqualified_id (cp_parser* parser,
4565 bool template_keyword_p,
4566 bool check_dependency_p,
4572 /* Peek at the next token. */
4573 token = cp_lexer_peek_token (parser->lexer);
4575 switch (token->type)
4581 /* We don't know yet whether or not this will be a
4583 cp_parser_parse_tentatively (parser);
4584 /* Try a template-id. */
4585 id = cp_parser_template_id (parser, template_keyword_p,
4588 /* If it worked, we're done. */
4589 if (cp_parser_parse_definitely (parser))
4591 /* Otherwise, it's an ordinary identifier. */
4592 return cp_parser_identifier (parser);
4595 case CPP_TEMPLATE_ID:
4596 return cp_parser_template_id (parser, template_keyword_p,
4603 tree qualifying_scope;
4608 /* Consume the `~' token. */
4609 cp_lexer_consume_token (parser->lexer);
4610 /* Parse the class-name. The standard, as written, seems to
4613 template <typename T> struct S { ~S (); };
4614 template <typename T> S<T>::~S() {}
4616 is invalid, since `~' must be followed by a class-name, but
4617 `S<T>' is dependent, and so not known to be a class.
4618 That's not right; we need to look in uninstantiated
4619 templates. A further complication arises from:
4621 template <typename T> void f(T t) {
4625 Here, it is not possible to look up `T' in the scope of `T'
4626 itself. We must look in both the current scope, and the
4627 scope of the containing complete expression.
4629 Yet another issue is:
4638 The standard does not seem to say that the `S' in `~S'
4639 should refer to the type `S' and not the data member
4642 /* DR 244 says that we look up the name after the "~" in the
4643 same scope as we looked up the qualifying name. That idea
4644 isn't fully worked out; it's more complicated than that. */
4645 scope = parser->scope;
4646 object_scope = parser->object_scope;
4647 qualifying_scope = parser->qualifying_scope;
4649 /* Check for invalid scopes. */
4650 if (scope == error_mark_node)
4652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4653 cp_lexer_consume_token (parser->lexer);
4654 return error_mark_node;
4656 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4658 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4659 error_at (token->location,
4660 "scope %qT before %<~%> is not a class-name",
4662 cp_parser_simulate_error (parser);
4663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4664 cp_lexer_consume_token (parser->lexer);
4665 return error_mark_node;
4667 gcc_assert (!scope || TYPE_P (scope));
4669 /* If the name is of the form "X::~X" it's OK even if X is a
4671 token = cp_lexer_peek_token (parser->lexer);
4673 && token->type == CPP_NAME
4674 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4676 && (token->u.value == TYPE_IDENTIFIER (scope)
4677 || (CLASS_TYPE_P (scope)
4678 && constructor_name_p (token->u.value, scope))))
4680 cp_lexer_consume_token (parser->lexer);
4681 return build_nt (BIT_NOT_EXPR, scope);
4684 /* If there was an explicit qualification (S::~T), first look
4685 in the scope given by the qualification (i.e., S).
4687 Note: in the calls to cp_parser_class_name below we pass
4688 typename_type so that lookup finds the injected-class-name
4689 rather than the constructor. */
4691 type_decl = NULL_TREE;
4694 cp_parser_parse_tentatively (parser);
4695 type_decl = cp_parser_class_name (parser,
4696 /*typename_keyword_p=*/false,
4697 /*template_keyword_p=*/false,
4699 /*check_dependency=*/false,
4700 /*class_head_p=*/false,
4702 if (cp_parser_parse_definitely (parser))
4705 /* In "N::S::~S", look in "N" as well. */
4706 if (!done && scope && qualifying_scope)
4708 cp_parser_parse_tentatively (parser);
4709 parser->scope = qualifying_scope;
4710 parser->object_scope = NULL_TREE;
4711 parser->qualifying_scope = NULL_TREE;
4713 = cp_parser_class_name (parser,
4714 /*typename_keyword_p=*/false,
4715 /*template_keyword_p=*/false,
4717 /*check_dependency=*/false,
4718 /*class_head_p=*/false,
4720 if (cp_parser_parse_definitely (parser))
4723 /* In "p->S::~T", look in the scope given by "*p" as well. */
4724 else if (!done && object_scope)
4726 cp_parser_parse_tentatively (parser);
4727 parser->scope = object_scope;
4728 parser->object_scope = NULL_TREE;
4729 parser->qualifying_scope = NULL_TREE;
4731 = cp_parser_class_name (parser,
4732 /*typename_keyword_p=*/false,
4733 /*template_keyword_p=*/false,
4735 /*check_dependency=*/false,
4736 /*class_head_p=*/false,
4738 if (cp_parser_parse_definitely (parser))
4741 /* Look in the surrounding context. */
4744 parser->scope = NULL_TREE;
4745 parser->object_scope = NULL_TREE;
4746 parser->qualifying_scope = NULL_TREE;
4747 if (processing_template_decl)
4748 cp_parser_parse_tentatively (parser);
4750 = cp_parser_class_name (parser,
4751 /*typename_keyword_p=*/false,
4752 /*template_keyword_p=*/false,
4754 /*check_dependency=*/false,
4755 /*class_head_p=*/false,
4757 if (processing_template_decl
4758 && ! cp_parser_parse_definitely (parser))
4760 /* We couldn't find a type with this name, so just accept
4761 it and check for a match at instantiation time. */
4762 type_decl = cp_parser_identifier (parser);
4763 if (type_decl != error_mark_node)
4764 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4768 /* If an error occurred, assume that the name of the
4769 destructor is the same as the name of the qualifying
4770 class. That allows us to keep parsing after running
4771 into ill-formed destructor names. */
4772 if (type_decl == error_mark_node && scope)
4773 return build_nt (BIT_NOT_EXPR, scope);
4774 else if (type_decl == error_mark_node)
4775 return error_mark_node;
4777 /* Check that destructor name and scope match. */
4778 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4780 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4781 error_at (token->location,
4782 "declaration of %<~%T%> as member of %qT",
4784 cp_parser_simulate_error (parser);
4785 return error_mark_node;
4790 A typedef-name that names a class shall not be used as the
4791 identifier in the declarator for a destructor declaration. */
4793 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4794 && !DECL_SELF_REFERENCE_P (type_decl)
4795 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4796 error_at (token->location,
4797 "typedef-name %qD used as destructor declarator",
4800 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4804 if (token->keyword == RID_OPERATOR)
4808 /* This could be a template-id, so we try that first. */
4809 cp_parser_parse_tentatively (parser);
4810 /* Try a template-id. */
4811 id = cp_parser_template_id (parser, template_keyword_p,
4812 /*check_dependency_p=*/true,
4814 /* If that worked, we're done. */
4815 if (cp_parser_parse_definitely (parser))
4817 /* We still don't know whether we're looking at an
4818 operator-function-id or a conversion-function-id. */
4819 cp_parser_parse_tentatively (parser);
4820 /* Try an operator-function-id. */
4821 id = cp_parser_operator_function_id (parser);
4822 /* If that didn't work, try a conversion-function-id. */
4823 if (!cp_parser_parse_definitely (parser))
4824 id = cp_parser_conversion_function_id (parser);
4825 else if (UDLIT_OPER_P (id))
4828 const char *name = UDLIT_OP_SUFFIX (id);
4829 if (name[0] != '_' && !in_system_header)
4830 warning (0, "literal operator suffixes not preceded by %<_%>"
4831 " are reserved for future standardization");
4841 cp_parser_error (parser, "expected unqualified-id");
4842 return error_mark_node;
4846 /* Parse an (optional) nested-name-specifier.
4848 nested-name-specifier: [C++98]
4849 class-or-namespace-name :: nested-name-specifier [opt]
4850 class-or-namespace-name :: template nested-name-specifier [opt]
4852 nested-name-specifier: [C++0x]
4855 nested-name-specifier identifier ::
4856 nested-name-specifier template [opt] simple-template-id ::
4858 PARSER->SCOPE should be set appropriately before this function is
4859 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4860 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4863 Sets PARSER->SCOPE to the class (TYPE) or namespace
4864 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4865 it unchanged if there is no nested-name-specifier. Returns the new
4866 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4868 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4869 part of a declaration and/or decl-specifier. */
4872 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4873 bool typename_keyword_p,
4874 bool check_dependency_p,
4876 bool is_declaration)
4878 bool success = false;
4879 cp_token_position start = 0;
4882 /* Remember where the nested-name-specifier starts. */
4883 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4885 start = cp_lexer_token_position (parser->lexer, false);
4886 push_deferring_access_checks (dk_deferred);
4893 tree saved_qualifying_scope;
4894 bool template_keyword_p;
4896 /* Spot cases that cannot be the beginning of a
4897 nested-name-specifier. */
4898 token = cp_lexer_peek_token (parser->lexer);
4900 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4901 the already parsed nested-name-specifier. */
4902 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4904 /* Grab the nested-name-specifier and continue the loop. */
4905 cp_parser_pre_parsed_nested_name_specifier (parser);
4906 /* If we originally encountered this nested-name-specifier
4907 with IS_DECLARATION set to false, we will not have
4908 resolved TYPENAME_TYPEs, so we must do so here. */
4910 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4912 new_scope = resolve_typename_type (parser->scope,
4913 /*only_current_p=*/false);
4914 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4915 parser->scope = new_scope;
4921 /* Spot cases that cannot be the beginning of a
4922 nested-name-specifier. On the second and subsequent times
4923 through the loop, we look for the `template' keyword. */
4924 if (success && token->keyword == RID_TEMPLATE)
4926 /* A template-id can start a nested-name-specifier. */
4927 else if (token->type == CPP_TEMPLATE_ID)
4929 /* DR 743: decltype can be used in a nested-name-specifier. */
4930 else if (token_is_decltype (token))
4934 /* If the next token is not an identifier, then it is
4935 definitely not a type-name or namespace-name. */
4936 if (token->type != CPP_NAME)
4938 /* If the following token is neither a `<' (to begin a
4939 template-id), nor a `::', then we are not looking at a
4940 nested-name-specifier. */
4941 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4943 if (token->type == CPP_COLON
4944 && parser->colon_corrects_to_scope_p
4945 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4947 error_at (token->location,
4948 "found %<:%> in nested-name-specifier, expected %<::%>");
4949 token->type = CPP_SCOPE;
4952 if (token->type != CPP_SCOPE
4953 && !cp_parser_nth_token_starts_template_argument_list_p
4958 /* The nested-name-specifier is optional, so we parse
4960 cp_parser_parse_tentatively (parser);
4962 /* Look for the optional `template' keyword, if this isn't the
4963 first time through the loop. */
4965 template_keyword_p = cp_parser_optional_template_keyword (parser);
4967 template_keyword_p = false;
4969 /* Save the old scope since the name lookup we are about to do
4970 might destroy it. */
4971 old_scope = parser->scope;
4972 saved_qualifying_scope = parser->qualifying_scope;
4973 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4974 look up names in "X<T>::I" in order to determine that "Y" is
4975 a template. So, if we have a typename at this point, we make
4976 an effort to look through it. */
4978 && !typename_keyword_p
4980 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4981 parser->scope = resolve_typename_type (parser->scope,
4982 /*only_current_p=*/false);
4983 /* Parse the qualifying entity. */
4985 = cp_parser_qualifying_entity (parser,
4991 /* Look for the `::' token. */
4992 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4994 /* If we found what we wanted, we keep going; otherwise, we're
4996 if (!cp_parser_parse_definitely (parser))
4998 bool error_p = false;
5000 /* Restore the OLD_SCOPE since it was valid before the
5001 failed attempt at finding the last
5002 class-or-namespace-name. */
5003 parser->scope = old_scope;
5004 parser->qualifying_scope = saved_qualifying_scope;
5006 /* If the next token is a decltype, and the one after that is a
5007 `::', then the decltype has failed to resolve to a class or
5008 enumeration type. Give this error even when parsing
5009 tentatively since it can't possibly be valid--and we're going
5010 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5011 won't get another chance.*/
5012 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5013 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5016 token = cp_lexer_consume_token (parser->lexer);
5017 error_at (token->location, "decltype evaluates to %qT, "
5018 "which is not a class or enumeration type",
5020 parser->scope = error_mark_node;
5024 cp_lexer_consume_token (parser->lexer);
5027 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5029 /* If the next token is an identifier, and the one after
5030 that is a `::', then any valid interpretation would have
5031 found a class-or-namespace-name. */
5032 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5033 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5035 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5038 token = cp_lexer_consume_token (parser->lexer);
5041 if (!token->ambiguous_p)
5044 tree ambiguous_decls;
5046 decl = cp_parser_lookup_name (parser, token->u.value,
5048 /*is_template=*/false,
5049 /*is_namespace=*/false,
5050 /*check_dependency=*/true,
5053 if (TREE_CODE (decl) == TEMPLATE_DECL)
5054 error_at (token->location,
5055 "%qD used without template parameters",
5057 else if (ambiguous_decls)
5059 error_at (token->location,
5060 "reference to %qD is ambiguous",
5062 print_candidates (ambiguous_decls);
5063 decl = error_mark_node;
5067 if (cxx_dialect != cxx98)
5068 cp_parser_name_lookup_error
5069 (parser, token->u.value, decl, NLE_NOT_CXX98,
5072 cp_parser_name_lookup_error
5073 (parser, token->u.value, decl, NLE_CXX98,
5077 parser->scope = error_mark_node;
5079 /* Treat this as a successful nested-name-specifier
5084 If the name found is not a class-name (clause
5085 _class_) or namespace-name (_namespace.def_), the
5086 program is ill-formed. */
5089 cp_lexer_consume_token (parser->lexer);
5093 /* We've found one valid nested-name-specifier. */
5095 /* Name lookup always gives us a DECL. */
5096 if (TREE_CODE (new_scope) == TYPE_DECL)
5097 new_scope = TREE_TYPE (new_scope);
5098 /* Uses of "template" must be followed by actual templates. */
5099 if (template_keyword_p
5100 && !(CLASS_TYPE_P (new_scope)
5101 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5102 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5103 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5104 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5105 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5106 == TEMPLATE_ID_EXPR)))
5107 permerror (input_location, TYPE_P (new_scope)
5108 ? G_("%qT is not a template")
5109 : G_("%qD is not a template"),
5111 /* If it is a class scope, try to complete it; we are about to
5112 be looking up names inside the class. */
5113 if (TYPE_P (new_scope)
5114 /* Since checking types for dependency can be expensive,
5115 avoid doing it if the type is already complete. */
5116 && !COMPLETE_TYPE_P (new_scope)
5117 /* Do not try to complete dependent types. */
5118 && !dependent_type_p (new_scope))
5120 new_scope = complete_type (new_scope);
5121 /* If it is a typedef to current class, use the current
5122 class instead, as the typedef won't have any names inside
5124 if (!COMPLETE_TYPE_P (new_scope)
5125 && currently_open_class (new_scope))
5126 new_scope = TYPE_MAIN_VARIANT (new_scope);
5128 /* Make sure we look in the right scope the next time through
5130 parser->scope = new_scope;
5133 /* If parsing tentatively, replace the sequence of tokens that makes
5134 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5135 token. That way, should we re-parse the token stream, we will
5136 not have to repeat the effort required to do the parse, nor will
5137 we issue duplicate error messages. */
5138 if (success && start)
5142 token = cp_lexer_token_at (parser->lexer, start);
5143 /* Reset the contents of the START token. */
5144 token->type = CPP_NESTED_NAME_SPECIFIER;
5145 /* Retrieve any deferred checks. Do not pop this access checks yet
5146 so the memory will not be reclaimed during token replacing below. */
5147 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5148 token->u.tree_check_value->value = parser->scope;
5149 token->u.tree_check_value->checks = get_deferred_access_checks ();
5150 token->u.tree_check_value->qualifying_scope =
5151 parser->qualifying_scope;
5152 token->keyword = RID_MAX;
5154 /* Purge all subsequent tokens. */
5155 cp_lexer_purge_tokens_after (parser->lexer, start);
5159 pop_to_parent_deferring_access_checks ();
5161 return success ? parser->scope : NULL_TREE;
5164 /* Parse a nested-name-specifier. See
5165 cp_parser_nested_name_specifier_opt for details. This function
5166 behaves identically, except that it will an issue an error if no
5167 nested-name-specifier is present. */
5170 cp_parser_nested_name_specifier (cp_parser *parser,
5171 bool typename_keyword_p,
5172 bool check_dependency_p,
5174 bool is_declaration)
5178 /* Look for the nested-name-specifier. */
5179 scope = cp_parser_nested_name_specifier_opt (parser,
5184 /* If it was not present, issue an error message. */
5187 cp_parser_error (parser, "expected nested-name-specifier");
5188 parser->scope = NULL_TREE;
5194 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5195 this is either a class-name or a namespace-name (which corresponds
5196 to the class-or-namespace-name production in the grammar). For
5197 C++0x, it can also be a type-name that refers to an enumeration
5198 type or a simple-template-id.
5200 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5201 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5202 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5203 TYPE_P is TRUE iff the next name should be taken as a class-name,
5204 even the same name is declared to be another entity in the same
5207 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5208 specified by the class-or-namespace-name. If neither is found the
5209 ERROR_MARK_NODE is returned. */
5212 cp_parser_qualifying_entity (cp_parser *parser,
5213 bool typename_keyword_p,
5214 bool template_keyword_p,
5215 bool check_dependency_p,
5217 bool is_declaration)
5220 tree saved_qualifying_scope;
5221 tree saved_object_scope;
5224 bool successful_parse_p;
5226 /* DR 743: decltype can appear in a nested-name-specifier. */
5227 if (cp_lexer_next_token_is_decltype (parser->lexer))
5229 scope = cp_parser_decltype (parser);
5230 if (TREE_CODE (scope) != ENUMERAL_TYPE
5231 && !MAYBE_CLASS_TYPE_P (scope))
5233 cp_parser_simulate_error (parser);
5234 return error_mark_node;
5236 if (TYPE_NAME (scope))
5237 scope = TYPE_NAME (scope);
5241 /* Before we try to parse the class-name, we must save away the
5242 current PARSER->SCOPE since cp_parser_class_name will destroy
5244 saved_scope = parser->scope;
5245 saved_qualifying_scope = parser->qualifying_scope;
5246 saved_object_scope = parser->object_scope;
5247 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5248 there is no need to look for a namespace-name. */
5249 only_class_p = template_keyword_p
5250 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5252 cp_parser_parse_tentatively (parser);
5253 scope = cp_parser_class_name (parser,
5256 type_p ? class_type : none_type,
5258 /*class_head_p=*/false,
5260 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5261 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5263 && cxx_dialect != cxx98
5264 && !successful_parse_p)
5266 /* Restore the saved scope. */
5267 parser->scope = saved_scope;
5268 parser->qualifying_scope = saved_qualifying_scope;
5269 parser->object_scope = saved_object_scope;
5271 /* Parse tentatively. */
5272 cp_parser_parse_tentatively (parser);
5274 /* Parse a type-name */
5275 scope = cp_parser_type_name (parser);
5277 /* "If the name found does not designate a namespace or a class,
5278 enumeration, or dependent type, the program is ill-formed."
5280 We cover classes and dependent types above and namespaces below,
5281 so this code is only looking for enums. */
5282 if (!scope || TREE_CODE (scope) != TYPE_DECL
5283 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5284 cp_parser_simulate_error (parser);
5286 successful_parse_p = cp_parser_parse_definitely (parser);
5288 /* If that didn't work, try for a namespace-name. */
5289 if (!only_class_p && !successful_parse_p)
5291 /* Restore the saved scope. */
5292 parser->scope = saved_scope;
5293 parser->qualifying_scope = saved_qualifying_scope;
5294 parser->object_scope = saved_object_scope;
5295 /* If we are not looking at an identifier followed by the scope
5296 resolution operator, then this is not part of a
5297 nested-name-specifier. (Note that this function is only used
5298 to parse the components of a nested-name-specifier.) */
5299 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5300 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5301 return error_mark_node;
5302 scope = cp_parser_namespace_name (parser);
5308 /* Parse a postfix-expression.
5312 postfix-expression [ expression ]
5313 postfix-expression ( expression-list [opt] )
5314 simple-type-specifier ( expression-list [opt] )
5315 typename :: [opt] nested-name-specifier identifier
5316 ( expression-list [opt] )
5317 typename :: [opt] nested-name-specifier template [opt] template-id
5318 ( expression-list [opt] )
5319 postfix-expression . template [opt] id-expression
5320 postfix-expression -> template [opt] id-expression
5321 postfix-expression . pseudo-destructor-name
5322 postfix-expression -> pseudo-destructor-name
5323 postfix-expression ++
5324 postfix-expression --
5325 dynamic_cast < type-id > ( expression )
5326 static_cast < type-id > ( expression )
5327 reinterpret_cast < type-id > ( expression )
5328 const_cast < type-id > ( expression )
5329 typeid ( expression )
5335 ( type-id ) { initializer-list , [opt] }
5337 This extension is a GNU version of the C99 compound-literal
5338 construct. (The C99 grammar uses `type-name' instead of `type-id',
5339 but they are essentially the same concept.)
5341 If ADDRESS_P is true, the postfix expression is the operand of the
5342 `&' operator. CAST_P is true if this expression is the target of a
5345 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5346 class member access expressions [expr.ref].
5348 Returns a representation of the expression. */
5351 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5352 bool member_access_only_p,
5353 cp_id_kind * pidk_return)
5357 cp_id_kind idk = CP_ID_KIND_NONE;
5358 tree postfix_expression = NULL_TREE;
5359 bool is_member_access = false;
5361 /* Peek at the next token. */
5362 token = cp_lexer_peek_token (parser->lexer);
5363 /* Some of the productions are determined by keywords. */
5364 keyword = token->keyword;
5374 const char *saved_message;
5376 /* All of these can be handled in the same way from the point
5377 of view of parsing. Begin by consuming the token
5378 identifying the cast. */
5379 cp_lexer_consume_token (parser->lexer);
5381 /* New types cannot be defined in the cast. */
5382 saved_message = parser->type_definition_forbidden_message;
5383 parser->type_definition_forbidden_message
5384 = G_("types may not be defined in casts");
5386 /* Look for the opening `<'. */
5387 cp_parser_require (parser, CPP_LESS, RT_LESS);
5388 /* Parse the type to which we are casting. */
5389 type = cp_parser_type_id (parser);
5390 /* Look for the closing `>'. */
5391 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5392 /* Restore the old message. */
5393 parser->type_definition_forbidden_message = saved_message;
5395 /* And the expression which is being cast. */
5396 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5397 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5398 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5400 /* Only type conversions to integral or enumeration types
5401 can be used in constant-expressions. */
5402 if (!cast_valid_in_integral_constant_expression_p (type)
5403 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5404 return error_mark_node;
5410 = build_dynamic_cast (type, expression, tf_warning_or_error);
5414 = build_static_cast (type, expression, tf_warning_or_error);
5418 = build_reinterpret_cast (type, expression,
5419 tf_warning_or_error);
5423 = build_const_cast (type, expression, tf_warning_or_error);
5434 const char *saved_message;
5435 bool saved_in_type_id_in_expr_p;
5437 /* Consume the `typeid' token. */
5438 cp_lexer_consume_token (parser->lexer);
5439 /* Look for the `(' token. */
5440 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5441 /* Types cannot be defined in a `typeid' expression. */
5442 saved_message = parser->type_definition_forbidden_message;
5443 parser->type_definition_forbidden_message
5444 = G_("types may not be defined in a %<typeid%> expression");
5445 /* We can't be sure yet whether we're looking at a type-id or an
5447 cp_parser_parse_tentatively (parser);
5448 /* Try a type-id first. */
5449 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5450 parser->in_type_id_in_expr_p = true;
5451 type = cp_parser_type_id (parser);
5452 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5453 /* Look for the `)' token. Otherwise, we can't be sure that
5454 we're not looking at an expression: consider `typeid (int
5455 (3))', for example. */
5456 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5457 /* If all went well, simply lookup the type-id. */
5458 if (cp_parser_parse_definitely (parser))
5459 postfix_expression = get_typeid (type);
5460 /* Otherwise, fall back to the expression variant. */
5465 /* Look for an expression. */
5466 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5467 /* Compute its typeid. */
5468 postfix_expression = build_typeid (expression);
5469 /* Look for the `)' token. */
5470 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5472 /* Restore the saved message. */
5473 parser->type_definition_forbidden_message = saved_message;
5474 /* `typeid' may not appear in an integral constant expression. */
5475 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5476 return error_mark_node;
5483 /* The syntax permitted here is the same permitted for an
5484 elaborated-type-specifier. */
5485 type = cp_parser_elaborated_type_specifier (parser,
5486 /*is_friend=*/false,
5487 /*is_declaration=*/false);
5488 postfix_expression = cp_parser_functional_cast (parser, type);
5496 /* If the next thing is a simple-type-specifier, we may be
5497 looking at a functional cast. We could also be looking at
5498 an id-expression. So, we try the functional cast, and if
5499 that doesn't work we fall back to the primary-expression. */
5500 cp_parser_parse_tentatively (parser);
5501 /* Look for the simple-type-specifier. */
5502 type = cp_parser_simple_type_specifier (parser,
5503 /*decl_specs=*/NULL,
5504 CP_PARSER_FLAGS_NONE);
5505 /* Parse the cast itself. */
5506 if (!cp_parser_error_occurred (parser))
5508 = cp_parser_functional_cast (parser, type);
5509 /* If that worked, we're done. */
5510 if (cp_parser_parse_definitely (parser))
5513 /* If the functional-cast didn't work out, try a
5514 compound-literal. */
5515 if (cp_parser_allow_gnu_extensions_p (parser)
5516 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5518 VEC(constructor_elt,gc) *initializer_list = NULL;
5519 bool saved_in_type_id_in_expr_p;
5521 cp_parser_parse_tentatively (parser);
5522 /* Consume the `('. */
5523 cp_lexer_consume_token (parser->lexer);
5524 /* Parse the type. */
5525 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5526 parser->in_type_id_in_expr_p = true;
5527 type = cp_parser_type_id (parser);
5528 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5529 /* Look for the `)'. */
5530 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5531 /* Look for the `{'. */
5532 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5533 /* If things aren't going well, there's no need to
5535 if (!cp_parser_error_occurred (parser))
5537 bool non_constant_p;
5538 /* Parse the initializer-list. */
5540 = cp_parser_initializer_list (parser, &non_constant_p);
5541 /* Allow a trailing `,'. */
5542 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5543 cp_lexer_consume_token (parser->lexer);
5544 /* Look for the final `}'. */
5545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5547 /* If that worked, we're definitely looking at a
5548 compound-literal expression. */
5549 if (cp_parser_parse_definitely (parser))
5551 /* Warn the user that a compound literal is not
5552 allowed in standard C++. */
5553 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5554 /* For simplicity, we disallow compound literals in
5555 constant-expressions. We could
5556 allow compound literals of integer type, whose
5557 initializer was a constant, in constant
5558 expressions. Permitting that usage, as a further
5559 extension, would not change the meaning of any
5560 currently accepted programs. (Of course, as
5561 compound literals are not part of ISO C++, the
5562 standard has nothing to say.) */
5563 if (cp_parser_non_integral_constant_expression (parser,
5566 postfix_expression = error_mark_node;
5569 /* Form the representation of the compound-literal. */
5571 = (finish_compound_literal
5572 (type, build_constructor (init_list_type_node,
5574 tf_warning_or_error));
5579 /* It must be a primary-expression. */
5581 = cp_parser_primary_expression (parser, address_p, cast_p,
5582 /*template_arg_p=*/false,
5588 /* Keep looping until the postfix-expression is complete. */
5591 if (idk == CP_ID_KIND_UNQUALIFIED
5592 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5593 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5594 /* It is not a Koenig lookup function call. */
5596 = unqualified_name_lookup_error (postfix_expression);
5598 /* Peek at the next token. */
5599 token = cp_lexer_peek_token (parser->lexer);
5601 switch (token->type)
5603 case CPP_OPEN_SQUARE:
5605 = cp_parser_postfix_open_square_expression (parser,
5608 idk = CP_ID_KIND_NONE;
5609 is_member_access = false;
5612 case CPP_OPEN_PAREN:
5613 /* postfix-expression ( expression-list [opt] ) */
5616 bool is_builtin_constant_p;
5617 bool saved_integral_constant_expression_p = false;
5618 bool saved_non_integral_constant_expression_p = false;
5621 is_member_access = false;
5623 is_builtin_constant_p
5624 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5625 if (is_builtin_constant_p)
5627 /* The whole point of __builtin_constant_p is to allow
5628 non-constant expressions to appear as arguments. */
5629 saved_integral_constant_expression_p
5630 = parser->integral_constant_expression_p;
5631 saved_non_integral_constant_expression_p
5632 = parser->non_integral_constant_expression_p;
5633 parser->integral_constant_expression_p = false;
5635 args = (cp_parser_parenthesized_expression_list
5637 /*cast_p=*/false, /*allow_expansion_p=*/true,
5638 /*non_constant_p=*/NULL));
5639 if (is_builtin_constant_p)
5641 parser->integral_constant_expression_p
5642 = saved_integral_constant_expression_p;
5643 parser->non_integral_constant_expression_p
5644 = saved_non_integral_constant_expression_p;
5649 postfix_expression = error_mark_node;
5653 /* Function calls are not permitted in
5654 constant-expressions. */
5655 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5656 && cp_parser_non_integral_constant_expression (parser,
5659 postfix_expression = error_mark_node;
5660 release_tree_vector (args);
5665 if (idk == CP_ID_KIND_UNQUALIFIED
5666 || idk == CP_ID_KIND_TEMPLATE_ID)
5668 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5670 if (!VEC_empty (tree, args))
5673 if (!any_type_dependent_arguments_p (args))
5675 = perform_koenig_lookup (postfix_expression, args,
5676 /*include_std=*/false,
5677 tf_warning_or_error);
5681 = unqualified_fn_lookup_error (postfix_expression);
5683 /* We do not perform argument-dependent lookup if
5684 normal lookup finds a non-function, in accordance
5685 with the expected resolution of DR 218. */
5686 else if (!VEC_empty (tree, args)
5687 && is_overloaded_fn (postfix_expression))
5689 tree fn = get_first_fn (postfix_expression);
5690 fn = STRIP_TEMPLATE (fn);
5692 /* Do not do argument dependent lookup if regular
5693 lookup finds a member function or a block-scope
5694 function declaration. [basic.lookup.argdep]/3 */
5695 if (!DECL_FUNCTION_MEMBER_P (fn)
5696 && !DECL_LOCAL_FUNCTION_P (fn))
5699 if (!any_type_dependent_arguments_p (args))
5701 = perform_koenig_lookup (postfix_expression, args,
5702 /*include_std=*/false,
5703 tf_warning_or_error);
5708 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5710 tree instance = TREE_OPERAND (postfix_expression, 0);
5711 tree fn = TREE_OPERAND (postfix_expression, 1);
5713 if (processing_template_decl
5714 && (type_dependent_expression_p (instance)
5715 || (!BASELINK_P (fn)
5716 && TREE_CODE (fn) != FIELD_DECL)
5717 || type_dependent_expression_p (fn)
5718 || any_type_dependent_arguments_p (args)))
5721 = build_nt_call_vec (postfix_expression, args);
5722 release_tree_vector (args);
5726 if (BASELINK_P (fn))
5729 = (build_new_method_call
5730 (instance, fn, &args, NULL_TREE,
5731 (idk == CP_ID_KIND_QUALIFIED
5732 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5735 tf_warning_or_error));
5739 = finish_call_expr (postfix_expression, &args,
5740 /*disallow_virtual=*/false,
5742 tf_warning_or_error);
5744 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5745 || TREE_CODE (postfix_expression) == MEMBER_REF
5746 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5747 postfix_expression = (build_offset_ref_call_from_tree
5748 (postfix_expression, &args));
5749 else if (idk == CP_ID_KIND_QUALIFIED)
5750 /* A call to a static class member, or a namespace-scope
5753 = finish_call_expr (postfix_expression, &args,
5754 /*disallow_virtual=*/true,
5756 tf_warning_or_error);
5758 /* All other function calls. */
5760 = finish_call_expr (postfix_expression, &args,
5761 /*disallow_virtual=*/false,
5763 tf_warning_or_error);
5765 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5766 idk = CP_ID_KIND_NONE;
5768 release_tree_vector (args);
5774 /* postfix-expression . template [opt] id-expression
5775 postfix-expression . pseudo-destructor-name
5776 postfix-expression -> template [opt] id-expression
5777 postfix-expression -> pseudo-destructor-name */
5779 /* Consume the `.' or `->' operator. */
5780 cp_lexer_consume_token (parser->lexer);
5783 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5788 is_member_access = true;
5792 /* postfix-expression ++ */
5793 /* Consume the `++' token. */
5794 cp_lexer_consume_token (parser->lexer);
5795 /* Generate a representation for the complete expression. */
5797 = finish_increment_expr (postfix_expression,
5798 POSTINCREMENT_EXPR);
5799 /* Increments may not appear in constant-expressions. */
5800 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5801 postfix_expression = error_mark_node;
5802 idk = CP_ID_KIND_NONE;
5803 is_member_access = false;
5806 case CPP_MINUS_MINUS:
5807 /* postfix-expression -- */
5808 /* Consume the `--' token. */
5809 cp_lexer_consume_token (parser->lexer);
5810 /* Generate a representation for the complete expression. */
5812 = finish_increment_expr (postfix_expression,
5813 POSTDECREMENT_EXPR);
5814 /* Decrements may not appear in constant-expressions. */
5815 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5816 postfix_expression = error_mark_node;
5817 idk = CP_ID_KIND_NONE;
5818 is_member_access = false;
5822 if (pidk_return != NULL)
5823 * pidk_return = idk;
5824 if (member_access_only_p)
5825 return is_member_access? postfix_expression : error_mark_node;
5827 return postfix_expression;
5831 /* We should never get here. */
5833 return error_mark_node;
5836 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5837 by cp_parser_builtin_offsetof. We're looking for
5839 postfix-expression [ expression ]
5840 postfix-expression [ braced-init-list ] (C++11)
5842 FOR_OFFSETOF is set if we're being called in that context, which
5843 changes how we deal with integer constant expressions. */
5846 cp_parser_postfix_open_square_expression (cp_parser *parser,
5847 tree postfix_expression,
5852 /* Consume the `[' token. */
5853 cp_lexer_consume_token (parser->lexer);
5855 /* Parse the index expression. */
5856 /* ??? For offsetof, there is a question of what to allow here. If
5857 offsetof is not being used in an integral constant expression context,
5858 then we *could* get the right answer by computing the value at runtime.
5859 If we are in an integral constant expression context, then we might
5860 could accept any constant expression; hard to say without analysis.
5861 Rather than open the barn door too wide right away, allow only integer
5862 constant expressions here. */
5864 index = cp_parser_constant_expression (parser, false, NULL);
5867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5869 bool expr_nonconst_p;
5870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5871 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5874 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5877 /* Look for the closing `]'. */
5878 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5880 /* Build the ARRAY_REF. */
5881 postfix_expression = grok_array_decl (postfix_expression, index);
5883 /* When not doing offsetof, array references are not permitted in
5884 constant-expressions. */
5886 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5887 postfix_expression = error_mark_node;
5889 return postfix_expression;
5892 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5893 by cp_parser_builtin_offsetof. We're looking for
5895 postfix-expression . template [opt] id-expression
5896 postfix-expression . pseudo-destructor-name
5897 postfix-expression -> template [opt] id-expression
5898 postfix-expression -> pseudo-destructor-name
5900 FOR_OFFSETOF is set if we're being called in that context. That sorta
5901 limits what of the above we'll actually accept, but nevermind.
5902 TOKEN_TYPE is the "." or "->" token, which will already have been
5903 removed from the stream. */
5906 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5907 enum cpp_ttype token_type,
5908 tree postfix_expression,
5909 bool for_offsetof, cp_id_kind *idk,
5910 location_t location)
5914 bool pseudo_destructor_p;
5915 tree scope = NULL_TREE;
5917 /* If this is a `->' operator, dereference the pointer. */
5918 if (token_type == CPP_DEREF)
5919 postfix_expression = build_x_arrow (postfix_expression);
5920 /* Check to see whether or not the expression is type-dependent. */
5921 dependent_p = type_dependent_expression_p (postfix_expression);
5922 /* The identifier following the `->' or `.' is not qualified. */
5923 parser->scope = NULL_TREE;
5924 parser->qualifying_scope = NULL_TREE;
5925 parser->object_scope = NULL_TREE;
5926 *idk = CP_ID_KIND_NONE;
5928 /* Enter the scope corresponding to the type of the object
5929 given by the POSTFIX_EXPRESSION. */
5930 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5932 scope = TREE_TYPE (postfix_expression);
5933 /* According to the standard, no expression should ever have
5934 reference type. Unfortunately, we do not currently match
5935 the standard in this respect in that our internal representation
5936 of an expression may have reference type even when the standard
5937 says it does not. Therefore, we have to manually obtain the
5938 underlying type here. */
5939 scope = non_reference (scope);
5940 /* The type of the POSTFIX_EXPRESSION must be complete. */
5941 if (scope == unknown_type_node)
5943 error_at (location, "%qE does not have class type",
5944 postfix_expression);
5947 /* Unlike the object expression in other contexts, *this is not
5948 required to be of complete type for purposes of class member
5949 access (5.2.5) outside the member function body. */
5950 else if (scope != current_class_ref
5951 && !(processing_template_decl && scope == current_class_type))
5952 scope = complete_type_or_else (scope, NULL_TREE);
5953 /* Let the name lookup machinery know that we are processing a
5954 class member access expression. */
5955 parser->context->object_type = scope;
5956 /* If something went wrong, we want to be able to discern that case,
5957 as opposed to the case where there was no SCOPE due to the type
5958 of expression being dependent. */
5960 scope = error_mark_node;
5961 /* If the SCOPE was erroneous, make the various semantic analysis
5962 functions exit quickly -- and without issuing additional error
5964 if (scope == error_mark_node)
5965 postfix_expression = error_mark_node;
5968 /* Assume this expression is not a pseudo-destructor access. */
5969 pseudo_destructor_p = false;
5971 /* If the SCOPE is a scalar type, then, if this is a valid program,
5972 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5973 is type dependent, it can be pseudo-destructor-name or something else.
5974 Try to parse it as pseudo-destructor-name first. */
5975 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5980 cp_parser_parse_tentatively (parser);
5981 /* Parse the pseudo-destructor-name. */
5983 cp_parser_pseudo_destructor_name (parser, &s, &type);
5985 && (cp_parser_error_occurred (parser)
5986 || TREE_CODE (type) != TYPE_DECL
5987 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5988 cp_parser_abort_tentative_parse (parser);
5989 else if (cp_parser_parse_definitely (parser))
5991 pseudo_destructor_p = true;
5993 = finish_pseudo_destructor_expr (postfix_expression,
5994 s, TREE_TYPE (type));
5998 if (!pseudo_destructor_p)
6000 /* If the SCOPE is not a scalar type, we are looking at an
6001 ordinary class member access expression, rather than a
6002 pseudo-destructor-name. */
6004 cp_token *token = cp_lexer_peek_token (parser->lexer);
6005 /* Parse the id-expression. */
6006 name = (cp_parser_id_expression
6008 cp_parser_optional_template_keyword (parser),
6009 /*check_dependency_p=*/true,
6011 /*declarator_p=*/false,
6012 /*optional_p=*/false));
6013 /* In general, build a SCOPE_REF if the member name is qualified.
6014 However, if the name was not dependent and has already been
6015 resolved; there is no need to build the SCOPE_REF. For example;
6017 struct X { void f(); };
6018 template <typename T> void f(T* t) { t->X::f(); }
6020 Even though "t" is dependent, "X::f" is not and has been resolved
6021 to a BASELINK; there is no need to include scope information. */
6023 /* But we do need to remember that there was an explicit scope for
6024 virtual function calls. */
6026 *idk = CP_ID_KIND_QUALIFIED;
6028 /* If the name is a template-id that names a type, we will get a
6029 TYPE_DECL here. That is invalid code. */
6030 if (TREE_CODE (name) == TYPE_DECL)
6032 error_at (token->location, "invalid use of %qD", name);
6033 postfix_expression = error_mark_node;
6037 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6039 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6041 error_at (token->location, "%<%D::%D%> is not a class member",
6042 parser->scope, name);
6043 postfix_expression = error_mark_node;
6046 name = build_qualified_name (/*type=*/NULL_TREE,
6050 parser->scope = NULL_TREE;
6051 parser->qualifying_scope = NULL_TREE;
6052 parser->object_scope = NULL_TREE;
6054 if (parser->scope && name && BASELINK_P (name))
6055 adjust_result_of_qualified_name_lookup
6056 (name, parser->scope, scope);
6058 = finish_class_member_access_expr (postfix_expression, name,
6060 tf_warning_or_error);
6064 /* We no longer need to look up names in the scope of the object on
6065 the left-hand side of the `.' or `->' operator. */
6066 parser->context->object_type = NULL_TREE;
6068 /* Outside of offsetof, these operators may not appear in
6069 constant-expressions. */
6071 && (cp_parser_non_integral_constant_expression
6072 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6073 postfix_expression = error_mark_node;
6075 return postfix_expression;
6078 /* Parse a parenthesized expression-list.
6081 assignment-expression
6082 expression-list, assignment-expression
6087 identifier, expression-list
6089 CAST_P is true if this expression is the target of a cast.
6091 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6094 Returns a vector of trees. Each element is a representation of an
6095 assignment-expression. NULL is returned if the ( and or ) are
6096 missing. An empty, but allocated, vector is returned on no
6097 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6098 if we are parsing an attribute list for an attribute that wants a
6099 plain identifier argument, normal_attr for an attribute that wants
6100 an expression, or non_attr if we aren't parsing an attribute list. If
6101 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6102 not all of the expressions in the list were constant. */
6104 static VEC(tree,gc) *
6105 cp_parser_parenthesized_expression_list (cp_parser* parser,
6106 int is_attribute_list,
6108 bool allow_expansion_p,
6109 bool *non_constant_p)
6111 VEC(tree,gc) *expression_list;
6112 bool fold_expr_p = is_attribute_list != non_attr;
6113 tree identifier = NULL_TREE;
6114 bool saved_greater_than_is_operator_p;
6116 /* Assume all the expressions will be constant. */
6118 *non_constant_p = false;
6120 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6123 expression_list = make_tree_vector ();
6125 /* Within a parenthesized expression, a `>' token is always
6126 the greater-than operator. */
6127 saved_greater_than_is_operator_p
6128 = parser->greater_than_is_operator_p;
6129 parser->greater_than_is_operator_p = true;
6131 /* Consume expressions until there are no more. */
6132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6137 /* At the beginning of attribute lists, check to see if the
6138 next token is an identifier. */
6139 if (is_attribute_list == id_attr
6140 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6144 /* Consume the identifier. */
6145 token = cp_lexer_consume_token (parser->lexer);
6146 /* Save the identifier. */
6147 identifier = token->u.value;
6151 bool expr_non_constant_p;
6153 /* Parse the next assignment-expression. */
6154 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6156 /* A braced-init-list. */
6157 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6158 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6159 if (non_constant_p && expr_non_constant_p)
6160 *non_constant_p = true;
6162 else if (non_constant_p)
6164 expr = (cp_parser_constant_expression
6165 (parser, /*allow_non_constant_p=*/true,
6166 &expr_non_constant_p));
6167 if (expr_non_constant_p)
6168 *non_constant_p = true;
6171 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6174 expr = fold_non_dependent_expr (expr);
6176 /* If we have an ellipsis, then this is an expression
6178 if (allow_expansion_p
6179 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6181 /* Consume the `...'. */
6182 cp_lexer_consume_token (parser->lexer);
6184 /* Build the argument pack. */
6185 expr = make_pack_expansion (expr);
6188 /* Add it to the list. We add error_mark_node
6189 expressions to the list, so that we can still tell if
6190 the correct form for a parenthesized expression-list
6191 is found. That gives better errors. */
6192 VEC_safe_push (tree, gc, expression_list, expr);
6194 if (expr == error_mark_node)
6198 /* After the first item, attribute lists look the same as
6199 expression lists. */
6200 is_attribute_list = non_attr;
6203 /* If the next token isn't a `,', then we are done. */
6204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6207 /* Otherwise, consume the `,' and keep going. */
6208 cp_lexer_consume_token (parser->lexer);
6211 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6216 /* We try and resync to an unnested comma, as that will give the
6217 user better diagnostics. */
6218 ending = cp_parser_skip_to_closing_parenthesis (parser,
6219 /*recovering=*/true,
6221 /*consume_paren=*/true);
6226 parser->greater_than_is_operator_p
6227 = saved_greater_than_is_operator_p;
6232 parser->greater_than_is_operator_p
6233 = saved_greater_than_is_operator_p;
6236 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6238 return expression_list;
6241 /* Parse a pseudo-destructor-name.
6243 pseudo-destructor-name:
6244 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6245 :: [opt] nested-name-specifier template template-id :: ~ type-name
6246 :: [opt] nested-name-specifier [opt] ~ type-name
6248 If either of the first two productions is used, sets *SCOPE to the
6249 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6250 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6251 or ERROR_MARK_NODE if the parse fails. */
6254 cp_parser_pseudo_destructor_name (cp_parser* parser,
6258 bool nested_name_specifier_p;
6260 /* Assume that things will not work out. */
6261 *type = error_mark_node;
6263 /* Look for the optional `::' operator. */
6264 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6265 /* Look for the optional nested-name-specifier. */
6266 nested_name_specifier_p
6267 = (cp_parser_nested_name_specifier_opt (parser,
6268 /*typename_keyword_p=*/false,
6269 /*check_dependency_p=*/true,
6271 /*is_declaration=*/false)
6273 /* Now, if we saw a nested-name-specifier, we might be doing the
6274 second production. */
6275 if (nested_name_specifier_p
6276 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6278 /* Consume the `template' keyword. */
6279 cp_lexer_consume_token (parser->lexer);
6280 /* Parse the template-id. */
6281 cp_parser_template_id (parser,
6282 /*template_keyword_p=*/true,
6283 /*check_dependency_p=*/false,
6284 /*is_declaration=*/true);
6285 /* Look for the `::' token. */
6286 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6288 /* If the next token is not a `~', then there might be some
6289 additional qualification. */
6290 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6292 /* At this point, we're looking for "type-name :: ~". The type-name
6293 must not be a class-name, since this is a pseudo-destructor. So,
6294 it must be either an enum-name, or a typedef-name -- both of which
6295 are just identifiers. So, we peek ahead to check that the "::"
6296 and "~" tokens are present; if they are not, then we can avoid
6297 calling type_name. */
6298 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6299 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6300 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6302 cp_parser_error (parser, "non-scalar type");
6306 /* Look for the type-name. */
6307 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6308 if (*scope == error_mark_node)
6311 /* Look for the `::' token. */
6312 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6317 /* Look for the `~'. */
6318 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6320 /* Once we see the ~, this has to be a pseudo-destructor. */
6321 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6322 cp_parser_commit_to_tentative_parse (parser);
6324 /* Look for the type-name again. We are not responsible for
6325 checking that it matches the first type-name. */
6326 *type = cp_parser_nonclass_name (parser);
6329 /* Parse a unary-expression.
6335 unary-operator cast-expression
6336 sizeof unary-expression
6338 alignof ( type-id ) [C++0x]
6345 __extension__ cast-expression
6346 __alignof__ unary-expression
6347 __alignof__ ( type-id )
6348 alignof unary-expression [C++0x]
6349 __real__ cast-expression
6350 __imag__ cast-expression
6353 ADDRESS_P is true iff the unary-expression is appearing as the
6354 operand of the `&' operator. CAST_P is true if this expression is
6355 the target of a cast.
6357 Returns a representation of the expression. */
6360 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6364 enum tree_code unary_operator;
6366 /* Peek at the next token. */
6367 token = cp_lexer_peek_token (parser->lexer);
6368 /* Some keywords give away the kind of expression. */
6369 if (token->type == CPP_KEYWORD)
6371 enum rid keyword = token->keyword;
6381 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6382 /* Consume the token. */
6383 cp_lexer_consume_token (parser->lexer);
6384 /* Parse the operand. */
6385 operand = cp_parser_sizeof_operand (parser, keyword);
6387 if (TYPE_P (operand))
6388 return cxx_sizeof_or_alignof_type (operand, op, true);
6391 /* ISO C++ defines alignof only with types, not with
6392 expressions. So pedwarn if alignof is used with a non-
6393 type expression. However, __alignof__ is ok. */
6394 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6395 pedwarn (token->location, OPT_pedantic,
6396 "ISO C++ does not allow %<alignof%> "
6399 return cxx_sizeof_or_alignof_expr (operand, op, true);
6404 return cp_parser_new_expression (parser);
6407 return cp_parser_delete_expression (parser);
6411 /* The saved value of the PEDANTIC flag. */
6415 /* Save away the PEDANTIC flag. */
6416 cp_parser_extension_opt (parser, &saved_pedantic);
6417 /* Parse the cast-expression. */
6418 expr = cp_parser_simple_cast_expression (parser);
6419 /* Restore the PEDANTIC flag. */
6420 pedantic = saved_pedantic;
6430 /* Consume the `__real__' or `__imag__' token. */
6431 cp_lexer_consume_token (parser->lexer);
6432 /* Parse the cast-expression. */
6433 expression = cp_parser_simple_cast_expression (parser);
6434 /* Create the complete representation. */
6435 return build_x_unary_op ((keyword == RID_REALPART
6436 ? REALPART_EXPR : IMAGPART_EXPR),
6438 tf_warning_or_error);
6442 case RID_TRANSACTION_ATOMIC:
6443 case RID_TRANSACTION_RELAXED:
6444 return cp_parser_transaction_expression (parser, keyword);
6449 const char *saved_message;
6450 bool saved_integral_constant_expression_p;
6451 bool saved_non_integral_constant_expression_p;
6452 bool saved_greater_than_is_operator_p;
6454 cp_lexer_consume_token (parser->lexer);
6455 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6457 saved_message = parser->type_definition_forbidden_message;
6458 parser->type_definition_forbidden_message
6459 = G_("types may not be defined in %<noexcept%> expressions");
6461 saved_integral_constant_expression_p
6462 = parser->integral_constant_expression_p;
6463 saved_non_integral_constant_expression_p
6464 = parser->non_integral_constant_expression_p;
6465 parser->integral_constant_expression_p = false;
6467 saved_greater_than_is_operator_p
6468 = parser->greater_than_is_operator_p;
6469 parser->greater_than_is_operator_p = true;
6471 ++cp_unevaluated_operand;
6472 ++c_inhibit_evaluation_warnings;
6473 expr = cp_parser_expression (parser, false, NULL);
6474 --c_inhibit_evaluation_warnings;
6475 --cp_unevaluated_operand;
6477 parser->greater_than_is_operator_p
6478 = saved_greater_than_is_operator_p;
6480 parser->integral_constant_expression_p
6481 = saved_integral_constant_expression_p;
6482 parser->non_integral_constant_expression_p
6483 = saved_non_integral_constant_expression_p;
6485 parser->type_definition_forbidden_message = saved_message;
6487 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6488 return finish_noexcept_expr (expr, tf_warning_or_error);
6496 /* Look for the `:: new' and `:: delete', which also signal the
6497 beginning of a new-expression, or delete-expression,
6498 respectively. If the next token is `::', then it might be one of
6500 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6504 /* See if the token after the `::' is one of the keywords in
6505 which we're interested. */
6506 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6507 /* If it's `new', we have a new-expression. */
6508 if (keyword == RID_NEW)
6509 return cp_parser_new_expression (parser);
6510 /* Similarly, for `delete'. */
6511 else if (keyword == RID_DELETE)
6512 return cp_parser_delete_expression (parser);
6515 /* Look for a unary operator. */
6516 unary_operator = cp_parser_unary_operator (token);
6517 /* The `++' and `--' operators can be handled similarly, even though
6518 they are not technically unary-operators in the grammar. */
6519 if (unary_operator == ERROR_MARK)
6521 if (token->type == CPP_PLUS_PLUS)
6522 unary_operator = PREINCREMENT_EXPR;
6523 else if (token->type == CPP_MINUS_MINUS)
6524 unary_operator = PREDECREMENT_EXPR;
6525 /* Handle the GNU address-of-label extension. */
6526 else if (cp_parser_allow_gnu_extensions_p (parser)
6527 && token->type == CPP_AND_AND)
6531 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6533 /* Consume the '&&' token. */
6534 cp_lexer_consume_token (parser->lexer);
6535 /* Look for the identifier. */
6536 identifier = cp_parser_identifier (parser);
6537 /* Create an expression representing the address. */
6538 expression = finish_label_address_expr (identifier, loc);
6539 if (cp_parser_non_integral_constant_expression (parser,
6541 expression = error_mark_node;
6545 if (unary_operator != ERROR_MARK)
6547 tree cast_expression;
6548 tree expression = error_mark_node;
6549 non_integral_constant non_constant_p = NIC_NONE;
6551 /* Consume the operator token. */
6552 token = cp_lexer_consume_token (parser->lexer);
6553 /* Parse the cast-expression. */
6555 = cp_parser_cast_expression (parser,
6556 unary_operator == ADDR_EXPR,
6557 /*cast_p=*/false, pidk);
6558 /* Now, build an appropriate representation. */
6559 switch (unary_operator)
6562 non_constant_p = NIC_STAR;
6563 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6564 tf_warning_or_error);
6568 non_constant_p = NIC_ADDR;
6571 expression = build_x_unary_op (unary_operator, cast_expression,
6572 tf_warning_or_error);
6575 case PREINCREMENT_EXPR:
6576 case PREDECREMENT_EXPR:
6577 non_constant_p = unary_operator == PREINCREMENT_EXPR
6578 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6580 case UNARY_PLUS_EXPR:
6582 case TRUTH_NOT_EXPR:
6583 expression = finish_unary_op_expr (unary_operator, cast_expression);
6590 if (non_constant_p != NIC_NONE
6591 && cp_parser_non_integral_constant_expression (parser,
6593 expression = error_mark_node;
6598 return cp_parser_postfix_expression (parser, address_p, cast_p,
6599 /*member_access_only_p=*/false,
6603 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6604 unary-operator, the corresponding tree code is returned. */
6606 static enum tree_code
6607 cp_parser_unary_operator (cp_token* token)
6609 switch (token->type)
6612 return INDIRECT_REF;
6618 return UNARY_PLUS_EXPR;
6624 return TRUTH_NOT_EXPR;
6627 return BIT_NOT_EXPR;
6634 /* Parse a new-expression.
6637 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6638 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6640 Returns a representation of the expression. */
6643 cp_parser_new_expression (cp_parser* parser)
6645 bool global_scope_p;
6646 VEC(tree,gc) *placement;
6648 VEC(tree,gc) *initializer;
6652 /* Look for the optional `::' operator. */
6654 = (cp_parser_global_scope_opt (parser,
6655 /*current_scope_valid_p=*/false)
6657 /* Look for the `new' operator. */
6658 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6659 /* There's no easy way to tell a new-placement from the
6660 `( type-id )' construct. */
6661 cp_parser_parse_tentatively (parser);
6662 /* Look for a new-placement. */
6663 placement = cp_parser_new_placement (parser);
6664 /* If that didn't work out, there's no new-placement. */
6665 if (!cp_parser_parse_definitely (parser))
6667 if (placement != NULL)
6668 release_tree_vector (placement);
6672 /* If the next token is a `(', then we have a parenthesized
6674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6677 const char *saved_message = parser->type_definition_forbidden_message;
6679 /* Consume the `('. */
6680 cp_lexer_consume_token (parser->lexer);
6682 /* Parse the type-id. */
6683 parser->type_definition_forbidden_message
6684 = G_("types may not be defined in a new-expression");
6685 type = cp_parser_type_id (parser);
6686 parser->type_definition_forbidden_message = saved_message;
6688 /* Look for the closing `)'. */
6689 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6690 token = cp_lexer_peek_token (parser->lexer);
6691 /* There should not be a direct-new-declarator in this production,
6692 but GCC used to allowed this, so we check and emit a sensible error
6693 message for this case. */
6694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6696 error_at (token->location,
6697 "array bound forbidden after parenthesized type-id");
6698 inform (token->location,
6699 "try removing the parentheses around the type-id");
6700 cp_parser_direct_new_declarator (parser);
6704 /* Otherwise, there must be a new-type-id. */
6706 type = cp_parser_new_type_id (parser, &nelts);
6708 /* If the next token is a `(' or '{', then we have a new-initializer. */
6709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6710 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6711 initializer = cp_parser_new_initializer (parser);
6715 /* A new-expression may not appear in an integral constant
6717 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6718 ret = error_mark_node;
6721 /* Create a representation of the new-expression. */
6722 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6723 tf_warning_or_error);
6726 if (placement != NULL)
6727 release_tree_vector (placement);
6728 if (initializer != NULL)
6729 release_tree_vector (initializer);
6734 /* Parse a new-placement.
6739 Returns the same representation as for an expression-list. */
6741 static VEC(tree,gc) *
6742 cp_parser_new_placement (cp_parser* parser)
6744 VEC(tree,gc) *expression_list;
6746 /* Parse the expression-list. */
6747 expression_list = (cp_parser_parenthesized_expression_list
6748 (parser, non_attr, /*cast_p=*/false,
6749 /*allow_expansion_p=*/true,
6750 /*non_constant_p=*/NULL));
6752 return expression_list;
6755 /* Parse a new-type-id.
6758 type-specifier-seq new-declarator [opt]
6760 Returns the TYPE allocated. If the new-type-id indicates an array
6761 type, *NELTS is set to the number of elements in the last array
6762 bound; the TYPE will not include the last array bound. */
6765 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6767 cp_decl_specifier_seq type_specifier_seq;
6768 cp_declarator *new_declarator;
6769 cp_declarator *declarator;
6770 cp_declarator *outer_declarator;
6771 const char *saved_message;
6774 /* The type-specifier sequence must not contain type definitions.
6775 (It cannot contain declarations of new types either, but if they
6776 are not definitions we will catch that because they are not
6778 saved_message = parser->type_definition_forbidden_message;
6779 parser->type_definition_forbidden_message
6780 = G_("types may not be defined in a new-type-id");
6781 /* Parse the type-specifier-seq. */
6782 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6783 /*is_trailing_return=*/false,
6784 &type_specifier_seq);
6785 /* Restore the old message. */
6786 parser->type_definition_forbidden_message = saved_message;
6787 /* Parse the new-declarator. */
6788 new_declarator = cp_parser_new_declarator_opt (parser);
6790 /* Determine the number of elements in the last array dimension, if
6793 /* Skip down to the last array dimension. */
6794 declarator = new_declarator;
6795 outer_declarator = NULL;
6796 while (declarator && (declarator->kind == cdk_pointer
6797 || declarator->kind == cdk_ptrmem))
6799 outer_declarator = declarator;
6800 declarator = declarator->declarator;
6803 && declarator->kind == cdk_array
6804 && declarator->declarator
6805 && declarator->declarator->kind == cdk_array)
6807 outer_declarator = declarator;
6808 declarator = declarator->declarator;
6811 if (declarator && declarator->kind == cdk_array)
6813 *nelts = declarator->u.array.bounds;
6814 if (*nelts == error_mark_node)
6815 *nelts = integer_one_node;
6817 if (outer_declarator)
6818 outer_declarator->declarator = declarator->declarator;
6820 new_declarator = NULL;
6823 type = groktypename (&type_specifier_seq, new_declarator, false);
6827 /* Parse an (optional) new-declarator.
6830 ptr-operator new-declarator [opt]
6831 direct-new-declarator
6833 Returns the declarator. */
6835 static cp_declarator *
6836 cp_parser_new_declarator_opt (cp_parser* parser)
6838 enum tree_code code;
6840 cp_cv_quals cv_quals;
6842 /* We don't know if there's a ptr-operator next, or not. */
6843 cp_parser_parse_tentatively (parser);
6844 /* Look for a ptr-operator. */
6845 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6846 /* If that worked, look for more new-declarators. */
6847 if (cp_parser_parse_definitely (parser))
6849 cp_declarator *declarator;
6851 /* Parse another optional declarator. */
6852 declarator = cp_parser_new_declarator_opt (parser);
6854 return cp_parser_make_indirect_declarator
6855 (code, type, cv_quals, declarator);
6858 /* If the next token is a `[', there is a direct-new-declarator. */
6859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6860 return cp_parser_direct_new_declarator (parser);
6865 /* Parse a direct-new-declarator.
6867 direct-new-declarator:
6869 direct-new-declarator [constant-expression]
6873 static cp_declarator *
6874 cp_parser_direct_new_declarator (cp_parser* parser)
6876 cp_declarator *declarator = NULL;
6882 /* Look for the opening `['. */
6883 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6884 /* The first expression is not required to be constant. */
6887 cp_token *token = cp_lexer_peek_token (parser->lexer);
6888 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6889 /* The standard requires that the expression have integral
6890 type. DR 74 adds enumeration types. We believe that the
6891 real intent is that these expressions be handled like the
6892 expression in a `switch' condition, which also allows
6893 classes with a single conversion to integral or
6894 enumeration type. */
6895 if (!processing_template_decl)
6898 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6903 error_at (token->location,
6904 "expression in new-declarator must have integral "
6905 "or enumeration type");
6906 expression = error_mark_node;
6910 /* But all the other expressions must be. */
6913 = cp_parser_constant_expression (parser,
6914 /*allow_non_constant=*/false,
6916 /* Look for the closing `]'. */
6917 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6919 /* Add this bound to the declarator. */
6920 declarator = make_array_declarator (declarator, expression);
6922 /* If the next token is not a `[', then there are no more
6924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6931 /* Parse a new-initializer.
6934 ( expression-list [opt] )
6937 Returns a representation of the expression-list. */
6939 static VEC(tree,gc) *
6940 cp_parser_new_initializer (cp_parser* parser)
6942 VEC(tree,gc) *expression_list;
6944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6947 bool expr_non_constant_p;
6948 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6949 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6950 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6951 expression_list = make_tree_vector_single (t);
6954 expression_list = (cp_parser_parenthesized_expression_list
6955 (parser, non_attr, /*cast_p=*/false,
6956 /*allow_expansion_p=*/true,
6957 /*non_constant_p=*/NULL));
6959 return expression_list;
6962 /* Parse a delete-expression.
6965 :: [opt] delete cast-expression
6966 :: [opt] delete [ ] cast-expression
6968 Returns a representation of the expression. */
6971 cp_parser_delete_expression (cp_parser* parser)
6973 bool global_scope_p;
6977 /* Look for the optional `::' operator. */
6979 = (cp_parser_global_scope_opt (parser,
6980 /*current_scope_valid_p=*/false)
6982 /* Look for the `delete' keyword. */
6983 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6984 /* See if the array syntax is in use. */
6985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6987 /* Consume the `[' token. */
6988 cp_lexer_consume_token (parser->lexer);
6989 /* Look for the `]' token. */
6990 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6991 /* Remember that this is the `[]' construct. */
6997 /* Parse the cast-expression. */
6998 expression = cp_parser_simple_cast_expression (parser);
7000 /* A delete-expression may not appear in an integral constant
7002 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7003 return error_mark_node;
7005 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7006 tf_warning_or_error);
7009 /* Returns true if TOKEN may start a cast-expression and false
7013 cp_parser_token_starts_cast_expression (cp_token *token)
7015 switch (token->type)
7021 case CPP_CLOSE_SQUARE:
7022 case CPP_CLOSE_PAREN:
7023 case CPP_CLOSE_BRACE:
7027 case CPP_DEREF_STAR:
7035 case CPP_GREATER_EQ:
7055 /* '[' may start a primary-expression in obj-c++. */
7056 case CPP_OPEN_SQUARE:
7057 return c_dialect_objc ();
7064 /* Parse a cast-expression.
7068 ( type-id ) cast-expression
7070 ADDRESS_P is true iff the unary-expression is appearing as the
7071 operand of the `&' operator. CAST_P is true if this expression is
7072 the target of a cast.
7074 Returns a representation of the expression. */
7077 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7080 /* If it's a `(', then we might be looking at a cast. */
7081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7083 tree type = NULL_TREE;
7084 tree expr = NULL_TREE;
7085 bool compound_literal_p;
7086 const char *saved_message;
7088 /* There's no way to know yet whether or not this is a cast.
7089 For example, `(int (3))' is a unary-expression, while `(int)
7090 3' is a cast. So, we resort to parsing tentatively. */
7091 cp_parser_parse_tentatively (parser);
7092 /* Types may not be defined in a cast. */
7093 saved_message = parser->type_definition_forbidden_message;
7094 parser->type_definition_forbidden_message
7095 = G_("types may not be defined in casts");
7096 /* Consume the `('. */
7097 cp_lexer_consume_token (parser->lexer);
7098 /* A very tricky bit is that `(struct S) { 3 }' is a
7099 compound-literal (which we permit in C++ as an extension).
7100 But, that construct is not a cast-expression -- it is a
7101 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7102 is legal; if the compound-literal were a cast-expression,
7103 you'd need an extra set of parentheses.) But, if we parse
7104 the type-id, and it happens to be a class-specifier, then we
7105 will commit to the parse at that point, because we cannot
7106 undo the action that is done when creating a new class. So,
7107 then we cannot back up and do a postfix-expression.
7109 Therefore, we scan ahead to the closing `)', and check to see
7110 if the token after the `)' is a `{'. If so, we are not
7111 looking at a cast-expression.
7113 Save tokens so that we can put them back. */
7114 cp_lexer_save_tokens (parser->lexer);
7115 /* Skip tokens until the next token is a closing parenthesis.
7116 If we find the closing `)', and the next token is a `{', then
7117 we are looking at a compound-literal. */
7119 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7120 /*consume_paren=*/true)
7121 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7122 /* Roll back the tokens we skipped. */
7123 cp_lexer_rollback_tokens (parser->lexer);
7124 /* If we were looking at a compound-literal, simulate an error
7125 so that the call to cp_parser_parse_definitely below will
7127 if (compound_literal_p)
7128 cp_parser_simulate_error (parser);
7131 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7132 parser->in_type_id_in_expr_p = true;
7133 /* Look for the type-id. */
7134 type = cp_parser_type_id (parser);
7135 /* Look for the closing `)'. */
7136 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7137 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7140 /* Restore the saved message. */
7141 parser->type_definition_forbidden_message = saved_message;
7143 /* At this point this can only be either a cast or a
7144 parenthesized ctor such as `(T ())' that looks like a cast to
7145 function returning T. */
7146 if (!cp_parser_error_occurred (parser)
7147 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7150 cp_parser_parse_definitely (parser);
7151 expr = cp_parser_cast_expression (parser,
7152 /*address_p=*/false,
7153 /*cast_p=*/true, pidk);
7155 /* Warn about old-style casts, if so requested. */
7156 if (warn_old_style_cast
7157 && !in_system_header
7158 && !VOID_TYPE_P (type)
7159 && current_lang_name != lang_name_c)
7160 warning (OPT_Wold_style_cast, "use of old-style cast");
7162 /* Only type conversions to integral or enumeration types
7163 can be used in constant-expressions. */
7164 if (!cast_valid_in_integral_constant_expression_p (type)
7165 && cp_parser_non_integral_constant_expression (parser,
7167 return error_mark_node;
7169 /* Perform the cast. */
7170 expr = build_c_cast (input_location, type, expr);
7174 cp_parser_abort_tentative_parse (parser);
7177 /* If we get here, then it's not a cast, so it must be a
7178 unary-expression. */
7179 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7182 /* Parse a binary expression of the general form:
7186 pm-expression .* cast-expression
7187 pm-expression ->* cast-expression
7189 multiplicative-expression:
7191 multiplicative-expression * pm-expression
7192 multiplicative-expression / pm-expression
7193 multiplicative-expression % pm-expression
7195 additive-expression:
7196 multiplicative-expression
7197 additive-expression + multiplicative-expression
7198 additive-expression - multiplicative-expression
7202 shift-expression << additive-expression
7203 shift-expression >> additive-expression
7205 relational-expression:
7207 relational-expression < shift-expression
7208 relational-expression > shift-expression
7209 relational-expression <= shift-expression
7210 relational-expression >= shift-expression
7214 relational-expression:
7215 relational-expression <? shift-expression
7216 relational-expression >? shift-expression
7218 equality-expression:
7219 relational-expression
7220 equality-expression == relational-expression
7221 equality-expression != relational-expression
7225 and-expression & equality-expression
7227 exclusive-or-expression:
7229 exclusive-or-expression ^ and-expression
7231 inclusive-or-expression:
7232 exclusive-or-expression
7233 inclusive-or-expression | exclusive-or-expression
7235 logical-and-expression:
7236 inclusive-or-expression
7237 logical-and-expression && inclusive-or-expression
7239 logical-or-expression:
7240 logical-and-expression
7241 logical-or-expression || logical-and-expression
7243 All these are implemented with a single function like:
7246 simple-cast-expression
7247 binary-expression <token> binary-expression
7249 CAST_P is true if this expression is the target of a cast.
7251 The binops_by_token map is used to get the tree codes for each <token> type.
7252 binary-expressions are associated according to a precedence table. */
7254 #define TOKEN_PRECEDENCE(token) \
7255 (((token->type == CPP_GREATER \
7256 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7257 && !parser->greater_than_is_operator_p) \
7258 ? PREC_NOT_OPERATOR \
7259 : binops_by_token[token->type].prec)
7262 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7263 bool no_toplevel_fold_p,
7264 enum cp_parser_prec prec,
7267 cp_parser_expression_stack stack;
7268 cp_parser_expression_stack_entry *sp = &stack[0];
7271 enum tree_code tree_type, lhs_type, rhs_type;
7272 enum cp_parser_prec new_prec, lookahead_prec;
7275 /* Parse the first expression. */
7276 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7277 lhs_type = ERROR_MARK;
7281 /* Get an operator token. */
7282 token = cp_lexer_peek_token (parser->lexer);
7284 if (warn_cxx0x_compat
7285 && token->type == CPP_RSHIFT
7286 && !parser->greater_than_is_operator_p)
7288 if (warning_at (token->location, OPT_Wc__0x_compat,
7289 "%<>>%> operator is treated as"
7290 " two right angle brackets in C++11"))
7291 inform (token->location,
7292 "suggest parentheses around %<>>%> expression");
7295 new_prec = TOKEN_PRECEDENCE (token);
7297 /* Popping an entry off the stack means we completed a subexpression:
7298 - either we found a token which is not an operator (`>' where it is not
7299 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7300 will happen repeatedly;
7301 - or, we found an operator which has lower priority. This is the case
7302 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7304 if (new_prec <= prec)
7313 tree_type = binops_by_token[token->type].tree_type;
7315 /* We used the operator token. */
7316 cp_lexer_consume_token (parser->lexer);
7318 /* For "false && x" or "true || x", x will never be executed;
7319 disable warnings while evaluating it. */
7320 if (tree_type == TRUTH_ANDIF_EXPR)
7321 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7322 else if (tree_type == TRUTH_ORIF_EXPR)
7323 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7325 /* Extract another operand. It may be the RHS of this expression
7326 or the LHS of a new, higher priority expression. */
7327 rhs = cp_parser_simple_cast_expression (parser);
7328 rhs_type = ERROR_MARK;
7330 /* Get another operator token. Look up its precedence to avoid
7331 building a useless (immediately popped) stack entry for common
7332 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7333 token = cp_lexer_peek_token (parser->lexer);
7334 lookahead_prec = TOKEN_PRECEDENCE (token);
7335 if (lookahead_prec > new_prec)
7337 /* ... and prepare to parse the RHS of the new, higher priority
7338 expression. Since precedence levels on the stack are
7339 monotonically increasing, we do not have to care about
7342 sp->tree_type = tree_type;
7344 sp->lhs_type = lhs_type;
7347 lhs_type = rhs_type;
7349 new_prec = lookahead_prec;
7353 lookahead_prec = new_prec;
7354 /* If the stack is not empty, we have parsed into LHS the right side
7355 (`4' in the example above) of an expression we had suspended.
7356 We can use the information on the stack to recover the LHS (`3')
7357 from the stack together with the tree code (`MULT_EXPR'), and
7358 the precedence of the higher level subexpression
7359 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7360 which will be used to actually build the additive expression. */
7363 tree_type = sp->tree_type;
7365 rhs_type = lhs_type;
7367 lhs_type = sp->lhs_type;
7370 /* Undo the disabling of warnings done above. */
7371 if (tree_type == TRUTH_ANDIF_EXPR)
7372 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7373 else if (tree_type == TRUTH_ORIF_EXPR)
7374 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7377 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7378 ERROR_MARK for everything that is not a binary expression.
7379 This makes warn_about_parentheses miss some warnings that
7380 involve unary operators. For unary expressions we should
7381 pass the correct tree_code unless the unary expression was
7382 surrounded by parentheses.
7384 if (no_toplevel_fold_p
7385 && lookahead_prec <= prec
7387 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7388 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7390 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7391 &overload, tf_warning_or_error);
7392 lhs_type = tree_type;
7394 /* If the binary operator required the use of an overloaded operator,
7395 then this expression cannot be an integral constant-expression.
7396 An overloaded operator can be used even if both operands are
7397 otherwise permissible in an integral constant-expression if at
7398 least one of the operands is of enumeration type. */
7401 && cp_parser_non_integral_constant_expression (parser,
7403 return error_mark_node;
7410 /* Parse the `? expression : assignment-expression' part of a
7411 conditional-expression. The LOGICAL_OR_EXPR is the
7412 logical-or-expression that started the conditional-expression.
7413 Returns a representation of the entire conditional-expression.
7415 This routine is used by cp_parser_assignment_expression.
7417 ? expression : assignment-expression
7421 ? : assignment-expression */
7424 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7427 tree assignment_expr;
7428 struct cp_token *token;
7430 /* Consume the `?' token. */
7431 cp_lexer_consume_token (parser->lexer);
7432 token = cp_lexer_peek_token (parser->lexer);
7433 if (cp_parser_allow_gnu_extensions_p (parser)
7434 && token->type == CPP_COLON)
7436 pedwarn (token->location, OPT_pedantic,
7437 "ISO C++ does not allow ?: with omitted middle operand");
7438 /* Implicit true clause. */
7440 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7441 warn_for_omitted_condop (token->location, logical_or_expr);
7445 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7446 parser->colon_corrects_to_scope_p = false;
7447 /* Parse the expression. */
7448 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7449 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7450 c_inhibit_evaluation_warnings +=
7451 ((logical_or_expr == truthvalue_true_node)
7452 - (logical_or_expr == truthvalue_false_node));
7453 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7456 /* The next token should be a `:'. */
7457 cp_parser_require (parser, CPP_COLON, RT_COLON);
7458 /* Parse the assignment-expression. */
7459 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7460 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7462 /* Build the conditional-expression. */
7463 return build_x_conditional_expr (logical_or_expr,
7466 tf_warning_or_error);
7469 /* Parse an assignment-expression.
7471 assignment-expression:
7472 conditional-expression
7473 logical-or-expression assignment-operator assignment_expression
7476 CAST_P is true if this expression is the target of a cast.
7478 Returns a representation for the expression. */
7481 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7486 /* If the next token is the `throw' keyword, then we're looking at
7487 a throw-expression. */
7488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7489 expr = cp_parser_throw_expression (parser);
7490 /* Otherwise, it must be that we are looking at a
7491 logical-or-expression. */
7494 /* Parse the binary expressions (logical-or-expression). */
7495 expr = cp_parser_binary_expression (parser, cast_p, false,
7496 PREC_NOT_OPERATOR, pidk);
7497 /* If the next token is a `?' then we're actually looking at a
7498 conditional-expression. */
7499 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7500 return cp_parser_question_colon_clause (parser, expr);
7503 enum tree_code assignment_operator;
7505 /* If it's an assignment-operator, we're using the second
7508 = cp_parser_assignment_operator_opt (parser);
7509 if (assignment_operator != ERROR_MARK)
7511 bool non_constant_p;
7513 /* Parse the right-hand side of the assignment. */
7514 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7516 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7517 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7519 /* An assignment may not appear in a
7520 constant-expression. */
7521 if (cp_parser_non_integral_constant_expression (parser,
7523 return error_mark_node;
7524 /* Build the assignment expression. */
7525 expr = build_x_modify_expr (expr,
7526 assignment_operator,
7528 tf_warning_or_error);
7536 /* Parse an (optional) assignment-operator.
7538 assignment-operator: one of
7539 = *= /= %= += -= >>= <<= &= ^= |=
7543 assignment-operator: one of
7546 If the next token is an assignment operator, the corresponding tree
7547 code is returned, and the token is consumed. For example, for
7548 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7549 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7550 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7551 operator, ERROR_MARK is returned. */
7553 static enum tree_code
7554 cp_parser_assignment_operator_opt (cp_parser* parser)
7559 /* Peek at the next token. */
7560 token = cp_lexer_peek_token (parser->lexer);
7562 switch (token->type)
7573 op = TRUNC_DIV_EXPR;
7577 op = TRUNC_MOD_EXPR;
7609 /* Nothing else is an assignment operator. */
7613 /* If it was an assignment operator, consume it. */
7614 if (op != ERROR_MARK)
7615 cp_lexer_consume_token (parser->lexer);
7620 /* Parse an expression.
7623 assignment-expression
7624 expression , assignment-expression
7626 CAST_P is true if this expression is the target of a cast.
7628 Returns a representation of the expression. */
7631 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7633 tree expression = NULL_TREE;
7637 tree assignment_expression;
7639 /* Parse the next assignment-expression. */
7640 assignment_expression
7641 = cp_parser_assignment_expression (parser, cast_p, pidk);
7642 /* If this is the first assignment-expression, we can just
7645 expression = assignment_expression;
7647 expression = build_x_compound_expr (expression,
7648 assignment_expression,
7649 tf_warning_or_error);
7650 /* If the next token is not a comma, then we are done with the
7652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7654 /* Consume the `,'. */
7655 cp_lexer_consume_token (parser->lexer);
7656 /* A comma operator cannot appear in a constant-expression. */
7657 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7658 expression = error_mark_node;
7664 /* Parse a constant-expression.
7666 constant-expression:
7667 conditional-expression
7669 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7670 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7671 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7672 is false, NON_CONSTANT_P should be NULL. */
7675 cp_parser_constant_expression (cp_parser* parser,
7676 bool allow_non_constant_p,
7677 bool *non_constant_p)
7679 bool saved_integral_constant_expression_p;
7680 bool saved_allow_non_integral_constant_expression_p;
7681 bool saved_non_integral_constant_expression_p;
7684 /* It might seem that we could simply parse the
7685 conditional-expression, and then check to see if it were
7686 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7687 one that the compiler can figure out is constant, possibly after
7688 doing some simplifications or optimizations. The standard has a
7689 precise definition of constant-expression, and we must honor
7690 that, even though it is somewhat more restrictive.
7696 is not a legal declaration, because `(2, 3)' is not a
7697 constant-expression. The `,' operator is forbidden in a
7698 constant-expression. However, GCC's constant-folding machinery
7699 will fold this operation to an INTEGER_CST for `3'. */
7701 /* Save the old settings. */
7702 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7703 saved_allow_non_integral_constant_expression_p
7704 = parser->allow_non_integral_constant_expression_p;
7705 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7706 /* We are now parsing a constant-expression. */
7707 parser->integral_constant_expression_p = true;
7708 parser->allow_non_integral_constant_expression_p
7709 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7710 parser->non_integral_constant_expression_p = false;
7711 /* Although the grammar says "conditional-expression", we parse an
7712 "assignment-expression", which also permits "throw-expression"
7713 and the use of assignment operators. In the case that
7714 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7715 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7716 actually essential that we look for an assignment-expression.
7717 For example, cp_parser_initializer_clauses uses this function to
7718 determine whether a particular assignment-expression is in fact
7720 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7721 /* Restore the old settings. */
7722 parser->integral_constant_expression_p
7723 = saved_integral_constant_expression_p;
7724 parser->allow_non_integral_constant_expression_p
7725 = saved_allow_non_integral_constant_expression_p;
7726 if (cxx_dialect >= cxx0x)
7728 /* Require an rvalue constant expression here; that's what our
7729 callers expect. Reference constant expressions are handled
7730 separately in e.g. cp_parser_template_argument. */
7731 bool is_const = potential_rvalue_constant_expression (expression);
7732 parser->non_integral_constant_expression_p = !is_const;
7733 if (!is_const && !allow_non_constant_p)
7734 require_potential_rvalue_constant_expression (expression);
7736 if (allow_non_constant_p)
7737 *non_constant_p = parser->non_integral_constant_expression_p;
7738 parser->non_integral_constant_expression_p
7739 = saved_non_integral_constant_expression_p;
7744 /* Parse __builtin_offsetof.
7746 offsetof-expression:
7747 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7749 offsetof-member-designator:
7751 | offsetof-member-designator "." id-expression
7752 | offsetof-member-designator "[" expression "]"
7753 | offsetof-member-designator "->" id-expression */
7756 cp_parser_builtin_offsetof (cp_parser *parser)
7758 int save_ice_p, save_non_ice_p;
7763 /* We're about to accept non-integral-constant things, but will
7764 definitely yield an integral constant expression. Save and
7765 restore these values around our local parsing. */
7766 save_ice_p = parser->integral_constant_expression_p;
7767 save_non_ice_p = parser->non_integral_constant_expression_p;
7769 /* Consume the "__builtin_offsetof" token. */
7770 cp_lexer_consume_token (parser->lexer);
7771 /* Consume the opening `('. */
7772 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7773 /* Parse the type-id. */
7774 type = cp_parser_type_id (parser);
7775 /* Look for the `,'. */
7776 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7777 token = cp_lexer_peek_token (parser->lexer);
7779 /* Build the (type *)null that begins the traditional offsetof macro. */
7780 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7781 tf_warning_or_error);
7783 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7784 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7785 true, &dummy, token->location);
7788 token = cp_lexer_peek_token (parser->lexer);
7789 switch (token->type)
7791 case CPP_OPEN_SQUARE:
7792 /* offsetof-member-designator "[" expression "]" */
7793 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7797 /* offsetof-member-designator "->" identifier */
7798 expr = grok_array_decl (expr, integer_zero_node);
7802 /* offsetof-member-designator "." identifier */
7803 cp_lexer_consume_token (parser->lexer);
7804 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7809 case CPP_CLOSE_PAREN:
7810 /* Consume the ")" token. */
7811 cp_lexer_consume_token (parser->lexer);
7815 /* Error. We know the following require will fail, but
7816 that gives the proper error message. */
7817 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7818 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7819 expr = error_mark_node;
7825 /* If we're processing a template, we can't finish the semantics yet.
7826 Otherwise we can fold the entire expression now. */
7827 if (processing_template_decl)
7828 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7830 expr = finish_offsetof (expr);
7833 parser->integral_constant_expression_p = save_ice_p;
7834 parser->non_integral_constant_expression_p = save_non_ice_p;
7839 /* Parse a trait expression.
7841 Returns a representation of the expression, the underlying type
7842 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7845 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7848 tree type1, type2 = NULL_TREE;
7849 bool binary = false;
7850 cp_decl_specifier_seq decl_specs;
7854 case RID_HAS_NOTHROW_ASSIGN:
7855 kind = CPTK_HAS_NOTHROW_ASSIGN;
7857 case RID_HAS_NOTHROW_CONSTRUCTOR:
7858 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7860 case RID_HAS_NOTHROW_COPY:
7861 kind = CPTK_HAS_NOTHROW_COPY;
7863 case RID_HAS_TRIVIAL_ASSIGN:
7864 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7866 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7867 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7869 case RID_HAS_TRIVIAL_COPY:
7870 kind = CPTK_HAS_TRIVIAL_COPY;
7872 case RID_HAS_TRIVIAL_DESTRUCTOR:
7873 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7875 case RID_HAS_VIRTUAL_DESTRUCTOR:
7876 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7878 case RID_IS_ABSTRACT:
7879 kind = CPTK_IS_ABSTRACT;
7881 case RID_IS_BASE_OF:
7882 kind = CPTK_IS_BASE_OF;
7886 kind = CPTK_IS_CLASS;
7888 case RID_IS_CONVERTIBLE_TO:
7889 kind = CPTK_IS_CONVERTIBLE_TO;
7893 kind = CPTK_IS_EMPTY;
7896 kind = CPTK_IS_ENUM;
7899 kind = CPTK_IS_FINAL;
7901 case RID_IS_LITERAL_TYPE:
7902 kind = CPTK_IS_LITERAL_TYPE;
7907 case RID_IS_POLYMORPHIC:
7908 kind = CPTK_IS_POLYMORPHIC;
7910 case RID_IS_STD_LAYOUT:
7911 kind = CPTK_IS_STD_LAYOUT;
7913 case RID_IS_TRIVIAL:
7914 kind = CPTK_IS_TRIVIAL;
7917 kind = CPTK_IS_UNION;
7919 case RID_UNDERLYING_TYPE:
7920 kind = CPTK_UNDERLYING_TYPE;
7925 case RID_DIRECT_BASES:
7926 kind = CPTK_DIRECT_BASES;
7932 /* Consume the token. */
7933 cp_lexer_consume_token (parser->lexer);
7935 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7937 type1 = cp_parser_type_id (parser);
7939 if (type1 == error_mark_node)
7940 return error_mark_node;
7942 /* Build a trivial decl-specifier-seq. */
7943 clear_decl_specs (&decl_specs);
7944 decl_specs.type = type1;
7946 /* Call grokdeclarator to figure out what type this is. */
7947 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7948 /*initialized=*/0, /*attrlist=*/NULL);
7952 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7954 type2 = cp_parser_type_id (parser);
7956 if (type2 == error_mark_node)
7957 return error_mark_node;
7959 /* Build a trivial decl-specifier-seq. */
7960 clear_decl_specs (&decl_specs);
7961 decl_specs.type = type2;
7963 /* Call grokdeclarator to figure out what type this is. */
7964 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7965 /*initialized=*/0, /*attrlist=*/NULL);
7968 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7970 /* Complete the trait expression, which may mean either processing
7971 the trait expr now or saving it for template instantiation. */
7974 case CPTK_UNDERLYING_TYPE:
7975 return finish_underlying_type (type1);
7977 return finish_bases (type1, false);
7978 case CPTK_DIRECT_BASES:
7979 return finish_bases (type1, true);
7981 return finish_trait_expr (kind, type1, type2);
7985 /* Lambdas that appear in variable initializer or default argument scope
7986 get that in their mangling, so we need to record it. We might as well
7987 use the count for function and namespace scopes as well. */
7988 static GTY(()) tree lambda_scope;
7989 static GTY(()) int lambda_count;
7990 typedef struct GTY(()) tree_int
7995 DEF_VEC_O(tree_int);
7996 DEF_VEC_ALLOC_O(tree_int,gc);
7997 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8000 start_lambda_scope (tree decl)
8004 /* Once we're inside a function, we ignore other scopes and just push
8005 the function again so that popping works properly. */
8006 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8007 decl = current_function_decl;
8008 ti.t = lambda_scope;
8009 ti.i = lambda_count;
8010 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
8011 if (lambda_scope != decl)
8013 /* Don't reset the count if we're still in the same function. */
8014 lambda_scope = decl;
8020 record_lambda_scope (tree lambda)
8022 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8023 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8027 finish_lambda_scope (void)
8029 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8030 if (lambda_scope != p->t)
8032 lambda_scope = p->t;
8033 lambda_count = p->i;
8035 VEC_pop (tree_int, lambda_scope_stack);
8038 /* Parse a lambda expression.
8041 lambda-introducer lambda-declarator [opt] compound-statement
8043 Returns a representation of the expression. */
8046 cp_parser_lambda_expression (cp_parser* parser)
8048 tree lambda_expr = build_lambda_expr ();
8052 LAMBDA_EXPR_LOCATION (lambda_expr)
8053 = cp_lexer_peek_token (parser->lexer)->location;
8055 if (cp_unevaluated_operand)
8056 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8057 "lambda-expression in unevaluated context");
8059 /* We may be in the middle of deferred access check. Disable
8061 push_deferring_access_checks (dk_no_deferred);
8063 cp_parser_lambda_introducer (parser, lambda_expr);
8065 type = begin_lambda_type (lambda_expr);
8066 if (type == error_mark_node)
8067 return error_mark_node;
8069 record_lambda_scope (lambda_expr);
8071 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8072 determine_visibility (TYPE_NAME (type));
8074 /* Now that we've started the type, add the capture fields for any
8075 explicit captures. */
8076 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8079 /* Inside the class, surrounding template-parameter-lists do not apply. */
8080 unsigned int saved_num_template_parameter_lists
8081 = parser->num_template_parameter_lists;
8082 unsigned char in_statement = parser->in_statement;
8083 bool in_switch_statement_p = parser->in_switch_statement_p;
8085 parser->num_template_parameter_lists = 0;
8086 parser->in_statement = 0;
8087 parser->in_switch_statement_p = false;
8089 /* By virtue of defining a local class, a lambda expression has access to
8090 the private variables of enclosing classes. */
8092 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8095 cp_parser_lambda_body (parser, lambda_expr);
8096 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8097 cp_parser_skip_to_end_of_block_or_statement (parser);
8099 /* The capture list was built up in reverse order; fix that now. */
8101 tree newlist = NULL_TREE;
8104 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8107 next = TREE_CHAIN (elt);
8108 TREE_CHAIN (elt) = newlist;
8111 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8115 maybe_add_lambda_conv_op (type);
8117 type = finish_struct (type, /*attributes=*/NULL_TREE);
8119 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8120 parser->in_statement = in_statement;
8121 parser->in_switch_statement_p = in_switch_statement_p;
8124 pop_deferring_access_checks ();
8126 /* This field is only used during parsing of the lambda. */
8127 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8129 /* This lambda shouldn't have any proxies left at this point. */
8130 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8131 /* And now that we're done, push proxies for an enclosing lambda. */
8132 insert_pending_capture_proxies ();
8135 return build_lambda_object (lambda_expr);
8137 return error_mark_node;
8140 /* Parse the beginning of a lambda expression.
8143 [ lambda-capture [opt] ]
8145 LAMBDA_EXPR is the current representation of the lambda expression. */
8148 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8150 /* Need commas after the first capture. */
8153 /* Eat the leading `['. */
8154 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8156 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8157 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8158 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8159 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8160 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8161 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8163 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8165 cp_lexer_consume_token (parser->lexer);
8169 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8171 cp_token* capture_token;
8173 tree capture_init_expr;
8174 cp_id_kind idk = CP_ID_KIND_NONE;
8175 bool explicit_init_p = false;
8177 enum capture_kind_type
8182 enum capture_kind_type capture_kind = BY_COPY;
8184 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8186 error ("expected end of capture-list");
8193 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8195 /* Possibly capture `this'. */
8196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8198 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8199 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8200 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8201 "with by-copy capture default");
8202 cp_lexer_consume_token (parser->lexer);
8203 add_capture (lambda_expr,
8204 /*id=*/this_identifier,
8205 /*initializer=*/finish_this_expr(),
8206 /*by_reference_p=*/false,
8211 /* Remember whether we want to capture as a reference or not. */
8212 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8214 capture_kind = BY_REFERENCE;
8215 cp_lexer_consume_token (parser->lexer);
8218 /* Get the identifier. */
8219 capture_token = cp_lexer_peek_token (parser->lexer);
8220 capture_id = cp_parser_identifier (parser);
8222 if (capture_id == error_mark_node)
8223 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8224 delimiters, but I modified this to stop on unnested ']' as well. It
8225 was already changed to stop on unnested '}', so the
8226 "closing_parenthesis" name is no more misleading with my change. */
8228 cp_parser_skip_to_closing_parenthesis (parser,
8229 /*recovering=*/true,
8231 /*consume_paren=*/true);
8235 /* Find the initializer for this capture. */
8236 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8238 /* An explicit expression exists. */
8239 cp_lexer_consume_token (parser->lexer);
8240 pedwarn (input_location, OPT_pedantic,
8241 "ISO C++ does not allow initializers "
8242 "in lambda expression capture lists");
8243 capture_init_expr = cp_parser_assignment_expression (parser,
8246 explicit_init_p = true;
8250 const char* error_msg;
8252 /* Turn the identifier into an id-expression. */
8254 = cp_parser_lookup_name
8258 /*is_template=*/false,
8259 /*is_namespace=*/false,
8260 /*check_dependency=*/true,
8261 /*ambiguous_decls=*/NULL,
8262 capture_token->location);
8264 if (capture_init_expr == error_mark_node)
8266 unqualified_name_lookup_error (capture_id);
8269 else if (DECL_P (capture_init_expr)
8270 && (TREE_CODE (capture_init_expr) != VAR_DECL
8271 && TREE_CODE (capture_init_expr) != PARM_DECL))
8273 error_at (capture_token->location,
8274 "capture of non-variable %qD ",
8276 inform (0, "%q+#D declared here", capture_init_expr);
8279 if (TREE_CODE (capture_init_expr) == VAR_DECL
8280 && decl_storage_duration (capture_init_expr) != dk_auto)
8282 pedwarn (capture_token->location, 0, "capture of variable "
8283 "%qD with non-automatic storage duration",
8285 inform (0, "%q+#D declared here", capture_init_expr);
8290 = finish_id_expression
8295 /*integral_constant_expression_p=*/false,
8296 /*allow_non_integral_constant_expression_p=*/false,
8297 /*non_integral_constant_expression_p=*/NULL,
8298 /*template_p=*/false,
8300 /*address_p=*/false,
8301 /*template_arg_p=*/false,
8303 capture_token->location);
8306 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8307 && !explicit_init_p)
8309 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8310 && capture_kind == BY_COPY)
8311 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8312 "of %qD redundant with by-copy capture default",
8314 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8315 && capture_kind == BY_REFERENCE)
8316 pedwarn (capture_token->location, 0, "explicit by-reference "
8317 "capture of %qD redundant with by-reference capture "
8318 "default", capture_id);
8321 add_capture (lambda_expr,
8324 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8328 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8331 /* Parse the (optional) middle of a lambda expression.
8334 ( parameter-declaration-clause [opt] )
8335 attribute-specifier [opt]
8337 exception-specification [opt]
8338 lambda-return-type-clause [opt]
8340 LAMBDA_EXPR is the current representation of the lambda expression. */
8343 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8345 /* 5.1.1.4 of the standard says:
8346 If a lambda-expression does not include a lambda-declarator, it is as if
8347 the lambda-declarator were ().
8348 This means an empty parameter list, no attributes, and no exception
8350 tree param_list = void_list_node;
8351 tree attributes = NULL_TREE;
8352 tree exception_spec = NULL_TREE;
8355 /* The lambda-declarator is optional, but must begin with an opening
8356 parenthesis if present. */
8357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8359 cp_lexer_consume_token (parser->lexer);
8361 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8363 /* Parse parameters. */
8364 param_list = cp_parser_parameter_declaration_clause (parser);
8366 /* Default arguments shall not be specified in the
8367 parameter-declaration-clause of a lambda-declarator. */
8368 for (t = param_list; t; t = TREE_CHAIN (t))
8369 if (TREE_PURPOSE (t))
8370 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8371 "default argument specified for lambda parameter");
8373 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8375 attributes = cp_parser_attributes_opt (parser);
8377 /* Parse optional `mutable' keyword. */
8378 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8380 cp_lexer_consume_token (parser->lexer);
8381 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8384 /* Parse optional exception specification. */
8385 exception_spec = cp_parser_exception_specification_opt (parser);
8387 /* Parse optional trailing return type. */
8388 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8390 cp_lexer_consume_token (parser->lexer);
8391 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8394 /* The function parameters must be in scope all the way until after the
8395 trailing-return-type in case of decltype. */
8396 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8397 pop_binding (DECL_NAME (t), t);
8402 /* Create the function call operator.
8404 Messing with declarators like this is no uglier than building up the
8405 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8408 cp_decl_specifier_seq return_type_specs;
8409 cp_declarator* declarator;
8414 clear_decl_specs (&return_type_specs);
8415 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8416 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8418 /* Maybe we will deduce the return type later, but we can use void
8419 as a placeholder return type anyways. */
8420 return_type_specs.type = void_type_node;
8422 p = obstack_alloc (&declarator_obstack, 0);
8424 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8427 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8428 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8429 declarator = make_call_declarator (declarator, param_list, quals,
8430 VIRT_SPEC_UNSPECIFIED,
8432 /*late_return_type=*/NULL_TREE);
8433 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8435 fco = grokmethod (&return_type_specs,
8438 if (fco != error_mark_node)
8440 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8441 DECL_ARTIFICIAL (fco) = 1;
8442 /* Give the object parameter a different name. */
8443 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8446 finish_member_declaration (fco);
8448 obstack_free (&declarator_obstack, p);
8450 return (fco != error_mark_node);
8454 /* Parse the body of a lambda expression, which is simply
8458 but which requires special handling.
8459 LAMBDA_EXPR is the current representation of the lambda expression. */
8462 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8464 bool nested = (current_function_decl != NULL_TREE);
8465 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8467 push_function_context ();
8469 /* Still increment function_depth so that we don't GC in the
8470 middle of an expression. */
8472 /* Clear this in case we're in the middle of a default argument. */
8473 parser->local_variables_forbidden_p = false;
8475 /* Finish the function call operator
8477 + late_parsing_for_member
8478 + function_definition_after_declarator
8479 + ctor_initializer_opt_and_function_body */
8481 tree fco = lambda_function (lambda_expr);
8487 /* Let the front end know that we are going to be defining this
8489 start_preparsed_function (fco,
8491 SF_PRE_PARSED | SF_INCLASS_INLINE);
8493 start_lambda_scope (fco);
8494 body = begin_function_body ();
8496 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8499 /* Push the proxies for any explicit captures. */
8500 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8501 cap = TREE_CHAIN (cap))
8502 build_capture_proxy (TREE_PURPOSE (cap));
8504 compound_stmt = begin_compound_stmt (0);
8506 /* 5.1.1.4 of the standard says:
8507 If a lambda-expression does not include a trailing-return-type, it
8508 is as if the trailing-return-type denotes the following type:
8509 * if the compound-statement is of the form
8510 { return attribute-specifier [opt] expression ; }
8511 the type of the returned expression after lvalue-to-rvalue
8512 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8513 (_conv.array_ 4.2), and function-to-pointer conversion
8515 * otherwise, void. */
8517 /* In a lambda that has neither a lambda-return-type-clause
8518 nor a deducible form, errors should be reported for return statements
8519 in the body. Since we used void as the placeholder return type, parsing
8520 the body as usual will give such desired behavior. */
8521 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8522 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8523 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8525 tree expr = NULL_TREE;
8526 cp_id_kind idk = CP_ID_KIND_NONE;
8528 /* Parse tentatively in case there's more after the initial return
8530 cp_parser_parse_tentatively (parser);
8532 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8534 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8536 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8537 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8539 if (cp_parser_parse_definitely (parser))
8541 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8543 /* Will get error here if type not deduced yet. */
8544 finish_return_stmt (expr);
8552 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8553 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8554 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8555 cp_parser_label_declaration (parser);
8556 cp_parser_statement_seq_opt (parser, NULL_TREE);
8557 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8558 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8561 finish_compound_stmt (compound_stmt);
8564 finish_function_body (body);
8565 finish_lambda_scope ();
8567 /* Finish the function and generate code for it if necessary. */
8568 expand_or_defer_fn (finish_function (/*inline*/2));
8571 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8573 pop_function_context();
8578 /* Statements [gram.stmt.stmt] */
8580 /* Parse a statement.
8584 expression-statement
8589 declaration-statement
8597 IN_COMPOUND is true when the statement is nested inside a
8598 cp_parser_compound_statement; this matters for certain pragmas.
8600 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8601 is a (possibly labeled) if statement which is not enclosed in braces
8602 and has an else clause. This is used to implement -Wparentheses. */
8605 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8606 bool in_compound, bool *if_p)
8610 location_t statement_location;
8615 /* There is no statement yet. */
8616 statement = NULL_TREE;
8617 /* Peek at the next token. */
8618 token = cp_lexer_peek_token (parser->lexer);
8619 /* Remember the location of the first token in the statement. */
8620 statement_location = token->location;
8621 /* If this is a keyword, then that will often determine what kind of
8622 statement we have. */
8623 if (token->type == CPP_KEYWORD)
8625 enum rid keyword = token->keyword;
8631 /* Looks like a labeled-statement with a case label.
8632 Parse the label, and then use tail recursion to parse
8634 cp_parser_label_for_labeled_statement (parser);
8639 statement = cp_parser_selection_statement (parser, if_p);
8645 statement = cp_parser_iteration_statement (parser);
8652 statement = cp_parser_jump_statement (parser);
8655 /* Objective-C++ exception-handling constructs. */
8658 case RID_AT_FINALLY:
8659 case RID_AT_SYNCHRONIZED:
8661 statement = cp_parser_objc_statement (parser);
8665 statement = cp_parser_try_block (parser);
8669 /* This must be a namespace alias definition. */
8670 cp_parser_declaration_statement (parser);
8673 case RID_TRANSACTION_ATOMIC:
8674 case RID_TRANSACTION_RELAXED:
8675 statement = cp_parser_transaction (parser, keyword);
8677 case RID_TRANSACTION_CANCEL:
8678 statement = cp_parser_transaction_cancel (parser);
8682 /* It might be a keyword like `int' that can start a
8683 declaration-statement. */
8687 else if (token->type == CPP_NAME)
8689 /* If the next token is a `:', then we are looking at a
8690 labeled-statement. */
8691 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8692 if (token->type == CPP_COLON)
8694 /* Looks like a labeled-statement with an ordinary label.
8695 Parse the label, and then use tail recursion to parse
8697 cp_parser_label_for_labeled_statement (parser);
8701 /* Anything that starts with a `{' must be a compound-statement. */
8702 else if (token->type == CPP_OPEN_BRACE)
8703 statement = cp_parser_compound_statement (parser, NULL, false, false);
8704 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8705 a statement all its own. */
8706 else if (token->type == CPP_PRAGMA)
8708 /* Only certain OpenMP pragmas are attached to statements, and thus
8709 are considered statements themselves. All others are not. In
8710 the context of a compound, accept the pragma as a "statement" and
8711 return so that we can check for a close brace. Otherwise we
8712 require a real statement and must go back and read one. */
8714 cp_parser_pragma (parser, pragma_compound);
8715 else if (!cp_parser_pragma (parser, pragma_stmt))
8719 else if (token->type == CPP_EOF)
8721 cp_parser_error (parser, "expected statement");
8725 /* Everything else must be a declaration-statement or an
8726 expression-statement. Try for the declaration-statement
8727 first, unless we are looking at a `;', in which case we know that
8728 we have an expression-statement. */
8731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8733 cp_parser_parse_tentatively (parser);
8734 /* Try to parse the declaration-statement. */
8735 cp_parser_declaration_statement (parser);
8736 /* If that worked, we're done. */
8737 if (cp_parser_parse_definitely (parser))
8740 /* Look for an expression-statement instead. */
8741 statement = cp_parser_expression_statement (parser, in_statement_expr);
8744 /* Set the line number for the statement. */
8745 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8746 SET_EXPR_LOCATION (statement, statement_location);
8749 /* Parse the label for a labeled-statement, i.e.
8752 case constant-expression :
8756 case constant-expression ... constant-expression : statement
8758 When a label is parsed without errors, the label is added to the
8759 parse tree by the finish_* functions, so this function doesn't
8760 have to return the label. */
8763 cp_parser_label_for_labeled_statement (cp_parser* parser)
8766 tree label = NULL_TREE;
8767 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8769 /* The next token should be an identifier. */
8770 token = cp_lexer_peek_token (parser->lexer);
8771 if (token->type != CPP_NAME
8772 && token->type != CPP_KEYWORD)
8774 cp_parser_error (parser, "expected labeled-statement");
8778 parser->colon_corrects_to_scope_p = false;
8779 switch (token->keyword)
8786 /* Consume the `case' token. */
8787 cp_lexer_consume_token (parser->lexer);
8788 /* Parse the constant-expression. */
8789 expr = cp_parser_constant_expression (parser,
8790 /*allow_non_constant_p=*/false,
8793 ellipsis = cp_lexer_peek_token (parser->lexer);
8794 if (ellipsis->type == CPP_ELLIPSIS)
8796 /* Consume the `...' token. */
8797 cp_lexer_consume_token (parser->lexer);
8799 cp_parser_constant_expression (parser,
8800 /*allow_non_constant_p=*/false,
8802 /* We don't need to emit warnings here, as the common code
8803 will do this for us. */
8806 expr_hi = NULL_TREE;
8808 if (parser->in_switch_statement_p)
8809 finish_case_label (token->location, expr, expr_hi);
8811 error_at (token->location,
8812 "case label %qE not within a switch statement",
8818 /* Consume the `default' token. */
8819 cp_lexer_consume_token (parser->lexer);
8821 if (parser->in_switch_statement_p)
8822 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8824 error_at (token->location, "case label not within a switch statement");
8828 /* Anything else must be an ordinary label. */
8829 label = finish_label_stmt (cp_parser_identifier (parser));
8833 /* Require the `:' token. */
8834 cp_parser_require (parser, CPP_COLON, RT_COLON);
8836 /* An ordinary label may optionally be followed by attributes.
8837 However, this is only permitted if the attributes are then
8838 followed by a semicolon. This is because, for backward
8839 compatibility, when parsing
8840 lab: __attribute__ ((unused)) int i;
8841 we want the attribute to attach to "i", not "lab". */
8842 if (label != NULL_TREE
8843 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8847 cp_parser_parse_tentatively (parser);
8848 attrs = cp_parser_attributes_opt (parser);
8849 if (attrs == NULL_TREE
8850 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8851 cp_parser_abort_tentative_parse (parser);
8852 else if (!cp_parser_parse_definitely (parser))
8855 cplus_decl_attributes (&label, attrs, 0);
8858 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8861 /* Parse an expression-statement.
8863 expression-statement:
8866 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8867 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8868 indicates whether this expression-statement is part of an
8869 expression statement. */
8872 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8874 tree statement = NULL_TREE;
8875 cp_token *token = cp_lexer_peek_token (parser->lexer);
8877 /* If the next token is a ';', then there is no expression
8879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8880 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8882 /* Give a helpful message for "A<T>::type t;" and the like. */
8883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8884 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8886 if (TREE_CODE (statement) == SCOPE_REF)
8887 error_at (token->location, "need %<typename%> before %qE because "
8888 "%qT is a dependent scope",
8889 statement, TREE_OPERAND (statement, 0));
8890 else if (is_overloaded_fn (statement)
8891 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8894 tree fn = get_first_fn (statement);
8895 error_at (token->location,
8896 "%<%T::%D%> names the constructor, not the type",
8897 DECL_CONTEXT (fn), DECL_NAME (fn));
8901 /* Consume the final `;'. */
8902 cp_parser_consume_semicolon_at_end_of_statement (parser);
8904 if (in_statement_expr
8905 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8906 /* This is the final expression statement of a statement
8908 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8910 statement = finish_expr_stmt (statement);
8917 /* Parse a compound-statement.
8920 { statement-seq [opt] }
8925 { label-declaration-seq [opt] statement-seq [opt] }
8927 label-declaration-seq:
8929 label-declaration-seq label-declaration
8931 Returns a tree representing the statement. */
8934 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8935 bool in_try, bool function_body)
8939 /* Consume the `{'. */
8940 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8941 return error_mark_node;
8942 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8944 pedwarn (input_location, OPT_pedantic,
8945 "compound-statement in constexpr function");
8946 /* Begin the compound-statement. */
8947 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8948 /* If the next keyword is `__label__' we have a label declaration. */
8949 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8950 cp_parser_label_declaration (parser);
8951 /* Parse an (optional) statement-seq. */
8952 cp_parser_statement_seq_opt (parser, in_statement_expr);
8953 /* Finish the compound-statement. */
8954 finish_compound_stmt (compound_stmt);
8955 /* Consume the `}'. */
8956 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8958 return compound_stmt;
8961 /* Parse an (optional) statement-seq.
8965 statement-seq [opt] statement */
8968 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8970 /* Scan statements until there aren't any more. */
8973 cp_token *token = cp_lexer_peek_token (parser->lexer);
8975 /* If we are looking at a `}', then we have run out of
8976 statements; the same is true if we have reached the end
8977 of file, or have stumbled upon a stray '@end'. */
8978 if (token->type == CPP_CLOSE_BRACE
8979 || token->type == CPP_EOF
8980 || token->type == CPP_PRAGMA_EOL
8981 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8984 /* If we are in a compound statement and find 'else' then
8985 something went wrong. */
8986 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8988 if (parser->in_statement & IN_IF_STMT)
8992 token = cp_lexer_consume_token (parser->lexer);
8993 error_at (token->location, "%<else%> without a previous %<if%>");
8997 /* Parse the statement. */
8998 cp_parser_statement (parser, in_statement_expr, true, NULL);
9002 /* Parse a selection-statement.
9004 selection-statement:
9005 if ( condition ) statement
9006 if ( condition ) statement else statement
9007 switch ( condition ) statement
9009 Returns the new IF_STMT or SWITCH_STMT.
9011 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9012 is a (possibly labeled) if statement which is not enclosed in
9013 braces and has an else clause. This is used to implement
9017 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9025 /* Peek at the next token. */
9026 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9028 /* See what kind of keyword it is. */
9029 keyword = token->keyword;
9038 /* Look for the `('. */
9039 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9041 cp_parser_skip_to_end_of_statement (parser);
9042 return error_mark_node;
9045 /* Begin the selection-statement. */
9046 if (keyword == RID_IF)
9047 statement = begin_if_stmt ();
9049 statement = begin_switch_stmt ();
9051 /* Parse the condition. */
9052 condition = cp_parser_condition (parser);
9053 /* Look for the `)'. */
9054 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9055 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9056 /*consume_paren=*/true);
9058 if (keyword == RID_IF)
9061 unsigned char in_statement;
9063 /* Add the condition. */
9064 finish_if_stmt_cond (condition, statement);
9066 /* Parse the then-clause. */
9067 in_statement = parser->in_statement;
9068 parser->in_statement |= IN_IF_STMT;
9069 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9071 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9072 add_stmt (build_empty_stmt (loc));
9073 cp_lexer_consume_token (parser->lexer);
9074 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9075 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9076 "empty body in an %<if%> statement");
9080 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9081 parser->in_statement = in_statement;
9083 finish_then_clause (statement);
9085 /* If the next token is `else', parse the else-clause. */
9086 if (cp_lexer_next_token_is_keyword (parser->lexer,
9089 /* Consume the `else' keyword. */
9090 cp_lexer_consume_token (parser->lexer);
9091 begin_else_clause (statement);
9092 /* Parse the else-clause. */
9093 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9096 loc = cp_lexer_peek_token (parser->lexer)->location;
9098 OPT_Wempty_body, "suggest braces around "
9099 "empty body in an %<else%> statement");
9100 add_stmt (build_empty_stmt (loc));
9101 cp_lexer_consume_token (parser->lexer);
9104 cp_parser_implicitly_scoped_statement (parser, NULL);
9106 finish_else_clause (statement);
9108 /* If we are currently parsing a then-clause, then
9109 IF_P will not be NULL. We set it to true to
9110 indicate that this if statement has an else clause.
9111 This may trigger the Wparentheses warning below
9112 when we get back up to the parent if statement. */
9118 /* This if statement does not have an else clause. If
9119 NESTED_IF is true, then the then-clause is an if
9120 statement which does have an else clause. We warn
9121 about the potential ambiguity. */
9123 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9124 "suggest explicit braces to avoid ambiguous"
9128 /* Now we're all done with the if-statement. */
9129 finish_if_stmt (statement);
9133 bool in_switch_statement_p;
9134 unsigned char in_statement;
9136 /* Add the condition. */
9137 finish_switch_cond (condition, statement);
9139 /* Parse the body of the switch-statement. */
9140 in_switch_statement_p = parser->in_switch_statement_p;
9141 in_statement = parser->in_statement;
9142 parser->in_switch_statement_p = true;
9143 parser->in_statement |= IN_SWITCH_STMT;
9144 cp_parser_implicitly_scoped_statement (parser, NULL);
9145 parser->in_switch_statement_p = in_switch_statement_p;
9146 parser->in_statement = in_statement;
9148 /* Now we're all done with the switch-statement. */
9149 finish_switch_stmt (statement);
9157 cp_parser_error (parser, "expected selection-statement");
9158 return error_mark_node;
9162 /* Parse a condition.
9166 type-specifier-seq declarator = initializer-clause
9167 type-specifier-seq declarator braced-init-list
9172 type-specifier-seq declarator asm-specification [opt]
9173 attributes [opt] = assignment-expression
9175 Returns the expression that should be tested. */
9178 cp_parser_condition (cp_parser* parser)
9180 cp_decl_specifier_seq type_specifiers;
9181 const char *saved_message;
9182 int declares_class_or_enum;
9184 /* Try the declaration first. */
9185 cp_parser_parse_tentatively (parser);
9186 /* New types are not allowed in the type-specifier-seq for a
9188 saved_message = parser->type_definition_forbidden_message;
9189 parser->type_definition_forbidden_message
9190 = G_("types may not be defined in conditions");
9191 /* Parse the type-specifier-seq. */
9192 cp_parser_decl_specifier_seq (parser,
9193 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9195 &declares_class_or_enum);
9196 /* Restore the saved message. */
9197 parser->type_definition_forbidden_message = saved_message;
9198 /* If all is well, we might be looking at a declaration. */
9199 if (!cp_parser_error_occurred (parser))
9202 tree asm_specification;
9204 cp_declarator *declarator;
9205 tree initializer = NULL_TREE;
9207 /* Parse the declarator. */
9208 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9209 /*ctor_dtor_or_conv_p=*/NULL,
9210 /*parenthesized_p=*/NULL,
9211 /*member_p=*/false);
9212 /* Parse the attributes. */
9213 attributes = cp_parser_attributes_opt (parser);
9214 /* Parse the asm-specification. */
9215 asm_specification = cp_parser_asm_specification_opt (parser);
9216 /* If the next token is not an `=' or '{', then we might still be
9217 looking at an expression. For example:
9221 looks like a decl-specifier-seq and a declarator -- but then
9222 there is no `=', so this is an expression. */
9223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9224 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9225 cp_parser_simulate_error (parser);
9227 /* If we did see an `=' or '{', then we are looking at a declaration
9229 if (cp_parser_parse_definitely (parser))
9232 bool non_constant_p;
9233 bool flags = LOOKUP_ONLYCONVERTING;
9235 /* Create the declaration. */
9236 decl = start_decl (declarator, &type_specifiers,
9237 /*initialized_p=*/true,
9238 attributes, /*prefix_attributes=*/NULL_TREE,
9241 /* Parse the initializer. */
9242 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9244 initializer = cp_parser_braced_list (parser, &non_constant_p);
9245 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9250 /* Consume the `='. */
9251 cp_parser_require (parser, CPP_EQ, RT_EQ);
9252 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9254 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9255 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9257 /* Process the initializer. */
9258 cp_finish_decl (decl,
9259 initializer, !non_constant_p,
9264 pop_scope (pushed_scope);
9266 return convert_from_reference (decl);
9269 /* If we didn't even get past the declarator successfully, we are
9270 definitely not looking at a declaration. */
9272 cp_parser_abort_tentative_parse (parser);
9274 /* Otherwise, we are looking at an expression. */
9275 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9278 /* Parses a for-statement or range-for-statement until the closing ')',
9282 cp_parser_for (cp_parser *parser)
9284 tree init, scope, decl;
9287 /* Begin the for-statement. */
9288 scope = begin_for_scope (&init);
9290 /* Parse the initialization. */
9291 is_range_for = cp_parser_for_init_statement (parser, &decl);
9294 return cp_parser_range_for (parser, scope, init, decl);
9296 return cp_parser_c_for (parser, scope, init);
9300 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9302 /* Normal for loop */
9303 tree condition = NULL_TREE;
9304 tree expression = NULL_TREE;
9307 stmt = begin_for_stmt (scope, init);
9308 /* The for-init-statement has already been parsed in
9309 cp_parser_for_init_statement, so no work is needed here. */
9310 finish_for_init_stmt (stmt);
9312 /* If there's a condition, process it. */
9313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9314 condition = cp_parser_condition (parser);
9315 finish_for_cond (condition, stmt);
9316 /* Look for the `;'. */
9317 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9319 /* If there's an expression, process it. */
9320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9321 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9322 finish_for_expr (expression, stmt);
9327 /* Tries to parse a range-based for-statement:
9330 decl-specifier-seq declarator : expression
9332 The decl-specifier-seq declarator and the `:' are already parsed by
9333 cp_parser_for_init_statement. If processing_template_decl it returns a
9334 newly created RANGE_FOR_STMT; if not, it is converted to a
9335 regular FOR_STMT. */
9338 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9340 tree stmt, range_expr;
9342 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9344 bool expr_non_constant_p;
9345 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9348 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9350 /* If in template, STMT is converted to a normal for-statement
9351 at instantiation. If not, it is done just ahead. */
9352 if (processing_template_decl)
9354 if (check_for_bare_parameter_packs (range_expr))
9355 range_expr = error_mark_node;
9356 stmt = begin_range_for_stmt (scope, init);
9357 finish_range_for_decl (stmt, range_decl, range_expr);
9358 if (!type_dependent_expression_p (range_expr)
9359 /* do_auto_deduction doesn't mess with template init-lists. */
9360 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9361 do_range_for_auto_deduction (range_decl, range_expr);
9365 stmt = begin_for_stmt (scope, init);
9366 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9371 /* Subroutine of cp_convert_range_for: given the initializer expression,
9372 builds up the range temporary. */
9375 build_range_temp (tree range_expr)
9377 tree range_type, range_temp;
9379 /* Find out the type deduced by the declaration
9380 `auto &&__range = range_expr'. */
9381 range_type = cp_build_reference_type (make_auto (), true);
9382 range_type = do_auto_deduction (range_type, range_expr,
9383 type_uses_auto (range_type));
9385 /* Create the __range variable. */
9386 range_temp = build_decl (input_location, VAR_DECL,
9387 get_identifier ("__for_range"), range_type);
9388 TREE_USED (range_temp) = 1;
9389 DECL_ARTIFICIAL (range_temp) = 1;
9394 /* Used by cp_parser_range_for in template context: we aren't going to
9395 do a full conversion yet, but we still need to resolve auto in the
9396 type of the for-range-declaration if present. This is basically
9397 a shortcut version of cp_convert_range_for. */
9400 do_range_for_auto_deduction (tree decl, tree range_expr)
9402 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9405 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9406 range_temp = convert_from_reference (build_range_temp (range_expr));
9407 iter_type = (cp_parser_perform_range_for_lookup
9408 (range_temp, &begin_dummy, &end_dummy));
9409 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9410 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9411 tf_warning_or_error);
9412 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9413 iter_decl, auto_node);
9417 /* Converts a range-based for-statement into a normal
9418 for-statement, as per the definition.
9420 for (RANGE_DECL : RANGE_EXPR)
9423 should be equivalent to:
9426 auto &&__range = RANGE_EXPR;
9427 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9431 RANGE_DECL = *__begin;
9436 If RANGE_EXPR is an array:
9437 BEGIN_EXPR = __range
9438 END_EXPR = __range + ARRAY_SIZE(__range)
9439 Else if RANGE_EXPR has a member 'begin' or 'end':
9440 BEGIN_EXPR = __range.begin()
9441 END_EXPR = __range.end()
9443 BEGIN_EXPR = begin(__range)
9444 END_EXPR = end(__range);
9446 If __range has a member 'begin' but not 'end', or vice versa, we must
9447 still use the second alternative (it will surely fail, however).
9448 When calling begin()/end() in the third alternative we must use
9449 argument dependent lookup, but always considering 'std' as an associated
9453 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9456 tree iter_type, begin_expr, end_expr;
9457 tree condition, expression;
9459 if (range_decl == error_mark_node || range_expr == error_mark_node)
9460 /* If an error happened previously do nothing or else a lot of
9461 unhelpful errors would be issued. */
9462 begin_expr = end_expr = iter_type = error_mark_node;
9465 tree range_temp = build_range_temp (range_expr);
9466 pushdecl (range_temp);
9467 cp_finish_decl (range_temp, range_expr,
9468 /*is_constant_init*/false, NULL_TREE,
9469 LOOKUP_ONLYCONVERTING);
9471 range_temp = convert_from_reference (range_temp);
9472 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9473 &begin_expr, &end_expr);
9476 /* The new for initialization statement. */
9477 begin = build_decl (input_location, VAR_DECL,
9478 get_identifier ("__for_begin"), iter_type);
9479 TREE_USED (begin) = 1;
9480 DECL_ARTIFICIAL (begin) = 1;
9482 cp_finish_decl (begin, begin_expr,
9483 /*is_constant_init*/false, NULL_TREE,
9484 LOOKUP_ONLYCONVERTING);
9486 end = build_decl (input_location, VAR_DECL,
9487 get_identifier ("__for_end"), iter_type);
9488 TREE_USED (end) = 1;
9489 DECL_ARTIFICIAL (end) = 1;
9491 cp_finish_decl (end, end_expr,
9492 /*is_constant_init*/false, NULL_TREE,
9493 LOOKUP_ONLYCONVERTING);
9495 finish_for_init_stmt (statement);
9497 /* The new for condition. */
9498 condition = build_x_binary_op (NE_EXPR,
9501 NULL, tf_warning_or_error);
9502 finish_for_cond (condition, statement);
9504 /* The new increment expression. */
9505 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9506 finish_for_expr (expression, statement);
9508 /* The declaration is initialized with *__begin inside the loop body. */
9509 cp_finish_decl (range_decl,
9510 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9511 /*is_constant_init*/false, NULL_TREE,
9512 LOOKUP_ONLYCONVERTING);
9517 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9518 We need to solve both at the same time because the method used
9519 depends on the existence of members begin or end.
9520 Returns the type deduced for the iterator expression. */
9523 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9525 if (error_operand_p (range))
9527 *begin = *end = error_mark_node;
9528 return error_mark_node;
9531 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9533 error ("range-based %<for%> expression of type %qT "
9534 "has incomplete type", TREE_TYPE (range));
9535 *begin = *end = error_mark_node;
9536 return error_mark_node;
9538 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9540 /* If RANGE is an array, we will use pointer arithmetic. */
9542 *end = build_binary_op (input_location, PLUS_EXPR,
9544 array_type_nelts_top (TREE_TYPE (range)),
9546 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9550 /* If it is not an array, we must do a bit of magic. */
9551 tree id_begin, id_end;
9552 tree member_begin, member_end;
9554 *begin = *end = error_mark_node;
9556 id_begin = get_identifier ("begin");
9557 id_end = get_identifier ("end");
9558 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9559 /*protect=*/2, /*want_type=*/false,
9560 tf_warning_or_error);
9561 member_end = lookup_member (TREE_TYPE (range), id_end,
9562 /*protect=*/2, /*want_type=*/false,
9563 tf_warning_or_error);
9565 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9567 /* Use the member functions. */
9568 if (member_begin != NULL_TREE)
9569 *begin = cp_parser_range_for_member_function (range, id_begin);
9571 error ("range-based %<for%> expression of type %qT has an "
9572 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9574 if (member_end != NULL_TREE)
9575 *end = cp_parser_range_for_member_function (range, id_end);
9577 error ("range-based %<for%> expression of type %qT has a "
9578 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9582 /* Use global functions with ADL. */
9584 vec = make_tree_vector ();
9586 VEC_safe_push (tree, gc, vec, range);
9588 member_begin = perform_koenig_lookup (id_begin, vec,
9589 /*include_std=*/true,
9590 tf_warning_or_error);
9591 *begin = finish_call_expr (member_begin, &vec, false, true,
9592 tf_warning_or_error);
9593 member_end = perform_koenig_lookup (id_end, vec,
9594 /*include_std=*/true,
9595 tf_warning_or_error);
9596 *end = finish_call_expr (member_end, &vec, false, true,
9597 tf_warning_or_error);
9599 release_tree_vector (vec);
9602 /* Last common checks. */
9603 if (*begin == error_mark_node || *end == error_mark_node)
9605 /* If one of the expressions is an error do no more checks. */
9606 *begin = *end = error_mark_node;
9607 return error_mark_node;
9611 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9612 /* The unqualified type of the __begin and __end temporaries should
9613 be the same, as required by the multiple auto declaration. */
9614 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9615 error ("inconsistent begin/end types in range-based %<for%> "
9616 "statement: %qT and %qT",
9617 TREE_TYPE (*begin), TREE_TYPE (*end));
9623 /* Helper function for cp_parser_perform_range_for_lookup.
9624 Builds a tree for RANGE.IDENTIFIER(). */
9627 cp_parser_range_for_member_function (tree range, tree identifier)
9632 member = finish_class_member_access_expr (range, identifier,
9633 false, tf_warning_or_error);
9634 if (member == error_mark_node)
9635 return error_mark_node;
9637 vec = make_tree_vector ();
9638 res = finish_call_expr (member, &vec,
9639 /*disallow_virtual=*/false,
9641 tf_warning_or_error);
9642 release_tree_vector (vec);
9646 /* Parse an iteration-statement.
9648 iteration-statement:
9649 while ( condition ) statement
9650 do statement while ( expression ) ;
9651 for ( for-init-statement condition [opt] ; expression [opt] )
9654 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9657 cp_parser_iteration_statement (cp_parser* parser)
9662 unsigned char in_statement;
9664 /* Peek at the next token. */
9665 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9667 return error_mark_node;
9669 /* Remember whether or not we are already within an iteration
9671 in_statement = parser->in_statement;
9673 /* See what kind of keyword it is. */
9674 keyword = token->keyword;
9681 /* Begin the while-statement. */
9682 statement = begin_while_stmt ();
9683 /* Look for the `('. */
9684 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9685 /* Parse the condition. */
9686 condition = cp_parser_condition (parser);
9687 finish_while_stmt_cond (condition, statement);
9688 /* Look for the `)'. */
9689 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9690 /* Parse the dependent statement. */
9691 parser->in_statement = IN_ITERATION_STMT;
9692 cp_parser_already_scoped_statement (parser);
9693 parser->in_statement = in_statement;
9694 /* We're done with the while-statement. */
9695 finish_while_stmt (statement);
9703 /* Begin the do-statement. */
9704 statement = begin_do_stmt ();
9705 /* Parse the body of the do-statement. */
9706 parser->in_statement = IN_ITERATION_STMT;
9707 cp_parser_implicitly_scoped_statement (parser, NULL);
9708 parser->in_statement = in_statement;
9709 finish_do_body (statement);
9710 /* Look for the `while' keyword. */
9711 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9712 /* Look for the `('. */
9713 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9714 /* Parse the expression. */
9715 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9716 /* We're done with the do-statement. */
9717 finish_do_stmt (expression, statement);
9718 /* Look for the `)'. */
9719 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9720 /* Look for the `;'. */
9721 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9727 /* Look for the `('. */
9728 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9730 statement = cp_parser_for (parser);
9732 /* Look for the `)'. */
9733 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9735 /* Parse the body of the for-statement. */
9736 parser->in_statement = IN_ITERATION_STMT;
9737 cp_parser_already_scoped_statement (parser);
9738 parser->in_statement = in_statement;
9740 /* We're done with the for-statement. */
9741 finish_for_stmt (statement);
9746 cp_parser_error (parser, "expected iteration-statement");
9747 statement = error_mark_node;
9754 /* Parse a for-init-statement or the declarator of a range-based-for.
9755 Returns true if a range-based-for declaration is seen.
9758 expression-statement
9759 simple-declaration */
9762 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9764 /* If the next token is a `;', then we have an empty
9765 expression-statement. Grammatically, this is also a
9766 simple-declaration, but an invalid one, because it does not
9767 declare anything. Therefore, if we did not handle this case
9768 specially, we would issue an error message about an invalid
9770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9772 bool is_range_for = false;
9773 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9775 parser->colon_corrects_to_scope_p = false;
9777 /* We're going to speculatively look for a declaration, falling back
9778 to an expression, if necessary. */
9779 cp_parser_parse_tentatively (parser);
9780 /* Parse the declaration. */
9781 cp_parser_simple_declaration (parser,
9782 /*function_definition_allowed_p=*/false,
9784 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9785 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9787 /* It is a range-for, consume the ':' */
9788 cp_lexer_consume_token (parser->lexer);
9789 is_range_for = true;
9790 if (cxx_dialect < cxx0x)
9792 error_at (cp_lexer_peek_token (parser->lexer)->location,
9793 "range-based %<for%> loops are not allowed "
9795 *decl = error_mark_node;
9799 /* The ';' is not consumed yet because we told
9800 cp_parser_simple_declaration not to. */
9801 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9803 if (cp_parser_parse_definitely (parser))
9804 return is_range_for;
9805 /* If the tentative parse failed, then we shall need to look for an
9806 expression-statement. */
9808 /* If we are here, it is an expression-statement. */
9809 cp_parser_expression_statement (parser, NULL_TREE);
9813 /* Parse a jump-statement.
9818 return expression [opt] ;
9819 return braced-init-list ;
9827 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9830 cp_parser_jump_statement (cp_parser* parser)
9832 tree statement = error_mark_node;
9835 unsigned char in_statement;
9837 /* Peek at the next token. */
9838 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9840 return error_mark_node;
9842 /* See what kind of keyword it is. */
9843 keyword = token->keyword;
9847 in_statement = parser->in_statement & ~IN_IF_STMT;
9848 switch (in_statement)
9851 error_at (token->location, "break statement not within loop or switch");
9854 gcc_assert ((in_statement & IN_SWITCH_STMT)
9855 || in_statement == IN_ITERATION_STMT);
9856 statement = finish_break_stmt ();
9859 error_at (token->location, "invalid exit from OpenMP structured block");
9862 error_at (token->location, "break statement used with OpenMP for loop");
9865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9869 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9872 error_at (token->location, "continue statement not within a loop");
9874 case IN_ITERATION_STMT:
9876 statement = finish_continue_stmt ();
9879 error_at (token->location, "invalid exit from OpenMP structured block");
9884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9890 bool expr_non_constant_p;
9892 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9894 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9895 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9897 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9898 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9900 /* If the next token is a `;', then there is no
9903 /* Build the return-statement. */
9904 statement = finish_return_stmt (expr);
9905 /* Look for the final `;'. */
9906 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9911 /* Create the goto-statement. */
9912 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9914 /* Issue a warning about this use of a GNU extension. */
9915 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9916 /* Consume the '*' token. */
9917 cp_lexer_consume_token (parser->lexer);
9918 /* Parse the dependent expression. */
9919 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9922 finish_goto_stmt (cp_parser_identifier (parser));
9923 /* Look for the final `;'. */
9924 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9928 cp_parser_error (parser, "expected jump-statement");
9935 /* Parse a declaration-statement.
9937 declaration-statement:
9938 block-declaration */
9941 cp_parser_declaration_statement (cp_parser* parser)
9945 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9946 p = obstack_alloc (&declarator_obstack, 0);
9948 /* Parse the block-declaration. */
9949 cp_parser_block_declaration (parser, /*statement_p=*/true);
9951 /* Free any declarators allocated. */
9952 obstack_free (&declarator_obstack, p);
9954 /* Finish off the statement. */
9958 /* Some dependent statements (like `if (cond) statement'), are
9959 implicitly in their own scope. In other words, if the statement is
9960 a single statement (as opposed to a compound-statement), it is
9961 none-the-less treated as if it were enclosed in braces. Any
9962 declarations appearing in the dependent statement are out of scope
9963 after control passes that point. This function parses a statement,
9964 but ensures that is in its own scope, even if it is not a
9967 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9968 is a (possibly labeled) if statement which is not enclosed in
9969 braces and has an else clause. This is used to implement
9972 Returns the new statement. */
9975 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9982 /* Mark if () ; with a special NOP_EXPR. */
9983 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9985 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9986 cp_lexer_consume_token (parser->lexer);
9987 statement = add_stmt (build_empty_stmt (loc));
9989 /* if a compound is opened, we simply parse the statement directly. */
9990 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9991 statement = cp_parser_compound_statement (parser, NULL, false, false);
9992 /* If the token is not a `{', then we must take special action. */
9995 /* Create a compound-statement. */
9996 statement = begin_compound_stmt (0);
9997 /* Parse the dependent-statement. */
9998 cp_parser_statement (parser, NULL_TREE, false, if_p);
9999 /* Finish the dummy compound-statement. */
10000 finish_compound_stmt (statement);
10003 /* Return the statement. */
10007 /* For some dependent statements (like `while (cond) statement'), we
10008 have already created a scope. Therefore, even if the dependent
10009 statement is a compound-statement, we do not want to create another
10013 cp_parser_already_scoped_statement (cp_parser* parser)
10015 /* If the token is a `{', then we must take special action. */
10016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10017 cp_parser_statement (parser, NULL_TREE, false, NULL);
10020 /* Avoid calling cp_parser_compound_statement, so that we
10021 don't create a new scope. Do everything else by hand. */
10022 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10023 /* If the next keyword is `__label__' we have a label declaration. */
10024 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10025 cp_parser_label_declaration (parser);
10026 /* Parse an (optional) statement-seq. */
10027 cp_parser_statement_seq_opt (parser, NULL_TREE);
10028 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10032 /* Declarations [gram.dcl.dcl] */
10034 /* Parse an optional declaration-sequence.
10038 declaration-seq declaration */
10041 cp_parser_declaration_seq_opt (cp_parser* parser)
10047 token = cp_lexer_peek_token (parser->lexer);
10049 if (token->type == CPP_CLOSE_BRACE
10050 || token->type == CPP_EOF
10051 || token->type == CPP_PRAGMA_EOL)
10054 if (token->type == CPP_SEMICOLON)
10056 /* A declaration consisting of a single semicolon is
10057 invalid. Allow it unless we're being pedantic. */
10058 cp_lexer_consume_token (parser->lexer);
10059 if (!in_system_header)
10060 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10064 /* If we're entering or exiting a region that's implicitly
10065 extern "C", modify the lang context appropriately. */
10066 if (!parser->implicit_extern_c && token->implicit_extern_c)
10068 push_lang_context (lang_name_c);
10069 parser->implicit_extern_c = true;
10071 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10073 pop_lang_context ();
10074 parser->implicit_extern_c = false;
10077 if (token->type == CPP_PRAGMA)
10079 /* A top-level declaration can consist solely of a #pragma.
10080 A nested declaration cannot, so this is done here and not
10081 in cp_parser_declaration. (A #pragma at block scope is
10082 handled in cp_parser_statement.) */
10083 cp_parser_pragma (parser, pragma_external);
10087 /* Parse the declaration itself. */
10088 cp_parser_declaration (parser);
10092 /* Parse a declaration.
10096 function-definition
10097 template-declaration
10098 explicit-instantiation
10099 explicit-specialization
10100 linkage-specification
10101 namespace-definition
10106 __extension__ declaration */
10109 cp_parser_declaration (cp_parser* parser)
10113 int saved_pedantic;
10115 tree attributes = NULL_TREE;
10117 /* Check for the `__extension__' keyword. */
10118 if (cp_parser_extension_opt (parser, &saved_pedantic))
10120 /* Parse the qualified declaration. */
10121 cp_parser_declaration (parser);
10122 /* Restore the PEDANTIC flag. */
10123 pedantic = saved_pedantic;
10128 /* Try to figure out what kind of declaration is present. */
10129 token1 = *cp_lexer_peek_token (parser->lexer);
10131 if (token1.type != CPP_EOF)
10132 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10135 token2.type = CPP_EOF;
10136 token2.keyword = RID_MAX;
10139 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10140 p = obstack_alloc (&declarator_obstack, 0);
10142 /* If the next token is `extern' and the following token is a string
10143 literal, then we have a linkage specification. */
10144 if (token1.keyword == RID_EXTERN
10145 && cp_parser_is_pure_string_literal (&token2))
10146 cp_parser_linkage_specification (parser);
10147 /* If the next token is `template', then we have either a template
10148 declaration, an explicit instantiation, or an explicit
10150 else if (token1.keyword == RID_TEMPLATE)
10152 /* `template <>' indicates a template specialization. */
10153 if (token2.type == CPP_LESS
10154 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10155 cp_parser_explicit_specialization (parser);
10156 /* `template <' indicates a template declaration. */
10157 else if (token2.type == CPP_LESS)
10158 cp_parser_template_declaration (parser, /*member_p=*/false);
10159 /* Anything else must be an explicit instantiation. */
10161 cp_parser_explicit_instantiation (parser);
10163 /* If the next token is `export', then we have a template
10165 else if (token1.keyword == RID_EXPORT)
10166 cp_parser_template_declaration (parser, /*member_p=*/false);
10167 /* If the next token is `extern', 'static' or 'inline' and the one
10168 after that is `template', we have a GNU extended explicit
10169 instantiation directive. */
10170 else if (cp_parser_allow_gnu_extensions_p (parser)
10171 && (token1.keyword == RID_EXTERN
10172 || token1.keyword == RID_STATIC
10173 || token1.keyword == RID_INLINE)
10174 && token2.keyword == RID_TEMPLATE)
10175 cp_parser_explicit_instantiation (parser);
10176 /* If the next token is `namespace', check for a named or unnamed
10177 namespace definition. */
10178 else if (token1.keyword == RID_NAMESPACE
10179 && (/* A named namespace definition. */
10180 (token2.type == CPP_NAME
10181 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10183 /* An unnamed namespace definition. */
10184 || token2.type == CPP_OPEN_BRACE
10185 || token2.keyword == RID_ATTRIBUTE))
10186 cp_parser_namespace_definition (parser);
10187 /* An inline (associated) namespace definition. */
10188 else if (token1.keyword == RID_INLINE
10189 && token2.keyword == RID_NAMESPACE)
10190 cp_parser_namespace_definition (parser);
10191 /* Objective-C++ declaration/definition. */
10192 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10193 cp_parser_objc_declaration (parser, NULL_TREE);
10194 else if (c_dialect_objc ()
10195 && token1.keyword == RID_ATTRIBUTE
10196 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10197 cp_parser_objc_declaration (parser, attributes);
10198 /* We must have either a block declaration or a function
10201 /* Try to parse a block-declaration, or a function-definition. */
10202 cp_parser_block_declaration (parser, /*statement_p=*/false);
10204 /* Free any declarators allocated. */
10205 obstack_free (&declarator_obstack, p);
10208 /* Parse a block-declaration.
10213 namespace-alias-definition
10220 __extension__ block-declaration
10225 static_assert-declaration
10227 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10228 part of a declaration-statement. */
10231 cp_parser_block_declaration (cp_parser *parser,
10235 int saved_pedantic;
10237 /* Check for the `__extension__' keyword. */
10238 if (cp_parser_extension_opt (parser, &saved_pedantic))
10240 /* Parse the qualified declaration. */
10241 cp_parser_block_declaration (parser, statement_p);
10242 /* Restore the PEDANTIC flag. */
10243 pedantic = saved_pedantic;
10248 /* Peek at the next token to figure out which kind of declaration is
10250 token1 = cp_lexer_peek_token (parser->lexer);
10252 /* If the next keyword is `asm', we have an asm-definition. */
10253 if (token1->keyword == RID_ASM)
10256 cp_parser_commit_to_tentative_parse (parser);
10257 cp_parser_asm_definition (parser);
10259 /* If the next keyword is `namespace', we have a
10260 namespace-alias-definition. */
10261 else if (token1->keyword == RID_NAMESPACE)
10262 cp_parser_namespace_alias_definition (parser);
10263 /* If the next keyword is `using', we have a
10264 using-declaration, a using-directive, or an alias-declaration. */
10265 else if (token1->keyword == RID_USING)
10270 cp_parser_commit_to_tentative_parse (parser);
10271 /* If the token after `using' is `namespace', then we have a
10272 using-directive. */
10273 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10274 if (token2->keyword == RID_NAMESPACE)
10275 cp_parser_using_directive (parser);
10276 /* If the second token after 'using' is '=', then we have an
10277 alias-declaration. */
10278 else if (cxx_dialect >= cxx0x
10279 && token2->type == CPP_NAME
10280 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10281 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10282 == RID_ATTRIBUTE)))
10283 cp_parser_alias_declaration (parser);
10284 /* Otherwise, it's a using-declaration. */
10286 cp_parser_using_declaration (parser,
10287 /*access_declaration_p=*/false);
10289 /* If the next keyword is `__label__' we have a misplaced label
10291 else if (token1->keyword == RID_LABEL)
10293 cp_lexer_consume_token (parser->lexer);
10294 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10295 cp_parser_skip_to_end_of_statement (parser);
10296 /* If the next token is now a `;', consume it. */
10297 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10298 cp_lexer_consume_token (parser->lexer);
10300 /* If the next token is `static_assert' we have a static assertion. */
10301 else if (token1->keyword == RID_STATIC_ASSERT)
10302 cp_parser_static_assert (parser, /*member_p=*/false);
10303 /* Anything else must be a simple-declaration. */
10305 cp_parser_simple_declaration (parser, !statement_p,
10306 /*maybe_range_for_decl*/NULL);
10309 /* Parse a simple-declaration.
10311 simple-declaration:
10312 decl-specifier-seq [opt] init-declarator-list [opt] ;
10314 init-declarator-list:
10316 init-declarator-list , init-declarator
10318 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10319 function-definition as a simple-declaration.
10321 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10322 parsed declaration if it is an uninitialized single declarator not followed
10323 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10324 if present, will not be consumed. */
10327 cp_parser_simple_declaration (cp_parser* parser,
10328 bool function_definition_allowed_p,
10329 tree *maybe_range_for_decl)
10331 cp_decl_specifier_seq decl_specifiers;
10332 int declares_class_or_enum;
10333 bool saw_declarator;
10335 if (maybe_range_for_decl)
10336 *maybe_range_for_decl = NULL_TREE;
10338 /* Defer access checks until we know what is being declared; the
10339 checks for names appearing in the decl-specifier-seq should be
10340 done as if we were in the scope of the thing being declared. */
10341 push_deferring_access_checks (dk_deferred);
10343 /* Parse the decl-specifier-seq. We have to keep track of whether
10344 or not the decl-specifier-seq declares a named class or
10345 enumeration type, since that is the only case in which the
10346 init-declarator-list is allowed to be empty.
10350 In a simple-declaration, the optional init-declarator-list can be
10351 omitted only when declaring a class or enumeration, that is when
10352 the decl-specifier-seq contains either a class-specifier, an
10353 elaborated-type-specifier, or an enum-specifier. */
10354 cp_parser_decl_specifier_seq (parser,
10355 CP_PARSER_FLAGS_OPTIONAL,
10357 &declares_class_or_enum);
10358 /* We no longer need to defer access checks. */
10359 stop_deferring_access_checks ();
10361 /* In a block scope, a valid declaration must always have a
10362 decl-specifier-seq. By not trying to parse declarators, we can
10363 resolve the declaration/expression ambiguity more quickly. */
10364 if (!function_definition_allowed_p
10365 && !decl_specifiers.any_specifiers_p)
10367 cp_parser_error (parser, "expected declaration");
10371 /* If the next two tokens are both identifiers, the code is
10372 erroneous. The usual cause of this situation is code like:
10376 where "T" should name a type -- but does not. */
10377 if (!decl_specifiers.any_type_specifiers_p
10378 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10380 /* If parsing tentatively, we should commit; we really are
10381 looking at a declaration. */
10382 cp_parser_commit_to_tentative_parse (parser);
10387 /* If we have seen at least one decl-specifier, and the next token
10388 is not a parenthesis, then we must be looking at a declaration.
10389 (After "int (" we might be looking at a functional cast.) */
10390 if (decl_specifiers.any_specifiers_p
10391 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10392 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10393 && !cp_parser_error_occurred (parser))
10394 cp_parser_commit_to_tentative_parse (parser);
10396 /* Keep going until we hit the `;' at the end of the simple
10398 saw_declarator = false;
10399 while (cp_lexer_next_token_is_not (parser->lexer,
10403 bool function_definition_p;
10406 if (saw_declarator)
10408 /* If we are processing next declarator, coma is expected */
10409 token = cp_lexer_peek_token (parser->lexer);
10410 gcc_assert (token->type == CPP_COMMA);
10411 cp_lexer_consume_token (parser->lexer);
10412 if (maybe_range_for_decl)
10413 *maybe_range_for_decl = error_mark_node;
10416 saw_declarator = true;
10418 /* Parse the init-declarator. */
10419 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10421 function_definition_allowed_p,
10422 /*member_p=*/false,
10423 declares_class_or_enum,
10424 &function_definition_p,
10425 maybe_range_for_decl);
10426 /* If an error occurred while parsing tentatively, exit quickly.
10427 (That usually happens when in the body of a function; each
10428 statement is treated as a declaration-statement until proven
10430 if (cp_parser_error_occurred (parser))
10432 /* Handle function definitions specially. */
10433 if (function_definition_p)
10435 /* If the next token is a `,', then we are probably
10436 processing something like:
10440 which is erroneous. */
10441 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10443 cp_token *token = cp_lexer_peek_token (parser->lexer);
10444 error_at (token->location,
10446 " declarations and function-definitions is forbidden");
10448 /* Otherwise, we're done with the list of declarators. */
10451 pop_deferring_access_checks ();
10455 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10456 *maybe_range_for_decl = decl;
10457 /* The next token should be either a `,' or a `;'. */
10458 token = cp_lexer_peek_token (parser->lexer);
10459 /* If it's a `,', there are more declarators to come. */
10460 if (token->type == CPP_COMMA)
10461 /* will be consumed next time around */;
10462 /* If it's a `;', we are done. */
10463 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10465 /* Anything else is an error. */
10468 /* If we have already issued an error message we don't need
10469 to issue another one. */
10470 if (decl != error_mark_node
10471 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10472 cp_parser_error (parser, "expected %<,%> or %<;%>");
10473 /* Skip tokens until we reach the end of the statement. */
10474 cp_parser_skip_to_end_of_statement (parser);
10475 /* If the next token is now a `;', consume it. */
10476 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10477 cp_lexer_consume_token (parser->lexer);
10480 /* After the first time around, a function-definition is not
10481 allowed -- even if it was OK at first. For example:
10486 function_definition_allowed_p = false;
10489 /* Issue an error message if no declarators are present, and the
10490 decl-specifier-seq does not itself declare a class or
10492 if (!saw_declarator)
10494 if (cp_parser_declares_only_class_p (parser))
10495 shadow_tag (&decl_specifiers);
10496 /* Perform any deferred access checks. */
10497 perform_deferred_access_checks ();
10500 /* Consume the `;'. */
10501 if (!maybe_range_for_decl)
10502 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10505 pop_deferring_access_checks ();
10508 /* Parse a decl-specifier-seq.
10510 decl-specifier-seq:
10511 decl-specifier-seq [opt] decl-specifier
10514 storage-class-specifier
10525 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10527 The parser flags FLAGS is used to control type-specifier parsing.
10529 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10532 1: one of the decl-specifiers is an elaborated-type-specifier
10533 (i.e., a type declaration)
10534 2: one of the decl-specifiers is an enum-specifier or a
10535 class-specifier (i.e., a type definition)
10540 cp_parser_decl_specifier_seq (cp_parser* parser,
10541 cp_parser_flags flags,
10542 cp_decl_specifier_seq *decl_specs,
10543 int* declares_class_or_enum)
10545 bool constructor_possible_p = !parser->in_declarator_p;
10546 cp_token *start_token = NULL;
10548 /* Clear DECL_SPECS. */
10549 clear_decl_specs (decl_specs);
10551 /* Assume no class or enumeration type is declared. */
10552 *declares_class_or_enum = 0;
10554 /* Keep reading specifiers until there are no more to read. */
10557 bool constructor_p;
10558 bool found_decl_spec;
10561 /* Peek at the next token. */
10562 token = cp_lexer_peek_token (parser->lexer);
10564 /* Save the first token of the decl spec list for error
10567 start_token = token;
10568 /* Handle attributes. */
10569 if (token->keyword == RID_ATTRIBUTE)
10571 /* Parse the attributes. */
10572 decl_specs->attributes
10573 = chainon (decl_specs->attributes,
10574 cp_parser_attributes_opt (parser));
10577 /* Assume we will find a decl-specifier keyword. */
10578 found_decl_spec = true;
10579 /* If the next token is an appropriate keyword, we can simply
10580 add it to the list. */
10581 switch (token->keyword)
10587 if (!at_class_scope_p ())
10589 error_at (token->location, "%<friend%> used outside of class");
10590 cp_lexer_purge_token (parser->lexer);
10594 ++decl_specs->specs[(int) ds_friend];
10595 /* Consume the token. */
10596 cp_lexer_consume_token (parser->lexer);
10600 case RID_CONSTEXPR:
10601 ++decl_specs->specs[(int) ds_constexpr];
10602 cp_lexer_consume_token (parser->lexer);
10605 /* function-specifier:
10612 cp_parser_function_specifier_opt (parser, decl_specs);
10618 ++decl_specs->specs[(int) ds_typedef];
10619 /* Consume the token. */
10620 cp_lexer_consume_token (parser->lexer);
10621 /* A constructor declarator cannot appear in a typedef. */
10622 constructor_possible_p = false;
10623 /* The "typedef" keyword can only occur in a declaration; we
10624 may as well commit at this point. */
10625 cp_parser_commit_to_tentative_parse (parser);
10627 if (decl_specs->storage_class != sc_none)
10628 decl_specs->conflicting_specifiers_p = true;
10631 /* storage-class-specifier:
10641 if (cxx_dialect == cxx98)
10643 /* Consume the token. */
10644 cp_lexer_consume_token (parser->lexer);
10646 /* Complain about `auto' as a storage specifier, if
10647 we're complaining about C++0x compatibility. */
10648 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10649 " changes meaning in C++11; please remove it");
10651 /* Set the storage class anyway. */
10652 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10656 /* C++0x auto type-specifier. */
10657 found_decl_spec = false;
10664 /* Consume the token. */
10665 cp_lexer_consume_token (parser->lexer);
10666 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10670 /* Consume the token. */
10671 cp_lexer_consume_token (parser->lexer);
10672 ++decl_specs->specs[(int) ds_thread];
10676 /* We did not yet find a decl-specifier yet. */
10677 found_decl_spec = false;
10681 if (found_decl_spec
10682 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10683 && token->keyword != RID_CONSTEXPR)
10684 error ("decl-specifier invalid in condition");
10686 /* Constructors are a special case. The `S' in `S()' is not a
10687 decl-specifier; it is the beginning of the declarator. */
10689 = (!found_decl_spec
10690 && constructor_possible_p
10691 && (cp_parser_constructor_declarator_p
10692 (parser, decl_specs->specs[(int) ds_friend] != 0)));
10694 /* If we don't have a DECL_SPEC yet, then we must be looking at
10695 a type-specifier. */
10696 if (!found_decl_spec && !constructor_p)
10698 int decl_spec_declares_class_or_enum;
10699 bool is_cv_qualifier;
10703 = cp_parser_type_specifier (parser, flags,
10705 /*is_declaration=*/true,
10706 &decl_spec_declares_class_or_enum,
10708 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10710 /* If this type-specifier referenced a user-defined type
10711 (a typedef, class-name, etc.), then we can't allow any
10712 more such type-specifiers henceforth.
10716 The longest sequence of decl-specifiers that could
10717 possibly be a type name is taken as the
10718 decl-specifier-seq of a declaration. The sequence shall
10719 be self-consistent as described below.
10723 As a general rule, at most one type-specifier is allowed
10724 in the complete decl-specifier-seq of a declaration. The
10725 only exceptions are the following:
10727 -- const or volatile can be combined with any other
10730 -- signed or unsigned can be combined with char, long,
10738 void g (const int Pc);
10740 Here, Pc is *not* part of the decl-specifier seq; it's
10741 the declarator. Therefore, once we see a type-specifier
10742 (other than a cv-qualifier), we forbid any additional
10743 user-defined types. We *do* still allow things like `int
10744 int' to be considered a decl-specifier-seq, and issue the
10745 error message later. */
10746 if (type_spec && !is_cv_qualifier)
10747 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10748 /* A constructor declarator cannot follow a type-specifier. */
10751 constructor_possible_p = false;
10752 found_decl_spec = true;
10753 if (!is_cv_qualifier)
10754 decl_specs->any_type_specifiers_p = true;
10758 /* If we still do not have a DECL_SPEC, then there are no more
10759 decl-specifiers. */
10760 if (!found_decl_spec)
10763 decl_specs->any_specifiers_p = true;
10764 /* After we see one decl-specifier, further decl-specifiers are
10765 always optional. */
10766 flags |= CP_PARSER_FLAGS_OPTIONAL;
10769 cp_parser_check_decl_spec (decl_specs, start_token->location);
10771 /* Don't allow a friend specifier with a class definition. */
10772 if (decl_specs->specs[(int) ds_friend] != 0
10773 && (*declares_class_or_enum & 2))
10774 error_at (start_token->location,
10775 "class definition may not be declared a friend");
10778 /* Parse an (optional) storage-class-specifier.
10780 storage-class-specifier:
10789 storage-class-specifier:
10792 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10795 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10797 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10800 if (cxx_dialect != cxx98)
10802 /* Fall through for C++98. */
10809 /* Consume the token. */
10810 return cp_lexer_consume_token (parser->lexer)->u.value;
10817 /* Parse an (optional) function-specifier.
10819 function-specifier:
10824 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10825 Updates DECL_SPECS, if it is non-NULL. */
10828 cp_parser_function_specifier_opt (cp_parser* parser,
10829 cp_decl_specifier_seq *decl_specs)
10831 cp_token *token = cp_lexer_peek_token (parser->lexer);
10832 switch (token->keyword)
10836 ++decl_specs->specs[(int) ds_inline];
10840 /* 14.5.2.3 [temp.mem]
10842 A member function template shall not be virtual. */
10843 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10844 error_at (token->location, "templates may not be %<virtual%>");
10845 else if (decl_specs)
10846 ++decl_specs->specs[(int) ds_virtual];
10851 ++decl_specs->specs[(int) ds_explicit];
10858 /* Consume the token. */
10859 return cp_lexer_consume_token (parser->lexer)->u.value;
10862 /* Parse a linkage-specification.
10864 linkage-specification:
10865 extern string-literal { declaration-seq [opt] }
10866 extern string-literal declaration */
10869 cp_parser_linkage_specification (cp_parser* parser)
10873 /* Look for the `extern' keyword. */
10874 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10876 /* Look for the string-literal. */
10877 linkage = cp_parser_string_literal (parser, false, false);
10879 /* Transform the literal into an identifier. If the literal is a
10880 wide-character string, or contains embedded NULs, then we can't
10881 handle it as the user wants. */
10882 if (strlen (TREE_STRING_POINTER (linkage))
10883 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10885 cp_parser_error (parser, "invalid linkage-specification");
10886 /* Assume C++ linkage. */
10887 linkage = lang_name_cplusplus;
10890 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10892 /* We're now using the new linkage. */
10893 push_lang_context (linkage);
10895 /* If the next token is a `{', then we're using the first
10897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10899 /* Consume the `{' token. */
10900 cp_lexer_consume_token (parser->lexer);
10901 /* Parse the declarations. */
10902 cp_parser_declaration_seq_opt (parser);
10903 /* Look for the closing `}'. */
10904 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10906 /* Otherwise, there's just one declaration. */
10909 bool saved_in_unbraced_linkage_specification_p;
10911 saved_in_unbraced_linkage_specification_p
10912 = parser->in_unbraced_linkage_specification_p;
10913 parser->in_unbraced_linkage_specification_p = true;
10914 cp_parser_declaration (parser);
10915 parser->in_unbraced_linkage_specification_p
10916 = saved_in_unbraced_linkage_specification_p;
10919 /* We're done with the linkage-specification. */
10920 pop_lang_context ();
10923 /* Parse a static_assert-declaration.
10925 static_assert-declaration:
10926 static_assert ( constant-expression , string-literal ) ;
10928 If MEMBER_P, this static_assert is a class member. */
10931 cp_parser_static_assert(cp_parser *parser, bool member_p)
10936 location_t saved_loc;
10939 /* Peek at the `static_assert' token so we can keep track of exactly
10940 where the static assertion started. */
10941 token = cp_lexer_peek_token (parser->lexer);
10942 saved_loc = token->location;
10944 /* Look for the `static_assert' keyword. */
10945 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10949 /* We know we are in a static assertion; commit to any tentative
10951 if (cp_parser_parsing_tentatively (parser))
10952 cp_parser_commit_to_tentative_parse (parser);
10954 /* Parse the `(' starting the static assertion condition. */
10955 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10957 /* Parse the constant-expression. Allow a non-constant expression
10958 here in order to give better diagnostics in finish_static_assert. */
10960 cp_parser_constant_expression (parser,
10961 /*allow_non_constant_p=*/true,
10962 /*non_constant_p=*/&dummy);
10964 /* Parse the separating `,'. */
10965 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10967 /* Parse the string-literal message. */
10968 message = cp_parser_string_literal (parser,
10969 /*translate=*/false,
10972 /* A `)' completes the static assertion. */
10973 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10974 cp_parser_skip_to_closing_parenthesis (parser,
10975 /*recovering=*/true,
10976 /*or_comma=*/false,
10977 /*consume_paren=*/true);
10979 /* A semicolon terminates the declaration. */
10980 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10982 /* Complete the static assertion, which may mean either processing
10983 the static assert now or saving it for template instantiation. */
10984 finish_static_assert (condition, message, saved_loc, member_p);
10987 /* Parse a `decltype' type. Returns the type.
10989 simple-type-specifier:
10990 decltype ( expression ) */
10993 cp_parser_decltype (cp_parser *parser)
10996 bool id_expression_or_member_access_p = false;
10997 const char *saved_message;
10998 bool saved_integral_constant_expression_p;
10999 bool saved_non_integral_constant_expression_p;
11000 cp_token *id_expr_start_token;
11001 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11003 if (start_token->type == CPP_DECLTYPE)
11005 /* Already parsed. */
11006 cp_lexer_consume_token (parser->lexer);
11007 return start_token->u.value;
11010 /* Look for the `decltype' token. */
11011 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11012 return error_mark_node;
11014 /* Types cannot be defined in a `decltype' expression. Save away the
11016 saved_message = parser->type_definition_forbidden_message;
11018 /* And create the new one. */
11019 parser->type_definition_forbidden_message
11020 = G_("types may not be defined in %<decltype%> expressions");
11022 /* The restrictions on constant-expressions do not apply inside
11023 decltype expressions. */
11024 saved_integral_constant_expression_p
11025 = parser->integral_constant_expression_p;
11026 saved_non_integral_constant_expression_p
11027 = parser->non_integral_constant_expression_p;
11028 parser->integral_constant_expression_p = false;
11030 /* Do not actually evaluate the expression. */
11031 ++cp_unevaluated_operand;
11033 /* Do not warn about problems with the expression. */
11034 ++c_inhibit_evaluation_warnings;
11036 /* Parse the opening `('. */
11037 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11038 return error_mark_node;
11040 /* First, try parsing an id-expression. */
11041 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11042 cp_parser_parse_tentatively (parser);
11043 expr = cp_parser_id_expression (parser,
11044 /*template_keyword_p=*/false,
11045 /*check_dependency_p=*/true,
11046 /*template_p=*/NULL,
11047 /*declarator_p=*/false,
11048 /*optional_p=*/false);
11050 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11052 bool non_integral_constant_expression_p = false;
11053 tree id_expression = expr;
11055 const char *error_msg;
11057 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11058 /* Lookup the name we got back from the id-expression. */
11059 expr = cp_parser_lookup_name (parser, expr,
11061 /*is_template=*/false,
11062 /*is_namespace=*/false,
11063 /*check_dependency=*/true,
11064 /*ambiguous_decls=*/NULL,
11065 id_expr_start_token->location);
11068 && expr != error_mark_node
11069 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11070 && TREE_CODE (expr) != TYPE_DECL
11071 && (TREE_CODE (expr) != BIT_NOT_EXPR
11072 || !TYPE_P (TREE_OPERAND (expr, 0)))
11073 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11075 /* Complete lookup of the id-expression. */
11076 expr = (finish_id_expression
11077 (id_expression, expr, parser->scope, &idk,
11078 /*integral_constant_expression_p=*/false,
11079 /*allow_non_integral_constant_expression_p=*/true,
11080 &non_integral_constant_expression_p,
11081 /*template_p=*/false,
11083 /*address_p=*/false,
11084 /*template_arg_p=*/false,
11086 id_expr_start_token->location));
11088 if (expr == error_mark_node)
11089 /* We found an id-expression, but it was something that we
11090 should not have found. This is an error, not something
11091 we can recover from, so note that we found an
11092 id-expression and we'll recover as gracefully as
11094 id_expression_or_member_access_p = true;
11098 && expr != error_mark_node
11099 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11100 /* We have an id-expression. */
11101 id_expression_or_member_access_p = true;
11104 if (!id_expression_or_member_access_p)
11106 /* Abort the id-expression parse. */
11107 cp_parser_abort_tentative_parse (parser);
11109 /* Parsing tentatively, again. */
11110 cp_parser_parse_tentatively (parser);
11112 /* Parse a class member access. */
11113 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11115 /*member_access_only_p=*/true, NULL);
11118 && expr != error_mark_node
11119 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11120 /* We have an id-expression. */
11121 id_expression_or_member_access_p = true;
11124 if (id_expression_or_member_access_p)
11125 /* We have parsed the complete id-expression or member access. */
11126 cp_parser_parse_definitely (parser);
11129 bool saved_greater_than_is_operator_p;
11131 /* Abort our attempt to parse an id-expression or member access
11133 cp_parser_abort_tentative_parse (parser);
11135 /* Within a parenthesized expression, a `>' token is always
11136 the greater-than operator. */
11137 saved_greater_than_is_operator_p
11138 = parser->greater_than_is_operator_p;
11139 parser->greater_than_is_operator_p = true;
11141 /* Parse a full expression. */
11142 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11144 /* The `>' token might be the end of a template-id or
11145 template-parameter-list now. */
11146 parser->greater_than_is_operator_p
11147 = saved_greater_than_is_operator_p;
11150 /* Go back to evaluating expressions. */
11151 --cp_unevaluated_operand;
11152 --c_inhibit_evaluation_warnings;
11154 /* Restore the old message and the integral constant expression
11156 parser->type_definition_forbidden_message = saved_message;
11157 parser->integral_constant_expression_p
11158 = saved_integral_constant_expression_p;
11159 parser->non_integral_constant_expression_p
11160 = saved_non_integral_constant_expression_p;
11162 /* Parse to the closing `)'. */
11163 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11165 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11166 /*consume_paren=*/true);
11167 return error_mark_node;
11170 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11171 tf_warning_or_error);
11173 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11175 start_token->type = CPP_DECLTYPE;
11176 start_token->u.value = expr;
11177 start_token->keyword = RID_MAX;
11178 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11183 /* Special member functions [gram.special] */
11185 /* Parse a conversion-function-id.
11187 conversion-function-id:
11188 operator conversion-type-id
11190 Returns an IDENTIFIER_NODE representing the operator. */
11193 cp_parser_conversion_function_id (cp_parser* parser)
11197 tree saved_qualifying_scope;
11198 tree saved_object_scope;
11199 tree pushed_scope = NULL_TREE;
11201 /* Look for the `operator' token. */
11202 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11203 return error_mark_node;
11204 /* When we parse the conversion-type-id, the current scope will be
11205 reset. However, we need that information in able to look up the
11206 conversion function later, so we save it here. */
11207 saved_scope = parser->scope;
11208 saved_qualifying_scope = parser->qualifying_scope;
11209 saved_object_scope = parser->object_scope;
11210 /* We must enter the scope of the class so that the names of
11211 entities declared within the class are available in the
11212 conversion-type-id. For example, consider:
11219 S::operator I() { ... }
11221 In order to see that `I' is a type-name in the definition, we
11222 must be in the scope of `S'. */
11224 pushed_scope = push_scope (saved_scope);
11225 /* Parse the conversion-type-id. */
11226 type = cp_parser_conversion_type_id (parser);
11227 /* Leave the scope of the class, if any. */
11229 pop_scope (pushed_scope);
11230 /* Restore the saved scope. */
11231 parser->scope = saved_scope;
11232 parser->qualifying_scope = saved_qualifying_scope;
11233 parser->object_scope = saved_object_scope;
11234 /* If the TYPE is invalid, indicate failure. */
11235 if (type == error_mark_node)
11236 return error_mark_node;
11237 return mangle_conv_op_name_for_type (type);
11240 /* Parse a conversion-type-id:
11242 conversion-type-id:
11243 type-specifier-seq conversion-declarator [opt]
11245 Returns the TYPE specified. */
11248 cp_parser_conversion_type_id (cp_parser* parser)
11251 cp_decl_specifier_seq type_specifiers;
11252 cp_declarator *declarator;
11253 tree type_specified;
11255 /* Parse the attributes. */
11256 attributes = cp_parser_attributes_opt (parser);
11257 /* Parse the type-specifiers. */
11258 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11259 /*is_trailing_return=*/false,
11261 /* If that didn't work, stop. */
11262 if (type_specifiers.type == error_mark_node)
11263 return error_mark_node;
11264 /* Parse the conversion-declarator. */
11265 declarator = cp_parser_conversion_declarator_opt (parser);
11267 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11268 /*initialized=*/0, &attributes);
11270 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11272 /* Don't give this error when parsing tentatively. This happens to
11273 work because we always parse this definitively once. */
11274 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11275 && type_uses_auto (type_specified))
11277 error ("invalid use of %<auto%> in conversion operator");
11278 return error_mark_node;
11281 return type_specified;
11284 /* Parse an (optional) conversion-declarator.
11286 conversion-declarator:
11287 ptr-operator conversion-declarator [opt]
11291 static cp_declarator *
11292 cp_parser_conversion_declarator_opt (cp_parser* parser)
11294 enum tree_code code;
11296 cp_cv_quals cv_quals;
11298 /* We don't know if there's a ptr-operator next, or not. */
11299 cp_parser_parse_tentatively (parser);
11300 /* Try the ptr-operator. */
11301 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11302 /* If it worked, look for more conversion-declarators. */
11303 if (cp_parser_parse_definitely (parser))
11305 cp_declarator *declarator;
11307 /* Parse another optional declarator. */
11308 declarator = cp_parser_conversion_declarator_opt (parser);
11310 return cp_parser_make_indirect_declarator
11311 (code, class_type, cv_quals, declarator);
11317 /* Parse an (optional) ctor-initializer.
11320 : mem-initializer-list
11322 Returns TRUE iff the ctor-initializer was actually present. */
11325 cp_parser_ctor_initializer_opt (cp_parser* parser)
11327 /* If the next token is not a `:', then there is no
11328 ctor-initializer. */
11329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11331 /* Do default initialization of any bases and members. */
11332 if (DECL_CONSTRUCTOR_P (current_function_decl))
11333 finish_mem_initializers (NULL_TREE);
11338 /* Consume the `:' token. */
11339 cp_lexer_consume_token (parser->lexer);
11340 /* And the mem-initializer-list. */
11341 cp_parser_mem_initializer_list (parser);
11346 /* Parse a mem-initializer-list.
11348 mem-initializer-list:
11349 mem-initializer ... [opt]
11350 mem-initializer ... [opt] , mem-initializer-list */
11353 cp_parser_mem_initializer_list (cp_parser* parser)
11355 tree mem_initializer_list = NULL_TREE;
11356 tree target_ctor = error_mark_node;
11357 cp_token *token = cp_lexer_peek_token (parser->lexer);
11359 /* Let the semantic analysis code know that we are starting the
11360 mem-initializer-list. */
11361 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11362 error_at (token->location,
11363 "only constructors take member initializers");
11365 /* Loop through the list. */
11368 tree mem_initializer;
11370 token = cp_lexer_peek_token (parser->lexer);
11371 /* Parse the mem-initializer. */
11372 mem_initializer = cp_parser_mem_initializer (parser);
11373 /* If the next token is a `...', we're expanding member initializers. */
11374 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11376 /* Consume the `...'. */
11377 cp_lexer_consume_token (parser->lexer);
11379 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11380 can be expanded but members cannot. */
11381 if (mem_initializer != error_mark_node
11382 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11384 error_at (token->location,
11385 "cannot expand initializer for member %<%D%>",
11386 TREE_PURPOSE (mem_initializer));
11387 mem_initializer = error_mark_node;
11390 /* Construct the pack expansion type. */
11391 if (mem_initializer != error_mark_node)
11392 mem_initializer = make_pack_expansion (mem_initializer);
11394 if (target_ctor != error_mark_node
11395 && mem_initializer != error_mark_node)
11397 error ("mem-initializer for %qD follows constructor delegation",
11398 TREE_PURPOSE (mem_initializer));
11399 mem_initializer = error_mark_node;
11401 /* Look for a target constructor. */
11402 if (mem_initializer != error_mark_node
11403 && TYPE_P (TREE_PURPOSE (mem_initializer))
11404 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11406 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11407 if (mem_initializer_list)
11409 error ("constructor delegation follows mem-initializer for %qD",
11410 TREE_PURPOSE (mem_initializer_list));
11411 mem_initializer = error_mark_node;
11413 target_ctor = mem_initializer;
11415 /* Add it to the list, unless it was erroneous. */
11416 if (mem_initializer != error_mark_node)
11418 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11419 mem_initializer_list = mem_initializer;
11421 /* If the next token is not a `,', we're done. */
11422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11424 /* Consume the `,' token. */
11425 cp_lexer_consume_token (parser->lexer);
11428 /* Perform semantic analysis. */
11429 if (DECL_CONSTRUCTOR_P (current_function_decl))
11430 finish_mem_initializers (mem_initializer_list);
11433 /* Parse a mem-initializer.
11436 mem-initializer-id ( expression-list [opt] )
11437 mem-initializer-id braced-init-list
11442 ( expression-list [opt] )
11444 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11445 class) or FIELD_DECL (for a non-static data member) to initialize;
11446 the TREE_VALUE is the expression-list. An empty initialization
11447 list is represented by void_list_node. */
11450 cp_parser_mem_initializer (cp_parser* parser)
11452 tree mem_initializer_id;
11453 tree expression_list;
11455 cp_token *token = cp_lexer_peek_token (parser->lexer);
11457 /* Find out what is being initialized. */
11458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11460 permerror (token->location,
11461 "anachronistic old-style base class initializer");
11462 mem_initializer_id = NULL_TREE;
11466 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11467 if (mem_initializer_id == error_mark_node)
11468 return mem_initializer_id;
11470 member = expand_member_init (mem_initializer_id);
11471 if (member && !DECL_P (member))
11472 in_base_initializer = 1;
11474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11476 bool expr_non_constant_p;
11477 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11478 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11479 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11480 expression_list = build_tree_list (NULL_TREE, expression_list);
11485 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11487 /*allow_expansion_p=*/true,
11488 /*non_constant_p=*/NULL);
11490 return error_mark_node;
11491 expression_list = build_tree_list_vec (vec);
11492 release_tree_vector (vec);
11495 if (expression_list == error_mark_node)
11496 return error_mark_node;
11497 if (!expression_list)
11498 expression_list = void_type_node;
11500 in_base_initializer = 0;
11502 return member ? build_tree_list (member, expression_list) : error_mark_node;
11505 /* Parse a mem-initializer-id.
11507 mem-initializer-id:
11508 :: [opt] nested-name-specifier [opt] class-name
11511 Returns a TYPE indicating the class to be initializer for the first
11512 production. Returns an IDENTIFIER_NODE indicating the data member
11513 to be initialized for the second production. */
11516 cp_parser_mem_initializer_id (cp_parser* parser)
11518 bool global_scope_p;
11519 bool nested_name_specifier_p;
11520 bool template_p = false;
11523 cp_token *token = cp_lexer_peek_token (parser->lexer);
11525 /* `typename' is not allowed in this context ([temp.res]). */
11526 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11528 error_at (token->location,
11529 "keyword %<typename%> not allowed in this context (a qualified "
11530 "member initializer is implicitly a type)");
11531 cp_lexer_consume_token (parser->lexer);
11533 /* Look for the optional `::' operator. */
11535 = (cp_parser_global_scope_opt (parser,
11536 /*current_scope_valid_p=*/false)
11538 /* Look for the optional nested-name-specifier. The simplest way to
11543 The keyword `typename' is not permitted in a base-specifier or
11544 mem-initializer; in these contexts a qualified name that
11545 depends on a template-parameter is implicitly assumed to be a
11548 is to assume that we have seen the `typename' keyword at this
11550 nested_name_specifier_p
11551 = (cp_parser_nested_name_specifier_opt (parser,
11552 /*typename_keyword_p=*/true,
11553 /*check_dependency_p=*/true,
11555 /*is_declaration=*/true)
11557 if (nested_name_specifier_p)
11558 template_p = cp_parser_optional_template_keyword (parser);
11559 /* If there is a `::' operator or a nested-name-specifier, then we
11560 are definitely looking for a class-name. */
11561 if (global_scope_p || nested_name_specifier_p)
11562 return cp_parser_class_name (parser,
11563 /*typename_keyword_p=*/true,
11564 /*template_keyword_p=*/template_p,
11566 /*check_dependency_p=*/true,
11567 /*class_head_p=*/false,
11568 /*is_declaration=*/true);
11569 /* Otherwise, we could also be looking for an ordinary identifier. */
11570 cp_parser_parse_tentatively (parser);
11571 /* Try a class-name. */
11572 id = cp_parser_class_name (parser,
11573 /*typename_keyword_p=*/true,
11574 /*template_keyword_p=*/false,
11576 /*check_dependency_p=*/true,
11577 /*class_head_p=*/false,
11578 /*is_declaration=*/true);
11579 /* If we found one, we're done. */
11580 if (cp_parser_parse_definitely (parser))
11582 /* Otherwise, look for an ordinary identifier. */
11583 return cp_parser_identifier (parser);
11586 /* Overloading [gram.over] */
11588 /* Parse an operator-function-id.
11590 operator-function-id:
11593 Returns an IDENTIFIER_NODE for the operator which is a
11594 human-readable spelling of the identifier, e.g., `operator +'. */
11597 cp_parser_operator_function_id (cp_parser* parser)
11599 /* Look for the `operator' keyword. */
11600 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11601 return error_mark_node;
11602 /* And then the name of the operator itself. */
11603 return cp_parser_operator (parser);
11606 /* Return an identifier node for a user-defined literal operator.
11607 The suffix identifier is chained to the operator name identifier. */
11610 cp_literal_operator_id (const char* name)
11613 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11614 + strlen (name) + 10);
11615 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11616 identifier = get_identifier (buffer);
11617 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11622 /* Parse an operator.
11625 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11626 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11627 || ++ -- , ->* -> () []
11634 Returns an IDENTIFIER_NODE for the operator which is a
11635 human-readable spelling of the identifier, e.g., `operator +'. */
11638 cp_parser_operator (cp_parser* parser)
11640 tree id = NULL_TREE;
11643 /* Peek at the next token. */
11644 token = cp_lexer_peek_token (parser->lexer);
11645 /* Figure out which operator we have. */
11646 switch (token->type)
11652 /* The keyword should be either `new' or `delete'. */
11653 if (token->keyword == RID_NEW)
11655 else if (token->keyword == RID_DELETE)
11660 /* Consume the `new' or `delete' token. */
11661 cp_lexer_consume_token (parser->lexer);
11663 /* Peek at the next token. */
11664 token = cp_lexer_peek_token (parser->lexer);
11665 /* If it's a `[' token then this is the array variant of the
11667 if (token->type == CPP_OPEN_SQUARE)
11669 /* Consume the `[' token. */
11670 cp_lexer_consume_token (parser->lexer);
11671 /* Look for the `]' token. */
11672 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11673 id = ansi_opname (op == NEW_EXPR
11674 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11676 /* Otherwise, we have the non-array variant. */
11678 id = ansi_opname (op);
11684 id = ansi_opname (PLUS_EXPR);
11688 id = ansi_opname (MINUS_EXPR);
11692 id = ansi_opname (MULT_EXPR);
11696 id = ansi_opname (TRUNC_DIV_EXPR);
11700 id = ansi_opname (TRUNC_MOD_EXPR);
11704 id = ansi_opname (BIT_XOR_EXPR);
11708 id = ansi_opname (BIT_AND_EXPR);
11712 id = ansi_opname (BIT_IOR_EXPR);
11716 id = ansi_opname (BIT_NOT_EXPR);
11720 id = ansi_opname (TRUTH_NOT_EXPR);
11724 id = ansi_assopname (NOP_EXPR);
11728 id = ansi_opname (LT_EXPR);
11732 id = ansi_opname (GT_EXPR);
11736 id = ansi_assopname (PLUS_EXPR);
11740 id = ansi_assopname (MINUS_EXPR);
11744 id = ansi_assopname (MULT_EXPR);
11748 id = ansi_assopname (TRUNC_DIV_EXPR);
11752 id = ansi_assopname (TRUNC_MOD_EXPR);
11756 id = ansi_assopname (BIT_XOR_EXPR);
11760 id = ansi_assopname (BIT_AND_EXPR);
11764 id = ansi_assopname (BIT_IOR_EXPR);
11768 id = ansi_opname (LSHIFT_EXPR);
11772 id = ansi_opname (RSHIFT_EXPR);
11775 case CPP_LSHIFT_EQ:
11776 id = ansi_assopname (LSHIFT_EXPR);
11779 case CPP_RSHIFT_EQ:
11780 id = ansi_assopname (RSHIFT_EXPR);
11784 id = ansi_opname (EQ_EXPR);
11788 id = ansi_opname (NE_EXPR);
11792 id = ansi_opname (LE_EXPR);
11795 case CPP_GREATER_EQ:
11796 id = ansi_opname (GE_EXPR);
11800 id = ansi_opname (TRUTH_ANDIF_EXPR);
11804 id = ansi_opname (TRUTH_ORIF_EXPR);
11807 case CPP_PLUS_PLUS:
11808 id = ansi_opname (POSTINCREMENT_EXPR);
11811 case CPP_MINUS_MINUS:
11812 id = ansi_opname (PREDECREMENT_EXPR);
11816 id = ansi_opname (COMPOUND_EXPR);
11819 case CPP_DEREF_STAR:
11820 id = ansi_opname (MEMBER_REF);
11824 id = ansi_opname (COMPONENT_REF);
11827 case CPP_OPEN_PAREN:
11828 /* Consume the `('. */
11829 cp_lexer_consume_token (parser->lexer);
11830 /* Look for the matching `)'. */
11831 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11832 return ansi_opname (CALL_EXPR);
11834 case CPP_OPEN_SQUARE:
11835 /* Consume the `['. */
11836 cp_lexer_consume_token (parser->lexer);
11837 /* Look for the matching `]'. */
11838 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11839 return ansi_opname (ARRAY_REF);
11842 if (cxx_dialect == cxx98)
11843 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11844 if (TREE_STRING_LENGTH (token->u.value) > 2)
11846 error ("expected empty string after %<operator%> keyword");
11847 return error_mark_node;
11849 /* Consume the string. */
11850 cp_lexer_consume_token (parser->lexer);
11851 /* Look for the suffix identifier. */
11852 token = cp_lexer_peek_token (parser->lexer);
11853 if (token->type == CPP_NAME)
11855 id = cp_parser_identifier (parser);
11856 if (id != error_mark_node)
11858 const char *name = IDENTIFIER_POINTER (id);
11859 return cp_literal_operator_id (name);
11864 error ("expected suffix identifier");
11865 return error_mark_node;
11868 case CPP_STRING_USERDEF:
11869 error ("missing space between %<\"\"%> and suffix identifier");
11870 return error_mark_node;
11873 /* Anything else is an error. */
11877 /* If we have selected an identifier, we need to consume the
11880 cp_lexer_consume_token (parser->lexer);
11881 /* Otherwise, no valid operator name was present. */
11884 cp_parser_error (parser, "expected operator");
11885 id = error_mark_node;
11891 /* Parse a template-declaration.
11893 template-declaration:
11894 export [opt] template < template-parameter-list > declaration
11896 If MEMBER_P is TRUE, this template-declaration occurs within a
11899 The grammar rule given by the standard isn't correct. What
11900 is really meant is:
11902 template-declaration:
11903 export [opt] template-parameter-list-seq
11904 decl-specifier-seq [opt] init-declarator [opt] ;
11905 export [opt] template-parameter-list-seq
11906 function-definition
11908 template-parameter-list-seq:
11909 template-parameter-list-seq [opt]
11910 template < template-parameter-list > */
11913 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11915 /* Check for `export'. */
11916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11918 /* Consume the `export' token. */
11919 cp_lexer_consume_token (parser->lexer);
11920 /* Warn that we do not support `export'. */
11921 warning (0, "keyword %<export%> not implemented, and will be ignored");
11924 cp_parser_template_declaration_after_export (parser, member_p);
11927 /* Parse a template-parameter-list.
11929 template-parameter-list:
11931 template-parameter-list , template-parameter
11933 Returns a TREE_LIST. Each node represents a template parameter.
11934 The nodes are connected via their TREE_CHAINs. */
11937 cp_parser_template_parameter_list (cp_parser* parser)
11939 tree parameter_list = NULL_TREE;
11941 begin_template_parm_list ();
11943 /* The loop below parses the template parms. We first need to know
11944 the total number of template parms to be able to compute proper
11945 canonical types of each dependent type. So after the loop, when
11946 we know the total number of template parms,
11947 end_template_parm_list computes the proper canonical types and
11948 fixes up the dependent types accordingly. */
11953 bool is_parameter_pack;
11954 location_t parm_loc;
11956 /* Parse the template-parameter. */
11957 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11958 parameter = cp_parser_template_parameter (parser,
11960 &is_parameter_pack);
11961 /* Add it to the list. */
11962 if (parameter != error_mark_node)
11963 parameter_list = process_template_parm (parameter_list,
11971 tree err_parm = build_tree_list (parameter, parameter);
11972 parameter_list = chainon (parameter_list, err_parm);
11975 /* If the next token is not a `,', we're done. */
11976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11978 /* Otherwise, consume the `,' token. */
11979 cp_lexer_consume_token (parser->lexer);
11982 return end_template_parm_list (parameter_list);
11985 /* Parse a template-parameter.
11987 template-parameter:
11989 parameter-declaration
11991 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11992 the parameter. The TREE_PURPOSE is the default value, if any.
11993 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11994 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11995 set to true iff this parameter is a parameter pack. */
11998 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11999 bool *is_parameter_pack)
12002 cp_parameter_declarator *parameter_declarator;
12003 cp_declarator *id_declarator;
12006 /* Assume it is a type parameter or a template parameter. */
12007 *is_non_type = false;
12008 /* Assume it not a parameter pack. */
12009 *is_parameter_pack = false;
12010 /* Peek at the next token. */
12011 token = cp_lexer_peek_token (parser->lexer);
12012 /* If it is `class' or `template', we have a type-parameter. */
12013 if (token->keyword == RID_TEMPLATE)
12014 return cp_parser_type_parameter (parser, is_parameter_pack);
12015 /* If it is `class' or `typename' we do not know yet whether it is a
12016 type parameter or a non-type parameter. Consider:
12018 template <typename T, typename T::X X> ...
12022 template <class C, class D*> ...
12024 Here, the first parameter is a type parameter, and the second is
12025 a non-type parameter. We can tell by looking at the token after
12026 the identifier -- if it is a `,', `=', or `>' then we have a type
12028 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12030 /* Peek at the token after `class' or `typename'. */
12031 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12032 /* If it's an ellipsis, we have a template type parameter
12034 if (token->type == CPP_ELLIPSIS)
12035 return cp_parser_type_parameter (parser, is_parameter_pack);
12036 /* If it's an identifier, skip it. */
12037 if (token->type == CPP_NAME)
12038 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12039 /* Now, see if the token looks like the end of a template
12041 if (token->type == CPP_COMMA
12042 || token->type == CPP_EQ
12043 || token->type == CPP_GREATER)
12044 return cp_parser_type_parameter (parser, is_parameter_pack);
12047 /* Otherwise, it is a non-type parameter.
12051 When parsing a default template-argument for a non-type
12052 template-parameter, the first non-nested `>' is taken as the end
12053 of the template parameter-list rather than a greater-than
12055 *is_non_type = true;
12056 parameter_declarator
12057 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12058 /*parenthesized_p=*/NULL);
12060 /* If the parameter declaration is marked as a parameter pack, set
12061 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12062 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12064 if (parameter_declarator
12065 && parameter_declarator->declarator
12066 && parameter_declarator->declarator->parameter_pack_p)
12068 *is_parameter_pack = true;
12069 parameter_declarator->declarator->parameter_pack_p = false;
12072 /* If the next token is an ellipsis, and we don't already have it
12073 marked as a parameter pack, then we have a parameter pack (that
12074 has no declarator). */
12075 if (!*is_parameter_pack
12076 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12077 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12079 /* Consume the `...'. */
12080 cp_lexer_consume_token (parser->lexer);
12081 maybe_warn_variadic_templates ();
12083 *is_parameter_pack = true;
12085 /* We might end up with a pack expansion as the type of the non-type
12086 template parameter, in which case this is a non-type template
12088 else if (parameter_declarator
12089 && parameter_declarator->decl_specifiers.type
12090 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12092 *is_parameter_pack = true;
12093 parameter_declarator->decl_specifiers.type =
12094 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12097 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12099 /* Parameter packs cannot have default arguments. However, a
12100 user may try to do so, so we'll parse them and give an
12101 appropriate diagnostic here. */
12103 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12105 /* Find the name of the parameter pack. */
12106 id_declarator = parameter_declarator->declarator;
12107 while (id_declarator && id_declarator->kind != cdk_id)
12108 id_declarator = id_declarator->declarator;
12110 if (id_declarator && id_declarator->kind == cdk_id)
12111 error_at (start_token->location,
12112 "template parameter pack %qD cannot have a default argument",
12113 id_declarator->u.id.unqualified_name);
12115 error_at (start_token->location,
12116 "template parameter pack cannot have a default argument");
12118 /* Parse the default argument, but throw away the result. */
12119 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12122 parm = grokdeclarator (parameter_declarator->declarator,
12123 ¶meter_declarator->decl_specifiers,
12124 TPARM, /*initialized=*/0,
12125 /*attrlist=*/NULL);
12126 if (parm == error_mark_node)
12127 return error_mark_node;
12129 return build_tree_list (parameter_declarator->default_argument, parm);
12132 /* Parse a type-parameter.
12135 class identifier [opt]
12136 class identifier [opt] = type-id
12137 typename identifier [opt]
12138 typename identifier [opt] = type-id
12139 template < template-parameter-list > class identifier [opt]
12140 template < template-parameter-list > class identifier [opt]
12143 GNU Extension (variadic templates):
12146 class ... identifier [opt]
12147 typename ... identifier [opt]
12149 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12150 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12151 the declaration of the parameter.
12153 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12156 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12161 /* Look for a keyword to tell us what kind of parameter this is. */
12162 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12164 return error_mark_node;
12166 switch (token->keyword)
12172 tree default_argument;
12174 /* If the next token is an ellipsis, we have a template
12176 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12178 /* Consume the `...' token. */
12179 cp_lexer_consume_token (parser->lexer);
12180 maybe_warn_variadic_templates ();
12182 *is_parameter_pack = true;
12185 /* If the next token is an identifier, then it names the
12187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12188 identifier = cp_parser_identifier (parser);
12190 identifier = NULL_TREE;
12192 /* Create the parameter. */
12193 parameter = finish_template_type_parm (class_type_node, identifier);
12195 /* If the next token is an `=', we have a default argument. */
12196 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12198 /* Consume the `=' token. */
12199 cp_lexer_consume_token (parser->lexer);
12200 /* Parse the default-argument. */
12201 push_deferring_access_checks (dk_no_deferred);
12202 default_argument = cp_parser_type_id (parser);
12204 /* Template parameter packs cannot have default
12206 if (*is_parameter_pack)
12209 error_at (token->location,
12210 "template parameter pack %qD cannot have a "
12211 "default argument", identifier);
12213 error_at (token->location,
12214 "template parameter packs cannot have "
12215 "default arguments");
12216 default_argument = NULL_TREE;
12218 pop_deferring_access_checks ();
12221 default_argument = NULL_TREE;
12223 /* Create the combined representation of the parameter and the
12224 default argument. */
12225 parameter = build_tree_list (default_argument, parameter);
12232 tree default_argument;
12234 /* Look for the `<'. */
12235 cp_parser_require (parser, CPP_LESS, RT_LESS);
12236 /* Parse the template-parameter-list. */
12237 cp_parser_template_parameter_list (parser);
12238 /* Look for the `>'. */
12239 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12240 /* Look for the `class' keyword. */
12241 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12242 /* If the next token is an ellipsis, we have a template
12244 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12246 /* Consume the `...' token. */
12247 cp_lexer_consume_token (parser->lexer);
12248 maybe_warn_variadic_templates ();
12250 *is_parameter_pack = true;
12252 /* If the next token is an `=', then there is a
12253 default-argument. If the next token is a `>', we are at
12254 the end of the parameter-list. If the next token is a `,',
12255 then we are at the end of this parameter. */
12256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12257 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12258 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12260 identifier = cp_parser_identifier (parser);
12261 /* Treat invalid names as if the parameter were nameless. */
12262 if (identifier == error_mark_node)
12263 identifier = NULL_TREE;
12266 identifier = NULL_TREE;
12268 /* Create the template parameter. */
12269 parameter = finish_template_template_parm (class_type_node,
12272 /* If the next token is an `=', then there is a
12273 default-argument. */
12274 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12278 /* Consume the `='. */
12279 cp_lexer_consume_token (parser->lexer);
12280 /* Parse the id-expression. */
12281 push_deferring_access_checks (dk_no_deferred);
12282 /* save token before parsing the id-expression, for error
12284 token = cp_lexer_peek_token (parser->lexer);
12286 = cp_parser_id_expression (parser,
12287 /*template_keyword_p=*/false,
12288 /*check_dependency_p=*/true,
12289 /*template_p=*/&is_template,
12290 /*declarator_p=*/false,
12291 /*optional_p=*/false);
12292 if (TREE_CODE (default_argument) == TYPE_DECL)
12293 /* If the id-expression was a template-id that refers to
12294 a template-class, we already have the declaration here,
12295 so no further lookup is needed. */
12298 /* Look up the name. */
12300 = cp_parser_lookup_name (parser, default_argument,
12302 /*is_template=*/is_template,
12303 /*is_namespace=*/false,
12304 /*check_dependency=*/true,
12305 /*ambiguous_decls=*/NULL,
12307 /* See if the default argument is valid. */
12309 = check_template_template_default_arg (default_argument);
12311 /* Template parameter packs cannot have default
12313 if (*is_parameter_pack)
12316 error_at (token->location,
12317 "template parameter pack %qD cannot "
12318 "have a default argument",
12321 error_at (token->location, "template parameter packs cannot "
12322 "have default arguments");
12323 default_argument = NULL_TREE;
12325 pop_deferring_access_checks ();
12328 default_argument = NULL_TREE;
12330 /* Create the combined representation of the parameter and the
12331 default argument. */
12332 parameter = build_tree_list (default_argument, parameter);
12337 gcc_unreachable ();
12344 /* Parse a template-id.
12347 template-name < template-argument-list [opt] >
12349 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12350 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12351 returned. Otherwise, if the template-name names a function, or set
12352 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12353 names a class, returns a TYPE_DECL for the specialization.
12355 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12356 uninstantiated templates. */
12359 cp_parser_template_id (cp_parser *parser,
12360 bool template_keyword_p,
12361 bool check_dependency_p,
12362 bool is_declaration)
12368 cp_token_position start_of_id = 0;
12369 deferred_access_check *chk;
12370 VEC (deferred_access_check,gc) *access_check;
12371 cp_token *next_token = NULL, *next_token_2 = NULL;
12372 bool is_identifier;
12374 /* If the next token corresponds to a template-id, there is no need
12376 next_token = cp_lexer_peek_token (parser->lexer);
12377 if (next_token->type == CPP_TEMPLATE_ID)
12379 struct tree_check *check_value;
12381 /* Get the stored value. */
12382 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12383 /* Perform any access checks that were deferred. */
12384 access_check = check_value->checks;
12387 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12388 perform_or_defer_access_check (chk->binfo,
12392 /* Return the stored value. */
12393 return check_value->value;
12396 /* Avoid performing name lookup if there is no possibility of
12397 finding a template-id. */
12398 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12399 || (next_token->type == CPP_NAME
12400 && !cp_parser_nth_token_starts_template_argument_list_p
12403 cp_parser_error (parser, "expected template-id");
12404 return error_mark_node;
12407 /* Remember where the template-id starts. */
12408 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12409 start_of_id = cp_lexer_token_position (parser->lexer, false);
12411 push_deferring_access_checks (dk_deferred);
12413 /* Parse the template-name. */
12414 is_identifier = false;
12415 templ = cp_parser_template_name (parser, template_keyword_p,
12416 check_dependency_p,
12419 if (templ == error_mark_node || is_identifier)
12421 pop_deferring_access_checks ();
12425 /* If we find the sequence `[:' after a template-name, it's probably
12426 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12427 parse correctly the argument list. */
12428 next_token = cp_lexer_peek_token (parser->lexer);
12429 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12430 if (next_token->type == CPP_OPEN_SQUARE
12431 && next_token->flags & DIGRAPH
12432 && next_token_2->type == CPP_COLON
12433 && !(next_token_2->flags & PREV_WHITE))
12435 cp_parser_parse_tentatively (parser);
12436 /* Change `:' into `::'. */
12437 next_token_2->type = CPP_SCOPE;
12438 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12440 cp_lexer_consume_token (parser->lexer);
12442 /* Parse the arguments. */
12443 arguments = cp_parser_enclosed_template_argument_list (parser);
12444 if (!cp_parser_parse_definitely (parser))
12446 /* If we couldn't parse an argument list, then we revert our changes
12447 and return simply an error. Maybe this is not a template-id
12449 next_token_2->type = CPP_COLON;
12450 cp_parser_error (parser, "expected %<<%>");
12451 pop_deferring_access_checks ();
12452 return error_mark_node;
12454 /* Otherwise, emit an error about the invalid digraph, but continue
12455 parsing because we got our argument list. */
12456 if (permerror (next_token->location,
12457 "%<<::%> cannot begin a template-argument list"))
12459 static bool hint = false;
12460 inform (next_token->location,
12461 "%<<:%> is an alternate spelling for %<[%>."
12462 " Insert whitespace between %<<%> and %<::%>");
12463 if (!hint && !flag_permissive)
12465 inform (next_token->location, "(if you use %<-fpermissive%>"
12466 " G++ will accept your code)");
12473 /* Look for the `<' that starts the template-argument-list. */
12474 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12476 pop_deferring_access_checks ();
12477 return error_mark_node;
12479 /* Parse the arguments. */
12480 arguments = cp_parser_enclosed_template_argument_list (parser);
12483 /* Build a representation of the specialization. */
12484 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12485 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12486 else if (DECL_TYPE_TEMPLATE_P (templ)
12487 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12489 bool entering_scope;
12490 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12491 template (rather than some instantiation thereof) only if
12492 is not nested within some other construct. For example, in
12493 "template <typename T> void f(T) { A<T>::", A<T> is just an
12494 instantiation of A. */
12495 entering_scope = (template_parm_scope_p ()
12496 && cp_lexer_next_token_is (parser->lexer,
12499 = finish_template_type (templ, arguments, entering_scope);
12503 /* If it's not a class-template or a template-template, it should be
12504 a function-template. */
12505 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12506 || TREE_CODE (templ) == OVERLOAD
12507 || BASELINK_P (templ)));
12509 template_id = lookup_template_function (templ, arguments);
12512 /* If parsing tentatively, replace the sequence of tokens that makes
12513 up the template-id with a CPP_TEMPLATE_ID token. That way,
12514 should we re-parse the token stream, we will not have to repeat
12515 the effort required to do the parse, nor will we issue duplicate
12516 error messages about problems during instantiation of the
12520 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12522 /* Reset the contents of the START_OF_ID token. */
12523 token->type = CPP_TEMPLATE_ID;
12524 /* Retrieve any deferred checks. Do not pop this access checks yet
12525 so the memory will not be reclaimed during token replacing below. */
12526 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12527 token->u.tree_check_value->value = template_id;
12528 token->u.tree_check_value->checks = get_deferred_access_checks ();
12529 token->keyword = RID_MAX;
12531 /* Purge all subsequent tokens. */
12532 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12534 /* ??? Can we actually assume that, if template_id ==
12535 error_mark_node, we will have issued a diagnostic to the
12536 user, as opposed to simply marking the tentative parse as
12538 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12539 error_at (token->location, "parse error in template argument list");
12542 pop_deferring_access_checks ();
12543 return template_id;
12546 /* Parse a template-name.
12551 The standard should actually say:
12555 operator-function-id
12557 A defect report has been filed about this issue.
12559 A conversion-function-id cannot be a template name because they cannot
12560 be part of a template-id. In fact, looking at this code:
12562 a.operator K<int>()
12564 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12565 It is impossible to call a templated conversion-function-id with an
12566 explicit argument list, since the only allowed template parameter is
12567 the type to which it is converting.
12569 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12570 `template' keyword, in a construction like:
12574 In that case `f' is taken to be a template-name, even though there
12575 is no way of knowing for sure.
12577 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12578 name refers to a set of overloaded functions, at least one of which
12579 is a template, or an IDENTIFIER_NODE with the name of the template,
12580 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12581 names are looked up inside uninstantiated templates. */
12584 cp_parser_template_name (cp_parser* parser,
12585 bool template_keyword_p,
12586 bool check_dependency_p,
12587 bool is_declaration,
12588 bool *is_identifier)
12593 cp_token *token = cp_lexer_peek_token (parser->lexer);
12595 /* If the next token is `operator', then we have either an
12596 operator-function-id or a conversion-function-id. */
12597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12599 /* We don't know whether we're looking at an
12600 operator-function-id or a conversion-function-id. */
12601 cp_parser_parse_tentatively (parser);
12602 /* Try an operator-function-id. */
12603 identifier = cp_parser_operator_function_id (parser);
12604 /* If that didn't work, try a conversion-function-id. */
12605 if (!cp_parser_parse_definitely (parser))
12607 cp_parser_error (parser, "expected template-name");
12608 return error_mark_node;
12611 /* Look for the identifier. */
12613 identifier = cp_parser_identifier (parser);
12615 /* If we didn't find an identifier, we don't have a template-id. */
12616 if (identifier == error_mark_node)
12617 return error_mark_node;
12619 /* If the name immediately followed the `template' keyword, then it
12620 is a template-name. However, if the next token is not `<', then
12621 we do not treat it as a template-name, since it is not being used
12622 as part of a template-id. This enables us to handle constructs
12625 template <typename T> struct S { S(); };
12626 template <typename T> S<T>::S();
12628 correctly. We would treat `S' as a template -- if it were `S<T>'
12629 -- but we do not if there is no `<'. */
12631 if (processing_template_decl
12632 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12634 /* In a declaration, in a dependent context, we pretend that the
12635 "template" keyword was present in order to improve error
12636 recovery. For example, given:
12638 template <typename T> void f(T::X<int>);
12640 we want to treat "X<int>" as a template-id. */
12642 && !template_keyword_p
12643 && parser->scope && TYPE_P (parser->scope)
12644 && check_dependency_p
12645 && dependent_scope_p (parser->scope)
12646 /* Do not do this for dtors (or ctors), since they never
12647 need the template keyword before their name. */
12648 && !constructor_name_p (identifier, parser->scope))
12650 cp_token_position start = 0;
12652 /* Explain what went wrong. */
12653 error_at (token->location, "non-template %qD used as template",
12655 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12656 parser->scope, identifier);
12657 /* If parsing tentatively, find the location of the "<" token. */
12658 if (cp_parser_simulate_error (parser))
12659 start = cp_lexer_token_position (parser->lexer, true);
12660 /* Parse the template arguments so that we can issue error
12661 messages about them. */
12662 cp_lexer_consume_token (parser->lexer);
12663 cp_parser_enclosed_template_argument_list (parser);
12664 /* Skip tokens until we find a good place from which to
12665 continue parsing. */
12666 cp_parser_skip_to_closing_parenthesis (parser,
12667 /*recovering=*/true,
12669 /*consume_paren=*/false);
12670 /* If parsing tentatively, permanently remove the
12671 template argument list. That will prevent duplicate
12672 error messages from being issued about the missing
12673 "template" keyword. */
12675 cp_lexer_purge_tokens_after (parser->lexer, start);
12677 *is_identifier = true;
12681 /* If the "template" keyword is present, then there is generally
12682 no point in doing name-lookup, so we just return IDENTIFIER.
12683 But, if the qualifying scope is non-dependent then we can
12684 (and must) do name-lookup normally. */
12685 if (template_keyword_p
12687 || (TYPE_P (parser->scope)
12688 && dependent_type_p (parser->scope))))
12692 /* Look up the name. */
12693 decl = cp_parser_lookup_name (parser, identifier,
12695 /*is_template=*/true,
12696 /*is_namespace=*/false,
12697 check_dependency_p,
12698 /*ambiguous_decls=*/NULL,
12701 /* If DECL is a template, then the name was a template-name. */
12702 if (TREE_CODE (decl) == TEMPLATE_DECL)
12706 tree fn = NULL_TREE;
12708 /* The standard does not explicitly indicate whether a name that
12709 names a set of overloaded declarations, some of which are
12710 templates, is a template-name. However, such a name should
12711 be a template-name; otherwise, there is no way to form a
12712 template-id for the overloaded templates. */
12713 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12714 if (TREE_CODE (fns) == OVERLOAD)
12715 for (fn = fns; fn; fn = OVL_NEXT (fn))
12716 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12721 /* The name does not name a template. */
12722 cp_parser_error (parser, "expected template-name");
12723 return error_mark_node;
12727 /* If DECL is dependent, and refers to a function, then just return
12728 its name; we will look it up again during template instantiation. */
12729 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12731 tree scope = ovl_scope (decl);
12732 if (TYPE_P (scope) && dependent_type_p (scope))
12739 /* Parse a template-argument-list.
12741 template-argument-list:
12742 template-argument ... [opt]
12743 template-argument-list , template-argument ... [opt]
12745 Returns a TREE_VEC containing the arguments. */
12748 cp_parser_template_argument_list (cp_parser* parser)
12750 tree fixed_args[10];
12751 unsigned n_args = 0;
12752 unsigned alloced = 10;
12753 tree *arg_ary = fixed_args;
12755 bool saved_in_template_argument_list_p;
12757 bool saved_non_ice_p;
12759 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12760 parser->in_template_argument_list_p = true;
12761 /* Even if the template-id appears in an integral
12762 constant-expression, the contents of the argument list do
12764 saved_ice_p = parser->integral_constant_expression_p;
12765 parser->integral_constant_expression_p = false;
12766 saved_non_ice_p = parser->non_integral_constant_expression_p;
12767 parser->non_integral_constant_expression_p = false;
12769 /* Parse the arguments. */
12775 /* Consume the comma. */
12776 cp_lexer_consume_token (parser->lexer);
12778 /* Parse the template-argument. */
12779 argument = cp_parser_template_argument (parser);
12781 /* If the next token is an ellipsis, we're expanding a template
12783 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12785 if (argument == error_mark_node)
12787 cp_token *token = cp_lexer_peek_token (parser->lexer);
12788 error_at (token->location,
12789 "expected parameter pack before %<...%>");
12791 /* Consume the `...' token. */
12792 cp_lexer_consume_token (parser->lexer);
12794 /* Make the argument into a TYPE_PACK_EXPANSION or
12795 EXPR_PACK_EXPANSION. */
12796 argument = make_pack_expansion (argument);
12799 if (n_args == alloced)
12803 if (arg_ary == fixed_args)
12805 arg_ary = XNEWVEC (tree, alloced);
12806 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12809 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12811 arg_ary[n_args++] = argument;
12813 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12815 vec = make_tree_vec (n_args);
12818 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12820 if (arg_ary != fixed_args)
12822 parser->non_integral_constant_expression_p = saved_non_ice_p;
12823 parser->integral_constant_expression_p = saved_ice_p;
12824 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12825 #ifdef ENABLE_CHECKING
12826 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12831 /* Parse a template-argument.
12834 assignment-expression
12838 The representation is that of an assignment-expression, type-id, or
12839 id-expression -- except that the qualified id-expression is
12840 evaluated, so that the value returned is either a DECL or an
12843 Although the standard says "assignment-expression", it forbids
12844 throw-expressions or assignments in the template argument.
12845 Therefore, we use "conditional-expression" instead. */
12848 cp_parser_template_argument (cp_parser* parser)
12853 bool maybe_type_id = false;
12854 cp_token *token = NULL, *argument_start_token = NULL;
12857 /* There's really no way to know what we're looking at, so we just
12858 try each alternative in order.
12862 In a template-argument, an ambiguity between a type-id and an
12863 expression is resolved to a type-id, regardless of the form of
12864 the corresponding template-parameter.
12866 Therefore, we try a type-id first. */
12867 cp_parser_parse_tentatively (parser);
12868 argument = cp_parser_template_type_arg (parser);
12869 /* If there was no error parsing the type-id but the next token is a
12870 '>>', our behavior depends on which dialect of C++ we're
12871 parsing. In C++98, we probably found a typo for '> >'. But there
12872 are type-id which are also valid expressions. For instance:
12874 struct X { int operator >> (int); };
12875 template <int V> struct Foo {};
12878 Here 'X()' is a valid type-id of a function type, but the user just
12879 wanted to write the expression "X() >> 5". Thus, we remember that we
12880 found a valid type-id, but we still try to parse the argument as an
12881 expression to see what happens.
12883 In C++0x, the '>>' will be considered two separate '>'
12885 if (!cp_parser_error_occurred (parser)
12886 && cxx_dialect == cxx98
12887 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12889 maybe_type_id = true;
12890 cp_parser_abort_tentative_parse (parser);
12894 /* If the next token isn't a `,' or a `>', then this argument wasn't
12895 really finished. This means that the argument is not a valid
12897 if (!cp_parser_next_token_ends_template_argument_p (parser))
12898 cp_parser_error (parser, "expected template-argument");
12899 /* If that worked, we're done. */
12900 if (cp_parser_parse_definitely (parser))
12903 /* We're still not sure what the argument will be. */
12904 cp_parser_parse_tentatively (parser);
12905 /* Try a template. */
12906 argument_start_token = cp_lexer_peek_token (parser->lexer);
12907 argument = cp_parser_id_expression (parser,
12908 /*template_keyword_p=*/false,
12909 /*check_dependency_p=*/true,
12911 /*declarator_p=*/false,
12912 /*optional_p=*/false);
12913 /* If the next token isn't a `,' or a `>', then this argument wasn't
12914 really finished. */
12915 if (!cp_parser_next_token_ends_template_argument_p (parser))
12916 cp_parser_error (parser, "expected template-argument");
12917 if (!cp_parser_error_occurred (parser))
12919 /* Figure out what is being referred to. If the id-expression
12920 was for a class template specialization, then we will have a
12921 TYPE_DECL at this point. There is no need to do name lookup
12922 at this point in that case. */
12923 if (TREE_CODE (argument) != TYPE_DECL)
12924 argument = cp_parser_lookup_name (parser, argument,
12926 /*is_template=*/template_p,
12927 /*is_namespace=*/false,
12928 /*check_dependency=*/true,
12929 /*ambiguous_decls=*/NULL,
12930 argument_start_token->location);
12931 if (TREE_CODE (argument) != TEMPLATE_DECL
12932 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12933 cp_parser_error (parser, "expected template-name");
12935 if (cp_parser_parse_definitely (parser))
12937 /* It must be a non-type argument. There permitted cases are given
12938 in [temp.arg.nontype]:
12940 -- an integral constant-expression of integral or enumeration
12943 -- the name of a non-type template-parameter; or
12945 -- the name of an object or function with external linkage...
12947 -- the address of an object or function with external linkage...
12949 -- a pointer to member... */
12950 /* Look for a non-type template parameter. */
12951 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12953 cp_parser_parse_tentatively (parser);
12954 argument = cp_parser_primary_expression (parser,
12955 /*address_p=*/false,
12957 /*template_arg_p=*/true,
12959 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12960 || !cp_parser_next_token_ends_template_argument_p (parser))
12961 cp_parser_simulate_error (parser);
12962 if (cp_parser_parse_definitely (parser))
12966 /* If the next token is "&", the argument must be the address of an
12967 object or function with external linkage. */
12968 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12970 cp_lexer_consume_token (parser->lexer);
12971 /* See if we might have an id-expression. */
12972 token = cp_lexer_peek_token (parser->lexer);
12973 if (token->type == CPP_NAME
12974 || token->keyword == RID_OPERATOR
12975 || token->type == CPP_SCOPE
12976 || token->type == CPP_TEMPLATE_ID
12977 || token->type == CPP_NESTED_NAME_SPECIFIER)
12979 cp_parser_parse_tentatively (parser);
12980 argument = cp_parser_primary_expression (parser,
12983 /*template_arg_p=*/true,
12985 if (cp_parser_error_occurred (parser)
12986 || !cp_parser_next_token_ends_template_argument_p (parser))
12987 cp_parser_abort_tentative_parse (parser);
12992 if (TREE_CODE (argument) == INDIRECT_REF)
12994 gcc_assert (REFERENCE_REF_P (argument));
12995 argument = TREE_OPERAND (argument, 0);
12998 /* If we're in a template, we represent a qualified-id referring
12999 to a static data member as a SCOPE_REF even if the scope isn't
13000 dependent so that we can check access control later. */
13002 if (TREE_CODE (probe) == SCOPE_REF)
13003 probe = TREE_OPERAND (probe, 1);
13004 if (TREE_CODE (probe) == VAR_DECL)
13006 /* A variable without external linkage might still be a
13007 valid constant-expression, so no error is issued here
13008 if the external-linkage check fails. */
13009 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13010 cp_parser_simulate_error (parser);
13012 else if (is_overloaded_fn (argument))
13013 /* All overloaded functions are allowed; if the external
13014 linkage test does not pass, an error will be issued
13018 && (TREE_CODE (argument) == OFFSET_REF
13019 || TREE_CODE (argument) == SCOPE_REF))
13020 /* A pointer-to-member. */
13022 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13025 cp_parser_simulate_error (parser);
13027 if (cp_parser_parse_definitely (parser))
13030 argument = build_x_unary_op (ADDR_EXPR, argument,
13031 tf_warning_or_error);
13036 /* If the argument started with "&", there are no other valid
13037 alternatives at this point. */
13040 cp_parser_error (parser, "invalid non-type template argument");
13041 return error_mark_node;
13044 /* If the argument wasn't successfully parsed as a type-id followed
13045 by '>>', the argument can only be a constant expression now.
13046 Otherwise, we try parsing the constant-expression tentatively,
13047 because the argument could really be a type-id. */
13049 cp_parser_parse_tentatively (parser);
13050 argument = cp_parser_constant_expression (parser,
13051 /*allow_non_constant_p=*/false,
13052 /*non_constant_p=*/NULL);
13053 argument = fold_non_dependent_expr (argument);
13054 if (!maybe_type_id)
13056 if (!cp_parser_next_token_ends_template_argument_p (parser))
13057 cp_parser_error (parser, "expected template-argument");
13058 if (cp_parser_parse_definitely (parser))
13060 /* We did our best to parse the argument as a non type-id, but that
13061 was the only alternative that matched (albeit with a '>' after
13062 it). We can assume it's just a typo from the user, and a
13063 diagnostic will then be issued. */
13064 return cp_parser_template_type_arg (parser);
13067 /* Parse an explicit-instantiation.
13069 explicit-instantiation:
13070 template declaration
13072 Although the standard says `declaration', what it really means is:
13074 explicit-instantiation:
13075 template decl-specifier-seq [opt] declarator [opt] ;
13077 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13078 supposed to be allowed. A defect report has been filed about this
13083 explicit-instantiation:
13084 storage-class-specifier template
13085 decl-specifier-seq [opt] declarator [opt] ;
13086 function-specifier template
13087 decl-specifier-seq [opt] declarator [opt] ; */
13090 cp_parser_explicit_instantiation (cp_parser* parser)
13092 int declares_class_or_enum;
13093 cp_decl_specifier_seq decl_specifiers;
13094 tree extension_specifier = NULL_TREE;
13096 timevar_push (TV_TEMPLATE_INST);
13098 /* Look for an (optional) storage-class-specifier or
13099 function-specifier. */
13100 if (cp_parser_allow_gnu_extensions_p (parser))
13102 extension_specifier
13103 = cp_parser_storage_class_specifier_opt (parser);
13104 if (!extension_specifier)
13105 extension_specifier
13106 = cp_parser_function_specifier_opt (parser,
13107 /*decl_specs=*/NULL);
13110 /* Look for the `template' keyword. */
13111 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13112 /* Let the front end know that we are processing an explicit
13114 begin_explicit_instantiation ();
13115 /* [temp.explicit] says that we are supposed to ignore access
13116 control while processing explicit instantiation directives. */
13117 push_deferring_access_checks (dk_no_check);
13118 /* Parse a decl-specifier-seq. */
13119 cp_parser_decl_specifier_seq (parser,
13120 CP_PARSER_FLAGS_OPTIONAL,
13122 &declares_class_or_enum);
13123 /* If there was exactly one decl-specifier, and it declared a class,
13124 and there's no declarator, then we have an explicit type
13126 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13130 type = check_tag_decl (&decl_specifiers);
13131 /* Turn access control back on for names used during
13132 template instantiation. */
13133 pop_deferring_access_checks ();
13135 do_type_instantiation (type, extension_specifier,
13136 /*complain=*/tf_error);
13140 cp_declarator *declarator;
13143 /* Parse the declarator. */
13145 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13146 /*ctor_dtor_or_conv_p=*/NULL,
13147 /*parenthesized_p=*/NULL,
13148 /*member_p=*/false);
13149 if (declares_class_or_enum & 2)
13150 cp_parser_check_for_definition_in_return_type (declarator,
13151 decl_specifiers.type,
13152 decl_specifiers.type_location);
13153 if (declarator != cp_error_declarator)
13155 if (decl_specifiers.specs[(int)ds_inline])
13156 permerror (input_location, "explicit instantiation shall not use"
13157 " %<inline%> specifier");
13158 if (decl_specifiers.specs[(int)ds_constexpr])
13159 permerror (input_location, "explicit instantiation shall not use"
13160 " %<constexpr%> specifier");
13162 decl = grokdeclarator (declarator, &decl_specifiers,
13163 NORMAL, 0, &decl_specifiers.attributes);
13164 /* Turn access control back on for names used during
13165 template instantiation. */
13166 pop_deferring_access_checks ();
13167 /* Do the explicit instantiation. */
13168 do_decl_instantiation (decl, extension_specifier);
13172 pop_deferring_access_checks ();
13173 /* Skip the body of the explicit instantiation. */
13174 cp_parser_skip_to_end_of_statement (parser);
13177 /* We're done with the instantiation. */
13178 end_explicit_instantiation ();
13180 cp_parser_consume_semicolon_at_end_of_statement (parser);
13182 timevar_pop (TV_TEMPLATE_INST);
13185 /* Parse an explicit-specialization.
13187 explicit-specialization:
13188 template < > declaration
13190 Although the standard says `declaration', what it really means is:
13192 explicit-specialization:
13193 template <> decl-specifier [opt] init-declarator [opt] ;
13194 template <> function-definition
13195 template <> explicit-specialization
13196 template <> template-declaration */
13199 cp_parser_explicit_specialization (cp_parser* parser)
13201 bool need_lang_pop;
13202 cp_token *token = cp_lexer_peek_token (parser->lexer);
13204 /* Look for the `template' keyword. */
13205 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13206 /* Look for the `<'. */
13207 cp_parser_require (parser, CPP_LESS, RT_LESS);
13208 /* Look for the `>'. */
13209 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13210 /* We have processed another parameter list. */
13211 ++parser->num_template_parameter_lists;
13214 A template ... explicit specialization ... shall not have C
13216 if (current_lang_name == lang_name_c)
13218 error_at (token->location, "template specialization with C linkage");
13219 /* Give it C++ linkage to avoid confusing other parts of the
13221 push_lang_context (lang_name_cplusplus);
13222 need_lang_pop = true;
13225 need_lang_pop = false;
13226 /* Let the front end know that we are beginning a specialization. */
13227 if (!begin_specialization ())
13229 end_specialization ();
13233 /* If the next keyword is `template', we need to figure out whether
13234 or not we're looking a template-declaration. */
13235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13237 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13238 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13239 cp_parser_template_declaration_after_export (parser,
13240 /*member_p=*/false);
13242 cp_parser_explicit_specialization (parser);
13245 /* Parse the dependent declaration. */
13246 cp_parser_single_declaration (parser,
13248 /*member_p=*/false,
13249 /*explicit_specialization_p=*/true,
13250 /*friend_p=*/NULL);
13251 /* We're done with the specialization. */
13252 end_specialization ();
13253 /* For the erroneous case of a template with C linkage, we pushed an
13254 implicit C++ linkage scope; exit that scope now. */
13256 pop_lang_context ();
13257 /* We're done with this parameter list. */
13258 --parser->num_template_parameter_lists;
13261 /* Parse a type-specifier.
13264 simple-type-specifier
13267 elaborated-type-specifier
13275 Returns a representation of the type-specifier. For a
13276 class-specifier, enum-specifier, or elaborated-type-specifier, a
13277 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13279 The parser flags FLAGS is used to control type-specifier parsing.
13281 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13282 in a decl-specifier-seq.
13284 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13285 class-specifier, enum-specifier, or elaborated-type-specifier, then
13286 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13287 if a type is declared; 2 if it is defined. Otherwise, it is set to
13290 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13291 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13292 is set to FALSE. */
13295 cp_parser_type_specifier (cp_parser* parser,
13296 cp_parser_flags flags,
13297 cp_decl_specifier_seq *decl_specs,
13298 bool is_declaration,
13299 int* declares_class_or_enum,
13300 bool* is_cv_qualifier)
13302 tree type_spec = NULL_TREE;
13305 cp_decl_spec ds = ds_last;
13307 /* Assume this type-specifier does not declare a new type. */
13308 if (declares_class_or_enum)
13309 *declares_class_or_enum = 0;
13310 /* And that it does not specify a cv-qualifier. */
13311 if (is_cv_qualifier)
13312 *is_cv_qualifier = false;
13313 /* Peek at the next token. */
13314 token = cp_lexer_peek_token (parser->lexer);
13316 /* If we're looking at a keyword, we can use that to guide the
13317 production we choose. */
13318 keyword = token->keyword;
13322 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13323 goto elaborated_type_specifier;
13325 /* Look for the enum-specifier. */
13326 type_spec = cp_parser_enum_specifier (parser);
13327 /* If that worked, we're done. */
13330 if (declares_class_or_enum)
13331 *declares_class_or_enum = 2;
13333 cp_parser_set_decl_spec_type (decl_specs,
13336 /*type_definition_p=*/true);
13340 goto elaborated_type_specifier;
13342 /* Any of these indicate either a class-specifier, or an
13343 elaborated-type-specifier. */
13347 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13348 goto elaborated_type_specifier;
13350 /* Parse tentatively so that we can back up if we don't find a
13351 class-specifier. */
13352 cp_parser_parse_tentatively (parser);
13353 /* Look for the class-specifier. */
13354 type_spec = cp_parser_class_specifier (parser);
13355 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13356 /* If that worked, we're done. */
13357 if (cp_parser_parse_definitely (parser))
13359 if (declares_class_or_enum)
13360 *declares_class_or_enum = 2;
13362 cp_parser_set_decl_spec_type (decl_specs,
13365 /*type_definition_p=*/true);
13369 /* Fall through. */
13370 elaborated_type_specifier:
13371 /* We're declaring (not defining) a class or enum. */
13372 if (declares_class_or_enum)
13373 *declares_class_or_enum = 1;
13375 /* Fall through. */
13377 /* Look for an elaborated-type-specifier. */
13379 = (cp_parser_elaborated_type_specifier
13381 decl_specs && decl_specs->specs[(int) ds_friend],
13384 cp_parser_set_decl_spec_type (decl_specs,
13387 /*type_definition_p=*/false);
13392 if (is_cv_qualifier)
13393 *is_cv_qualifier = true;
13398 if (is_cv_qualifier)
13399 *is_cv_qualifier = true;
13404 if (is_cv_qualifier)
13405 *is_cv_qualifier = true;
13409 /* The `__complex__' keyword is a GNU extension. */
13417 /* Handle simple keywords. */
13422 ++decl_specs->specs[(int)ds];
13423 decl_specs->any_specifiers_p = true;
13425 return cp_lexer_consume_token (parser->lexer)->u.value;
13428 /* If we do not already have a type-specifier, assume we are looking
13429 at a simple-type-specifier. */
13430 type_spec = cp_parser_simple_type_specifier (parser,
13434 /* If we didn't find a type-specifier, and a type-specifier was not
13435 optional in this context, issue an error message. */
13436 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13438 cp_parser_error (parser, "expected type specifier");
13439 return error_mark_node;
13445 /* Parse a simple-type-specifier.
13447 simple-type-specifier:
13448 :: [opt] nested-name-specifier [opt] type-name
13449 :: [opt] nested-name-specifier template template-id
13464 simple-type-specifier:
13466 decltype ( expression )
13469 __underlying_type ( type-id )
13473 simple-type-specifier:
13475 __typeof__ unary-expression
13476 __typeof__ ( type-id )
13478 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13479 appropriately updated. */
13482 cp_parser_simple_type_specifier (cp_parser* parser,
13483 cp_decl_specifier_seq *decl_specs,
13484 cp_parser_flags flags)
13486 tree type = NULL_TREE;
13489 /* Peek at the next token. */
13490 token = cp_lexer_peek_token (parser->lexer);
13492 /* If we're looking at a keyword, things are easy. */
13493 switch (token->keyword)
13497 decl_specs->explicit_char_p = true;
13498 type = char_type_node;
13501 type = char16_type_node;
13504 type = char32_type_node;
13507 type = wchar_type_node;
13510 type = boolean_type_node;
13514 ++decl_specs->specs[(int) ds_short];
13515 type = short_integer_type_node;
13519 decl_specs->explicit_int_p = true;
13520 type = integer_type_node;
13523 if (!int128_integer_type_node)
13526 decl_specs->explicit_int128_p = true;
13527 type = int128_integer_type_node;
13531 ++decl_specs->specs[(int) ds_long];
13532 type = long_integer_type_node;
13536 ++decl_specs->specs[(int) ds_signed];
13537 type = integer_type_node;
13541 ++decl_specs->specs[(int) ds_unsigned];
13542 type = unsigned_type_node;
13545 type = float_type_node;
13548 type = double_type_node;
13551 type = void_type_node;
13555 maybe_warn_cpp0x (CPP0X_AUTO);
13556 type = make_auto ();
13560 /* Since DR 743, decltype can either be a simple-type-specifier by
13561 itself or begin a nested-name-specifier. Parsing it will replace
13562 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13563 handling below decide what to do. */
13564 cp_parser_decltype (parser);
13565 cp_lexer_set_token_position (parser->lexer, token);
13569 /* Consume the `typeof' token. */
13570 cp_lexer_consume_token (parser->lexer);
13571 /* Parse the operand to `typeof'. */
13572 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13573 /* If it is not already a TYPE, take its type. */
13574 if (!TYPE_P (type))
13575 type = finish_typeof (type);
13578 cp_parser_set_decl_spec_type (decl_specs, type,
13580 /*type_definition_p=*/false);
13584 case RID_UNDERLYING_TYPE:
13585 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13587 cp_parser_set_decl_spec_type (decl_specs, type,
13589 /*type_definition_p=*/false);
13594 case RID_DIRECT_BASES:
13595 type = cp_parser_trait_expr (parser, token->keyword);
13597 cp_parser_set_decl_spec_type (decl_specs, type,
13599 /*type_definition_p=*/false);
13605 /* If token is an already-parsed decltype not followed by ::,
13606 it's a simple-type-specifier. */
13607 if (token->type == CPP_DECLTYPE
13608 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13610 type = token->u.value;
13612 cp_parser_set_decl_spec_type (decl_specs, type,
13614 /*type_definition_p=*/false);
13615 cp_lexer_consume_token (parser->lexer);
13619 /* If the type-specifier was for a built-in type, we're done. */
13622 /* Record the type. */
13624 && (token->keyword != RID_SIGNED
13625 && token->keyword != RID_UNSIGNED
13626 && token->keyword != RID_SHORT
13627 && token->keyword != RID_LONG))
13628 cp_parser_set_decl_spec_type (decl_specs,
13631 /*type_definition_p=*/false);
13633 decl_specs->any_specifiers_p = true;
13635 /* Consume the token. */
13636 cp_lexer_consume_token (parser->lexer);
13638 /* There is no valid C++ program where a non-template type is
13639 followed by a "<". That usually indicates that the user thought
13640 that the type was a template. */
13641 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13643 return TYPE_NAME (type);
13646 /* The type-specifier must be a user-defined type. */
13647 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13652 /* Don't gobble tokens or issue error messages if this is an
13653 optional type-specifier. */
13654 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13655 cp_parser_parse_tentatively (parser);
13657 /* Look for the optional `::' operator. */
13659 = (cp_parser_global_scope_opt (parser,
13660 /*current_scope_valid_p=*/false)
13662 /* Look for the nested-name specifier. */
13664 = (cp_parser_nested_name_specifier_opt (parser,
13665 /*typename_keyword_p=*/false,
13666 /*check_dependency_p=*/true,
13668 /*is_declaration=*/false)
13670 token = cp_lexer_peek_token (parser->lexer);
13671 /* If we have seen a nested-name-specifier, and the next token
13672 is `template', then we are using the template-id production. */
13674 && cp_parser_optional_template_keyword (parser))
13676 /* Look for the template-id. */
13677 type = cp_parser_template_id (parser,
13678 /*template_keyword_p=*/true,
13679 /*check_dependency_p=*/true,
13680 /*is_declaration=*/false);
13681 /* If the template-id did not name a type, we are out of
13683 if (TREE_CODE (type) != TYPE_DECL)
13685 cp_parser_error (parser, "expected template-id for type");
13689 /* Otherwise, look for a type-name. */
13691 type = cp_parser_type_name (parser);
13692 /* Keep track of all name-lookups performed in class scopes. */
13696 && TREE_CODE (type) == TYPE_DECL
13697 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13698 maybe_note_name_used_in_class (DECL_NAME (type), type);
13699 /* If it didn't work out, we don't have a TYPE. */
13700 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13701 && !cp_parser_parse_definitely (parser))
13703 if (type && decl_specs)
13704 cp_parser_set_decl_spec_type (decl_specs, type,
13706 /*type_definition_p=*/false);
13709 /* If we didn't get a type-name, issue an error message. */
13710 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13712 cp_parser_error (parser, "expected type-name");
13713 return error_mark_node;
13716 if (type && type != error_mark_node)
13718 /* See if TYPE is an Objective-C type, and if so, parse and
13719 accept any protocol references following it. Do this before
13720 the cp_parser_check_for_invalid_template_id() call, because
13721 Objective-C types can be followed by '<...>' which would
13722 enclose protocol names rather than template arguments, and so
13723 everything is fine. */
13724 if (c_dialect_objc () && !parser->scope
13725 && (objc_is_id (type) || objc_is_class_name (type)))
13727 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13728 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13730 /* Clobber the "unqualified" type previously entered into
13731 DECL_SPECS with the new, improved protocol-qualified version. */
13733 decl_specs->type = qual_type;
13738 /* There is no valid C++ program where a non-template type is
13739 followed by a "<". That usually indicates that the user
13740 thought that the type was a template. */
13741 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13748 /* Parse a type-name.
13754 simple-template-id [in c++0x]
13762 Returns a TYPE_DECL for the type. */
13765 cp_parser_type_name (cp_parser* parser)
13769 /* We can't know yet whether it is a class-name or not. */
13770 cp_parser_parse_tentatively (parser);
13771 /* Try a class-name. */
13772 type_decl = cp_parser_class_name (parser,
13773 /*typename_keyword_p=*/false,
13774 /*template_keyword_p=*/false,
13776 /*check_dependency_p=*/true,
13777 /*class_head_p=*/false,
13778 /*is_declaration=*/false);
13779 /* If it's not a class-name, keep looking. */
13780 if (!cp_parser_parse_definitely (parser))
13782 if (cxx_dialect < cxx0x)
13783 /* It must be a typedef-name or an enum-name. */
13784 return cp_parser_nonclass_name (parser);
13786 cp_parser_parse_tentatively (parser);
13787 /* It is either a simple-template-id representing an
13788 instantiation of an alias template... */
13789 type_decl = cp_parser_template_id (parser,
13790 /*template_keyword_p=*/false,
13791 /*check_dependency_p=*/false,
13792 /*is_declaration=*/false);
13793 /* Note that this must be an instantiation of an alias template
13794 because [temp.names]/6 says:
13796 A template-id that names an alias template specialization
13799 Whereas [temp.names]/7 says:
13801 A simple-template-id that names a class template
13802 specialization is a class-name. */
13803 if (type_decl != NULL_TREE
13804 && TREE_CODE (type_decl) == TYPE_DECL
13805 && TYPE_DECL_ALIAS_P (type_decl))
13806 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13808 cp_parser_simulate_error (parser);
13810 if (!cp_parser_parse_definitely (parser))
13811 /* ... Or a typedef-name or an enum-name. */
13812 return cp_parser_nonclass_name (parser);
13818 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13826 Returns a TYPE_DECL for the type. */
13829 cp_parser_nonclass_name (cp_parser* parser)
13834 cp_token *token = cp_lexer_peek_token (parser->lexer);
13835 identifier = cp_parser_identifier (parser);
13836 if (identifier == error_mark_node)
13837 return error_mark_node;
13839 /* Look up the type-name. */
13840 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13842 if (TREE_CODE (type_decl) == USING_DECL)
13844 if (!DECL_DEPENDENT_P (type_decl))
13845 type_decl = strip_using_decl (type_decl);
13846 else if (USING_DECL_TYPENAME_P (type_decl))
13848 /* We have found a type introduced by a using
13849 declaration at class scope that refers to a dependent
13852 using typename :: [opt] nested-name-specifier unqualified-id ;
13854 type_decl = make_typename_type (TREE_TYPE (type_decl),
13855 DECL_NAME (type_decl),
13856 typename_type, tf_error);
13857 if (type_decl != error_mark_node)
13858 type_decl = TYPE_NAME (type_decl);
13862 if (TREE_CODE (type_decl) != TYPE_DECL
13863 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13865 /* See if this is an Objective-C type. */
13866 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13867 tree type = objc_get_protocol_qualified_type (identifier, protos);
13869 type_decl = TYPE_NAME (type);
13872 /* Issue an error if we did not find a type-name. */
13873 if (TREE_CODE (type_decl) != TYPE_DECL
13874 /* In Objective-C, we have the complication that class names are
13875 normally type names and start declarations (eg, the
13876 "NSObject" in "NSObject *object;"), but can be used in an
13877 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13878 is an expression. So, a classname followed by a dot is not a
13879 valid type-name. */
13880 || (objc_is_class_name (TREE_TYPE (type_decl))
13881 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13883 if (!cp_parser_simulate_error (parser))
13884 cp_parser_name_lookup_error (parser, identifier, type_decl,
13885 NLE_TYPE, token->location);
13886 return error_mark_node;
13888 /* Remember that the name was used in the definition of the
13889 current class so that we can check later to see if the
13890 meaning would have been different after the class was
13891 entirely defined. */
13892 else if (type_decl != error_mark_node
13894 maybe_note_name_used_in_class (identifier, type_decl);
13899 /* Parse an elaborated-type-specifier. Note that the grammar given
13900 here incorporates the resolution to DR68.
13902 elaborated-type-specifier:
13903 class-key :: [opt] nested-name-specifier [opt] identifier
13904 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13905 enum-key :: [opt] nested-name-specifier [opt] identifier
13906 typename :: [opt] nested-name-specifier identifier
13907 typename :: [opt] nested-name-specifier template [opt]
13912 elaborated-type-specifier:
13913 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13914 class-key attributes :: [opt] nested-name-specifier [opt]
13915 template [opt] template-id
13916 enum attributes :: [opt] nested-name-specifier [opt] identifier
13918 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13919 declared `friend'. If IS_DECLARATION is TRUE, then this
13920 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13921 something is being declared.
13923 Returns the TYPE specified. */
13926 cp_parser_elaborated_type_specifier (cp_parser* parser,
13928 bool is_declaration)
13930 enum tag_types tag_type;
13932 tree type = NULL_TREE;
13933 tree attributes = NULL_TREE;
13935 cp_token *token = NULL;
13937 /* See if we're looking at the `enum' keyword. */
13938 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13940 /* Consume the `enum' token. */
13941 cp_lexer_consume_token (parser->lexer);
13942 /* Remember that it's an enumeration type. */
13943 tag_type = enum_type;
13944 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13945 enums) is used here. */
13946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13947 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13949 pedwarn (input_location, 0, "elaborated-type-specifier "
13950 "for a scoped enum must not use the %<%D%> keyword",
13951 cp_lexer_peek_token (parser->lexer)->u.value);
13952 /* Consume the `struct' or `class' and parse it anyway. */
13953 cp_lexer_consume_token (parser->lexer);
13955 /* Parse the attributes. */
13956 attributes = cp_parser_attributes_opt (parser);
13958 /* Or, it might be `typename'. */
13959 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13962 /* Consume the `typename' token. */
13963 cp_lexer_consume_token (parser->lexer);
13964 /* Remember that it's a `typename' type. */
13965 tag_type = typename_type;
13967 /* Otherwise it must be a class-key. */
13970 tag_type = cp_parser_class_key (parser);
13971 if (tag_type == none_type)
13972 return error_mark_node;
13973 /* Parse the attributes. */
13974 attributes = cp_parser_attributes_opt (parser);
13977 /* Look for the `::' operator. */
13978 globalscope = cp_parser_global_scope_opt (parser,
13979 /*current_scope_valid_p=*/false);
13980 /* Look for the nested-name-specifier. */
13981 if (tag_type == typename_type && !globalscope)
13983 if (!cp_parser_nested_name_specifier (parser,
13984 /*typename_keyword_p=*/true,
13985 /*check_dependency_p=*/true,
13988 return error_mark_node;
13991 /* Even though `typename' is not present, the proposed resolution
13992 to Core Issue 180 says that in `class A<T>::B', `B' should be
13993 considered a type-name, even if `A<T>' is dependent. */
13994 cp_parser_nested_name_specifier_opt (parser,
13995 /*typename_keyword_p=*/true,
13996 /*check_dependency_p=*/true,
13999 /* For everything but enumeration types, consider a template-id.
14000 For an enumeration type, consider only a plain identifier. */
14001 if (tag_type != enum_type)
14003 bool template_p = false;
14006 /* Allow the `template' keyword. */
14007 template_p = cp_parser_optional_template_keyword (parser);
14008 /* If we didn't see `template', we don't know if there's a
14009 template-id or not. */
14011 cp_parser_parse_tentatively (parser);
14012 /* Parse the template-id. */
14013 token = cp_lexer_peek_token (parser->lexer);
14014 decl = cp_parser_template_id (parser, template_p,
14015 /*check_dependency_p=*/true,
14017 /* If we didn't find a template-id, look for an ordinary
14019 if (!template_p && !cp_parser_parse_definitely (parser))
14021 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14022 in effect, then we must assume that, upon instantiation, the
14023 template will correspond to a class. */
14024 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14025 && tag_type == typename_type)
14026 type = make_typename_type (parser->scope, decl,
14028 /*complain=*/tf_error);
14029 /* If the `typename' keyword is in effect and DECL is not a type
14030 decl. Then type is non existant. */
14031 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14034 type = check_elaborated_type_specifier (tag_type, decl,
14035 /*allow_template_p=*/true);
14040 token = cp_lexer_peek_token (parser->lexer);
14041 identifier = cp_parser_identifier (parser);
14043 if (identifier == error_mark_node)
14045 parser->scope = NULL_TREE;
14046 return error_mark_node;
14049 /* For a `typename', we needn't call xref_tag. */
14050 if (tag_type == typename_type
14051 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14052 return cp_parser_make_typename_type (parser, parser->scope,
14055 /* Look up a qualified name in the usual way. */
14059 tree ambiguous_decls;
14061 decl = cp_parser_lookup_name (parser, identifier,
14063 /*is_template=*/false,
14064 /*is_namespace=*/false,
14065 /*check_dependency=*/true,
14069 /* If the lookup was ambiguous, an error will already have been
14071 if (ambiguous_decls)
14072 return error_mark_node;
14074 /* If we are parsing friend declaration, DECL may be a
14075 TEMPLATE_DECL tree node here. However, we need to check
14076 whether this TEMPLATE_DECL results in valid code. Consider
14077 the following example:
14080 template <class T> class C {};
14083 template <class T> friend class N::C; // #1, valid code
14085 template <class T> class Y {
14086 friend class N::C; // #2, invalid code
14089 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14090 name lookup of `N::C'. We see that friend declaration must
14091 be template for the code to be valid. Note that
14092 processing_template_decl does not work here since it is
14093 always 1 for the above two cases. */
14095 decl = (cp_parser_maybe_treat_template_as_class
14096 (decl, /*tag_name_p=*/is_friend
14097 && parser->num_template_parameter_lists));
14099 if (TREE_CODE (decl) != TYPE_DECL)
14101 cp_parser_diagnose_invalid_type_name (parser,
14105 return error_mark_node;
14108 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14110 bool allow_template = (parser->num_template_parameter_lists
14111 || DECL_SELF_REFERENCE_P (decl));
14112 type = check_elaborated_type_specifier (tag_type, decl,
14115 if (type == error_mark_node)
14116 return error_mark_node;
14119 /* Forward declarations of nested types, such as
14124 are invalid unless all components preceding the final '::'
14125 are complete. If all enclosing types are complete, these
14126 declarations become merely pointless.
14128 Invalid forward declarations of nested types are errors
14129 caught elsewhere in parsing. Those that are pointless arrive
14132 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14133 && !is_friend && !processing_explicit_instantiation)
14134 warning (0, "declaration %qD does not declare anything", decl);
14136 type = TREE_TYPE (decl);
14140 /* An elaborated-type-specifier sometimes introduces a new type and
14141 sometimes names an existing type. Normally, the rule is that it
14142 introduces a new type only if there is not an existing type of
14143 the same name already in scope. For example, given:
14146 void f() { struct S s; }
14148 the `struct S' in the body of `f' is the same `struct S' as in
14149 the global scope; the existing definition is used. However, if
14150 there were no global declaration, this would introduce a new
14151 local class named `S'.
14153 An exception to this rule applies to the following code:
14155 namespace N { struct S; }
14157 Here, the elaborated-type-specifier names a new type
14158 unconditionally; even if there is already an `S' in the
14159 containing scope this declaration names a new type.
14160 This exception only applies if the elaborated-type-specifier
14161 forms the complete declaration:
14165 A declaration consisting solely of `class-key identifier ;' is
14166 either a redeclaration of the name in the current scope or a
14167 forward declaration of the identifier as a class name. It
14168 introduces the name into the current scope.
14170 We are in this situation precisely when the next token is a `;'.
14172 An exception to the exception is that a `friend' declaration does
14173 *not* name a new type; i.e., given:
14175 struct S { friend struct T; };
14177 `T' is not a new type in the scope of `S'.
14179 Also, `new struct S' or `sizeof (struct S)' never results in the
14180 definition of a new type; a new type can only be declared in a
14181 declaration context. */
14187 /* Friends have special name lookup rules. */
14188 ts = ts_within_enclosing_non_class;
14189 else if (is_declaration
14190 && cp_lexer_next_token_is (parser->lexer,
14192 /* This is a `class-key identifier ;' */
14198 (parser->num_template_parameter_lists
14199 && (cp_parser_next_token_starts_class_definition_p (parser)
14200 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14201 /* An unqualified name was used to reference this type, so
14202 there were no qualifying templates. */
14203 if (!cp_parser_check_template_parameters (parser,
14204 /*num_templates=*/0,
14206 /*declarator=*/NULL))
14207 return error_mark_node;
14208 type = xref_tag (tag_type, identifier, ts, template_p);
14212 if (type == error_mark_node)
14213 return error_mark_node;
14215 /* Allow attributes on forward declarations of classes. */
14218 if (TREE_CODE (type) == TYPENAME_TYPE)
14219 warning (OPT_Wattributes,
14220 "attributes ignored on uninstantiated type");
14221 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14222 && ! processing_explicit_instantiation)
14223 warning (OPT_Wattributes,
14224 "attributes ignored on template instantiation");
14225 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14226 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14228 warning (OPT_Wattributes,
14229 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14232 if (tag_type != enum_type)
14234 /* Indicate whether this class was declared as a `class' or as a
14236 if (TREE_CODE (type) == RECORD_TYPE)
14237 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14238 cp_parser_check_class_key (tag_type, type);
14241 /* A "<" cannot follow an elaborated type specifier. If that
14242 happens, the user was probably trying to form a template-id. */
14243 cp_parser_check_for_invalid_template_id (parser, type, token->location);
14248 /* Parse an enum-specifier.
14251 enum-head { enumerator-list [opt] }
14252 enum-head { enumerator-list , } [C++0x]
14255 enum-key identifier [opt] enum-base [opt]
14256 enum-key nested-name-specifier identifier enum-base [opt]
14261 enum struct [C++0x]
14264 : type-specifier-seq
14266 opaque-enum-specifier:
14267 enum-key identifier enum-base [opt] ;
14270 enum-key attributes[opt] identifier [opt] enum-base [opt]
14271 { enumerator-list [opt] }attributes[opt]
14272 enum-key attributes[opt] identifier [opt] enum-base [opt]
14273 { enumerator-list, }attributes[opt] [C++0x]
14275 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14276 if the token stream isn't an enum-specifier after all. */
14279 cp_parser_enum_specifier (cp_parser* parser)
14282 tree type = NULL_TREE;
14284 tree nested_name_specifier = NULL_TREE;
14286 bool scoped_enum_p = false;
14287 bool has_underlying_type = false;
14288 bool nested_being_defined = false;
14289 bool new_value_list = false;
14290 bool is_new_type = false;
14291 bool is_anonymous = false;
14292 tree underlying_type = NULL_TREE;
14293 cp_token *type_start_token = NULL;
14294 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14296 parser->colon_corrects_to_scope_p = false;
14298 /* Parse tentatively so that we can back up if we don't find a
14300 cp_parser_parse_tentatively (parser);
14302 /* Caller guarantees that the current token is 'enum', an identifier
14303 possibly follows, and the token after that is an opening brace.
14304 If we don't have an identifier, fabricate an anonymous name for
14305 the enumeration being defined. */
14306 cp_lexer_consume_token (parser->lexer);
14308 /* Parse the "class" or "struct", which indicates a scoped
14309 enumeration type in C++0x. */
14310 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14311 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14313 if (cxx_dialect < cxx0x)
14314 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14316 /* Consume the `struct' or `class' token. */
14317 cp_lexer_consume_token (parser->lexer);
14319 scoped_enum_p = true;
14322 attributes = cp_parser_attributes_opt (parser);
14324 /* Clear the qualification. */
14325 parser->scope = NULL_TREE;
14326 parser->qualifying_scope = NULL_TREE;
14327 parser->object_scope = NULL_TREE;
14329 /* Figure out in what scope the declaration is being placed. */
14330 prev_scope = current_scope ();
14332 type_start_token = cp_lexer_peek_token (parser->lexer);
14334 push_deferring_access_checks (dk_no_check);
14335 nested_name_specifier
14336 = cp_parser_nested_name_specifier_opt (parser,
14337 /*typename_keyword_p=*/true,
14338 /*check_dependency_p=*/false,
14340 /*is_declaration=*/false);
14342 if (nested_name_specifier)
14346 identifier = cp_parser_identifier (parser);
14347 name = cp_parser_lookup_name (parser, identifier,
14349 /*is_template=*/false,
14350 /*is_namespace=*/false,
14351 /*check_dependency=*/true,
14352 /*ambiguous_decls=*/NULL,
14356 type = TREE_TYPE (name);
14357 if (TREE_CODE (type) == TYPENAME_TYPE)
14359 /* Are template enums allowed in ISO? */
14360 if (template_parm_scope_p ())
14361 pedwarn (type_start_token->location, OPT_pedantic,
14362 "%qD is an enumeration template", name);
14363 /* ignore a typename reference, for it will be solved by name
14369 error_at (type_start_token->location,
14370 "%qD is not an enumerator-name", identifier);
14374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14375 identifier = cp_parser_identifier (parser);
14378 identifier = make_anon_name ();
14379 is_anonymous = true;
14382 pop_deferring_access_checks ();
14384 /* Check for the `:' that denotes a specified underlying type in C++0x.
14385 Note that a ':' could also indicate a bitfield width, however. */
14386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14388 cp_decl_specifier_seq type_specifiers;
14390 /* Consume the `:'. */
14391 cp_lexer_consume_token (parser->lexer);
14393 /* Parse the type-specifier-seq. */
14394 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14395 /*is_trailing_return=*/false,
14398 /* At this point this is surely not elaborated type specifier. */
14399 if (!cp_parser_parse_definitely (parser))
14402 if (cxx_dialect < cxx0x)
14403 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14405 has_underlying_type = true;
14407 /* If that didn't work, stop. */
14408 if (type_specifiers.type != error_mark_node)
14410 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14411 /*initialized=*/0, NULL);
14412 if (underlying_type == error_mark_node)
14413 underlying_type = NULL_TREE;
14417 /* Look for the `{' but don't consume it yet. */
14418 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14420 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14422 cp_parser_error (parser, "expected %<{%>");
14423 if (has_underlying_type)
14429 /* An opaque-enum-specifier must have a ';' here. */
14430 if ((scoped_enum_p || underlying_type)
14431 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14433 cp_parser_error (parser, "expected %<;%> or %<{%>");
14434 if (has_underlying_type)
14442 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14445 if (nested_name_specifier)
14447 if (CLASS_TYPE_P (nested_name_specifier))
14449 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14450 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14451 push_scope (nested_name_specifier);
14453 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14455 push_nested_namespace (nested_name_specifier);
14459 /* Issue an error message if type-definitions are forbidden here. */
14460 if (!cp_parser_check_type_definition (parser))
14461 type = error_mark_node;
14463 /* Create the new type. We do this before consuming the opening
14464 brace so the enum will be recorded as being on the line of its
14465 tag (or the 'enum' keyword, if there is no tag). */
14466 type = start_enum (identifier, type, underlying_type,
14467 scoped_enum_p, &is_new_type);
14469 /* If the next token is not '{' it is an opaque-enum-specifier or an
14470 elaborated-type-specifier. */
14471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14473 timevar_push (TV_PARSE_ENUM);
14474 if (nested_name_specifier)
14476 /* The following catches invalid code such as:
14477 enum class S<int>::E { A, B, C }; */
14478 if (!processing_specialization
14479 && CLASS_TYPE_P (nested_name_specifier)
14480 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14481 error_at (type_start_token->location, "cannot add an enumerator "
14482 "list to a template instantiation");
14484 /* If that scope does not contain the scope in which the
14485 class was originally declared, the program is invalid. */
14486 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14488 if (at_namespace_scope_p ())
14489 error_at (type_start_token->location,
14490 "declaration of %qD in namespace %qD which does not "
14492 type, prev_scope, nested_name_specifier);
14494 error_at (type_start_token->location,
14495 "declaration of %qD in %qD which does not enclose %qD",
14496 type, prev_scope, nested_name_specifier);
14497 type = error_mark_node;
14502 begin_scope (sk_scoped_enum, type);
14504 /* Consume the opening brace. */
14505 cp_lexer_consume_token (parser->lexer);
14507 if (type == error_mark_node)
14508 ; /* Nothing to add */
14509 else if (OPAQUE_ENUM_P (type)
14510 || (cxx_dialect > cxx98 && processing_specialization))
14512 new_value_list = true;
14513 SET_OPAQUE_ENUM_P (type, false);
14514 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14518 error_at (type_start_token->location, "multiple definition of %q#T", type);
14519 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14520 "previous definition here");
14521 type = error_mark_node;
14524 if (type == error_mark_node)
14525 cp_parser_skip_to_end_of_block_or_statement (parser);
14526 /* If the next token is not '}', then there are some enumerators. */
14527 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14528 cp_parser_enumerator_list (parser, type);
14530 /* Consume the final '}'. */
14531 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14535 timevar_pop (TV_PARSE_ENUM);
14539 /* If a ';' follows, then it is an opaque-enum-specifier
14540 and additional restrictions apply. */
14541 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14544 error_at (type_start_token->location,
14545 "opaque-enum-specifier without name");
14546 else if (nested_name_specifier)
14547 error_at (type_start_token->location,
14548 "opaque-enum-specifier must use a simple identifier");
14552 /* Look for trailing attributes to apply to this enumeration, and
14553 apply them if appropriate. */
14554 if (cp_parser_allow_gnu_extensions_p (parser))
14556 tree trailing_attr = cp_parser_attributes_opt (parser);
14557 trailing_attr = chainon (trailing_attr, attributes);
14558 cplus_decl_attributes (&type,
14560 (int) ATTR_FLAG_TYPE_IN_PLACE);
14563 /* Finish up the enumeration. */
14564 if (type != error_mark_node)
14566 if (new_value_list)
14567 finish_enum_value_list (type);
14569 finish_enum (type);
14572 if (nested_name_specifier)
14574 if (CLASS_TYPE_P (nested_name_specifier))
14576 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14577 pop_scope (nested_name_specifier);
14579 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14581 pop_nested_namespace (nested_name_specifier);
14585 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14589 /* Parse an enumerator-list. The enumerators all have the indicated
14593 enumerator-definition
14594 enumerator-list , enumerator-definition */
14597 cp_parser_enumerator_list (cp_parser* parser, tree type)
14601 /* Parse an enumerator-definition. */
14602 cp_parser_enumerator_definition (parser, type);
14604 /* If the next token is not a ',', we've reached the end of
14606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14608 /* Otherwise, consume the `,' and keep going. */
14609 cp_lexer_consume_token (parser->lexer);
14610 /* If the next token is a `}', there is a trailing comma. */
14611 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14613 if (cxx_dialect < cxx0x && !in_system_header)
14614 pedwarn (input_location, OPT_pedantic,
14615 "comma at end of enumerator list");
14621 /* Parse an enumerator-definition. The enumerator has the indicated
14624 enumerator-definition:
14626 enumerator = constant-expression
14632 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14638 /* Save the input location because we are interested in the location
14639 of the identifier and not the location of the explicit value. */
14640 loc = cp_lexer_peek_token (parser->lexer)->location;
14642 /* Look for the identifier. */
14643 identifier = cp_parser_identifier (parser);
14644 if (identifier == error_mark_node)
14647 /* If the next token is an '=', then there is an explicit value. */
14648 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14650 /* Consume the `=' token. */
14651 cp_lexer_consume_token (parser->lexer);
14652 /* Parse the value. */
14653 value = cp_parser_constant_expression (parser,
14654 /*allow_non_constant_p=*/false,
14660 /* If we are processing a template, make sure the initializer of the
14661 enumerator doesn't contain any bare template parameter pack. */
14662 if (check_for_bare_parameter_packs (value))
14663 value = error_mark_node;
14665 /* integral_constant_value will pull out this expression, so make sure
14666 it's folded as appropriate. */
14667 value = fold_non_dependent_expr (value);
14669 /* Create the enumerator. */
14670 build_enumerator (identifier, value, type, loc);
14673 /* Parse a namespace-name.
14676 original-namespace-name
14679 Returns the NAMESPACE_DECL for the namespace. */
14682 cp_parser_namespace_name (cp_parser* parser)
14685 tree namespace_decl;
14687 cp_token *token = cp_lexer_peek_token (parser->lexer);
14689 /* Get the name of the namespace. */
14690 identifier = cp_parser_identifier (parser);
14691 if (identifier == error_mark_node)
14692 return error_mark_node;
14694 /* Look up the identifier in the currently active scope. Look only
14695 for namespaces, due to:
14697 [basic.lookup.udir]
14699 When looking up a namespace-name in a using-directive or alias
14700 definition, only namespace names are considered.
14704 [basic.lookup.qual]
14706 During the lookup of a name preceding the :: scope resolution
14707 operator, object, function, and enumerator names are ignored.
14709 (Note that cp_parser_qualifying_entity only calls this
14710 function if the token after the name is the scope resolution
14712 namespace_decl = cp_parser_lookup_name (parser, identifier,
14714 /*is_template=*/false,
14715 /*is_namespace=*/true,
14716 /*check_dependency=*/true,
14717 /*ambiguous_decls=*/NULL,
14719 /* If it's not a namespace, issue an error. */
14720 if (namespace_decl == error_mark_node
14721 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14723 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14724 error_at (token->location, "%qD is not a namespace-name", identifier);
14725 cp_parser_error (parser, "expected namespace-name");
14726 namespace_decl = error_mark_node;
14729 return namespace_decl;
14732 /* Parse a namespace-definition.
14734 namespace-definition:
14735 named-namespace-definition
14736 unnamed-namespace-definition
14738 named-namespace-definition:
14739 original-namespace-definition
14740 extension-namespace-definition
14742 original-namespace-definition:
14743 namespace identifier { namespace-body }
14745 extension-namespace-definition:
14746 namespace original-namespace-name { namespace-body }
14748 unnamed-namespace-definition:
14749 namespace { namespace-body } */
14752 cp_parser_namespace_definition (cp_parser* parser)
14754 tree identifier, attribs;
14755 bool has_visibility;
14758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14760 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14762 cp_lexer_consume_token (parser->lexer);
14767 /* Look for the `namespace' keyword. */
14768 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14770 /* Get the name of the namespace. We do not attempt to distinguish
14771 between an original-namespace-definition and an
14772 extension-namespace-definition at this point. The semantic
14773 analysis routines are responsible for that. */
14774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14775 identifier = cp_parser_identifier (parser);
14777 identifier = NULL_TREE;
14779 /* Parse any specified attributes. */
14780 attribs = cp_parser_attributes_opt (parser);
14782 /* Look for the `{' to start the namespace. */
14783 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14784 /* Start the namespace. */
14785 push_namespace (identifier);
14787 /* "inline namespace" is equivalent to a stub namespace definition
14788 followed by a strong using directive. */
14791 tree name_space = current_namespace;
14792 /* Set up namespace association. */
14793 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14794 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14795 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14796 /* Import the contents of the inline namespace. */
14798 do_using_directive (name_space);
14799 push_namespace (identifier);
14802 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14804 /* Parse the body of the namespace. */
14805 cp_parser_namespace_body (parser);
14807 if (has_visibility)
14808 pop_visibility (1);
14810 /* Finish the namespace. */
14812 /* Look for the final `}'. */
14813 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14816 /* Parse a namespace-body.
14819 declaration-seq [opt] */
14822 cp_parser_namespace_body (cp_parser* parser)
14824 cp_parser_declaration_seq_opt (parser);
14827 /* Parse a namespace-alias-definition.
14829 namespace-alias-definition:
14830 namespace identifier = qualified-namespace-specifier ; */
14833 cp_parser_namespace_alias_definition (cp_parser* parser)
14836 tree namespace_specifier;
14838 cp_token *token = cp_lexer_peek_token (parser->lexer);
14840 /* Look for the `namespace' keyword. */
14841 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14842 /* Look for the identifier. */
14843 identifier = cp_parser_identifier (parser);
14844 if (identifier == error_mark_node)
14846 /* Look for the `=' token. */
14847 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14848 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14850 error_at (token->location, "%<namespace%> definition is not allowed here");
14851 /* Skip the definition. */
14852 cp_lexer_consume_token (parser->lexer);
14853 if (cp_parser_skip_to_closing_brace (parser))
14854 cp_lexer_consume_token (parser->lexer);
14857 cp_parser_require (parser, CPP_EQ, RT_EQ);
14858 /* Look for the qualified-namespace-specifier. */
14859 namespace_specifier
14860 = cp_parser_qualified_namespace_specifier (parser);
14861 /* Look for the `;' token. */
14862 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14864 /* Register the alias in the symbol table. */
14865 do_namespace_alias (identifier, namespace_specifier);
14868 /* Parse a qualified-namespace-specifier.
14870 qualified-namespace-specifier:
14871 :: [opt] nested-name-specifier [opt] namespace-name
14873 Returns a NAMESPACE_DECL corresponding to the specified
14877 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14879 /* Look for the optional `::'. */
14880 cp_parser_global_scope_opt (parser,
14881 /*current_scope_valid_p=*/false);
14883 /* Look for the optional nested-name-specifier. */
14884 cp_parser_nested_name_specifier_opt (parser,
14885 /*typename_keyword_p=*/false,
14886 /*check_dependency_p=*/true,
14888 /*is_declaration=*/true);
14890 return cp_parser_namespace_name (parser);
14893 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14894 access declaration.
14897 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14898 using :: unqualified-id ;
14900 access-declaration:
14906 cp_parser_using_declaration (cp_parser* parser,
14907 bool access_declaration_p)
14910 bool typename_p = false;
14911 bool global_scope_p;
14915 int oldcount = errorcount;
14916 cp_token *diag_token = NULL;
14918 if (access_declaration_p)
14920 diag_token = cp_lexer_peek_token (parser->lexer);
14921 cp_parser_parse_tentatively (parser);
14925 /* Look for the `using' keyword. */
14926 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14928 /* Peek at the next token. */
14929 token = cp_lexer_peek_token (parser->lexer);
14930 /* See if it's `typename'. */
14931 if (token->keyword == RID_TYPENAME)
14933 /* Remember that we've seen it. */
14935 /* Consume the `typename' token. */
14936 cp_lexer_consume_token (parser->lexer);
14940 /* Look for the optional global scope qualification. */
14942 = (cp_parser_global_scope_opt (parser,
14943 /*current_scope_valid_p=*/false)
14946 /* If we saw `typename', or didn't see `::', then there must be a
14947 nested-name-specifier present. */
14948 if (typename_p || !global_scope_p)
14949 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14950 /*check_dependency_p=*/true,
14952 /*is_declaration=*/true);
14953 /* Otherwise, we could be in either of the two productions. In that
14954 case, treat the nested-name-specifier as optional. */
14956 qscope = cp_parser_nested_name_specifier_opt (parser,
14957 /*typename_keyword_p=*/false,
14958 /*check_dependency_p=*/true,
14960 /*is_declaration=*/true);
14962 qscope = global_namespace;
14964 if (access_declaration_p && cp_parser_error_occurred (parser))
14965 /* Something has already gone wrong; there's no need to parse
14966 further. Since an error has occurred, the return value of
14967 cp_parser_parse_definitely will be false, as required. */
14968 return cp_parser_parse_definitely (parser);
14970 token = cp_lexer_peek_token (parser->lexer);
14971 /* Parse the unqualified-id. */
14972 identifier = cp_parser_unqualified_id (parser,
14973 /*template_keyword_p=*/false,
14974 /*check_dependency_p=*/true,
14975 /*declarator_p=*/true,
14976 /*optional_p=*/false);
14978 if (access_declaration_p)
14980 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14981 cp_parser_simulate_error (parser);
14982 if (!cp_parser_parse_definitely (parser))
14986 /* The function we call to handle a using-declaration is different
14987 depending on what scope we are in. */
14988 if (qscope == error_mark_node || identifier == error_mark_node)
14990 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14991 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14992 /* [namespace.udecl]
14994 A using declaration shall not name a template-id. */
14995 error_at (token->location,
14996 "a template-id may not appear in a using-declaration");
14999 if (at_class_scope_p ())
15001 /* Create the USING_DECL. */
15002 decl = do_class_using_decl (parser->scope, identifier);
15004 if (decl && typename_p)
15005 USING_DECL_TYPENAME_P (decl) = 1;
15007 if (check_for_bare_parameter_packs (decl))
15010 /* Add it to the list of members in this class. */
15011 finish_member_declaration (decl);
15015 decl = cp_parser_lookup_name_simple (parser,
15018 if (decl == error_mark_node)
15019 cp_parser_name_lookup_error (parser, identifier,
15022 else if (check_for_bare_parameter_packs (decl))
15024 else if (!at_namespace_scope_p ())
15025 do_local_using_decl (decl, qscope, identifier);
15027 do_toplevel_using_decl (decl, qscope, identifier);
15031 /* Look for the final `;'. */
15032 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15034 if (access_declaration_p && errorcount == oldcount)
15035 warning_at (diag_token->location, OPT_Wdeprecated,
15036 "access declarations are deprecated "
15037 "in favour of using-declarations; "
15038 "suggestion: add the %<using%> keyword");
15043 /* Parse an alias-declaration.
15046 using identifier attribute-specifier-seq [opt] = type-id */
15049 cp_parser_alias_declaration (cp_parser* parser)
15051 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15052 location_t id_location;
15053 cp_declarator *declarator;
15054 cp_decl_specifier_seq decl_specs;
15056 const char *saved_message = NULL;
15058 /* Look for the `using' keyword. */
15059 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15060 id_location = cp_lexer_peek_token (parser->lexer)->location;
15061 id = cp_parser_identifier (parser);
15062 if (id == error_mark_node)
15063 return error_mark_node;
15065 attributes = cp_parser_attributes_opt (parser);
15066 if (attributes == error_mark_node)
15067 return error_mark_node;
15069 cp_parser_require (parser, CPP_EQ, RT_EQ);
15071 /* Now we are going to parse the type-id of the declaration. */
15076 "A type-specifier-seq shall not define a class or enumeration
15077 unless it appears in the type-id of an alias-declaration (7.1.3) that
15078 is not the declaration of a template-declaration."
15080 In other words, if we currently are in an alias template, the
15081 type-id should not define a type.
15083 So let's set parser->type_definition_forbidden_message in that
15084 case; cp_parser_check_type_definition (called by
15085 cp_parser_class_specifier) will then emit an error if a type is
15086 defined in the type-id. */
15087 if (parser->num_template_parameter_lists)
15089 saved_message = parser->type_definition_forbidden_message;
15090 parser->type_definition_forbidden_message =
15091 G_("types may not be defined in alias template declarations");
15094 type = cp_parser_type_id (parser);
15096 /* Restore the error message if need be. */
15097 if (parser->num_template_parameter_lists)
15098 parser->type_definition_forbidden_message = saved_message;
15100 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15102 if (cp_parser_error_occurred (parser))
15103 return error_mark_node;
15105 /* A typedef-name can also be introduced by an alias-declaration. The
15106 identifier following the using keyword becomes a typedef-name. It has
15107 the same semantics as if it were introduced by the typedef
15108 specifier. In particular, it does not define a new type and it shall
15109 not appear in the type-id. */
15111 clear_decl_specs (&decl_specs);
15112 decl_specs.type = type;
15113 decl_specs.attributes = attributes;
15114 ++decl_specs.specs[(int) ds_typedef];
15115 ++decl_specs.specs[(int) ds_alias];
15117 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15118 declarator->id_loc = id_location;
15120 member_p = at_class_scope_p ();
15122 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15123 NULL_TREE, attributes);
15125 decl = start_decl (declarator, &decl_specs, 0,
15126 attributes, NULL_TREE, &pushed_scope);
15127 if (decl == error_mark_node)
15130 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15133 pop_scope (pushed_scope);
15135 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15136 added into the symbol table; otherwise, return the TYPE_DECL. */
15137 if (DECL_LANG_SPECIFIC (decl)
15138 && DECL_TEMPLATE_INFO (decl)
15139 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15141 decl = DECL_TI_TEMPLATE (decl);
15143 check_member_template (decl);
15149 /* Parse a using-directive.
15152 using namespace :: [opt] nested-name-specifier [opt]
15153 namespace-name ; */
15156 cp_parser_using_directive (cp_parser* parser)
15158 tree namespace_decl;
15161 /* Look for the `using' keyword. */
15162 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15163 /* And the `namespace' keyword. */
15164 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15165 /* Look for the optional `::' operator. */
15166 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15167 /* And the optional nested-name-specifier. */
15168 cp_parser_nested_name_specifier_opt (parser,
15169 /*typename_keyword_p=*/false,
15170 /*check_dependency_p=*/true,
15172 /*is_declaration=*/true);
15173 /* Get the namespace being used. */
15174 namespace_decl = cp_parser_namespace_name (parser);
15175 /* And any specified attributes. */
15176 attribs = cp_parser_attributes_opt (parser);
15177 /* Update the symbol table. */
15178 parse_using_directive (namespace_decl, attribs);
15179 /* Look for the final `;'. */
15180 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15183 /* Parse an asm-definition.
15186 asm ( string-literal ) ;
15191 asm volatile [opt] ( string-literal ) ;
15192 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15193 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15194 : asm-operand-list [opt] ) ;
15195 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15196 : asm-operand-list [opt]
15197 : asm-clobber-list [opt] ) ;
15198 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15199 : asm-clobber-list [opt]
15200 : asm-goto-list ) ; */
15203 cp_parser_asm_definition (cp_parser* parser)
15206 tree outputs = NULL_TREE;
15207 tree inputs = NULL_TREE;
15208 tree clobbers = NULL_TREE;
15209 tree labels = NULL_TREE;
15211 bool volatile_p = false;
15212 bool extended_p = false;
15213 bool invalid_inputs_p = false;
15214 bool invalid_outputs_p = false;
15215 bool goto_p = false;
15216 required_token missing = RT_NONE;
15218 /* Look for the `asm' keyword. */
15219 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15220 /* See if the next token is `volatile'. */
15221 if (cp_parser_allow_gnu_extensions_p (parser)
15222 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15224 /* Remember that we saw the `volatile' keyword. */
15226 /* Consume the token. */
15227 cp_lexer_consume_token (parser->lexer);
15229 if (cp_parser_allow_gnu_extensions_p (parser)
15230 && parser->in_function_body
15231 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15233 /* Remember that we saw the `goto' keyword. */
15235 /* Consume the token. */
15236 cp_lexer_consume_token (parser->lexer);
15238 /* Look for the opening `('. */
15239 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15241 /* Look for the string. */
15242 string = cp_parser_string_literal (parser, false, false);
15243 if (string == error_mark_node)
15245 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15246 /*consume_paren=*/true);
15250 /* If we're allowing GNU extensions, check for the extended assembly
15251 syntax. Unfortunately, the `:' tokens need not be separated by
15252 a space in C, and so, for compatibility, we tolerate that here
15253 too. Doing that means that we have to treat the `::' operator as
15255 if (cp_parser_allow_gnu_extensions_p (parser)
15256 && parser->in_function_body
15257 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15258 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15260 bool inputs_p = false;
15261 bool clobbers_p = false;
15262 bool labels_p = false;
15264 /* The extended syntax was used. */
15267 /* Look for outputs. */
15268 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15270 /* Consume the `:'. */
15271 cp_lexer_consume_token (parser->lexer);
15272 /* Parse the output-operands. */
15273 if (cp_lexer_next_token_is_not (parser->lexer,
15275 && cp_lexer_next_token_is_not (parser->lexer,
15277 && cp_lexer_next_token_is_not (parser->lexer,
15280 outputs = cp_parser_asm_operand_list (parser);
15282 if (outputs == error_mark_node)
15283 invalid_outputs_p = true;
15285 /* If the next token is `::', there are no outputs, and the
15286 next token is the beginning of the inputs. */
15287 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15288 /* The inputs are coming next. */
15291 /* Look for inputs. */
15293 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15295 /* Consume the `:' or `::'. */
15296 cp_lexer_consume_token (parser->lexer);
15297 /* Parse the output-operands. */
15298 if (cp_lexer_next_token_is_not (parser->lexer,
15300 && cp_lexer_next_token_is_not (parser->lexer,
15302 && cp_lexer_next_token_is_not (parser->lexer,
15304 inputs = cp_parser_asm_operand_list (parser);
15306 if (inputs == error_mark_node)
15307 invalid_inputs_p = true;
15309 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15310 /* The clobbers are coming next. */
15313 /* Look for clobbers. */
15315 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15318 /* Consume the `:' or `::'. */
15319 cp_lexer_consume_token (parser->lexer);
15320 /* Parse the clobbers. */
15321 if (cp_lexer_next_token_is_not (parser->lexer,
15323 && cp_lexer_next_token_is_not (parser->lexer,
15325 clobbers = cp_parser_asm_clobber_list (parser);
15328 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15329 /* The labels are coming next. */
15332 /* Look for labels. */
15334 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15337 /* Consume the `:' or `::'. */
15338 cp_lexer_consume_token (parser->lexer);
15339 /* Parse the labels. */
15340 labels = cp_parser_asm_label_list (parser);
15343 if (goto_p && !labels_p)
15344 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15347 missing = RT_COLON_SCOPE;
15349 /* Look for the closing `)'. */
15350 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15351 missing ? missing : RT_CLOSE_PAREN))
15352 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15353 /*consume_paren=*/true);
15354 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15356 if (!invalid_inputs_p && !invalid_outputs_p)
15358 /* Create the ASM_EXPR. */
15359 if (parser->in_function_body)
15361 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15362 inputs, clobbers, labels);
15363 /* If the extended syntax was not used, mark the ASM_EXPR. */
15366 tree temp = asm_stmt;
15367 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15368 temp = TREE_OPERAND (temp, 0);
15370 ASM_INPUT_P (temp) = 1;
15374 cgraph_add_asm_node (string);
15378 /* Declarators [gram.dcl.decl] */
15380 /* Parse an init-declarator.
15383 declarator initializer [opt]
15388 declarator asm-specification [opt] attributes [opt] initializer [opt]
15390 function-definition:
15391 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15393 decl-specifier-seq [opt] declarator function-try-block
15397 function-definition:
15398 __extension__ function-definition
15402 function-definition:
15403 decl-specifier-seq [opt] declarator function-transaction-block
15405 The DECL_SPECIFIERS apply to this declarator. Returns a
15406 representation of the entity declared. If MEMBER_P is TRUE, then
15407 this declarator appears in a class scope. The new DECL created by
15408 this declarator is returned.
15410 The CHECKS are access checks that should be performed once we know
15411 what entity is being declared (and, therefore, what classes have
15414 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15415 for a function-definition here as well. If the declarator is a
15416 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15417 be TRUE upon return. By that point, the function-definition will
15418 have been completely parsed.
15420 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15423 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15424 parsed declaration if it is an uninitialized single declarator not followed
15425 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15426 if present, will not be consumed. If returned, this declarator will be
15427 created with SD_INITIALIZED but will not call cp_finish_decl. */
15430 cp_parser_init_declarator (cp_parser* parser,
15431 cp_decl_specifier_seq *decl_specifiers,
15432 VEC (deferred_access_check,gc)* checks,
15433 bool function_definition_allowed_p,
15435 int declares_class_or_enum,
15436 bool* function_definition_p,
15437 tree* maybe_range_for_decl)
15439 cp_token *token = NULL, *asm_spec_start_token = NULL,
15440 *attributes_start_token = NULL;
15441 cp_declarator *declarator;
15442 tree prefix_attributes;
15444 tree asm_specification;
15446 tree decl = NULL_TREE;
15448 int is_initialized;
15449 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15450 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15452 enum cpp_ttype initialization_kind;
15453 bool is_direct_init = false;
15454 bool is_non_constant_init;
15455 int ctor_dtor_or_conv_p;
15457 tree pushed_scope = NULL_TREE;
15458 bool range_for_decl_p = false;
15460 /* Gather the attributes that were provided with the
15461 decl-specifiers. */
15462 prefix_attributes = decl_specifiers->attributes;
15464 /* Assume that this is not the declarator for a function
15466 if (function_definition_p)
15467 *function_definition_p = false;
15469 /* Defer access checks while parsing the declarator; we cannot know
15470 what names are accessible until we know what is being
15472 resume_deferring_access_checks ();
15474 /* Parse the declarator. */
15475 token = cp_lexer_peek_token (parser->lexer);
15477 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15478 &ctor_dtor_or_conv_p,
15479 /*parenthesized_p=*/NULL,
15481 /* Gather up the deferred checks. */
15482 stop_deferring_access_checks ();
15484 /* If the DECLARATOR was erroneous, there's no need to go
15486 if (declarator == cp_error_declarator)
15487 return error_mark_node;
15489 /* Check that the number of template-parameter-lists is OK. */
15490 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15492 return error_mark_node;
15494 if (declares_class_or_enum & 2)
15495 cp_parser_check_for_definition_in_return_type (declarator,
15496 decl_specifiers->type,
15497 decl_specifiers->type_location);
15499 /* Figure out what scope the entity declared by the DECLARATOR is
15500 located in. `grokdeclarator' sometimes changes the scope, so
15501 we compute it now. */
15502 scope = get_scope_of_declarator (declarator);
15504 /* Perform any lookups in the declared type which were thought to be
15505 dependent, but are not in the scope of the declarator. */
15506 decl_specifiers->type
15507 = maybe_update_decl_type (decl_specifiers->type, scope);
15509 /* If we're allowing GNU extensions, look for an asm-specification
15511 if (cp_parser_allow_gnu_extensions_p (parser))
15513 /* Look for an asm-specification. */
15514 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15515 asm_specification = cp_parser_asm_specification_opt (parser);
15516 /* And attributes. */
15517 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15518 attributes = cp_parser_attributes_opt (parser);
15522 asm_specification = NULL_TREE;
15523 attributes = NULL_TREE;
15526 /* Peek at the next token. */
15527 token = cp_lexer_peek_token (parser->lexer);
15528 /* Check to see if the token indicates the start of a
15529 function-definition. */
15530 if (function_declarator_p (declarator)
15531 && cp_parser_token_starts_function_definition_p (token))
15533 if (!function_definition_allowed_p)
15535 /* If a function-definition should not appear here, issue an
15537 cp_parser_error (parser,
15538 "a function-definition is not allowed here");
15539 return error_mark_node;
15543 location_t func_brace_location
15544 = cp_lexer_peek_token (parser->lexer)->location;
15546 /* Neither attributes nor an asm-specification are allowed
15547 on a function-definition. */
15548 if (asm_specification)
15549 error_at (asm_spec_start_token->location,
15550 "an asm-specification is not allowed "
15551 "on a function-definition");
15553 error_at (attributes_start_token->location,
15554 "attributes are not allowed on a function-definition");
15555 /* This is a function-definition. */
15556 *function_definition_p = true;
15558 /* Parse the function definition. */
15560 decl = cp_parser_save_member_function_body (parser,
15563 prefix_attributes);
15566 = (cp_parser_function_definition_from_specifiers_and_declarator
15567 (parser, decl_specifiers, prefix_attributes, declarator));
15569 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15571 /* This is where the prologue starts... */
15572 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15573 = func_brace_location;
15582 Only in function declarations for constructors, destructors, and
15583 type conversions can the decl-specifier-seq be omitted.
15585 We explicitly postpone this check past the point where we handle
15586 function-definitions because we tolerate function-definitions
15587 that are missing their return types in some modes. */
15588 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15590 cp_parser_error (parser,
15591 "expected constructor, destructor, or type conversion");
15592 return error_mark_node;
15595 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15596 if (token->type == CPP_EQ
15597 || token->type == CPP_OPEN_PAREN
15598 || token->type == CPP_OPEN_BRACE)
15600 is_initialized = SD_INITIALIZED;
15601 initialization_kind = token->type;
15602 if (maybe_range_for_decl)
15603 *maybe_range_for_decl = error_mark_node;
15605 if (token->type == CPP_EQ
15606 && function_declarator_p (declarator))
15608 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15609 if (t2->keyword == RID_DEFAULT)
15610 is_initialized = SD_DEFAULTED;
15611 else if (t2->keyword == RID_DELETE)
15612 is_initialized = SD_DELETED;
15617 /* If the init-declarator isn't initialized and isn't followed by a
15618 `,' or `;', it's not a valid init-declarator. */
15619 if (token->type != CPP_COMMA
15620 && token->type != CPP_SEMICOLON)
15622 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15623 range_for_decl_p = true;
15626 cp_parser_error (parser, "expected initializer");
15627 return error_mark_node;
15630 is_initialized = SD_UNINITIALIZED;
15631 initialization_kind = CPP_EOF;
15634 /* Because start_decl has side-effects, we should only call it if we
15635 know we're going ahead. By this point, we know that we cannot
15636 possibly be looking at any other construct. */
15637 cp_parser_commit_to_tentative_parse (parser);
15639 /* If the decl specifiers were bad, issue an error now that we're
15640 sure this was intended to be a declarator. Then continue
15641 declaring the variable(s), as int, to try to cut down on further
15643 if (decl_specifiers->any_specifiers_p
15644 && decl_specifiers->type == error_mark_node)
15646 cp_parser_error (parser, "invalid type in declaration");
15647 decl_specifiers->type = integer_type_node;
15650 /* Check to see whether or not this declaration is a friend. */
15651 friend_p = cp_parser_friend_p (decl_specifiers);
15653 /* Enter the newly declared entry in the symbol table. If we're
15654 processing a declaration in a class-specifier, we wait until
15655 after processing the initializer. */
15658 if (parser->in_unbraced_linkage_specification_p)
15659 decl_specifiers->storage_class = sc_extern;
15660 decl = start_decl (declarator, decl_specifiers,
15661 range_for_decl_p? SD_INITIALIZED : is_initialized,
15662 attributes, prefix_attributes,
15664 /* Adjust location of decl if declarator->id_loc is more appropriate:
15665 set, and decl wasn't merged with another decl, in which case its
15666 location would be different from input_location, and more accurate. */
15668 && declarator->id_loc != UNKNOWN_LOCATION
15669 && DECL_SOURCE_LOCATION (decl) == input_location)
15670 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15673 /* Enter the SCOPE. That way unqualified names appearing in the
15674 initializer will be looked up in SCOPE. */
15675 pushed_scope = push_scope (scope);
15677 /* Perform deferred access control checks, now that we know in which
15678 SCOPE the declared entity resides. */
15679 if (!member_p && decl)
15681 tree saved_current_function_decl = NULL_TREE;
15683 /* If the entity being declared is a function, pretend that we
15684 are in its scope. If it is a `friend', it may have access to
15685 things that would not otherwise be accessible. */
15686 if (TREE_CODE (decl) == FUNCTION_DECL)
15688 saved_current_function_decl = current_function_decl;
15689 current_function_decl = decl;
15692 /* Perform access checks for template parameters. */
15693 cp_parser_perform_template_parameter_access_checks (checks);
15695 /* Perform the access control checks for the declarator and the
15696 decl-specifiers. */
15697 perform_deferred_access_checks ();
15699 /* Restore the saved value. */
15700 if (TREE_CODE (decl) == FUNCTION_DECL)
15701 current_function_decl = saved_current_function_decl;
15704 /* Parse the initializer. */
15705 initializer = NULL_TREE;
15706 is_direct_init = false;
15707 is_non_constant_init = true;
15708 if (is_initialized)
15710 if (function_declarator_p (declarator))
15712 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15713 if (initialization_kind == CPP_EQ)
15714 initializer = cp_parser_pure_specifier (parser);
15717 /* If the declaration was erroneous, we don't really
15718 know what the user intended, so just silently
15719 consume the initializer. */
15720 if (decl != error_mark_node)
15721 error_at (initializer_start_token->location,
15722 "initializer provided for function");
15723 cp_parser_skip_to_closing_parenthesis (parser,
15724 /*recovering=*/true,
15725 /*or_comma=*/false,
15726 /*consume_paren=*/true);
15731 /* We want to record the extra mangling scope for in-class
15732 initializers of class members and initializers of static data
15733 member templates. The former involves deferring
15734 parsing of the initializer until end of class as with default
15735 arguments. So right here we only handle the latter. */
15736 if (!member_p && processing_template_decl)
15737 start_lambda_scope (decl);
15738 initializer = cp_parser_initializer (parser,
15740 &is_non_constant_init);
15741 if (!member_p && processing_template_decl)
15742 finish_lambda_scope ();
15746 /* The old parser allows attributes to appear after a parenthesized
15747 initializer. Mark Mitchell proposed removing this functionality
15748 on the GCC mailing lists on 2002-08-13. This parser accepts the
15749 attributes -- but ignores them. */
15750 if (cp_parser_allow_gnu_extensions_p (parser)
15751 && initialization_kind == CPP_OPEN_PAREN)
15752 if (cp_parser_attributes_opt (parser))
15753 warning (OPT_Wattributes,
15754 "attributes after parenthesized initializer ignored");
15756 /* For an in-class declaration, use `grokfield' to create the
15762 pop_scope (pushed_scope);
15763 pushed_scope = NULL_TREE;
15765 decl = grokfield (declarator, decl_specifiers,
15766 initializer, !is_non_constant_init,
15767 /*asmspec=*/NULL_TREE,
15768 prefix_attributes);
15769 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15770 cp_parser_save_default_args (parser, decl);
15773 /* Finish processing the declaration. But, skip member
15775 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15777 cp_finish_decl (decl,
15778 initializer, !is_non_constant_init,
15780 /* If the initializer is in parentheses, then this is
15781 a direct-initialization, which means that an
15782 `explicit' constructor is OK. Otherwise, an
15783 `explicit' constructor cannot be used. */
15784 ((is_direct_init || !is_initialized)
15785 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15787 else if ((cxx_dialect != cxx98) && friend_p
15788 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15789 /* Core issue #226 (C++0x only): A default template-argument
15790 shall not be specified in a friend class template
15792 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15793 /*is_partial=*/0, /*is_friend_decl=*/1);
15795 if (!friend_p && pushed_scope)
15796 pop_scope (pushed_scope);
15801 /* Parse a declarator.
15805 ptr-operator declarator
15807 abstract-declarator:
15808 ptr-operator abstract-declarator [opt]
15809 direct-abstract-declarator
15814 attributes [opt] direct-declarator
15815 attributes [opt] ptr-operator declarator
15817 abstract-declarator:
15818 attributes [opt] ptr-operator abstract-declarator [opt]
15819 attributes [opt] direct-abstract-declarator
15821 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15822 detect constructor, destructor or conversion operators. It is set
15823 to -1 if the declarator is a name, and +1 if it is a
15824 function. Otherwise it is set to zero. Usually you just want to
15825 test for >0, but internally the negative value is used.
15827 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15828 a decl-specifier-seq unless it declares a constructor, destructor,
15829 or conversion. It might seem that we could check this condition in
15830 semantic analysis, rather than parsing, but that makes it difficult
15831 to handle something like `f()'. We want to notice that there are
15832 no decl-specifiers, and therefore realize that this is an
15833 expression, not a declaration.)
15835 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15836 the declarator is a direct-declarator of the form "(...)".
15838 MEMBER_P is true iff this declarator is a member-declarator. */
15840 static cp_declarator *
15841 cp_parser_declarator (cp_parser* parser,
15842 cp_parser_declarator_kind dcl_kind,
15843 int* ctor_dtor_or_conv_p,
15844 bool* parenthesized_p,
15847 cp_declarator *declarator;
15848 enum tree_code code;
15849 cp_cv_quals cv_quals;
15851 tree attributes = NULL_TREE;
15853 /* Assume this is not a constructor, destructor, or type-conversion
15855 if (ctor_dtor_or_conv_p)
15856 *ctor_dtor_or_conv_p = 0;
15858 if (cp_parser_allow_gnu_extensions_p (parser))
15859 attributes = cp_parser_attributes_opt (parser);
15861 /* Check for the ptr-operator production. */
15862 cp_parser_parse_tentatively (parser);
15863 /* Parse the ptr-operator. */
15864 code = cp_parser_ptr_operator (parser,
15867 /* If that worked, then we have a ptr-operator. */
15868 if (cp_parser_parse_definitely (parser))
15870 /* If a ptr-operator was found, then this declarator was not
15872 if (parenthesized_p)
15873 *parenthesized_p = true;
15874 /* The dependent declarator is optional if we are parsing an
15875 abstract-declarator. */
15876 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15877 cp_parser_parse_tentatively (parser);
15879 /* Parse the dependent declarator. */
15880 declarator = cp_parser_declarator (parser, dcl_kind,
15881 /*ctor_dtor_or_conv_p=*/NULL,
15882 /*parenthesized_p=*/NULL,
15883 /*member_p=*/false);
15885 /* If we are parsing an abstract-declarator, we must handle the
15886 case where the dependent declarator is absent. */
15887 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15888 && !cp_parser_parse_definitely (parser))
15891 declarator = cp_parser_make_indirect_declarator
15892 (code, class_type, cv_quals, declarator);
15894 /* Everything else is a direct-declarator. */
15897 if (parenthesized_p)
15898 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15900 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15901 ctor_dtor_or_conv_p,
15905 if (attributes && declarator && declarator != cp_error_declarator)
15906 declarator->attributes = attributes;
15911 /* Parse a direct-declarator or direct-abstract-declarator.
15915 direct-declarator ( parameter-declaration-clause )
15916 cv-qualifier-seq [opt]
15917 exception-specification [opt]
15918 direct-declarator [ constant-expression [opt] ]
15921 direct-abstract-declarator:
15922 direct-abstract-declarator [opt]
15923 ( parameter-declaration-clause )
15924 cv-qualifier-seq [opt]
15925 exception-specification [opt]
15926 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15927 ( abstract-declarator )
15929 Returns a representation of the declarator. DCL_KIND is
15930 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15931 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15932 we are parsing a direct-declarator. It is
15933 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15934 of ambiguity we prefer an abstract declarator, as per
15935 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15936 cp_parser_declarator. */
15938 static cp_declarator *
15939 cp_parser_direct_declarator (cp_parser* parser,
15940 cp_parser_declarator_kind dcl_kind,
15941 int* ctor_dtor_or_conv_p,
15945 cp_declarator *declarator = NULL;
15946 tree scope = NULL_TREE;
15947 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15948 bool saved_in_declarator_p = parser->in_declarator_p;
15950 tree pushed_scope = NULL_TREE;
15954 /* Peek at the next token. */
15955 token = cp_lexer_peek_token (parser->lexer);
15956 if (token->type == CPP_OPEN_PAREN)
15958 /* This is either a parameter-declaration-clause, or a
15959 parenthesized declarator. When we know we are parsing a
15960 named declarator, it must be a parenthesized declarator
15961 if FIRST is true. For instance, `(int)' is a
15962 parameter-declaration-clause, with an omitted
15963 direct-abstract-declarator. But `((*))', is a
15964 parenthesized abstract declarator. Finally, when T is a
15965 template parameter `(T)' is a
15966 parameter-declaration-clause, and not a parenthesized
15969 We first try and parse a parameter-declaration-clause,
15970 and then try a nested declarator (if FIRST is true).
15972 It is not an error for it not to be a
15973 parameter-declaration-clause, even when FIRST is
15979 The first is the declaration of a function while the
15980 second is the definition of a variable, including its
15983 Having seen only the parenthesis, we cannot know which of
15984 these two alternatives should be selected. Even more
15985 complex are examples like:
15990 The former is a function-declaration; the latter is a
15991 variable initialization.
15993 Thus again, we try a parameter-declaration-clause, and if
15994 that fails, we back out and return. */
15996 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15999 unsigned saved_num_template_parameter_lists;
16000 bool is_declarator = false;
16003 /* In a member-declarator, the only valid interpretation
16004 of a parenthesis is the start of a
16005 parameter-declaration-clause. (It is invalid to
16006 initialize a static data member with a parenthesized
16007 initializer; only the "=" form of initialization is
16010 cp_parser_parse_tentatively (parser);
16012 /* Consume the `('. */
16013 cp_lexer_consume_token (parser->lexer);
16016 /* If this is going to be an abstract declarator, we're
16017 in a declarator and we can't have default args. */
16018 parser->default_arg_ok_p = false;
16019 parser->in_declarator_p = true;
16022 /* Inside the function parameter list, surrounding
16023 template-parameter-lists do not apply. */
16024 saved_num_template_parameter_lists
16025 = parser->num_template_parameter_lists;
16026 parser->num_template_parameter_lists = 0;
16028 begin_scope (sk_function_parms, NULL_TREE);
16030 /* Parse the parameter-declaration-clause. */
16031 params = cp_parser_parameter_declaration_clause (parser);
16033 parser->num_template_parameter_lists
16034 = saved_num_template_parameter_lists;
16036 /* Consume the `)'. */
16037 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16039 /* If all went well, parse the cv-qualifier-seq and the
16040 exception-specification. */
16041 if (member_p || cp_parser_parse_definitely (parser))
16043 cp_cv_quals cv_quals;
16044 cp_virt_specifiers virt_specifiers;
16045 tree exception_specification;
16048 is_declarator = true;
16050 if (ctor_dtor_or_conv_p)
16051 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16054 /* Parse the cv-qualifier-seq. */
16055 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16056 /* And the exception-specification. */
16057 exception_specification
16058 = cp_parser_exception_specification_opt (parser);
16059 /* Parse the virt-specifier-seq. */
16060 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16062 late_return = (cp_parser_late_return_type_opt
16063 (parser, member_p ? cv_quals : -1));
16065 /* Create the function-declarator. */
16066 declarator = make_call_declarator (declarator,
16070 exception_specification,
16072 /* Any subsequent parameter lists are to do with
16073 return type, so are not those of the declared
16075 parser->default_arg_ok_p = false;
16078 /* Remove the function parms from scope. */
16079 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16080 pop_binding (DECL_NAME (t), t);
16084 /* Repeat the main loop. */
16088 /* If this is the first, we can try a parenthesized
16092 bool saved_in_type_id_in_expr_p;
16094 parser->default_arg_ok_p = saved_default_arg_ok_p;
16095 parser->in_declarator_p = saved_in_declarator_p;
16097 /* Consume the `('. */
16098 cp_lexer_consume_token (parser->lexer);
16099 /* Parse the nested declarator. */
16100 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16101 parser->in_type_id_in_expr_p = true;
16103 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16104 /*parenthesized_p=*/NULL,
16106 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16108 /* Expect a `)'. */
16109 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16110 declarator = cp_error_declarator;
16111 if (declarator == cp_error_declarator)
16114 goto handle_declarator;
16116 /* Otherwise, we must be done. */
16120 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16121 && token->type == CPP_OPEN_SQUARE)
16123 /* Parse an array-declarator. */
16126 if (ctor_dtor_or_conv_p)
16127 *ctor_dtor_or_conv_p = 0;
16130 parser->default_arg_ok_p = false;
16131 parser->in_declarator_p = true;
16132 /* Consume the `['. */
16133 cp_lexer_consume_token (parser->lexer);
16134 /* Peek at the next token. */
16135 token = cp_lexer_peek_token (parser->lexer);
16136 /* If the next token is `]', then there is no
16137 constant-expression. */
16138 if (token->type != CPP_CLOSE_SQUARE)
16140 bool non_constant_p;
16143 = cp_parser_constant_expression (parser,
16144 /*allow_non_constant=*/true,
16146 if (!non_constant_p)
16148 else if (error_operand_p (bounds))
16149 /* Already gave an error. */;
16150 else if (!parser->in_function_body
16151 || current_binding_level->kind == sk_function_parms)
16153 /* Normally, the array bound must be an integral constant
16154 expression. However, as an extension, we allow VLAs
16155 in function scopes as long as they aren't part of a
16156 parameter declaration. */
16157 cp_parser_error (parser,
16158 "array bound is not an integer constant");
16159 bounds = error_mark_node;
16161 else if (processing_template_decl)
16163 /* Remember this wasn't a constant-expression. */
16164 bounds = build_nop (TREE_TYPE (bounds), bounds);
16165 TREE_SIDE_EFFECTS (bounds) = 1;
16169 bounds = NULL_TREE;
16170 /* Look for the closing `]'. */
16171 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16173 declarator = cp_error_declarator;
16177 declarator = make_array_declarator (declarator, bounds);
16179 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16182 tree qualifying_scope;
16183 tree unqualified_name;
16184 special_function_kind sfk;
16186 bool pack_expansion_p = false;
16187 cp_token *declarator_id_start_token;
16189 /* Parse a declarator-id */
16190 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16193 cp_parser_parse_tentatively (parser);
16195 /* If we see an ellipsis, we should be looking at a
16197 if (token->type == CPP_ELLIPSIS)
16199 /* Consume the `...' */
16200 cp_lexer_consume_token (parser->lexer);
16202 pack_expansion_p = true;
16206 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16208 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16209 qualifying_scope = parser->scope;
16214 if (!unqualified_name && pack_expansion_p)
16216 /* Check whether an error occurred. */
16217 okay = !cp_parser_error_occurred (parser);
16219 /* We already consumed the ellipsis to mark a
16220 parameter pack, but we have no way to report it,
16221 so abort the tentative parse. We will be exiting
16222 immediately anyway. */
16223 cp_parser_abort_tentative_parse (parser);
16226 okay = cp_parser_parse_definitely (parser);
16229 unqualified_name = error_mark_node;
16230 else if (unqualified_name
16231 && (qualifying_scope
16232 || (TREE_CODE (unqualified_name)
16233 != IDENTIFIER_NODE)))
16235 cp_parser_error (parser, "expected unqualified-id");
16236 unqualified_name = error_mark_node;
16240 if (!unqualified_name)
16242 if (unqualified_name == error_mark_node)
16244 declarator = cp_error_declarator;
16245 pack_expansion_p = false;
16246 declarator->parameter_pack_p = false;
16250 if (qualifying_scope && at_namespace_scope_p ()
16251 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16253 /* In the declaration of a member of a template class
16254 outside of the class itself, the SCOPE will sometimes
16255 be a TYPENAME_TYPE. For example, given:
16257 template <typename T>
16258 int S<T>::R::i = 3;
16260 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16261 this context, we must resolve S<T>::R to an ordinary
16262 type, rather than a typename type.
16264 The reason we normally avoid resolving TYPENAME_TYPEs
16265 is that a specialization of `S' might render
16266 `S<T>::R' not a type. However, if `S' is
16267 specialized, then this `i' will not be used, so there
16268 is no harm in resolving the types here. */
16271 /* Resolve the TYPENAME_TYPE. */
16272 type = resolve_typename_type (qualifying_scope,
16273 /*only_current_p=*/false);
16274 /* If that failed, the declarator is invalid. */
16275 if (TREE_CODE (type) == TYPENAME_TYPE)
16277 if (typedef_variant_p (type))
16278 error_at (declarator_id_start_token->location,
16279 "cannot define member of dependent typedef "
16282 error_at (declarator_id_start_token->location,
16283 "%<%T::%E%> is not a type",
16284 TYPE_CONTEXT (qualifying_scope),
16285 TYPE_IDENTIFIER (qualifying_scope));
16287 qualifying_scope = type;
16292 if (unqualified_name)
16296 if (qualifying_scope
16297 && CLASS_TYPE_P (qualifying_scope))
16298 class_type = qualifying_scope;
16300 class_type = current_class_type;
16302 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16304 tree name_type = TREE_TYPE (unqualified_name);
16305 if (class_type && same_type_p (name_type, class_type))
16307 if (qualifying_scope
16308 && CLASSTYPE_USE_TEMPLATE (name_type))
16310 error_at (declarator_id_start_token->location,
16311 "invalid use of constructor as a template");
16312 inform (declarator_id_start_token->location,
16313 "use %<%T::%D%> instead of %<%T::%D%> to "
16314 "name the constructor in a qualified name",
16316 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16317 class_type, name_type);
16318 declarator = cp_error_declarator;
16322 unqualified_name = constructor_name (class_type);
16326 /* We do not attempt to print the declarator
16327 here because we do not have enough
16328 information about its original syntactic
16330 cp_parser_error (parser, "invalid declarator");
16331 declarator = cp_error_declarator;
16338 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16339 sfk = sfk_destructor;
16340 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16341 sfk = sfk_conversion;
16342 else if (/* There's no way to declare a constructor
16343 for an anonymous type, even if the type
16344 got a name for linkage purposes. */
16345 !TYPE_WAS_ANONYMOUS (class_type)
16346 && constructor_name_p (unqualified_name,
16349 unqualified_name = constructor_name (class_type);
16350 sfk = sfk_constructor;
16352 else if (is_overloaded_fn (unqualified_name)
16353 && DECL_CONSTRUCTOR_P (get_first_fn
16354 (unqualified_name)))
16355 sfk = sfk_constructor;
16357 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16358 *ctor_dtor_or_conv_p = -1;
16361 declarator = make_id_declarator (qualifying_scope,
16364 declarator->id_loc = token->location;
16365 declarator->parameter_pack_p = pack_expansion_p;
16367 if (pack_expansion_p)
16368 maybe_warn_variadic_templates ();
16371 handle_declarator:;
16372 scope = get_scope_of_declarator (declarator);
16374 /* Any names that appear after the declarator-id for a
16375 member are looked up in the containing scope. */
16376 pushed_scope = push_scope (scope);
16377 parser->in_declarator_p = true;
16378 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16379 || (declarator && declarator->kind == cdk_id))
16380 /* Default args are only allowed on function
16382 parser->default_arg_ok_p = saved_default_arg_ok_p;
16384 parser->default_arg_ok_p = false;
16393 /* For an abstract declarator, we might wind up with nothing at this
16394 point. That's an error; the declarator is not optional. */
16396 cp_parser_error (parser, "expected declarator");
16398 /* If we entered a scope, we must exit it now. */
16400 pop_scope (pushed_scope);
16402 parser->default_arg_ok_p = saved_default_arg_ok_p;
16403 parser->in_declarator_p = saved_in_declarator_p;
16408 /* Parse a ptr-operator.
16411 * cv-qualifier-seq [opt]
16413 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16418 & cv-qualifier-seq [opt]
16420 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16421 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16422 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16423 filled in with the TYPE containing the member. *CV_QUALS is
16424 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16425 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16426 Note that the tree codes returned by this function have nothing
16427 to do with the types of trees that will be eventually be created
16428 to represent the pointer or reference type being parsed. They are
16429 just constants with suggestive names. */
16430 static enum tree_code
16431 cp_parser_ptr_operator (cp_parser* parser,
16433 cp_cv_quals *cv_quals)
16435 enum tree_code code = ERROR_MARK;
16438 /* Assume that it's not a pointer-to-member. */
16440 /* And that there are no cv-qualifiers. */
16441 *cv_quals = TYPE_UNQUALIFIED;
16443 /* Peek at the next token. */
16444 token = cp_lexer_peek_token (parser->lexer);
16446 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16447 if (token->type == CPP_MULT)
16448 code = INDIRECT_REF;
16449 else if (token->type == CPP_AND)
16451 else if ((cxx_dialect != cxx98) &&
16452 token->type == CPP_AND_AND) /* C++0x only */
16453 code = NON_LVALUE_EXPR;
16455 if (code != ERROR_MARK)
16457 /* Consume the `*', `&' or `&&'. */
16458 cp_lexer_consume_token (parser->lexer);
16460 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16461 `&', if we are allowing GNU extensions. (The only qualifier
16462 that can legally appear after `&' is `restrict', but that is
16463 enforced during semantic analysis. */
16464 if (code == INDIRECT_REF
16465 || cp_parser_allow_gnu_extensions_p (parser))
16466 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16470 /* Try the pointer-to-member case. */
16471 cp_parser_parse_tentatively (parser);
16472 /* Look for the optional `::' operator. */
16473 cp_parser_global_scope_opt (parser,
16474 /*current_scope_valid_p=*/false);
16475 /* Look for the nested-name specifier. */
16476 token = cp_lexer_peek_token (parser->lexer);
16477 cp_parser_nested_name_specifier (parser,
16478 /*typename_keyword_p=*/false,
16479 /*check_dependency_p=*/true,
16481 /*is_declaration=*/false);
16482 /* If we found it, and the next token is a `*', then we are
16483 indeed looking at a pointer-to-member operator. */
16484 if (!cp_parser_error_occurred (parser)
16485 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16487 /* Indicate that the `*' operator was used. */
16488 code = INDIRECT_REF;
16490 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16491 error_at (token->location, "%qD is a namespace", parser->scope);
16492 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16493 error_at (token->location, "cannot form pointer to member of "
16494 "non-class %q#T", parser->scope);
16497 /* The type of which the member is a member is given by the
16499 *type = parser->scope;
16500 /* The next name will not be qualified. */
16501 parser->scope = NULL_TREE;
16502 parser->qualifying_scope = NULL_TREE;
16503 parser->object_scope = NULL_TREE;
16504 /* Look for the optional cv-qualifier-seq. */
16505 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16508 /* If that didn't work we don't have a ptr-operator. */
16509 if (!cp_parser_parse_definitely (parser))
16510 cp_parser_error (parser, "expected ptr-operator");
16516 /* Parse an (optional) cv-qualifier-seq.
16519 cv-qualifier cv-qualifier-seq [opt]
16530 Returns a bitmask representing the cv-qualifiers. */
16533 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16535 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16540 cp_cv_quals cv_qualifier;
16542 /* Peek at the next token. */
16543 token = cp_lexer_peek_token (parser->lexer);
16544 /* See if it's a cv-qualifier. */
16545 switch (token->keyword)
16548 cv_qualifier = TYPE_QUAL_CONST;
16552 cv_qualifier = TYPE_QUAL_VOLATILE;
16556 cv_qualifier = TYPE_QUAL_RESTRICT;
16560 cv_qualifier = TYPE_UNQUALIFIED;
16567 if (cv_quals & cv_qualifier)
16569 error_at (token->location, "duplicate cv-qualifier");
16570 cp_lexer_purge_token (parser->lexer);
16574 cp_lexer_consume_token (parser->lexer);
16575 cv_quals |= cv_qualifier;
16582 /* Parse an (optional) virt-specifier-seq.
16584 virt-specifier-seq:
16585 virt-specifier virt-specifier-seq [opt]
16591 Returns a bitmask representing the virt-specifiers. */
16593 static cp_virt_specifiers
16594 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16596 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16601 cp_virt_specifiers virt_specifier;
16603 /* Peek at the next token. */
16604 token = cp_lexer_peek_token (parser->lexer);
16605 /* See if it's a virt-specifier-qualifier. */
16606 if (token->type != CPP_NAME)
16608 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16610 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16611 virt_specifier = VIRT_SPEC_OVERRIDE;
16613 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16615 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16616 virt_specifier = VIRT_SPEC_FINAL;
16618 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16620 virt_specifier = VIRT_SPEC_FINAL;
16625 if (virt_specifiers & virt_specifier)
16627 error_at (token->location, "duplicate virt-specifier");
16628 cp_lexer_purge_token (parser->lexer);
16632 cp_lexer_consume_token (parser->lexer);
16633 virt_specifiers |= virt_specifier;
16636 return virt_specifiers;
16639 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16640 is in scope even though it isn't real. */
16643 inject_this_parameter (tree ctype, cp_cv_quals quals)
16647 if (current_class_ptr)
16649 /* We don't clear this between NSDMIs. Is it already what we want? */
16650 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16651 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16652 && cp_type_quals (type) == quals)
16656 this_parm = build_this_parm (ctype, quals);
16657 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16658 current_class_ptr = NULL_TREE;
16660 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16661 current_class_ptr = this_parm;
16664 /* Parse a late-specified return type, if any. This is not a separate
16665 non-terminal, but part of a function declarator, which looks like
16667 -> trailing-type-specifier-seq abstract-declarator(opt)
16669 Returns the type indicated by the type-id.
16671 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16675 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16680 /* Peek at the next token. */
16681 token = cp_lexer_peek_token (parser->lexer);
16682 /* A late-specified return type is indicated by an initial '->'. */
16683 if (token->type != CPP_DEREF)
16686 /* Consume the ->. */
16687 cp_lexer_consume_token (parser->lexer);
16691 /* DR 1207: 'this' is in scope in the trailing return type. */
16692 gcc_assert (current_class_ptr == NULL_TREE);
16693 inject_this_parameter (current_class_type, quals);
16696 type = cp_parser_trailing_type_id (parser);
16699 current_class_ptr = current_class_ref = NULL_TREE;
16704 /* Parse a declarator-id.
16708 :: [opt] nested-name-specifier [opt] type-name
16710 In the `id-expression' case, the value returned is as for
16711 cp_parser_id_expression if the id-expression was an unqualified-id.
16712 If the id-expression was a qualified-id, then a SCOPE_REF is
16713 returned. The first operand is the scope (either a NAMESPACE_DECL
16714 or TREE_TYPE), but the second is still just a representation of an
16718 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16721 /* The expression must be an id-expression. Assume that qualified
16722 names are the names of types so that:
16725 int S<T>::R::i = 3;
16727 will work; we must treat `S<T>::R' as the name of a type.
16728 Similarly, assume that qualified names are templates, where
16732 int S<T>::R<T>::i = 3;
16735 id = cp_parser_id_expression (parser,
16736 /*template_keyword_p=*/false,
16737 /*check_dependency_p=*/false,
16738 /*template_p=*/NULL,
16739 /*declarator_p=*/true,
16741 if (id && BASELINK_P (id))
16742 id = BASELINK_FUNCTIONS (id);
16746 /* Parse a type-id.
16749 type-specifier-seq abstract-declarator [opt]
16751 Returns the TYPE specified. */
16754 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16755 bool is_trailing_return)
16757 cp_decl_specifier_seq type_specifier_seq;
16758 cp_declarator *abstract_declarator;
16760 /* Parse the type-specifier-seq. */
16761 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16762 is_trailing_return,
16763 &type_specifier_seq);
16764 if (type_specifier_seq.type == error_mark_node)
16765 return error_mark_node;
16767 /* There might or might not be an abstract declarator. */
16768 cp_parser_parse_tentatively (parser);
16769 /* Look for the declarator. */
16770 abstract_declarator
16771 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16772 /*parenthesized_p=*/NULL,
16773 /*member_p=*/false);
16774 /* Check to see if there really was a declarator. */
16775 if (!cp_parser_parse_definitely (parser))
16776 abstract_declarator = NULL;
16778 if (type_specifier_seq.type
16779 && type_uses_auto (type_specifier_seq.type))
16781 /* A type-id with type 'auto' is only ok if the abstract declarator
16782 is a function declarator with a late-specified return type. */
16783 if (abstract_declarator
16784 && abstract_declarator->kind == cdk_function
16785 && abstract_declarator->u.function.late_return_type)
16789 error ("invalid use of %<auto%>");
16790 return error_mark_node;
16794 return groktypename (&type_specifier_seq, abstract_declarator,
16798 static tree cp_parser_type_id (cp_parser *parser)
16800 return cp_parser_type_id_1 (parser, false, false);
16803 static tree cp_parser_template_type_arg (cp_parser *parser)
16806 const char *saved_message = parser->type_definition_forbidden_message;
16807 parser->type_definition_forbidden_message
16808 = G_("types may not be defined in template arguments");
16809 r = cp_parser_type_id_1 (parser, true, false);
16810 parser->type_definition_forbidden_message = saved_message;
16814 static tree cp_parser_trailing_type_id (cp_parser *parser)
16816 return cp_parser_type_id_1 (parser, false, true);
16819 /* Parse a type-specifier-seq.
16821 type-specifier-seq:
16822 type-specifier type-specifier-seq [opt]
16826 type-specifier-seq:
16827 attributes type-specifier-seq [opt]
16829 If IS_DECLARATION is true, we are at the start of a "condition" or
16830 exception-declaration, so we might be followed by a declarator-id.
16832 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16833 i.e. we've just seen "->".
16835 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16838 cp_parser_type_specifier_seq (cp_parser* parser,
16839 bool is_declaration,
16840 bool is_trailing_return,
16841 cp_decl_specifier_seq *type_specifier_seq)
16843 bool seen_type_specifier = false;
16844 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16845 cp_token *start_token = NULL;
16847 /* Clear the TYPE_SPECIFIER_SEQ. */
16848 clear_decl_specs (type_specifier_seq);
16850 /* In the context of a trailing return type, enum E { } is an
16851 elaborated-type-specifier followed by a function-body, not an
16853 if (is_trailing_return)
16854 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16856 /* Parse the type-specifiers and attributes. */
16859 tree type_specifier;
16860 bool is_cv_qualifier;
16862 /* Check for attributes first. */
16863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16865 type_specifier_seq->attributes =
16866 chainon (type_specifier_seq->attributes,
16867 cp_parser_attributes_opt (parser));
16871 /* record the token of the beginning of the type specifier seq,
16872 for error reporting purposes*/
16874 start_token = cp_lexer_peek_token (parser->lexer);
16876 /* Look for the type-specifier. */
16877 type_specifier = cp_parser_type_specifier (parser,
16879 type_specifier_seq,
16880 /*is_declaration=*/false,
16883 if (!type_specifier)
16885 /* If the first type-specifier could not be found, this is not a
16886 type-specifier-seq at all. */
16887 if (!seen_type_specifier)
16889 cp_parser_error (parser, "expected type-specifier");
16890 type_specifier_seq->type = error_mark_node;
16893 /* If subsequent type-specifiers could not be found, the
16894 type-specifier-seq is complete. */
16898 seen_type_specifier = true;
16899 /* The standard says that a condition can be:
16901 type-specifier-seq declarator = assignment-expression
16908 we should treat the "S" as a declarator, not as a
16909 type-specifier. The standard doesn't say that explicitly for
16910 type-specifier-seq, but it does say that for
16911 decl-specifier-seq in an ordinary declaration. Perhaps it
16912 would be clearer just to allow a decl-specifier-seq here, and
16913 then add a semantic restriction that if any decl-specifiers
16914 that are not type-specifiers appear, the program is invalid. */
16915 if (is_declaration && !is_cv_qualifier)
16916 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16919 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16922 /* Parse a parameter-declaration-clause.
16924 parameter-declaration-clause:
16925 parameter-declaration-list [opt] ... [opt]
16926 parameter-declaration-list , ...
16928 Returns a representation for the parameter declarations. A return
16929 value of NULL indicates a parameter-declaration-clause consisting
16930 only of an ellipsis. */
16933 cp_parser_parameter_declaration_clause (cp_parser* parser)
16940 /* Peek at the next token. */
16941 token = cp_lexer_peek_token (parser->lexer);
16942 /* Check for trivial parameter-declaration-clauses. */
16943 if (token->type == CPP_ELLIPSIS)
16945 /* Consume the `...' token. */
16946 cp_lexer_consume_token (parser->lexer);
16949 else if (token->type == CPP_CLOSE_PAREN)
16950 /* There are no parameters. */
16952 #ifndef NO_IMPLICIT_EXTERN_C
16953 if (in_system_header && current_class_type == NULL
16954 && current_lang_name == lang_name_c)
16958 return void_list_node;
16960 /* Check for `(void)', too, which is a special case. */
16961 else if (token->keyword == RID_VOID
16962 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16963 == CPP_CLOSE_PAREN))
16965 /* Consume the `void' token. */
16966 cp_lexer_consume_token (parser->lexer);
16967 /* There are no parameters. */
16968 return void_list_node;
16971 /* Parse the parameter-declaration-list. */
16972 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16973 /* If a parse error occurred while parsing the
16974 parameter-declaration-list, then the entire
16975 parameter-declaration-clause is erroneous. */
16979 /* Peek at the next token. */
16980 token = cp_lexer_peek_token (parser->lexer);
16981 /* If it's a `,', the clause should terminate with an ellipsis. */
16982 if (token->type == CPP_COMMA)
16984 /* Consume the `,'. */
16985 cp_lexer_consume_token (parser->lexer);
16986 /* Expect an ellipsis. */
16988 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16990 /* It might also be `...' if the optional trailing `,' was
16992 else if (token->type == CPP_ELLIPSIS)
16994 /* Consume the `...' token. */
16995 cp_lexer_consume_token (parser->lexer);
16996 /* And remember that we saw it. */
17000 ellipsis_p = false;
17002 /* Finish the parameter list. */
17004 parameters = chainon (parameters, void_list_node);
17009 /* Parse a parameter-declaration-list.
17011 parameter-declaration-list:
17012 parameter-declaration
17013 parameter-declaration-list , parameter-declaration
17015 Returns a representation of the parameter-declaration-list, as for
17016 cp_parser_parameter_declaration_clause. However, the
17017 `void_list_node' is never appended to the list. Upon return,
17018 *IS_ERROR will be true iff an error occurred. */
17021 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17023 tree parameters = NULL_TREE;
17024 tree *tail = ¶meters;
17025 bool saved_in_unbraced_linkage_specification_p;
17028 /* Assume all will go well. */
17030 /* The special considerations that apply to a function within an
17031 unbraced linkage specifications do not apply to the parameters
17032 to the function. */
17033 saved_in_unbraced_linkage_specification_p
17034 = parser->in_unbraced_linkage_specification_p;
17035 parser->in_unbraced_linkage_specification_p = false;
17037 /* Look for more parameters. */
17040 cp_parameter_declarator *parameter;
17041 tree decl = error_mark_node;
17042 bool parenthesized_p = false;
17043 /* Parse the parameter. */
17045 = cp_parser_parameter_declaration (parser,
17046 /*template_parm_p=*/false,
17049 /* We don't know yet if the enclosing context is deprecated, so wait
17050 and warn in grokparms if appropriate. */
17051 deprecated_state = DEPRECATED_SUPPRESS;
17054 decl = grokdeclarator (parameter->declarator,
17055 ¶meter->decl_specifiers,
17057 parameter->default_argument != NULL_TREE,
17058 ¶meter->decl_specifiers.attributes);
17060 deprecated_state = DEPRECATED_NORMAL;
17062 /* If a parse error occurred parsing the parameter declaration,
17063 then the entire parameter-declaration-list is erroneous. */
17064 if (decl == error_mark_node)
17067 parameters = error_mark_node;
17071 if (parameter->decl_specifiers.attributes)
17072 cplus_decl_attributes (&decl,
17073 parameter->decl_specifiers.attributes,
17075 if (DECL_NAME (decl))
17076 decl = pushdecl (decl);
17078 if (decl != error_mark_node)
17080 retrofit_lang_decl (decl);
17081 DECL_PARM_INDEX (decl) = ++index;
17082 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17085 /* Add the new parameter to the list. */
17086 *tail = build_tree_list (parameter->default_argument, decl);
17087 tail = &TREE_CHAIN (*tail);
17089 /* Peek at the next token. */
17090 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17091 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17092 /* These are for Objective-C++ */
17093 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17094 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17095 /* The parameter-declaration-list is complete. */
17097 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17101 /* Peek at the next token. */
17102 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17103 /* If it's an ellipsis, then the list is complete. */
17104 if (token->type == CPP_ELLIPSIS)
17106 /* Otherwise, there must be more parameters. Consume the
17108 cp_lexer_consume_token (parser->lexer);
17109 /* When parsing something like:
17111 int i(float f, double d)
17113 we can tell after seeing the declaration for "f" that we
17114 are not looking at an initialization of a variable "i",
17115 but rather at the declaration of a function "i".
17117 Due to the fact that the parsing of template arguments
17118 (as specified to a template-id) requires backtracking we
17119 cannot use this technique when inside a template argument
17121 if (!parser->in_template_argument_list_p
17122 && !parser->in_type_id_in_expr_p
17123 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17124 /* However, a parameter-declaration of the form
17125 "foat(f)" (which is a valid declaration of a
17126 parameter "f") can also be interpreted as an
17127 expression (the conversion of "f" to "float"). */
17128 && !parenthesized_p)
17129 cp_parser_commit_to_tentative_parse (parser);
17133 cp_parser_error (parser, "expected %<,%> or %<...%>");
17134 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17135 cp_parser_skip_to_closing_parenthesis (parser,
17136 /*recovering=*/true,
17137 /*or_comma=*/false,
17138 /*consume_paren=*/false);
17143 parser->in_unbraced_linkage_specification_p
17144 = saved_in_unbraced_linkage_specification_p;
17149 /* Parse a parameter declaration.
17151 parameter-declaration:
17152 decl-specifier-seq ... [opt] declarator
17153 decl-specifier-seq declarator = assignment-expression
17154 decl-specifier-seq ... [opt] abstract-declarator [opt]
17155 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17157 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17158 declares a template parameter. (In that case, a non-nested `>'
17159 token encountered during the parsing of the assignment-expression
17160 is not interpreted as a greater-than operator.)
17162 Returns a representation of the parameter, or NULL if an error
17163 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17164 true iff the declarator is of the form "(p)". */
17166 static cp_parameter_declarator *
17167 cp_parser_parameter_declaration (cp_parser *parser,
17168 bool template_parm_p,
17169 bool *parenthesized_p)
17171 int declares_class_or_enum;
17172 cp_decl_specifier_seq decl_specifiers;
17173 cp_declarator *declarator;
17174 tree default_argument;
17175 cp_token *token = NULL, *declarator_token_start = NULL;
17176 const char *saved_message;
17178 /* In a template parameter, `>' is not an operator.
17182 When parsing a default template-argument for a non-type
17183 template-parameter, the first non-nested `>' is taken as the end
17184 of the template parameter-list rather than a greater-than
17187 /* Type definitions may not appear in parameter types. */
17188 saved_message = parser->type_definition_forbidden_message;
17189 parser->type_definition_forbidden_message
17190 = G_("types may not be defined in parameter types");
17192 /* Parse the declaration-specifiers. */
17193 cp_parser_decl_specifier_seq (parser,
17194 CP_PARSER_FLAGS_NONE,
17196 &declares_class_or_enum);
17198 /* Complain about missing 'typename' or other invalid type names. */
17199 if (!decl_specifiers.any_type_specifiers_p)
17200 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17202 /* If an error occurred, there's no reason to attempt to parse the
17203 rest of the declaration. */
17204 if (cp_parser_error_occurred (parser))
17206 parser->type_definition_forbidden_message = saved_message;
17210 /* Peek at the next token. */
17211 token = cp_lexer_peek_token (parser->lexer);
17213 /* If the next token is a `)', `,', `=', `>', or `...', then there
17214 is no declarator. However, when variadic templates are enabled,
17215 there may be a declarator following `...'. */
17216 if (token->type == CPP_CLOSE_PAREN
17217 || token->type == CPP_COMMA
17218 || token->type == CPP_EQ
17219 || token->type == CPP_GREATER)
17222 if (parenthesized_p)
17223 *parenthesized_p = false;
17225 /* Otherwise, there should be a declarator. */
17228 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17229 parser->default_arg_ok_p = false;
17231 /* After seeing a decl-specifier-seq, if the next token is not a
17232 "(", there is no possibility that the code is a valid
17233 expression. Therefore, if parsing tentatively, we commit at
17235 if (!parser->in_template_argument_list_p
17236 /* In an expression context, having seen:
17240 we cannot be sure whether we are looking at a
17241 function-type (taking a "char" as a parameter) or a cast
17242 of some object of type "char" to "int". */
17243 && !parser->in_type_id_in_expr_p
17244 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17245 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17246 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17247 cp_parser_commit_to_tentative_parse (parser);
17248 /* Parse the declarator. */
17249 declarator_token_start = token;
17250 declarator = cp_parser_declarator (parser,
17251 CP_PARSER_DECLARATOR_EITHER,
17252 /*ctor_dtor_or_conv_p=*/NULL,
17254 /*member_p=*/false);
17255 parser->default_arg_ok_p = saved_default_arg_ok_p;
17256 /* After the declarator, allow more attributes. */
17257 decl_specifiers.attributes
17258 = chainon (decl_specifiers.attributes,
17259 cp_parser_attributes_opt (parser));
17262 /* If the next token is an ellipsis, and we have not seen a
17263 declarator name, and the type of the declarator contains parameter
17264 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17265 a parameter pack expansion expression. Otherwise, leave the
17266 ellipsis for a C-style variadic function. */
17267 token = cp_lexer_peek_token (parser->lexer);
17268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17270 tree type = decl_specifiers.type;
17272 if (type && DECL_P (type))
17273 type = TREE_TYPE (type);
17276 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17277 && declarator_can_be_parameter_pack (declarator)
17278 && (!declarator || !declarator->parameter_pack_p)
17279 && uses_parameter_packs (type))
17281 /* Consume the `...'. */
17282 cp_lexer_consume_token (parser->lexer);
17283 maybe_warn_variadic_templates ();
17285 /* Build a pack expansion type */
17287 declarator->parameter_pack_p = true;
17289 decl_specifiers.type = make_pack_expansion (type);
17293 /* The restriction on defining new types applies only to the type
17294 of the parameter, not to the default argument. */
17295 parser->type_definition_forbidden_message = saved_message;
17297 /* If the next token is `=', then process a default argument. */
17298 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17300 token = cp_lexer_peek_token (parser->lexer);
17301 /* If we are defining a class, then the tokens that make up the
17302 default argument must be saved and processed later. */
17303 if (!template_parm_p && at_class_scope_p ()
17304 && TYPE_BEING_DEFINED (current_class_type)
17305 && !LAMBDA_TYPE_P (current_class_type))
17306 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17307 /* Outside of a class definition, we can just parse the
17308 assignment-expression. */
17311 = cp_parser_default_argument (parser, template_parm_p);
17313 if (!parser->default_arg_ok_p)
17315 if (flag_permissive)
17316 warning (0, "deprecated use of default argument for parameter of non-function");
17319 error_at (token->location,
17320 "default arguments are only "
17321 "permitted for function parameters");
17322 default_argument = NULL_TREE;
17325 else if ((declarator && declarator->parameter_pack_p)
17326 || (decl_specifiers.type
17327 && PACK_EXPANSION_P (decl_specifiers.type)))
17329 /* Find the name of the parameter pack. */
17330 cp_declarator *id_declarator = declarator;
17331 while (id_declarator && id_declarator->kind != cdk_id)
17332 id_declarator = id_declarator->declarator;
17334 if (id_declarator && id_declarator->kind == cdk_id)
17335 error_at (declarator_token_start->location,
17337 ? G_("template parameter pack %qD "
17338 "cannot have a default argument")
17339 : G_("parameter pack %qD cannot have "
17340 "a default argument"),
17341 id_declarator->u.id.unqualified_name);
17343 error_at (declarator_token_start->location,
17345 ? G_("template parameter pack cannot have "
17346 "a default argument")
17347 : G_("parameter pack cannot have a "
17348 "default argument"));
17350 default_argument = NULL_TREE;
17354 default_argument = NULL_TREE;
17356 return make_parameter_declarator (&decl_specifiers,
17361 /* Parse a default argument and return it.
17363 TEMPLATE_PARM_P is true if this is a default argument for a
17364 non-type template parameter. */
17366 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17368 tree default_argument = NULL_TREE;
17369 bool saved_greater_than_is_operator_p;
17370 bool saved_local_variables_forbidden_p;
17371 bool non_constant_p, is_direct_init;
17373 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17375 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17376 parser->greater_than_is_operator_p = !template_parm_p;
17377 /* Local variable names (and the `this' keyword) may not
17378 appear in a default argument. */
17379 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17380 parser->local_variables_forbidden_p = true;
17381 /* Parse the assignment-expression. */
17382 if (template_parm_p)
17383 push_deferring_access_checks (dk_no_deferred);
17385 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17386 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17387 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17388 if (template_parm_p)
17389 pop_deferring_access_checks ();
17390 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17391 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17393 return default_argument;
17396 /* Parse a function-body.
17399 compound_statement */
17402 cp_parser_function_body (cp_parser *parser)
17404 cp_parser_compound_statement (parser, NULL, false, true);
17407 /* Parse a ctor-initializer-opt followed by a function-body. Return
17408 true if a ctor-initializer was present. */
17411 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17414 bool ctor_initializer_p;
17415 const bool check_body_p =
17416 DECL_CONSTRUCTOR_P (current_function_decl)
17417 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17420 /* Begin the function body. */
17421 body = begin_function_body ();
17422 /* Parse the optional ctor-initializer. */
17423 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17425 /* If we're parsing a constexpr constructor definition, we need
17426 to check that the constructor body is indeed empty. However,
17427 before we get to cp_parser_function_body lot of junk has been
17428 generated, so we can't just check that we have an empty block.
17429 Rather we take a snapshot of the outermost block, and check whether
17430 cp_parser_function_body changed its state. */
17433 list = cur_stmt_list;
17434 if (STATEMENT_LIST_TAIL (list))
17435 last = STATEMENT_LIST_TAIL (list)->stmt;
17437 /* Parse the function-body. */
17438 cp_parser_function_body (parser);
17440 check_constexpr_ctor_body (last, list);
17441 /* Finish the function body. */
17442 finish_function_body (body);
17444 return ctor_initializer_p;
17447 /* Parse an initializer.
17450 = initializer-clause
17451 ( expression-list )
17453 Returns an expression representing the initializer. If no
17454 initializer is present, NULL_TREE is returned.
17456 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17457 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17458 set to TRUE if there is no initializer present. If there is an
17459 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17460 is set to true; otherwise it is set to false. */
17463 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17464 bool* non_constant_p)
17469 /* Peek at the next token. */
17470 token = cp_lexer_peek_token (parser->lexer);
17472 /* Let our caller know whether or not this initializer was
17474 *is_direct_init = (token->type != CPP_EQ);
17475 /* Assume that the initializer is constant. */
17476 *non_constant_p = false;
17478 if (token->type == CPP_EQ)
17480 /* Consume the `='. */
17481 cp_lexer_consume_token (parser->lexer);
17482 /* Parse the initializer-clause. */
17483 init = cp_parser_initializer_clause (parser, non_constant_p);
17485 else if (token->type == CPP_OPEN_PAREN)
17488 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17490 /*allow_expansion_p=*/true,
17493 return error_mark_node;
17494 init = build_tree_list_vec (vec);
17495 release_tree_vector (vec);
17497 else if (token->type == CPP_OPEN_BRACE)
17499 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17500 init = cp_parser_braced_list (parser, non_constant_p);
17501 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17505 /* Anything else is an error. */
17506 cp_parser_error (parser, "expected initializer");
17507 init = error_mark_node;
17513 /* Parse an initializer-clause.
17515 initializer-clause:
17516 assignment-expression
17519 Returns an expression representing the initializer.
17521 If the `assignment-expression' production is used the value
17522 returned is simply a representation for the expression.
17524 Otherwise, calls cp_parser_braced_list. */
17527 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17531 /* Assume the expression is constant. */
17532 *non_constant_p = false;
17534 /* If it is not a `{', then we are looking at an
17535 assignment-expression. */
17536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17539 = cp_parser_constant_expression (parser,
17540 /*allow_non_constant_p=*/true,
17544 initializer = cp_parser_braced_list (parser, non_constant_p);
17546 return initializer;
17549 /* Parse a brace-enclosed initializer list.
17552 { initializer-list , [opt] }
17555 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17556 the elements of the initializer-list (or NULL, if the last
17557 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17558 NULL_TREE. There is no way to detect whether or not the optional
17559 trailing `,' was provided. NON_CONSTANT_P is as for
17560 cp_parser_initializer. */
17563 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17567 /* Consume the `{' token. */
17568 cp_lexer_consume_token (parser->lexer);
17569 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17570 initializer = make_node (CONSTRUCTOR);
17571 /* If it's not a `}', then there is a non-trivial initializer. */
17572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17574 /* Parse the initializer list. */
17575 CONSTRUCTOR_ELTS (initializer)
17576 = cp_parser_initializer_list (parser, non_constant_p);
17577 /* A trailing `,' token is allowed. */
17578 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17579 cp_lexer_consume_token (parser->lexer);
17581 /* Now, there should be a trailing `}'. */
17582 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17583 TREE_TYPE (initializer) = init_list_type_node;
17584 return initializer;
17587 /* Parse an initializer-list.
17590 initializer-clause ... [opt]
17591 initializer-list , initializer-clause ... [opt]
17596 designation initializer-clause ...[opt]
17597 initializer-list , designation initializer-clause ...[opt]
17602 [ constant-expression ] =
17604 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17605 for the initializer. If the INDEX of the elt is non-NULL, it is the
17606 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17607 as for cp_parser_initializer. */
17609 static VEC(constructor_elt,gc) *
17610 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17612 VEC(constructor_elt,gc) *v = NULL;
17614 /* Assume all of the expressions are constant. */
17615 *non_constant_p = false;
17617 /* Parse the rest of the list. */
17623 bool clause_non_constant_p;
17625 /* If the next token is an identifier and the following one is a
17626 colon, we are looking at the GNU designated-initializer
17628 if (cp_parser_allow_gnu_extensions_p (parser)
17629 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17630 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17632 /* Warn the user that they are using an extension. */
17633 pedwarn (input_location, OPT_pedantic,
17634 "ISO C++ does not allow designated initializers");
17635 /* Consume the identifier. */
17636 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17637 /* Consume the `:'. */
17638 cp_lexer_consume_token (parser->lexer);
17640 /* Also handle the C99 syntax, '. id ='. */
17641 else if (cp_parser_allow_gnu_extensions_p (parser)
17642 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17643 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17644 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17646 /* Warn the user that they are using an extension. */
17647 pedwarn (input_location, OPT_pedantic,
17648 "ISO C++ does not allow C99 designated initializers");
17649 /* Consume the `.'. */
17650 cp_lexer_consume_token (parser->lexer);
17651 /* Consume the identifier. */
17652 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17653 /* Consume the `='. */
17654 cp_lexer_consume_token (parser->lexer);
17656 /* Also handle C99 array designators, '[ const ] ='. */
17657 else if (cp_parser_allow_gnu_extensions_p (parser)
17658 && !c_dialect_objc ()
17659 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17661 /* In C++11, [ could start a lambda-introducer. */
17662 cp_parser_parse_tentatively (parser);
17663 cp_lexer_consume_token (parser->lexer);
17664 designator = cp_parser_constant_expression (parser, false, NULL);
17665 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17666 cp_parser_require (parser, CPP_EQ, RT_EQ);
17667 if (!cp_parser_parse_definitely (parser))
17668 designator = NULL_TREE;
17671 designator = NULL_TREE;
17673 /* Parse the initializer. */
17674 initializer = cp_parser_initializer_clause (parser,
17675 &clause_non_constant_p);
17676 /* If any clause is non-constant, so is the entire initializer. */
17677 if (clause_non_constant_p)
17678 *non_constant_p = true;
17680 /* If we have an ellipsis, this is an initializer pack
17682 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17684 /* Consume the `...'. */
17685 cp_lexer_consume_token (parser->lexer);
17687 /* Turn the initializer into an initializer expansion. */
17688 initializer = make_pack_expansion (initializer);
17691 /* Add it to the vector. */
17692 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17694 /* If the next token is not a comma, we have reached the end of
17696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17699 /* Peek at the next token. */
17700 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17701 /* If the next token is a `}', then we're still done. An
17702 initializer-clause can have a trailing `,' after the
17703 initializer-list and before the closing `}'. */
17704 if (token->type == CPP_CLOSE_BRACE)
17707 /* Consume the `,' token. */
17708 cp_lexer_consume_token (parser->lexer);
17714 /* Classes [gram.class] */
17716 /* Parse a class-name.
17722 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17723 to indicate that names looked up in dependent types should be
17724 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17725 keyword has been used to indicate that the name that appears next
17726 is a template. TAG_TYPE indicates the explicit tag given before
17727 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17728 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17729 is the class being defined in a class-head.
17731 Returns the TYPE_DECL representing the class. */
17734 cp_parser_class_name (cp_parser *parser,
17735 bool typename_keyword_p,
17736 bool template_keyword_p,
17737 enum tag_types tag_type,
17738 bool check_dependency_p,
17740 bool is_declaration)
17746 tree identifier = NULL_TREE;
17748 /* All class-names start with an identifier. */
17749 token = cp_lexer_peek_token (parser->lexer);
17750 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17752 cp_parser_error (parser, "expected class-name");
17753 return error_mark_node;
17756 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17757 to a template-id, so we save it here. */
17758 scope = parser->scope;
17759 if (scope == error_mark_node)
17760 return error_mark_node;
17762 /* Any name names a type if we're following the `typename' keyword
17763 in a qualified name where the enclosing scope is type-dependent. */
17764 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17765 && dependent_type_p (scope));
17766 /* Handle the common case (an identifier, but not a template-id)
17768 if (token->type == CPP_NAME
17769 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17771 cp_token *identifier_token;
17774 /* Look for the identifier. */
17775 identifier_token = cp_lexer_peek_token (parser->lexer);
17776 ambiguous_p = identifier_token->ambiguous_p;
17777 identifier = cp_parser_identifier (parser);
17778 /* If the next token isn't an identifier, we are certainly not
17779 looking at a class-name. */
17780 if (identifier == error_mark_node)
17781 decl = error_mark_node;
17782 /* If we know this is a type-name, there's no need to look it
17784 else if (typename_p)
17788 tree ambiguous_decls;
17789 /* If we already know that this lookup is ambiguous, then
17790 we've already issued an error message; there's no reason
17794 cp_parser_simulate_error (parser);
17795 return error_mark_node;
17797 /* If the next token is a `::', then the name must be a type
17800 [basic.lookup.qual]
17802 During the lookup for a name preceding the :: scope
17803 resolution operator, object, function, and enumerator
17804 names are ignored. */
17805 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17806 tag_type = typename_type;
17807 /* Look up the name. */
17808 decl = cp_parser_lookup_name (parser, identifier,
17810 /*is_template=*/false,
17811 /*is_namespace=*/false,
17812 check_dependency_p,
17814 identifier_token->location);
17815 if (ambiguous_decls)
17817 if (cp_parser_parsing_tentatively (parser))
17818 cp_parser_simulate_error (parser);
17819 return error_mark_node;
17825 /* Try a template-id. */
17826 decl = cp_parser_template_id (parser, template_keyword_p,
17827 check_dependency_p,
17829 if (decl == error_mark_node)
17830 return error_mark_node;
17833 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17835 /* If this is a typename, create a TYPENAME_TYPE. */
17836 if (typename_p && decl != error_mark_node)
17838 decl = make_typename_type (scope, decl, typename_type,
17839 /*complain=*/tf_error);
17840 if (decl != error_mark_node)
17841 decl = TYPE_NAME (decl);
17844 decl = strip_using_decl (decl);
17846 /* Check to see that it is really the name of a class. */
17847 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17848 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17849 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17850 /* Situations like this:
17852 template <typename T> struct A {
17853 typename T::template X<int>::I i;
17856 are problematic. Is `T::template X<int>' a class-name? The
17857 standard does not seem to be definitive, but there is no other
17858 valid interpretation of the following `::'. Therefore, those
17859 names are considered class-names. */
17861 decl = make_typename_type (scope, decl, tag_type, tf_error);
17862 if (decl != error_mark_node)
17863 decl = TYPE_NAME (decl);
17865 else if (TREE_CODE (decl) != TYPE_DECL
17866 || TREE_TYPE (decl) == error_mark_node
17867 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17868 /* In Objective-C 2.0, a classname followed by '.' starts a
17869 dot-syntax expression, and it's not a type-name. */
17870 || (c_dialect_objc ()
17871 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17872 && objc_is_class_name (decl)))
17873 decl = error_mark_node;
17875 if (decl == error_mark_node)
17876 cp_parser_error (parser, "expected class-name");
17877 else if (identifier && !parser->scope)
17878 maybe_note_name_used_in_class (identifier, decl);
17883 /* Parse a class-specifier.
17886 class-head { member-specification [opt] }
17888 Returns the TREE_TYPE representing the class. */
17891 cp_parser_class_specifier_1 (cp_parser* parser)
17894 tree attributes = NULL_TREE;
17895 bool nested_name_specifier_p;
17896 unsigned saved_num_template_parameter_lists;
17897 bool saved_in_function_body;
17898 unsigned char in_statement;
17899 bool in_switch_statement_p;
17900 bool saved_in_unbraced_linkage_specification_p;
17901 tree old_scope = NULL_TREE;
17902 tree scope = NULL_TREE;
17904 cp_token *closing_brace;
17906 push_deferring_access_checks (dk_no_deferred);
17908 /* Parse the class-head. */
17909 type = cp_parser_class_head (parser,
17910 &nested_name_specifier_p,
17913 /* If the class-head was a semantic disaster, skip the entire body
17917 cp_parser_skip_to_end_of_block_or_statement (parser);
17918 pop_deferring_access_checks ();
17919 return error_mark_node;
17922 /* Look for the `{'. */
17923 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17925 pop_deferring_access_checks ();
17926 return error_mark_node;
17929 /* Process the base classes. If they're invalid, skip the
17930 entire class body. */
17931 if (!xref_basetypes (type, bases))
17933 /* Consuming the closing brace yields better error messages
17935 if (cp_parser_skip_to_closing_brace (parser))
17936 cp_lexer_consume_token (parser->lexer);
17937 pop_deferring_access_checks ();
17938 return error_mark_node;
17941 /* Issue an error message if type-definitions are forbidden here. */
17942 cp_parser_check_type_definition (parser);
17943 /* Remember that we are defining one more class. */
17944 ++parser->num_classes_being_defined;
17945 /* Inside the class, surrounding template-parameter-lists do not
17947 saved_num_template_parameter_lists
17948 = parser->num_template_parameter_lists;
17949 parser->num_template_parameter_lists = 0;
17950 /* We are not in a function body. */
17951 saved_in_function_body = parser->in_function_body;
17952 parser->in_function_body = false;
17953 /* Or in a loop. */
17954 in_statement = parser->in_statement;
17955 parser->in_statement = 0;
17956 /* Or in a switch. */
17957 in_switch_statement_p = parser->in_switch_statement_p;
17958 parser->in_switch_statement_p = false;
17959 /* We are not immediately inside an extern "lang" block. */
17960 saved_in_unbraced_linkage_specification_p
17961 = parser->in_unbraced_linkage_specification_p;
17962 parser->in_unbraced_linkage_specification_p = false;
17964 /* Start the class. */
17965 if (nested_name_specifier_p)
17967 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17968 old_scope = push_inner_scope (scope);
17970 type = begin_class_definition (type, attributes);
17972 if (type == error_mark_node)
17973 /* If the type is erroneous, skip the entire body of the class. */
17974 cp_parser_skip_to_closing_brace (parser);
17976 /* Parse the member-specification. */
17977 cp_parser_member_specification_opt (parser);
17979 /* Look for the trailing `}'. */
17980 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17981 /* Look for trailing attributes to apply to this class. */
17982 if (cp_parser_allow_gnu_extensions_p (parser))
17983 attributes = cp_parser_attributes_opt (parser);
17984 if (type != error_mark_node)
17985 type = finish_struct (type, attributes);
17986 if (nested_name_specifier_p)
17987 pop_inner_scope (old_scope, scope);
17989 /* We've finished a type definition. Check for the common syntax
17990 error of forgetting a semicolon after the definition. We need to
17991 be careful, as we can't just check for not-a-semicolon and be done
17992 with it; the user might have typed:
17994 class X { } c = ...;
17995 class X { } *p = ...;
17997 and so forth. Instead, enumerate all the possible tokens that
17998 might follow this production; if we don't see one of them, then
17999 complain and silently insert the semicolon. */
18001 cp_token *token = cp_lexer_peek_token (parser->lexer);
18002 bool want_semicolon = true;
18004 switch (token->type)
18007 case CPP_SEMICOLON:
18010 case CPP_OPEN_PAREN:
18011 case CPP_CLOSE_PAREN:
18013 want_semicolon = false;
18016 /* While it's legal for type qualifiers and storage class
18017 specifiers to follow type definitions in the grammar, only
18018 compiler testsuites contain code like that. Assume that if
18019 we see such code, then what we're really seeing is a case
18023 const <type> var = ...;
18028 static <type> func (...) ...
18030 i.e. the qualifier or specifier applies to the next
18031 declaration. To do so, however, we need to look ahead one
18032 more token to see if *that* token is a type specifier.
18034 This code could be improved to handle:
18037 static const <type> var = ...; */
18039 if (keyword_is_decl_specifier (token->keyword))
18041 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18043 /* Handling user-defined types here would be nice, but very
18046 = (lookahead->type == CPP_KEYWORD
18047 && keyword_begins_type_specifier (lookahead->keyword));
18054 /* If we don't have a type, then something is very wrong and we
18055 shouldn't try to do anything clever. Likewise for not seeing the
18057 if (closing_brace && TYPE_P (type) && want_semicolon)
18059 cp_token_position prev
18060 = cp_lexer_previous_token_position (parser->lexer);
18061 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18062 location_t loc = prev_token->location;
18064 if (CLASSTYPE_DECLARED_CLASS (type))
18065 error_at (loc, "expected %<;%> after class definition");
18066 else if (TREE_CODE (type) == RECORD_TYPE)
18067 error_at (loc, "expected %<;%> after struct definition");
18068 else if (TREE_CODE (type) == UNION_TYPE)
18069 error_at (loc, "expected %<;%> after union definition");
18071 gcc_unreachable ();
18073 /* Unget one token and smash it to look as though we encountered
18074 a semicolon in the input stream. */
18075 cp_lexer_set_token_position (parser->lexer, prev);
18076 token = cp_lexer_peek_token (parser->lexer);
18077 token->type = CPP_SEMICOLON;
18078 token->keyword = RID_MAX;
18082 /* If this class is not itself within the scope of another class,
18083 then we need to parse the bodies of all of the queued function
18084 definitions. Note that the queued functions defined in a class
18085 are not always processed immediately following the
18086 class-specifier for that class. Consider:
18089 struct B { void f() { sizeof (A); } };
18092 If `f' were processed before the processing of `A' were
18093 completed, there would be no way to compute the size of `A'.
18094 Note that the nesting we are interested in here is lexical --
18095 not the semantic nesting given by TYPE_CONTEXT. In particular,
18098 struct A { struct B; };
18099 struct A::B { void f() { } };
18101 there is no need to delay the parsing of `A::B::f'. */
18102 if (--parser->num_classes_being_defined == 0)
18105 tree class_type = NULL_TREE;
18106 tree pushed_scope = NULL_TREE;
18108 cp_default_arg_entry *e;
18109 tree save_ccp, save_ccr;
18111 /* In a first pass, parse default arguments to the functions.
18112 Then, in a second pass, parse the bodies of the functions.
18113 This two-phased approach handles cases like:
18121 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18125 /* If there are default arguments that have not yet been processed,
18126 take care of them now. */
18127 if (class_type != e->class_type)
18130 pop_scope (pushed_scope);
18131 class_type = e->class_type;
18132 pushed_scope = push_scope (class_type);
18134 /* Make sure that any template parameters are in scope. */
18135 maybe_begin_member_template_processing (decl);
18136 /* Parse the default argument expressions. */
18137 cp_parser_late_parsing_default_args (parser, decl);
18138 /* Remove any template parameters from the symbol table. */
18139 maybe_end_member_template_processing ();
18141 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18142 /* Now parse any NSDMIs. */
18143 save_ccp = current_class_ptr;
18144 save_ccr = current_class_ref;
18145 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18147 if (class_type != DECL_CONTEXT (decl))
18150 pop_scope (pushed_scope);
18151 class_type = DECL_CONTEXT (decl);
18152 pushed_scope = push_scope (class_type);
18154 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18155 cp_parser_late_parsing_nsdmi (parser, decl);
18157 VEC_truncate (tree, unparsed_nsdmis, 0);
18158 current_class_ptr = save_ccp;
18159 current_class_ref = save_ccr;
18161 pop_scope (pushed_scope);
18162 /* Now parse the body of the functions. */
18163 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18164 cp_parser_late_parsing_for_member (parser, decl);
18165 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18168 /* Put back any saved access checks. */
18169 pop_deferring_access_checks ();
18171 /* Restore saved state. */
18172 parser->in_switch_statement_p = in_switch_statement_p;
18173 parser->in_statement = in_statement;
18174 parser->in_function_body = saved_in_function_body;
18175 parser->num_template_parameter_lists
18176 = saved_num_template_parameter_lists;
18177 parser->in_unbraced_linkage_specification_p
18178 = saved_in_unbraced_linkage_specification_p;
18184 cp_parser_class_specifier (cp_parser* parser)
18187 timevar_push (TV_PARSE_STRUCT);
18188 ret = cp_parser_class_specifier_1 (parser);
18189 timevar_pop (TV_PARSE_STRUCT);
18193 /* Parse a class-head.
18196 class-key identifier [opt] base-clause [opt]
18197 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18198 class-key nested-name-specifier [opt] template-id
18201 class-virt-specifier:
18205 class-key attributes identifier [opt] base-clause [opt]
18206 class-key attributes nested-name-specifier identifier base-clause [opt]
18207 class-key attributes nested-name-specifier [opt] template-id
18210 Upon return BASES is initialized to the list of base classes (or
18211 NULL, if there are none) in the same form returned by
18212 cp_parser_base_clause.
18214 Returns the TYPE of the indicated class. Sets
18215 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18216 involving a nested-name-specifier was used, and FALSE otherwise.
18218 Returns error_mark_node if this is not a class-head.
18220 Returns NULL_TREE if the class-head is syntactically valid, but
18221 semantically invalid in a way that means we should skip the entire
18222 body of the class. */
18225 cp_parser_class_head (cp_parser* parser,
18226 bool* nested_name_specifier_p,
18227 tree *attributes_p,
18230 tree nested_name_specifier;
18231 enum tag_types class_key;
18232 tree id = NULL_TREE;
18233 tree type = NULL_TREE;
18235 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18236 bool template_id_p = false;
18237 bool qualified_p = false;
18238 bool invalid_nested_name_p = false;
18239 bool invalid_explicit_specialization_p = false;
18240 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18241 tree pushed_scope = NULL_TREE;
18242 unsigned num_templates;
18243 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18244 /* Assume no nested-name-specifier will be present. */
18245 *nested_name_specifier_p = false;
18246 /* Assume no template parameter lists will be used in defining the
18249 parser->colon_corrects_to_scope_p = false;
18251 *bases = NULL_TREE;
18253 /* Look for the class-key. */
18254 class_key = cp_parser_class_key (parser);
18255 if (class_key == none_type)
18256 return error_mark_node;
18258 /* Parse the attributes. */
18259 attributes = cp_parser_attributes_opt (parser);
18261 /* If the next token is `::', that is invalid -- but sometimes
18262 people do try to write:
18266 Handle this gracefully by accepting the extra qualifier, and then
18267 issuing an error about it later if this really is a
18268 class-head. If it turns out just to be an elaborated type
18269 specifier, remain silent. */
18270 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18271 qualified_p = true;
18273 push_deferring_access_checks (dk_no_check);
18275 /* Determine the name of the class. Begin by looking for an
18276 optional nested-name-specifier. */
18277 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18278 nested_name_specifier
18279 = cp_parser_nested_name_specifier_opt (parser,
18280 /*typename_keyword_p=*/false,
18281 /*check_dependency_p=*/false,
18283 /*is_declaration=*/false);
18284 /* If there was a nested-name-specifier, then there *must* be an
18286 if (nested_name_specifier)
18288 type_start_token = cp_lexer_peek_token (parser->lexer);
18289 /* Although the grammar says `identifier', it really means
18290 `class-name' or `template-name'. You are only allowed to
18291 define a class that has already been declared with this
18294 The proposed resolution for Core Issue 180 says that wherever
18295 you see `class T::X' you should treat `X' as a type-name.
18297 It is OK to define an inaccessible class; for example:
18299 class A { class B; };
18302 We do not know if we will see a class-name, or a
18303 template-name. We look for a class-name first, in case the
18304 class-name is a template-id; if we looked for the
18305 template-name first we would stop after the template-name. */
18306 cp_parser_parse_tentatively (parser);
18307 type = cp_parser_class_name (parser,
18308 /*typename_keyword_p=*/false,
18309 /*template_keyword_p=*/false,
18311 /*check_dependency_p=*/false,
18312 /*class_head_p=*/true,
18313 /*is_declaration=*/false);
18314 /* If that didn't work, ignore the nested-name-specifier. */
18315 if (!cp_parser_parse_definitely (parser))
18317 invalid_nested_name_p = true;
18318 type_start_token = cp_lexer_peek_token (parser->lexer);
18319 id = cp_parser_identifier (parser);
18320 if (id == error_mark_node)
18323 /* If we could not find a corresponding TYPE, treat this
18324 declaration like an unqualified declaration. */
18325 if (type == error_mark_node)
18326 nested_name_specifier = NULL_TREE;
18327 /* Otherwise, count the number of templates used in TYPE and its
18328 containing scopes. */
18333 for (scope = TREE_TYPE (type);
18334 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18335 scope = (TYPE_P (scope)
18336 ? TYPE_CONTEXT (scope)
18337 : DECL_CONTEXT (scope)))
18339 && CLASS_TYPE_P (scope)
18340 && CLASSTYPE_TEMPLATE_INFO (scope)
18341 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18342 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18346 /* Otherwise, the identifier is optional. */
18349 /* We don't know whether what comes next is a template-id,
18350 an identifier, or nothing at all. */
18351 cp_parser_parse_tentatively (parser);
18352 /* Check for a template-id. */
18353 type_start_token = cp_lexer_peek_token (parser->lexer);
18354 id = cp_parser_template_id (parser,
18355 /*template_keyword_p=*/false,
18356 /*check_dependency_p=*/true,
18357 /*is_declaration=*/true);
18358 /* If that didn't work, it could still be an identifier. */
18359 if (!cp_parser_parse_definitely (parser))
18361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18363 type_start_token = cp_lexer_peek_token (parser->lexer);
18364 id = cp_parser_identifier (parser);
18371 template_id_p = true;
18376 pop_deferring_access_checks ();
18380 cp_parser_check_for_invalid_template_id (parser, id,
18381 type_start_token->location);
18383 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18385 /* If it's not a `:' or a `{' then we can't really be looking at a
18386 class-head, since a class-head only appears as part of a
18387 class-specifier. We have to detect this situation before calling
18388 xref_tag, since that has irreversible side-effects. */
18389 if (!cp_parser_next_token_starts_class_definition_p (parser))
18391 cp_parser_error (parser, "expected %<{%> or %<:%>");
18392 type = error_mark_node;
18396 /* At this point, we're going ahead with the class-specifier, even
18397 if some other problem occurs. */
18398 cp_parser_commit_to_tentative_parse (parser);
18399 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18401 cp_parser_error (parser,
18402 "cannot specify %<override%> for a class");
18403 type = error_mark_node;
18406 /* Issue the error about the overly-qualified name now. */
18409 cp_parser_error (parser,
18410 "global qualification of class name is invalid");
18411 type = error_mark_node;
18414 else if (invalid_nested_name_p)
18416 cp_parser_error (parser,
18417 "qualified name does not name a class");
18418 type = error_mark_node;
18421 else if (nested_name_specifier)
18425 /* Reject typedef-names in class heads. */
18426 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18428 error_at (type_start_token->location,
18429 "invalid class name in declaration of %qD",
18435 /* Figure out in what scope the declaration is being placed. */
18436 scope = current_scope ();
18437 /* If that scope does not contain the scope in which the
18438 class was originally declared, the program is invalid. */
18439 if (scope && !is_ancestor (scope, nested_name_specifier))
18441 if (at_namespace_scope_p ())
18442 error_at (type_start_token->location,
18443 "declaration of %qD in namespace %qD which does not "
18445 type, scope, nested_name_specifier);
18447 error_at (type_start_token->location,
18448 "declaration of %qD in %qD which does not enclose %qD",
18449 type, scope, nested_name_specifier);
18455 A declarator-id shall not be qualified except for the
18456 definition of a ... nested class outside of its class
18457 ... [or] the definition or explicit instantiation of a
18458 class member of a namespace outside of its namespace. */
18459 if (scope == nested_name_specifier)
18461 permerror (nested_name_specifier_token_start->location,
18462 "extra qualification not allowed");
18463 nested_name_specifier = NULL_TREE;
18467 /* An explicit-specialization must be preceded by "template <>". If
18468 it is not, try to recover gracefully. */
18469 if (at_namespace_scope_p ()
18470 && parser->num_template_parameter_lists == 0
18473 error_at (type_start_token->location,
18474 "an explicit specialization must be preceded by %<template <>%>");
18475 invalid_explicit_specialization_p = true;
18476 /* Take the same action that would have been taken by
18477 cp_parser_explicit_specialization. */
18478 ++parser->num_template_parameter_lists;
18479 begin_specialization ();
18481 /* There must be no "return" statements between this point and the
18482 end of this function; set "type "to the correct return value and
18483 use "goto done;" to return. */
18484 /* Make sure that the right number of template parameters were
18486 if (!cp_parser_check_template_parameters (parser, num_templates,
18487 type_start_token->location,
18488 /*declarator=*/NULL))
18490 /* If something went wrong, there is no point in even trying to
18491 process the class-definition. */
18496 /* Look up the type. */
18499 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18500 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18501 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18503 error_at (type_start_token->location,
18504 "function template %qD redeclared as a class template", id);
18505 type = error_mark_node;
18509 type = TREE_TYPE (id);
18510 type = maybe_process_partial_specialization (type);
18512 if (nested_name_specifier)
18513 pushed_scope = push_scope (nested_name_specifier);
18515 else if (nested_name_specifier)
18521 template <typename T> struct S { struct T };
18522 template <typename T> struct S<T>::T { };
18524 we will get a TYPENAME_TYPE when processing the definition of
18525 `S::T'. We need to resolve it to the actual type before we
18526 try to define it. */
18527 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18529 class_type = resolve_typename_type (TREE_TYPE (type),
18530 /*only_current_p=*/false);
18531 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18532 type = TYPE_NAME (class_type);
18535 cp_parser_error (parser, "could not resolve typename type");
18536 type = error_mark_node;
18540 if (maybe_process_partial_specialization (TREE_TYPE (type))
18541 == error_mark_node)
18547 class_type = current_class_type;
18548 /* Enter the scope indicated by the nested-name-specifier. */
18549 pushed_scope = push_scope (nested_name_specifier);
18550 /* Get the canonical version of this type. */
18551 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18552 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18553 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18555 type = push_template_decl (type);
18556 if (type == error_mark_node)
18563 type = TREE_TYPE (type);
18564 *nested_name_specifier_p = true;
18566 else /* The name is not a nested name. */
18568 /* If the class was unnamed, create a dummy name. */
18570 id = make_anon_name ();
18571 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18572 parser->num_template_parameter_lists);
18575 /* Indicate whether this class was declared as a `class' or as a
18577 if (TREE_CODE (type) == RECORD_TYPE)
18578 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18579 cp_parser_check_class_key (class_key, type);
18581 /* If this type was already complete, and we see another definition,
18582 that's an error. */
18583 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18585 error_at (type_start_token->location, "redefinition of %q#T",
18587 error_at (type_start_token->location, "previous definition of %q+#T",
18592 else if (type == error_mark_node)
18595 /* We will have entered the scope containing the class; the names of
18596 base classes should be looked up in that context. For example:
18598 struct A { struct B {}; struct C; };
18599 struct A::C : B {};
18603 /* Get the list of base-classes, if there is one. */
18604 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18605 *bases = cp_parser_base_clause (parser);
18608 /* Leave the scope given by the nested-name-specifier. We will
18609 enter the class scope itself while processing the members. */
18611 pop_scope (pushed_scope);
18613 if (invalid_explicit_specialization_p)
18615 end_specialization ();
18616 --parser->num_template_parameter_lists;
18620 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18621 *attributes_p = attributes;
18622 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18623 CLASSTYPE_FINAL (type) = 1;
18625 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18629 /* Parse a class-key.
18636 Returns the kind of class-key specified, or none_type to indicate
18639 static enum tag_types
18640 cp_parser_class_key (cp_parser* parser)
18643 enum tag_types tag_type;
18645 /* Look for the class-key. */
18646 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18650 /* Check to see if the TOKEN is a class-key. */
18651 tag_type = cp_parser_token_is_class_key (token);
18653 cp_parser_error (parser, "expected class-key");
18657 /* Parse an (optional) member-specification.
18659 member-specification:
18660 member-declaration member-specification [opt]
18661 access-specifier : member-specification [opt] */
18664 cp_parser_member_specification_opt (cp_parser* parser)
18671 /* Peek at the next token. */
18672 token = cp_lexer_peek_token (parser->lexer);
18673 /* If it's a `}', or EOF then we've seen all the members. */
18674 if (token->type == CPP_CLOSE_BRACE
18675 || token->type == CPP_EOF
18676 || token->type == CPP_PRAGMA_EOL)
18679 /* See if this token is a keyword. */
18680 keyword = token->keyword;
18684 case RID_PROTECTED:
18686 /* Consume the access-specifier. */
18687 cp_lexer_consume_token (parser->lexer);
18688 /* Remember which access-specifier is active. */
18689 current_access_specifier = token->u.value;
18690 /* Look for the `:'. */
18691 cp_parser_require (parser, CPP_COLON, RT_COLON);
18695 /* Accept #pragmas at class scope. */
18696 if (token->type == CPP_PRAGMA)
18698 cp_parser_pragma (parser, pragma_external);
18702 /* Otherwise, the next construction must be a
18703 member-declaration. */
18704 cp_parser_member_declaration (parser);
18709 /* Parse a member-declaration.
18711 member-declaration:
18712 decl-specifier-seq [opt] member-declarator-list [opt] ;
18713 function-definition ; [opt]
18714 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18716 template-declaration
18719 member-declarator-list:
18721 member-declarator-list , member-declarator
18724 declarator pure-specifier [opt]
18725 declarator constant-initializer [opt]
18726 identifier [opt] : constant-expression
18730 member-declaration:
18731 __extension__ member-declaration
18734 declarator attributes [opt] pure-specifier [opt]
18735 declarator attributes [opt] constant-initializer [opt]
18736 identifier [opt] attributes [opt] : constant-expression
18740 member-declaration:
18741 static_assert-declaration */
18744 cp_parser_member_declaration (cp_parser* parser)
18746 cp_decl_specifier_seq decl_specifiers;
18747 tree prefix_attributes;
18749 int declares_class_or_enum;
18751 cp_token *token = NULL;
18752 cp_token *decl_spec_token_start = NULL;
18753 cp_token *initializer_token_start = NULL;
18754 int saved_pedantic;
18755 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18757 /* Check for the `__extension__' keyword. */
18758 if (cp_parser_extension_opt (parser, &saved_pedantic))
18761 cp_parser_member_declaration (parser);
18762 /* Restore the old value of the PEDANTIC flag. */
18763 pedantic = saved_pedantic;
18768 /* Check for a template-declaration. */
18769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18771 /* An explicit specialization here is an error condition, and we
18772 expect the specialization handler to detect and report this. */
18773 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18774 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18775 cp_parser_explicit_specialization (parser);
18777 cp_parser_template_declaration (parser, /*member_p=*/true);
18782 /* Check for a using-declaration. */
18783 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18785 if (cxx_dialect < cxx0x)
18787 /* Parse the using-declaration. */
18788 cp_parser_using_declaration (parser,
18789 /*access_declaration_p=*/false);
18795 cp_parser_parse_tentatively (parser);
18796 decl = cp_parser_alias_declaration (parser);
18797 if (cp_parser_parse_definitely (parser))
18798 finish_member_declaration (decl);
18800 cp_parser_using_declaration (parser,
18801 /*access_declaration_p=*/false);
18806 /* Check for @defs. */
18807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18810 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18811 ivar = ivar_chains;
18815 ivar = TREE_CHAIN (member);
18816 TREE_CHAIN (member) = NULL_TREE;
18817 finish_member_declaration (member);
18822 /* If the next token is `static_assert' we have a static assertion. */
18823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18825 cp_parser_static_assert (parser, /*member_p=*/true);
18829 parser->colon_corrects_to_scope_p = false;
18831 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18834 /* Parse the decl-specifier-seq. */
18835 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18836 cp_parser_decl_specifier_seq (parser,
18837 CP_PARSER_FLAGS_OPTIONAL,
18839 &declares_class_or_enum);
18840 prefix_attributes = decl_specifiers.attributes;
18841 decl_specifiers.attributes = NULL_TREE;
18842 /* Check for an invalid type-name. */
18843 if (!decl_specifiers.any_type_specifiers_p
18844 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18846 /* If there is no declarator, then the decl-specifier-seq should
18848 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18850 /* If there was no decl-specifier-seq, and the next token is a
18851 `;', then we have something like:
18857 Each member-declaration shall declare at least one member
18858 name of the class. */
18859 if (!decl_specifiers.any_specifiers_p)
18861 cp_token *token = cp_lexer_peek_token (parser->lexer);
18862 if (!in_system_header_at (token->location))
18863 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18869 /* See if this declaration is a friend. */
18870 friend_p = cp_parser_friend_p (&decl_specifiers);
18871 /* If there were decl-specifiers, check to see if there was
18872 a class-declaration. */
18873 type = check_tag_decl (&decl_specifiers);
18874 /* Nested classes have already been added to the class, but
18875 a `friend' needs to be explicitly registered. */
18878 /* If the `friend' keyword was present, the friend must
18879 be introduced with a class-key. */
18880 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18881 pedwarn (decl_spec_token_start->location, OPT_pedantic,
18882 "in C++03 a class-key must be used "
18883 "when declaring a friend");
18886 template <typename T> struct A {
18887 friend struct A<T>::B;
18890 A<T>::B will be represented by a TYPENAME_TYPE, and
18891 therefore not recognized by check_tag_decl. */
18894 type = decl_specifiers.type;
18895 if (type && TREE_CODE (type) == TYPE_DECL)
18896 type = TREE_TYPE (type);
18898 if (!type || !TYPE_P (type))
18899 error_at (decl_spec_token_start->location,
18900 "friend declaration does not name a class or "
18903 make_friend_class (current_class_type, type,
18904 /*complain=*/true);
18906 /* If there is no TYPE, an error message will already have
18908 else if (!type || type == error_mark_node)
18910 /* An anonymous aggregate has to be handled specially; such
18911 a declaration really declares a data member (with a
18912 particular type), as opposed to a nested class. */
18913 else if (ANON_AGGR_TYPE_P (type))
18915 /* Remove constructors and such from TYPE, now that we
18916 know it is an anonymous aggregate. */
18917 fixup_anonymous_aggr (type);
18918 /* And make the corresponding data member. */
18919 decl = build_decl (decl_spec_token_start->location,
18920 FIELD_DECL, NULL_TREE, type);
18921 /* Add it to the class. */
18922 finish_member_declaration (decl);
18925 cp_parser_check_access_in_redeclaration
18927 decl_spec_token_start->location);
18932 bool assume_semicolon = false;
18934 /* See if these declarations will be friends. */
18935 friend_p = cp_parser_friend_p (&decl_specifiers);
18937 /* Keep going until we hit the `;' at the end of the
18939 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18941 tree attributes = NULL_TREE;
18942 tree first_attribute;
18944 /* Peek at the next token. */
18945 token = cp_lexer_peek_token (parser->lexer);
18947 /* Check for a bitfield declaration. */
18948 if (token->type == CPP_COLON
18949 || (token->type == CPP_NAME
18950 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18956 /* Get the name of the bitfield. Note that we cannot just
18957 check TOKEN here because it may have been invalidated by
18958 the call to cp_lexer_peek_nth_token above. */
18959 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18960 identifier = cp_parser_identifier (parser);
18962 identifier = NULL_TREE;
18964 /* Consume the `:' token. */
18965 cp_lexer_consume_token (parser->lexer);
18966 /* Get the width of the bitfield. */
18968 = cp_parser_constant_expression (parser,
18969 /*allow_non_constant=*/false,
18972 /* Look for attributes that apply to the bitfield. */
18973 attributes = cp_parser_attributes_opt (parser);
18974 /* Remember which attributes are prefix attributes and
18976 first_attribute = attributes;
18977 /* Combine the attributes. */
18978 attributes = chainon (prefix_attributes, attributes);
18980 /* Create the bitfield declaration. */
18981 decl = grokbitfield (identifier
18982 ? make_id_declarator (NULL_TREE,
18992 cp_declarator *declarator;
18994 tree asm_specification;
18995 int ctor_dtor_or_conv_p;
18997 /* Parse the declarator. */
18999 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19000 &ctor_dtor_or_conv_p,
19001 /*parenthesized_p=*/NULL,
19002 /*member_p=*/true);
19004 /* If something went wrong parsing the declarator, make sure
19005 that we at least consume some tokens. */
19006 if (declarator == cp_error_declarator)
19008 /* Skip to the end of the statement. */
19009 cp_parser_skip_to_end_of_statement (parser);
19010 /* If the next token is not a semicolon, that is
19011 probably because we just skipped over the body of
19012 a function. So, we consume a semicolon if
19013 present, but do not issue an error message if it
19015 if (cp_lexer_next_token_is (parser->lexer,
19017 cp_lexer_consume_token (parser->lexer);
19021 if (declares_class_or_enum & 2)
19022 cp_parser_check_for_definition_in_return_type
19023 (declarator, decl_specifiers.type,
19024 decl_specifiers.type_location);
19026 /* Look for an asm-specification. */
19027 asm_specification = cp_parser_asm_specification_opt (parser);
19028 /* Look for attributes that apply to the declaration. */
19029 attributes = cp_parser_attributes_opt (parser);
19030 /* Remember which attributes are prefix attributes and
19032 first_attribute = attributes;
19033 /* Combine the attributes. */
19034 attributes = chainon (prefix_attributes, attributes);
19036 /* If it's an `=', then we have a constant-initializer or a
19037 pure-specifier. It is not correct to parse the
19038 initializer before registering the member declaration
19039 since the member declaration should be in scope while
19040 its initializer is processed. However, the rest of the
19041 front end does not yet provide an interface that allows
19042 us to handle this correctly. */
19043 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19047 A pure-specifier shall be used only in the declaration of
19048 a virtual function.
19050 A member-declarator can contain a constant-initializer
19051 only if it declares a static member of integral or
19054 Therefore, if the DECLARATOR is for a function, we look
19055 for a pure-specifier; otherwise, we look for a
19056 constant-initializer. When we call `grokfield', it will
19057 perform more stringent semantics checks. */
19058 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19059 if (function_declarator_p (declarator)
19060 || (decl_specifiers.type
19061 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19062 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19063 == FUNCTION_TYPE)))
19064 initializer = cp_parser_pure_specifier (parser);
19065 else if (decl_specifiers.storage_class != sc_static)
19066 initializer = cp_parser_save_nsdmi (parser);
19067 else if (cxx_dialect >= cxx0x)
19070 /* Don't require a constant rvalue in C++11, since we
19071 might want a reference constant. We'll enforce
19072 constancy later. */
19073 cp_lexer_consume_token (parser->lexer);
19074 /* Parse the initializer. */
19075 initializer = cp_parser_initializer_clause (parser,
19079 /* Parse the initializer. */
19080 initializer = cp_parser_constant_initializer (parser);
19082 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19083 && !function_declarator_p (declarator))
19086 if (decl_specifiers.storage_class != sc_static)
19087 initializer = cp_parser_save_nsdmi (parser);
19089 initializer = cp_parser_initializer (parser, &x, &x);
19091 /* Otherwise, there is no initializer. */
19093 initializer = NULL_TREE;
19095 /* See if we are probably looking at a function
19096 definition. We are certainly not looking at a
19097 member-declarator. Calling `grokfield' has
19098 side-effects, so we must not do it unless we are sure
19099 that we are looking at a member-declarator. */
19100 if (cp_parser_token_starts_function_definition_p
19101 (cp_lexer_peek_token (parser->lexer)))
19103 /* The grammar does not allow a pure-specifier to be
19104 used when a member function is defined. (It is
19105 possible that this fact is an oversight in the
19106 standard, since a pure function may be defined
19107 outside of the class-specifier. */
19109 error_at (initializer_token_start->location,
19110 "pure-specifier on function-definition");
19111 decl = cp_parser_save_member_function_body (parser,
19115 /* If the member was not a friend, declare it here. */
19117 finish_member_declaration (decl);
19118 /* Peek at the next token. */
19119 token = cp_lexer_peek_token (parser->lexer);
19120 /* If the next token is a semicolon, consume it. */
19121 if (token->type == CPP_SEMICOLON)
19122 cp_lexer_consume_token (parser->lexer);
19126 if (declarator->kind == cdk_function)
19127 declarator->id_loc = token->location;
19128 /* Create the declaration. */
19129 decl = grokfield (declarator, &decl_specifiers,
19130 initializer, /*init_const_expr_p=*/true,
19135 /* Reset PREFIX_ATTRIBUTES. */
19136 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19137 attributes = TREE_CHAIN (attributes);
19139 TREE_CHAIN (attributes) = NULL_TREE;
19141 /* If there is any qualification still in effect, clear it
19142 now; we will be starting fresh with the next declarator. */
19143 parser->scope = NULL_TREE;
19144 parser->qualifying_scope = NULL_TREE;
19145 parser->object_scope = NULL_TREE;
19146 /* If it's a `,', then there are more declarators. */
19147 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19148 cp_lexer_consume_token (parser->lexer);
19149 /* If the next token isn't a `;', then we have a parse error. */
19150 else if (cp_lexer_next_token_is_not (parser->lexer,
19153 /* The next token might be a ways away from where the
19154 actual semicolon is missing. Find the previous token
19155 and use that for our error position. */
19156 cp_token *token = cp_lexer_previous_token (parser->lexer);
19157 error_at (token->location,
19158 "expected %<;%> at end of member declaration");
19160 /* Assume that the user meant to provide a semicolon. If
19161 we were to cp_parser_skip_to_end_of_statement, we might
19162 skip to a semicolon inside a member function definition
19163 and issue nonsensical error messages. */
19164 assume_semicolon = true;
19169 /* Add DECL to the list of members. */
19171 finish_member_declaration (decl);
19173 if (TREE_CODE (decl) == FUNCTION_DECL)
19174 cp_parser_save_default_args (parser, decl);
19175 else if (TREE_CODE (decl) == FIELD_DECL
19176 && !DECL_C_BIT_FIELD (decl)
19177 && DECL_INITIAL (decl))
19178 /* Add DECL to the queue of NSDMI to be parsed later. */
19179 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19182 if (assume_semicolon)
19187 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19189 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19192 /* Parse a pure-specifier.
19197 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19198 Otherwise, ERROR_MARK_NODE is returned. */
19201 cp_parser_pure_specifier (cp_parser* parser)
19205 /* Look for the `=' token. */
19206 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19207 return error_mark_node;
19208 /* Look for the `0' token. */
19209 token = cp_lexer_peek_token (parser->lexer);
19211 if (token->type == CPP_EOF
19212 || token->type == CPP_PRAGMA_EOL)
19213 return error_mark_node;
19215 cp_lexer_consume_token (parser->lexer);
19217 /* Accept = default or = delete in c++0x mode. */
19218 if (token->keyword == RID_DEFAULT
19219 || token->keyword == RID_DELETE)
19221 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19222 return token->u.value;
19225 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19226 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19228 cp_parser_error (parser,
19229 "invalid pure specifier (only %<= 0%> is allowed)");
19230 cp_parser_skip_to_end_of_statement (parser);
19231 return error_mark_node;
19233 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19235 error_at (token->location, "templates may not be %<virtual%>");
19236 return error_mark_node;
19239 return integer_zero_node;
19242 /* Parse a constant-initializer.
19244 constant-initializer:
19245 = constant-expression
19247 Returns a representation of the constant-expression. */
19250 cp_parser_constant_initializer (cp_parser* parser)
19252 /* Look for the `=' token. */
19253 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19254 return error_mark_node;
19256 /* It is invalid to write:
19258 struct S { static const int i = { 7 }; };
19261 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19263 cp_parser_error (parser,
19264 "a brace-enclosed initializer is not allowed here");
19265 /* Consume the opening brace. */
19266 cp_lexer_consume_token (parser->lexer);
19267 /* Skip the initializer. */
19268 cp_parser_skip_to_closing_brace (parser);
19269 /* Look for the trailing `}'. */
19270 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19272 return error_mark_node;
19275 return cp_parser_constant_expression (parser,
19276 /*allow_non_constant=*/false,
19280 /* Derived classes [gram.class.derived] */
19282 /* Parse a base-clause.
19285 : base-specifier-list
19287 base-specifier-list:
19288 base-specifier ... [opt]
19289 base-specifier-list , base-specifier ... [opt]
19291 Returns a TREE_LIST representing the base-classes, in the order in
19292 which they were declared. The representation of each node is as
19293 described by cp_parser_base_specifier.
19295 In the case that no bases are specified, this function will return
19296 NULL_TREE, not ERROR_MARK_NODE. */
19299 cp_parser_base_clause (cp_parser* parser)
19301 tree bases = NULL_TREE;
19303 /* Look for the `:' that begins the list. */
19304 cp_parser_require (parser, CPP_COLON, RT_COLON);
19306 /* Scan the base-specifier-list. */
19311 bool pack_expansion_p = false;
19313 /* Look for the base-specifier. */
19314 base = cp_parser_base_specifier (parser);
19315 /* Look for the (optional) ellipsis. */
19316 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19318 /* Consume the `...'. */
19319 cp_lexer_consume_token (parser->lexer);
19321 pack_expansion_p = true;
19324 /* Add BASE to the front of the list. */
19325 if (base && base != error_mark_node)
19327 if (pack_expansion_p)
19328 /* Make this a pack expansion type. */
19329 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19331 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19333 TREE_CHAIN (base) = bases;
19337 /* Peek at the next token. */
19338 token = cp_lexer_peek_token (parser->lexer);
19339 /* If it's not a comma, then the list is complete. */
19340 if (token->type != CPP_COMMA)
19342 /* Consume the `,'. */
19343 cp_lexer_consume_token (parser->lexer);
19346 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19347 base class had a qualified name. However, the next name that
19348 appears is certainly not qualified. */
19349 parser->scope = NULL_TREE;
19350 parser->qualifying_scope = NULL_TREE;
19351 parser->object_scope = NULL_TREE;
19353 return nreverse (bases);
19356 /* Parse a base-specifier.
19359 :: [opt] nested-name-specifier [opt] class-name
19360 virtual access-specifier [opt] :: [opt] nested-name-specifier
19362 access-specifier virtual [opt] :: [opt] nested-name-specifier
19365 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19366 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19367 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19368 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19371 cp_parser_base_specifier (cp_parser* parser)
19375 bool virtual_p = false;
19376 bool duplicate_virtual_error_issued_p = false;
19377 bool duplicate_access_error_issued_p = false;
19378 bool class_scope_p, template_p;
19379 tree access = access_default_node;
19382 /* Process the optional `virtual' and `access-specifier'. */
19385 /* Peek at the next token. */
19386 token = cp_lexer_peek_token (parser->lexer);
19387 /* Process `virtual'. */
19388 switch (token->keyword)
19391 /* If `virtual' appears more than once, issue an error. */
19392 if (virtual_p && !duplicate_virtual_error_issued_p)
19394 cp_parser_error (parser,
19395 "%<virtual%> specified more than once in base-specified");
19396 duplicate_virtual_error_issued_p = true;
19401 /* Consume the `virtual' token. */
19402 cp_lexer_consume_token (parser->lexer);
19407 case RID_PROTECTED:
19409 /* If more than one access specifier appears, issue an
19411 if (access != access_default_node
19412 && !duplicate_access_error_issued_p)
19414 cp_parser_error (parser,
19415 "more than one access specifier in base-specified");
19416 duplicate_access_error_issued_p = true;
19419 access = ridpointers[(int) token->keyword];
19421 /* Consume the access-specifier. */
19422 cp_lexer_consume_token (parser->lexer);
19431 /* It is not uncommon to see programs mechanically, erroneously, use
19432 the 'typename' keyword to denote (dependent) qualified types
19433 as base classes. */
19434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19436 token = cp_lexer_peek_token (parser->lexer);
19437 if (!processing_template_decl)
19438 error_at (token->location,
19439 "keyword %<typename%> not allowed outside of templates");
19441 error_at (token->location,
19442 "keyword %<typename%> not allowed in this context "
19443 "(the base class is implicitly a type)");
19444 cp_lexer_consume_token (parser->lexer);
19447 /* Look for the optional `::' operator. */
19448 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19449 /* Look for the nested-name-specifier. The simplest way to
19454 The keyword `typename' is not permitted in a base-specifier or
19455 mem-initializer; in these contexts a qualified name that
19456 depends on a template-parameter is implicitly assumed to be a
19459 is to pretend that we have seen the `typename' keyword at this
19461 cp_parser_nested_name_specifier_opt (parser,
19462 /*typename_keyword_p=*/true,
19463 /*check_dependency_p=*/true,
19465 /*is_declaration=*/true);
19466 /* If the base class is given by a qualified name, assume that names
19467 we see are type names or templates, as appropriate. */
19468 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19469 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19472 && cp_lexer_next_token_is_decltype (parser->lexer))
19473 /* DR 950 allows decltype as a base-specifier. */
19474 type = cp_parser_decltype (parser);
19477 /* Otherwise, look for the class-name. */
19478 type = cp_parser_class_name (parser,
19482 /*check_dependency_p=*/true,
19483 /*class_head_p=*/false,
19484 /*is_declaration=*/true);
19485 type = TREE_TYPE (type);
19488 if (type == error_mark_node)
19489 return error_mark_node;
19491 return finish_base_specifier (type, access, virtual_p);
19494 /* Exception handling [gram.exception] */
19496 /* Parse an (optional) noexcept-specification.
19498 noexcept-specification:
19499 noexcept ( constant-expression ) [opt]
19501 If no noexcept-specification is present, returns NULL_TREE.
19502 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19503 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19504 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19505 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19506 in which case a boolean condition is returned instead. */
19509 cp_parser_noexcept_specification_opt (cp_parser* parser,
19510 bool require_constexpr,
19511 bool* consumed_expr,
19515 const char *saved_message;
19517 /* Peek at the next token. */
19518 token = cp_lexer_peek_token (parser->lexer);
19520 /* Is it a noexcept-specification? */
19521 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19524 cp_lexer_consume_token (parser->lexer);
19526 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19528 cp_lexer_consume_token (parser->lexer);
19530 if (require_constexpr)
19532 /* Types may not be defined in an exception-specification. */
19533 saved_message = parser->type_definition_forbidden_message;
19534 parser->type_definition_forbidden_message
19535 = G_("types may not be defined in an exception-specification");
19537 expr = cp_parser_constant_expression (parser, false, NULL);
19539 /* Restore the saved message. */
19540 parser->type_definition_forbidden_message = saved_message;
19544 expr = cp_parser_expression (parser, false, NULL);
19545 *consumed_expr = true;
19548 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19552 expr = boolean_true_node;
19553 if (!require_constexpr)
19554 *consumed_expr = false;
19557 /* We cannot build a noexcept-spec right away because this will check
19558 that expr is a constexpr. */
19560 return build_noexcept_spec (expr, tf_warning_or_error);
19568 /* Parse an (optional) exception-specification.
19570 exception-specification:
19571 throw ( type-id-list [opt] )
19573 Returns a TREE_LIST representing the exception-specification. The
19574 TREE_VALUE of each node is a type. */
19577 cp_parser_exception_specification_opt (cp_parser* parser)
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 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19589 if (type_id_list != NULL_TREE)
19590 return type_id_list;
19592 /* If it's not `throw', then there's no exception-specification. */
19593 if (!cp_parser_is_keyword (token, RID_THROW))
19597 /* Enable this once a lot of code has transitioned to noexcept? */
19598 if (cxx_dialect == cxx0x && !in_system_header)
19599 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19600 "deprecated in C++0x; use %<noexcept%> instead");
19603 /* Consume the `throw'. */
19604 cp_lexer_consume_token (parser->lexer);
19606 /* Look for the `('. */
19607 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19609 /* Peek at the next token. */
19610 token = cp_lexer_peek_token (parser->lexer);
19611 /* If it's not a `)', then there is a type-id-list. */
19612 if (token->type != CPP_CLOSE_PAREN)
19614 /* Types may not be defined in an exception-specification. */
19615 saved_message = parser->type_definition_forbidden_message;
19616 parser->type_definition_forbidden_message
19617 = G_("types may not be defined in an exception-specification");
19618 /* Parse the type-id-list. */
19619 type_id_list = cp_parser_type_id_list (parser);
19620 /* Restore the saved message. */
19621 parser->type_definition_forbidden_message = saved_message;
19624 type_id_list = empty_except_spec;
19626 /* Look for the `)'. */
19627 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19629 return type_id_list;
19632 /* Parse an (optional) type-id-list.
19636 type-id-list , type-id ... [opt]
19638 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19639 in the order that the types were presented. */
19642 cp_parser_type_id_list (cp_parser* parser)
19644 tree types = NULL_TREE;
19651 /* Get the next type-id. */
19652 type = cp_parser_type_id (parser);
19653 /* Parse the optional ellipsis. */
19654 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19656 /* Consume the `...'. */
19657 cp_lexer_consume_token (parser->lexer);
19659 /* Turn the type into a pack expansion expression. */
19660 type = make_pack_expansion (type);
19662 /* Add it to the list. */
19663 types = add_exception_specifier (types, type, /*complain=*/1);
19664 /* Peek at the next token. */
19665 token = cp_lexer_peek_token (parser->lexer);
19666 /* If it is not a `,', we are done. */
19667 if (token->type != CPP_COMMA)
19669 /* Consume the `,'. */
19670 cp_lexer_consume_token (parser->lexer);
19673 return nreverse (types);
19676 /* Parse a try-block.
19679 try compound-statement handler-seq */
19682 cp_parser_try_block (cp_parser* parser)
19686 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19687 try_block = begin_try_block ();
19688 cp_parser_compound_statement (parser, NULL, true, false);
19689 finish_try_block (try_block);
19690 cp_parser_handler_seq (parser);
19691 finish_handler_sequence (try_block);
19696 /* Parse a function-try-block.
19698 function-try-block:
19699 try ctor-initializer [opt] function-body handler-seq */
19702 cp_parser_function_try_block (cp_parser* parser)
19704 tree compound_stmt;
19706 bool ctor_initializer_p;
19708 /* Look for the `try' keyword. */
19709 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19711 /* Let the rest of the front end know where we are. */
19712 try_block = begin_function_try_block (&compound_stmt);
19713 /* Parse the function-body. */
19715 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19716 /* We're done with the `try' part. */
19717 finish_function_try_block (try_block);
19718 /* Parse the handlers. */
19719 cp_parser_handler_seq (parser);
19720 /* We're done with the handlers. */
19721 finish_function_handler_sequence (try_block, compound_stmt);
19723 return ctor_initializer_p;
19726 /* Parse a handler-seq.
19729 handler handler-seq [opt] */
19732 cp_parser_handler_seq (cp_parser* parser)
19738 /* Parse the handler. */
19739 cp_parser_handler (parser);
19740 /* Peek at the next token. */
19741 token = cp_lexer_peek_token (parser->lexer);
19742 /* If it's not `catch' then there are no more handlers. */
19743 if (!cp_parser_is_keyword (token, RID_CATCH))
19748 /* Parse a handler.
19751 catch ( exception-declaration ) compound-statement */
19754 cp_parser_handler (cp_parser* parser)
19759 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19760 handler = begin_handler ();
19761 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19762 declaration = cp_parser_exception_declaration (parser);
19763 finish_handler_parms (declaration, handler);
19764 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19765 cp_parser_compound_statement (parser, NULL, false, false);
19766 finish_handler (handler);
19769 /* Parse an exception-declaration.
19771 exception-declaration:
19772 type-specifier-seq declarator
19773 type-specifier-seq abstract-declarator
19777 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19778 ellipsis variant is used. */
19781 cp_parser_exception_declaration (cp_parser* parser)
19783 cp_decl_specifier_seq type_specifiers;
19784 cp_declarator *declarator;
19785 const char *saved_message;
19787 /* If it's an ellipsis, it's easy to handle. */
19788 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19790 /* Consume the `...' token. */
19791 cp_lexer_consume_token (parser->lexer);
19795 /* Types may not be defined in exception-declarations. */
19796 saved_message = parser->type_definition_forbidden_message;
19797 parser->type_definition_forbidden_message
19798 = G_("types may not be defined in exception-declarations");
19800 /* Parse the type-specifier-seq. */
19801 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19802 /*is_trailing_return=*/false,
19804 /* If it's a `)', then there is no declarator. */
19805 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19808 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19809 /*ctor_dtor_or_conv_p=*/NULL,
19810 /*parenthesized_p=*/NULL,
19811 /*member_p=*/false);
19813 /* Restore the saved message. */
19814 parser->type_definition_forbidden_message = saved_message;
19816 if (!type_specifiers.any_specifiers_p)
19817 return error_mark_node;
19819 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19822 /* Parse a throw-expression.
19825 throw assignment-expression [opt]
19827 Returns a THROW_EXPR representing the throw-expression. */
19830 cp_parser_throw_expression (cp_parser* parser)
19835 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19836 token = cp_lexer_peek_token (parser->lexer);
19837 /* Figure out whether or not there is an assignment-expression
19838 following the "throw" keyword. */
19839 if (token->type == CPP_COMMA
19840 || token->type == CPP_SEMICOLON
19841 || token->type == CPP_CLOSE_PAREN
19842 || token->type == CPP_CLOSE_SQUARE
19843 || token->type == CPP_CLOSE_BRACE
19844 || token->type == CPP_COLON)
19845 expression = NULL_TREE;
19847 expression = cp_parser_assignment_expression (parser,
19848 /*cast_p=*/false, NULL);
19850 return build_throw (expression);
19853 /* GNU Extensions */
19855 /* Parse an (optional) asm-specification.
19858 asm ( string-literal )
19860 If the asm-specification is present, returns a STRING_CST
19861 corresponding to the string-literal. Otherwise, returns
19865 cp_parser_asm_specification_opt (cp_parser* parser)
19868 tree asm_specification;
19870 /* Peek at the next token. */
19871 token = cp_lexer_peek_token (parser->lexer);
19872 /* If the next token isn't the `asm' keyword, then there's no
19873 asm-specification. */
19874 if (!cp_parser_is_keyword (token, RID_ASM))
19877 /* Consume the `asm' token. */
19878 cp_lexer_consume_token (parser->lexer);
19879 /* Look for the `('. */
19880 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19882 /* Look for the string-literal. */
19883 asm_specification = cp_parser_string_literal (parser, false, false);
19885 /* Look for the `)'. */
19886 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19888 return asm_specification;
19891 /* Parse an asm-operand-list.
19895 asm-operand-list , asm-operand
19898 string-literal ( expression )
19899 [ string-literal ] string-literal ( expression )
19901 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19902 each node is the expression. The TREE_PURPOSE is itself a
19903 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19904 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19905 is a STRING_CST for the string literal before the parenthesis. Returns
19906 ERROR_MARK_NODE if any of the operands are invalid. */
19909 cp_parser_asm_operand_list (cp_parser* parser)
19911 tree asm_operands = NULL_TREE;
19912 bool invalid_operands = false;
19916 tree string_literal;
19920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19922 /* Consume the `[' token. */
19923 cp_lexer_consume_token (parser->lexer);
19924 /* Read the operand name. */
19925 name = cp_parser_identifier (parser);
19926 if (name != error_mark_node)
19927 name = build_string (IDENTIFIER_LENGTH (name),
19928 IDENTIFIER_POINTER (name));
19929 /* Look for the closing `]'. */
19930 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19934 /* Look for the string-literal. */
19935 string_literal = cp_parser_string_literal (parser, false, false);
19937 /* Look for the `('. */
19938 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19939 /* Parse the expression. */
19940 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19941 /* Look for the `)'. */
19942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19944 if (name == error_mark_node
19945 || string_literal == error_mark_node
19946 || expression == error_mark_node)
19947 invalid_operands = true;
19949 /* Add this operand to the list. */
19950 asm_operands = tree_cons (build_tree_list (name, string_literal),
19953 /* If the next token is not a `,', there are no more
19955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19957 /* Consume the `,'. */
19958 cp_lexer_consume_token (parser->lexer);
19961 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19964 /* Parse an asm-clobber-list.
19968 asm-clobber-list , string-literal
19970 Returns a TREE_LIST, indicating the clobbers in the order that they
19971 appeared. The TREE_VALUE of each node is a STRING_CST. */
19974 cp_parser_asm_clobber_list (cp_parser* parser)
19976 tree clobbers = NULL_TREE;
19980 tree string_literal;
19982 /* Look for the string literal. */
19983 string_literal = cp_parser_string_literal (parser, false, false);
19984 /* Add it to the list. */
19985 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19986 /* If the next token is not a `,', then the list is
19988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19990 /* Consume the `,' token. */
19991 cp_lexer_consume_token (parser->lexer);
19997 /* Parse an asm-label-list.
20001 asm-label-list , identifier
20003 Returns a TREE_LIST, indicating the labels in the order that they
20004 appeared. The TREE_VALUE of each node is a label. */
20007 cp_parser_asm_label_list (cp_parser* parser)
20009 tree labels = NULL_TREE;
20013 tree identifier, label, name;
20015 /* Look for the identifier. */
20016 identifier = cp_parser_identifier (parser);
20017 if (!error_operand_p (identifier))
20019 label = lookup_label (identifier);
20020 if (TREE_CODE (label) == LABEL_DECL)
20022 TREE_USED (label) = 1;
20023 check_goto (label);
20024 name = build_string (IDENTIFIER_LENGTH (identifier),
20025 IDENTIFIER_POINTER (identifier));
20026 labels = tree_cons (name, label, labels);
20029 /* If the next token is not a `,', then the list is
20031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20033 /* Consume the `,' token. */
20034 cp_lexer_consume_token (parser->lexer);
20037 return nreverse (labels);
20040 /* Parse an (optional) series of attributes.
20043 attributes attribute
20046 __attribute__ (( attribute-list [opt] ))
20048 The return value is as for cp_parser_attribute_list. */
20051 cp_parser_attributes_opt (cp_parser* parser)
20053 tree attributes = NULL_TREE;
20058 tree attribute_list;
20060 /* Peek at the next token. */
20061 token = cp_lexer_peek_token (parser->lexer);
20062 /* If it's not `__attribute__', then we're done. */
20063 if (token->keyword != RID_ATTRIBUTE)
20066 /* Consume the `__attribute__' keyword. */
20067 cp_lexer_consume_token (parser->lexer);
20068 /* Look for the two `(' tokens. */
20069 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20070 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20072 /* Peek at the next token. */
20073 token = cp_lexer_peek_token (parser->lexer);
20074 if (token->type != CPP_CLOSE_PAREN)
20075 /* Parse the attribute-list. */
20076 attribute_list = cp_parser_attribute_list (parser);
20078 /* If the next token is a `)', then there is no attribute
20080 attribute_list = NULL;
20082 /* Look for the two `)' tokens. */
20083 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20084 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20086 /* Add these new attributes to the list. */
20087 attributes = chainon (attributes, attribute_list);
20093 /* Parse an attribute-list.
20097 attribute-list , attribute
20101 identifier ( identifier )
20102 identifier ( identifier , expression-list )
20103 identifier ( expression-list )
20105 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20106 to an attribute. The TREE_PURPOSE of each node is the identifier
20107 indicating which attribute is in use. The TREE_VALUE represents
20108 the arguments, if any. */
20111 cp_parser_attribute_list (cp_parser* parser)
20113 tree attribute_list = NULL_TREE;
20114 bool save_translate_strings_p = parser->translate_strings_p;
20116 parser->translate_strings_p = false;
20123 /* Look for the identifier. We also allow keywords here; for
20124 example `__attribute__ ((const))' is legal. */
20125 token = cp_lexer_peek_token (parser->lexer);
20126 if (token->type == CPP_NAME
20127 || token->type == CPP_KEYWORD)
20129 tree arguments = NULL_TREE;
20131 /* Consume the token. */
20132 token = cp_lexer_consume_token (parser->lexer);
20134 /* Save away the identifier that indicates which attribute
20136 identifier = (token->type == CPP_KEYWORD)
20137 /* For keywords, use the canonical spelling, not the
20138 parsed identifier. */
20139 ? ridpointers[(int) token->keyword]
20142 attribute = build_tree_list (identifier, NULL_TREE);
20144 /* Peek at the next token. */
20145 token = cp_lexer_peek_token (parser->lexer);
20146 /* If it's an `(', then parse the attribute arguments. */
20147 if (token->type == CPP_OPEN_PAREN)
20150 int attr_flag = (attribute_takes_identifier_p (identifier)
20151 ? id_attr : normal_attr);
20152 vec = cp_parser_parenthesized_expression_list
20153 (parser, attr_flag, /*cast_p=*/false,
20154 /*allow_expansion_p=*/false,
20155 /*non_constant_p=*/NULL);
20157 arguments = error_mark_node;
20160 arguments = build_tree_list_vec (vec);
20161 release_tree_vector (vec);
20163 /* Save the arguments away. */
20164 TREE_VALUE (attribute) = arguments;
20167 if (arguments != error_mark_node)
20169 /* Add this attribute to the list. */
20170 TREE_CHAIN (attribute) = attribute_list;
20171 attribute_list = attribute;
20174 token = cp_lexer_peek_token (parser->lexer);
20176 /* Now, look for more attributes. If the next token isn't a
20177 `,', we're done. */
20178 if (token->type != CPP_COMMA)
20181 /* Consume the comma and keep going. */
20182 cp_lexer_consume_token (parser->lexer);
20184 parser->translate_strings_p = save_translate_strings_p;
20186 /* We built up the list in reverse order. */
20187 return nreverse (attribute_list);
20190 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20191 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20192 current value of the PEDANTIC flag, regardless of whether or not
20193 the `__extension__' keyword is present. The caller is responsible
20194 for restoring the value of the PEDANTIC flag. */
20197 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20199 /* Save the old value of the PEDANTIC flag. */
20200 *saved_pedantic = pedantic;
20202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20204 /* Consume the `__extension__' token. */
20205 cp_lexer_consume_token (parser->lexer);
20206 /* We're not being pedantic while the `__extension__' keyword is
20216 /* Parse a label declaration.
20219 __label__ label-declarator-seq ;
20221 label-declarator-seq:
20222 identifier , label-declarator-seq
20226 cp_parser_label_declaration (cp_parser* parser)
20228 /* Look for the `__label__' keyword. */
20229 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20235 /* Look for an identifier. */
20236 identifier = cp_parser_identifier (parser);
20237 /* If we failed, stop. */
20238 if (identifier == error_mark_node)
20240 /* Declare it as a label. */
20241 finish_label_decl (identifier);
20242 /* If the next token is a `;', stop. */
20243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20245 /* Look for the `,' separating the label declarations. */
20246 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20249 /* Look for the final `;'. */
20250 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20253 /* Support Functions */
20255 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20256 NAME should have one of the representations used for an
20257 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20258 is returned. If PARSER->SCOPE is a dependent type, then a
20259 SCOPE_REF is returned.
20261 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20262 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20263 was formed. Abstractly, such entities should not be passed to this
20264 function, because they do not need to be looked up, but it is
20265 simpler to check for this special case here, rather than at the
20268 In cases not explicitly covered above, this function returns a
20269 DECL, OVERLOAD, or baselink representing the result of the lookup.
20270 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20273 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20274 (e.g., "struct") that was used. In that case bindings that do not
20275 refer to types are ignored.
20277 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20280 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20283 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20286 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20287 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20288 NULL_TREE otherwise. */
20291 cp_parser_lookup_name (cp_parser *parser, tree name,
20292 enum tag_types tag_type,
20295 bool check_dependency,
20296 tree *ambiguous_decls,
20297 location_t name_location)
20301 tree object_type = parser->context->object_type;
20303 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20304 flags |= LOOKUP_COMPLAIN;
20306 /* Assume that the lookup will be unambiguous. */
20307 if (ambiguous_decls)
20308 *ambiguous_decls = NULL_TREE;
20310 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20311 no longer valid. Note that if we are parsing tentatively, and
20312 the parse fails, OBJECT_TYPE will be automatically restored. */
20313 parser->context->object_type = NULL_TREE;
20315 if (name == error_mark_node)
20316 return error_mark_node;
20318 /* A template-id has already been resolved; there is no lookup to
20320 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20322 if (BASELINK_P (name))
20324 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20325 == TEMPLATE_ID_EXPR);
20329 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20330 it should already have been checked to make sure that the name
20331 used matches the type being destroyed. */
20332 if (TREE_CODE (name) == BIT_NOT_EXPR)
20336 /* Figure out to which type this destructor applies. */
20338 type = parser->scope;
20339 else if (object_type)
20340 type = object_type;
20342 type = current_class_type;
20343 /* If that's not a class type, there is no destructor. */
20344 if (!type || !CLASS_TYPE_P (type))
20345 return error_mark_node;
20346 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20347 lazily_declare_fn (sfk_destructor, type);
20348 if (!CLASSTYPE_DESTRUCTORS (type))
20349 return error_mark_node;
20350 /* If it was a class type, return the destructor. */
20351 return CLASSTYPE_DESTRUCTORS (type);
20354 /* By this point, the NAME should be an ordinary identifier. If
20355 the id-expression was a qualified name, the qualifying scope is
20356 stored in PARSER->SCOPE at this point. */
20357 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20359 /* Perform the lookup. */
20364 if (parser->scope == error_mark_node)
20365 return error_mark_node;
20367 /* If the SCOPE is dependent, the lookup must be deferred until
20368 the template is instantiated -- unless we are explicitly
20369 looking up names in uninstantiated templates. Even then, we
20370 cannot look up the name if the scope is not a class type; it
20371 might, for example, be a template type parameter. */
20372 dependent_p = (TYPE_P (parser->scope)
20373 && dependent_scope_p (parser->scope));
20374 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20376 /* Defer lookup. */
20377 decl = error_mark_node;
20380 tree pushed_scope = NULL_TREE;
20382 /* If PARSER->SCOPE is a dependent type, then it must be a
20383 class type, and we must not be checking dependencies;
20384 otherwise, we would have processed this lookup above. So
20385 that PARSER->SCOPE is not considered a dependent base by
20386 lookup_member, we must enter the scope here. */
20388 pushed_scope = push_scope (parser->scope);
20390 /* If the PARSER->SCOPE is a template specialization, it
20391 may be instantiated during name lookup. In that case,
20392 errors may be issued. Even if we rollback the current
20393 tentative parse, those errors are valid. */
20394 decl = lookup_qualified_name (parser->scope, name,
20395 tag_type != none_type,
20396 /*complain=*/true);
20398 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20399 lookup result and the nested-name-specifier nominates a class C:
20400 * if the name specified after the nested-name-specifier, when
20401 looked up in C, is the injected-class-name of C (Clause 9), or
20402 * if the name specified after the nested-name-specifier is the
20403 same as the identifier or the simple-template-id's template-
20404 name in the last component of the nested-name-specifier,
20405 the name is instead considered to name the constructor of
20406 class C. [ Note: for example, the constructor is not an
20407 acceptable lookup result in an elaborated-type-specifier so
20408 the constructor would not be used in place of the
20409 injected-class-name. --end note ] Such a constructor name
20410 shall be used only in the declarator-id of a declaration that
20411 names a constructor or in a using-declaration. */
20412 if (tag_type == none_type
20413 && DECL_SELF_REFERENCE_P (decl)
20414 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20415 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20416 tag_type != none_type,
20417 /*complain=*/true);
20419 /* If we have a single function from a using decl, pull it out. */
20420 if (TREE_CODE (decl) == OVERLOAD
20421 && !really_overloaded_fn (decl))
20422 decl = OVL_FUNCTION (decl);
20425 pop_scope (pushed_scope);
20428 /* If the scope is a dependent type and either we deferred lookup or
20429 we did lookup but didn't find the name, rememeber the name. */
20430 if (decl == error_mark_node && TYPE_P (parser->scope)
20431 && dependent_type_p (parser->scope))
20437 /* The resolution to Core Issue 180 says that `struct
20438 A::B' should be considered a type-name, even if `A'
20440 type = make_typename_type (parser->scope, name, tag_type,
20441 /*complain=*/tf_error);
20442 decl = TYPE_NAME (type);
20444 else if (is_template
20445 && (cp_parser_next_token_ends_template_argument_p (parser)
20446 || cp_lexer_next_token_is (parser->lexer,
20448 decl = make_unbound_class_template (parser->scope,
20450 /*complain=*/tf_error);
20452 decl = build_qualified_name (/*type=*/NULL_TREE,
20453 parser->scope, name,
20456 parser->qualifying_scope = parser->scope;
20457 parser->object_scope = NULL_TREE;
20459 else if (object_type)
20461 tree object_decl = NULL_TREE;
20462 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20463 OBJECT_TYPE is not a class. */
20464 if (CLASS_TYPE_P (object_type))
20465 /* If the OBJECT_TYPE is a template specialization, it may
20466 be instantiated during name lookup. In that case, errors
20467 may be issued. Even if we rollback the current tentative
20468 parse, those errors are valid. */
20469 object_decl = lookup_member (object_type,
20472 tag_type != none_type,
20473 tf_warning_or_error);
20474 /* Look it up in the enclosing context, too. */
20475 decl = lookup_name_real (name, tag_type != none_type,
20477 /*block_p=*/true, is_namespace, flags);
20478 parser->object_scope = object_type;
20479 parser->qualifying_scope = NULL_TREE;
20481 decl = object_decl;
20485 decl = lookup_name_real (name, tag_type != none_type,
20487 /*block_p=*/true, is_namespace, flags);
20488 parser->qualifying_scope = NULL_TREE;
20489 parser->object_scope = NULL_TREE;
20492 /* If the lookup failed, let our caller know. */
20493 if (!decl || decl == error_mark_node)
20494 return error_mark_node;
20496 /* Pull out the template from an injected-class-name (or multiple). */
20498 decl = maybe_get_template_decl_from_type_decl (decl);
20500 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20501 if (TREE_CODE (decl) == TREE_LIST)
20503 if (ambiguous_decls)
20504 *ambiguous_decls = decl;
20505 /* The error message we have to print is too complicated for
20506 cp_parser_error, so we incorporate its actions directly. */
20507 if (!cp_parser_simulate_error (parser))
20509 error_at (name_location, "reference to %qD is ambiguous",
20511 print_candidates (decl);
20513 return error_mark_node;
20516 gcc_assert (DECL_P (decl)
20517 || TREE_CODE (decl) == OVERLOAD
20518 || TREE_CODE (decl) == SCOPE_REF
20519 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20520 || BASELINK_P (decl));
20522 /* If we have resolved the name of a member declaration, check to
20523 see if the declaration is accessible. When the name resolves to
20524 set of overloaded functions, accessibility is checked when
20525 overload resolution is done.
20527 During an explicit instantiation, access is not checked at all,
20528 as per [temp.explicit]. */
20530 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20532 maybe_record_typedef_use (decl);
20537 /* Like cp_parser_lookup_name, but for use in the typical case where
20538 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20539 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20542 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20544 return cp_parser_lookup_name (parser, name,
20546 /*is_template=*/false,
20547 /*is_namespace=*/false,
20548 /*check_dependency=*/true,
20549 /*ambiguous_decls=*/NULL,
20553 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20554 the current context, return the TYPE_DECL. If TAG_NAME_P is
20555 true, the DECL indicates the class being defined in a class-head,
20556 or declared in an elaborated-type-specifier.
20558 Otherwise, return DECL. */
20561 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20563 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20564 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20567 template <typename T> struct B;
20570 template <typename T> struct A::B {};
20572 Similarly, in an elaborated-type-specifier:
20574 namespace N { struct X{}; }
20577 template <typename T> friend struct N::X;
20580 However, if the DECL refers to a class type, and we are in
20581 the scope of the class, then the name lookup automatically
20582 finds the TYPE_DECL created by build_self_reference rather
20583 than a TEMPLATE_DECL. For example, in:
20585 template <class T> struct S {
20589 there is no need to handle such case. */
20591 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20592 return DECL_TEMPLATE_RESULT (decl);
20597 /* If too many, or too few, template-parameter lists apply to the
20598 declarator, issue an error message. Returns TRUE if all went well,
20599 and FALSE otherwise. */
20602 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20603 cp_declarator *declarator,
20604 location_t declarator_location)
20606 unsigned num_templates;
20608 /* We haven't seen any classes that involve template parameters yet. */
20611 switch (declarator->kind)
20614 if (declarator->u.id.qualifying_scope)
20618 scope = declarator->u.id.qualifying_scope;
20620 while (scope && CLASS_TYPE_P (scope))
20622 /* You're supposed to have one `template <...>'
20623 for every template class, but you don't need one
20624 for a full specialization. For example:
20626 template <class T> struct S{};
20627 template <> struct S<int> { void f(); };
20628 void S<int>::f () {}
20630 is correct; there shouldn't be a `template <>' for
20631 the definition of `S<int>::f'. */
20632 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20633 /* If SCOPE does not have template information of any
20634 kind, then it is not a template, nor is it nested
20635 within a template. */
20637 if (explicit_class_specialization_p (scope))
20639 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20642 scope = TYPE_CONTEXT (scope);
20645 else if (TREE_CODE (declarator->u.id.unqualified_name)
20646 == TEMPLATE_ID_EXPR)
20647 /* If the DECLARATOR has the form `X<y>' then it uses one
20648 additional level of template parameters. */
20651 return cp_parser_check_template_parameters
20652 (parser, num_templates, declarator_location, declarator);
20658 case cdk_reference:
20660 return (cp_parser_check_declarator_template_parameters
20661 (parser, declarator->declarator, declarator_location));
20667 gcc_unreachable ();
20672 /* NUM_TEMPLATES were used in the current declaration. If that is
20673 invalid, return FALSE and issue an error messages. Otherwise,
20674 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20675 declarator and we can print more accurate diagnostics. */
20678 cp_parser_check_template_parameters (cp_parser* parser,
20679 unsigned num_templates,
20680 location_t location,
20681 cp_declarator *declarator)
20683 /* If there are the same number of template classes and parameter
20684 lists, that's OK. */
20685 if (parser->num_template_parameter_lists == num_templates)
20687 /* If there are more, but only one more, then we are referring to a
20688 member template. That's OK too. */
20689 if (parser->num_template_parameter_lists == num_templates + 1)
20691 /* If there are more template classes than parameter lists, we have
20694 template <class T> void S<T>::R<T>::f (); */
20695 if (parser->num_template_parameter_lists < num_templates)
20697 if (declarator && !current_function_decl)
20698 error_at (location, "specializing member %<%T::%E%> "
20699 "requires %<template<>%> syntax",
20700 declarator->u.id.qualifying_scope,
20701 declarator->u.id.unqualified_name);
20702 else if (declarator)
20703 error_at (location, "invalid declaration of %<%T::%E%>",
20704 declarator->u.id.qualifying_scope,
20705 declarator->u.id.unqualified_name);
20707 error_at (location, "too few template-parameter-lists");
20710 /* Otherwise, there are too many template parameter lists. We have
20713 template <class T> template <class U> void S::f(); */
20714 error_at (location, "too many template-parameter-lists");
20718 /* Parse an optional `::' token indicating that the following name is
20719 from the global namespace. If so, PARSER->SCOPE is set to the
20720 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20721 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20722 Returns the new value of PARSER->SCOPE, if the `::' token is
20723 present, and NULL_TREE otherwise. */
20726 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20730 /* Peek at the next token. */
20731 token = cp_lexer_peek_token (parser->lexer);
20732 /* If we're looking at a `::' token then we're starting from the
20733 global namespace, not our current location. */
20734 if (token->type == CPP_SCOPE)
20736 /* Consume the `::' token. */
20737 cp_lexer_consume_token (parser->lexer);
20738 /* Set the SCOPE so that we know where to start the lookup. */
20739 parser->scope = global_namespace;
20740 parser->qualifying_scope = global_namespace;
20741 parser->object_scope = NULL_TREE;
20743 return parser->scope;
20745 else if (!current_scope_valid_p)
20747 parser->scope = NULL_TREE;
20748 parser->qualifying_scope = NULL_TREE;
20749 parser->object_scope = NULL_TREE;
20755 /* Returns TRUE if the upcoming token sequence is the start of a
20756 constructor declarator. If FRIEND_P is true, the declarator is
20757 preceded by the `friend' specifier. */
20760 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20762 bool constructor_p;
20763 tree nested_name_specifier;
20764 cp_token *next_token;
20766 /* The common case is that this is not a constructor declarator, so
20767 try to avoid doing lots of work if at all possible. It's not
20768 valid declare a constructor at function scope. */
20769 if (parser->in_function_body)
20771 /* And only certain tokens can begin a constructor declarator. */
20772 next_token = cp_lexer_peek_token (parser->lexer);
20773 if (next_token->type != CPP_NAME
20774 && next_token->type != CPP_SCOPE
20775 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20776 && next_token->type != CPP_TEMPLATE_ID)
20779 /* Parse tentatively; we are going to roll back all of the tokens
20781 cp_parser_parse_tentatively (parser);
20782 /* Assume that we are looking at a constructor declarator. */
20783 constructor_p = true;
20785 /* Look for the optional `::' operator. */
20786 cp_parser_global_scope_opt (parser,
20787 /*current_scope_valid_p=*/false);
20788 /* Look for the nested-name-specifier. */
20789 nested_name_specifier
20790 = (cp_parser_nested_name_specifier_opt (parser,
20791 /*typename_keyword_p=*/false,
20792 /*check_dependency_p=*/false,
20794 /*is_declaration=*/false));
20795 /* Outside of a class-specifier, there must be a
20796 nested-name-specifier. */
20797 if (!nested_name_specifier &&
20798 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20800 constructor_p = false;
20801 else if (nested_name_specifier == error_mark_node)
20802 constructor_p = false;
20804 /* If we have a class scope, this is easy; DR 147 says that S::S always
20805 names the constructor, and no other qualified name could. */
20806 if (constructor_p && nested_name_specifier
20807 && CLASS_TYPE_P (nested_name_specifier))
20809 tree id = cp_parser_unqualified_id (parser,
20810 /*template_keyword_p=*/false,
20811 /*check_dependency_p=*/false,
20812 /*declarator_p=*/true,
20813 /*optional_p=*/false);
20814 if (is_overloaded_fn (id))
20815 id = DECL_NAME (get_first_fn (id));
20816 if (!constructor_name_p (id, nested_name_specifier))
20817 constructor_p = false;
20819 /* If we still think that this might be a constructor-declarator,
20820 look for a class-name. */
20821 else if (constructor_p)
20825 template <typename T> struct S {
20829 we must recognize that the nested `S' names a class. */
20831 type_decl = cp_parser_class_name (parser,
20832 /*typename_keyword_p=*/false,
20833 /*template_keyword_p=*/false,
20835 /*check_dependency_p=*/false,
20836 /*class_head_p=*/false,
20837 /*is_declaration=*/false);
20838 /* If there was no class-name, then this is not a constructor. */
20839 constructor_p = !cp_parser_error_occurred (parser);
20841 /* If we're still considering a constructor, we have to see a `(',
20842 to begin the parameter-declaration-clause, followed by either a
20843 `)', an `...', or a decl-specifier. We need to check for a
20844 type-specifier to avoid being fooled into thinking that:
20848 is a constructor. (It is actually a function named `f' that
20849 takes one parameter (of type `int') and returns a value of type
20852 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20853 constructor_p = false;
20856 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20857 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20858 /* A parameter declaration begins with a decl-specifier,
20859 which is either the "attribute" keyword, a storage class
20860 specifier, or (usually) a type-specifier. */
20861 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20864 tree pushed_scope = NULL_TREE;
20865 unsigned saved_num_template_parameter_lists;
20867 /* Names appearing in the type-specifier should be looked up
20868 in the scope of the class. */
20869 if (current_class_type)
20873 type = TREE_TYPE (type_decl);
20874 if (TREE_CODE (type) == TYPENAME_TYPE)
20876 type = resolve_typename_type (type,
20877 /*only_current_p=*/false);
20878 if (TREE_CODE (type) == TYPENAME_TYPE)
20880 cp_parser_abort_tentative_parse (parser);
20884 pushed_scope = push_scope (type);
20887 /* Inside the constructor parameter list, surrounding
20888 template-parameter-lists do not apply. */
20889 saved_num_template_parameter_lists
20890 = parser->num_template_parameter_lists;
20891 parser->num_template_parameter_lists = 0;
20893 /* Look for the type-specifier. */
20894 cp_parser_type_specifier (parser,
20895 CP_PARSER_FLAGS_NONE,
20896 /*decl_specs=*/NULL,
20897 /*is_declarator=*/true,
20898 /*declares_class_or_enum=*/NULL,
20899 /*is_cv_qualifier=*/NULL);
20901 parser->num_template_parameter_lists
20902 = saved_num_template_parameter_lists;
20904 /* Leave the scope of the class. */
20906 pop_scope (pushed_scope);
20908 constructor_p = !cp_parser_error_occurred (parser);
20912 /* We did not really want to consume any tokens. */
20913 cp_parser_abort_tentative_parse (parser);
20915 return constructor_p;
20918 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20919 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20920 they must be performed once we are in the scope of the function.
20922 Returns the function defined. */
20925 cp_parser_function_definition_from_specifiers_and_declarator
20926 (cp_parser* parser,
20927 cp_decl_specifier_seq *decl_specifiers,
20929 const cp_declarator *declarator)
20934 /* Begin the function-definition. */
20935 success_p = start_function (decl_specifiers, declarator, attributes);
20937 /* The things we're about to see are not directly qualified by any
20938 template headers we've seen thus far. */
20939 reset_specialization ();
20941 /* If there were names looked up in the decl-specifier-seq that we
20942 did not check, check them now. We must wait until we are in the
20943 scope of the function to perform the checks, since the function
20944 might be a friend. */
20945 perform_deferred_access_checks ();
20949 /* Skip the entire function. */
20950 cp_parser_skip_to_end_of_block_or_statement (parser);
20951 fn = error_mark_node;
20953 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20955 /* Seen already, skip it. An error message has already been output. */
20956 cp_parser_skip_to_end_of_block_or_statement (parser);
20957 fn = current_function_decl;
20958 current_function_decl = NULL_TREE;
20959 /* If this is a function from a class, pop the nested class. */
20960 if (current_class_name)
20961 pop_nested_class ();
20966 if (DECL_DECLARED_INLINE_P (current_function_decl))
20967 tv = TV_PARSE_INLINE;
20969 tv = TV_PARSE_FUNC;
20971 fn = cp_parser_function_definition_after_declarator (parser,
20972 /*inline_p=*/false);
20979 /* Parse the part of a function-definition that follows the
20980 declarator. INLINE_P is TRUE iff this function is an inline
20981 function defined within a class-specifier.
20983 Returns the function defined. */
20986 cp_parser_function_definition_after_declarator (cp_parser* parser,
20990 bool ctor_initializer_p = false;
20991 bool saved_in_unbraced_linkage_specification_p;
20992 bool saved_in_function_body;
20993 unsigned saved_num_template_parameter_lists;
20996 saved_in_function_body = parser->in_function_body;
20997 parser->in_function_body = true;
20998 /* If the next token is `return', then the code may be trying to
20999 make use of the "named return value" extension that G++ used to
21001 token = cp_lexer_peek_token (parser->lexer);
21002 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21004 /* Consume the `return' keyword. */
21005 cp_lexer_consume_token (parser->lexer);
21006 /* Look for the identifier that indicates what value is to be
21008 cp_parser_identifier (parser);
21009 /* Issue an error message. */
21010 error_at (token->location,
21011 "named return values are no longer supported");
21012 /* Skip tokens until we reach the start of the function body. */
21015 cp_token *token = cp_lexer_peek_token (parser->lexer);
21016 if (token->type == CPP_OPEN_BRACE
21017 || token->type == CPP_EOF
21018 || token->type == CPP_PRAGMA_EOL)
21020 cp_lexer_consume_token (parser->lexer);
21023 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21024 anything declared inside `f'. */
21025 saved_in_unbraced_linkage_specification_p
21026 = parser->in_unbraced_linkage_specification_p;
21027 parser->in_unbraced_linkage_specification_p = false;
21028 /* Inside the function, surrounding template-parameter-lists do not
21030 saved_num_template_parameter_lists
21031 = parser->num_template_parameter_lists;
21032 parser->num_template_parameter_lists = 0;
21034 start_lambda_scope (current_function_decl);
21036 /* If the next token is `try', `__transaction_atomic', or
21037 `__transaction_relaxed`, then we are looking at either function-try-block
21038 or function-transaction-block. Note that all of these include the
21040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21041 ctor_initializer_p = cp_parser_function_transaction (parser,
21042 RID_TRANSACTION_ATOMIC);
21043 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21044 RID_TRANSACTION_RELAXED))
21045 ctor_initializer_p = cp_parser_function_transaction (parser,
21046 RID_TRANSACTION_RELAXED);
21047 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21048 ctor_initializer_p = cp_parser_function_try_block (parser);
21051 = cp_parser_ctor_initializer_opt_and_function_body (parser);
21053 finish_lambda_scope ();
21055 /* Finish the function. */
21056 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21057 (inline_p ? 2 : 0));
21058 /* Generate code for it, if necessary. */
21059 expand_or_defer_fn (fn);
21060 /* Restore the saved values. */
21061 parser->in_unbraced_linkage_specification_p
21062 = saved_in_unbraced_linkage_specification_p;
21063 parser->num_template_parameter_lists
21064 = saved_num_template_parameter_lists;
21065 parser->in_function_body = saved_in_function_body;
21070 /* Parse a template-declaration, assuming that the `export' (and
21071 `extern') keywords, if present, has already been scanned. MEMBER_P
21072 is as for cp_parser_template_declaration. */
21075 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21077 tree decl = NULL_TREE;
21078 VEC (deferred_access_check,gc) *checks;
21079 tree parameter_list;
21080 bool friend_p = false;
21081 bool need_lang_pop;
21084 /* Look for the `template' keyword. */
21085 token = cp_lexer_peek_token (parser->lexer);
21086 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21090 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21092 if (at_class_scope_p () && current_function_decl)
21094 /* 14.5.2.2 [temp.mem]
21096 A local class shall not have member templates. */
21097 error_at (token->location,
21098 "invalid declaration of member template in local class");
21099 cp_parser_skip_to_end_of_block_or_statement (parser);
21104 A template ... shall not have C linkage. */
21105 if (current_lang_name == lang_name_c)
21107 error_at (token->location, "template with C linkage");
21108 /* Give it C++ linkage to avoid confusing other parts of the
21110 push_lang_context (lang_name_cplusplus);
21111 need_lang_pop = true;
21114 need_lang_pop = false;
21116 /* We cannot perform access checks on the template parameter
21117 declarations until we know what is being declared, just as we
21118 cannot check the decl-specifier list. */
21119 push_deferring_access_checks (dk_deferred);
21121 /* If the next token is `>', then we have an invalid
21122 specialization. Rather than complain about an invalid template
21123 parameter, issue an error message here. */
21124 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21126 cp_parser_error (parser, "invalid explicit specialization");
21127 begin_specialization ();
21128 parameter_list = NULL_TREE;
21132 /* Parse the template parameters. */
21133 parameter_list = cp_parser_template_parameter_list (parser);
21134 fixup_template_parms ();
21137 /* Get the deferred access checks from the parameter list. These
21138 will be checked once we know what is being declared, as for a
21139 member template the checks must be performed in the scope of the
21140 class containing the member. */
21141 checks = get_deferred_access_checks ();
21143 /* Look for the `>'. */
21144 cp_parser_skip_to_end_of_template_parameter_list (parser);
21145 /* We just processed one more parameter list. */
21146 ++parser->num_template_parameter_lists;
21147 /* If the next token is `template', there are more template
21149 if (cp_lexer_next_token_is_keyword (parser->lexer,
21151 cp_parser_template_declaration_after_export (parser, member_p);
21152 else if (cxx_dialect >= cxx0x
21153 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21154 decl = cp_parser_alias_declaration (parser);
21157 /* There are no access checks when parsing a template, as we do not
21158 know if a specialization will be a friend. */
21159 push_deferring_access_checks (dk_no_check);
21160 token = cp_lexer_peek_token (parser->lexer);
21161 decl = cp_parser_single_declaration (parser,
21164 /*explicit_specialization_p=*/false,
21166 pop_deferring_access_checks ();
21168 /* If this is a member template declaration, let the front
21170 if (member_p && !friend_p && decl)
21172 if (TREE_CODE (decl) == TYPE_DECL)
21173 cp_parser_check_access_in_redeclaration (decl, token->location);
21175 decl = finish_member_template_decl (decl);
21177 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21178 make_friend_class (current_class_type, TREE_TYPE (decl),
21179 /*complain=*/true);
21181 /* We are done with the current parameter list. */
21182 --parser->num_template_parameter_lists;
21184 pop_deferring_access_checks ();
21187 finish_template_decl (parameter_list);
21189 /* Check the template arguments for a literal operator template. */
21191 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21192 && UDLIT_OPER_P (DECL_NAME (decl)))
21195 if (parameter_list == NULL_TREE)
21199 int num_parms = TREE_VEC_LENGTH (parameter_list);
21200 if (num_parms != 1)
21204 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21205 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21206 if (TREE_TYPE (parm) != char_type_node
21207 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21212 error ("literal operator template %qD has invalid parameter list."
21213 " Expected non-type template argument pack <char...>",
21216 /* Register member declarations. */
21217 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21218 finish_member_declaration (decl);
21219 /* For the erroneous case of a template with C linkage, we pushed an
21220 implicit C++ linkage scope; exit that scope now. */
21222 pop_lang_context ();
21223 /* If DECL is a function template, we must return to parse it later.
21224 (Even though there is no definition, there might be default
21225 arguments that need handling.) */
21226 if (member_p && decl
21227 && (TREE_CODE (decl) == FUNCTION_DECL
21228 || DECL_FUNCTION_TEMPLATE_P (decl)))
21229 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21232 /* Perform the deferred access checks from a template-parameter-list.
21233 CHECKS is a TREE_LIST of access checks, as returned by
21234 get_deferred_access_checks. */
21237 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21239 ++processing_template_parmlist;
21240 perform_access_checks (checks);
21241 --processing_template_parmlist;
21244 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21245 `function-definition' sequence. MEMBER_P is true, this declaration
21246 appears in a class scope.
21248 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21249 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21252 cp_parser_single_declaration (cp_parser* parser,
21253 VEC (deferred_access_check,gc)* checks,
21255 bool explicit_specialization_p,
21258 int declares_class_or_enum;
21259 tree decl = NULL_TREE;
21260 cp_decl_specifier_seq decl_specifiers;
21261 bool function_definition_p = false;
21262 cp_token *decl_spec_token_start;
21264 /* This function is only used when processing a template
21266 gcc_assert (innermost_scope_kind () == sk_template_parms
21267 || innermost_scope_kind () == sk_template_spec);
21269 /* Defer access checks until we know what is being declared. */
21270 push_deferring_access_checks (dk_deferred);
21272 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21274 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21275 cp_parser_decl_specifier_seq (parser,
21276 CP_PARSER_FLAGS_OPTIONAL,
21278 &declares_class_or_enum);
21280 *friend_p = cp_parser_friend_p (&decl_specifiers);
21282 /* There are no template typedefs. */
21283 if (decl_specifiers.specs[(int) ds_typedef])
21285 error_at (decl_spec_token_start->location,
21286 "template declaration of %<typedef%>");
21287 decl = error_mark_node;
21290 /* Gather up the access checks that occurred the
21291 decl-specifier-seq. */
21292 stop_deferring_access_checks ();
21294 /* Check for the declaration of a template class. */
21295 if (declares_class_or_enum)
21297 if (cp_parser_declares_only_class_p (parser))
21299 decl = shadow_tag (&decl_specifiers);
21304 friend template <typename T> struct A<T>::B;
21307 A<T>::B will be represented by a TYPENAME_TYPE, and
21308 therefore not recognized by shadow_tag. */
21309 if (friend_p && *friend_p
21311 && decl_specifiers.type
21312 && TYPE_P (decl_specifiers.type))
21313 decl = decl_specifiers.type;
21315 if (decl && decl != error_mark_node)
21316 decl = TYPE_NAME (decl);
21318 decl = error_mark_node;
21320 /* Perform access checks for template parameters. */
21321 cp_parser_perform_template_parameter_access_checks (checks);
21325 /* Complain about missing 'typename' or other invalid type names. */
21326 if (!decl_specifiers.any_type_specifiers_p
21327 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21329 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21330 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21331 the rest of this declaration. */
21332 decl = error_mark_node;
21336 /* If it's not a template class, try for a template function. If
21337 the next token is a `;', then this declaration does not declare
21338 anything. But, if there were errors in the decl-specifiers, then
21339 the error might well have come from an attempted class-specifier.
21340 In that case, there's no need to warn about a missing declarator. */
21342 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21343 || decl_specifiers.type != error_mark_node))
21345 decl = cp_parser_init_declarator (parser,
21348 /*function_definition_allowed_p=*/true,
21350 declares_class_or_enum,
21351 &function_definition_p,
21354 /* 7.1.1-1 [dcl.stc]
21356 A storage-class-specifier shall not be specified in an explicit
21357 specialization... */
21359 && explicit_specialization_p
21360 && decl_specifiers.storage_class != sc_none)
21362 error_at (decl_spec_token_start->location,
21363 "explicit template specialization cannot have a storage class");
21364 decl = error_mark_node;
21368 /* Look for a trailing `;' after the declaration. */
21369 if (!function_definition_p
21370 && (decl == error_mark_node
21371 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21372 cp_parser_skip_to_end_of_block_or_statement (parser);
21375 pop_deferring_access_checks ();
21377 /* Clear any current qualification; whatever comes next is the start
21378 of something new. */
21379 parser->scope = NULL_TREE;
21380 parser->qualifying_scope = NULL_TREE;
21381 parser->object_scope = NULL_TREE;
21386 /* Parse a cast-expression that is not the operand of a unary "&". */
21389 cp_parser_simple_cast_expression (cp_parser *parser)
21391 return cp_parser_cast_expression (parser, /*address_p=*/false,
21392 /*cast_p=*/false, NULL);
21395 /* Parse a functional cast to TYPE. Returns an expression
21396 representing the cast. */
21399 cp_parser_functional_cast (cp_parser* parser, tree type)
21402 tree expression_list;
21406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21408 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21409 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21410 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21411 if (TREE_CODE (type) == TYPE_DECL)
21412 type = TREE_TYPE (type);
21413 return finish_compound_literal (type, expression_list,
21414 tf_warning_or_error);
21418 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21420 /*allow_expansion_p=*/true,
21421 /*non_constant_p=*/NULL);
21423 expression_list = error_mark_node;
21426 expression_list = build_tree_list_vec (vec);
21427 release_tree_vector (vec);
21430 cast = build_functional_cast (type, expression_list,
21431 tf_warning_or_error);
21432 /* [expr.const]/1: In an integral constant expression "only type
21433 conversions to integral or enumeration type can be used". */
21434 if (TREE_CODE (type) == TYPE_DECL)
21435 type = TREE_TYPE (type);
21436 if (cast != error_mark_node
21437 && !cast_valid_in_integral_constant_expression_p (type)
21438 && cp_parser_non_integral_constant_expression (parser,
21440 return error_mark_node;
21444 /* Save the tokens that make up the body of a member function defined
21445 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21446 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21447 specifiers applied to the declaration. Returns the FUNCTION_DECL
21448 for the member function. */
21451 cp_parser_save_member_function_body (cp_parser* parser,
21452 cp_decl_specifier_seq *decl_specifiers,
21453 cp_declarator *declarator,
21460 /* Create the FUNCTION_DECL. */
21461 fn = grokmethod (decl_specifiers, declarator, attributes);
21462 /* If something went badly wrong, bail out now. */
21463 if (fn == error_mark_node)
21465 /* If there's a function-body, skip it. */
21466 if (cp_parser_token_starts_function_definition_p
21467 (cp_lexer_peek_token (parser->lexer)))
21468 cp_parser_skip_to_end_of_block_or_statement (parser);
21469 return error_mark_node;
21472 /* Remember it, if there default args to post process. */
21473 cp_parser_save_default_args (parser, fn);
21475 /* Save away the tokens that make up the body of the
21477 first = parser->lexer->next_token;
21478 /* We can have braced-init-list mem-initializers before the fn body. */
21479 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21481 cp_lexer_consume_token (parser->lexer);
21482 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21483 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21485 /* cache_group will stop after an un-nested { } pair, too. */
21486 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21489 /* variadic mem-inits have ... after the ')'. */
21490 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21491 cp_lexer_consume_token (parser->lexer);
21494 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21495 /* Handle function try blocks. */
21496 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21497 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21498 last = parser->lexer->next_token;
21500 /* Save away the inline definition; we will process it when the
21501 class is complete. */
21502 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21503 DECL_PENDING_INLINE_P (fn) = 1;
21505 /* We need to know that this was defined in the class, so that
21506 friend templates are handled correctly. */
21507 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21509 /* Add FN to the queue of functions to be parsed later. */
21510 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21515 /* Save the tokens that make up the in-class initializer for a non-static
21516 data member. Returns a DEFAULT_ARG. */
21519 cp_parser_save_nsdmi (cp_parser* parser)
21521 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21524 /* Parse a template-argument-list, as well as the trailing ">" (but
21525 not the opening "<"). See cp_parser_template_argument_list for the
21529 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21533 tree saved_qualifying_scope;
21534 tree saved_object_scope;
21535 bool saved_greater_than_is_operator_p;
21536 int saved_unevaluated_operand;
21537 int saved_inhibit_evaluation_warnings;
21541 When parsing a template-id, the first non-nested `>' is taken as
21542 the end of the template-argument-list rather than a greater-than
21544 saved_greater_than_is_operator_p
21545 = parser->greater_than_is_operator_p;
21546 parser->greater_than_is_operator_p = false;
21547 /* Parsing the argument list may modify SCOPE, so we save it
21549 saved_scope = parser->scope;
21550 saved_qualifying_scope = parser->qualifying_scope;
21551 saved_object_scope = parser->object_scope;
21552 /* We need to evaluate the template arguments, even though this
21553 template-id may be nested within a "sizeof". */
21554 saved_unevaluated_operand = cp_unevaluated_operand;
21555 cp_unevaluated_operand = 0;
21556 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21557 c_inhibit_evaluation_warnings = 0;
21558 /* Parse the template-argument-list itself. */
21559 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21560 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21561 arguments = NULL_TREE;
21563 arguments = cp_parser_template_argument_list (parser);
21564 /* Look for the `>' that ends the template-argument-list. If we find
21565 a '>>' instead, it's probably just a typo. */
21566 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21568 if (cxx_dialect != cxx98)
21570 /* In C++0x, a `>>' in a template argument list or cast
21571 expression is considered to be two separate `>'
21572 tokens. So, change the current token to a `>', but don't
21573 consume it: it will be consumed later when the outer
21574 template argument list (or cast expression) is parsed.
21575 Note that this replacement of `>' for `>>' is necessary
21576 even if we are parsing tentatively: in the tentative
21577 case, after calling
21578 cp_parser_enclosed_template_argument_list we will always
21579 throw away all of the template arguments and the first
21580 closing `>', either because the template argument list
21581 was erroneous or because we are replacing those tokens
21582 with a CPP_TEMPLATE_ID token. The second `>' (which will
21583 not have been thrown away) is needed either to close an
21584 outer template argument list or to complete a new-style
21586 cp_token *token = cp_lexer_peek_token (parser->lexer);
21587 token->type = CPP_GREATER;
21589 else if (!saved_greater_than_is_operator_p)
21591 /* If we're in a nested template argument list, the '>>' has
21592 to be a typo for '> >'. We emit the error message, but we
21593 continue parsing and we push a '>' as next token, so that
21594 the argument list will be parsed correctly. Note that the
21595 global source location is still on the token before the
21596 '>>', so we need to say explicitly where we want it. */
21597 cp_token *token = cp_lexer_peek_token (parser->lexer);
21598 error_at (token->location, "%<>>%> should be %<> >%> "
21599 "within a nested template argument list");
21601 token->type = CPP_GREATER;
21605 /* If this is not a nested template argument list, the '>>'
21606 is a typo for '>'. Emit an error message and continue.
21607 Same deal about the token location, but here we can get it
21608 right by consuming the '>>' before issuing the diagnostic. */
21609 cp_token *token = cp_lexer_consume_token (parser->lexer);
21610 error_at (token->location,
21611 "spurious %<>>%>, use %<>%> to terminate "
21612 "a template argument list");
21616 cp_parser_skip_to_end_of_template_parameter_list (parser);
21617 /* The `>' token might be a greater-than operator again now. */
21618 parser->greater_than_is_operator_p
21619 = saved_greater_than_is_operator_p;
21620 /* Restore the SAVED_SCOPE. */
21621 parser->scope = saved_scope;
21622 parser->qualifying_scope = saved_qualifying_scope;
21623 parser->object_scope = saved_object_scope;
21624 cp_unevaluated_operand = saved_unevaluated_operand;
21625 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21630 /* MEMBER_FUNCTION is a member function, or a friend. If default
21631 arguments, or the body of the function have not yet been parsed,
21635 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21637 timevar_push (TV_PARSE_INMETH);
21638 /* If this member is a template, get the underlying
21640 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21641 member_function = DECL_TEMPLATE_RESULT (member_function);
21643 /* There should not be any class definitions in progress at this
21644 point; the bodies of members are only parsed outside of all class
21646 gcc_assert (parser->num_classes_being_defined == 0);
21647 /* While we're parsing the member functions we might encounter more
21648 classes. We want to handle them right away, but we don't want
21649 them getting mixed up with functions that are currently in the
21651 push_unparsed_function_queues (parser);
21653 /* Make sure that any template parameters are in scope. */
21654 maybe_begin_member_template_processing (member_function);
21656 /* If the body of the function has not yet been parsed, parse it
21658 if (DECL_PENDING_INLINE_P (member_function))
21660 tree function_scope;
21661 cp_token_cache *tokens;
21663 /* The function is no longer pending; we are processing it. */
21664 tokens = DECL_PENDING_INLINE_INFO (member_function);
21665 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21666 DECL_PENDING_INLINE_P (member_function) = 0;
21668 /* If this is a local class, enter the scope of the containing
21670 function_scope = current_function_decl;
21671 if (function_scope)
21672 push_function_context ();
21674 /* Push the body of the function onto the lexer stack. */
21675 cp_parser_push_lexer_for_tokens (parser, tokens);
21677 /* Let the front end know that we going to be defining this
21679 start_preparsed_function (member_function, NULL_TREE,
21680 SF_PRE_PARSED | SF_INCLASS_INLINE);
21682 /* Don't do access checking if it is a templated function. */
21683 if (processing_template_decl)
21684 push_deferring_access_checks (dk_no_check);
21686 /* Now, parse the body of the function. */
21687 cp_parser_function_definition_after_declarator (parser,
21688 /*inline_p=*/true);
21690 if (processing_template_decl)
21691 pop_deferring_access_checks ();
21693 /* Leave the scope of the containing function. */
21694 if (function_scope)
21695 pop_function_context ();
21696 cp_parser_pop_lexer (parser);
21699 /* Remove any template parameters from the symbol table. */
21700 maybe_end_member_template_processing ();
21702 /* Restore the queue. */
21703 pop_unparsed_function_queues (parser);
21704 timevar_pop (TV_PARSE_INMETH);
21707 /* If DECL contains any default args, remember it on the unparsed
21708 functions queue. */
21711 cp_parser_save_default_args (cp_parser* parser, tree decl)
21715 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21717 probe = TREE_CHAIN (probe))
21718 if (TREE_PURPOSE (probe))
21720 cp_default_arg_entry *entry
21721 = VEC_safe_push (cp_default_arg_entry, gc,
21722 unparsed_funs_with_default_args, NULL);
21723 entry->class_type = current_class_type;
21724 entry->decl = decl;
21729 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21730 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21731 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21732 from the parameter-type-list. */
21735 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21736 tree default_arg, tree parmtype)
21738 cp_token_cache *tokens;
21742 if (default_arg == error_mark_node)
21743 return error_mark_node;
21745 /* Push the saved tokens for the default argument onto the parser's
21747 tokens = DEFARG_TOKENS (default_arg);
21748 cp_parser_push_lexer_for_tokens (parser, tokens);
21750 start_lambda_scope (decl);
21752 /* Parse the default argument. */
21753 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21754 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21755 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21757 finish_lambda_scope ();
21759 if (!processing_template_decl)
21761 /* In a non-template class, check conversions now. In a template,
21762 we'll wait and instantiate these as needed. */
21763 if (TREE_CODE (decl) == PARM_DECL)
21764 parsed_arg = check_default_argument (parmtype, parsed_arg);
21767 int flags = LOOKUP_IMPLICIT;
21768 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21769 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21770 flags = LOOKUP_NORMAL;
21771 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21775 /* If the token stream has not been completely used up, then
21776 there was extra junk after the end of the default
21778 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21780 if (TREE_CODE (decl) == PARM_DECL)
21781 cp_parser_error (parser, "expected %<,%>");
21783 cp_parser_error (parser, "expected %<;%>");
21786 /* Revert to the main lexer. */
21787 cp_parser_pop_lexer (parser);
21792 /* FIELD is a non-static data member with an initializer which we saved for
21793 later; parse it now. */
21796 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21800 push_unparsed_function_queues (parser);
21801 def = cp_parser_late_parse_one_default_arg (parser, field,
21802 DECL_INITIAL (field),
21804 pop_unparsed_function_queues (parser);
21806 DECL_INITIAL (field) = def;
21809 /* FN is a FUNCTION_DECL which may contains a parameter with an
21810 unparsed DEFAULT_ARG. Parse the default args now. This function
21811 assumes that the current scope is the scope in which the default
21812 argument should be processed. */
21815 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21817 bool saved_local_variables_forbidden_p;
21818 tree parm, parmdecl;
21820 /* While we're parsing the default args, we might (due to the
21821 statement expression extension) encounter more classes. We want
21822 to handle them right away, but we don't want them getting mixed
21823 up with default args that are currently in the queue. */
21824 push_unparsed_function_queues (parser);
21826 /* Local variable names (and the `this' keyword) may not appear
21827 in a default argument. */
21828 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21829 parser->local_variables_forbidden_p = true;
21831 push_defarg_context (fn);
21833 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21834 parmdecl = DECL_ARGUMENTS (fn);
21835 parm && parm != void_list_node;
21836 parm = TREE_CHAIN (parm),
21837 parmdecl = DECL_CHAIN (parmdecl))
21839 tree default_arg = TREE_PURPOSE (parm);
21841 VEC(tree,gc) *insts;
21848 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21849 /* This can happen for a friend declaration for a function
21850 already declared with default arguments. */
21854 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21856 TREE_VALUE (parm));
21857 if (parsed_arg == error_mark_node)
21862 TREE_PURPOSE (parm) = parsed_arg;
21864 /* Update any instantiations we've already created. */
21865 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21866 VEC_iterate (tree, insts, ix, copy); ix++)
21867 TREE_PURPOSE (copy) = parsed_arg;
21870 pop_defarg_context ();
21872 /* Make sure no default arg is missing. */
21873 check_default_args (fn);
21875 /* Restore the state of local_variables_forbidden_p. */
21876 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21878 /* Restore the queue. */
21879 pop_unparsed_function_queues (parser);
21882 /* Parse the operand of `sizeof' (or a similar operator). Returns
21883 either a TYPE or an expression, depending on the form of the
21884 input. The KEYWORD indicates which kind of expression we have
21888 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21890 tree expr = NULL_TREE;
21891 const char *saved_message;
21893 bool saved_integral_constant_expression_p;
21894 bool saved_non_integral_constant_expression_p;
21895 bool pack_expansion_p = false;
21897 /* Types cannot be defined in a `sizeof' expression. Save away the
21899 saved_message = parser->type_definition_forbidden_message;
21900 /* And create the new one. */
21901 tmp = concat ("types may not be defined in %<",
21902 IDENTIFIER_POINTER (ridpointers[keyword]),
21903 "%> expressions", NULL);
21904 parser->type_definition_forbidden_message = tmp;
21906 /* The restrictions on constant-expressions do not apply inside
21907 sizeof expressions. */
21908 saved_integral_constant_expression_p
21909 = parser->integral_constant_expression_p;
21910 saved_non_integral_constant_expression_p
21911 = parser->non_integral_constant_expression_p;
21912 parser->integral_constant_expression_p = false;
21914 /* If it's a `...', then we are computing the length of a parameter
21916 if (keyword == RID_SIZEOF
21917 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21919 /* Consume the `...'. */
21920 cp_lexer_consume_token (parser->lexer);
21921 maybe_warn_variadic_templates ();
21923 /* Note that this is an expansion. */
21924 pack_expansion_p = true;
21927 /* Do not actually evaluate the expression. */
21928 ++cp_unevaluated_operand;
21929 ++c_inhibit_evaluation_warnings;
21930 /* If it's a `(', then we might be looking at the type-id
21932 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21935 bool saved_in_type_id_in_expr_p;
21937 /* We can't be sure yet whether we're looking at a type-id or an
21939 cp_parser_parse_tentatively (parser);
21940 /* Consume the `('. */
21941 cp_lexer_consume_token (parser->lexer);
21942 /* Parse the type-id. */
21943 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21944 parser->in_type_id_in_expr_p = true;
21945 type = cp_parser_type_id (parser);
21946 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21947 /* Now, look for the trailing `)'. */
21948 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21949 /* If all went well, then we're done. */
21950 if (cp_parser_parse_definitely (parser))
21952 cp_decl_specifier_seq decl_specs;
21954 /* Build a trivial decl-specifier-seq. */
21955 clear_decl_specs (&decl_specs);
21956 decl_specs.type = type;
21958 /* Call grokdeclarator to figure out what type this is. */
21959 expr = grokdeclarator (NULL,
21963 /*attrlist=*/NULL);
21967 /* If the type-id production did not work out, then we must be
21968 looking at the unary-expression production. */
21970 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21971 /*cast_p=*/false, NULL);
21973 if (pack_expansion_p)
21974 /* Build a pack expansion. */
21975 expr = make_pack_expansion (expr);
21977 /* Go back to evaluating expressions. */
21978 --cp_unevaluated_operand;
21979 --c_inhibit_evaluation_warnings;
21981 /* Free the message we created. */
21983 /* And restore the old one. */
21984 parser->type_definition_forbidden_message = saved_message;
21985 parser->integral_constant_expression_p
21986 = saved_integral_constant_expression_p;
21987 parser->non_integral_constant_expression_p
21988 = saved_non_integral_constant_expression_p;
21993 /* If the current declaration has no declarator, return true. */
21996 cp_parser_declares_only_class_p (cp_parser *parser)
21998 /* If the next token is a `;' or a `,' then there is no
22000 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22001 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22004 /* Update the DECL_SPECS to reflect the storage class indicated by
22008 cp_parser_set_storage_class (cp_parser *parser,
22009 cp_decl_specifier_seq *decl_specs,
22011 location_t location)
22013 cp_storage_class storage_class;
22015 if (parser->in_unbraced_linkage_specification_p)
22017 error_at (location, "invalid use of %qD in linkage specification",
22018 ridpointers[keyword]);
22021 else if (decl_specs->storage_class != sc_none)
22023 decl_specs->conflicting_specifiers_p = true;
22027 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22028 && decl_specs->specs[(int) ds_thread])
22030 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22031 decl_specs->specs[(int) ds_thread] = 0;
22037 storage_class = sc_auto;
22040 storage_class = sc_register;
22043 storage_class = sc_static;
22046 storage_class = sc_extern;
22049 storage_class = sc_mutable;
22052 gcc_unreachable ();
22054 decl_specs->storage_class = storage_class;
22056 /* A storage class specifier cannot be applied alongside a typedef
22057 specifier. If there is a typedef specifier present then set
22058 conflicting_specifiers_p which will trigger an error later
22059 on in grokdeclarator. */
22060 if (decl_specs->specs[(int)ds_typedef])
22061 decl_specs->conflicting_specifiers_p = true;
22064 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22065 is true, the type is a class or enum definition. */
22068 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22070 location_t location,
22071 bool type_definition_p)
22073 decl_specs->any_specifiers_p = true;
22075 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22076 (with, for example, in "typedef int wchar_t;") we remember that
22077 this is what happened. In system headers, we ignore these
22078 declarations so that G++ can work with system headers that are not
22080 if (decl_specs->specs[(int) ds_typedef]
22081 && !type_definition_p
22082 && (type_spec == boolean_type_node
22083 || type_spec == char16_type_node
22084 || type_spec == char32_type_node
22085 || type_spec == wchar_type_node)
22086 && (decl_specs->type
22087 || decl_specs->specs[(int) ds_long]
22088 || decl_specs->specs[(int) ds_short]
22089 || decl_specs->specs[(int) ds_unsigned]
22090 || decl_specs->specs[(int) ds_signed]))
22092 decl_specs->redefined_builtin_type = type_spec;
22093 if (!decl_specs->type)
22095 decl_specs->type = type_spec;
22096 decl_specs->type_definition_p = false;
22097 decl_specs->type_location = location;
22100 else if (decl_specs->type)
22101 decl_specs->multiple_types_p = true;
22104 decl_specs->type = type_spec;
22105 decl_specs->type_definition_p = type_definition_p;
22106 decl_specs->redefined_builtin_type = NULL_TREE;
22107 decl_specs->type_location = location;
22111 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22112 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22115 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22117 return decl_specifiers->specs[(int) ds_friend] != 0;
22120 /* Issue an error message indicating that TOKEN_DESC was expected.
22121 If KEYWORD is true, it indicated this function is called by
22122 cp_parser_require_keword and the required token can only be
22123 a indicated keyword. */
22126 cp_parser_required_error (cp_parser *parser,
22127 required_token token_desc,
22130 switch (token_desc)
22133 cp_parser_error (parser, "expected %<new%>");
22136 cp_parser_error (parser, "expected %<delete%>");
22139 cp_parser_error (parser, "expected %<return%>");
22142 cp_parser_error (parser, "expected %<while%>");
22145 cp_parser_error (parser, "expected %<extern%>");
22147 case RT_STATIC_ASSERT:
22148 cp_parser_error (parser, "expected %<static_assert%>");
22151 cp_parser_error (parser, "expected %<decltype%>");
22154 cp_parser_error (parser, "expected %<operator%>");
22157 cp_parser_error (parser, "expected %<class%>");
22160 cp_parser_error (parser, "expected %<template%>");
22163 cp_parser_error (parser, "expected %<namespace%>");
22166 cp_parser_error (parser, "expected %<using%>");
22169 cp_parser_error (parser, "expected %<asm%>");
22172 cp_parser_error (parser, "expected %<try%>");
22175 cp_parser_error (parser, "expected %<catch%>");
22178 cp_parser_error (parser, "expected %<throw%>");
22181 cp_parser_error (parser, "expected %<__label__%>");
22184 cp_parser_error (parser, "expected %<@try%>");
22186 case RT_AT_SYNCHRONIZED:
22187 cp_parser_error (parser, "expected %<@synchronized%>");
22190 cp_parser_error (parser, "expected %<@throw%>");
22192 case RT_TRANSACTION_ATOMIC:
22193 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22195 case RT_TRANSACTION_RELAXED:
22196 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22203 switch (token_desc)
22206 cp_parser_error (parser, "expected %<;%>");
22208 case RT_OPEN_PAREN:
22209 cp_parser_error (parser, "expected %<(%>");
22211 case RT_CLOSE_BRACE:
22212 cp_parser_error (parser, "expected %<}%>");
22214 case RT_OPEN_BRACE:
22215 cp_parser_error (parser, "expected %<{%>");
22217 case RT_CLOSE_SQUARE:
22218 cp_parser_error (parser, "expected %<]%>");
22220 case RT_OPEN_SQUARE:
22221 cp_parser_error (parser, "expected %<[%>");
22224 cp_parser_error (parser, "expected %<,%>");
22227 cp_parser_error (parser, "expected %<::%>");
22230 cp_parser_error (parser, "expected %<<%>");
22233 cp_parser_error (parser, "expected %<>%>");
22236 cp_parser_error (parser, "expected %<=%>");
22239 cp_parser_error (parser, "expected %<...%>");
22242 cp_parser_error (parser, "expected %<*%>");
22245 cp_parser_error (parser, "expected %<~%>");
22248 cp_parser_error (parser, "expected %<:%>");
22250 case RT_COLON_SCOPE:
22251 cp_parser_error (parser, "expected %<:%> or %<::%>");
22253 case RT_CLOSE_PAREN:
22254 cp_parser_error (parser, "expected %<)%>");
22256 case RT_COMMA_CLOSE_PAREN:
22257 cp_parser_error (parser, "expected %<,%> or %<)%>");
22259 case RT_PRAGMA_EOL:
22260 cp_parser_error (parser, "expected end of line");
22263 cp_parser_error (parser, "expected identifier");
22266 cp_parser_error (parser, "expected selection-statement");
22268 case RT_INTERATION:
22269 cp_parser_error (parser, "expected iteration-statement");
22272 cp_parser_error (parser, "expected jump-statement");
22275 cp_parser_error (parser, "expected class-key");
22277 case RT_CLASS_TYPENAME_TEMPLATE:
22278 cp_parser_error (parser,
22279 "expected %<class%>, %<typename%>, or %<template%>");
22282 gcc_unreachable ();
22286 gcc_unreachable ();
22291 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22292 issue an error message indicating that TOKEN_DESC was expected.
22294 Returns the token consumed, if the token had the appropriate type.
22295 Otherwise, returns NULL. */
22298 cp_parser_require (cp_parser* parser,
22299 enum cpp_ttype type,
22300 required_token token_desc)
22302 if (cp_lexer_next_token_is (parser->lexer, type))
22303 return cp_lexer_consume_token (parser->lexer);
22306 /* Output the MESSAGE -- unless we're parsing tentatively. */
22307 if (!cp_parser_simulate_error (parser))
22308 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22313 /* An error message is produced if the next token is not '>'.
22314 All further tokens are skipped until the desired token is
22315 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22318 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22320 /* Current level of '< ... >'. */
22321 unsigned level = 0;
22322 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22323 unsigned nesting_depth = 0;
22325 /* Are we ready, yet? If not, issue error message. */
22326 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22329 /* Skip tokens until the desired token is found. */
22332 /* Peek at the next token. */
22333 switch (cp_lexer_peek_token (parser->lexer)->type)
22336 if (!nesting_depth)
22341 if (cxx_dialect == cxx98)
22342 /* C++0x views the `>>' operator as two `>' tokens, but
22345 else if (!nesting_depth && level-- == 0)
22347 /* We've hit a `>>' where the first `>' closes the
22348 template argument list, and the second `>' is
22349 spurious. Just consume the `>>' and stop; we've
22350 already produced at least one error. */
22351 cp_lexer_consume_token (parser->lexer);
22354 /* Fall through for C++0x, so we handle the second `>' in
22358 if (!nesting_depth && level-- == 0)
22360 /* We've reached the token we want, consume it and stop. */
22361 cp_lexer_consume_token (parser->lexer);
22366 case CPP_OPEN_PAREN:
22367 case CPP_OPEN_SQUARE:
22371 case CPP_CLOSE_PAREN:
22372 case CPP_CLOSE_SQUARE:
22373 if (nesting_depth-- == 0)
22378 case CPP_PRAGMA_EOL:
22379 case CPP_SEMICOLON:
22380 case CPP_OPEN_BRACE:
22381 case CPP_CLOSE_BRACE:
22382 /* The '>' was probably forgotten, don't look further. */
22389 /* Consume this token. */
22390 cp_lexer_consume_token (parser->lexer);
22394 /* If the next token is the indicated keyword, consume it. Otherwise,
22395 issue an error message indicating that TOKEN_DESC was expected.
22397 Returns the token consumed, if the token had the appropriate type.
22398 Otherwise, returns NULL. */
22401 cp_parser_require_keyword (cp_parser* parser,
22403 required_token token_desc)
22405 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22407 if (token && token->keyword != keyword)
22409 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22416 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22417 function-definition. */
22420 cp_parser_token_starts_function_definition_p (cp_token* token)
22422 return (/* An ordinary function-body begins with an `{'. */
22423 token->type == CPP_OPEN_BRACE
22424 /* A ctor-initializer begins with a `:'. */
22425 || token->type == CPP_COLON
22426 /* A function-try-block begins with `try'. */
22427 || token->keyword == RID_TRY
22428 /* A function-transaction-block begins with `__transaction_atomic'
22429 or `__transaction_relaxed'. */
22430 || token->keyword == RID_TRANSACTION_ATOMIC
22431 || token->keyword == RID_TRANSACTION_RELAXED
22432 /* The named return value extension begins with `return'. */
22433 || token->keyword == RID_RETURN);
22436 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22440 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22444 token = cp_lexer_peek_token (parser->lexer);
22445 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22448 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22449 C++0x) ending a template-argument. */
22452 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22456 token = cp_lexer_peek_token (parser->lexer);
22457 return (token->type == CPP_COMMA
22458 || token->type == CPP_GREATER
22459 || token->type == CPP_ELLIPSIS
22460 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22463 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22464 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22467 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22472 token = cp_lexer_peek_nth_token (parser->lexer, n);
22473 if (token->type == CPP_LESS)
22475 /* Check for the sequence `<::' in the original code. It would be lexed as
22476 `[:', where `[' is a digraph, and there is no whitespace before
22478 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22481 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22482 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22488 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22489 or none_type otherwise. */
22491 static enum tag_types
22492 cp_parser_token_is_class_key (cp_token* token)
22494 switch (token->keyword)
22499 return record_type;
22508 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22511 cp_parser_check_class_key (enum tag_types class_key, tree type)
22513 if (type == error_mark_node)
22515 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22517 permerror (input_location, "%qs tag used in naming %q#T",
22518 class_key == union_type ? "union"
22519 : class_key == record_type ? "struct" : "class",
22521 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22522 "%q#T was previously declared here", type);
22526 /* Issue an error message if DECL is redeclared with different
22527 access than its original declaration [class.access.spec/3].
22528 This applies to nested classes and nested class templates.
22532 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22534 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22537 if ((TREE_PRIVATE (decl)
22538 != (current_access_specifier == access_private_node))
22539 || (TREE_PROTECTED (decl)
22540 != (current_access_specifier == access_protected_node)))
22541 error_at (location, "%qD redeclared with different access", decl);
22544 /* Look for the `template' keyword, as a syntactic disambiguator.
22545 Return TRUE iff it is present, in which case it will be
22549 cp_parser_optional_template_keyword (cp_parser *parser)
22551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22553 /* The `template' keyword can only be used within templates;
22554 outside templates the parser can always figure out what is a
22555 template and what is not. */
22556 if (!processing_template_decl)
22558 cp_token *token = cp_lexer_peek_token (parser->lexer);
22559 error_at (token->location,
22560 "%<template%> (as a disambiguator) is only allowed "
22561 "within templates");
22562 /* If this part of the token stream is rescanned, the same
22563 error message would be generated. So, we purge the token
22564 from the stream. */
22565 cp_lexer_purge_token (parser->lexer);
22570 /* Consume the `template' keyword. */
22571 cp_lexer_consume_token (parser->lexer);
22579 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22580 set PARSER->SCOPE, and perform other related actions. */
22583 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22586 struct tree_check *check_value;
22587 deferred_access_check *chk;
22588 VEC (deferred_access_check,gc) *checks;
22590 /* Get the stored value. */
22591 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22592 /* Perform any access checks that were deferred. */
22593 checks = check_value->checks;
22596 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22597 perform_or_defer_access_check (chk->binfo,
22601 /* Set the scope from the stored value. */
22602 parser->scope = check_value->value;
22603 parser->qualifying_scope = check_value->qualifying_scope;
22604 parser->object_scope = NULL_TREE;
22607 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22608 encounter the end of a block before what we were looking for. */
22611 cp_parser_cache_group (cp_parser *parser,
22612 enum cpp_ttype end,
22617 cp_token *token = cp_lexer_peek_token (parser->lexer);
22619 /* Abort a parenthesized expression if we encounter a semicolon. */
22620 if ((end == CPP_CLOSE_PAREN || depth == 0)
22621 && token->type == CPP_SEMICOLON)
22623 /* If we've reached the end of the file, stop. */
22624 if (token->type == CPP_EOF
22625 || (end != CPP_PRAGMA_EOL
22626 && token->type == CPP_PRAGMA_EOL))
22628 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22629 /* We've hit the end of an enclosing block, so there's been some
22630 kind of syntax error. */
22633 /* Consume the token. */
22634 cp_lexer_consume_token (parser->lexer);
22635 /* See if it starts a new group. */
22636 if (token->type == CPP_OPEN_BRACE)
22638 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22639 /* In theory this should probably check end == '}', but
22640 cp_parser_save_member_function_body needs it to exit
22641 after either '}' or ')' when called with ')'. */
22645 else if (token->type == CPP_OPEN_PAREN)
22647 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22648 if (depth == 0 && end == CPP_CLOSE_PAREN)
22651 else if (token->type == CPP_PRAGMA)
22652 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22653 else if (token->type == end)
22658 /* Like above, for caching a default argument or NSDMI. Both of these are
22659 terminated by a non-nested comma, but it can be unclear whether or not a
22660 comma is nested in a template argument list unless we do more parsing.
22661 In order to handle this ambiguity, when we encounter a ',' after a '<'
22662 we try to parse what follows as a parameter-declaration-list (in the
22663 case of a default argument) or a member-declarator (in the case of an
22664 NSDMI). If that succeeds, then we stop caching. */
22667 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22669 unsigned depth = 0;
22670 int maybe_template_id = 0;
22671 cp_token *first_token;
22673 tree default_argument;
22675 /* Add tokens until we have processed the entire default
22676 argument. We add the range [first_token, token). */
22677 first_token = cp_lexer_peek_token (parser->lexer);
22678 if (first_token->type == CPP_OPEN_BRACE)
22680 /* For list-initialization, this is straightforward. */
22681 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22682 token = cp_lexer_peek_token (parser->lexer);
22688 /* Peek at the next token. */
22689 token = cp_lexer_peek_token (parser->lexer);
22690 /* What we do depends on what token we have. */
22691 switch (token->type)
22693 /* In valid code, a default argument must be
22694 immediately followed by a `,' `)', or `...'. */
22696 if (depth == 0 && maybe_template_id)
22698 /* If we've seen a '<', we might be in a
22699 template-argument-list. Until Core issue 325 is
22700 resolved, we don't know how this situation ought
22701 to be handled, so try to DTRT. We check whether
22702 what comes after the comma is a valid parameter
22703 declaration list. If it is, then the comma ends
22704 the default argument; otherwise the default
22705 argument continues. */
22706 bool error = false;
22709 /* Set ITALP so cp_parser_parameter_declaration_list
22710 doesn't decide to commit to this parse. */
22711 bool saved_italp = parser->in_template_argument_list_p;
22712 parser->in_template_argument_list_p = true;
22714 cp_parser_parse_tentatively (parser);
22715 cp_lexer_consume_token (parser->lexer);
22719 int ctor_dtor_or_conv_p;
22720 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22721 &ctor_dtor_or_conv_p,
22722 /*parenthesized_p=*/NULL,
22723 /*member_p=*/true);
22727 begin_scope (sk_function_parms, NULL_TREE);
22728 cp_parser_parameter_declaration_list (parser, &error);
22729 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22730 pop_binding (DECL_NAME (t), t);
22733 if (!cp_parser_error_occurred (parser) && !error)
22735 cp_parser_abort_tentative_parse (parser);
22737 parser->in_template_argument_list_p = saved_italp;
22740 case CPP_CLOSE_PAREN:
22742 /* If we run into a non-nested `;', `}', or `]',
22743 then the code is invalid -- but the default
22744 argument is certainly over. */
22745 case CPP_SEMICOLON:
22746 case CPP_CLOSE_BRACE:
22747 case CPP_CLOSE_SQUARE:
22750 /* Update DEPTH, if necessary. */
22751 else if (token->type == CPP_CLOSE_PAREN
22752 || token->type == CPP_CLOSE_BRACE
22753 || token->type == CPP_CLOSE_SQUARE)
22757 case CPP_OPEN_PAREN:
22758 case CPP_OPEN_SQUARE:
22759 case CPP_OPEN_BRACE:
22765 /* This might be the comparison operator, or it might
22766 start a template argument list. */
22767 ++maybe_template_id;
22771 if (cxx_dialect == cxx98)
22773 /* Fall through for C++0x, which treats the `>>'
22774 operator like two `>' tokens in certain
22780 /* This might be an operator, or it might close a
22781 template argument list. But if a previous '<'
22782 started a template argument list, this will have
22783 closed it, so we can't be in one anymore. */
22784 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22785 if (maybe_template_id < 0)
22786 maybe_template_id = 0;
22790 /* If we run out of tokens, issue an error message. */
22792 case CPP_PRAGMA_EOL:
22793 error_at (token->location, "file ends in default argument");
22799 /* In these cases, we should look for template-ids.
22800 For example, if the default argument is
22801 `X<int, double>()', we need to do name lookup to
22802 figure out whether or not `X' is a template; if
22803 so, the `,' does not end the default argument.
22805 That is not yet done. */
22812 /* If we've reached the end, stop. */
22816 /* Add the token to the token block. */
22817 token = cp_lexer_consume_token (parser->lexer);
22820 /* Create a DEFAULT_ARG to represent the unparsed default
22822 default_argument = make_node (DEFAULT_ARG);
22823 DEFARG_TOKENS (default_argument)
22824 = cp_token_cache_new (first_token, token);
22825 DEFARG_INSTANTIATIONS (default_argument) = NULL;
22827 return default_argument;
22830 /* Begin parsing tentatively. We always save tokens while parsing
22831 tentatively so that if the tentative parsing fails we can restore the
22835 cp_parser_parse_tentatively (cp_parser* parser)
22837 /* Enter a new parsing context. */
22838 parser->context = cp_parser_context_new (parser->context);
22839 /* Begin saving tokens. */
22840 cp_lexer_save_tokens (parser->lexer);
22841 /* In order to avoid repetitive access control error messages,
22842 access checks are queued up until we are no longer parsing
22844 push_deferring_access_checks (dk_deferred);
22847 /* Commit to the currently active tentative parse. */
22850 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22852 cp_parser_context *context;
22855 /* Mark all of the levels as committed. */
22856 lexer = parser->lexer;
22857 for (context = parser->context; context->next; context = context->next)
22859 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22861 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22862 while (!cp_lexer_saving_tokens (lexer))
22863 lexer = lexer->next;
22864 cp_lexer_commit_tokens (lexer);
22868 /* Abort the currently active tentative parse. All consumed tokens
22869 will be rolled back, and no diagnostics will be issued. */
22872 cp_parser_abort_tentative_parse (cp_parser* parser)
22874 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22875 || errorcount > 0);
22876 cp_parser_simulate_error (parser);
22877 /* Now, pretend that we want to see if the construct was
22878 successfully parsed. */
22879 cp_parser_parse_definitely (parser);
22882 /* Stop parsing tentatively. If a parse error has occurred, restore the
22883 token stream. Otherwise, commit to the tokens we have consumed.
22884 Returns true if no error occurred; false otherwise. */
22887 cp_parser_parse_definitely (cp_parser* parser)
22889 bool error_occurred;
22890 cp_parser_context *context;
22892 /* Remember whether or not an error occurred, since we are about to
22893 destroy that information. */
22894 error_occurred = cp_parser_error_occurred (parser);
22895 /* Remove the topmost context from the stack. */
22896 context = parser->context;
22897 parser->context = context->next;
22898 /* If no parse errors occurred, commit to the tentative parse. */
22899 if (!error_occurred)
22901 /* Commit to the tokens read tentatively, unless that was
22903 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22904 cp_lexer_commit_tokens (parser->lexer);
22906 pop_to_parent_deferring_access_checks ();
22908 /* Otherwise, if errors occurred, roll back our state so that things
22909 are just as they were before we began the tentative parse. */
22912 cp_lexer_rollback_tokens (parser->lexer);
22913 pop_deferring_access_checks ();
22915 /* Add the context to the front of the free list. */
22916 context->next = cp_parser_context_free_list;
22917 cp_parser_context_free_list = context;
22919 return !error_occurred;
22922 /* Returns true if we are parsing tentatively and are not committed to
22923 this tentative parse. */
22926 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22928 return (cp_parser_parsing_tentatively (parser)
22929 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22932 /* Returns nonzero iff an error has occurred during the most recent
22933 tentative parse. */
22936 cp_parser_error_occurred (cp_parser* parser)
22938 return (cp_parser_parsing_tentatively (parser)
22939 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22942 /* Returns nonzero if GNU extensions are allowed. */
22945 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22947 return parser->allow_gnu_extensions_p;
22950 /* Objective-C++ Productions */
22953 /* Parse an Objective-C expression, which feeds into a primary-expression
22957 objc-message-expression
22958 objc-string-literal
22959 objc-encode-expression
22960 objc-protocol-expression
22961 objc-selector-expression
22963 Returns a tree representation of the expression. */
22966 cp_parser_objc_expression (cp_parser* parser)
22968 /* Try to figure out what kind of declaration is present. */
22969 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22973 case CPP_OPEN_SQUARE:
22974 return cp_parser_objc_message_expression (parser);
22976 case CPP_OBJC_STRING:
22977 kwd = cp_lexer_consume_token (parser->lexer);
22978 return objc_build_string_object (kwd->u.value);
22981 switch (kwd->keyword)
22983 case RID_AT_ENCODE:
22984 return cp_parser_objc_encode_expression (parser);
22986 case RID_AT_PROTOCOL:
22987 return cp_parser_objc_protocol_expression (parser);
22989 case RID_AT_SELECTOR:
22990 return cp_parser_objc_selector_expression (parser);
22996 error_at (kwd->location,
22997 "misplaced %<@%D%> Objective-C++ construct",
22999 cp_parser_skip_to_end_of_block_or_statement (parser);
23002 return error_mark_node;
23005 /* Parse an Objective-C message expression.
23007 objc-message-expression:
23008 [ objc-message-receiver objc-message-args ]
23010 Returns a representation of an Objective-C message. */
23013 cp_parser_objc_message_expression (cp_parser* parser)
23015 tree receiver, messageargs;
23017 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23018 receiver = cp_parser_objc_message_receiver (parser);
23019 messageargs = cp_parser_objc_message_args (parser);
23020 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23022 return objc_build_message_expr (receiver, messageargs);
23025 /* Parse an objc-message-receiver.
23027 objc-message-receiver:
23029 simple-type-specifier
23031 Returns a representation of the type or expression. */
23034 cp_parser_objc_message_receiver (cp_parser* parser)
23038 /* An Objective-C message receiver may be either (1) a type
23039 or (2) an expression. */
23040 cp_parser_parse_tentatively (parser);
23041 rcv = cp_parser_expression (parser, false, NULL);
23043 if (cp_parser_parse_definitely (parser))
23046 rcv = cp_parser_simple_type_specifier (parser,
23047 /*decl_specs=*/NULL,
23048 CP_PARSER_FLAGS_NONE);
23050 return objc_get_class_reference (rcv);
23053 /* Parse the arguments and selectors comprising an Objective-C message.
23058 objc-selector-args , objc-comma-args
23060 objc-selector-args:
23061 objc-selector [opt] : assignment-expression
23062 objc-selector-args objc-selector [opt] : assignment-expression
23065 assignment-expression
23066 objc-comma-args , assignment-expression
23068 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23069 selector arguments and TREE_VALUE containing a list of comma
23073 cp_parser_objc_message_args (cp_parser* parser)
23075 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23076 bool maybe_unary_selector_p = true;
23077 cp_token *token = cp_lexer_peek_token (parser->lexer);
23079 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23081 tree selector = NULL_TREE, arg;
23083 if (token->type != CPP_COLON)
23084 selector = cp_parser_objc_selector (parser);
23086 /* Detect if we have a unary selector. */
23087 if (maybe_unary_selector_p
23088 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23089 return build_tree_list (selector, NULL_TREE);
23091 maybe_unary_selector_p = false;
23092 cp_parser_require (parser, CPP_COLON, RT_COLON);
23093 arg = cp_parser_assignment_expression (parser, false, NULL);
23096 = chainon (sel_args,
23097 build_tree_list (selector, arg));
23099 token = cp_lexer_peek_token (parser->lexer);
23102 /* Handle non-selector arguments, if any. */
23103 while (token->type == CPP_COMMA)
23107 cp_lexer_consume_token (parser->lexer);
23108 arg = cp_parser_assignment_expression (parser, false, NULL);
23111 = chainon (addl_args,
23112 build_tree_list (NULL_TREE, arg));
23114 token = cp_lexer_peek_token (parser->lexer);
23117 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23119 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23120 return build_tree_list (error_mark_node, error_mark_node);
23123 return build_tree_list (sel_args, addl_args);
23126 /* Parse an Objective-C encode expression.
23128 objc-encode-expression:
23129 @encode objc-typename
23131 Returns an encoded representation of the type argument. */
23134 cp_parser_objc_encode_expression (cp_parser* parser)
23139 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23140 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23141 token = cp_lexer_peek_token (parser->lexer);
23142 type = complete_type (cp_parser_type_id (parser));
23143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23147 error_at (token->location,
23148 "%<@encode%> must specify a type as an argument");
23149 return error_mark_node;
23152 /* This happens if we find @encode(T) (where T is a template
23153 typename or something dependent on a template typename) when
23154 parsing a template. In that case, we can't compile it
23155 immediately, but we rather create an AT_ENCODE_EXPR which will
23156 need to be instantiated when the template is used.
23158 if (dependent_type_p (type))
23160 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23161 TREE_READONLY (value) = 1;
23165 return objc_build_encode_expr (type);
23168 /* Parse an Objective-C @defs expression. */
23171 cp_parser_objc_defs_expression (cp_parser *parser)
23175 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23176 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23177 name = cp_parser_identifier (parser);
23178 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23180 return objc_get_class_ivars (name);
23183 /* Parse an Objective-C protocol expression.
23185 objc-protocol-expression:
23186 @protocol ( identifier )
23188 Returns a representation of the protocol expression. */
23191 cp_parser_objc_protocol_expression (cp_parser* parser)
23195 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23196 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23197 proto = cp_parser_identifier (parser);
23198 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23200 return objc_build_protocol_expr (proto);
23203 /* Parse an Objective-C selector expression.
23205 objc-selector-expression:
23206 @selector ( objc-method-signature )
23208 objc-method-signature:
23214 objc-selector-seq objc-selector :
23216 Returns a representation of the method selector. */
23219 cp_parser_objc_selector_expression (cp_parser* parser)
23221 tree sel_seq = NULL_TREE;
23222 bool maybe_unary_selector_p = true;
23224 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23226 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23227 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23228 token = cp_lexer_peek_token (parser->lexer);
23230 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23231 || token->type == CPP_SCOPE)
23233 tree selector = NULL_TREE;
23235 if (token->type != CPP_COLON
23236 || token->type == CPP_SCOPE)
23237 selector = cp_parser_objc_selector (parser);
23239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23240 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23242 /* Detect if we have a unary selector. */
23243 if (maybe_unary_selector_p)
23245 sel_seq = selector;
23246 goto finish_selector;
23250 cp_parser_error (parser, "expected %<:%>");
23253 maybe_unary_selector_p = false;
23254 token = cp_lexer_consume_token (parser->lexer);
23256 if (token->type == CPP_SCOPE)
23259 = chainon (sel_seq,
23260 build_tree_list (selector, NULL_TREE));
23262 = chainon (sel_seq,
23263 build_tree_list (NULL_TREE, NULL_TREE));
23267 = chainon (sel_seq,
23268 build_tree_list (selector, NULL_TREE));
23270 token = cp_lexer_peek_token (parser->lexer);
23274 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23276 return objc_build_selector_expr (loc, sel_seq);
23279 /* Parse a list of identifiers.
23281 objc-identifier-list:
23283 objc-identifier-list , identifier
23285 Returns a TREE_LIST of identifier nodes. */
23288 cp_parser_objc_identifier_list (cp_parser* parser)
23294 identifier = cp_parser_identifier (parser);
23295 if (identifier == error_mark_node)
23296 return error_mark_node;
23298 list = build_tree_list (NULL_TREE, identifier);
23299 sep = cp_lexer_peek_token (parser->lexer);
23301 while (sep->type == CPP_COMMA)
23303 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23304 identifier = cp_parser_identifier (parser);
23305 if (identifier == error_mark_node)
23308 list = chainon (list, build_tree_list (NULL_TREE,
23310 sep = cp_lexer_peek_token (parser->lexer);
23316 /* Parse an Objective-C alias declaration.
23318 objc-alias-declaration:
23319 @compatibility_alias identifier identifier ;
23321 This function registers the alias mapping with the Objective-C front end.
23322 It returns nothing. */
23325 cp_parser_objc_alias_declaration (cp_parser* parser)
23329 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23330 alias = cp_parser_identifier (parser);
23331 orig = cp_parser_identifier (parser);
23332 objc_declare_alias (alias, orig);
23333 cp_parser_consume_semicolon_at_end_of_statement (parser);
23336 /* Parse an Objective-C class forward-declaration.
23338 objc-class-declaration:
23339 @class objc-identifier-list ;
23341 The function registers the forward declarations with the Objective-C
23342 front end. It returns nothing. */
23345 cp_parser_objc_class_declaration (cp_parser* parser)
23347 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23352 id = cp_parser_identifier (parser);
23353 if (id == error_mark_node)
23356 objc_declare_class (id);
23358 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23359 cp_lexer_consume_token (parser->lexer);
23363 cp_parser_consume_semicolon_at_end_of_statement (parser);
23366 /* Parse a list of Objective-C protocol references.
23368 objc-protocol-refs-opt:
23369 objc-protocol-refs [opt]
23371 objc-protocol-refs:
23372 < objc-identifier-list >
23374 Returns a TREE_LIST of identifiers, if any. */
23377 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23379 tree protorefs = NULL_TREE;
23381 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23383 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23384 protorefs = cp_parser_objc_identifier_list (parser);
23385 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23391 /* Parse a Objective-C visibility specification. */
23394 cp_parser_objc_visibility_spec (cp_parser* parser)
23396 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23398 switch (vis->keyword)
23400 case RID_AT_PRIVATE:
23401 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23403 case RID_AT_PROTECTED:
23404 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23406 case RID_AT_PUBLIC:
23407 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23409 case RID_AT_PACKAGE:
23410 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23416 /* Eat '@private'/'@protected'/'@public'. */
23417 cp_lexer_consume_token (parser->lexer);
23420 /* Parse an Objective-C method type. Return 'true' if it is a class
23421 (+) method, and 'false' if it is an instance (-) method. */
23424 cp_parser_objc_method_type (cp_parser* parser)
23426 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23432 /* Parse an Objective-C protocol qualifier. */
23435 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23437 tree quals = NULL_TREE, node;
23438 cp_token *token = cp_lexer_peek_token (parser->lexer);
23440 node = token->u.value;
23442 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23443 && (node == ridpointers [(int) RID_IN]
23444 || node == ridpointers [(int) RID_OUT]
23445 || node == ridpointers [(int) RID_INOUT]
23446 || node == ridpointers [(int) RID_BYCOPY]
23447 || node == ridpointers [(int) RID_BYREF]
23448 || node == ridpointers [(int) RID_ONEWAY]))
23450 quals = tree_cons (NULL_TREE, node, quals);
23451 cp_lexer_consume_token (parser->lexer);
23452 token = cp_lexer_peek_token (parser->lexer);
23453 node = token->u.value;
23459 /* Parse an Objective-C typename. */
23462 cp_parser_objc_typename (cp_parser* parser)
23464 tree type_name = NULL_TREE;
23466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23468 tree proto_quals, cp_type = NULL_TREE;
23470 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23471 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23473 /* An ObjC type name may consist of just protocol qualifiers, in which
23474 case the type shall default to 'id'. */
23475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23477 cp_type = cp_parser_type_id (parser);
23479 /* If the type could not be parsed, an error has already
23480 been produced. For error recovery, behave as if it had
23481 not been specified, which will use the default type
23483 if (cp_type == error_mark_node)
23485 cp_type = NULL_TREE;
23486 /* We need to skip to the closing parenthesis as
23487 cp_parser_type_id() does not seem to do it for
23489 cp_parser_skip_to_closing_parenthesis (parser,
23490 /*recovering=*/true,
23491 /*or_comma=*/false,
23492 /*consume_paren=*/false);
23496 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23497 type_name = build_tree_list (proto_quals, cp_type);
23503 /* Check to see if TYPE refers to an Objective-C selector name. */
23506 cp_parser_objc_selector_p (enum cpp_ttype type)
23508 return (type == CPP_NAME || type == CPP_KEYWORD
23509 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23510 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23511 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23512 || type == CPP_XOR || type == CPP_XOR_EQ);
23515 /* Parse an Objective-C selector. */
23518 cp_parser_objc_selector (cp_parser* parser)
23520 cp_token *token = cp_lexer_consume_token (parser->lexer);
23522 if (!cp_parser_objc_selector_p (token->type))
23524 error_at (token->location, "invalid Objective-C++ selector name");
23525 return error_mark_node;
23528 /* C++ operator names are allowed to appear in ObjC selectors. */
23529 switch (token->type)
23531 case CPP_AND_AND: return get_identifier ("and");
23532 case CPP_AND_EQ: return get_identifier ("and_eq");
23533 case CPP_AND: return get_identifier ("bitand");
23534 case CPP_OR: return get_identifier ("bitor");
23535 case CPP_COMPL: return get_identifier ("compl");
23536 case CPP_NOT: return get_identifier ("not");
23537 case CPP_NOT_EQ: return get_identifier ("not_eq");
23538 case CPP_OR_OR: return get_identifier ("or");
23539 case CPP_OR_EQ: return get_identifier ("or_eq");
23540 case CPP_XOR: return get_identifier ("xor");
23541 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23542 default: return token->u.value;
23546 /* Parse an Objective-C params list. */
23549 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23551 tree params = NULL_TREE;
23552 bool maybe_unary_selector_p = true;
23553 cp_token *token = cp_lexer_peek_token (parser->lexer);
23555 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23557 tree selector = NULL_TREE, type_name, identifier;
23558 tree parm_attr = NULL_TREE;
23560 if (token->keyword == RID_ATTRIBUTE)
23563 if (token->type != CPP_COLON)
23564 selector = cp_parser_objc_selector (parser);
23566 /* Detect if we have a unary selector. */
23567 if (maybe_unary_selector_p
23568 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23570 params = selector; /* Might be followed by attributes. */
23574 maybe_unary_selector_p = false;
23575 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23577 /* Something went quite wrong. There should be a colon
23578 here, but there is not. Stop parsing parameters. */
23581 type_name = cp_parser_objc_typename (parser);
23582 /* New ObjC allows attributes on parameters too. */
23583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23584 parm_attr = cp_parser_attributes_opt (parser);
23585 identifier = cp_parser_identifier (parser);
23589 objc_build_keyword_decl (selector,
23594 token = cp_lexer_peek_token (parser->lexer);
23597 if (params == NULL_TREE)
23599 cp_parser_error (parser, "objective-c++ method declaration is expected");
23600 return error_mark_node;
23603 /* We allow tail attributes for the method. */
23604 if (token->keyword == RID_ATTRIBUTE)
23606 *attributes = cp_parser_attributes_opt (parser);
23607 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23608 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23610 cp_parser_error (parser,
23611 "method attributes must be specified at the end");
23612 return error_mark_node;
23615 if (params == NULL_TREE)
23617 cp_parser_error (parser, "objective-c++ method declaration is expected");
23618 return error_mark_node;
23623 /* Parse the non-keyword Objective-C params. */
23626 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23629 tree params = make_node (TREE_LIST);
23630 cp_token *token = cp_lexer_peek_token (parser->lexer);
23631 *ellipsisp = false; /* Initially, assume no ellipsis. */
23633 while (token->type == CPP_COMMA)
23635 cp_parameter_declarator *parmdecl;
23638 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23639 token = cp_lexer_peek_token (parser->lexer);
23641 if (token->type == CPP_ELLIPSIS)
23643 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23645 token = cp_lexer_peek_token (parser->lexer);
23649 /* TODO: parse attributes for tail parameters. */
23650 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23651 parm = grokdeclarator (parmdecl->declarator,
23652 &parmdecl->decl_specifiers,
23653 PARM, /*initialized=*/0,
23654 /*attrlist=*/NULL);
23656 chainon (params, build_tree_list (NULL_TREE, parm));
23657 token = cp_lexer_peek_token (parser->lexer);
23660 /* We allow tail attributes for the method. */
23661 if (token->keyword == RID_ATTRIBUTE)
23663 if (*attributes == NULL_TREE)
23665 *attributes = cp_parser_attributes_opt (parser);
23666 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23667 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23671 /* We have an error, but parse the attributes, so that we can
23673 *attributes = cp_parser_attributes_opt (parser);
23675 cp_parser_error (parser,
23676 "method attributes must be specified at the end");
23677 return error_mark_node;
23683 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23686 cp_parser_objc_interstitial_code (cp_parser* parser)
23688 cp_token *token = cp_lexer_peek_token (parser->lexer);
23690 /* If the next token is `extern' and the following token is a string
23691 literal, then we have a linkage specification. */
23692 if (token->keyword == RID_EXTERN
23693 && cp_parser_is_pure_string_literal
23694 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23695 cp_parser_linkage_specification (parser);
23696 /* Handle #pragma, if any. */
23697 else if (token->type == CPP_PRAGMA)
23698 cp_parser_pragma (parser, pragma_external);
23699 /* Allow stray semicolons. */
23700 else if (token->type == CPP_SEMICOLON)
23701 cp_lexer_consume_token (parser->lexer);
23702 /* Mark methods as optional or required, when building protocols. */
23703 else if (token->keyword == RID_AT_OPTIONAL)
23705 cp_lexer_consume_token (parser->lexer);
23706 objc_set_method_opt (true);
23708 else if (token->keyword == RID_AT_REQUIRED)
23710 cp_lexer_consume_token (parser->lexer);
23711 objc_set_method_opt (false);
23713 else if (token->keyword == RID_NAMESPACE)
23714 cp_parser_namespace_definition (parser);
23715 /* Other stray characters must generate errors. */
23716 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23718 cp_lexer_consume_token (parser->lexer);
23719 error ("stray %qs between Objective-C++ methods",
23720 token->type == CPP_OPEN_BRACE ? "{" : "}");
23722 /* Finally, try to parse a block-declaration, or a function-definition. */
23724 cp_parser_block_declaration (parser, /*statement_p=*/false);
23727 /* Parse a method signature. */
23730 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23732 tree rettype, kwdparms, optparms;
23733 bool ellipsis = false;
23734 bool is_class_method;
23736 is_class_method = cp_parser_objc_method_type (parser);
23737 rettype = cp_parser_objc_typename (parser);
23738 *attributes = NULL_TREE;
23739 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23740 if (kwdparms == error_mark_node)
23741 return error_mark_node;
23742 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23743 if (optparms == error_mark_node)
23744 return error_mark_node;
23746 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23750 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23753 cp_lexer_save_tokens (parser->lexer);
23754 tattr = cp_parser_attributes_opt (parser);
23755 gcc_assert (tattr) ;
23757 /* If the attributes are followed by a method introducer, this is not allowed.
23758 Dump the attributes and flag the situation. */
23759 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23760 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23763 /* Otherwise, the attributes introduce some interstitial code, possibly so
23764 rewind to allow that check. */
23765 cp_lexer_rollback_tokens (parser->lexer);
23769 /* Parse an Objective-C method prototype list. */
23772 cp_parser_objc_method_prototype_list (cp_parser* parser)
23774 cp_token *token = cp_lexer_peek_token (parser->lexer);
23776 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23778 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23780 tree attributes, sig;
23781 bool is_class_method;
23782 if (token->type == CPP_PLUS)
23783 is_class_method = true;
23785 is_class_method = false;
23786 sig = cp_parser_objc_method_signature (parser, &attributes);
23787 if (sig == error_mark_node)
23789 cp_parser_skip_to_end_of_block_or_statement (parser);
23790 token = cp_lexer_peek_token (parser->lexer);
23793 objc_add_method_declaration (is_class_method, sig, attributes);
23794 cp_parser_consume_semicolon_at_end_of_statement (parser);
23796 else if (token->keyword == RID_AT_PROPERTY)
23797 cp_parser_objc_at_property_declaration (parser);
23798 else if (token->keyword == RID_ATTRIBUTE
23799 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23800 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23802 "prefix attributes are ignored for methods");
23804 /* Allow for interspersed non-ObjC++ code. */
23805 cp_parser_objc_interstitial_code (parser);
23807 token = cp_lexer_peek_token (parser->lexer);
23810 if (token->type != CPP_EOF)
23811 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23813 cp_parser_error (parser, "expected %<@end%>");
23815 objc_finish_interface ();
23818 /* Parse an Objective-C method definition list. */
23821 cp_parser_objc_method_definition_list (cp_parser* parser)
23823 cp_token *token = cp_lexer_peek_token (parser->lexer);
23825 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23829 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23832 tree sig, attribute;
23833 bool is_class_method;
23834 if (token->type == CPP_PLUS)
23835 is_class_method = true;
23837 is_class_method = false;
23838 push_deferring_access_checks (dk_deferred);
23839 sig = cp_parser_objc_method_signature (parser, &attribute);
23840 if (sig == error_mark_node)
23842 cp_parser_skip_to_end_of_block_or_statement (parser);
23843 token = cp_lexer_peek_token (parser->lexer);
23846 objc_start_method_definition (is_class_method, sig, attribute,
23849 /* For historical reasons, we accept an optional semicolon. */
23850 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23851 cp_lexer_consume_token (parser->lexer);
23853 ptk = cp_lexer_peek_token (parser->lexer);
23854 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23855 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23857 perform_deferred_access_checks ();
23858 stop_deferring_access_checks ();
23859 meth = cp_parser_function_definition_after_declarator (parser,
23861 pop_deferring_access_checks ();
23862 objc_finish_method_definition (meth);
23865 /* The following case will be removed once @synthesize is
23866 completely implemented. */
23867 else if (token->keyword == RID_AT_PROPERTY)
23868 cp_parser_objc_at_property_declaration (parser);
23869 else if (token->keyword == RID_AT_SYNTHESIZE)
23870 cp_parser_objc_at_synthesize_declaration (parser);
23871 else if (token->keyword == RID_AT_DYNAMIC)
23872 cp_parser_objc_at_dynamic_declaration (parser);
23873 else if (token->keyword == RID_ATTRIBUTE
23874 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23875 warning_at (token->location, OPT_Wattributes,
23876 "prefix attributes are ignored for methods");
23878 /* Allow for interspersed non-ObjC++ code. */
23879 cp_parser_objc_interstitial_code (parser);
23881 token = cp_lexer_peek_token (parser->lexer);
23884 if (token->type != CPP_EOF)
23885 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23887 cp_parser_error (parser, "expected %<@end%>");
23889 objc_finish_implementation ();
23892 /* Parse Objective-C ivars. */
23895 cp_parser_objc_class_ivars (cp_parser* parser)
23897 cp_token *token = cp_lexer_peek_token (parser->lexer);
23899 if (token->type != CPP_OPEN_BRACE)
23900 return; /* No ivars specified. */
23902 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
23903 token = cp_lexer_peek_token (parser->lexer);
23905 while (token->type != CPP_CLOSE_BRACE
23906 && token->keyword != RID_AT_END && token->type != CPP_EOF)
23908 cp_decl_specifier_seq declspecs;
23909 int decl_class_or_enum_p;
23910 tree prefix_attributes;
23912 cp_parser_objc_visibility_spec (parser);
23914 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23917 cp_parser_decl_specifier_seq (parser,
23918 CP_PARSER_FLAGS_OPTIONAL,
23920 &decl_class_or_enum_p);
23922 /* auto, register, static, extern, mutable. */
23923 if (declspecs.storage_class != sc_none)
23925 cp_parser_error (parser, "invalid type for instance variable");
23926 declspecs.storage_class = sc_none;
23930 if (declspecs.specs[(int) ds_thread])
23932 cp_parser_error (parser, "invalid type for instance variable");
23933 declspecs.specs[(int) ds_thread] = 0;
23937 if (declspecs.specs[(int) ds_typedef])
23939 cp_parser_error (parser, "invalid type for instance variable");
23940 declspecs.specs[(int) ds_typedef] = 0;
23943 prefix_attributes = declspecs.attributes;
23944 declspecs.attributes = NULL_TREE;
23946 /* Keep going until we hit the `;' at the end of the
23948 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23950 tree width = NULL_TREE, attributes, first_attribute, decl;
23951 cp_declarator *declarator = NULL;
23952 int ctor_dtor_or_conv_p;
23954 /* Check for a (possibly unnamed) bitfield declaration. */
23955 token = cp_lexer_peek_token (parser->lexer);
23956 if (token->type == CPP_COLON)
23959 if (token->type == CPP_NAME
23960 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23963 /* Get the name of the bitfield. */
23964 declarator = make_id_declarator (NULL_TREE,
23965 cp_parser_identifier (parser),
23969 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23970 /* Get the width of the bitfield. */
23972 = cp_parser_constant_expression (parser,
23973 /*allow_non_constant=*/false,
23978 /* Parse the declarator. */
23980 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23981 &ctor_dtor_or_conv_p,
23982 /*parenthesized_p=*/NULL,
23983 /*member_p=*/false);
23986 /* Look for attributes that apply to the ivar. */
23987 attributes = cp_parser_attributes_opt (parser);
23988 /* Remember which attributes are prefix attributes and
23990 first_attribute = attributes;
23991 /* Combine the attributes. */
23992 attributes = chainon (prefix_attributes, attributes);
23995 /* Create the bitfield declaration. */
23996 decl = grokbitfield (declarator, &declspecs,
24000 decl = grokfield (declarator, &declspecs,
24001 NULL_TREE, /*init_const_expr_p=*/false,
24002 NULL_TREE, attributes);
24004 /* Add the instance variable. */
24005 if (decl != error_mark_node && decl != NULL_TREE)
24006 objc_add_instance_variable (decl);
24008 /* Reset PREFIX_ATTRIBUTES. */
24009 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24010 attributes = TREE_CHAIN (attributes);
24012 TREE_CHAIN (attributes) = NULL_TREE;
24014 token = cp_lexer_peek_token (parser->lexer);
24016 if (token->type == CPP_COMMA)
24018 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24024 cp_parser_consume_semicolon_at_end_of_statement (parser);
24025 token = cp_lexer_peek_token (parser->lexer);
24028 if (token->keyword == RID_AT_END)
24029 cp_parser_error (parser, "expected %<}%>");
24031 /* Do not consume the RID_AT_END, so it will be read again as terminating
24032 the @interface of @implementation. */
24033 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24034 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24036 /* For historical reasons, we accept an optional semicolon. */
24037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24038 cp_lexer_consume_token (parser->lexer);
24041 /* Parse an Objective-C protocol declaration. */
24044 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24046 tree proto, protorefs;
24049 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24052 tok = cp_lexer_peek_token (parser->lexer);
24053 error_at (tok->location, "identifier expected after %<@protocol%>");
24054 cp_parser_consume_semicolon_at_end_of_statement (parser);
24058 /* See if we have a forward declaration or a definition. */
24059 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24061 /* Try a forward declaration first. */
24062 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24068 id = cp_parser_identifier (parser);
24069 if (id == error_mark_node)
24072 objc_declare_protocol (id, attributes);
24074 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24075 cp_lexer_consume_token (parser->lexer);
24079 cp_parser_consume_semicolon_at_end_of_statement (parser);
24082 /* Ok, we got a full-fledged definition (or at least should). */
24085 proto = cp_parser_identifier (parser);
24086 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24087 objc_start_protocol (proto, protorefs, attributes);
24088 cp_parser_objc_method_prototype_list (parser);
24092 /* Parse an Objective-C superclass or category. */
24095 cp_parser_objc_superclass_or_category (cp_parser *parser,
24098 tree *categ, bool *is_class_extension)
24100 cp_token *next = cp_lexer_peek_token (parser->lexer);
24102 *super = *categ = NULL_TREE;
24103 *is_class_extension = false;
24104 if (next->type == CPP_COLON)
24106 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24107 *super = cp_parser_identifier (parser);
24109 else if (next->type == CPP_OPEN_PAREN)
24111 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24113 /* If there is no category name, and this is an @interface, we
24114 have a class extension. */
24115 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24117 *categ = NULL_TREE;
24118 *is_class_extension = true;
24121 *categ = cp_parser_identifier (parser);
24123 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24127 /* Parse an Objective-C class interface. */
24130 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24132 tree name, super, categ, protos;
24133 bool is_class_extension;
24135 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24136 name = cp_parser_identifier (parser);
24137 if (name == error_mark_node)
24139 /* It's hard to recover because even if valid @interface stuff
24140 is to follow, we can't compile it (or validate it) if we
24141 don't even know which class it refers to. Let's assume this
24142 was a stray '@interface' token in the stream and skip it.
24146 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24147 &is_class_extension);
24148 protos = cp_parser_objc_protocol_refs_opt (parser);
24150 /* We have either a class or a category on our hands. */
24151 if (categ || is_class_extension)
24152 objc_start_category_interface (name, categ, protos, attributes);
24155 objc_start_class_interface (name, super, protos, attributes);
24156 /* Handle instance variable declarations, if any. */
24157 cp_parser_objc_class_ivars (parser);
24158 objc_continue_interface ();
24161 cp_parser_objc_method_prototype_list (parser);
24164 /* Parse an Objective-C class implementation. */
24167 cp_parser_objc_class_implementation (cp_parser* parser)
24169 tree name, super, categ;
24170 bool is_class_extension;
24172 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24173 name = cp_parser_identifier (parser);
24174 if (name == error_mark_node)
24176 /* It's hard to recover because even if valid @implementation
24177 stuff is to follow, we can't compile it (or validate it) if
24178 we don't even know which class it refers to. Let's assume
24179 this was a stray '@implementation' token in the stream and
24184 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24185 &is_class_extension);
24187 /* We have either a class or a category on our hands. */
24189 objc_start_category_implementation (name, categ);
24192 objc_start_class_implementation (name, super);
24193 /* Handle instance variable declarations, if any. */
24194 cp_parser_objc_class_ivars (parser);
24195 objc_continue_implementation ();
24198 cp_parser_objc_method_definition_list (parser);
24201 /* Consume the @end token and finish off the implementation. */
24204 cp_parser_objc_end_implementation (cp_parser* parser)
24206 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24207 objc_finish_implementation ();
24210 /* Parse an Objective-C declaration. */
24213 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24215 /* Try to figure out what kind of declaration is present. */
24216 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24219 switch (kwd->keyword)
24224 error_at (kwd->location, "attributes may not be specified before"
24225 " the %<@%D%> Objective-C++ keyword",
24229 case RID_AT_IMPLEMENTATION:
24230 warning_at (kwd->location, OPT_Wattributes,
24231 "prefix attributes are ignored before %<@%D%>",
24238 switch (kwd->keyword)
24241 cp_parser_objc_alias_declaration (parser);
24244 cp_parser_objc_class_declaration (parser);
24246 case RID_AT_PROTOCOL:
24247 cp_parser_objc_protocol_declaration (parser, attributes);
24249 case RID_AT_INTERFACE:
24250 cp_parser_objc_class_interface (parser, attributes);
24252 case RID_AT_IMPLEMENTATION:
24253 cp_parser_objc_class_implementation (parser);
24256 cp_parser_objc_end_implementation (parser);
24259 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24261 cp_parser_skip_to_end_of_block_or_statement (parser);
24265 /* Parse an Objective-C try-catch-finally statement.
24267 objc-try-catch-finally-stmt:
24268 @try compound-statement objc-catch-clause-seq [opt]
24269 objc-finally-clause [opt]
24271 objc-catch-clause-seq:
24272 objc-catch-clause objc-catch-clause-seq [opt]
24275 @catch ( objc-exception-declaration ) compound-statement
24277 objc-finally-clause:
24278 @finally compound-statement
24280 objc-exception-declaration:
24281 parameter-declaration
24284 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24288 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24289 for C. Keep them in sync. */
24292 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24294 location_t location;
24297 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24298 location = cp_lexer_peek_token (parser->lexer)->location;
24299 objc_maybe_warn_exceptions (location);
24300 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24301 node, lest it get absorbed into the surrounding block. */
24302 stmt = push_stmt_list ();
24303 cp_parser_compound_statement (parser, NULL, false, false);
24304 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24306 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24308 cp_parameter_declarator *parm;
24309 tree parameter_declaration = error_mark_node;
24310 bool seen_open_paren = false;
24312 cp_lexer_consume_token (parser->lexer);
24313 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24314 seen_open_paren = true;
24315 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24317 /* We have "@catch (...)" (where the '...' are literally
24318 what is in the code). Skip the '...'.
24319 parameter_declaration is set to NULL_TREE, and
24320 objc_being_catch_clauses() knows that that means
24322 cp_lexer_consume_token (parser->lexer);
24323 parameter_declaration = NULL_TREE;
24327 /* We have "@catch (NSException *exception)" or something
24328 like that. Parse the parameter declaration. */
24329 parm = cp_parser_parameter_declaration (parser, false, NULL);
24331 parameter_declaration = error_mark_node;
24333 parameter_declaration = grokdeclarator (parm->declarator,
24334 &parm->decl_specifiers,
24335 PARM, /*initialized=*/0,
24336 /*attrlist=*/NULL);
24338 if (seen_open_paren)
24339 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24342 /* If there was no open parenthesis, we are recovering from
24343 an error, and we are trying to figure out what mistake
24344 the user has made. */
24346 /* If there is an immediate closing parenthesis, the user
24347 probably forgot the opening one (ie, they typed "@catch
24348 NSException *e)". Parse the closing parenthesis and keep
24350 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24351 cp_lexer_consume_token (parser->lexer);
24353 /* If these is no immediate closing parenthesis, the user
24354 probably doesn't know that parenthesis are required at
24355 all (ie, they typed "@catch NSException *e"). So, just
24356 forget about the closing parenthesis and keep going. */
24358 objc_begin_catch_clause (parameter_declaration);
24359 cp_parser_compound_statement (parser, NULL, false, false);
24360 objc_finish_catch_clause ();
24362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24364 cp_lexer_consume_token (parser->lexer);
24365 location = cp_lexer_peek_token (parser->lexer)->location;
24366 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24367 node, lest it get absorbed into the surrounding block. */
24368 stmt = push_stmt_list ();
24369 cp_parser_compound_statement (parser, NULL, false, false);
24370 objc_build_finally_clause (location, pop_stmt_list (stmt));
24373 return objc_finish_try_stmt ();
24376 /* Parse an Objective-C synchronized statement.
24378 objc-synchronized-stmt:
24379 @synchronized ( expression ) compound-statement
24381 Returns NULL_TREE. */
24384 cp_parser_objc_synchronized_statement (cp_parser *parser)
24386 location_t location;
24389 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24391 location = cp_lexer_peek_token (parser->lexer)->location;
24392 objc_maybe_warn_exceptions (location);
24393 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24394 lock = cp_parser_expression (parser, false, NULL);
24395 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24397 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24398 node, lest it get absorbed into the surrounding block. */
24399 stmt = push_stmt_list ();
24400 cp_parser_compound_statement (parser, NULL, false, false);
24402 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24405 /* Parse an Objective-C throw statement.
24408 @throw assignment-expression [opt] ;
24410 Returns a constructed '@throw' statement. */
24413 cp_parser_objc_throw_statement (cp_parser *parser)
24415 tree expr = NULL_TREE;
24416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24418 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24421 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24423 cp_parser_consume_semicolon_at_end_of_statement (parser);
24425 return objc_build_throw_stmt (loc, expr);
24428 /* Parse an Objective-C statement. */
24431 cp_parser_objc_statement (cp_parser * parser)
24433 /* Try to figure out what kind of declaration is present. */
24434 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24436 switch (kwd->keyword)
24439 return cp_parser_objc_try_catch_finally_statement (parser);
24440 case RID_AT_SYNCHRONIZED:
24441 return cp_parser_objc_synchronized_statement (parser);
24443 return cp_parser_objc_throw_statement (parser);
24445 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24447 cp_parser_skip_to_end_of_block_or_statement (parser);
24450 return error_mark_node;
24453 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24454 look ahead to see if an objc keyword follows the attributes. This
24455 is to detect the use of prefix attributes on ObjC @interface and
24459 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24461 cp_lexer_save_tokens (parser->lexer);
24462 *attrib = cp_parser_attributes_opt (parser);
24463 gcc_assert (*attrib);
24464 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24466 cp_lexer_commit_tokens (parser->lexer);
24469 cp_lexer_rollback_tokens (parser->lexer);
24473 /* This routine is a minimal replacement for
24474 c_parser_struct_declaration () used when parsing the list of
24475 types/names or ObjC++ properties. For example, when parsing the
24478 @property (readonly) int a, b, c;
24480 this function is responsible for parsing "int a, int b, int c" and
24481 returning the declarations as CHAIN of DECLs.
24483 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24484 similar parsing. */
24486 cp_parser_objc_struct_declaration (cp_parser *parser)
24488 tree decls = NULL_TREE;
24489 cp_decl_specifier_seq declspecs;
24490 int decl_class_or_enum_p;
24491 tree prefix_attributes;
24493 cp_parser_decl_specifier_seq (parser,
24494 CP_PARSER_FLAGS_NONE,
24496 &decl_class_or_enum_p);
24498 if (declspecs.type == error_mark_node)
24499 return error_mark_node;
24501 /* auto, register, static, extern, mutable. */
24502 if (declspecs.storage_class != sc_none)
24504 cp_parser_error (parser, "invalid type for property");
24505 declspecs.storage_class = sc_none;
24509 if (declspecs.specs[(int) ds_thread])
24511 cp_parser_error (parser, "invalid type for property");
24512 declspecs.specs[(int) ds_thread] = 0;
24516 if (declspecs.specs[(int) ds_typedef])
24518 cp_parser_error (parser, "invalid type for property");
24519 declspecs.specs[(int) ds_typedef] = 0;
24522 prefix_attributes = declspecs.attributes;
24523 declspecs.attributes = NULL_TREE;
24525 /* Keep going until we hit the `;' at the end of the declaration. */
24526 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24528 tree attributes, first_attribute, decl;
24529 cp_declarator *declarator;
24532 /* Parse the declarator. */
24533 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24534 NULL, NULL, false);
24536 /* Look for attributes that apply to the ivar. */
24537 attributes = cp_parser_attributes_opt (parser);
24538 /* Remember which attributes are prefix attributes and
24540 first_attribute = attributes;
24541 /* Combine the attributes. */
24542 attributes = chainon (prefix_attributes, attributes);
24544 decl = grokfield (declarator, &declspecs,
24545 NULL_TREE, /*init_const_expr_p=*/false,
24546 NULL_TREE, attributes);
24548 if (decl == error_mark_node || decl == NULL_TREE)
24549 return error_mark_node;
24551 /* Reset PREFIX_ATTRIBUTES. */
24552 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24553 attributes = TREE_CHAIN (attributes);
24555 TREE_CHAIN (attributes) = NULL_TREE;
24557 DECL_CHAIN (decl) = decls;
24560 token = cp_lexer_peek_token (parser->lexer);
24561 if (token->type == CPP_COMMA)
24563 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24572 /* Parse an Objective-C @property declaration. The syntax is:
24574 objc-property-declaration:
24575 '@property' objc-property-attributes[opt] struct-declaration ;
24577 objc-property-attributes:
24578 '(' objc-property-attribute-list ')'
24580 objc-property-attribute-list:
24581 objc-property-attribute
24582 objc-property-attribute-list, objc-property-attribute
24584 objc-property-attribute
24585 'getter' = identifier
24586 'setter' = identifier
24595 @property NSString *name;
24596 @property (readonly) id object;
24597 @property (retain, nonatomic, getter=getTheName) id name;
24598 @property int a, b, c;
24600 PS: This function is identical to
24601 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24603 cp_parser_objc_at_property_declaration (cp_parser *parser)
24605 /* The following variables hold the attributes of the properties as
24606 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24607 seen. When we see an attribute, we set them to 'true' (if they
24608 are boolean properties) or to the identifier (if they have an
24609 argument, ie, for getter and setter). Note that here we only
24610 parse the list of attributes, check the syntax and accumulate the
24611 attributes that we find. objc_add_property_declaration() will
24612 then process the information. */
24613 bool property_assign = false;
24614 bool property_copy = false;
24615 tree property_getter_ident = NULL_TREE;
24616 bool property_nonatomic = false;
24617 bool property_readonly = false;
24618 bool property_readwrite = false;
24619 bool property_retain = false;
24620 tree property_setter_ident = NULL_TREE;
24622 /* 'properties' is the list of properties that we read. Usually a
24623 single one, but maybe more (eg, in "@property int a, b, c;" there
24628 loc = cp_lexer_peek_token (parser->lexer)->location;
24630 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24632 /* Parse the optional attribute list... */
24633 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24636 cp_lexer_consume_token (parser->lexer);
24640 bool syntax_error = false;
24641 cp_token *token = cp_lexer_peek_token (parser->lexer);
24644 if (token->type != CPP_NAME)
24646 cp_parser_error (parser, "expected identifier");
24649 keyword = C_RID_CODE (token->u.value);
24650 cp_lexer_consume_token (parser->lexer);
24653 case RID_ASSIGN: property_assign = true; break;
24654 case RID_COPY: property_copy = true; break;
24655 case RID_NONATOMIC: property_nonatomic = true; break;
24656 case RID_READONLY: property_readonly = true; break;
24657 case RID_READWRITE: property_readwrite = true; break;
24658 case RID_RETAIN: property_retain = true; break;
24662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24664 if (keyword == RID_GETTER)
24665 cp_parser_error (parser,
24666 "missing %<=%> (after %<getter%> attribute)");
24668 cp_parser_error (parser,
24669 "missing %<=%> (after %<setter%> attribute)");
24670 syntax_error = true;
24673 cp_lexer_consume_token (parser->lexer); /* eat the = */
24674 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24676 cp_parser_error (parser, "expected identifier");
24677 syntax_error = true;
24680 if (keyword == RID_SETTER)
24682 if (property_setter_ident != NULL_TREE)
24684 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24685 cp_lexer_consume_token (parser->lexer);
24688 property_setter_ident = cp_parser_objc_selector (parser);
24689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24690 cp_parser_error (parser, "setter name must terminate with %<:%>");
24692 cp_lexer_consume_token (parser->lexer);
24696 if (property_getter_ident != NULL_TREE)
24698 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24699 cp_lexer_consume_token (parser->lexer);
24702 property_getter_ident = cp_parser_objc_selector (parser);
24706 cp_parser_error (parser, "unknown property attribute");
24707 syntax_error = true;
24714 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24715 cp_lexer_consume_token (parser->lexer);
24720 /* FIXME: "@property (setter, assign);" will generate a spurious
24721 "error: expected ‘)’ before ‘,’ token". This is because
24722 cp_parser_require, unlike the C counterpart, will produce an
24723 error even if we are in error recovery. */
24724 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24726 cp_parser_skip_to_closing_parenthesis (parser,
24727 /*recovering=*/true,
24728 /*or_comma=*/false,
24729 /*consume_paren=*/true);
24733 /* ... and the property declaration(s). */
24734 properties = cp_parser_objc_struct_declaration (parser);
24736 if (properties == error_mark_node)
24738 cp_parser_skip_to_end_of_statement (parser);
24739 /* If the next token is now a `;', consume it. */
24740 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24741 cp_lexer_consume_token (parser->lexer);
24745 if (properties == NULL_TREE)
24746 cp_parser_error (parser, "expected identifier");
24749 /* Comma-separated properties are chained together in
24750 reverse order; add them one by one. */
24751 properties = nreverse (properties);
24753 for (; properties; properties = TREE_CHAIN (properties))
24754 objc_add_property_declaration (loc, copy_node (properties),
24755 property_readonly, property_readwrite,
24756 property_assign, property_retain,
24757 property_copy, property_nonatomic,
24758 property_getter_ident, property_setter_ident);
24761 cp_parser_consume_semicolon_at_end_of_statement (parser);
24764 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24766 objc-synthesize-declaration:
24767 @synthesize objc-synthesize-identifier-list ;
24769 objc-synthesize-identifier-list:
24770 objc-synthesize-identifier
24771 objc-synthesize-identifier-list, objc-synthesize-identifier
24773 objc-synthesize-identifier
24775 identifier = identifier
24778 @synthesize MyProperty;
24779 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24781 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24782 for C. Keep them in sync.
24785 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24787 tree list = NULL_TREE;
24789 loc = cp_lexer_peek_token (parser->lexer)->location;
24791 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24794 tree property, ivar;
24795 property = cp_parser_identifier (parser);
24796 if (property == error_mark_node)
24798 cp_parser_consume_semicolon_at_end_of_statement (parser);
24801 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24803 cp_lexer_consume_token (parser->lexer);
24804 ivar = cp_parser_identifier (parser);
24805 if (ivar == error_mark_node)
24807 cp_parser_consume_semicolon_at_end_of_statement (parser);
24813 list = chainon (list, build_tree_list (ivar, property));
24814 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24815 cp_lexer_consume_token (parser->lexer);
24819 cp_parser_consume_semicolon_at_end_of_statement (parser);
24820 objc_add_synthesize_declaration (loc, list);
24823 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24825 objc-dynamic-declaration:
24826 @dynamic identifier-list ;
24829 @dynamic MyProperty;
24830 @dynamic MyProperty, AnotherProperty;
24832 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24833 for C. Keep them in sync.
24836 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24838 tree list = NULL_TREE;
24840 loc = cp_lexer_peek_token (parser->lexer)->location;
24842 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24846 property = cp_parser_identifier (parser);
24847 if (property == error_mark_node)
24849 cp_parser_consume_semicolon_at_end_of_statement (parser);
24852 list = chainon (list, build_tree_list (NULL, property));
24853 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24854 cp_lexer_consume_token (parser->lexer);
24858 cp_parser_consume_semicolon_at_end_of_statement (parser);
24859 objc_add_dynamic_declaration (loc, list);
24863 /* OpenMP 2.5 parsing routines. */
24865 /* Returns name of the next clause.
24866 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24867 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24868 returned and the token is consumed. */
24870 static pragma_omp_clause
24871 cp_parser_omp_clause_name (cp_parser *parser)
24873 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24876 result = PRAGMA_OMP_CLAUSE_IF;
24877 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24878 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24879 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24880 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24881 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24883 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24884 const char *p = IDENTIFIER_POINTER (id);
24889 if (!strcmp ("collapse", p))
24890 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24891 else if (!strcmp ("copyin", p))
24892 result = PRAGMA_OMP_CLAUSE_COPYIN;
24893 else if (!strcmp ("copyprivate", p))
24894 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24897 if (!strcmp ("final", p))
24898 result = PRAGMA_OMP_CLAUSE_FINAL;
24899 else if (!strcmp ("firstprivate", p))
24900 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24903 if (!strcmp ("lastprivate", p))
24904 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24907 if (!strcmp ("mergeable", p))
24908 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24911 if (!strcmp ("nowait", p))
24912 result = PRAGMA_OMP_CLAUSE_NOWAIT;
24913 else if (!strcmp ("num_threads", p))
24914 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24917 if (!strcmp ("ordered", p))
24918 result = PRAGMA_OMP_CLAUSE_ORDERED;
24921 if (!strcmp ("reduction", p))
24922 result = PRAGMA_OMP_CLAUSE_REDUCTION;
24925 if (!strcmp ("schedule", p))
24926 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24927 else if (!strcmp ("shared", p))
24928 result = PRAGMA_OMP_CLAUSE_SHARED;
24931 if (!strcmp ("untied", p))
24932 result = PRAGMA_OMP_CLAUSE_UNTIED;
24937 if (result != PRAGMA_OMP_CLAUSE_NONE)
24938 cp_lexer_consume_token (parser->lexer);
24943 /* Validate that a clause of the given type does not already exist. */
24946 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24947 const char *name, location_t location)
24951 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24952 if (OMP_CLAUSE_CODE (c) == code)
24954 error_at (location, "too many %qs clauses", name);
24962 variable-list , identifier
24964 In addition, we match a closing parenthesis. An opening parenthesis
24965 will have been consumed by the caller.
24967 If KIND is nonzero, create the appropriate node and install the decl
24968 in OMP_CLAUSE_DECL and add the node to the head of the list.
24970 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24971 return the list created. */
24974 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24982 token = cp_lexer_peek_token (parser->lexer);
24983 name = cp_parser_id_expression (parser, /*template_p=*/false,
24984 /*check_dependency_p=*/true,
24985 /*template_p=*/NULL,
24986 /*declarator_p=*/false,
24987 /*optional_p=*/false);
24988 if (name == error_mark_node)
24991 decl = cp_parser_lookup_name_simple (parser, name, token->location);
24992 if (decl == error_mark_node)
24993 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24995 else if (kind != 0)
24997 tree u = build_omp_clause (token->location, kind);
24998 OMP_CLAUSE_DECL (u) = decl;
24999 OMP_CLAUSE_CHAIN (u) = list;
25003 list = tree_cons (decl, NULL_TREE, list);
25006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25008 cp_lexer_consume_token (parser->lexer);
25011 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25015 /* Try to resync to an unnested comma. Copied from
25016 cp_parser_parenthesized_expression_list. */
25018 ending = cp_parser_skip_to_closing_parenthesis (parser,
25019 /*recovering=*/true,
25021 /*consume_paren=*/true);
25029 /* Similarly, but expect leading and trailing parenthesis. This is a very
25030 common case for omp clauses. */
25033 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25035 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25036 return cp_parser_omp_var_list_no_open (parser, kind, list);
25041 collapse ( constant-expression ) */
25044 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25050 loc = cp_lexer_peek_token (parser->lexer)->location;
25051 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25054 num = cp_parser_constant_expression (parser, false, NULL);
25056 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25057 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25058 /*or_comma=*/false,
25059 /*consume_paren=*/true);
25061 if (num == error_mark_node)
25063 num = fold_non_dependent_expr (num);
25064 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25065 || !host_integerp (num, 0)
25066 || (n = tree_low_cst (num, 0)) <= 0
25069 error_at (loc, "collapse argument needs positive constant integer expression");
25073 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25074 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25075 OMP_CLAUSE_CHAIN (c) = list;
25076 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25082 default ( shared | none ) */
25085 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25087 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25090 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25092 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25094 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25095 const char *p = IDENTIFIER_POINTER (id);
25100 if (strcmp ("none", p) != 0)
25102 kind = OMP_CLAUSE_DEFAULT_NONE;
25106 if (strcmp ("shared", p) != 0)
25108 kind = OMP_CLAUSE_DEFAULT_SHARED;
25115 cp_lexer_consume_token (parser->lexer);
25120 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25123 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25124 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25125 /*or_comma=*/false,
25126 /*consume_paren=*/true);
25128 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25131 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25132 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25133 OMP_CLAUSE_CHAIN (c) = list;
25134 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25140 final ( expression ) */
25143 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25147 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25150 t = cp_parser_condition (parser);
25152 if (t == error_mark_node
25153 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25154 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25155 /*or_comma=*/false,
25156 /*consume_paren=*/true);
25158 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25160 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25161 OMP_CLAUSE_FINAL_EXPR (c) = t;
25162 OMP_CLAUSE_CHAIN (c) = list;
25168 if ( expression ) */
25171 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25175 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25178 t = cp_parser_condition (parser);
25180 if (t == error_mark_node
25181 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25182 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25183 /*or_comma=*/false,
25184 /*consume_paren=*/true);
25186 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25188 c = build_omp_clause (location, OMP_CLAUSE_IF);
25189 OMP_CLAUSE_IF_EXPR (c) = t;
25190 OMP_CLAUSE_CHAIN (c) = list;
25199 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25200 tree list, location_t location)
25204 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25207 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25208 OMP_CLAUSE_CHAIN (c) = list;
25216 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25217 tree list, location_t location)
25221 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25223 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25224 OMP_CLAUSE_CHAIN (c) = list;
25229 num_threads ( expression ) */
25232 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25233 location_t location)
25237 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25240 t = cp_parser_expression (parser, false, NULL);
25242 if (t == error_mark_node
25243 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25244 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25245 /*or_comma=*/false,
25246 /*consume_paren=*/true);
25248 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25249 "num_threads", location);
25251 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25252 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25253 OMP_CLAUSE_CHAIN (c) = list;
25262 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25263 tree list, location_t location)
25267 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25268 "ordered", location);
25270 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25271 OMP_CLAUSE_CHAIN (c) = list;
25276 reduction ( reduction-operator : variable-list )
25278 reduction-operator:
25279 One of: + * - & ^ | && ||
25283 reduction-operator:
25284 One of: + * - & ^ | && || min max */
25287 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25289 enum tree_code code;
25292 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25295 switch (cp_lexer_peek_token (parser->lexer)->type)
25307 code = BIT_AND_EXPR;
25310 code = BIT_XOR_EXPR;
25313 code = BIT_IOR_EXPR;
25316 code = TRUTH_ANDIF_EXPR;
25319 code = TRUTH_ORIF_EXPR;
25323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25324 const char *p = IDENTIFIER_POINTER (id);
25326 if (strcmp (p, "min") == 0)
25331 if (strcmp (p, "max") == 0)
25339 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25340 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25342 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25343 /*or_comma=*/false,
25344 /*consume_paren=*/true);
25347 cp_lexer_consume_token (parser->lexer);
25349 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25352 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25353 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25354 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25360 schedule ( schedule-kind )
25361 schedule ( schedule-kind , expression )
25364 static | dynamic | guided | runtime | auto */
25367 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25371 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25374 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25378 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25379 const char *p = IDENTIFIER_POINTER (id);
25384 if (strcmp ("dynamic", p) != 0)
25386 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25390 if (strcmp ("guided", p) != 0)
25392 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25396 if (strcmp ("runtime", p) != 0)
25398 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25405 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25406 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25407 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25408 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25411 cp_lexer_consume_token (parser->lexer);
25413 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25416 cp_lexer_consume_token (parser->lexer);
25418 token = cp_lexer_peek_token (parser->lexer);
25419 t = cp_parser_assignment_expression (parser, false, NULL);
25421 if (t == error_mark_node)
25423 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25424 error_at (token->location, "schedule %<runtime%> does not take "
25425 "a %<chunk_size%> parameter");
25426 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25427 error_at (token->location, "schedule %<auto%> does not take "
25428 "a %<chunk_size%> parameter");
25430 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25432 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25435 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25438 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25439 OMP_CLAUSE_CHAIN (c) = list;
25443 cp_parser_error (parser, "invalid schedule kind");
25445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25446 /*or_comma=*/false,
25447 /*consume_paren=*/true);
25455 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25456 tree list, location_t location)
25460 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25462 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25463 OMP_CLAUSE_CHAIN (c) = list;
25467 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25468 is a bitmask in MASK. Return the list of clauses found; the result
25469 of clause default goes in *pdefault. */
25472 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25473 const char *where, cp_token *pragma_tok)
25475 tree clauses = NULL;
25477 cp_token *token = NULL;
25479 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25481 pragma_omp_clause c_kind;
25482 const char *c_name;
25483 tree prev = clauses;
25485 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25486 cp_lexer_consume_token (parser->lexer);
25488 token = cp_lexer_peek_token (parser->lexer);
25489 c_kind = cp_parser_omp_clause_name (parser);
25494 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25495 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25497 c_name = "collapse";
25499 case PRAGMA_OMP_CLAUSE_COPYIN:
25500 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25503 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25504 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25506 c_name = "copyprivate";
25508 case PRAGMA_OMP_CLAUSE_DEFAULT:
25509 clauses = cp_parser_omp_clause_default (parser, clauses,
25511 c_name = "default";
25513 case PRAGMA_OMP_CLAUSE_FINAL:
25514 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25517 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25518 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25520 c_name = "firstprivate";
25522 case PRAGMA_OMP_CLAUSE_IF:
25523 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25526 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25527 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25529 c_name = "lastprivate";
25531 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25532 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25534 c_name = "mergeable";
25536 case PRAGMA_OMP_CLAUSE_NOWAIT:
25537 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25540 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25541 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25543 c_name = "num_threads";
25545 case PRAGMA_OMP_CLAUSE_ORDERED:
25546 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25548 c_name = "ordered";
25550 case PRAGMA_OMP_CLAUSE_PRIVATE:
25551 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25553 c_name = "private";
25555 case PRAGMA_OMP_CLAUSE_REDUCTION:
25556 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25557 c_name = "reduction";
25559 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25560 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25562 c_name = "schedule";
25564 case PRAGMA_OMP_CLAUSE_SHARED:
25565 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25569 case PRAGMA_OMP_CLAUSE_UNTIED:
25570 clauses = cp_parser_omp_clause_untied (parser, clauses,
25575 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25579 if (((mask >> c_kind) & 1) == 0)
25581 /* Remove the invalid clause(s) from the list to avoid
25582 confusing the rest of the compiler. */
25584 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25589 return finish_omp_clauses (clauses);
25596 In practice, we're also interested in adding the statement to an
25597 outer node. So it is convenient if we work around the fact that
25598 cp_parser_statement calls add_stmt. */
25601 cp_parser_begin_omp_structured_block (cp_parser *parser)
25603 unsigned save = parser->in_statement;
25605 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25606 This preserves the "not within loop or switch" style error messages
25607 for nonsense cases like
25613 if (parser->in_statement)
25614 parser->in_statement = IN_OMP_BLOCK;
25620 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25622 parser->in_statement = save;
25626 cp_parser_omp_structured_block (cp_parser *parser)
25628 tree stmt = begin_omp_structured_block ();
25629 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25631 cp_parser_statement (parser, NULL_TREE, false, NULL);
25633 cp_parser_end_omp_structured_block (parser, save);
25634 return finish_omp_structured_block (stmt);
25638 # pragma omp atomic new-line
25642 x binop= expr | x++ | ++x | x-- | --x
25644 +, *, -, /, &, ^, |, <<, >>
25646 where x is an lvalue expression with scalar type.
25649 # pragma omp atomic new-line
25652 # pragma omp atomic read new-line
25655 # pragma omp atomic write new-line
25658 # pragma omp atomic update new-line
25661 # pragma omp atomic capture new-line
25664 # pragma omp atomic capture new-line
25672 expression-stmt | x = x binop expr
25674 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25676 { v = x; update-stmt; } | { update-stmt; v = x; }
25678 where x and v are lvalue expressions with scalar type. */
25681 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25683 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25684 tree rhs1 = NULL_TREE, orig_lhs;
25685 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25686 bool structured_block = false;
25688 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25690 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25691 const char *p = IDENTIFIER_POINTER (id);
25693 if (!strcmp (p, "read"))
25694 code = OMP_ATOMIC_READ;
25695 else if (!strcmp (p, "write"))
25697 else if (!strcmp (p, "update"))
25699 else if (!strcmp (p, "capture"))
25700 code = OMP_ATOMIC_CAPTURE_NEW;
25704 cp_lexer_consume_token (parser->lexer);
25706 cp_parser_require_pragma_eol (parser, pragma_tok);
25710 case OMP_ATOMIC_READ:
25711 case NOP_EXPR: /* atomic write */
25712 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25713 /*cast_p=*/false, NULL);
25714 if (v == error_mark_node)
25716 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25718 if (code == NOP_EXPR)
25719 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25721 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25722 /*cast_p=*/false, NULL);
25723 if (lhs == error_mark_node)
25725 if (code == NOP_EXPR)
25727 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25735 case OMP_ATOMIC_CAPTURE_NEW:
25736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25738 cp_lexer_consume_token (parser->lexer);
25739 structured_block = true;
25743 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25744 /*cast_p=*/false, NULL);
25745 if (v == error_mark_node)
25747 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25755 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25756 /*cast_p=*/false, NULL);
25758 switch (TREE_CODE (lhs))
25763 case POSTINCREMENT_EXPR:
25764 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25765 code = OMP_ATOMIC_CAPTURE_OLD;
25767 case PREINCREMENT_EXPR:
25768 lhs = TREE_OPERAND (lhs, 0);
25769 opcode = PLUS_EXPR;
25770 rhs = integer_one_node;
25773 case POSTDECREMENT_EXPR:
25774 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25775 code = OMP_ATOMIC_CAPTURE_OLD;
25777 case PREDECREMENT_EXPR:
25778 lhs = TREE_OPERAND (lhs, 0);
25779 opcode = MINUS_EXPR;
25780 rhs = integer_one_node;
25783 case COMPOUND_EXPR:
25784 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25785 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25786 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25787 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25788 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25789 (TREE_OPERAND (lhs, 1), 0), 0)))
25791 /* Undo effects of boolean_increment for post {in,de}crement. */
25792 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25795 if (TREE_CODE (lhs) == MODIFY_EXPR
25796 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25798 /* Undo effects of boolean_increment. */
25799 if (integer_onep (TREE_OPERAND (lhs, 1)))
25801 /* This is pre or post increment. */
25802 rhs = TREE_OPERAND (lhs, 1);
25803 lhs = TREE_OPERAND (lhs, 0);
25805 if (code == OMP_ATOMIC_CAPTURE_NEW
25806 && !structured_block
25807 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25808 code = OMP_ATOMIC_CAPTURE_OLD;
25814 switch (cp_lexer_peek_token (parser->lexer)->type)
25817 opcode = MULT_EXPR;
25820 opcode = TRUNC_DIV_EXPR;
25823 opcode = PLUS_EXPR;
25826 opcode = MINUS_EXPR;
25828 case CPP_LSHIFT_EQ:
25829 opcode = LSHIFT_EXPR;
25831 case CPP_RSHIFT_EQ:
25832 opcode = RSHIFT_EXPR;
25835 opcode = BIT_AND_EXPR;
25838 opcode = BIT_IOR_EXPR;
25841 opcode = BIT_XOR_EXPR;
25844 if (structured_block || code == OMP_ATOMIC)
25846 enum cp_parser_prec oprec;
25848 cp_lexer_consume_token (parser->lexer);
25849 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25850 /*cast_p=*/false, NULL);
25851 if (rhs1 == error_mark_node)
25853 token = cp_lexer_peek_token (parser->lexer);
25854 switch (token->type)
25856 case CPP_SEMICOLON:
25857 if (code == OMP_ATOMIC_CAPTURE_NEW)
25859 code = OMP_ATOMIC_CAPTURE_OLD;
25864 cp_lexer_consume_token (parser->lexer);
25867 cp_parser_error (parser,
25868 "invalid form of %<#pragma omp atomic%>");
25871 opcode = MULT_EXPR;
25874 opcode = TRUNC_DIV_EXPR;
25877 opcode = PLUS_EXPR;
25880 opcode = MINUS_EXPR;
25883 opcode = LSHIFT_EXPR;
25886 opcode = RSHIFT_EXPR;
25889 opcode = BIT_AND_EXPR;
25892 opcode = BIT_IOR_EXPR;
25895 opcode = BIT_XOR_EXPR;
25898 cp_parser_error (parser,
25899 "invalid operator for %<#pragma omp atomic%>");
25902 oprec = TOKEN_PRECEDENCE (token);
25903 gcc_assert (oprec != PREC_NOT_OPERATOR);
25904 if (commutative_tree_code (opcode))
25905 oprec = (enum cp_parser_prec) (oprec - 1);
25906 cp_lexer_consume_token (parser->lexer);
25907 rhs = cp_parser_binary_expression (parser, false, false,
25909 if (rhs == error_mark_node)
25915 cp_parser_error (parser,
25916 "invalid operator for %<#pragma omp atomic%>");
25919 cp_lexer_consume_token (parser->lexer);
25921 rhs = cp_parser_expression (parser, false, NULL);
25922 if (rhs == error_mark_node)
25927 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25929 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25931 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25932 /*cast_p=*/false, NULL);
25933 if (v == error_mark_node)
25935 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25937 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25938 /*cast_p=*/false, NULL);
25939 if (lhs1 == error_mark_node)
25942 if (structured_block)
25944 cp_parser_consume_semicolon_at_end_of_statement (parser);
25945 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25948 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25949 if (!structured_block)
25950 cp_parser_consume_semicolon_at_end_of_statement (parser);
25954 cp_parser_skip_to_end_of_block_or_statement (parser);
25955 if (structured_block)
25957 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25958 cp_lexer_consume_token (parser->lexer);
25959 else if (code == OMP_ATOMIC_CAPTURE_NEW)
25961 cp_parser_skip_to_end_of_block_or_statement (parser);
25962 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25963 cp_lexer_consume_token (parser->lexer);
25970 # pragma omp barrier new-line */
25973 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25975 cp_parser_require_pragma_eol (parser, pragma_tok);
25976 finish_omp_barrier ();
25980 # pragma omp critical [(name)] new-line
25981 structured-block */
25984 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25986 tree stmt, name = NULL;
25988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25990 cp_lexer_consume_token (parser->lexer);
25992 name = cp_parser_identifier (parser);
25994 if (name == error_mark_node
25995 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25996 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25997 /*or_comma=*/false,
25998 /*consume_paren=*/true);
25999 if (name == error_mark_node)
26002 cp_parser_require_pragma_eol (parser, pragma_tok);
26004 stmt = cp_parser_omp_structured_block (parser);
26005 return c_finish_omp_critical (input_location, stmt, name);
26009 # pragma omp flush flush-vars[opt] new-line
26012 ( variable-list ) */
26015 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26017 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26018 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26019 cp_parser_require_pragma_eol (parser, pragma_tok);
26021 finish_omp_flush ();
26024 /* Helper function, to parse omp for increment expression. */
26027 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26029 tree cond = cp_parser_binary_expression (parser, false, true,
26030 PREC_NOT_OPERATOR, NULL);
26031 if (cond == error_mark_node
26032 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26034 cp_parser_skip_to_end_of_statement (parser);
26035 return error_mark_node;
26038 switch (TREE_CODE (cond))
26046 return error_mark_node;
26049 /* If decl is an iterator, preserve LHS and RHS of the relational
26050 expr until finish_omp_for. */
26052 && (type_dependent_expression_p (decl)
26053 || CLASS_TYPE_P (TREE_TYPE (decl))))
26056 return build_x_binary_op (TREE_CODE (cond),
26057 TREE_OPERAND (cond, 0), ERROR_MARK,
26058 TREE_OPERAND (cond, 1), ERROR_MARK,
26059 /*overload=*/NULL, tf_warning_or_error);
26062 /* Helper function, to parse omp for increment expression. */
26065 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26067 cp_token *token = cp_lexer_peek_token (parser->lexer);
26073 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26075 op = (token->type == CPP_PLUS_PLUS
26076 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26077 cp_lexer_consume_token (parser->lexer);
26078 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26080 return error_mark_node;
26081 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26084 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26086 return error_mark_node;
26088 token = cp_lexer_peek_token (parser->lexer);
26089 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26091 op = (token->type == CPP_PLUS_PLUS
26092 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26093 cp_lexer_consume_token (parser->lexer);
26094 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26097 op = cp_parser_assignment_operator_opt (parser);
26098 if (op == ERROR_MARK)
26099 return error_mark_node;
26101 if (op != NOP_EXPR)
26103 rhs = cp_parser_assignment_expression (parser, false, NULL);
26104 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26105 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26108 lhs = cp_parser_binary_expression (parser, false, false,
26109 PREC_ADDITIVE_EXPRESSION, NULL);
26110 token = cp_lexer_peek_token (parser->lexer);
26111 decl_first = lhs == decl;
26114 if (token->type != CPP_PLUS
26115 && token->type != CPP_MINUS)
26116 return error_mark_node;
26120 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26121 cp_lexer_consume_token (parser->lexer);
26122 rhs = cp_parser_binary_expression (parser, false, false,
26123 PREC_ADDITIVE_EXPRESSION, NULL);
26124 token = cp_lexer_peek_token (parser->lexer);
26125 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26127 if (lhs == NULL_TREE)
26129 if (op == PLUS_EXPR)
26132 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
26135 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
26136 NULL, tf_warning_or_error);
26139 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26143 if (rhs != decl || op == MINUS_EXPR)
26144 return error_mark_node;
26145 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26148 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26150 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26153 /* Parse the restricted form of the for statement allowed by OpenMP. */
26156 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26158 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26159 tree real_decl, initv, condv, incrv, declv;
26160 tree this_pre_body, cl;
26161 location_t loc_first;
26162 bool collapse_err = false;
26163 int i, collapse = 1, nbraces = 0;
26164 VEC(tree,gc) *for_block = make_tree_vector ();
26166 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26167 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26168 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26170 gcc_assert (collapse >= 1);
26172 declv = make_tree_vec (collapse);
26173 initv = make_tree_vec (collapse);
26174 condv = make_tree_vec (collapse);
26175 incrv = make_tree_vec (collapse);
26177 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26179 for (i = 0; i < collapse; i++)
26181 int bracecount = 0;
26182 bool add_private_clause = false;
26185 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26187 cp_parser_error (parser, "for statement expected");
26190 loc = cp_lexer_consume_token (parser->lexer)->location;
26192 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26195 init = decl = real_decl = NULL;
26196 this_pre_body = push_stmt_list ();
26197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26199 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26203 integer-type var = lb
26204 random-access-iterator-type var = lb
26205 pointer-type var = lb
26207 cp_decl_specifier_seq type_specifiers;
26209 /* First, try to parse as an initialized declaration. See
26210 cp_parser_condition, from whence the bulk of this is copied. */
26212 cp_parser_parse_tentatively (parser);
26213 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26214 /*is_trailing_return=*/false,
26216 if (cp_parser_parse_definitely (parser))
26218 /* If parsing a type specifier seq succeeded, then this
26219 MUST be a initialized declaration. */
26220 tree asm_specification, attributes;
26221 cp_declarator *declarator;
26223 declarator = cp_parser_declarator (parser,
26224 CP_PARSER_DECLARATOR_NAMED,
26225 /*ctor_dtor_or_conv_p=*/NULL,
26226 /*parenthesized_p=*/NULL,
26227 /*member_p=*/false);
26228 attributes = cp_parser_attributes_opt (parser);
26229 asm_specification = cp_parser_asm_specification_opt (parser);
26231 if (declarator == cp_error_declarator)
26232 cp_parser_skip_to_end_of_statement (parser);
26236 tree pushed_scope, auto_node;
26238 decl = start_decl (declarator, &type_specifiers,
26239 SD_INITIALIZED, attributes,
26240 /*prefix_attributes=*/NULL_TREE,
26243 auto_node = type_uses_auto (TREE_TYPE (decl));
26244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26246 if (cp_lexer_next_token_is (parser->lexer,
26248 error ("parenthesized initialization is not allowed in "
26249 "OpenMP %<for%> loop");
26251 /* Trigger an error. */
26252 cp_parser_require (parser, CPP_EQ, RT_EQ);
26254 init = error_mark_node;
26255 cp_parser_skip_to_end_of_statement (parser);
26257 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26258 || type_dependent_expression_p (decl)
26261 bool is_direct_init, is_non_constant_init;
26263 init = cp_parser_initializer (parser,
26265 &is_non_constant_init);
26270 = do_auto_deduction (TREE_TYPE (decl), init,
26273 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26274 && !type_dependent_expression_p (decl))
26278 cp_finish_decl (decl, init, !is_non_constant_init,
26280 LOOKUP_ONLYCONVERTING);
26281 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26283 VEC_safe_push (tree, gc, for_block, this_pre_body);
26287 init = pop_stmt_list (this_pre_body);
26288 this_pre_body = NULL_TREE;
26293 cp_lexer_consume_token (parser->lexer);
26294 init = cp_parser_assignment_expression (parser, false, NULL);
26297 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26298 init = error_mark_node;
26300 cp_finish_decl (decl, NULL_TREE,
26301 /*init_const_expr_p=*/false,
26303 LOOKUP_ONLYCONVERTING);
26307 pop_scope (pushed_scope);
26313 /* If parsing a type specifier sequence failed, then
26314 this MUST be a simple expression. */
26315 cp_parser_parse_tentatively (parser);
26316 decl = cp_parser_primary_expression (parser, false, false,
26318 if (!cp_parser_error_occurred (parser)
26321 && CLASS_TYPE_P (TREE_TYPE (decl)))
26325 cp_parser_parse_definitely (parser);
26326 cp_parser_require (parser, CPP_EQ, RT_EQ);
26327 rhs = cp_parser_assignment_expression (parser, false, NULL);
26328 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26330 tf_warning_or_error));
26331 add_private_clause = true;
26336 cp_parser_abort_tentative_parse (parser);
26337 init = cp_parser_expression (parser, false, NULL);
26340 if (TREE_CODE (init) == MODIFY_EXPR
26341 || TREE_CODE (init) == MODOP_EXPR)
26342 real_decl = TREE_OPERAND (init, 0);
26347 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26350 this_pre_body = pop_stmt_list (this_pre_body);
26354 pre_body = push_stmt_list ();
26356 add_stmt (this_pre_body);
26357 pre_body = pop_stmt_list (pre_body);
26360 pre_body = this_pre_body;
26365 if (par_clauses != NULL && real_decl != NULL_TREE)
26368 for (c = par_clauses; *c ; )
26369 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26370 && OMP_CLAUSE_DECL (*c) == real_decl)
26372 error_at (loc, "iteration variable %qD"
26373 " should not be firstprivate", real_decl);
26374 *c = OMP_CLAUSE_CHAIN (*c);
26376 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26377 && OMP_CLAUSE_DECL (*c) == real_decl)
26379 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26380 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26381 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26382 OMP_CLAUSE_DECL (l) = real_decl;
26383 OMP_CLAUSE_CHAIN (l) = clauses;
26384 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26386 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26387 CP_OMP_CLAUSE_INFO (*c) = NULL;
26388 add_private_clause = false;
26392 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26393 && OMP_CLAUSE_DECL (*c) == real_decl)
26394 add_private_clause = false;
26395 c = &OMP_CLAUSE_CHAIN (*c);
26399 if (add_private_clause)
26402 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26404 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26405 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26406 && OMP_CLAUSE_DECL (c) == decl)
26408 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26409 && OMP_CLAUSE_DECL (c) == decl)
26410 error_at (loc, "iteration variable %qD "
26411 "should not be firstprivate",
26413 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26414 && OMP_CLAUSE_DECL (c) == decl)
26415 error_at (loc, "iteration variable %qD should not be reduction",
26420 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26421 OMP_CLAUSE_DECL (c) = decl;
26422 c = finish_omp_clauses (c);
26425 OMP_CLAUSE_CHAIN (c) = clauses;
26432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26433 cond = cp_parser_omp_for_cond (parser, decl);
26434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26439 /* If decl is an iterator, preserve the operator on decl
26440 until finish_omp_for. */
26442 && ((processing_template_decl
26443 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26444 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26445 incr = cp_parser_omp_for_incr (parser, real_decl);
26447 incr = cp_parser_expression (parser, false, NULL);
26450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26451 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26452 /*or_comma=*/false,
26453 /*consume_paren=*/true);
26455 TREE_VEC_ELT (declv, i) = decl;
26456 TREE_VEC_ELT (initv, i) = init;
26457 TREE_VEC_ELT (condv, i) = cond;
26458 TREE_VEC_ELT (incrv, i) = incr;
26460 if (i == collapse - 1)
26463 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26464 in between the collapsed for loops to be still considered perfectly
26465 nested. Hopefully the final version clarifies this.
26466 For now handle (multiple) {'s and empty statements. */
26467 cp_parser_parse_tentatively (parser);
26470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26472 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26474 cp_lexer_consume_token (parser->lexer);
26477 else if (bracecount
26478 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26479 cp_lexer_consume_token (parser->lexer);
26482 loc = cp_lexer_peek_token (parser->lexer)->location;
26483 error_at (loc, "not enough collapsed for loops");
26484 collapse_err = true;
26485 cp_parser_abort_tentative_parse (parser);
26494 cp_parser_parse_definitely (parser);
26495 nbraces += bracecount;
26499 /* Note that we saved the original contents of this flag when we entered
26500 the structured block, and so we don't need to re-save it here. */
26501 parser->in_statement = IN_OMP_FOR;
26503 /* Note that the grammar doesn't call for a structured block here,
26504 though the loop as a whole is a structured block. */
26505 body = push_stmt_list ();
26506 cp_parser_statement (parser, NULL_TREE, false, NULL);
26507 body = pop_stmt_list (body);
26509 if (declv == NULL_TREE)
26512 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26513 pre_body, clauses);
26517 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26519 cp_lexer_consume_token (parser->lexer);
26522 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26523 cp_lexer_consume_token (parser->lexer);
26528 error_at (cp_lexer_peek_token (parser->lexer)->location,
26529 "collapsed loops not perfectly nested");
26531 collapse_err = true;
26532 cp_parser_statement_seq_opt (parser, NULL);
26533 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26538 while (!VEC_empty (tree, for_block))
26539 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26540 release_tree_vector (for_block);
26546 #pragma omp for for-clause[optseq] new-line
26549 #define OMP_FOR_CLAUSE_MASK \
26550 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26551 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26552 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26553 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26554 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26555 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26556 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26557 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26560 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26562 tree clauses, sb, ret;
26565 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26566 "#pragma omp for", pragma_tok);
26568 sb = begin_omp_structured_block ();
26569 save = cp_parser_begin_omp_structured_block (parser);
26571 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26573 cp_parser_end_omp_structured_block (parser, save);
26574 add_stmt (finish_omp_structured_block (sb));
26580 # pragma omp master new-line
26581 structured-block */
26584 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26586 cp_parser_require_pragma_eol (parser, pragma_tok);
26587 return c_finish_omp_master (input_location,
26588 cp_parser_omp_structured_block (parser));
26592 # pragma omp ordered new-line
26593 structured-block */
26596 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26598 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26599 cp_parser_require_pragma_eol (parser, pragma_tok);
26600 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26606 { section-sequence }
26609 section-directive[opt] structured-block
26610 section-sequence section-directive structured-block */
26613 cp_parser_omp_sections_scope (cp_parser *parser)
26615 tree stmt, substmt;
26616 bool error_suppress = false;
26619 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26622 stmt = push_stmt_list ();
26624 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26628 substmt = begin_omp_structured_block ();
26629 save = cp_parser_begin_omp_structured_block (parser);
26633 cp_parser_statement (parser, NULL_TREE, false, NULL);
26635 tok = cp_lexer_peek_token (parser->lexer);
26636 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26638 if (tok->type == CPP_CLOSE_BRACE)
26640 if (tok->type == CPP_EOF)
26644 cp_parser_end_omp_structured_block (parser, save);
26645 substmt = finish_omp_structured_block (substmt);
26646 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26647 add_stmt (substmt);
26652 tok = cp_lexer_peek_token (parser->lexer);
26653 if (tok->type == CPP_CLOSE_BRACE)
26655 if (tok->type == CPP_EOF)
26658 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26660 cp_lexer_consume_token (parser->lexer);
26661 cp_parser_require_pragma_eol (parser, tok);
26662 error_suppress = false;
26664 else if (!error_suppress)
26666 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26667 error_suppress = true;
26670 substmt = cp_parser_omp_structured_block (parser);
26671 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26672 add_stmt (substmt);
26674 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26676 substmt = pop_stmt_list (stmt);
26678 stmt = make_node (OMP_SECTIONS);
26679 TREE_TYPE (stmt) = void_type_node;
26680 OMP_SECTIONS_BODY (stmt) = substmt;
26687 # pragma omp sections sections-clause[optseq] newline
26690 #define OMP_SECTIONS_CLAUSE_MASK \
26691 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26692 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26693 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26694 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26695 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26698 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26702 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26703 "#pragma omp sections", pragma_tok);
26705 ret = cp_parser_omp_sections_scope (parser);
26707 OMP_SECTIONS_CLAUSES (ret) = clauses;
26713 # pragma parallel parallel-clause new-line
26714 # pragma parallel for parallel-for-clause new-line
26715 # pragma parallel sections parallel-sections-clause new-line */
26717 #define OMP_PARALLEL_CLAUSE_MASK \
26718 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26719 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26720 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26721 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26722 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26723 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26724 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26725 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26728 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26730 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26731 const char *p_name = "#pragma omp parallel";
26732 tree stmt, clauses, par_clause, ws_clause, block;
26733 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26735 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26739 cp_lexer_consume_token (parser->lexer);
26740 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26741 p_name = "#pragma omp parallel for";
26742 mask |= OMP_FOR_CLAUSE_MASK;
26743 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26745 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26747 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26748 const char *p = IDENTIFIER_POINTER (id);
26749 if (strcmp (p, "sections") == 0)
26751 cp_lexer_consume_token (parser->lexer);
26752 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26753 p_name = "#pragma omp parallel sections";
26754 mask |= OMP_SECTIONS_CLAUSE_MASK;
26755 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26759 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26760 block = begin_omp_parallel ();
26761 save = cp_parser_begin_omp_structured_block (parser);
26765 case PRAGMA_OMP_PARALLEL:
26766 cp_parser_statement (parser, NULL_TREE, false, NULL);
26767 par_clause = clauses;
26770 case PRAGMA_OMP_PARALLEL_FOR:
26771 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26772 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26775 case PRAGMA_OMP_PARALLEL_SECTIONS:
26776 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26777 stmt = cp_parser_omp_sections_scope (parser);
26779 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26783 gcc_unreachable ();
26786 cp_parser_end_omp_structured_block (parser, save);
26787 stmt = finish_omp_parallel (par_clause, block);
26788 if (p_kind != PRAGMA_OMP_PARALLEL)
26789 OMP_PARALLEL_COMBINED (stmt) = 1;
26794 # pragma omp single single-clause[optseq] new-line
26795 structured-block */
26797 #define OMP_SINGLE_CLAUSE_MASK \
26798 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26799 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26800 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26801 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26804 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26806 tree stmt = make_node (OMP_SINGLE);
26807 TREE_TYPE (stmt) = void_type_node;
26809 OMP_SINGLE_CLAUSES (stmt)
26810 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26811 "#pragma omp single", pragma_tok);
26812 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26814 return add_stmt (stmt);
26818 # pragma omp task task-clause[optseq] new-line
26819 structured-block */
26821 #define OMP_TASK_CLAUSE_MASK \
26822 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26823 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26824 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26825 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26826 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26827 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26828 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26829 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26832 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26834 tree clauses, block;
26837 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26838 "#pragma omp task", pragma_tok);
26839 block = begin_omp_task ();
26840 save = cp_parser_begin_omp_structured_block (parser);
26841 cp_parser_statement (parser, NULL_TREE, false, NULL);
26842 cp_parser_end_omp_structured_block (parser, save);
26843 return finish_omp_task (clauses, block);
26847 # pragma omp taskwait new-line */
26850 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26852 cp_parser_require_pragma_eol (parser, pragma_tok);
26853 finish_omp_taskwait ();
26857 # pragma omp taskyield new-line */
26860 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26862 cp_parser_require_pragma_eol (parser, pragma_tok);
26863 finish_omp_taskyield ();
26867 # pragma omp threadprivate (variable-list) */
26870 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26874 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26875 cp_parser_require_pragma_eol (parser, pragma_tok);
26877 finish_omp_threadprivate (vars);
26880 /* Main entry point to OpenMP statement pragmas. */
26883 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26887 switch (pragma_tok->pragma_kind)
26889 case PRAGMA_OMP_ATOMIC:
26890 cp_parser_omp_atomic (parser, pragma_tok);
26892 case PRAGMA_OMP_CRITICAL:
26893 stmt = cp_parser_omp_critical (parser, pragma_tok);
26895 case PRAGMA_OMP_FOR:
26896 stmt = cp_parser_omp_for (parser, pragma_tok);
26898 case PRAGMA_OMP_MASTER:
26899 stmt = cp_parser_omp_master (parser, pragma_tok);
26901 case PRAGMA_OMP_ORDERED:
26902 stmt = cp_parser_omp_ordered (parser, pragma_tok);
26904 case PRAGMA_OMP_PARALLEL:
26905 stmt = cp_parser_omp_parallel (parser, pragma_tok);
26907 case PRAGMA_OMP_SECTIONS:
26908 stmt = cp_parser_omp_sections (parser, pragma_tok);
26910 case PRAGMA_OMP_SINGLE:
26911 stmt = cp_parser_omp_single (parser, pragma_tok);
26913 case PRAGMA_OMP_TASK:
26914 stmt = cp_parser_omp_task (parser, pragma_tok);
26917 gcc_unreachable ();
26921 SET_EXPR_LOCATION (stmt, pragma_tok->location);
26924 /* Transactional Memory parsing routines. */
26926 /* Parse a transaction attribute.
26932 ??? Simplify this when C++0x bracket attributes are
26933 implemented properly. */
26936 cp_parser_txn_attribute_opt (cp_parser *parser)
26939 tree attr_name, attr = NULL;
26941 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26942 return cp_parser_attributes_opt (parser);
26944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26946 cp_lexer_consume_token (parser->lexer);
26947 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26950 token = cp_lexer_peek_token (parser->lexer);
26951 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26953 token = cp_lexer_consume_token (parser->lexer);
26955 attr_name = (token->type == CPP_KEYWORD
26956 /* For keywords, use the canonical spelling,
26957 not the parsed identifier. */
26958 ? ridpointers[(int) token->keyword]
26960 attr = build_tree_list (attr_name, NULL_TREE);
26963 cp_parser_error (parser, "expected identifier");
26965 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26967 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26971 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26973 transaction-statement:
26974 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
26976 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
26980 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26982 unsigned char old_in = parser->in_transaction;
26983 unsigned char this_in = 1, new_in;
26985 tree stmt, attrs, noex;
26987 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26988 || keyword == RID_TRANSACTION_RELAXED);
26989 token = cp_parser_require_keyword (parser, keyword,
26990 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26991 : RT_TRANSACTION_RELAXED));
26992 gcc_assert (token != NULL);
26994 if (keyword == RID_TRANSACTION_RELAXED)
26995 this_in |= TM_STMT_ATTR_RELAXED;
26998 attrs = cp_parser_txn_attribute_opt (parser);
27000 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27003 /* Parse a noexcept specification. */
27004 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27006 /* Keep track if we're in the lexical scope of an outer transaction. */
27007 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27009 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27011 parser->in_transaction = new_in;
27012 cp_parser_compound_statement (parser, NULL, false, false);
27013 parser->in_transaction = old_in;
27015 finish_transaction_stmt (stmt, NULL, this_in, noex);
27020 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27022 transaction-expression:
27023 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27024 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27028 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27030 unsigned char old_in = parser->in_transaction;
27031 unsigned char this_in = 1;
27036 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27037 || keyword == RID_TRANSACTION_RELAXED);
27040 error (keyword == RID_TRANSACTION_RELAXED
27041 ? G_("%<__transaction_relaxed%> without transactional memory "
27043 : G_("%<__transaction_atomic%> without transactional memory "
27044 "support enabled"));
27046 token = cp_parser_require_keyword (parser, keyword,
27047 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27048 : RT_TRANSACTION_RELAXED));
27049 gcc_assert (token != NULL);
27051 if (keyword == RID_TRANSACTION_RELAXED)
27052 this_in |= TM_STMT_ATTR_RELAXED;
27054 /* Set this early. This might mean that we allow transaction_cancel in
27055 an expression that we find out later actually has to be a constexpr.
27056 However, we expect that cxx_constant_value will be able to deal with
27057 this; also, if the noexcept has no constexpr, then what we parse next
27058 really is a transaction's body. */
27059 parser->in_transaction = this_in;
27061 /* Parse a noexcept specification. */
27062 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27065 if (!noex || !noex_expr
27066 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27068 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27070 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27071 finish_parenthesized_expr (expr);
27073 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27077 /* The only expression that is available got parsed for the noexcept
27078 already. noexcept is true then. */
27080 noex = boolean_true_node;
27083 expr = build_transaction_expr (token->location, expr, this_in, noex);
27084 parser->in_transaction = old_in;
27086 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27087 return error_mark_node;
27089 return (flag_tm ? expr : error_mark_node);
27092 /* Parse a function-transaction-block.
27094 function-transaction-block:
27095 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27097 __transaction_atomic txn-attribute[opt] function-try-block
27098 __transaction_relaxed ctor-initializer[opt] function-body
27099 __transaction_relaxed function-try-block
27103 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27105 unsigned char old_in = parser->in_transaction;
27106 unsigned char new_in = 1;
27107 tree compound_stmt, stmt, attrs;
27108 bool ctor_initializer_p;
27111 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27112 || keyword == RID_TRANSACTION_RELAXED);
27113 token = cp_parser_require_keyword (parser, keyword,
27114 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27115 : RT_TRANSACTION_RELAXED));
27116 gcc_assert (token != NULL);
27118 if (keyword == RID_TRANSACTION_RELAXED)
27119 new_in |= TM_STMT_ATTR_RELAXED;
27122 attrs = cp_parser_txn_attribute_opt (parser);
27124 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27127 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27129 parser->in_transaction = new_in;
27131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27132 ctor_initializer_p = cp_parser_function_try_block (parser);
27135 = cp_parser_ctor_initializer_opt_and_function_body (parser);
27137 parser->in_transaction = old_in;
27139 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27141 return ctor_initializer_p;
27144 /* Parse a __transaction_cancel statement.
27147 __transaction_cancel txn-attribute[opt] ;
27148 __transaction_cancel txn-attribute[opt] throw-expression ;
27150 ??? Cancel and throw is not yet implemented. */
27153 cp_parser_transaction_cancel (cp_parser *parser)
27156 bool is_outer = false;
27159 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27160 RT_TRANSACTION_CANCEL);
27161 gcc_assert (token != NULL);
27163 attrs = cp_parser_txn_attribute_opt (parser);
27165 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27167 /* ??? Parse cancel-and-throw here. */
27169 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27173 error_at (token->location, "%<__transaction_cancel%> without "
27174 "transactional memory support enabled");
27175 return error_mark_node;
27177 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27179 error_at (token->location, "%<__transaction_cancel%> within a "
27180 "%<__transaction_relaxed%>");
27181 return error_mark_node;
27185 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27186 && !is_tm_may_cancel_outer (current_function_decl))
27188 error_at (token->location, "outer %<__transaction_cancel%> not "
27189 "within outer %<__transaction_atomic%>");
27190 error_at (token->location,
27191 " or a %<transaction_may_cancel_outer%> function");
27192 return error_mark_node;
27195 else if (parser->in_transaction == 0)
27197 error_at (token->location, "%<__transaction_cancel%> not within "
27198 "%<__transaction_atomic%>");
27199 return error_mark_node;
27202 stmt = build_tm_abort_call (token->location, is_outer);
27211 static GTY (()) cp_parser *the_parser;
27214 /* Special handling for the first token or line in the file. The first
27215 thing in the file might be #pragma GCC pch_preprocess, which loads a
27216 PCH file, which is a GC collection point. So we need to handle this
27217 first pragma without benefit of an existing lexer structure.
27219 Always returns one token to the caller in *FIRST_TOKEN. This is
27220 either the true first token of the file, or the first token after
27221 the initial pragma. */
27224 cp_parser_initial_pragma (cp_token *first_token)
27228 cp_lexer_get_preprocessor_token (NULL, first_token);
27229 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27232 cp_lexer_get_preprocessor_token (NULL, first_token);
27233 if (first_token->type == CPP_STRING)
27235 name = first_token->u.value;
27237 cp_lexer_get_preprocessor_token (NULL, first_token);
27238 if (first_token->type != CPP_PRAGMA_EOL)
27239 error_at (first_token->location,
27240 "junk at end of %<#pragma GCC pch_preprocess%>");
27243 error_at (first_token->location, "expected string literal");
27245 /* Skip to the end of the pragma. */
27246 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27247 cp_lexer_get_preprocessor_token (NULL, first_token);
27249 /* Now actually load the PCH file. */
27251 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27253 /* Read one more token to return to our caller. We have to do this
27254 after reading the PCH file in, since its pointers have to be
27256 cp_lexer_get_preprocessor_token (NULL, first_token);
27259 /* Normal parsing of a pragma token. Here we can (and must) use the
27263 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27265 cp_token *pragma_tok;
27268 pragma_tok = cp_lexer_consume_token (parser->lexer);
27269 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27270 parser->lexer->in_pragma = true;
27272 id = pragma_tok->pragma_kind;
27275 case PRAGMA_GCC_PCH_PREPROCESS:
27276 error_at (pragma_tok->location,
27277 "%<#pragma GCC pch_preprocess%> must be first");
27280 case PRAGMA_OMP_BARRIER:
27283 case pragma_compound:
27284 cp_parser_omp_barrier (parser, pragma_tok);
27287 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27288 "used in compound statements");
27295 case PRAGMA_OMP_FLUSH:
27298 case pragma_compound:
27299 cp_parser_omp_flush (parser, pragma_tok);
27302 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27303 "used in compound statements");
27310 case PRAGMA_OMP_TASKWAIT:
27313 case pragma_compound:
27314 cp_parser_omp_taskwait (parser, pragma_tok);
27317 error_at (pragma_tok->location,
27318 "%<#pragma omp taskwait%> may only be "
27319 "used in compound statements");
27326 case PRAGMA_OMP_TASKYIELD:
27329 case pragma_compound:
27330 cp_parser_omp_taskyield (parser, pragma_tok);
27333 error_at (pragma_tok->location,
27334 "%<#pragma omp taskyield%> may only be "
27335 "used in compound statements");
27342 case PRAGMA_OMP_THREADPRIVATE:
27343 cp_parser_omp_threadprivate (parser, pragma_tok);
27346 case PRAGMA_OMP_ATOMIC:
27347 case PRAGMA_OMP_CRITICAL:
27348 case PRAGMA_OMP_FOR:
27349 case PRAGMA_OMP_MASTER:
27350 case PRAGMA_OMP_ORDERED:
27351 case PRAGMA_OMP_PARALLEL:
27352 case PRAGMA_OMP_SECTIONS:
27353 case PRAGMA_OMP_SINGLE:
27354 case PRAGMA_OMP_TASK:
27355 if (context == pragma_external)
27357 cp_parser_omp_construct (parser, pragma_tok);
27360 case PRAGMA_OMP_SECTION:
27361 error_at (pragma_tok->location,
27362 "%<#pragma omp section%> may only be used in "
27363 "%<#pragma omp sections%> construct");
27367 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27368 c_invoke_pragma_handler (id);
27372 cp_parser_error (parser, "expected declaration specifiers");
27376 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27380 /* The interface the pragma parsers have to the lexer. */
27383 pragma_lex (tree *value)
27386 enum cpp_ttype ret;
27388 tok = cp_lexer_peek_token (the_parser->lexer);
27391 *value = tok->u.value;
27393 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27395 else if (ret == CPP_STRING)
27396 *value = cp_parser_string_literal (the_parser, false, false);
27399 cp_lexer_consume_token (the_parser->lexer);
27400 if (ret == CPP_KEYWORD)
27408 /* External interface. */
27410 /* Parse one entire translation unit. */
27413 c_parse_file (void)
27415 static bool already_called = false;
27417 if (already_called)
27419 sorry ("inter-module optimizations not implemented for C++");
27422 already_called = true;
27424 the_parser = cp_parser_new ();
27425 push_deferring_access_checks (flag_access_control
27426 ? dk_no_deferred : dk_no_check);
27427 cp_parser_translation_unit (the_parser);
27431 #include "gt-cp-parser.h"