2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011 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 void cp_parser_parse_tentatively
2254 static void cp_parser_commit_to_tentative_parse
2256 static void cp_parser_abort_tentative_parse
2258 static bool cp_parser_parse_definitely
2260 static inline bool cp_parser_parsing_tentatively
2262 static bool cp_parser_uncommitted_to_tentative_parse_p
2264 static void cp_parser_error
2265 (cp_parser *, const char *);
2266 static void cp_parser_name_lookup_error
2267 (cp_parser *, tree, tree, name_lookup_error, location_t);
2268 static bool cp_parser_simulate_error
2270 static bool cp_parser_check_type_definition
2272 static void cp_parser_check_for_definition_in_return_type
2273 (cp_declarator *, tree, location_t type_location);
2274 static void cp_parser_check_for_invalid_template_id
2275 (cp_parser *, tree, location_t location);
2276 static bool cp_parser_non_integral_constant_expression
2277 (cp_parser *, non_integral_constant);
2278 static void cp_parser_diagnose_invalid_type_name
2279 (cp_parser *, tree, tree, location_t);
2280 static bool cp_parser_parse_and_diagnose_invalid_type_name
2282 static int cp_parser_skip_to_closing_parenthesis
2283 (cp_parser *, bool, bool, bool);
2284 static void cp_parser_skip_to_end_of_statement
2286 static void cp_parser_consume_semicolon_at_end_of_statement
2288 static void cp_parser_skip_to_end_of_block_or_statement
2290 static bool cp_parser_skip_to_closing_brace
2292 static void cp_parser_skip_to_end_of_template_parameter_list
2294 static void cp_parser_skip_to_pragma_eol
2295 (cp_parser*, cp_token *);
2296 static bool cp_parser_error_occurred
2298 static bool cp_parser_allow_gnu_extensions_p
2300 static bool cp_parser_is_pure_string_literal
2302 static bool cp_parser_is_string_literal
2304 static bool cp_parser_is_keyword
2305 (cp_token *, enum rid);
2306 static tree cp_parser_make_typename_type
2307 (cp_parser *, tree, tree, location_t location);
2308 static cp_declarator * cp_parser_make_indirect_declarator
2309 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2311 /* Returns nonzero if we are parsing tentatively. */
2314 cp_parser_parsing_tentatively (cp_parser* parser)
2316 return parser->context->next != NULL;
2319 /* Returns nonzero if TOKEN is a string literal. */
2322 cp_parser_is_pure_string_literal (cp_token* token)
2324 return (token->type == CPP_STRING ||
2325 token->type == CPP_STRING16 ||
2326 token->type == CPP_STRING32 ||
2327 token->type == CPP_WSTRING ||
2328 token->type == CPP_UTF8STRING);
2331 /* Returns nonzero if TOKEN is a string literal
2332 of a user-defined string literal. */
2335 cp_parser_is_string_literal (cp_token* token)
2337 return (cp_parser_is_pure_string_literal (token) ||
2338 token->type == CPP_STRING_USERDEF ||
2339 token->type == CPP_STRING16_USERDEF ||
2340 token->type == CPP_STRING32_USERDEF ||
2341 token->type == CPP_WSTRING_USERDEF ||
2342 token->type == CPP_UTF8STRING_USERDEF);
2345 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2348 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2350 return token->keyword == keyword;
2353 /* If not parsing tentatively, issue a diagnostic of the form
2354 FILE:LINE: MESSAGE before TOKEN
2355 where TOKEN is the next token in the input stream. MESSAGE
2356 (specified by the caller) is usually of the form "expected
2360 cp_parser_error (cp_parser* parser, const char* gmsgid)
2362 if (!cp_parser_simulate_error (parser))
2364 cp_token *token = cp_lexer_peek_token (parser->lexer);
2365 /* This diagnostic makes more sense if it is tagged to the line
2366 of the token we just peeked at. */
2367 cp_lexer_set_source_position_from_token (token);
2369 if (token->type == CPP_PRAGMA)
2371 error_at (token->location,
2372 "%<#pragma%> is not allowed here");
2373 cp_parser_skip_to_pragma_eol (parser, token);
2377 c_parse_error (gmsgid,
2378 /* Because c_parser_error does not understand
2379 CPP_KEYWORD, keywords are treated like
2381 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2382 token->u.value, token->flags);
2386 /* Issue an error about name-lookup failing. NAME is the
2387 IDENTIFIER_NODE DECL is the result of
2388 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2389 the thing that we hoped to find. */
2392 cp_parser_name_lookup_error (cp_parser* parser,
2395 name_lookup_error desired,
2396 location_t location)
2398 /* If name lookup completely failed, tell the user that NAME was not
2400 if (decl == error_mark_node)
2402 if (parser->scope && parser->scope != global_namespace)
2403 error_at (location, "%<%E::%E%> has not been declared",
2404 parser->scope, name);
2405 else if (parser->scope == global_namespace)
2406 error_at (location, "%<::%E%> has not been declared", name);
2407 else if (parser->object_scope
2408 && !CLASS_TYPE_P (parser->object_scope))
2409 error_at (location, "request for member %qE in non-class type %qT",
2410 name, parser->object_scope);
2411 else if (parser->object_scope)
2412 error_at (location, "%<%T::%E%> has not been declared",
2413 parser->object_scope, name);
2415 error_at (location, "%qE has not been declared", name);
2417 else if (parser->scope && parser->scope != global_namespace)
2422 error_at (location, "%<%E::%E%> is not a type",
2423 parser->scope, name);
2426 error_at (location, "%<%E::%E%> is not a class or namespace",
2427 parser->scope, name);
2431 "%<%E::%E%> is not a class, namespace, or enumeration",
2432 parser->scope, name);
2439 else if (parser->scope == global_namespace)
2444 error_at (location, "%<::%E%> is not a type", name);
2447 error_at (location, "%<::%E%> is not a class or namespace", name);
2451 "%<::%E%> is not a class, namespace, or enumeration",
2463 error_at (location, "%qE is not a type", name);
2466 error_at (location, "%qE is not a class or namespace", name);
2470 "%qE is not a class, namespace, or enumeration", name);
2478 /* If we are parsing tentatively, remember that an error has occurred
2479 during this tentative parse. Returns true if the error was
2480 simulated; false if a message should be issued by the caller. */
2483 cp_parser_simulate_error (cp_parser* parser)
2485 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2487 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2493 /* Check for repeated decl-specifiers. */
2496 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2497 location_t location)
2501 for (ds = ds_first; ds != ds_last; ++ds)
2503 unsigned count = decl_specs->specs[ds];
2506 /* The "long" specifier is a special case because of "long long". */
2510 error_at (location, "%<long long long%> is too long for GCC");
2512 pedwarn_cxx98 (location, OPT_Wlong_long,
2513 "ISO C++ 1998 does not support %<long long%>");
2517 static const char *const decl_spec_names[] = {
2535 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2540 /* This function is called when a type is defined. If type
2541 definitions are forbidden at this point, an error message is
2545 cp_parser_check_type_definition (cp_parser* parser)
2547 /* If types are forbidden here, issue a message. */
2548 if (parser->type_definition_forbidden_message)
2550 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2551 in the message need to be interpreted. */
2552 error (parser->type_definition_forbidden_message);
2558 /* This function is called when the DECLARATOR is processed. The TYPE
2559 was a type defined in the decl-specifiers. If it is invalid to
2560 define a type in the decl-specifiers for DECLARATOR, an error is
2561 issued. TYPE_LOCATION is the location of TYPE and is used
2562 for error reporting. */
2565 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2566 tree type, location_t type_location)
2568 /* [dcl.fct] forbids type definitions in return types.
2569 Unfortunately, it's not easy to know whether or not we are
2570 processing a return type until after the fact. */
2572 && (declarator->kind == cdk_pointer
2573 || declarator->kind == cdk_reference
2574 || declarator->kind == cdk_ptrmem))
2575 declarator = declarator->declarator;
2577 && declarator->kind == cdk_function)
2579 error_at (type_location,
2580 "new types may not be defined in a return type");
2581 inform (type_location,
2582 "(perhaps a semicolon is missing after the definition of %qT)",
2587 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2588 "<" in any valid C++ program. If the next token is indeed "<",
2589 issue a message warning the user about what appears to be an
2590 invalid attempt to form a template-id. LOCATION is the location
2591 of the type-specifier (TYPE) */
2594 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2595 tree type, location_t location)
2597 cp_token_position start = 0;
2599 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2602 error_at (location, "%qT is not a template", type);
2603 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2604 error_at (location, "%qE is not a template", type);
2606 error_at (location, "invalid template-id");
2607 /* Remember the location of the invalid "<". */
2608 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2609 start = cp_lexer_token_position (parser->lexer, true);
2610 /* Consume the "<". */
2611 cp_lexer_consume_token (parser->lexer);
2612 /* Parse the template arguments. */
2613 cp_parser_enclosed_template_argument_list (parser);
2614 /* Permanently remove the invalid template arguments so that
2615 this error message is not issued again. */
2617 cp_lexer_purge_tokens_after (parser->lexer, start);
2621 /* If parsing an integral constant-expression, issue an error message
2622 about the fact that THING appeared and return true. Otherwise,
2623 return false. In either case, set
2624 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2627 cp_parser_non_integral_constant_expression (cp_parser *parser,
2628 non_integral_constant thing)
2630 parser->non_integral_constant_expression_p = true;
2631 if (parser->integral_constant_expression_p)
2633 if (!parser->allow_non_integral_constant_expression_p)
2635 const char *msg = NULL;
2639 error ("floating-point literal "
2640 "cannot appear in a constant-expression");
2643 error ("a cast to a type other than an integral or "
2644 "enumeration type cannot appear in a "
2645 "constant-expression");
2648 error ("%<typeid%> operator "
2649 "cannot appear in a constant-expression");
2652 error ("non-constant compound literals "
2653 "cannot appear in a constant-expression");
2656 error ("a function call "
2657 "cannot appear in a constant-expression");
2660 error ("an increment "
2661 "cannot appear in a constant-expression");
2664 error ("an decrement "
2665 "cannot appear in a constant-expression");
2668 error ("an array reference "
2669 "cannot appear in a constant-expression");
2671 case NIC_ADDR_LABEL:
2672 error ("the address of a label "
2673 "cannot appear in a constant-expression");
2675 case NIC_OVERLOADED:
2676 error ("calls to overloaded operators "
2677 "cannot appear in a constant-expression");
2679 case NIC_ASSIGNMENT:
2680 error ("an assignment cannot appear in a constant-expression");
2683 error ("a comma operator "
2684 "cannot appear in a constant-expression");
2686 case NIC_CONSTRUCTOR:
2687 error ("a call to a constructor "
2688 "cannot appear in a constant-expression");
2690 case NIC_TRANSACTION:
2691 error ("a transaction expression "
2692 "cannot appear in a constant-expression");
2698 msg = "__FUNCTION__";
2700 case NIC_PRETTY_FUNC:
2701 msg = "__PRETTY_FUNCTION__";
2721 case NIC_PREINCREMENT:
2724 case NIC_PREDECREMENT:
2737 error ("%qs cannot appear in a constant-expression", msg);
2744 /* Emit a diagnostic for an invalid type name. SCOPE is the
2745 qualifying scope (or NULL, if none) for ID. This function commits
2746 to the current active tentative parse, if any. (Otherwise, the
2747 problematic construct might be encountered again later, resulting
2748 in duplicate error messages.) LOCATION is the location of ID. */
2751 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2752 tree scope, tree id,
2753 location_t location)
2755 tree decl, old_scope;
2756 cp_parser_commit_to_tentative_parse (parser);
2757 /* Try to lookup the identifier. */
2758 old_scope = parser->scope;
2759 parser->scope = scope;
2760 decl = cp_parser_lookup_name_simple (parser, id, location);
2761 parser->scope = old_scope;
2762 /* If the lookup found a template-name, it means that the user forgot
2763 to specify an argument list. Emit a useful error message. */
2764 if (TREE_CODE (decl) == TEMPLATE_DECL)
2766 "invalid use of template-name %qE without an argument list",
2768 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2769 error_at (location, "invalid use of destructor %qD as a type", id);
2770 else if (TREE_CODE (decl) == TYPE_DECL)
2771 /* Something like 'unsigned A a;' */
2772 error_at (location, "invalid combination of multiple type-specifiers");
2773 else if (!parser->scope)
2775 /* Issue an error message. */
2776 error_at (location, "%qE does not name a type", id);
2777 /* If we're in a template class, it's possible that the user was
2778 referring to a type from a base class. For example:
2780 template <typename T> struct A { typedef T X; };
2781 template <typename T> struct B : public A<T> { X x; };
2783 The user should have said "typename A<T>::X". */
2784 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2785 inform (location, "C++11 %<constexpr%> only available with "
2786 "-std=c++11 or -std=gnu++11");
2787 else if (processing_template_decl && current_class_type
2788 && TYPE_BINFO (current_class_type))
2792 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2796 tree base_type = BINFO_TYPE (b);
2797 if (CLASS_TYPE_P (base_type)
2798 && dependent_type_p (base_type))
2801 /* Go from a particular instantiation of the
2802 template (which will have an empty TYPE_FIELDs),
2803 to the main version. */
2804 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2805 for (field = TYPE_FIELDS (base_type);
2807 field = DECL_CHAIN (field))
2808 if (TREE_CODE (field) == TYPE_DECL
2809 && DECL_NAME (field) == id)
2812 "(perhaps %<typename %T::%E%> was intended)",
2813 BINFO_TYPE (b), id);
2822 /* Here we diagnose qualified-ids where the scope is actually correct,
2823 but the identifier does not resolve to a valid type name. */
2824 else if (parser->scope != error_mark_node)
2826 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2827 error_at (location, "%qE in namespace %qE does not name a type",
2829 else if (CLASS_TYPE_P (parser->scope)
2830 && constructor_name_p (id, parser->scope))
2833 error_at (location, "%<%T::%E%> names the constructor, not"
2834 " the type", parser->scope, id);
2835 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2836 error_at (location, "and %qT has no template constructors",
2839 else if (TYPE_P (parser->scope)
2840 && dependent_scope_p (parser->scope))
2841 error_at (location, "need %<typename%> before %<%T::%E%> because "
2842 "%qT is a dependent scope",
2843 parser->scope, id, parser->scope);
2844 else if (TYPE_P (parser->scope))
2845 error_at (location, "%qE in %q#T does not name a type",
2852 /* Check for a common situation where a type-name should be present,
2853 but is not, and issue a sensible error message. Returns true if an
2854 invalid type-name was detected.
2856 The situation handled by this function are variable declarations of the
2857 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2858 Usually, `ID' should name a type, but if we got here it means that it
2859 does not. We try to emit the best possible error message depending on
2860 how exactly the id-expression looks like. */
2863 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2866 cp_token *token = cp_lexer_peek_token (parser->lexer);
2868 /* Avoid duplicate error about ambiguous lookup. */
2869 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2871 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2872 if (next->type == CPP_NAME && next->ambiguous_p)
2876 cp_parser_parse_tentatively (parser);
2877 id = cp_parser_id_expression (parser,
2878 /*template_keyword_p=*/false,
2879 /*check_dependency_p=*/true,
2880 /*template_p=*/NULL,
2881 /*declarator_p=*/true,
2882 /*optional_p=*/false);
2883 /* If the next token is a (, this is a function with no explicit return
2884 type, i.e. constructor, destructor or conversion op. */
2885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2886 || TREE_CODE (id) == TYPE_DECL)
2888 cp_parser_abort_tentative_parse (parser);
2891 if (!cp_parser_parse_definitely (parser))
2894 /* Emit a diagnostic for the invalid type. */
2895 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2896 id, token->location);
2898 /* If we aren't in the middle of a declarator (i.e. in a
2899 parameter-declaration-clause), skip to the end of the declaration;
2900 there's no point in trying to process it. */
2901 if (!parser->in_declarator_p)
2902 cp_parser_skip_to_end_of_block_or_statement (parser);
2906 /* Consume tokens up to, and including, the next non-nested closing `)'.
2907 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2908 are doing error recovery. Returns -1 if OR_COMMA is true and we
2909 found an unnested comma. */
2912 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2917 unsigned paren_depth = 0;
2918 unsigned brace_depth = 0;
2919 unsigned square_depth = 0;
2921 if (recovering && !or_comma
2922 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2927 cp_token * token = cp_lexer_peek_token (parser->lexer);
2929 switch (token->type)
2932 case CPP_PRAGMA_EOL:
2933 /* If we've run out of tokens, then there is no closing `)'. */
2936 /* This is good for lambda expression capture-lists. */
2937 case CPP_OPEN_SQUARE:
2940 case CPP_CLOSE_SQUARE:
2941 if (!square_depth--)
2946 /* This matches the processing in skip_to_end_of_statement. */
2951 case CPP_OPEN_BRACE:
2954 case CPP_CLOSE_BRACE:
2960 if (recovering && or_comma && !brace_depth && !paren_depth
2965 case CPP_OPEN_PAREN:
2970 case CPP_CLOSE_PAREN:
2971 if (!brace_depth && !paren_depth--)
2974 cp_lexer_consume_token (parser->lexer);
2983 /* Consume the token. */
2984 cp_lexer_consume_token (parser->lexer);
2988 /* Consume tokens until we reach the end of the current statement.
2989 Normally, that will be just before consuming a `;'. However, if a
2990 non-nested `}' comes first, then we stop before consuming that. */
2993 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2995 unsigned nesting_depth = 0;
2999 cp_token *token = cp_lexer_peek_token (parser->lexer);
3001 switch (token->type)
3004 case CPP_PRAGMA_EOL:
3005 /* If we've run out of tokens, stop. */
3009 /* If the next token is a `;', we have reached the end of the
3015 case CPP_CLOSE_BRACE:
3016 /* If this is a non-nested '}', stop before consuming it.
3017 That way, when confronted with something like:
3021 we stop before consuming the closing '}', even though we
3022 have not yet reached a `;'. */
3023 if (nesting_depth == 0)
3026 /* If it is the closing '}' for a block that we have
3027 scanned, stop -- but only after consuming the token.
3033 we will stop after the body of the erroneously declared
3034 function, but before consuming the following `typedef'
3036 if (--nesting_depth == 0)
3038 cp_lexer_consume_token (parser->lexer);
3042 case CPP_OPEN_BRACE:
3050 /* Consume the token. */
3051 cp_lexer_consume_token (parser->lexer);
3055 /* This function is called at the end of a statement or declaration.
3056 If the next token is a semicolon, it is consumed; otherwise, error
3057 recovery is attempted. */
3060 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3062 /* Look for the trailing `;'. */
3063 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3065 /* If there is additional (erroneous) input, skip to the end of
3067 cp_parser_skip_to_end_of_statement (parser);
3068 /* If the next token is now a `;', consume it. */
3069 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3070 cp_lexer_consume_token (parser->lexer);
3074 /* Skip tokens until we have consumed an entire block, or until we
3075 have consumed a non-nested `;'. */
3078 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3080 int nesting_depth = 0;
3082 while (nesting_depth >= 0)
3084 cp_token *token = cp_lexer_peek_token (parser->lexer);
3086 switch (token->type)
3089 case CPP_PRAGMA_EOL:
3090 /* If we've run out of tokens, stop. */
3094 /* Stop if this is an unnested ';'. */
3099 case CPP_CLOSE_BRACE:
3100 /* Stop if this is an unnested '}', or closes the outermost
3103 if (nesting_depth < 0)
3109 case CPP_OPEN_BRACE:
3118 /* Consume the token. */
3119 cp_lexer_consume_token (parser->lexer);
3123 /* Skip tokens until a non-nested closing curly brace is the next
3124 token, or there are no more tokens. Return true in the first case,
3128 cp_parser_skip_to_closing_brace (cp_parser *parser)
3130 unsigned nesting_depth = 0;
3134 cp_token *token = cp_lexer_peek_token (parser->lexer);
3136 switch (token->type)
3139 case CPP_PRAGMA_EOL:
3140 /* If we've run out of tokens, stop. */
3143 case CPP_CLOSE_BRACE:
3144 /* If the next token is a non-nested `}', then we have reached
3145 the end of the current block. */
3146 if (nesting_depth-- == 0)
3150 case CPP_OPEN_BRACE:
3151 /* If it the next token is a `{', then we are entering a new
3152 block. Consume the entire block. */
3160 /* Consume the token. */
3161 cp_lexer_consume_token (parser->lexer);
3165 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3166 parameter is the PRAGMA token, allowing us to purge the entire pragma
3170 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3174 parser->lexer->in_pragma = false;
3177 token = cp_lexer_consume_token (parser->lexer);
3178 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3180 /* Ensure that the pragma is not parsed again. */
3181 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3184 /* Require pragma end of line, resyncing with it as necessary. The
3185 arguments are as for cp_parser_skip_to_pragma_eol. */
3188 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3190 parser->lexer->in_pragma = false;
3191 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3192 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3195 /* This is a simple wrapper around make_typename_type. When the id is
3196 an unresolved identifier node, we can provide a superior diagnostic
3197 using cp_parser_diagnose_invalid_type_name. */
3200 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3201 tree id, location_t id_location)
3204 if (TREE_CODE (id) == IDENTIFIER_NODE)
3206 result = make_typename_type (scope, id, typename_type,
3207 /*complain=*/tf_none);
3208 if (result == error_mark_node)
3209 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3212 return make_typename_type (scope, id, typename_type, tf_error);
3215 /* This is a wrapper around the
3216 make_{pointer,ptrmem,reference}_declarator functions that decides
3217 which one to call based on the CODE and CLASS_TYPE arguments. The
3218 CODE argument should be one of the values returned by
3219 cp_parser_ptr_operator. */
3220 static cp_declarator *
3221 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3222 cp_cv_quals cv_qualifiers,
3223 cp_declarator *target)
3225 if (code == ERROR_MARK)
3226 return cp_error_declarator;
3228 if (code == INDIRECT_REF)
3229 if (class_type == NULL_TREE)
3230 return make_pointer_declarator (cv_qualifiers, target);
3232 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3233 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3234 return make_reference_declarator (cv_qualifiers, target, false);
3235 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3236 return make_reference_declarator (cv_qualifiers, target, true);
3240 /* Create a new C++ parser. */
3243 cp_parser_new (void)
3249 /* cp_lexer_new_main is called before doing GC allocation because
3250 cp_lexer_new_main might load a PCH file. */
3251 lexer = cp_lexer_new_main ();
3253 /* Initialize the binops_by_token so that we can get the tree
3254 directly from the token. */
3255 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3256 binops_by_token[binops[i].token_type] = binops[i];
3258 parser = ggc_alloc_cleared_cp_parser ();
3259 parser->lexer = lexer;
3260 parser->context = cp_parser_context_new (NULL);
3262 /* For now, we always accept GNU extensions. */
3263 parser->allow_gnu_extensions_p = 1;
3265 /* The `>' token is a greater-than operator, not the end of a
3267 parser->greater_than_is_operator_p = true;
3269 parser->default_arg_ok_p = true;
3271 /* We are not parsing a constant-expression. */
3272 parser->integral_constant_expression_p = false;
3273 parser->allow_non_integral_constant_expression_p = false;
3274 parser->non_integral_constant_expression_p = false;
3276 /* Local variable names are not forbidden. */
3277 parser->local_variables_forbidden_p = false;
3279 /* We are not processing an `extern "C"' declaration. */
3280 parser->in_unbraced_linkage_specification_p = false;
3282 /* We are not processing a declarator. */
3283 parser->in_declarator_p = false;
3285 /* We are not processing a template-argument-list. */
3286 parser->in_template_argument_list_p = false;
3288 /* We are not in an iteration statement. */
3289 parser->in_statement = 0;
3291 /* We are not in a switch statement. */
3292 parser->in_switch_statement_p = false;
3294 /* We are not parsing a type-id inside an expression. */
3295 parser->in_type_id_in_expr_p = false;
3297 /* Declarations aren't implicitly extern "C". */
3298 parser->implicit_extern_c = false;
3300 /* String literals should be translated to the execution character set. */
3301 parser->translate_strings_p = true;
3303 /* We are not parsing a function body. */
3304 parser->in_function_body = false;
3306 /* We can correct until told otherwise. */
3307 parser->colon_corrects_to_scope_p = true;
3309 /* The unparsed function queue is empty. */
3310 push_unparsed_function_queues (parser);
3312 /* There are no classes being defined. */
3313 parser->num_classes_being_defined = 0;
3315 /* No template parameters apply. */
3316 parser->num_template_parameter_lists = 0;
3321 /* Create a cp_lexer structure which will emit the tokens in CACHE
3322 and push it onto the parser's lexer stack. This is used for delayed
3323 parsing of in-class method bodies and default arguments, and should
3324 not be confused with tentative parsing. */
3326 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3328 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3329 lexer->next = parser->lexer;
3330 parser->lexer = lexer;
3332 /* Move the current source position to that of the first token in the
3334 cp_lexer_set_source_position_from_token (lexer->next_token);
3337 /* Pop the top lexer off the parser stack. This is never used for the
3338 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3340 cp_parser_pop_lexer (cp_parser *parser)
3342 cp_lexer *lexer = parser->lexer;
3343 parser->lexer = lexer->next;
3344 cp_lexer_destroy (lexer);
3346 /* Put the current source position back where it was before this
3347 lexer was pushed. */
3348 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3351 /* Lexical conventions [gram.lex] */
3353 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3357 cp_parser_identifier (cp_parser* parser)
3361 /* Look for the identifier. */
3362 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3363 /* Return the value. */
3364 return token ? token->u.value : error_mark_node;
3367 /* Parse a sequence of adjacent string constants. Returns a
3368 TREE_STRING representing the combined, nul-terminated string
3369 constant. If TRANSLATE is true, translate the string to the
3370 execution character set. If WIDE_OK is true, a wide string is
3373 C++98 [lex.string] says that if a narrow string literal token is
3374 adjacent to a wide string literal token, the behavior is undefined.
3375 However, C99 6.4.5p4 says that this results in a wide string literal.
3376 We follow C99 here, for consistency with the C front end.
3378 This code is largely lifted from lex_string() in c-lex.c.
3380 FUTURE: ObjC++ will need to handle @-strings here. */
3382 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3386 struct obstack str_ob;
3387 cpp_string str, istr, *strs;
3389 enum cpp_ttype type, curr_type;
3390 int have_suffix_p = 0;
3392 tree suffix_id = NULL_TREE;
3393 bool curr_tok_is_userdef_p = false;
3395 tok = cp_lexer_peek_token (parser->lexer);
3396 if (!cp_parser_is_string_literal (tok))
3398 cp_parser_error (parser, "expected string-literal");
3399 return error_mark_node;
3402 if (cpp_userdef_string_p (tok->type))
3404 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3405 curr_type = cpp_userdef_string_remove_type (tok->type);
3406 curr_tok_is_userdef_p = true;
3410 string_tree = tok->u.value;
3411 curr_type = tok->type;
3415 /* Try to avoid the overhead of creating and destroying an obstack
3416 for the common case of just one string. */
3417 if (!cp_parser_is_string_literal
3418 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3420 cp_lexer_consume_token (parser->lexer);
3422 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3423 str.len = TREE_STRING_LENGTH (string_tree);
3426 if (curr_tok_is_userdef_p)
3428 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3430 curr_type = cpp_userdef_string_remove_type (tok->type);
3433 curr_type = tok->type;
3439 gcc_obstack_init (&str_ob);
3444 cp_lexer_consume_token (parser->lexer);
3446 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3447 str.len = TREE_STRING_LENGTH (string_tree);
3449 if (curr_tok_is_userdef_p)
3451 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3452 if (have_suffix_p == 0)
3454 suffix_id = curr_suffix_id;
3457 else if (have_suffix_p == 1
3458 && curr_suffix_id != suffix_id)
3460 error ("inconsistent user-defined literal suffixes"
3461 " %qD and %qD in string literal",
3462 suffix_id, curr_suffix_id);
3465 curr_type = cpp_userdef_string_remove_type (tok->type);
3468 curr_type = tok->type;
3470 if (type != curr_type)
3472 if (type == CPP_STRING)
3474 else if (curr_type != CPP_STRING)
3475 error_at (tok->location,
3476 "unsupported non-standard concatenation "
3477 "of string literals");
3480 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3482 tok = cp_lexer_peek_token (parser->lexer);
3483 if (cpp_userdef_string_p (tok->type))
3485 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3486 curr_type = cpp_userdef_string_remove_type (tok->type);
3487 curr_tok_is_userdef_p = true;
3491 string_tree = tok->u.value;
3492 curr_type = tok->type;
3493 curr_tok_is_userdef_p = false;
3496 while (cp_parser_is_string_literal (tok));
3498 strs = (cpp_string *) obstack_finish (&str_ob);
3501 if (type != CPP_STRING && !wide_ok)
3503 cp_parser_error (parser, "a wide string is invalid in this context");
3507 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3508 (parse_in, strs, count, &istr, type))
3510 value = build_string (istr.len, (const char *)istr.text);
3511 free (CONST_CAST (unsigned char *, istr.text));
3517 case CPP_UTF8STRING:
3518 TREE_TYPE (value) = char_array_type_node;
3521 TREE_TYPE (value) = char16_array_type_node;
3524 TREE_TYPE (value) = char32_array_type_node;
3527 TREE_TYPE (value) = wchar_array_type_node;
3531 value = fix_string_type (value);
3535 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3536 tok->u.value = literal;
3537 return cp_parser_userdef_string_literal (tok);
3541 /* cpp_interpret_string has issued an error. */
3542 value = error_mark_node;
3545 obstack_free (&str_ob, 0);
3550 /* Look up a literal operator with the name and the exact arguments. */
3553 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3556 decl = lookup_name (name);
3557 if (!decl || decl == error_mark_node)
3558 return error_mark_node;
3560 for (fns = decl; fns; fns = OVL_NEXT (fns))
3564 tree fn = OVL_CURRENT (fns);
3565 tree argtypes = NULL_TREE;
3566 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3567 if (argtypes != NULL_TREE)
3569 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3570 ++ix, argtypes = TREE_CHAIN (argtypes))
3572 tree targ = TREE_VALUE (argtypes);
3573 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3574 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3575 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3576 if ((ptr || arr || !same_type_p (targ, tparm))
3578 || !same_type_p (TREE_TYPE (targ),
3579 TREE_TYPE (tparm))))
3587 return error_mark_node;
3590 /* Parse a user-defined char constant. Returns a call to a user-defined
3591 literal operator taking the character as an argument. */
3594 cp_parser_userdef_char_literal (cp_parser *parser)
3596 cp_token *token = cp_lexer_consume_token (parser->lexer);
3597 tree literal = token->u.value;
3598 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3599 tree value = USERDEF_LITERAL_VALUE (literal);
3600 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3603 /* Build up a call to the user-defined operator */
3604 /* Lookup the name we got back from the id-expression. */
3605 VEC(tree,gc) *args = make_tree_vector ();
3606 VEC_safe_push (tree, gc, args, value);
3607 decl = lookup_literal_operator (name, args);
3608 if (!decl || decl == error_mark_node)
3610 error ("unable to find character literal operator %qD with %qT argument",
3611 name, TREE_TYPE (value));
3612 release_tree_vector (args);
3613 return error_mark_node;
3615 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3616 release_tree_vector (args);
3617 if (result != error_mark_node)
3620 error ("unable to find character literal operator %qD with %qT argument",
3621 name, TREE_TYPE (value));
3622 return error_mark_node;
3625 /* A subroutine of cp_parser_userdef_numeric_literal to
3626 create a char... template parameter pack from a string node. */
3629 make_char_string_pack (tree value)
3632 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3633 const char *str = TREE_STRING_POINTER (value);
3634 int i, len = TREE_STRING_LENGTH (value) - 1;
3635 tree argvec = make_tree_vec (1);
3637 /* Fill in CHARVEC with all of the parameters. */
3638 charvec = make_tree_vec (len);
3639 for (i = 0; i < len; ++i)
3640 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3642 /* Build the argument packs. */
3643 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3644 TREE_TYPE (argpack) = char_type_node;
3646 TREE_VEC_ELT (argvec, 0) = argpack;
3651 /* Parse a user-defined numeric constant. returns a call to a user-defined
3652 literal operator. */
3655 cp_parser_userdef_numeric_literal (cp_parser *parser)
3657 cp_token *token = cp_lexer_consume_token (parser->lexer);
3658 tree literal = token->u.value;
3659 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3660 tree value = USERDEF_LITERAL_VALUE (literal);
3661 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3662 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3666 /* Look for a literal operator taking the exact type of numeric argument
3667 as the literal value. */
3668 args = make_tree_vector ();
3669 VEC_safe_push (tree, gc, args, value);
3670 decl = lookup_literal_operator (name, args);
3671 if (decl && decl != error_mark_node)
3673 result = finish_call_expr (decl, &args, false, true, tf_none);
3674 if (result != error_mark_node)
3676 release_tree_vector (args);
3680 release_tree_vector (args);
3682 /* If the numeric argument didn't work, look for a raw literal
3683 operator taking a const char* argument consisting of the number
3684 in string format. */
3685 args = make_tree_vector ();
3686 VEC_safe_push (tree, gc, args, num_string);
3687 decl = lookup_literal_operator (name, args);
3688 if (decl && decl != error_mark_node)
3690 result = finish_call_expr (decl, &args, false, true, tf_none);
3691 if (result != error_mark_node)
3693 release_tree_vector (args);
3697 release_tree_vector (args);
3699 /* If the raw literal didn't work, look for a non-type template
3700 function with parameter pack char.... Call the function with
3701 template parameter characters representing the number. */
3702 args = make_tree_vector ();
3703 decl = lookup_literal_operator (name, args);
3704 if (decl && decl != error_mark_node)
3706 tree tmpl_args = make_char_string_pack (num_string);
3707 decl = lookup_template_function (decl, tmpl_args);
3708 result = finish_call_expr (decl, &args, false, true, tf_none);
3709 if (result != error_mark_node)
3711 release_tree_vector (args);
3715 release_tree_vector (args);
3717 error ("unable to find numeric literal operator %qD", name);
3718 return error_mark_node;
3721 /* Parse a user-defined string constant. Returns a call to a user-defined
3722 literal operator taking a character pointer and the length of the string
3726 cp_parser_userdef_string_literal (cp_token *token)
3728 tree literal = token->u.value;
3729 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3730 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3731 tree value = USERDEF_LITERAL_VALUE (literal);
3732 int len = TREE_STRING_LENGTH (value)
3733 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3736 /* Build up a call to the user-defined operator */
3737 /* Lookup the name we got back from the id-expression. */
3738 VEC(tree,gc) *args = make_tree_vector ();
3739 VEC_safe_push (tree, gc, args, value);
3740 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3741 decl = lookup_name (name);
3742 if (!decl || decl == error_mark_node)
3744 error ("unable to find string literal operator %qD", name);
3745 release_tree_vector (args);
3746 return error_mark_node;
3748 result = finish_call_expr (decl, &args, false, true, tf_none);
3749 release_tree_vector (args);
3750 if (result != error_mark_node)
3753 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3754 name, TREE_TYPE (value), size_type_node);
3755 return error_mark_node;
3759 /* Basic concepts [gram.basic] */
3761 /* Parse a translation-unit.
3764 declaration-seq [opt]
3766 Returns TRUE if all went well. */
3769 cp_parser_translation_unit (cp_parser* parser)
3771 /* The address of the first non-permanent object on the declarator
3773 static void *declarator_obstack_base;
3777 /* Create the declarator obstack, if necessary. */
3778 if (!cp_error_declarator)
3780 gcc_obstack_init (&declarator_obstack);
3781 /* Create the error declarator. */
3782 cp_error_declarator = make_declarator (cdk_error);
3783 /* Create the empty parameter list. */
3784 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3785 /* Remember where the base of the declarator obstack lies. */
3786 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3789 cp_parser_declaration_seq_opt (parser);
3791 /* If there are no tokens left then all went well. */
3792 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3794 /* Get rid of the token array; we don't need it any more. */
3795 cp_lexer_destroy (parser->lexer);
3796 parser->lexer = NULL;
3798 /* This file might have been a context that's implicitly extern
3799 "C". If so, pop the lang context. (Only relevant for PCH.) */
3800 if (parser->implicit_extern_c)
3802 pop_lang_context ();
3803 parser->implicit_extern_c = false;
3807 finish_translation_unit ();
3813 cp_parser_error (parser, "expected declaration");
3817 /* Make sure the declarator obstack was fully cleaned up. */
3818 gcc_assert (obstack_next_free (&declarator_obstack)
3819 == declarator_obstack_base);
3821 /* All went well. */
3825 /* Expressions [gram.expr] */
3827 /* Parse a primary-expression.
3838 ( compound-statement )
3839 __builtin_va_arg ( assignment-expression , type-id )
3840 __builtin_offsetof ( type-id , offsetof-expression )
3843 __has_nothrow_assign ( type-id )
3844 __has_nothrow_constructor ( type-id )
3845 __has_nothrow_copy ( type-id )
3846 __has_trivial_assign ( type-id )
3847 __has_trivial_constructor ( type-id )
3848 __has_trivial_copy ( type-id )
3849 __has_trivial_destructor ( type-id )
3850 __has_virtual_destructor ( type-id )
3851 __is_abstract ( type-id )
3852 __is_base_of ( type-id , type-id )
3853 __is_class ( type-id )
3854 __is_convertible_to ( type-id , type-id )
3855 __is_empty ( type-id )
3856 __is_enum ( type-id )
3857 __is_literal_type ( type-id )
3858 __is_pod ( type-id )
3859 __is_polymorphic ( type-id )
3860 __is_std_layout ( type-id )
3861 __is_trivial ( type-id )
3862 __is_union ( type-id )
3864 Objective-C++ Extension:
3872 ADDRESS_P is true iff this expression was immediately preceded by
3873 "&" and therefore might denote a pointer-to-member. CAST_P is true
3874 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3875 true iff this expression is a template argument.
3877 Returns a representation of the expression. Upon return, *IDK
3878 indicates what kind of id-expression (if any) was present. */
3881 cp_parser_primary_expression (cp_parser *parser,
3884 bool template_arg_p,
3887 cp_token *token = NULL;
3889 /* Assume the primary expression is not an id-expression. */
3890 *idk = CP_ID_KIND_NONE;
3892 /* Peek at the next token. */
3893 token = cp_lexer_peek_token (parser->lexer);
3894 switch (token->type)
3903 user-defined-literal */
3909 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3910 return cp_parser_userdef_numeric_literal (parser);
3911 token = cp_lexer_consume_token (parser->lexer);
3912 if (TREE_CODE (token->u.value) == FIXED_CST)
3914 error_at (token->location,
3915 "fixed-point types not supported in C++");
3916 return error_mark_node;
3918 /* Floating-point literals are only allowed in an integral
3919 constant expression if they are cast to an integral or
3920 enumeration type. */
3921 if (TREE_CODE (token->u.value) == REAL_CST
3922 && parser->integral_constant_expression_p
3925 /* CAST_P will be set even in invalid code like "int(2.7 +
3926 ...)". Therefore, we have to check that the next token
3927 is sure to end the cast. */
3930 cp_token *next_token;
3932 next_token = cp_lexer_peek_token (parser->lexer);
3933 if (/* The comma at the end of an
3934 enumerator-definition. */
3935 next_token->type != CPP_COMMA
3936 /* The curly brace at the end of an enum-specifier. */
3937 && next_token->type != CPP_CLOSE_BRACE
3938 /* The end of a statement. */
3939 && next_token->type != CPP_SEMICOLON
3940 /* The end of the cast-expression. */
3941 && next_token->type != CPP_CLOSE_PAREN
3942 /* The end of an array bound. */
3943 && next_token->type != CPP_CLOSE_SQUARE
3944 /* The closing ">" in a template-argument-list. */
3945 && (next_token->type != CPP_GREATER
3946 || parser->greater_than_is_operator_p)
3947 /* C++0x only: A ">>" treated like two ">" tokens,
3948 in a template-argument-list. */
3949 && (next_token->type != CPP_RSHIFT
3950 || (cxx_dialect == cxx98)
3951 || parser->greater_than_is_operator_p))
3955 /* If we are within a cast, then the constraint that the
3956 cast is to an integral or enumeration type will be
3957 checked at that point. If we are not within a cast, then
3958 this code is invalid. */
3960 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3962 return token->u.value;
3964 case CPP_CHAR_USERDEF:
3965 case CPP_CHAR16_USERDEF:
3966 case CPP_CHAR32_USERDEF:
3967 case CPP_WCHAR_USERDEF:
3968 return cp_parser_userdef_char_literal (parser);
3974 case CPP_UTF8STRING:
3975 case CPP_STRING_USERDEF:
3976 case CPP_STRING16_USERDEF:
3977 case CPP_STRING32_USERDEF:
3978 case CPP_WSTRING_USERDEF:
3979 case CPP_UTF8STRING_USERDEF:
3980 /* ??? Should wide strings be allowed when parser->translate_strings_p
3981 is false (i.e. in attributes)? If not, we can kill the third
3982 argument to cp_parser_string_literal. */
3983 return cp_parser_string_literal (parser,
3984 parser->translate_strings_p,
3987 case CPP_OPEN_PAREN:
3990 bool saved_greater_than_is_operator_p;
3992 /* Consume the `('. */
3993 cp_lexer_consume_token (parser->lexer);
3994 /* Within a parenthesized expression, a `>' token is always
3995 the greater-than operator. */
3996 saved_greater_than_is_operator_p
3997 = parser->greater_than_is_operator_p;
3998 parser->greater_than_is_operator_p = true;
3999 /* If we see `( { ' then we are looking at the beginning of
4000 a GNU statement-expression. */
4001 if (cp_parser_allow_gnu_extensions_p (parser)
4002 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4004 /* Statement-expressions are not allowed by the standard. */
4005 pedwarn (token->location, OPT_pedantic,
4006 "ISO C++ forbids braced-groups within expressions");
4008 /* And they're not allowed outside of a function-body; you
4009 cannot, for example, write:
4011 int i = ({ int j = 3; j + 1; });
4013 at class or namespace scope. */
4014 if (!parser->in_function_body
4015 || parser->in_template_argument_list_p)
4017 error_at (token->location,
4018 "statement-expressions are not allowed outside "
4019 "functions nor in template-argument lists");
4020 cp_parser_skip_to_end_of_block_or_statement (parser);
4021 expr = error_mark_node;
4025 /* Start the statement-expression. */
4026 expr = begin_stmt_expr ();
4027 /* Parse the compound-statement. */
4028 cp_parser_compound_statement (parser, expr, false, false);
4030 expr = finish_stmt_expr (expr, false);
4035 /* Parse the parenthesized expression. */
4036 expr = cp_parser_expression (parser, cast_p, idk);
4037 /* Let the front end know that this expression was
4038 enclosed in parentheses. This matters in case, for
4039 example, the expression is of the form `A::B', since
4040 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4042 finish_parenthesized_expr (expr);
4043 /* DR 705: Wrapping an unqualified name in parentheses
4044 suppresses arg-dependent lookup. We want to pass back
4045 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4046 (c++/37862), but none of the others. */
4047 if (*idk != CP_ID_KIND_QUALIFIED)
4048 *idk = CP_ID_KIND_NONE;
4050 /* The `>' token might be the end of a template-id or
4051 template-parameter-list now. */
4052 parser->greater_than_is_operator_p
4053 = saved_greater_than_is_operator_p;
4054 /* Consume the `)'. */
4055 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4056 cp_parser_skip_to_end_of_statement (parser);
4061 case CPP_OPEN_SQUARE:
4062 if (c_dialect_objc ())
4063 /* We have an Objective-C++ message. */
4064 return cp_parser_objc_expression (parser);
4066 tree lam = cp_parser_lambda_expression (parser);
4067 /* Don't warn about a failed tentative parse. */
4068 if (cp_parser_error_occurred (parser))
4069 return error_mark_node;
4070 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4074 case CPP_OBJC_STRING:
4075 if (c_dialect_objc ())
4076 /* We have an Objective-C++ string literal. */
4077 return cp_parser_objc_expression (parser);
4078 cp_parser_error (parser, "expected primary-expression");
4079 return error_mark_node;
4082 switch (token->keyword)
4084 /* These two are the boolean literals. */
4086 cp_lexer_consume_token (parser->lexer);
4087 return boolean_true_node;
4089 cp_lexer_consume_token (parser->lexer);
4090 return boolean_false_node;
4092 /* The `__null' literal. */
4094 cp_lexer_consume_token (parser->lexer);
4097 /* The `nullptr' literal. */
4099 cp_lexer_consume_token (parser->lexer);
4100 return nullptr_node;
4102 /* Recognize the `this' keyword. */
4104 cp_lexer_consume_token (parser->lexer);
4105 if (parser->local_variables_forbidden_p)
4107 error_at (token->location,
4108 "%<this%> may not be used in this context");
4109 return error_mark_node;
4111 /* Pointers cannot appear in constant-expressions. */
4112 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4113 return error_mark_node;
4114 return finish_this_expr ();
4116 /* The `operator' keyword can be the beginning of an
4121 case RID_FUNCTION_NAME:
4122 case RID_PRETTY_FUNCTION_NAME:
4123 case RID_C99_FUNCTION_NAME:
4125 non_integral_constant name;
4127 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4128 __func__ are the names of variables -- but they are
4129 treated specially. Therefore, they are handled here,
4130 rather than relying on the generic id-expression logic
4131 below. Grammatically, these names are id-expressions.
4133 Consume the token. */
4134 token = cp_lexer_consume_token (parser->lexer);
4136 switch (token->keyword)
4138 case RID_FUNCTION_NAME:
4139 name = NIC_FUNC_NAME;
4141 case RID_PRETTY_FUNCTION_NAME:
4142 name = NIC_PRETTY_FUNC;
4144 case RID_C99_FUNCTION_NAME:
4145 name = NIC_C99_FUNC;
4151 if (cp_parser_non_integral_constant_expression (parser, name))
4152 return error_mark_node;
4154 /* Look up the name. */
4155 return finish_fname (token->u.value);
4163 /* The `__builtin_va_arg' construct is used to handle
4164 `va_arg'. Consume the `__builtin_va_arg' token. */
4165 cp_lexer_consume_token (parser->lexer);
4166 /* Look for the opening `('. */
4167 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4168 /* Now, parse the assignment-expression. */
4169 expression = cp_parser_assignment_expression (parser,
4170 /*cast_p=*/false, NULL);
4171 /* Look for the `,'. */
4172 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4173 /* Parse the type-id. */
4174 type = cp_parser_type_id (parser);
4175 /* Look for the closing `)'. */
4176 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4177 /* Using `va_arg' in a constant-expression is not
4179 if (cp_parser_non_integral_constant_expression (parser,
4181 return error_mark_node;
4182 return build_x_va_arg (expression, type);
4186 return cp_parser_builtin_offsetof (parser);
4188 case RID_HAS_NOTHROW_ASSIGN:
4189 case RID_HAS_NOTHROW_CONSTRUCTOR:
4190 case RID_HAS_NOTHROW_COPY:
4191 case RID_HAS_TRIVIAL_ASSIGN:
4192 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4193 case RID_HAS_TRIVIAL_COPY:
4194 case RID_HAS_TRIVIAL_DESTRUCTOR:
4195 case RID_HAS_VIRTUAL_DESTRUCTOR:
4196 case RID_IS_ABSTRACT:
4197 case RID_IS_BASE_OF:
4199 case RID_IS_CONVERTIBLE_TO:
4202 case RID_IS_LITERAL_TYPE:
4204 case RID_IS_POLYMORPHIC:
4205 case RID_IS_STD_LAYOUT:
4206 case RID_IS_TRIVIAL:
4208 return cp_parser_trait_expr (parser, token->keyword);
4210 /* Objective-C++ expressions. */
4212 case RID_AT_PROTOCOL:
4213 case RID_AT_SELECTOR:
4214 return cp_parser_objc_expression (parser);
4217 if (parser->in_function_body
4218 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4221 error_at (token->location,
4222 "a template declaration cannot appear at block scope");
4223 cp_parser_skip_to_end_of_block_or_statement (parser);
4224 return error_mark_node;
4227 cp_parser_error (parser, "expected primary-expression");
4228 return error_mark_node;
4231 /* An id-expression can start with either an identifier, a
4232 `::' as the beginning of a qualified-id, or the "operator"
4236 case CPP_TEMPLATE_ID:
4237 case CPP_NESTED_NAME_SPECIFIER:
4241 const char *error_msg;
4244 cp_token *id_expr_token;
4247 /* Parse the id-expression. */
4249 = cp_parser_id_expression (parser,
4250 /*template_keyword_p=*/false,
4251 /*check_dependency_p=*/true,
4253 /*declarator_p=*/false,
4254 /*optional_p=*/false);
4255 if (id_expression == error_mark_node)
4256 return error_mark_node;
4257 id_expr_token = token;
4258 token = cp_lexer_peek_token (parser->lexer);
4259 done = (token->type != CPP_OPEN_SQUARE
4260 && token->type != CPP_OPEN_PAREN
4261 && token->type != CPP_DOT
4262 && token->type != CPP_DEREF
4263 && token->type != CPP_PLUS_PLUS
4264 && token->type != CPP_MINUS_MINUS);
4265 /* If we have a template-id, then no further lookup is
4266 required. If the template-id was for a template-class, we
4267 will sometimes have a TYPE_DECL at this point. */
4268 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4269 || TREE_CODE (id_expression) == TYPE_DECL)
4270 decl = id_expression;
4271 /* Look up the name. */
4274 tree ambiguous_decls;
4276 /* If we already know that this lookup is ambiguous, then
4277 we've already issued an error message; there's no reason
4279 if (id_expr_token->type == CPP_NAME
4280 && id_expr_token->ambiguous_p)
4282 cp_parser_simulate_error (parser);
4283 return error_mark_node;
4286 decl = cp_parser_lookup_name (parser, id_expression,
4289 /*is_namespace=*/false,
4290 /*check_dependency=*/true,
4292 id_expr_token->location);
4293 /* If the lookup was ambiguous, an error will already have
4295 if (ambiguous_decls)
4296 return error_mark_node;
4298 /* In Objective-C++, we may have an Objective-C 2.0
4299 dot-syntax for classes here. */
4300 if (c_dialect_objc ()
4301 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4302 && TREE_CODE (decl) == TYPE_DECL
4303 && objc_is_class_name (decl))
4306 cp_lexer_consume_token (parser->lexer);
4307 component = cp_parser_identifier (parser);
4308 if (component == error_mark_node)
4309 return error_mark_node;
4311 return objc_build_class_component_ref (id_expression, component);
4314 /* In Objective-C++, an instance variable (ivar) may be preferred
4315 to whatever cp_parser_lookup_name() found. */
4316 decl = objc_lookup_ivar (decl, id_expression);
4318 /* If name lookup gives us a SCOPE_REF, then the
4319 qualifying scope was dependent. */
4320 if (TREE_CODE (decl) == SCOPE_REF)
4322 /* At this point, we do not know if DECL is a valid
4323 integral constant expression. We assume that it is
4324 in fact such an expression, so that code like:
4326 template <int N> struct A {
4330 is accepted. At template-instantiation time, we
4331 will check that B<N>::i is actually a constant. */
4334 /* Check to see if DECL is a local variable in a context
4335 where that is forbidden. */
4336 if (parser->local_variables_forbidden_p
4337 && local_variable_p (decl))
4339 /* It might be that we only found DECL because we are
4340 trying to be generous with pre-ISO scoping rules.
4341 For example, consider:
4345 for (int i = 0; i < 10; ++i) {}
4346 extern void f(int j = i);
4349 Here, name look up will originally find the out
4350 of scope `i'. We need to issue a warning message,
4351 but then use the global `i'. */
4352 decl = check_for_out_of_scope_variable (decl);
4353 if (local_variable_p (decl))
4355 error_at (id_expr_token->location,
4356 "local variable %qD may not appear in this context",
4358 return error_mark_node;
4363 decl = (finish_id_expression
4364 (id_expression, decl, parser->scope,
4366 parser->integral_constant_expression_p,
4367 parser->allow_non_integral_constant_expression_p,
4368 &parser->non_integral_constant_expression_p,
4369 template_p, done, address_p,
4372 id_expr_token->location));
4374 cp_parser_error (parser, error_msg);
4378 /* Anything else is an error. */
4380 cp_parser_error (parser, "expected primary-expression");
4381 return error_mark_node;
4385 /* Parse an id-expression.
4392 :: [opt] nested-name-specifier template [opt] unqualified-id
4394 :: operator-function-id
4397 Return a representation of the unqualified portion of the
4398 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4399 a `::' or nested-name-specifier.
4401 Often, if the id-expression was a qualified-id, the caller will
4402 want to make a SCOPE_REF to represent the qualified-id. This
4403 function does not do this in order to avoid wastefully creating
4404 SCOPE_REFs when they are not required.
4406 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4409 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4410 uninstantiated templates.
4412 If *TEMPLATE_P is non-NULL, it is set to true iff the
4413 `template' keyword is used to explicitly indicate that the entity
4414 named is a template.
4416 If DECLARATOR_P is true, the id-expression is appearing as part of
4417 a declarator, rather than as part of an expression. */
4420 cp_parser_id_expression (cp_parser *parser,
4421 bool template_keyword_p,
4422 bool check_dependency_p,
4427 bool global_scope_p;
4428 bool nested_name_specifier_p;
4430 /* Assume the `template' keyword was not used. */
4432 *template_p = template_keyword_p;
4434 /* Look for the optional `::' operator. */
4436 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4438 /* Look for the optional nested-name-specifier. */
4439 nested_name_specifier_p
4440 = (cp_parser_nested_name_specifier_opt (parser,
4441 /*typename_keyword_p=*/false,
4446 /* If there is a nested-name-specifier, then we are looking at
4447 the first qualified-id production. */
4448 if (nested_name_specifier_p)
4451 tree saved_object_scope;
4452 tree saved_qualifying_scope;
4453 tree unqualified_id;
4456 /* See if the next token is the `template' keyword. */
4458 template_p = &is_template;
4459 *template_p = cp_parser_optional_template_keyword (parser);
4460 /* Name lookup we do during the processing of the
4461 unqualified-id might obliterate SCOPE. */
4462 saved_scope = parser->scope;
4463 saved_object_scope = parser->object_scope;
4464 saved_qualifying_scope = parser->qualifying_scope;
4465 /* Process the final unqualified-id. */
4466 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4469 /*optional_p=*/false);
4470 /* Restore the SAVED_SCOPE for our caller. */
4471 parser->scope = saved_scope;
4472 parser->object_scope = saved_object_scope;
4473 parser->qualifying_scope = saved_qualifying_scope;
4475 return unqualified_id;
4477 /* Otherwise, if we are in global scope, then we are looking at one
4478 of the other qualified-id productions. */
4479 else if (global_scope_p)
4484 /* Peek at the next token. */
4485 token = cp_lexer_peek_token (parser->lexer);
4487 /* If it's an identifier, and the next token is not a "<", then
4488 we can avoid the template-id case. This is an optimization
4489 for this common case. */
4490 if (token->type == CPP_NAME
4491 && !cp_parser_nth_token_starts_template_argument_list_p
4493 return cp_parser_identifier (parser);
4495 cp_parser_parse_tentatively (parser);
4496 /* Try a template-id. */
4497 id = cp_parser_template_id (parser,
4498 /*template_keyword_p=*/false,
4499 /*check_dependency_p=*/true,
4501 /* If that worked, we're done. */
4502 if (cp_parser_parse_definitely (parser))
4505 /* Peek at the next token. (Changes in the token buffer may
4506 have invalidated the pointer obtained above.) */
4507 token = cp_lexer_peek_token (parser->lexer);
4509 switch (token->type)
4512 return cp_parser_identifier (parser);
4515 if (token->keyword == RID_OPERATOR)
4516 return cp_parser_operator_function_id (parser);
4520 cp_parser_error (parser, "expected id-expression");
4521 return error_mark_node;
4525 return cp_parser_unqualified_id (parser, template_keyword_p,
4526 /*check_dependency_p=*/true,
4531 /* Parse an unqualified-id.
4535 operator-function-id
4536 conversion-function-id
4540 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4541 keyword, in a construct like `A::template ...'.
4543 Returns a representation of unqualified-id. For the `identifier'
4544 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4545 production a BIT_NOT_EXPR is returned; the operand of the
4546 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4547 other productions, see the documentation accompanying the
4548 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4549 names are looked up in uninstantiated templates. If DECLARATOR_P
4550 is true, the unqualified-id is appearing as part of a declarator,
4551 rather than as part of an expression. */
4554 cp_parser_unqualified_id (cp_parser* parser,
4555 bool template_keyword_p,
4556 bool check_dependency_p,
4562 /* Peek at the next token. */
4563 token = cp_lexer_peek_token (parser->lexer);
4565 switch (token->type)
4571 /* We don't know yet whether or not this will be a
4573 cp_parser_parse_tentatively (parser);
4574 /* Try a template-id. */
4575 id = cp_parser_template_id (parser, template_keyword_p,
4578 /* If it worked, we're done. */
4579 if (cp_parser_parse_definitely (parser))
4581 /* Otherwise, it's an ordinary identifier. */
4582 return cp_parser_identifier (parser);
4585 case CPP_TEMPLATE_ID:
4586 return cp_parser_template_id (parser, template_keyword_p,
4593 tree qualifying_scope;
4598 /* Consume the `~' token. */
4599 cp_lexer_consume_token (parser->lexer);
4600 /* Parse the class-name. The standard, as written, seems to
4603 template <typename T> struct S { ~S (); };
4604 template <typename T> S<T>::~S() {}
4606 is invalid, since `~' must be followed by a class-name, but
4607 `S<T>' is dependent, and so not known to be a class.
4608 That's not right; we need to look in uninstantiated
4609 templates. A further complication arises from:
4611 template <typename T> void f(T t) {
4615 Here, it is not possible to look up `T' in the scope of `T'
4616 itself. We must look in both the current scope, and the
4617 scope of the containing complete expression.
4619 Yet another issue is:
4628 The standard does not seem to say that the `S' in `~S'
4629 should refer to the type `S' and not the data member
4632 /* DR 244 says that we look up the name after the "~" in the
4633 same scope as we looked up the qualifying name. That idea
4634 isn't fully worked out; it's more complicated than that. */
4635 scope = parser->scope;
4636 object_scope = parser->object_scope;
4637 qualifying_scope = parser->qualifying_scope;
4639 /* Check for invalid scopes. */
4640 if (scope == error_mark_node)
4642 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4643 cp_lexer_consume_token (parser->lexer);
4644 return error_mark_node;
4646 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4648 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4649 error_at (token->location,
4650 "scope %qT before %<~%> is not a class-name",
4652 cp_parser_simulate_error (parser);
4653 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4654 cp_lexer_consume_token (parser->lexer);
4655 return error_mark_node;
4657 gcc_assert (!scope || TYPE_P (scope));
4659 /* If the name is of the form "X::~X" it's OK even if X is a
4661 token = cp_lexer_peek_token (parser->lexer);
4663 && token->type == CPP_NAME
4664 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4666 && (token->u.value == TYPE_IDENTIFIER (scope)
4667 || (CLASS_TYPE_P (scope)
4668 && constructor_name_p (token->u.value, scope))))
4670 cp_lexer_consume_token (parser->lexer);
4671 return build_nt (BIT_NOT_EXPR, scope);
4674 /* If there was an explicit qualification (S::~T), first look
4675 in the scope given by the qualification (i.e., S).
4677 Note: in the calls to cp_parser_class_name below we pass
4678 typename_type so that lookup finds the injected-class-name
4679 rather than the constructor. */
4681 type_decl = NULL_TREE;
4684 cp_parser_parse_tentatively (parser);
4685 type_decl = cp_parser_class_name (parser,
4686 /*typename_keyword_p=*/false,
4687 /*template_keyword_p=*/false,
4689 /*check_dependency=*/false,
4690 /*class_head_p=*/false,
4692 if (cp_parser_parse_definitely (parser))
4695 /* In "N::S::~S", look in "N" as well. */
4696 if (!done && scope && qualifying_scope)
4698 cp_parser_parse_tentatively (parser);
4699 parser->scope = qualifying_scope;
4700 parser->object_scope = NULL_TREE;
4701 parser->qualifying_scope = NULL_TREE;
4703 = cp_parser_class_name (parser,
4704 /*typename_keyword_p=*/false,
4705 /*template_keyword_p=*/false,
4707 /*check_dependency=*/false,
4708 /*class_head_p=*/false,
4710 if (cp_parser_parse_definitely (parser))
4713 /* In "p->S::~T", look in the scope given by "*p" as well. */
4714 else if (!done && object_scope)
4716 cp_parser_parse_tentatively (parser);
4717 parser->scope = object_scope;
4718 parser->object_scope = NULL_TREE;
4719 parser->qualifying_scope = NULL_TREE;
4721 = cp_parser_class_name (parser,
4722 /*typename_keyword_p=*/false,
4723 /*template_keyword_p=*/false,
4725 /*check_dependency=*/false,
4726 /*class_head_p=*/false,
4728 if (cp_parser_parse_definitely (parser))
4731 /* Look in the surrounding context. */
4734 parser->scope = NULL_TREE;
4735 parser->object_scope = NULL_TREE;
4736 parser->qualifying_scope = NULL_TREE;
4737 if (processing_template_decl)
4738 cp_parser_parse_tentatively (parser);
4740 = cp_parser_class_name (parser,
4741 /*typename_keyword_p=*/false,
4742 /*template_keyword_p=*/false,
4744 /*check_dependency=*/false,
4745 /*class_head_p=*/false,
4747 if (processing_template_decl
4748 && ! cp_parser_parse_definitely (parser))
4750 /* We couldn't find a type with this name, so just accept
4751 it and check for a match at instantiation time. */
4752 type_decl = cp_parser_identifier (parser);
4753 if (type_decl != error_mark_node)
4754 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4758 /* If an error occurred, assume that the name of the
4759 destructor is the same as the name of the qualifying
4760 class. That allows us to keep parsing after running
4761 into ill-formed destructor names. */
4762 if (type_decl == error_mark_node && scope)
4763 return build_nt (BIT_NOT_EXPR, scope);
4764 else if (type_decl == error_mark_node)
4765 return error_mark_node;
4767 /* Check that destructor name and scope match. */
4768 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4770 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4771 error_at (token->location,
4772 "declaration of %<~%T%> as member of %qT",
4774 cp_parser_simulate_error (parser);
4775 return error_mark_node;
4780 A typedef-name that names a class shall not be used as the
4781 identifier in the declarator for a destructor declaration. */
4783 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4784 && !DECL_SELF_REFERENCE_P (type_decl)
4785 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4786 error_at (token->location,
4787 "typedef-name %qD used as destructor declarator",
4790 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4794 if (token->keyword == RID_OPERATOR)
4798 /* This could be a template-id, so we try that first. */
4799 cp_parser_parse_tentatively (parser);
4800 /* Try a template-id. */
4801 id = cp_parser_template_id (parser, template_keyword_p,
4802 /*check_dependency_p=*/true,
4804 /* If that worked, we're done. */
4805 if (cp_parser_parse_definitely (parser))
4807 /* We still don't know whether we're looking at an
4808 operator-function-id or a conversion-function-id. */
4809 cp_parser_parse_tentatively (parser);
4810 /* Try an operator-function-id. */
4811 id = cp_parser_operator_function_id (parser);
4812 /* If that didn't work, try a conversion-function-id. */
4813 if (!cp_parser_parse_definitely (parser))
4814 id = cp_parser_conversion_function_id (parser);
4815 else if (UDLIT_OPER_P (id))
4818 const char *name = UDLIT_OP_SUFFIX (id);
4819 if (name[0] != '_' && !in_system_header)
4820 warning (0, "literal operator suffixes not preceded by %<_%>"
4821 " are reserved for future standardization");
4831 cp_parser_error (parser, "expected unqualified-id");
4832 return error_mark_node;
4836 /* Parse an (optional) nested-name-specifier.
4838 nested-name-specifier: [C++98]
4839 class-or-namespace-name :: nested-name-specifier [opt]
4840 class-or-namespace-name :: template nested-name-specifier [opt]
4842 nested-name-specifier: [C++0x]
4845 nested-name-specifier identifier ::
4846 nested-name-specifier template [opt] simple-template-id ::
4848 PARSER->SCOPE should be set appropriately before this function is
4849 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4850 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4853 Sets PARSER->SCOPE to the class (TYPE) or namespace
4854 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4855 it unchanged if there is no nested-name-specifier. Returns the new
4856 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4858 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4859 part of a declaration and/or decl-specifier. */
4862 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4863 bool typename_keyword_p,
4864 bool check_dependency_p,
4866 bool is_declaration)
4868 bool success = false;
4869 cp_token_position start = 0;
4872 /* Remember where the nested-name-specifier starts. */
4873 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4875 start = cp_lexer_token_position (parser->lexer, false);
4876 push_deferring_access_checks (dk_deferred);
4883 tree saved_qualifying_scope;
4884 bool template_keyword_p;
4886 /* Spot cases that cannot be the beginning of a
4887 nested-name-specifier. */
4888 token = cp_lexer_peek_token (parser->lexer);
4890 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4891 the already parsed nested-name-specifier. */
4892 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4894 /* Grab the nested-name-specifier and continue the loop. */
4895 cp_parser_pre_parsed_nested_name_specifier (parser);
4896 /* If we originally encountered this nested-name-specifier
4897 with IS_DECLARATION set to false, we will not have
4898 resolved TYPENAME_TYPEs, so we must do so here. */
4900 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4902 new_scope = resolve_typename_type (parser->scope,
4903 /*only_current_p=*/false);
4904 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4905 parser->scope = new_scope;
4911 /* Spot cases that cannot be the beginning of a
4912 nested-name-specifier. On the second and subsequent times
4913 through the loop, we look for the `template' keyword. */
4914 if (success && token->keyword == RID_TEMPLATE)
4916 /* A template-id can start a nested-name-specifier. */
4917 else if (token->type == CPP_TEMPLATE_ID)
4919 /* DR 743: decltype can be used in a nested-name-specifier. */
4920 else if (token_is_decltype (token))
4924 /* If the next token is not an identifier, then it is
4925 definitely not a type-name or namespace-name. */
4926 if (token->type != CPP_NAME)
4928 /* If the following token is neither a `<' (to begin a
4929 template-id), nor a `::', then we are not looking at a
4930 nested-name-specifier. */
4931 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4933 if (token->type == CPP_COLON
4934 && parser->colon_corrects_to_scope_p
4935 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4937 error_at (token->location,
4938 "found %<:%> in nested-name-specifier, expected %<::%>");
4939 token->type = CPP_SCOPE;
4942 if (token->type != CPP_SCOPE
4943 && !cp_parser_nth_token_starts_template_argument_list_p
4948 /* The nested-name-specifier is optional, so we parse
4950 cp_parser_parse_tentatively (parser);
4952 /* Look for the optional `template' keyword, if this isn't the
4953 first time through the loop. */
4955 template_keyword_p = cp_parser_optional_template_keyword (parser);
4957 template_keyword_p = false;
4959 /* Save the old scope since the name lookup we are about to do
4960 might destroy it. */
4961 old_scope = parser->scope;
4962 saved_qualifying_scope = parser->qualifying_scope;
4963 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4964 look up names in "X<T>::I" in order to determine that "Y" is
4965 a template. So, if we have a typename at this point, we make
4966 an effort to look through it. */
4968 && !typename_keyword_p
4970 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4971 parser->scope = resolve_typename_type (parser->scope,
4972 /*only_current_p=*/false);
4973 /* Parse the qualifying entity. */
4975 = cp_parser_qualifying_entity (parser,
4981 /* Look for the `::' token. */
4982 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4984 /* If we found what we wanted, we keep going; otherwise, we're
4986 if (!cp_parser_parse_definitely (parser))
4988 bool error_p = false;
4990 /* Restore the OLD_SCOPE since it was valid before the
4991 failed attempt at finding the last
4992 class-or-namespace-name. */
4993 parser->scope = old_scope;
4994 parser->qualifying_scope = saved_qualifying_scope;
4996 /* If the next token is a decltype, and the one after that is a
4997 `::', then the decltype has failed to resolve to a class or
4998 enumeration type. Give this error even when parsing
4999 tentatively since it can't possibly be valid--and we're going
5000 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5001 won't get another chance.*/
5002 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5003 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5006 token = cp_lexer_consume_token (parser->lexer);
5007 error_at (token->location, "decltype evaluates to %qT, "
5008 "which is not a class or enumeration type",
5010 parser->scope = error_mark_node;
5014 cp_lexer_consume_token (parser->lexer);
5017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5019 /* If the next token is an identifier, and the one after
5020 that is a `::', then any valid interpretation would have
5021 found a class-or-namespace-name. */
5022 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5023 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5025 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5028 token = cp_lexer_consume_token (parser->lexer);
5031 if (!token->ambiguous_p)
5034 tree ambiguous_decls;
5036 decl = cp_parser_lookup_name (parser, token->u.value,
5038 /*is_template=*/false,
5039 /*is_namespace=*/false,
5040 /*check_dependency=*/true,
5043 if (TREE_CODE (decl) == TEMPLATE_DECL)
5044 error_at (token->location,
5045 "%qD used without template parameters",
5047 else if (ambiguous_decls)
5049 error_at (token->location,
5050 "reference to %qD is ambiguous",
5052 print_candidates (ambiguous_decls);
5053 decl = error_mark_node;
5057 if (cxx_dialect != cxx98)
5058 cp_parser_name_lookup_error
5059 (parser, token->u.value, decl, NLE_NOT_CXX98,
5062 cp_parser_name_lookup_error
5063 (parser, token->u.value, decl, NLE_CXX98,
5067 parser->scope = error_mark_node;
5069 /* Treat this as a successful nested-name-specifier
5074 If the name found is not a class-name (clause
5075 _class_) or namespace-name (_namespace.def_), the
5076 program is ill-formed. */
5079 cp_lexer_consume_token (parser->lexer);
5083 /* We've found one valid nested-name-specifier. */
5085 /* Name lookup always gives us a DECL. */
5086 if (TREE_CODE (new_scope) == TYPE_DECL)
5087 new_scope = TREE_TYPE (new_scope);
5088 /* Uses of "template" must be followed by actual templates. */
5089 if (template_keyword_p
5090 && !(CLASS_TYPE_P (new_scope)
5091 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5092 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5093 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5094 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5095 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5096 == TEMPLATE_ID_EXPR)))
5097 permerror (input_location, TYPE_P (new_scope)
5098 ? G_("%qT is not a template")
5099 : G_("%qD is not a template"),
5101 /* If it is a class scope, try to complete it; we are about to
5102 be looking up names inside the class. */
5103 if (TYPE_P (new_scope)
5104 /* Since checking types for dependency can be expensive,
5105 avoid doing it if the type is already complete. */
5106 && !COMPLETE_TYPE_P (new_scope)
5107 /* Do not try to complete dependent types. */
5108 && !dependent_type_p (new_scope))
5110 new_scope = complete_type (new_scope);
5111 /* If it is a typedef to current class, use the current
5112 class instead, as the typedef won't have any names inside
5114 if (!COMPLETE_TYPE_P (new_scope)
5115 && currently_open_class (new_scope))
5116 new_scope = TYPE_MAIN_VARIANT (new_scope);
5118 /* Make sure we look in the right scope the next time through
5120 parser->scope = new_scope;
5123 /* If parsing tentatively, replace the sequence of tokens that makes
5124 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5125 token. That way, should we re-parse the token stream, we will
5126 not have to repeat the effort required to do the parse, nor will
5127 we issue duplicate error messages. */
5128 if (success && start)
5132 token = cp_lexer_token_at (parser->lexer, start);
5133 /* Reset the contents of the START token. */
5134 token->type = CPP_NESTED_NAME_SPECIFIER;
5135 /* Retrieve any deferred checks. Do not pop this access checks yet
5136 so the memory will not be reclaimed during token replacing below. */
5137 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5138 token->u.tree_check_value->value = parser->scope;
5139 token->u.tree_check_value->checks = get_deferred_access_checks ();
5140 token->u.tree_check_value->qualifying_scope =
5141 parser->qualifying_scope;
5142 token->keyword = RID_MAX;
5144 /* Purge all subsequent tokens. */
5145 cp_lexer_purge_tokens_after (parser->lexer, start);
5149 pop_to_parent_deferring_access_checks ();
5151 return success ? parser->scope : NULL_TREE;
5154 /* Parse a nested-name-specifier. See
5155 cp_parser_nested_name_specifier_opt for details. This function
5156 behaves identically, except that it will an issue an error if no
5157 nested-name-specifier is present. */
5160 cp_parser_nested_name_specifier (cp_parser *parser,
5161 bool typename_keyword_p,
5162 bool check_dependency_p,
5164 bool is_declaration)
5168 /* Look for the nested-name-specifier. */
5169 scope = cp_parser_nested_name_specifier_opt (parser,
5174 /* If it was not present, issue an error message. */
5177 cp_parser_error (parser, "expected nested-name-specifier");
5178 parser->scope = NULL_TREE;
5184 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5185 this is either a class-name or a namespace-name (which corresponds
5186 to the class-or-namespace-name production in the grammar). For
5187 C++0x, it can also be a type-name that refers to an enumeration
5188 type or a simple-template-id.
5190 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5191 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5192 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5193 TYPE_P is TRUE iff the next name should be taken as a class-name,
5194 even the same name is declared to be another entity in the same
5197 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5198 specified by the class-or-namespace-name. If neither is found the
5199 ERROR_MARK_NODE is returned. */
5202 cp_parser_qualifying_entity (cp_parser *parser,
5203 bool typename_keyword_p,
5204 bool template_keyword_p,
5205 bool check_dependency_p,
5207 bool is_declaration)
5210 tree saved_qualifying_scope;
5211 tree saved_object_scope;
5214 bool successful_parse_p;
5216 /* DR 743: decltype can appear in a nested-name-specifier. */
5217 if (cp_lexer_next_token_is_decltype (parser->lexer))
5219 scope = cp_parser_decltype (parser);
5220 if (TREE_CODE (scope) != ENUMERAL_TYPE
5221 && !MAYBE_CLASS_TYPE_P (scope))
5223 cp_parser_simulate_error (parser);
5224 return error_mark_node;
5226 if (TYPE_NAME (scope))
5227 scope = TYPE_NAME (scope);
5231 /* Before we try to parse the class-name, we must save away the
5232 current PARSER->SCOPE since cp_parser_class_name will destroy
5234 saved_scope = parser->scope;
5235 saved_qualifying_scope = parser->qualifying_scope;
5236 saved_object_scope = parser->object_scope;
5237 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5238 there is no need to look for a namespace-name. */
5239 only_class_p = template_keyword_p
5240 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5242 cp_parser_parse_tentatively (parser);
5243 scope = cp_parser_class_name (parser,
5246 type_p ? class_type : none_type,
5248 /*class_head_p=*/false,
5250 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5251 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5253 && cxx_dialect != cxx98
5254 && !successful_parse_p)
5256 /* Restore the saved scope. */
5257 parser->scope = saved_scope;
5258 parser->qualifying_scope = saved_qualifying_scope;
5259 parser->object_scope = saved_object_scope;
5261 /* Parse tentatively. */
5262 cp_parser_parse_tentatively (parser);
5264 /* Parse a type-name */
5265 scope = cp_parser_type_name (parser);
5267 /* "If the name found does not designate a namespace or a class,
5268 enumeration, or dependent type, the program is ill-formed."
5270 We cover classes and dependent types above and namespaces below,
5271 so this code is only looking for enums. */
5272 if (!scope || TREE_CODE (scope) != TYPE_DECL
5273 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5274 cp_parser_simulate_error (parser);
5276 successful_parse_p = cp_parser_parse_definitely (parser);
5278 /* If that didn't work, try for a namespace-name. */
5279 if (!only_class_p && !successful_parse_p)
5281 /* Restore the saved scope. */
5282 parser->scope = saved_scope;
5283 parser->qualifying_scope = saved_qualifying_scope;
5284 parser->object_scope = saved_object_scope;
5285 /* If we are not looking at an identifier followed by the scope
5286 resolution operator, then this is not part of a
5287 nested-name-specifier. (Note that this function is only used
5288 to parse the components of a nested-name-specifier.) */
5289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5290 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5291 return error_mark_node;
5292 scope = cp_parser_namespace_name (parser);
5298 /* Parse a postfix-expression.
5302 postfix-expression [ expression ]
5303 postfix-expression ( expression-list [opt] )
5304 simple-type-specifier ( expression-list [opt] )
5305 typename :: [opt] nested-name-specifier identifier
5306 ( expression-list [opt] )
5307 typename :: [opt] nested-name-specifier template [opt] template-id
5308 ( expression-list [opt] )
5309 postfix-expression . template [opt] id-expression
5310 postfix-expression -> template [opt] id-expression
5311 postfix-expression . pseudo-destructor-name
5312 postfix-expression -> pseudo-destructor-name
5313 postfix-expression ++
5314 postfix-expression --
5315 dynamic_cast < type-id > ( expression )
5316 static_cast < type-id > ( expression )
5317 reinterpret_cast < type-id > ( expression )
5318 const_cast < type-id > ( expression )
5319 typeid ( expression )
5325 ( type-id ) { initializer-list , [opt] }
5327 This extension is a GNU version of the C99 compound-literal
5328 construct. (The C99 grammar uses `type-name' instead of `type-id',
5329 but they are essentially the same concept.)
5331 If ADDRESS_P is true, the postfix expression is the operand of the
5332 `&' operator. CAST_P is true if this expression is the target of a
5335 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5336 class member access expressions [expr.ref].
5338 Returns a representation of the expression. */
5341 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5342 bool member_access_only_p,
5343 cp_id_kind * pidk_return)
5347 cp_id_kind idk = CP_ID_KIND_NONE;
5348 tree postfix_expression = NULL_TREE;
5349 bool is_member_access = false;
5351 /* Peek at the next token. */
5352 token = cp_lexer_peek_token (parser->lexer);
5353 /* Some of the productions are determined by keywords. */
5354 keyword = token->keyword;
5364 const char *saved_message;
5366 /* All of these can be handled in the same way from the point
5367 of view of parsing. Begin by consuming the token
5368 identifying the cast. */
5369 cp_lexer_consume_token (parser->lexer);
5371 /* New types cannot be defined in the cast. */
5372 saved_message = parser->type_definition_forbidden_message;
5373 parser->type_definition_forbidden_message
5374 = G_("types may not be defined in casts");
5376 /* Look for the opening `<'. */
5377 cp_parser_require (parser, CPP_LESS, RT_LESS);
5378 /* Parse the type to which we are casting. */
5379 type = cp_parser_type_id (parser);
5380 /* Look for the closing `>'. */
5381 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5382 /* Restore the old message. */
5383 parser->type_definition_forbidden_message = saved_message;
5385 /* And the expression which is being cast. */
5386 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5387 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5388 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5390 /* Only type conversions to integral or enumeration types
5391 can be used in constant-expressions. */
5392 if (!cast_valid_in_integral_constant_expression_p (type)
5393 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5394 return error_mark_node;
5400 = build_dynamic_cast (type, expression, tf_warning_or_error);
5404 = build_static_cast (type, expression, tf_warning_or_error);
5408 = build_reinterpret_cast (type, expression,
5409 tf_warning_or_error);
5413 = build_const_cast (type, expression, tf_warning_or_error);
5424 const char *saved_message;
5425 bool saved_in_type_id_in_expr_p;
5427 /* Consume the `typeid' token. */
5428 cp_lexer_consume_token (parser->lexer);
5429 /* Look for the `(' token. */
5430 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5431 /* Types cannot be defined in a `typeid' expression. */
5432 saved_message = parser->type_definition_forbidden_message;
5433 parser->type_definition_forbidden_message
5434 = G_("types may not be defined in a %<typeid%> expression");
5435 /* We can't be sure yet whether we're looking at a type-id or an
5437 cp_parser_parse_tentatively (parser);
5438 /* Try a type-id first. */
5439 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5440 parser->in_type_id_in_expr_p = true;
5441 type = cp_parser_type_id (parser);
5442 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5443 /* Look for the `)' token. Otherwise, we can't be sure that
5444 we're not looking at an expression: consider `typeid (int
5445 (3))', for example. */
5446 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5447 /* If all went well, simply lookup the type-id. */
5448 if (cp_parser_parse_definitely (parser))
5449 postfix_expression = get_typeid (type);
5450 /* Otherwise, fall back to the expression variant. */
5455 /* Look for an expression. */
5456 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5457 /* Compute its typeid. */
5458 postfix_expression = build_typeid (expression);
5459 /* Look for the `)' token. */
5460 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5462 /* Restore the saved message. */
5463 parser->type_definition_forbidden_message = saved_message;
5464 /* `typeid' may not appear in an integral constant expression. */
5465 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5466 return error_mark_node;
5473 /* The syntax permitted here is the same permitted for an
5474 elaborated-type-specifier. */
5475 type = cp_parser_elaborated_type_specifier (parser,
5476 /*is_friend=*/false,
5477 /*is_declaration=*/false);
5478 postfix_expression = cp_parser_functional_cast (parser, type);
5486 /* If the next thing is a simple-type-specifier, we may be
5487 looking at a functional cast. We could also be looking at
5488 an id-expression. So, we try the functional cast, and if
5489 that doesn't work we fall back to the primary-expression. */
5490 cp_parser_parse_tentatively (parser);
5491 /* Look for the simple-type-specifier. */
5492 type = cp_parser_simple_type_specifier (parser,
5493 /*decl_specs=*/NULL,
5494 CP_PARSER_FLAGS_NONE);
5495 /* Parse the cast itself. */
5496 if (!cp_parser_error_occurred (parser))
5498 = cp_parser_functional_cast (parser, type);
5499 /* If that worked, we're done. */
5500 if (cp_parser_parse_definitely (parser))
5503 /* If the functional-cast didn't work out, try a
5504 compound-literal. */
5505 if (cp_parser_allow_gnu_extensions_p (parser)
5506 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5508 VEC(constructor_elt,gc) *initializer_list = NULL;
5509 bool saved_in_type_id_in_expr_p;
5511 cp_parser_parse_tentatively (parser);
5512 /* Consume the `('. */
5513 cp_lexer_consume_token (parser->lexer);
5514 /* Parse the type. */
5515 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5516 parser->in_type_id_in_expr_p = true;
5517 type = cp_parser_type_id (parser);
5518 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5519 /* Look for the `)'. */
5520 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5521 /* Look for the `{'. */
5522 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5523 /* If things aren't going well, there's no need to
5525 if (!cp_parser_error_occurred (parser))
5527 bool non_constant_p;
5528 /* Parse the initializer-list. */
5530 = cp_parser_initializer_list (parser, &non_constant_p);
5531 /* Allow a trailing `,'. */
5532 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5533 cp_lexer_consume_token (parser->lexer);
5534 /* Look for the final `}'. */
5535 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5537 /* If that worked, we're definitely looking at a
5538 compound-literal expression. */
5539 if (cp_parser_parse_definitely (parser))
5541 /* Warn the user that a compound literal is not
5542 allowed in standard C++. */
5543 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5544 /* For simplicity, we disallow compound literals in
5545 constant-expressions. We could
5546 allow compound literals of integer type, whose
5547 initializer was a constant, in constant
5548 expressions. Permitting that usage, as a further
5549 extension, would not change the meaning of any
5550 currently accepted programs. (Of course, as
5551 compound literals are not part of ISO C++, the
5552 standard has nothing to say.) */
5553 if (cp_parser_non_integral_constant_expression (parser,
5556 postfix_expression = error_mark_node;
5559 /* Form the representation of the compound-literal. */
5561 = (finish_compound_literal
5562 (type, build_constructor (init_list_type_node,
5564 tf_warning_or_error));
5569 /* It must be a primary-expression. */
5571 = cp_parser_primary_expression (parser, address_p, cast_p,
5572 /*template_arg_p=*/false,
5578 /* Keep looping until the postfix-expression is complete. */
5581 if (idk == CP_ID_KIND_UNQUALIFIED
5582 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5583 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5584 /* It is not a Koenig lookup function call. */
5586 = unqualified_name_lookup_error (postfix_expression);
5588 /* Peek at the next token. */
5589 token = cp_lexer_peek_token (parser->lexer);
5591 switch (token->type)
5593 case CPP_OPEN_SQUARE:
5595 = cp_parser_postfix_open_square_expression (parser,
5598 idk = CP_ID_KIND_NONE;
5599 is_member_access = false;
5602 case CPP_OPEN_PAREN:
5603 /* postfix-expression ( expression-list [opt] ) */
5606 bool is_builtin_constant_p;
5607 bool saved_integral_constant_expression_p = false;
5608 bool saved_non_integral_constant_expression_p = false;
5611 is_member_access = false;
5613 is_builtin_constant_p
5614 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5615 if (is_builtin_constant_p)
5617 /* The whole point of __builtin_constant_p is to allow
5618 non-constant expressions to appear as arguments. */
5619 saved_integral_constant_expression_p
5620 = parser->integral_constant_expression_p;
5621 saved_non_integral_constant_expression_p
5622 = parser->non_integral_constant_expression_p;
5623 parser->integral_constant_expression_p = false;
5625 args = (cp_parser_parenthesized_expression_list
5627 /*cast_p=*/false, /*allow_expansion_p=*/true,
5628 /*non_constant_p=*/NULL));
5629 if (is_builtin_constant_p)
5631 parser->integral_constant_expression_p
5632 = saved_integral_constant_expression_p;
5633 parser->non_integral_constant_expression_p
5634 = saved_non_integral_constant_expression_p;
5639 postfix_expression = error_mark_node;
5643 /* Function calls are not permitted in
5644 constant-expressions. */
5645 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5646 && cp_parser_non_integral_constant_expression (parser,
5649 postfix_expression = error_mark_node;
5650 release_tree_vector (args);
5655 if (idk == CP_ID_KIND_UNQUALIFIED
5656 || idk == CP_ID_KIND_TEMPLATE_ID)
5658 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5660 if (!VEC_empty (tree, args))
5663 if (!any_type_dependent_arguments_p (args))
5665 = perform_koenig_lookup (postfix_expression, args,
5666 /*include_std=*/false,
5667 tf_warning_or_error);
5671 = unqualified_fn_lookup_error (postfix_expression);
5673 /* We do not perform argument-dependent lookup if
5674 normal lookup finds a non-function, in accordance
5675 with the expected resolution of DR 218. */
5676 else if (!VEC_empty (tree, args)
5677 && is_overloaded_fn (postfix_expression))
5679 tree fn = get_first_fn (postfix_expression);
5680 fn = STRIP_TEMPLATE (fn);
5682 /* Do not do argument dependent lookup if regular
5683 lookup finds a member function or a block-scope
5684 function declaration. [basic.lookup.argdep]/3 */
5685 if (!DECL_FUNCTION_MEMBER_P (fn)
5686 && !DECL_LOCAL_FUNCTION_P (fn))
5689 if (!any_type_dependent_arguments_p (args))
5691 = perform_koenig_lookup (postfix_expression, args,
5692 /*include_std=*/false,
5693 tf_warning_or_error);
5698 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5700 tree instance = TREE_OPERAND (postfix_expression, 0);
5701 tree fn = TREE_OPERAND (postfix_expression, 1);
5703 if (processing_template_decl
5704 && (type_dependent_expression_p (instance)
5705 || (!BASELINK_P (fn)
5706 && TREE_CODE (fn) != FIELD_DECL)
5707 || type_dependent_expression_p (fn)
5708 || any_type_dependent_arguments_p (args)))
5711 = build_nt_call_vec (postfix_expression, args);
5712 release_tree_vector (args);
5716 if (BASELINK_P (fn))
5719 = (build_new_method_call
5720 (instance, fn, &args, NULL_TREE,
5721 (idk == CP_ID_KIND_QUALIFIED
5722 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5725 tf_warning_or_error));
5729 = finish_call_expr (postfix_expression, &args,
5730 /*disallow_virtual=*/false,
5732 tf_warning_or_error);
5734 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5735 || TREE_CODE (postfix_expression) == MEMBER_REF
5736 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5737 postfix_expression = (build_offset_ref_call_from_tree
5738 (postfix_expression, &args));
5739 else if (idk == CP_ID_KIND_QUALIFIED)
5740 /* A call to a static class member, or a namespace-scope
5743 = finish_call_expr (postfix_expression, &args,
5744 /*disallow_virtual=*/true,
5746 tf_warning_or_error);
5748 /* All other function calls. */
5750 = finish_call_expr (postfix_expression, &args,
5751 /*disallow_virtual=*/false,
5753 tf_warning_or_error);
5755 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5756 idk = CP_ID_KIND_NONE;
5758 release_tree_vector (args);
5764 /* postfix-expression . template [opt] id-expression
5765 postfix-expression . pseudo-destructor-name
5766 postfix-expression -> template [opt] id-expression
5767 postfix-expression -> pseudo-destructor-name */
5769 /* Consume the `.' or `->' operator. */
5770 cp_lexer_consume_token (parser->lexer);
5773 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5778 is_member_access = true;
5782 /* postfix-expression ++ */
5783 /* Consume the `++' token. */
5784 cp_lexer_consume_token (parser->lexer);
5785 /* Generate a representation for the complete expression. */
5787 = finish_increment_expr (postfix_expression,
5788 POSTINCREMENT_EXPR);
5789 /* Increments may not appear in constant-expressions. */
5790 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5791 postfix_expression = error_mark_node;
5792 idk = CP_ID_KIND_NONE;
5793 is_member_access = false;
5796 case CPP_MINUS_MINUS:
5797 /* postfix-expression -- */
5798 /* Consume the `--' token. */
5799 cp_lexer_consume_token (parser->lexer);
5800 /* Generate a representation for the complete expression. */
5802 = finish_increment_expr (postfix_expression,
5803 POSTDECREMENT_EXPR);
5804 /* Decrements may not appear in constant-expressions. */
5805 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5806 postfix_expression = error_mark_node;
5807 idk = CP_ID_KIND_NONE;
5808 is_member_access = false;
5812 if (pidk_return != NULL)
5813 * pidk_return = idk;
5814 if (member_access_only_p)
5815 return is_member_access? postfix_expression : error_mark_node;
5817 return postfix_expression;
5821 /* We should never get here. */
5823 return error_mark_node;
5826 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5827 by cp_parser_builtin_offsetof. We're looking for
5829 postfix-expression [ expression ]
5831 FOR_OFFSETOF is set if we're being called in that context, which
5832 changes how we deal with integer constant expressions. */
5835 cp_parser_postfix_open_square_expression (cp_parser *parser,
5836 tree postfix_expression,
5841 /* Consume the `[' token. */
5842 cp_lexer_consume_token (parser->lexer);
5844 /* Parse the index expression. */
5845 /* ??? For offsetof, there is a question of what to allow here. If
5846 offsetof is not being used in an integral constant expression context,
5847 then we *could* get the right answer by computing the value at runtime.
5848 If we are in an integral constant expression context, then we might
5849 could accept any constant expression; hard to say without analysis.
5850 Rather than open the barn door too wide right away, allow only integer
5851 constant expressions here. */
5853 index = cp_parser_constant_expression (parser, false, NULL);
5855 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5857 /* Look for the closing `]'. */
5858 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5860 /* Build the ARRAY_REF. */
5861 postfix_expression = grok_array_decl (postfix_expression, index);
5863 /* When not doing offsetof, array references are not permitted in
5864 constant-expressions. */
5866 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5867 postfix_expression = error_mark_node;
5869 return postfix_expression;
5872 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5873 by cp_parser_builtin_offsetof. We're looking for
5875 postfix-expression . template [opt] id-expression
5876 postfix-expression . pseudo-destructor-name
5877 postfix-expression -> template [opt] id-expression
5878 postfix-expression -> pseudo-destructor-name
5880 FOR_OFFSETOF is set if we're being called in that context. That sorta
5881 limits what of the above we'll actually accept, but nevermind.
5882 TOKEN_TYPE is the "." or "->" token, which will already have been
5883 removed from the stream. */
5886 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5887 enum cpp_ttype token_type,
5888 tree postfix_expression,
5889 bool for_offsetof, cp_id_kind *idk,
5890 location_t location)
5894 bool pseudo_destructor_p;
5895 tree scope = NULL_TREE;
5897 /* If this is a `->' operator, dereference the pointer. */
5898 if (token_type == CPP_DEREF)
5899 postfix_expression = build_x_arrow (postfix_expression);
5900 /* Check to see whether or not the expression is type-dependent. */
5901 dependent_p = type_dependent_expression_p (postfix_expression);
5902 /* The identifier following the `->' or `.' is not qualified. */
5903 parser->scope = NULL_TREE;
5904 parser->qualifying_scope = NULL_TREE;
5905 parser->object_scope = NULL_TREE;
5906 *idk = CP_ID_KIND_NONE;
5908 /* Enter the scope corresponding to the type of the object
5909 given by the POSTFIX_EXPRESSION. */
5910 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5912 scope = TREE_TYPE (postfix_expression);
5913 /* According to the standard, no expression should ever have
5914 reference type. Unfortunately, we do not currently match
5915 the standard in this respect in that our internal representation
5916 of an expression may have reference type even when the standard
5917 says it does not. Therefore, we have to manually obtain the
5918 underlying type here. */
5919 scope = non_reference (scope);
5920 /* The type of the POSTFIX_EXPRESSION must be complete. */
5921 if (scope == unknown_type_node)
5923 error_at (location, "%qE does not have class type",
5924 postfix_expression);
5927 /* Unlike the object expression in other contexts, *this is not
5928 required to be of complete type for purposes of class member
5929 access (5.2.5) outside the member function body. */
5930 else if (scope != current_class_ref
5931 && !(processing_template_decl && scope == current_class_type))
5932 scope = complete_type_or_else (scope, NULL_TREE);
5933 /* Let the name lookup machinery know that we are processing a
5934 class member access expression. */
5935 parser->context->object_type = scope;
5936 /* If something went wrong, we want to be able to discern that case,
5937 as opposed to the case where there was no SCOPE due to the type
5938 of expression being dependent. */
5940 scope = error_mark_node;
5941 /* If the SCOPE was erroneous, make the various semantic analysis
5942 functions exit quickly -- and without issuing additional error
5944 if (scope == error_mark_node)
5945 postfix_expression = error_mark_node;
5948 /* Assume this expression is not a pseudo-destructor access. */
5949 pseudo_destructor_p = false;
5951 /* If the SCOPE is a scalar type, then, if this is a valid program,
5952 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5953 is type dependent, it can be pseudo-destructor-name or something else.
5954 Try to parse it as pseudo-destructor-name first. */
5955 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5960 cp_parser_parse_tentatively (parser);
5961 /* Parse the pseudo-destructor-name. */
5963 cp_parser_pseudo_destructor_name (parser, &s, &type);
5965 && (cp_parser_error_occurred (parser)
5966 || TREE_CODE (type) != TYPE_DECL
5967 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5968 cp_parser_abort_tentative_parse (parser);
5969 else if (cp_parser_parse_definitely (parser))
5971 pseudo_destructor_p = true;
5973 = finish_pseudo_destructor_expr (postfix_expression,
5974 s, TREE_TYPE (type));
5978 if (!pseudo_destructor_p)
5980 /* If the SCOPE is not a scalar type, we are looking at an
5981 ordinary class member access expression, rather than a
5982 pseudo-destructor-name. */
5984 cp_token *token = cp_lexer_peek_token (parser->lexer);
5985 /* Parse the id-expression. */
5986 name = (cp_parser_id_expression
5988 cp_parser_optional_template_keyword (parser),
5989 /*check_dependency_p=*/true,
5991 /*declarator_p=*/false,
5992 /*optional_p=*/false));
5993 /* In general, build a SCOPE_REF if the member name is qualified.
5994 However, if the name was not dependent and has already been
5995 resolved; there is no need to build the SCOPE_REF. For example;
5997 struct X { void f(); };
5998 template <typename T> void f(T* t) { t->X::f(); }
6000 Even though "t" is dependent, "X::f" is not and has been resolved
6001 to a BASELINK; there is no need to include scope information. */
6003 /* But we do need to remember that there was an explicit scope for
6004 virtual function calls. */
6006 *idk = CP_ID_KIND_QUALIFIED;
6008 /* If the name is a template-id that names a type, we will get a
6009 TYPE_DECL here. That is invalid code. */
6010 if (TREE_CODE (name) == TYPE_DECL)
6012 error_at (token->location, "invalid use of %qD", name);
6013 postfix_expression = error_mark_node;
6017 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6019 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6021 error_at (token->location, "%<%D::%D%> is not a class member",
6022 parser->scope, name);
6023 postfix_expression = error_mark_node;
6026 name = build_qualified_name (/*type=*/NULL_TREE,
6030 parser->scope = NULL_TREE;
6031 parser->qualifying_scope = NULL_TREE;
6032 parser->object_scope = NULL_TREE;
6034 if (scope && name && BASELINK_P (name))
6035 adjust_result_of_qualified_name_lookup
6036 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6038 = finish_class_member_access_expr (postfix_expression, name,
6040 tf_warning_or_error);
6044 /* We no longer need to look up names in the scope of the object on
6045 the left-hand side of the `.' or `->' operator. */
6046 parser->context->object_type = NULL_TREE;
6048 /* Outside of offsetof, these operators may not appear in
6049 constant-expressions. */
6051 && (cp_parser_non_integral_constant_expression
6052 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6053 postfix_expression = error_mark_node;
6055 return postfix_expression;
6058 /* Parse a parenthesized expression-list.
6061 assignment-expression
6062 expression-list, assignment-expression
6067 identifier, expression-list
6069 CAST_P is true if this expression is the target of a cast.
6071 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6074 Returns a vector of trees. Each element is a representation of an
6075 assignment-expression. NULL is returned if the ( and or ) are
6076 missing. An empty, but allocated, vector is returned on no
6077 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6078 if we are parsing an attribute list for an attribute that wants a
6079 plain identifier argument, normal_attr for an attribute that wants
6080 an expression, or non_attr if we aren't parsing an attribute list. If
6081 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6082 not all of the expressions in the list were constant. */
6084 static VEC(tree,gc) *
6085 cp_parser_parenthesized_expression_list (cp_parser* parser,
6086 int is_attribute_list,
6088 bool allow_expansion_p,
6089 bool *non_constant_p)
6091 VEC(tree,gc) *expression_list;
6092 bool fold_expr_p = is_attribute_list != non_attr;
6093 tree identifier = NULL_TREE;
6094 bool saved_greater_than_is_operator_p;
6096 /* Assume all the expressions will be constant. */
6098 *non_constant_p = false;
6100 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6103 expression_list = make_tree_vector ();
6105 /* Within a parenthesized expression, a `>' token is always
6106 the greater-than operator. */
6107 saved_greater_than_is_operator_p
6108 = parser->greater_than_is_operator_p;
6109 parser->greater_than_is_operator_p = true;
6111 /* Consume expressions until there are no more. */
6112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6117 /* At the beginning of attribute lists, check to see if the
6118 next token is an identifier. */
6119 if (is_attribute_list == id_attr
6120 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6124 /* Consume the identifier. */
6125 token = cp_lexer_consume_token (parser->lexer);
6126 /* Save the identifier. */
6127 identifier = token->u.value;
6131 bool expr_non_constant_p;
6133 /* Parse the next assignment-expression. */
6134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6136 /* A braced-init-list. */
6137 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6138 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6139 if (non_constant_p && expr_non_constant_p)
6140 *non_constant_p = true;
6142 else if (non_constant_p)
6144 expr = (cp_parser_constant_expression
6145 (parser, /*allow_non_constant_p=*/true,
6146 &expr_non_constant_p));
6147 if (expr_non_constant_p)
6148 *non_constant_p = true;
6151 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6154 expr = fold_non_dependent_expr (expr);
6156 /* If we have an ellipsis, then this is an expression
6158 if (allow_expansion_p
6159 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6161 /* Consume the `...'. */
6162 cp_lexer_consume_token (parser->lexer);
6164 /* Build the argument pack. */
6165 expr = make_pack_expansion (expr);
6168 /* Add it to the list. We add error_mark_node
6169 expressions to the list, so that we can still tell if
6170 the correct form for a parenthesized expression-list
6171 is found. That gives better errors. */
6172 VEC_safe_push (tree, gc, expression_list, expr);
6174 if (expr == error_mark_node)
6178 /* After the first item, attribute lists look the same as
6179 expression lists. */
6180 is_attribute_list = non_attr;
6183 /* If the next token isn't a `,', then we are done. */
6184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6187 /* Otherwise, consume the `,' and keep going. */
6188 cp_lexer_consume_token (parser->lexer);
6191 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6196 /* We try and resync to an unnested comma, as that will give the
6197 user better diagnostics. */
6198 ending = cp_parser_skip_to_closing_parenthesis (parser,
6199 /*recovering=*/true,
6201 /*consume_paren=*/true);
6206 parser->greater_than_is_operator_p
6207 = saved_greater_than_is_operator_p;
6212 parser->greater_than_is_operator_p
6213 = saved_greater_than_is_operator_p;
6216 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6218 return expression_list;
6221 /* Parse a pseudo-destructor-name.
6223 pseudo-destructor-name:
6224 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6225 :: [opt] nested-name-specifier template template-id :: ~ type-name
6226 :: [opt] nested-name-specifier [opt] ~ type-name
6228 If either of the first two productions is used, sets *SCOPE to the
6229 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6230 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6231 or ERROR_MARK_NODE if the parse fails. */
6234 cp_parser_pseudo_destructor_name (cp_parser* parser,
6238 bool nested_name_specifier_p;
6240 /* Assume that things will not work out. */
6241 *type = error_mark_node;
6243 /* Look for the optional `::' operator. */
6244 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6245 /* Look for the optional nested-name-specifier. */
6246 nested_name_specifier_p
6247 = (cp_parser_nested_name_specifier_opt (parser,
6248 /*typename_keyword_p=*/false,
6249 /*check_dependency_p=*/true,
6251 /*is_declaration=*/false)
6253 /* Now, if we saw a nested-name-specifier, we might be doing the
6254 second production. */
6255 if (nested_name_specifier_p
6256 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6258 /* Consume the `template' keyword. */
6259 cp_lexer_consume_token (parser->lexer);
6260 /* Parse the template-id. */
6261 cp_parser_template_id (parser,
6262 /*template_keyword_p=*/true,
6263 /*check_dependency_p=*/false,
6264 /*is_declaration=*/true);
6265 /* Look for the `::' token. */
6266 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6268 /* If the next token is not a `~', then there might be some
6269 additional qualification. */
6270 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6272 /* At this point, we're looking for "type-name :: ~". The type-name
6273 must not be a class-name, since this is a pseudo-destructor. So,
6274 it must be either an enum-name, or a typedef-name -- both of which
6275 are just identifiers. So, we peek ahead to check that the "::"
6276 and "~" tokens are present; if they are not, then we can avoid
6277 calling type_name. */
6278 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6279 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6280 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6282 cp_parser_error (parser, "non-scalar type");
6286 /* Look for the type-name. */
6287 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6288 if (*scope == error_mark_node)
6291 /* Look for the `::' token. */
6292 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6297 /* Look for the `~'. */
6298 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6300 /* Once we see the ~, this has to be a pseudo-destructor. */
6301 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6302 cp_parser_commit_to_tentative_parse (parser);
6304 /* Look for the type-name again. We are not responsible for
6305 checking that it matches the first type-name. */
6306 *type = cp_parser_nonclass_name (parser);
6309 /* Parse a unary-expression.
6315 unary-operator cast-expression
6316 sizeof unary-expression
6318 alignof ( type-id ) [C++0x]
6325 __extension__ cast-expression
6326 __alignof__ unary-expression
6327 __alignof__ ( type-id )
6328 alignof unary-expression [C++0x]
6329 __real__ cast-expression
6330 __imag__ cast-expression
6333 ADDRESS_P is true iff the unary-expression is appearing as the
6334 operand of the `&' operator. CAST_P is true if this expression is
6335 the target of a cast.
6337 Returns a representation of the expression. */
6340 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6344 enum tree_code unary_operator;
6346 /* Peek at the next token. */
6347 token = cp_lexer_peek_token (parser->lexer);
6348 /* Some keywords give away the kind of expression. */
6349 if (token->type == CPP_KEYWORD)
6351 enum rid keyword = token->keyword;
6361 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6362 /* Consume the token. */
6363 cp_lexer_consume_token (parser->lexer);
6364 /* Parse the operand. */
6365 operand = cp_parser_sizeof_operand (parser, keyword);
6367 if (TYPE_P (operand))
6368 return cxx_sizeof_or_alignof_type (operand, op, true);
6371 /* ISO C++ defines alignof only with types, not with
6372 expressions. So pedwarn if alignof is used with a non-
6373 type expression. However, __alignof__ is ok. */
6374 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6375 pedwarn (token->location, OPT_pedantic,
6376 "ISO C++ does not allow %<alignof%> "
6379 return cxx_sizeof_or_alignof_expr (operand, op, true);
6384 return cp_parser_new_expression (parser);
6387 return cp_parser_delete_expression (parser);
6391 /* The saved value of the PEDANTIC flag. */
6395 /* Save away the PEDANTIC flag. */
6396 cp_parser_extension_opt (parser, &saved_pedantic);
6397 /* Parse the cast-expression. */
6398 expr = cp_parser_simple_cast_expression (parser);
6399 /* Restore the PEDANTIC flag. */
6400 pedantic = saved_pedantic;
6410 /* Consume the `__real__' or `__imag__' token. */
6411 cp_lexer_consume_token (parser->lexer);
6412 /* Parse the cast-expression. */
6413 expression = cp_parser_simple_cast_expression (parser);
6414 /* Create the complete representation. */
6415 return build_x_unary_op ((keyword == RID_REALPART
6416 ? REALPART_EXPR : IMAGPART_EXPR),
6418 tf_warning_or_error);
6422 case RID_TRANSACTION_ATOMIC:
6423 case RID_TRANSACTION_RELAXED:
6424 return cp_parser_transaction_expression (parser, keyword);
6429 const char *saved_message;
6430 bool saved_integral_constant_expression_p;
6431 bool saved_non_integral_constant_expression_p;
6432 bool saved_greater_than_is_operator_p;
6434 cp_lexer_consume_token (parser->lexer);
6435 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6437 saved_message = parser->type_definition_forbidden_message;
6438 parser->type_definition_forbidden_message
6439 = G_("types may not be defined in %<noexcept%> expressions");
6441 saved_integral_constant_expression_p
6442 = parser->integral_constant_expression_p;
6443 saved_non_integral_constant_expression_p
6444 = parser->non_integral_constant_expression_p;
6445 parser->integral_constant_expression_p = false;
6447 saved_greater_than_is_operator_p
6448 = parser->greater_than_is_operator_p;
6449 parser->greater_than_is_operator_p = true;
6451 ++cp_unevaluated_operand;
6452 ++c_inhibit_evaluation_warnings;
6453 expr = cp_parser_expression (parser, false, NULL);
6454 --c_inhibit_evaluation_warnings;
6455 --cp_unevaluated_operand;
6457 parser->greater_than_is_operator_p
6458 = saved_greater_than_is_operator_p;
6460 parser->integral_constant_expression_p
6461 = saved_integral_constant_expression_p;
6462 parser->non_integral_constant_expression_p
6463 = saved_non_integral_constant_expression_p;
6465 parser->type_definition_forbidden_message = saved_message;
6467 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6468 return finish_noexcept_expr (expr, tf_warning_or_error);
6476 /* Look for the `:: new' and `:: delete', which also signal the
6477 beginning of a new-expression, or delete-expression,
6478 respectively. If the next token is `::', then it might be one of
6480 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6484 /* See if the token after the `::' is one of the keywords in
6485 which we're interested. */
6486 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6487 /* If it's `new', we have a new-expression. */
6488 if (keyword == RID_NEW)
6489 return cp_parser_new_expression (parser);
6490 /* Similarly, for `delete'. */
6491 else if (keyword == RID_DELETE)
6492 return cp_parser_delete_expression (parser);
6495 /* Look for a unary operator. */
6496 unary_operator = cp_parser_unary_operator (token);
6497 /* The `++' and `--' operators can be handled similarly, even though
6498 they are not technically unary-operators in the grammar. */
6499 if (unary_operator == ERROR_MARK)
6501 if (token->type == CPP_PLUS_PLUS)
6502 unary_operator = PREINCREMENT_EXPR;
6503 else if (token->type == CPP_MINUS_MINUS)
6504 unary_operator = PREDECREMENT_EXPR;
6505 /* Handle the GNU address-of-label extension. */
6506 else if (cp_parser_allow_gnu_extensions_p (parser)
6507 && token->type == CPP_AND_AND)
6511 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6513 /* Consume the '&&' token. */
6514 cp_lexer_consume_token (parser->lexer);
6515 /* Look for the identifier. */
6516 identifier = cp_parser_identifier (parser);
6517 /* Create an expression representing the address. */
6518 expression = finish_label_address_expr (identifier, loc);
6519 if (cp_parser_non_integral_constant_expression (parser,
6521 expression = error_mark_node;
6525 if (unary_operator != ERROR_MARK)
6527 tree cast_expression;
6528 tree expression = error_mark_node;
6529 non_integral_constant non_constant_p = NIC_NONE;
6531 /* Consume the operator token. */
6532 token = cp_lexer_consume_token (parser->lexer);
6533 /* Parse the cast-expression. */
6535 = cp_parser_cast_expression (parser,
6536 unary_operator == ADDR_EXPR,
6537 /*cast_p=*/false, pidk);
6538 /* Now, build an appropriate representation. */
6539 switch (unary_operator)
6542 non_constant_p = NIC_STAR;
6543 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6544 tf_warning_or_error);
6548 non_constant_p = NIC_ADDR;
6551 expression = build_x_unary_op (unary_operator, cast_expression,
6552 tf_warning_or_error);
6555 case PREINCREMENT_EXPR:
6556 case PREDECREMENT_EXPR:
6557 non_constant_p = unary_operator == PREINCREMENT_EXPR
6558 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6560 case UNARY_PLUS_EXPR:
6562 case TRUTH_NOT_EXPR:
6563 expression = finish_unary_op_expr (unary_operator, cast_expression);
6570 if (non_constant_p != NIC_NONE
6571 && cp_parser_non_integral_constant_expression (parser,
6573 expression = error_mark_node;
6578 return cp_parser_postfix_expression (parser, address_p, cast_p,
6579 /*member_access_only_p=*/false,
6583 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6584 unary-operator, the corresponding tree code is returned. */
6586 static enum tree_code
6587 cp_parser_unary_operator (cp_token* token)
6589 switch (token->type)
6592 return INDIRECT_REF;
6598 return UNARY_PLUS_EXPR;
6604 return TRUTH_NOT_EXPR;
6607 return BIT_NOT_EXPR;
6614 /* Parse a new-expression.
6617 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6618 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6620 Returns a representation of the expression. */
6623 cp_parser_new_expression (cp_parser* parser)
6625 bool global_scope_p;
6626 VEC(tree,gc) *placement;
6628 VEC(tree,gc) *initializer;
6632 /* Look for the optional `::' operator. */
6634 = (cp_parser_global_scope_opt (parser,
6635 /*current_scope_valid_p=*/false)
6637 /* Look for the `new' operator. */
6638 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6639 /* There's no easy way to tell a new-placement from the
6640 `( type-id )' construct. */
6641 cp_parser_parse_tentatively (parser);
6642 /* Look for a new-placement. */
6643 placement = cp_parser_new_placement (parser);
6644 /* If that didn't work out, there's no new-placement. */
6645 if (!cp_parser_parse_definitely (parser))
6647 if (placement != NULL)
6648 release_tree_vector (placement);
6652 /* If the next token is a `(', then we have a parenthesized
6654 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6657 /* Consume the `('. */
6658 cp_lexer_consume_token (parser->lexer);
6659 /* Parse the type-id. */
6660 type = cp_parser_type_id (parser);
6661 /* Look for the closing `)'. */
6662 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6663 token = cp_lexer_peek_token (parser->lexer);
6664 /* There should not be a direct-new-declarator in this production,
6665 but GCC used to allowed this, so we check and emit a sensible error
6666 message for this case. */
6667 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6669 error_at (token->location,
6670 "array bound forbidden after parenthesized type-id");
6671 inform (token->location,
6672 "try removing the parentheses around the type-id");
6673 cp_parser_direct_new_declarator (parser);
6677 /* Otherwise, there must be a new-type-id. */
6679 type = cp_parser_new_type_id (parser, &nelts);
6681 /* If the next token is a `(' or '{', then we have a new-initializer. */
6682 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6683 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6684 initializer = cp_parser_new_initializer (parser);
6688 /* A new-expression may not appear in an integral constant
6690 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6691 ret = error_mark_node;
6694 /* Create a representation of the new-expression. */
6695 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6696 tf_warning_or_error);
6699 if (placement != NULL)
6700 release_tree_vector (placement);
6701 if (initializer != NULL)
6702 release_tree_vector (initializer);
6707 /* Parse a new-placement.
6712 Returns the same representation as for an expression-list. */
6714 static VEC(tree,gc) *
6715 cp_parser_new_placement (cp_parser* parser)
6717 VEC(tree,gc) *expression_list;
6719 /* Parse the expression-list. */
6720 expression_list = (cp_parser_parenthesized_expression_list
6721 (parser, non_attr, /*cast_p=*/false,
6722 /*allow_expansion_p=*/true,
6723 /*non_constant_p=*/NULL));
6725 return expression_list;
6728 /* Parse a new-type-id.
6731 type-specifier-seq new-declarator [opt]
6733 Returns the TYPE allocated. If the new-type-id indicates an array
6734 type, *NELTS is set to the number of elements in the last array
6735 bound; the TYPE will not include the last array bound. */
6738 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6740 cp_decl_specifier_seq type_specifier_seq;
6741 cp_declarator *new_declarator;
6742 cp_declarator *declarator;
6743 cp_declarator *outer_declarator;
6744 const char *saved_message;
6747 /* The type-specifier sequence must not contain type definitions.
6748 (It cannot contain declarations of new types either, but if they
6749 are not definitions we will catch that because they are not
6751 saved_message = parser->type_definition_forbidden_message;
6752 parser->type_definition_forbidden_message
6753 = G_("types may not be defined in a new-type-id");
6754 /* Parse the type-specifier-seq. */
6755 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6756 /*is_trailing_return=*/false,
6757 &type_specifier_seq);
6758 /* Restore the old message. */
6759 parser->type_definition_forbidden_message = saved_message;
6760 /* Parse the new-declarator. */
6761 new_declarator = cp_parser_new_declarator_opt (parser);
6763 /* Determine the number of elements in the last array dimension, if
6766 /* Skip down to the last array dimension. */
6767 declarator = new_declarator;
6768 outer_declarator = NULL;
6769 while (declarator && (declarator->kind == cdk_pointer
6770 || declarator->kind == cdk_ptrmem))
6772 outer_declarator = declarator;
6773 declarator = declarator->declarator;
6776 && declarator->kind == cdk_array
6777 && declarator->declarator
6778 && declarator->declarator->kind == cdk_array)
6780 outer_declarator = declarator;
6781 declarator = declarator->declarator;
6784 if (declarator && declarator->kind == cdk_array)
6786 *nelts = declarator->u.array.bounds;
6787 if (*nelts == error_mark_node)
6788 *nelts = integer_one_node;
6790 if (outer_declarator)
6791 outer_declarator->declarator = declarator->declarator;
6793 new_declarator = NULL;
6796 type = groktypename (&type_specifier_seq, new_declarator, false);
6800 /* Parse an (optional) new-declarator.
6803 ptr-operator new-declarator [opt]
6804 direct-new-declarator
6806 Returns the declarator. */
6808 static cp_declarator *
6809 cp_parser_new_declarator_opt (cp_parser* parser)
6811 enum tree_code code;
6813 cp_cv_quals cv_quals;
6815 /* We don't know if there's a ptr-operator next, or not. */
6816 cp_parser_parse_tentatively (parser);
6817 /* Look for a ptr-operator. */
6818 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6819 /* If that worked, look for more new-declarators. */
6820 if (cp_parser_parse_definitely (parser))
6822 cp_declarator *declarator;
6824 /* Parse another optional declarator. */
6825 declarator = cp_parser_new_declarator_opt (parser);
6827 return cp_parser_make_indirect_declarator
6828 (code, type, cv_quals, declarator);
6831 /* If the next token is a `[', there is a direct-new-declarator. */
6832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6833 return cp_parser_direct_new_declarator (parser);
6838 /* Parse a direct-new-declarator.
6840 direct-new-declarator:
6842 direct-new-declarator [constant-expression]
6846 static cp_declarator *
6847 cp_parser_direct_new_declarator (cp_parser* parser)
6849 cp_declarator *declarator = NULL;
6855 /* Look for the opening `['. */
6856 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6857 /* The first expression is not required to be constant. */
6860 cp_token *token = cp_lexer_peek_token (parser->lexer);
6861 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6862 /* The standard requires that the expression have integral
6863 type. DR 74 adds enumeration types. We believe that the
6864 real intent is that these expressions be handled like the
6865 expression in a `switch' condition, which also allows
6866 classes with a single conversion to integral or
6867 enumeration type. */
6868 if (!processing_template_decl)
6871 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6876 error_at (token->location,
6877 "expression in new-declarator must have integral "
6878 "or enumeration type");
6879 expression = error_mark_node;
6883 /* But all the other expressions must be. */
6886 = cp_parser_constant_expression (parser,
6887 /*allow_non_constant=*/false,
6889 /* Look for the closing `]'. */
6890 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6892 /* Add this bound to the declarator. */
6893 declarator = make_array_declarator (declarator, expression);
6895 /* If the next token is not a `[', then there are no more
6897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6904 /* Parse a new-initializer.
6907 ( expression-list [opt] )
6910 Returns a representation of the expression-list. */
6912 static VEC(tree,gc) *
6913 cp_parser_new_initializer (cp_parser* parser)
6915 VEC(tree,gc) *expression_list;
6917 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6920 bool expr_non_constant_p;
6921 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6922 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6923 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6924 expression_list = make_tree_vector_single (t);
6927 expression_list = (cp_parser_parenthesized_expression_list
6928 (parser, non_attr, /*cast_p=*/false,
6929 /*allow_expansion_p=*/true,
6930 /*non_constant_p=*/NULL));
6932 return expression_list;
6935 /* Parse a delete-expression.
6938 :: [opt] delete cast-expression
6939 :: [opt] delete [ ] cast-expression
6941 Returns a representation of the expression. */
6944 cp_parser_delete_expression (cp_parser* parser)
6946 bool global_scope_p;
6950 /* Look for the optional `::' operator. */
6952 = (cp_parser_global_scope_opt (parser,
6953 /*current_scope_valid_p=*/false)
6955 /* Look for the `delete' keyword. */
6956 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6957 /* See if the array syntax is in use. */
6958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6960 /* Consume the `[' token. */
6961 cp_lexer_consume_token (parser->lexer);
6962 /* Look for the `]' token. */
6963 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6964 /* Remember that this is the `[]' construct. */
6970 /* Parse the cast-expression. */
6971 expression = cp_parser_simple_cast_expression (parser);
6973 /* A delete-expression may not appear in an integral constant
6975 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6976 return error_mark_node;
6978 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6979 tf_warning_or_error);
6982 /* Returns true if TOKEN may start a cast-expression and false
6986 cp_parser_token_starts_cast_expression (cp_token *token)
6988 switch (token->type)
6994 case CPP_CLOSE_SQUARE:
6995 case CPP_CLOSE_PAREN:
6996 case CPP_CLOSE_BRACE:
7000 case CPP_DEREF_STAR:
7008 case CPP_GREATER_EQ:
7028 /* '[' may start a primary-expression in obj-c++. */
7029 case CPP_OPEN_SQUARE:
7030 return c_dialect_objc ();
7037 /* Parse a cast-expression.
7041 ( type-id ) cast-expression
7043 ADDRESS_P is true iff the unary-expression is appearing as the
7044 operand of the `&' operator. CAST_P is true if this expression is
7045 the target of a cast.
7047 Returns a representation of the expression. */
7050 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7053 /* If it's a `(', then we might be looking at a cast. */
7054 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7056 tree type = NULL_TREE;
7057 tree expr = NULL_TREE;
7058 bool compound_literal_p;
7059 const char *saved_message;
7061 /* There's no way to know yet whether or not this is a cast.
7062 For example, `(int (3))' is a unary-expression, while `(int)
7063 3' is a cast. So, we resort to parsing tentatively. */
7064 cp_parser_parse_tentatively (parser);
7065 /* Types may not be defined in a cast. */
7066 saved_message = parser->type_definition_forbidden_message;
7067 parser->type_definition_forbidden_message
7068 = G_("types may not be defined in casts");
7069 /* Consume the `('. */
7070 cp_lexer_consume_token (parser->lexer);
7071 /* A very tricky bit is that `(struct S) { 3 }' is a
7072 compound-literal (which we permit in C++ as an extension).
7073 But, that construct is not a cast-expression -- it is a
7074 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7075 is legal; if the compound-literal were a cast-expression,
7076 you'd need an extra set of parentheses.) But, if we parse
7077 the type-id, and it happens to be a class-specifier, then we
7078 will commit to the parse at that point, because we cannot
7079 undo the action that is done when creating a new class. So,
7080 then we cannot back up and do a postfix-expression.
7082 Therefore, we scan ahead to the closing `)', and check to see
7083 if the token after the `)' is a `{'. If so, we are not
7084 looking at a cast-expression.
7086 Save tokens so that we can put them back. */
7087 cp_lexer_save_tokens (parser->lexer);
7088 /* Skip tokens until the next token is a closing parenthesis.
7089 If we find the closing `)', and the next token is a `{', then
7090 we are looking at a compound-literal. */
7092 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7093 /*consume_paren=*/true)
7094 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7095 /* Roll back the tokens we skipped. */
7096 cp_lexer_rollback_tokens (parser->lexer);
7097 /* If we were looking at a compound-literal, simulate an error
7098 so that the call to cp_parser_parse_definitely below will
7100 if (compound_literal_p)
7101 cp_parser_simulate_error (parser);
7104 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7105 parser->in_type_id_in_expr_p = true;
7106 /* Look for the type-id. */
7107 type = cp_parser_type_id (parser);
7108 /* Look for the closing `)'. */
7109 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7110 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7113 /* Restore the saved message. */
7114 parser->type_definition_forbidden_message = saved_message;
7116 /* At this point this can only be either a cast or a
7117 parenthesized ctor such as `(T ())' that looks like a cast to
7118 function returning T. */
7119 if (!cp_parser_error_occurred (parser)
7120 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7123 cp_parser_parse_definitely (parser);
7124 expr = cp_parser_cast_expression (parser,
7125 /*address_p=*/false,
7126 /*cast_p=*/true, pidk);
7128 /* Warn about old-style casts, if so requested. */
7129 if (warn_old_style_cast
7130 && !in_system_header
7131 && !VOID_TYPE_P (type)
7132 && current_lang_name != lang_name_c)
7133 warning (OPT_Wold_style_cast, "use of old-style cast");
7135 /* Only type conversions to integral or enumeration types
7136 can be used in constant-expressions. */
7137 if (!cast_valid_in_integral_constant_expression_p (type)
7138 && cp_parser_non_integral_constant_expression (parser,
7140 return error_mark_node;
7142 /* Perform the cast. */
7143 expr = build_c_cast (input_location, type, expr);
7147 cp_parser_abort_tentative_parse (parser);
7150 /* If we get here, then it's not a cast, so it must be a
7151 unary-expression. */
7152 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7155 /* Parse a binary expression of the general form:
7159 pm-expression .* cast-expression
7160 pm-expression ->* cast-expression
7162 multiplicative-expression:
7164 multiplicative-expression * pm-expression
7165 multiplicative-expression / pm-expression
7166 multiplicative-expression % pm-expression
7168 additive-expression:
7169 multiplicative-expression
7170 additive-expression + multiplicative-expression
7171 additive-expression - multiplicative-expression
7175 shift-expression << additive-expression
7176 shift-expression >> additive-expression
7178 relational-expression:
7180 relational-expression < shift-expression
7181 relational-expression > shift-expression
7182 relational-expression <= shift-expression
7183 relational-expression >= shift-expression
7187 relational-expression:
7188 relational-expression <? shift-expression
7189 relational-expression >? shift-expression
7191 equality-expression:
7192 relational-expression
7193 equality-expression == relational-expression
7194 equality-expression != relational-expression
7198 and-expression & equality-expression
7200 exclusive-or-expression:
7202 exclusive-or-expression ^ and-expression
7204 inclusive-or-expression:
7205 exclusive-or-expression
7206 inclusive-or-expression | exclusive-or-expression
7208 logical-and-expression:
7209 inclusive-or-expression
7210 logical-and-expression && inclusive-or-expression
7212 logical-or-expression:
7213 logical-and-expression
7214 logical-or-expression || logical-and-expression
7216 All these are implemented with a single function like:
7219 simple-cast-expression
7220 binary-expression <token> binary-expression
7222 CAST_P is true if this expression is the target of a cast.
7224 The binops_by_token map is used to get the tree codes for each <token> type.
7225 binary-expressions are associated according to a precedence table. */
7227 #define TOKEN_PRECEDENCE(token) \
7228 (((token->type == CPP_GREATER \
7229 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7230 && !parser->greater_than_is_operator_p) \
7231 ? PREC_NOT_OPERATOR \
7232 : binops_by_token[token->type].prec)
7235 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7236 bool no_toplevel_fold_p,
7237 enum cp_parser_prec prec,
7240 cp_parser_expression_stack stack;
7241 cp_parser_expression_stack_entry *sp = &stack[0];
7244 enum tree_code tree_type, lhs_type, rhs_type;
7245 enum cp_parser_prec new_prec, lookahead_prec;
7248 /* Parse the first expression. */
7249 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7250 lhs_type = ERROR_MARK;
7254 /* Get an operator token. */
7255 token = cp_lexer_peek_token (parser->lexer);
7257 if (warn_cxx0x_compat
7258 && token->type == CPP_RSHIFT
7259 && !parser->greater_than_is_operator_p)
7261 if (warning_at (token->location, OPT_Wc__0x_compat,
7262 "%<>>%> operator is treated as"
7263 " two right angle brackets in C++11"))
7264 inform (token->location,
7265 "suggest parentheses around %<>>%> expression");
7268 new_prec = TOKEN_PRECEDENCE (token);
7270 /* Popping an entry off the stack means we completed a subexpression:
7271 - either we found a token which is not an operator (`>' where it is not
7272 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7273 will happen repeatedly;
7274 - or, we found an operator which has lower priority. This is the case
7275 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7277 if (new_prec <= prec)
7286 tree_type = binops_by_token[token->type].tree_type;
7288 /* We used the operator token. */
7289 cp_lexer_consume_token (parser->lexer);
7291 /* For "false && x" or "true || x", x will never be executed;
7292 disable warnings while evaluating it. */
7293 if (tree_type == TRUTH_ANDIF_EXPR)
7294 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7295 else if (tree_type == TRUTH_ORIF_EXPR)
7296 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7298 /* Extract another operand. It may be the RHS of this expression
7299 or the LHS of a new, higher priority expression. */
7300 rhs = cp_parser_simple_cast_expression (parser);
7301 rhs_type = ERROR_MARK;
7303 /* Get another operator token. Look up its precedence to avoid
7304 building a useless (immediately popped) stack entry for common
7305 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7306 token = cp_lexer_peek_token (parser->lexer);
7307 lookahead_prec = TOKEN_PRECEDENCE (token);
7308 if (lookahead_prec > new_prec)
7310 /* ... and prepare to parse the RHS of the new, higher priority
7311 expression. Since precedence levels on the stack are
7312 monotonically increasing, we do not have to care about
7315 sp->tree_type = tree_type;
7317 sp->lhs_type = lhs_type;
7320 lhs_type = rhs_type;
7322 new_prec = lookahead_prec;
7326 lookahead_prec = new_prec;
7327 /* If the stack is not empty, we have parsed into LHS the right side
7328 (`4' in the example above) of an expression we had suspended.
7329 We can use the information on the stack to recover the LHS (`3')
7330 from the stack together with the tree code (`MULT_EXPR'), and
7331 the precedence of the higher level subexpression
7332 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7333 which will be used to actually build the additive expression. */
7336 tree_type = sp->tree_type;
7338 rhs_type = lhs_type;
7340 lhs_type = sp->lhs_type;
7343 /* Undo the disabling of warnings done above. */
7344 if (tree_type == TRUTH_ANDIF_EXPR)
7345 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7346 else if (tree_type == TRUTH_ORIF_EXPR)
7347 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7350 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7351 ERROR_MARK for everything that is not a binary expression.
7352 This makes warn_about_parentheses miss some warnings that
7353 involve unary operators. For unary expressions we should
7354 pass the correct tree_code unless the unary expression was
7355 surrounded by parentheses.
7357 if (no_toplevel_fold_p
7358 && lookahead_prec <= prec
7360 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7361 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7363 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7364 &overload, tf_warning_or_error);
7365 lhs_type = tree_type;
7367 /* If the binary operator required the use of an overloaded operator,
7368 then this expression cannot be an integral constant-expression.
7369 An overloaded operator can be used even if both operands are
7370 otherwise permissible in an integral constant-expression if at
7371 least one of the operands is of enumeration type. */
7374 && cp_parser_non_integral_constant_expression (parser,
7376 return error_mark_node;
7383 /* Parse the `? expression : assignment-expression' part of a
7384 conditional-expression. The LOGICAL_OR_EXPR is the
7385 logical-or-expression that started the conditional-expression.
7386 Returns a representation of the entire conditional-expression.
7388 This routine is used by cp_parser_assignment_expression.
7390 ? expression : assignment-expression
7394 ? : assignment-expression */
7397 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7400 tree assignment_expr;
7401 struct cp_token *token;
7403 /* Consume the `?' token. */
7404 cp_lexer_consume_token (parser->lexer);
7405 token = cp_lexer_peek_token (parser->lexer);
7406 if (cp_parser_allow_gnu_extensions_p (parser)
7407 && token->type == CPP_COLON)
7409 pedwarn (token->location, OPT_pedantic,
7410 "ISO C++ does not allow ?: with omitted middle operand");
7411 /* Implicit true clause. */
7413 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7414 warn_for_omitted_condop (token->location, logical_or_expr);
7418 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7419 parser->colon_corrects_to_scope_p = false;
7420 /* Parse the expression. */
7421 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7422 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7423 c_inhibit_evaluation_warnings +=
7424 ((logical_or_expr == truthvalue_true_node)
7425 - (logical_or_expr == truthvalue_false_node));
7426 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7429 /* The next token should be a `:'. */
7430 cp_parser_require (parser, CPP_COLON, RT_COLON);
7431 /* Parse the assignment-expression. */
7432 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7433 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7435 /* Build the conditional-expression. */
7436 return build_x_conditional_expr (logical_or_expr,
7439 tf_warning_or_error);
7442 /* Parse an assignment-expression.
7444 assignment-expression:
7445 conditional-expression
7446 logical-or-expression assignment-operator assignment_expression
7449 CAST_P is true if this expression is the target of a cast.
7451 Returns a representation for the expression. */
7454 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7459 /* If the next token is the `throw' keyword, then we're looking at
7460 a throw-expression. */
7461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7462 expr = cp_parser_throw_expression (parser);
7463 /* Otherwise, it must be that we are looking at a
7464 logical-or-expression. */
7467 /* Parse the binary expressions (logical-or-expression). */
7468 expr = cp_parser_binary_expression (parser, cast_p, false,
7469 PREC_NOT_OPERATOR, pidk);
7470 /* If the next token is a `?' then we're actually looking at a
7471 conditional-expression. */
7472 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7473 return cp_parser_question_colon_clause (parser, expr);
7476 enum tree_code assignment_operator;
7478 /* If it's an assignment-operator, we're using the second
7481 = cp_parser_assignment_operator_opt (parser);
7482 if (assignment_operator != ERROR_MARK)
7484 bool non_constant_p;
7486 /* Parse the right-hand side of the assignment. */
7487 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7489 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7490 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7492 /* An assignment may not appear in a
7493 constant-expression. */
7494 if (cp_parser_non_integral_constant_expression (parser,
7496 return error_mark_node;
7497 /* Build the assignment expression. */
7498 expr = build_x_modify_expr (expr,
7499 assignment_operator,
7501 tf_warning_or_error);
7509 /* Parse an (optional) assignment-operator.
7511 assignment-operator: one of
7512 = *= /= %= += -= >>= <<= &= ^= |=
7516 assignment-operator: one of
7519 If the next token is an assignment operator, the corresponding tree
7520 code is returned, and the token is consumed. For example, for
7521 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7522 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7523 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7524 operator, ERROR_MARK is returned. */
7526 static enum tree_code
7527 cp_parser_assignment_operator_opt (cp_parser* parser)
7532 /* Peek at the next token. */
7533 token = cp_lexer_peek_token (parser->lexer);
7535 switch (token->type)
7546 op = TRUNC_DIV_EXPR;
7550 op = TRUNC_MOD_EXPR;
7582 /* Nothing else is an assignment operator. */
7586 /* If it was an assignment operator, consume it. */
7587 if (op != ERROR_MARK)
7588 cp_lexer_consume_token (parser->lexer);
7593 /* Parse an expression.
7596 assignment-expression
7597 expression , assignment-expression
7599 CAST_P is true if this expression is the target of a cast.
7601 Returns a representation of the expression. */
7604 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7606 tree expression = NULL_TREE;
7610 tree assignment_expression;
7612 /* Parse the next assignment-expression. */
7613 assignment_expression
7614 = cp_parser_assignment_expression (parser, cast_p, pidk);
7615 /* If this is the first assignment-expression, we can just
7618 expression = assignment_expression;
7620 expression = build_x_compound_expr (expression,
7621 assignment_expression,
7622 tf_warning_or_error);
7623 /* If the next token is not a comma, then we are done with the
7625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7627 /* Consume the `,'. */
7628 cp_lexer_consume_token (parser->lexer);
7629 /* A comma operator cannot appear in a constant-expression. */
7630 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7631 expression = error_mark_node;
7637 /* Parse a constant-expression.
7639 constant-expression:
7640 conditional-expression
7642 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7643 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7644 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7645 is false, NON_CONSTANT_P should be NULL. */
7648 cp_parser_constant_expression (cp_parser* parser,
7649 bool allow_non_constant_p,
7650 bool *non_constant_p)
7652 bool saved_integral_constant_expression_p;
7653 bool saved_allow_non_integral_constant_expression_p;
7654 bool saved_non_integral_constant_expression_p;
7657 /* It might seem that we could simply parse the
7658 conditional-expression, and then check to see if it were
7659 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7660 one that the compiler can figure out is constant, possibly after
7661 doing some simplifications or optimizations. The standard has a
7662 precise definition of constant-expression, and we must honor
7663 that, even though it is somewhat more restrictive.
7669 is not a legal declaration, because `(2, 3)' is not a
7670 constant-expression. The `,' operator is forbidden in a
7671 constant-expression. However, GCC's constant-folding machinery
7672 will fold this operation to an INTEGER_CST for `3'. */
7674 /* Save the old settings. */
7675 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7676 saved_allow_non_integral_constant_expression_p
7677 = parser->allow_non_integral_constant_expression_p;
7678 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7679 /* We are now parsing a constant-expression. */
7680 parser->integral_constant_expression_p = true;
7681 parser->allow_non_integral_constant_expression_p
7682 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7683 parser->non_integral_constant_expression_p = false;
7684 /* Although the grammar says "conditional-expression", we parse an
7685 "assignment-expression", which also permits "throw-expression"
7686 and the use of assignment operators. In the case that
7687 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7688 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7689 actually essential that we look for an assignment-expression.
7690 For example, cp_parser_initializer_clauses uses this function to
7691 determine whether a particular assignment-expression is in fact
7693 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7694 /* Restore the old settings. */
7695 parser->integral_constant_expression_p
7696 = saved_integral_constant_expression_p;
7697 parser->allow_non_integral_constant_expression_p
7698 = saved_allow_non_integral_constant_expression_p;
7699 if (cxx_dialect >= cxx0x)
7701 /* Require an rvalue constant expression here; that's what our
7702 callers expect. Reference constant expressions are handled
7703 separately in e.g. cp_parser_template_argument. */
7704 bool is_const = potential_rvalue_constant_expression (expression);
7705 parser->non_integral_constant_expression_p = !is_const;
7706 if (!is_const && !allow_non_constant_p)
7707 require_potential_rvalue_constant_expression (expression);
7709 if (allow_non_constant_p)
7710 *non_constant_p = parser->non_integral_constant_expression_p;
7711 parser->non_integral_constant_expression_p
7712 = saved_non_integral_constant_expression_p;
7717 /* Parse __builtin_offsetof.
7719 offsetof-expression:
7720 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7722 offsetof-member-designator:
7724 | offsetof-member-designator "." id-expression
7725 | offsetof-member-designator "[" expression "]"
7726 | offsetof-member-designator "->" id-expression */
7729 cp_parser_builtin_offsetof (cp_parser *parser)
7731 int save_ice_p, save_non_ice_p;
7736 /* We're about to accept non-integral-constant things, but will
7737 definitely yield an integral constant expression. Save and
7738 restore these values around our local parsing. */
7739 save_ice_p = parser->integral_constant_expression_p;
7740 save_non_ice_p = parser->non_integral_constant_expression_p;
7742 /* Consume the "__builtin_offsetof" token. */
7743 cp_lexer_consume_token (parser->lexer);
7744 /* Consume the opening `('. */
7745 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7746 /* Parse the type-id. */
7747 type = cp_parser_type_id (parser);
7748 /* Look for the `,'. */
7749 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7750 token = cp_lexer_peek_token (parser->lexer);
7752 /* Build the (type *)null that begins the traditional offsetof macro. */
7753 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7754 tf_warning_or_error);
7756 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7757 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7758 true, &dummy, token->location);
7761 token = cp_lexer_peek_token (parser->lexer);
7762 switch (token->type)
7764 case CPP_OPEN_SQUARE:
7765 /* offsetof-member-designator "[" expression "]" */
7766 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7770 /* offsetof-member-designator "->" identifier */
7771 expr = grok_array_decl (expr, integer_zero_node);
7775 /* offsetof-member-designator "." identifier */
7776 cp_lexer_consume_token (parser->lexer);
7777 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7782 case CPP_CLOSE_PAREN:
7783 /* Consume the ")" token. */
7784 cp_lexer_consume_token (parser->lexer);
7788 /* Error. We know the following require will fail, but
7789 that gives the proper error message. */
7790 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7791 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7792 expr = error_mark_node;
7798 /* If we're processing a template, we can't finish the semantics yet.
7799 Otherwise we can fold the entire expression now. */
7800 if (processing_template_decl)
7801 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7803 expr = finish_offsetof (expr);
7806 parser->integral_constant_expression_p = save_ice_p;
7807 parser->non_integral_constant_expression_p = save_non_ice_p;
7812 /* Parse a trait expression.
7814 Returns a representation of the expression, the underlying type
7815 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7818 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7821 tree type1, type2 = NULL_TREE;
7822 bool binary = false;
7823 cp_decl_specifier_seq decl_specs;
7827 case RID_HAS_NOTHROW_ASSIGN:
7828 kind = CPTK_HAS_NOTHROW_ASSIGN;
7830 case RID_HAS_NOTHROW_CONSTRUCTOR:
7831 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7833 case RID_HAS_NOTHROW_COPY:
7834 kind = CPTK_HAS_NOTHROW_COPY;
7836 case RID_HAS_TRIVIAL_ASSIGN:
7837 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7839 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7840 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7842 case RID_HAS_TRIVIAL_COPY:
7843 kind = CPTK_HAS_TRIVIAL_COPY;
7845 case RID_HAS_TRIVIAL_DESTRUCTOR:
7846 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7848 case RID_HAS_VIRTUAL_DESTRUCTOR:
7849 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7851 case RID_IS_ABSTRACT:
7852 kind = CPTK_IS_ABSTRACT;
7854 case RID_IS_BASE_OF:
7855 kind = CPTK_IS_BASE_OF;
7859 kind = CPTK_IS_CLASS;
7861 case RID_IS_CONVERTIBLE_TO:
7862 kind = CPTK_IS_CONVERTIBLE_TO;
7866 kind = CPTK_IS_EMPTY;
7869 kind = CPTK_IS_ENUM;
7871 case RID_IS_LITERAL_TYPE:
7872 kind = CPTK_IS_LITERAL_TYPE;
7877 case RID_IS_POLYMORPHIC:
7878 kind = CPTK_IS_POLYMORPHIC;
7880 case RID_IS_STD_LAYOUT:
7881 kind = CPTK_IS_STD_LAYOUT;
7883 case RID_IS_TRIVIAL:
7884 kind = CPTK_IS_TRIVIAL;
7887 kind = CPTK_IS_UNION;
7889 case RID_UNDERLYING_TYPE:
7890 kind = CPTK_UNDERLYING_TYPE;
7895 case RID_DIRECT_BASES:
7896 kind = CPTK_DIRECT_BASES;
7902 /* Consume the token. */
7903 cp_lexer_consume_token (parser->lexer);
7905 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7907 type1 = cp_parser_type_id (parser);
7909 if (type1 == error_mark_node)
7910 return error_mark_node;
7912 /* Build a trivial decl-specifier-seq. */
7913 clear_decl_specs (&decl_specs);
7914 decl_specs.type = type1;
7916 /* Call grokdeclarator to figure out what type this is. */
7917 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7918 /*initialized=*/0, /*attrlist=*/NULL);
7922 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7924 type2 = cp_parser_type_id (parser);
7926 if (type2 == error_mark_node)
7927 return error_mark_node;
7929 /* Build a trivial decl-specifier-seq. */
7930 clear_decl_specs (&decl_specs);
7931 decl_specs.type = type2;
7933 /* Call grokdeclarator to figure out what type this is. */
7934 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7935 /*initialized=*/0, /*attrlist=*/NULL);
7938 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7940 /* Complete the trait expression, which may mean either processing
7941 the trait expr now or saving it for template instantiation. */
7944 case CPTK_UNDERLYING_TYPE:
7945 return finish_underlying_type (type1);
7947 return finish_bases (type1, false);
7948 case CPTK_DIRECT_BASES:
7949 return finish_bases (type1, true);
7951 return finish_trait_expr (kind, type1, type2);
7955 /* Lambdas that appear in variable initializer or default argument scope
7956 get that in their mangling, so we need to record it. We might as well
7957 use the count for function and namespace scopes as well. */
7958 static GTY(()) tree lambda_scope;
7959 static GTY(()) int lambda_count;
7960 typedef struct GTY(()) tree_int
7965 DEF_VEC_O(tree_int);
7966 DEF_VEC_ALLOC_O(tree_int,gc);
7967 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7970 start_lambda_scope (tree decl)
7974 /* Once we're inside a function, we ignore other scopes and just push
7975 the function again so that popping works properly. */
7976 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7977 decl = current_function_decl;
7978 ti.t = lambda_scope;
7979 ti.i = lambda_count;
7980 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7981 if (lambda_scope != decl)
7983 /* Don't reset the count if we're still in the same function. */
7984 lambda_scope = decl;
7990 record_lambda_scope (tree lambda)
7992 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7993 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7997 finish_lambda_scope (void)
7999 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8000 if (lambda_scope != p->t)
8002 lambda_scope = p->t;
8003 lambda_count = p->i;
8005 VEC_pop (tree_int, lambda_scope_stack);
8008 /* Parse a lambda expression.
8011 lambda-introducer lambda-declarator [opt] compound-statement
8013 Returns a representation of the expression. */
8016 cp_parser_lambda_expression (cp_parser* parser)
8018 tree lambda_expr = build_lambda_expr ();
8022 LAMBDA_EXPR_LOCATION (lambda_expr)
8023 = cp_lexer_peek_token (parser->lexer)->location;
8025 if (cp_unevaluated_operand)
8026 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8027 "lambda-expression in unevaluated context");
8029 /* We may be in the middle of deferred access check. Disable
8031 push_deferring_access_checks (dk_no_deferred);
8033 cp_parser_lambda_introducer (parser, lambda_expr);
8035 type = begin_lambda_type (lambda_expr);
8037 record_lambda_scope (lambda_expr);
8039 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8040 determine_visibility (TYPE_NAME (type));
8042 /* Now that we've started the type, add the capture fields for any
8043 explicit captures. */
8044 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8047 /* Inside the class, surrounding template-parameter-lists do not apply. */
8048 unsigned int saved_num_template_parameter_lists
8049 = parser->num_template_parameter_lists;
8050 unsigned char in_statement = parser->in_statement;
8051 bool in_switch_statement_p = parser->in_switch_statement_p;
8053 parser->num_template_parameter_lists = 0;
8054 parser->in_statement = 0;
8055 parser->in_switch_statement_p = false;
8057 /* By virtue of defining a local class, a lambda expression has access to
8058 the private variables of enclosing classes. */
8060 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8063 cp_parser_lambda_body (parser, lambda_expr);
8064 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8065 cp_parser_skip_to_end_of_block_or_statement (parser);
8067 /* The capture list was built up in reverse order; fix that now. */
8069 tree newlist = NULL_TREE;
8072 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8075 next = TREE_CHAIN (elt);
8076 TREE_CHAIN (elt) = newlist;
8079 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8083 maybe_add_lambda_conv_op (type);
8085 type = finish_struct (type, /*attributes=*/NULL_TREE);
8087 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8088 parser->in_statement = in_statement;
8089 parser->in_switch_statement_p = in_switch_statement_p;
8092 pop_deferring_access_checks ();
8094 /* This field is only used during parsing of the lambda. */
8095 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8097 /* This lambda shouldn't have any proxies left at this point. */
8098 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8099 /* And now that we're done, push proxies for an enclosing lambda. */
8100 insert_pending_capture_proxies ();
8103 return build_lambda_object (lambda_expr);
8105 return error_mark_node;
8108 /* Parse the beginning of a lambda expression.
8111 [ lambda-capture [opt] ]
8113 LAMBDA_EXPR is the current representation of the lambda expression. */
8116 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8118 /* Need commas after the first capture. */
8121 /* Eat the leading `['. */
8122 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8124 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8125 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8126 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8127 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8128 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8129 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8131 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8133 cp_lexer_consume_token (parser->lexer);
8137 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8139 cp_token* capture_token;
8141 tree capture_init_expr;
8142 cp_id_kind idk = CP_ID_KIND_NONE;
8143 bool explicit_init_p = false;
8145 enum capture_kind_type
8150 enum capture_kind_type capture_kind = BY_COPY;
8152 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8154 error ("expected end of capture-list");
8161 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8163 /* Possibly capture `this'. */
8164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8166 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8167 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8168 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8169 "with by-copy capture default");
8170 cp_lexer_consume_token (parser->lexer);
8171 add_capture (lambda_expr,
8172 /*id=*/this_identifier,
8173 /*initializer=*/finish_this_expr(),
8174 /*by_reference_p=*/false,
8179 /* Remember whether we want to capture as a reference or not. */
8180 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8182 capture_kind = BY_REFERENCE;
8183 cp_lexer_consume_token (parser->lexer);
8186 /* Get the identifier. */
8187 capture_token = cp_lexer_peek_token (parser->lexer);
8188 capture_id = cp_parser_identifier (parser);
8190 if (capture_id == error_mark_node)
8191 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8192 delimiters, but I modified this to stop on unnested ']' as well. It
8193 was already changed to stop on unnested '}', so the
8194 "closing_parenthesis" name is no more misleading with my change. */
8196 cp_parser_skip_to_closing_parenthesis (parser,
8197 /*recovering=*/true,
8199 /*consume_paren=*/true);
8203 /* Find the initializer for this capture. */
8204 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8206 /* An explicit expression exists. */
8207 cp_lexer_consume_token (parser->lexer);
8208 pedwarn (input_location, OPT_pedantic,
8209 "ISO C++ does not allow initializers "
8210 "in lambda expression capture lists");
8211 capture_init_expr = cp_parser_assignment_expression (parser,
8214 explicit_init_p = true;
8218 const char* error_msg;
8220 /* Turn the identifier into an id-expression. */
8222 = cp_parser_lookup_name
8226 /*is_template=*/false,
8227 /*is_namespace=*/false,
8228 /*check_dependency=*/true,
8229 /*ambiguous_decls=*/NULL,
8230 capture_token->location);
8232 if (capture_init_expr == error_mark_node)
8234 unqualified_name_lookup_error (capture_id);
8237 else if (DECL_P (capture_init_expr)
8238 && (TREE_CODE (capture_init_expr) != VAR_DECL
8239 && TREE_CODE (capture_init_expr) != PARM_DECL))
8241 error_at (capture_token->location,
8242 "capture of non-variable %qD ",
8244 inform (0, "%q+#D declared here", capture_init_expr);
8247 if (TREE_CODE (capture_init_expr) == VAR_DECL
8248 && decl_storage_duration (capture_init_expr) != dk_auto)
8250 pedwarn (capture_token->location, 0, "capture of variable "
8251 "%qD with non-automatic storage duration",
8253 inform (0, "%q+#D declared here", capture_init_expr);
8258 = finish_id_expression
8263 /*integral_constant_expression_p=*/false,
8264 /*allow_non_integral_constant_expression_p=*/false,
8265 /*non_integral_constant_expression_p=*/NULL,
8266 /*template_p=*/false,
8268 /*address_p=*/false,
8269 /*template_arg_p=*/false,
8271 capture_token->location);
8274 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8275 && !explicit_init_p)
8277 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8278 && capture_kind == BY_COPY)
8279 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8280 "of %qD redundant with by-copy capture default",
8282 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8283 && capture_kind == BY_REFERENCE)
8284 pedwarn (capture_token->location, 0, "explicit by-reference "
8285 "capture of %qD redundant with by-reference capture "
8286 "default", capture_id);
8289 add_capture (lambda_expr,
8292 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8296 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8299 /* Parse the (optional) middle of a lambda expression.
8302 ( parameter-declaration-clause [opt] )
8303 attribute-specifier [opt]
8305 exception-specification [opt]
8306 lambda-return-type-clause [opt]
8308 LAMBDA_EXPR is the current representation of the lambda expression. */
8311 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8313 /* 5.1.1.4 of the standard says:
8314 If a lambda-expression does not include a lambda-declarator, it is as if
8315 the lambda-declarator were ().
8316 This means an empty parameter list, no attributes, and no exception
8318 tree param_list = void_list_node;
8319 tree attributes = NULL_TREE;
8320 tree exception_spec = NULL_TREE;
8323 /* The lambda-declarator is optional, but must begin with an opening
8324 parenthesis if present. */
8325 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8327 cp_lexer_consume_token (parser->lexer);
8329 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8331 /* Parse parameters. */
8332 param_list = cp_parser_parameter_declaration_clause (parser);
8334 /* Default arguments shall not be specified in the
8335 parameter-declaration-clause of a lambda-declarator. */
8336 for (t = param_list; t; t = TREE_CHAIN (t))
8337 if (TREE_PURPOSE (t))
8338 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8339 "default argument specified for lambda parameter");
8341 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8343 attributes = cp_parser_attributes_opt (parser);
8345 /* Parse optional `mutable' keyword. */
8346 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8348 cp_lexer_consume_token (parser->lexer);
8349 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8352 /* Parse optional exception specification. */
8353 exception_spec = cp_parser_exception_specification_opt (parser);
8355 /* Parse optional trailing return type. */
8356 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8358 cp_lexer_consume_token (parser->lexer);
8359 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8362 /* The function parameters must be in scope all the way until after the
8363 trailing-return-type in case of decltype. */
8364 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8365 pop_binding (DECL_NAME (t), t);
8370 /* Create the function call operator.
8372 Messing with declarators like this is no uglier than building up the
8373 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8376 cp_decl_specifier_seq return_type_specs;
8377 cp_declarator* declarator;
8382 clear_decl_specs (&return_type_specs);
8383 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8384 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8386 /* Maybe we will deduce the return type later, but we can use void
8387 as a placeholder return type anyways. */
8388 return_type_specs.type = void_type_node;
8390 p = obstack_alloc (&declarator_obstack, 0);
8392 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8395 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8396 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8397 declarator = make_call_declarator (declarator, param_list, quals,
8398 VIRT_SPEC_UNSPECIFIED,
8400 /*late_return_type=*/NULL_TREE);
8401 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8403 fco = grokmethod (&return_type_specs,
8406 if (fco != error_mark_node)
8408 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8409 DECL_ARTIFICIAL (fco) = 1;
8410 /* Give the object parameter a different name. */
8411 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8414 finish_member_declaration (fco);
8416 obstack_free (&declarator_obstack, p);
8418 return (fco != error_mark_node);
8422 /* Parse the body of a lambda expression, which is simply
8426 but which requires special handling.
8427 LAMBDA_EXPR is the current representation of the lambda expression. */
8430 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8432 bool nested = (current_function_decl != NULL_TREE);
8433 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8435 push_function_context ();
8437 /* Still increment function_depth so that we don't GC in the
8438 middle of an expression. */
8440 /* Clear this in case we're in the middle of a default argument. */
8441 parser->local_variables_forbidden_p = false;
8443 /* Finish the function call operator
8445 + late_parsing_for_member
8446 + function_definition_after_declarator
8447 + ctor_initializer_opt_and_function_body */
8449 tree fco = lambda_function (lambda_expr);
8455 /* Let the front end know that we are going to be defining this
8457 start_preparsed_function (fco,
8459 SF_PRE_PARSED | SF_INCLASS_INLINE);
8461 start_lambda_scope (fco);
8462 body = begin_function_body ();
8464 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8467 /* Push the proxies for any explicit captures. */
8468 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8469 cap = TREE_CHAIN (cap))
8470 build_capture_proxy (TREE_PURPOSE (cap));
8472 compound_stmt = begin_compound_stmt (0);
8474 /* 5.1.1.4 of the standard says:
8475 If a lambda-expression does not include a trailing-return-type, it
8476 is as if the trailing-return-type denotes the following type:
8477 * if the compound-statement is of the form
8478 { return attribute-specifier [opt] expression ; }
8479 the type of the returned expression after lvalue-to-rvalue
8480 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8481 (_conv.array_ 4.2), and function-to-pointer conversion
8483 * otherwise, void. */
8485 /* In a lambda that has neither a lambda-return-type-clause
8486 nor a deducible form, errors should be reported for return statements
8487 in the body. Since we used void as the placeholder return type, parsing
8488 the body as usual will give such desired behavior. */
8489 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8490 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8491 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8493 tree expr = NULL_TREE;
8494 cp_id_kind idk = CP_ID_KIND_NONE;
8496 /* Parse tentatively in case there's more after the initial return
8498 cp_parser_parse_tentatively (parser);
8500 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8502 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8504 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8505 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8507 if (cp_parser_parse_definitely (parser))
8509 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8511 /* Will get error here if type not deduced yet. */
8512 finish_return_stmt (expr);
8520 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8521 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8522 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8523 cp_parser_label_declaration (parser);
8524 cp_parser_statement_seq_opt (parser, NULL_TREE);
8525 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8526 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8529 finish_compound_stmt (compound_stmt);
8532 finish_function_body (body);
8533 finish_lambda_scope ();
8535 /* Finish the function and generate code for it if necessary. */
8536 expand_or_defer_fn (finish_function (/*inline*/2));
8539 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8541 pop_function_context();
8546 /* Statements [gram.stmt.stmt] */
8548 /* Parse a statement.
8552 expression-statement
8557 declaration-statement
8565 IN_COMPOUND is true when the statement is nested inside a
8566 cp_parser_compound_statement; this matters for certain pragmas.
8568 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8569 is a (possibly labeled) if statement which is not enclosed in braces
8570 and has an else clause. This is used to implement -Wparentheses. */
8573 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8574 bool in_compound, bool *if_p)
8578 location_t statement_location;
8583 /* There is no statement yet. */
8584 statement = NULL_TREE;
8585 /* Peek at the next token. */
8586 token = cp_lexer_peek_token (parser->lexer);
8587 /* Remember the location of the first token in the statement. */
8588 statement_location = token->location;
8589 /* If this is a keyword, then that will often determine what kind of
8590 statement we have. */
8591 if (token->type == CPP_KEYWORD)
8593 enum rid keyword = token->keyword;
8599 /* Looks like a labeled-statement with a case label.
8600 Parse the label, and then use tail recursion to parse
8602 cp_parser_label_for_labeled_statement (parser);
8607 statement = cp_parser_selection_statement (parser, if_p);
8613 statement = cp_parser_iteration_statement (parser);
8620 statement = cp_parser_jump_statement (parser);
8623 /* Objective-C++ exception-handling constructs. */
8626 case RID_AT_FINALLY:
8627 case RID_AT_SYNCHRONIZED:
8629 statement = cp_parser_objc_statement (parser);
8633 statement = cp_parser_try_block (parser);
8637 /* This must be a namespace alias definition. */
8638 cp_parser_declaration_statement (parser);
8641 case RID_TRANSACTION_ATOMIC:
8642 case RID_TRANSACTION_RELAXED:
8643 statement = cp_parser_transaction (parser, keyword);
8645 case RID_TRANSACTION_CANCEL:
8646 statement = cp_parser_transaction_cancel (parser);
8650 /* It might be a keyword like `int' that can start a
8651 declaration-statement. */
8655 else if (token->type == CPP_NAME)
8657 /* If the next token is a `:', then we are looking at a
8658 labeled-statement. */
8659 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8660 if (token->type == CPP_COLON)
8662 /* Looks like a labeled-statement with an ordinary label.
8663 Parse the label, and then use tail recursion to parse
8665 cp_parser_label_for_labeled_statement (parser);
8669 /* Anything that starts with a `{' must be a compound-statement. */
8670 else if (token->type == CPP_OPEN_BRACE)
8671 statement = cp_parser_compound_statement (parser, NULL, false, false);
8672 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8673 a statement all its own. */
8674 else if (token->type == CPP_PRAGMA)
8676 /* Only certain OpenMP pragmas are attached to statements, and thus
8677 are considered statements themselves. All others are not. In
8678 the context of a compound, accept the pragma as a "statement" and
8679 return so that we can check for a close brace. Otherwise we
8680 require a real statement and must go back and read one. */
8682 cp_parser_pragma (parser, pragma_compound);
8683 else if (!cp_parser_pragma (parser, pragma_stmt))
8687 else if (token->type == CPP_EOF)
8689 cp_parser_error (parser, "expected statement");
8693 /* Everything else must be a declaration-statement or an
8694 expression-statement. Try for the declaration-statement
8695 first, unless we are looking at a `;', in which case we know that
8696 we have an expression-statement. */
8699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8701 cp_parser_parse_tentatively (parser);
8702 /* Try to parse the declaration-statement. */
8703 cp_parser_declaration_statement (parser);
8704 /* If that worked, we're done. */
8705 if (cp_parser_parse_definitely (parser))
8708 /* Look for an expression-statement instead. */
8709 statement = cp_parser_expression_statement (parser, in_statement_expr);
8712 /* Set the line number for the statement. */
8713 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8714 SET_EXPR_LOCATION (statement, statement_location);
8717 /* Parse the label for a labeled-statement, i.e.
8720 case constant-expression :
8724 case constant-expression ... constant-expression : statement
8726 When a label is parsed without errors, the label is added to the
8727 parse tree by the finish_* functions, so this function doesn't
8728 have to return the label. */
8731 cp_parser_label_for_labeled_statement (cp_parser* parser)
8734 tree label = NULL_TREE;
8735 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8737 /* The next token should be an identifier. */
8738 token = cp_lexer_peek_token (parser->lexer);
8739 if (token->type != CPP_NAME
8740 && token->type != CPP_KEYWORD)
8742 cp_parser_error (parser, "expected labeled-statement");
8746 parser->colon_corrects_to_scope_p = false;
8747 switch (token->keyword)
8754 /* Consume the `case' token. */
8755 cp_lexer_consume_token (parser->lexer);
8756 /* Parse the constant-expression. */
8757 expr = cp_parser_constant_expression (parser,
8758 /*allow_non_constant_p=*/false,
8761 ellipsis = cp_lexer_peek_token (parser->lexer);
8762 if (ellipsis->type == CPP_ELLIPSIS)
8764 /* Consume the `...' token. */
8765 cp_lexer_consume_token (parser->lexer);
8767 cp_parser_constant_expression (parser,
8768 /*allow_non_constant_p=*/false,
8770 /* We don't need to emit warnings here, as the common code
8771 will do this for us. */
8774 expr_hi = NULL_TREE;
8776 if (parser->in_switch_statement_p)
8777 finish_case_label (token->location, expr, expr_hi);
8779 error_at (token->location,
8780 "case label %qE not within a switch statement",
8786 /* Consume the `default' token. */
8787 cp_lexer_consume_token (parser->lexer);
8789 if (parser->in_switch_statement_p)
8790 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8792 error_at (token->location, "case label not within a switch statement");
8796 /* Anything else must be an ordinary label. */
8797 label = finish_label_stmt (cp_parser_identifier (parser));
8801 /* Require the `:' token. */
8802 cp_parser_require (parser, CPP_COLON, RT_COLON);
8804 /* An ordinary label may optionally be followed by attributes.
8805 However, this is only permitted if the attributes are then
8806 followed by a semicolon. This is because, for backward
8807 compatibility, when parsing
8808 lab: __attribute__ ((unused)) int i;
8809 we want the attribute to attach to "i", not "lab". */
8810 if (label != NULL_TREE
8811 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8815 cp_parser_parse_tentatively (parser);
8816 attrs = cp_parser_attributes_opt (parser);
8817 if (attrs == NULL_TREE
8818 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8819 cp_parser_abort_tentative_parse (parser);
8820 else if (!cp_parser_parse_definitely (parser))
8823 cplus_decl_attributes (&label, attrs, 0);
8826 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8829 /* Parse an expression-statement.
8831 expression-statement:
8834 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8835 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8836 indicates whether this expression-statement is part of an
8837 expression statement. */
8840 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8842 tree statement = NULL_TREE;
8843 cp_token *token = cp_lexer_peek_token (parser->lexer);
8845 /* If the next token is a ';', then there is no expression
8847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8848 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8850 /* Give a helpful message for "A<T>::type t;" and the like. */
8851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8852 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8854 if (TREE_CODE (statement) == SCOPE_REF)
8855 error_at (token->location, "need %<typename%> before %qE because "
8856 "%qT is a dependent scope",
8857 statement, TREE_OPERAND (statement, 0));
8858 else if (is_overloaded_fn (statement)
8859 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8862 tree fn = get_first_fn (statement);
8863 error_at (token->location,
8864 "%<%T::%D%> names the constructor, not the type",
8865 DECL_CONTEXT (fn), DECL_NAME (fn));
8869 /* Consume the final `;'. */
8870 cp_parser_consume_semicolon_at_end_of_statement (parser);
8872 if (in_statement_expr
8873 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8874 /* This is the final expression statement of a statement
8876 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8878 statement = finish_expr_stmt (statement);
8885 /* Parse a compound-statement.
8888 { statement-seq [opt] }
8893 { label-declaration-seq [opt] statement-seq [opt] }
8895 label-declaration-seq:
8897 label-declaration-seq label-declaration
8899 Returns a tree representing the statement. */
8902 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8903 bool in_try, bool function_body)
8907 /* Consume the `{'. */
8908 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8909 return error_mark_node;
8910 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8912 pedwarn (input_location, OPT_pedantic,
8913 "compound-statement in constexpr function");
8914 /* Begin the compound-statement. */
8915 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8916 /* If the next keyword is `__label__' we have a label declaration. */
8917 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8918 cp_parser_label_declaration (parser);
8919 /* Parse an (optional) statement-seq. */
8920 cp_parser_statement_seq_opt (parser, in_statement_expr);
8921 /* Finish the compound-statement. */
8922 finish_compound_stmt (compound_stmt);
8923 /* Consume the `}'. */
8924 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8926 return compound_stmt;
8929 /* Parse an (optional) statement-seq.
8933 statement-seq [opt] statement */
8936 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8938 /* Scan statements until there aren't any more. */
8941 cp_token *token = cp_lexer_peek_token (parser->lexer);
8943 /* If we are looking at a `}', then we have run out of
8944 statements; the same is true if we have reached the end
8945 of file, or have stumbled upon a stray '@end'. */
8946 if (token->type == CPP_CLOSE_BRACE
8947 || token->type == CPP_EOF
8948 || token->type == CPP_PRAGMA_EOL
8949 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8952 /* If we are in a compound statement and find 'else' then
8953 something went wrong. */
8954 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8956 if (parser->in_statement & IN_IF_STMT)
8960 token = cp_lexer_consume_token (parser->lexer);
8961 error_at (token->location, "%<else%> without a previous %<if%>");
8965 /* Parse the statement. */
8966 cp_parser_statement (parser, in_statement_expr, true, NULL);
8970 /* Parse a selection-statement.
8972 selection-statement:
8973 if ( condition ) statement
8974 if ( condition ) statement else statement
8975 switch ( condition ) statement
8977 Returns the new IF_STMT or SWITCH_STMT.
8979 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8980 is a (possibly labeled) if statement which is not enclosed in
8981 braces and has an else clause. This is used to implement
8985 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8993 /* Peek at the next token. */
8994 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8996 /* See what kind of keyword it is. */
8997 keyword = token->keyword;
9006 /* Look for the `('. */
9007 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9009 cp_parser_skip_to_end_of_statement (parser);
9010 return error_mark_node;
9013 /* Begin the selection-statement. */
9014 if (keyword == RID_IF)
9015 statement = begin_if_stmt ();
9017 statement = begin_switch_stmt ();
9019 /* Parse the condition. */
9020 condition = cp_parser_condition (parser);
9021 /* Look for the `)'. */
9022 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9023 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9024 /*consume_paren=*/true);
9026 if (keyword == RID_IF)
9029 unsigned char in_statement;
9031 /* Add the condition. */
9032 finish_if_stmt_cond (condition, statement);
9034 /* Parse the then-clause. */
9035 in_statement = parser->in_statement;
9036 parser->in_statement |= IN_IF_STMT;
9037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9039 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9040 add_stmt (build_empty_stmt (loc));
9041 cp_lexer_consume_token (parser->lexer);
9042 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9043 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9044 "empty body in an %<if%> statement");
9048 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9049 parser->in_statement = in_statement;
9051 finish_then_clause (statement);
9053 /* If the next token is `else', parse the else-clause. */
9054 if (cp_lexer_next_token_is_keyword (parser->lexer,
9057 /* Consume the `else' keyword. */
9058 cp_lexer_consume_token (parser->lexer);
9059 begin_else_clause (statement);
9060 /* Parse the else-clause. */
9061 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9064 loc = cp_lexer_peek_token (parser->lexer)->location;
9066 OPT_Wempty_body, "suggest braces around "
9067 "empty body in an %<else%> statement");
9068 add_stmt (build_empty_stmt (loc));
9069 cp_lexer_consume_token (parser->lexer);
9072 cp_parser_implicitly_scoped_statement (parser, NULL);
9074 finish_else_clause (statement);
9076 /* If we are currently parsing a then-clause, then
9077 IF_P will not be NULL. We set it to true to
9078 indicate that this if statement has an else clause.
9079 This may trigger the Wparentheses warning below
9080 when we get back up to the parent if statement. */
9086 /* This if statement does not have an else clause. If
9087 NESTED_IF is true, then the then-clause is an if
9088 statement which does have an else clause. We warn
9089 about the potential ambiguity. */
9091 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9092 "suggest explicit braces to avoid ambiguous"
9096 /* Now we're all done with the if-statement. */
9097 finish_if_stmt (statement);
9101 bool in_switch_statement_p;
9102 unsigned char in_statement;
9104 /* Add the condition. */
9105 finish_switch_cond (condition, statement);
9107 /* Parse the body of the switch-statement. */
9108 in_switch_statement_p = parser->in_switch_statement_p;
9109 in_statement = parser->in_statement;
9110 parser->in_switch_statement_p = true;
9111 parser->in_statement |= IN_SWITCH_STMT;
9112 cp_parser_implicitly_scoped_statement (parser, NULL);
9113 parser->in_switch_statement_p = in_switch_statement_p;
9114 parser->in_statement = in_statement;
9116 /* Now we're all done with the switch-statement. */
9117 finish_switch_stmt (statement);
9125 cp_parser_error (parser, "expected selection-statement");
9126 return error_mark_node;
9130 /* Parse a condition.
9134 type-specifier-seq declarator = initializer-clause
9135 type-specifier-seq declarator braced-init-list
9140 type-specifier-seq declarator asm-specification [opt]
9141 attributes [opt] = assignment-expression
9143 Returns the expression that should be tested. */
9146 cp_parser_condition (cp_parser* parser)
9148 cp_decl_specifier_seq type_specifiers;
9149 const char *saved_message;
9150 int declares_class_or_enum;
9152 /* Try the declaration first. */
9153 cp_parser_parse_tentatively (parser);
9154 /* New types are not allowed in the type-specifier-seq for a
9156 saved_message = parser->type_definition_forbidden_message;
9157 parser->type_definition_forbidden_message
9158 = G_("types may not be defined in conditions");
9159 /* Parse the type-specifier-seq. */
9160 cp_parser_decl_specifier_seq (parser,
9161 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9163 &declares_class_or_enum);
9164 /* Restore the saved message. */
9165 parser->type_definition_forbidden_message = saved_message;
9166 /* If all is well, we might be looking at a declaration. */
9167 if (!cp_parser_error_occurred (parser))
9170 tree asm_specification;
9172 cp_declarator *declarator;
9173 tree initializer = NULL_TREE;
9175 /* Parse the declarator. */
9176 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9177 /*ctor_dtor_or_conv_p=*/NULL,
9178 /*parenthesized_p=*/NULL,
9179 /*member_p=*/false);
9180 /* Parse the attributes. */
9181 attributes = cp_parser_attributes_opt (parser);
9182 /* Parse the asm-specification. */
9183 asm_specification = cp_parser_asm_specification_opt (parser);
9184 /* If the next token is not an `=' or '{', then we might still be
9185 looking at an expression. For example:
9189 looks like a decl-specifier-seq and a declarator -- but then
9190 there is no `=', so this is an expression. */
9191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9192 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9193 cp_parser_simulate_error (parser);
9195 /* If we did see an `=' or '{', then we are looking at a declaration
9197 if (cp_parser_parse_definitely (parser))
9200 bool non_constant_p;
9201 bool flags = LOOKUP_ONLYCONVERTING;
9203 /* Create the declaration. */
9204 decl = start_decl (declarator, &type_specifiers,
9205 /*initialized_p=*/true,
9206 attributes, /*prefix_attributes=*/NULL_TREE,
9209 /* Parse the initializer. */
9210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9212 initializer = cp_parser_braced_list (parser, &non_constant_p);
9213 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9218 /* Consume the `='. */
9219 cp_parser_require (parser, CPP_EQ, RT_EQ);
9220 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9222 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9223 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9225 /* Process the initializer. */
9226 cp_finish_decl (decl,
9227 initializer, !non_constant_p,
9232 pop_scope (pushed_scope);
9234 return convert_from_reference (decl);
9237 /* If we didn't even get past the declarator successfully, we are
9238 definitely not looking at a declaration. */
9240 cp_parser_abort_tentative_parse (parser);
9242 /* Otherwise, we are looking at an expression. */
9243 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9246 /* Parses a for-statement or range-for-statement until the closing ')',
9250 cp_parser_for (cp_parser *parser)
9252 tree init, scope, decl;
9255 /* Begin the for-statement. */
9256 scope = begin_for_scope (&init);
9258 /* Parse the initialization. */
9259 is_range_for = cp_parser_for_init_statement (parser, &decl);
9262 return cp_parser_range_for (parser, scope, init, decl);
9264 return cp_parser_c_for (parser, scope, init);
9268 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9270 /* Normal for loop */
9271 tree condition = NULL_TREE;
9272 tree expression = NULL_TREE;
9275 stmt = begin_for_stmt (scope, init);
9276 /* The for-init-statement has already been parsed in
9277 cp_parser_for_init_statement, so no work is needed here. */
9278 finish_for_init_stmt (stmt);
9280 /* If there's a condition, process it. */
9281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9282 condition = cp_parser_condition (parser);
9283 finish_for_cond (condition, stmt);
9284 /* Look for the `;'. */
9285 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9287 /* If there's an expression, process it. */
9288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9289 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9290 finish_for_expr (expression, stmt);
9295 /* Tries to parse a range-based for-statement:
9298 decl-specifier-seq declarator : expression
9300 The decl-specifier-seq declarator and the `:' are already parsed by
9301 cp_parser_for_init_statement. If processing_template_decl it returns a
9302 newly created RANGE_FOR_STMT; if not, it is converted to a
9303 regular FOR_STMT. */
9306 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9308 tree stmt, range_expr;
9310 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9312 bool expr_non_constant_p;
9313 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9316 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9318 /* If in template, STMT is converted to a normal for-statement
9319 at instantiation. If not, it is done just ahead. */
9320 if (processing_template_decl)
9322 if (check_for_bare_parameter_packs (range_expr))
9323 range_expr = error_mark_node;
9324 stmt = begin_range_for_stmt (scope, init);
9325 finish_range_for_decl (stmt, range_decl, range_expr);
9326 if (!type_dependent_expression_p (range_expr)
9327 /* do_auto_deduction doesn't mess with template init-lists. */
9328 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9329 do_range_for_auto_deduction (range_decl, range_expr);
9333 stmt = begin_for_stmt (scope, init);
9334 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9339 /* Subroutine of cp_convert_range_for: given the initializer expression,
9340 builds up the range temporary. */
9343 build_range_temp (tree range_expr)
9345 tree range_type, range_temp;
9347 /* Find out the type deduced by the declaration
9348 `auto &&__range = range_expr'. */
9349 range_type = cp_build_reference_type (make_auto (), true);
9350 range_type = do_auto_deduction (range_type, range_expr,
9351 type_uses_auto (range_type));
9353 /* Create the __range variable. */
9354 range_temp = build_decl (input_location, VAR_DECL,
9355 get_identifier ("__for_range"), range_type);
9356 TREE_USED (range_temp) = 1;
9357 DECL_ARTIFICIAL (range_temp) = 1;
9362 /* Used by cp_parser_range_for in template context: we aren't going to
9363 do a full conversion yet, but we still need to resolve auto in the
9364 type of the for-range-declaration if present. This is basically
9365 a shortcut version of cp_convert_range_for. */
9368 do_range_for_auto_deduction (tree decl, tree range_expr)
9370 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9373 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9374 range_temp = convert_from_reference (build_range_temp (range_expr));
9375 iter_type = (cp_parser_perform_range_for_lookup
9376 (range_temp, &begin_dummy, &end_dummy));
9377 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9378 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9379 tf_warning_or_error);
9380 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9381 iter_decl, auto_node);
9385 /* Converts a range-based for-statement into a normal
9386 for-statement, as per the definition.
9388 for (RANGE_DECL : RANGE_EXPR)
9391 should be equivalent to:
9394 auto &&__range = RANGE_EXPR;
9395 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9399 RANGE_DECL = *__begin;
9404 If RANGE_EXPR is an array:
9405 BEGIN_EXPR = __range
9406 END_EXPR = __range + ARRAY_SIZE(__range)
9407 Else if RANGE_EXPR has a member 'begin' or 'end':
9408 BEGIN_EXPR = __range.begin()
9409 END_EXPR = __range.end()
9411 BEGIN_EXPR = begin(__range)
9412 END_EXPR = end(__range);
9414 If __range has a member 'begin' but not 'end', or vice versa, we must
9415 still use the second alternative (it will surely fail, however).
9416 When calling begin()/end() in the third alternative we must use
9417 argument dependent lookup, but always considering 'std' as an associated
9421 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9424 tree iter_type, begin_expr, end_expr;
9425 tree condition, expression;
9427 if (range_decl == error_mark_node || range_expr == error_mark_node)
9428 /* If an error happened previously do nothing or else a lot of
9429 unhelpful errors would be issued. */
9430 begin_expr = end_expr = iter_type = error_mark_node;
9433 tree range_temp = build_range_temp (range_expr);
9434 pushdecl (range_temp);
9435 cp_finish_decl (range_temp, range_expr,
9436 /*is_constant_init*/false, NULL_TREE,
9437 LOOKUP_ONLYCONVERTING);
9439 range_temp = convert_from_reference (range_temp);
9440 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9441 &begin_expr, &end_expr);
9444 /* The new for initialization statement. */
9445 begin = build_decl (input_location, VAR_DECL,
9446 get_identifier ("__for_begin"), iter_type);
9447 TREE_USED (begin) = 1;
9448 DECL_ARTIFICIAL (begin) = 1;
9450 cp_finish_decl (begin, begin_expr,
9451 /*is_constant_init*/false, NULL_TREE,
9452 LOOKUP_ONLYCONVERTING);
9454 end = build_decl (input_location, VAR_DECL,
9455 get_identifier ("__for_end"), iter_type);
9456 TREE_USED (end) = 1;
9457 DECL_ARTIFICIAL (end) = 1;
9459 cp_finish_decl (end, end_expr,
9460 /*is_constant_init*/false, NULL_TREE,
9461 LOOKUP_ONLYCONVERTING);
9463 finish_for_init_stmt (statement);
9465 /* The new for condition. */
9466 condition = build_x_binary_op (NE_EXPR,
9469 NULL, tf_warning_or_error);
9470 finish_for_cond (condition, statement);
9472 /* The new increment expression. */
9473 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9474 finish_for_expr (expression, statement);
9476 /* The declaration is initialized with *__begin inside the loop body. */
9477 cp_finish_decl (range_decl,
9478 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9479 /*is_constant_init*/false, NULL_TREE,
9480 LOOKUP_ONLYCONVERTING);
9485 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9486 We need to solve both at the same time because the method used
9487 depends on the existence of members begin or end.
9488 Returns the type deduced for the iterator expression. */
9491 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9493 if (error_operand_p (range))
9495 *begin = *end = error_mark_node;
9496 return error_mark_node;
9499 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9501 error ("range-based %<for%> expression of type %qT "
9502 "has incomplete type", TREE_TYPE (range));
9503 *begin = *end = error_mark_node;
9504 return error_mark_node;
9506 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9508 /* If RANGE is an array, we will use pointer arithmetic. */
9510 *end = build_binary_op (input_location, PLUS_EXPR,
9512 array_type_nelts_top (TREE_TYPE (range)),
9514 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9518 /* If it is not an array, we must do a bit of magic. */
9519 tree id_begin, id_end;
9520 tree member_begin, member_end;
9522 *begin = *end = error_mark_node;
9524 id_begin = get_identifier ("begin");
9525 id_end = get_identifier ("end");
9526 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9527 /*protect=*/2, /*want_type=*/false,
9528 tf_warning_or_error);
9529 member_end = lookup_member (TREE_TYPE (range), id_end,
9530 /*protect=*/2, /*want_type=*/false,
9531 tf_warning_or_error);
9533 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9535 /* Use the member functions. */
9536 if (member_begin != NULL_TREE)
9537 *begin = cp_parser_range_for_member_function (range, id_begin);
9539 error ("range-based %<for%> expression of type %qT has an "
9540 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9542 if (member_end != NULL_TREE)
9543 *end = cp_parser_range_for_member_function (range, id_end);
9545 error ("range-based %<for%> expression of type %qT has a "
9546 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9550 /* Use global functions with ADL. */
9552 vec = make_tree_vector ();
9554 VEC_safe_push (tree, gc, vec, range);
9556 member_begin = perform_koenig_lookup (id_begin, vec,
9557 /*include_std=*/true,
9558 tf_warning_or_error);
9559 *begin = finish_call_expr (member_begin, &vec, false, true,
9560 tf_warning_or_error);
9561 member_end = perform_koenig_lookup (id_end, vec,
9562 /*include_std=*/true,
9563 tf_warning_or_error);
9564 *end = finish_call_expr (member_end, &vec, false, true,
9565 tf_warning_or_error);
9567 release_tree_vector (vec);
9570 /* Last common checks. */
9571 if (*begin == error_mark_node || *end == error_mark_node)
9573 /* If one of the expressions is an error do no more checks. */
9574 *begin = *end = error_mark_node;
9575 return error_mark_node;
9579 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9580 /* The unqualified type of the __begin and __end temporaries should
9581 be the same, as required by the multiple auto declaration. */
9582 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9583 error ("inconsistent begin/end types in range-based %<for%> "
9584 "statement: %qT and %qT",
9585 TREE_TYPE (*begin), TREE_TYPE (*end));
9591 /* Helper function for cp_parser_perform_range_for_lookup.
9592 Builds a tree for RANGE.IDENTIFIER(). */
9595 cp_parser_range_for_member_function (tree range, tree identifier)
9600 member = finish_class_member_access_expr (range, identifier,
9601 false, tf_warning_or_error);
9602 if (member == error_mark_node)
9603 return error_mark_node;
9605 vec = make_tree_vector ();
9606 res = finish_call_expr (member, &vec,
9607 /*disallow_virtual=*/false,
9609 tf_warning_or_error);
9610 release_tree_vector (vec);
9614 /* Parse an iteration-statement.
9616 iteration-statement:
9617 while ( condition ) statement
9618 do statement while ( expression ) ;
9619 for ( for-init-statement condition [opt] ; expression [opt] )
9622 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9625 cp_parser_iteration_statement (cp_parser* parser)
9630 unsigned char in_statement;
9632 /* Peek at the next token. */
9633 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9635 return error_mark_node;
9637 /* Remember whether or not we are already within an iteration
9639 in_statement = parser->in_statement;
9641 /* See what kind of keyword it is. */
9642 keyword = token->keyword;
9649 /* Begin the while-statement. */
9650 statement = begin_while_stmt ();
9651 /* Look for the `('. */
9652 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9653 /* Parse the condition. */
9654 condition = cp_parser_condition (parser);
9655 finish_while_stmt_cond (condition, statement);
9656 /* Look for the `)'. */
9657 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9658 /* Parse the dependent statement. */
9659 parser->in_statement = IN_ITERATION_STMT;
9660 cp_parser_already_scoped_statement (parser);
9661 parser->in_statement = in_statement;
9662 /* We're done with the while-statement. */
9663 finish_while_stmt (statement);
9671 /* Begin the do-statement. */
9672 statement = begin_do_stmt ();
9673 /* Parse the body of the do-statement. */
9674 parser->in_statement = IN_ITERATION_STMT;
9675 cp_parser_implicitly_scoped_statement (parser, NULL);
9676 parser->in_statement = in_statement;
9677 finish_do_body (statement);
9678 /* Look for the `while' keyword. */
9679 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9680 /* Look for the `('. */
9681 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9682 /* Parse the expression. */
9683 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9684 /* We're done with the do-statement. */
9685 finish_do_stmt (expression, statement);
9686 /* Look for the `)'. */
9687 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9688 /* Look for the `;'. */
9689 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9695 /* Look for the `('. */
9696 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9698 statement = cp_parser_for (parser);
9700 /* Look for the `)'. */
9701 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9703 /* Parse the body of the for-statement. */
9704 parser->in_statement = IN_ITERATION_STMT;
9705 cp_parser_already_scoped_statement (parser);
9706 parser->in_statement = in_statement;
9708 /* We're done with the for-statement. */
9709 finish_for_stmt (statement);
9714 cp_parser_error (parser, "expected iteration-statement");
9715 statement = error_mark_node;
9722 /* Parse a for-init-statement or the declarator of a range-based-for.
9723 Returns true if a range-based-for declaration is seen.
9726 expression-statement
9727 simple-declaration */
9730 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9732 /* If the next token is a `;', then we have an empty
9733 expression-statement. Grammatically, this is also a
9734 simple-declaration, but an invalid one, because it does not
9735 declare anything. Therefore, if we did not handle this case
9736 specially, we would issue an error message about an invalid
9738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9740 bool is_range_for = false;
9741 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9743 parser->colon_corrects_to_scope_p = false;
9745 /* We're going to speculatively look for a declaration, falling back
9746 to an expression, if necessary. */
9747 cp_parser_parse_tentatively (parser);
9748 /* Parse the declaration. */
9749 cp_parser_simple_declaration (parser,
9750 /*function_definition_allowed_p=*/false,
9752 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9753 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9755 /* It is a range-for, consume the ':' */
9756 cp_lexer_consume_token (parser->lexer);
9757 is_range_for = true;
9758 if (cxx_dialect < cxx0x)
9760 error_at (cp_lexer_peek_token (parser->lexer)->location,
9761 "range-based %<for%> loops are not allowed "
9763 *decl = error_mark_node;
9767 /* The ';' is not consumed yet because we told
9768 cp_parser_simple_declaration not to. */
9769 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9771 if (cp_parser_parse_definitely (parser))
9772 return is_range_for;
9773 /* If the tentative parse failed, then we shall need to look for an
9774 expression-statement. */
9776 /* If we are here, it is an expression-statement. */
9777 cp_parser_expression_statement (parser, NULL_TREE);
9781 /* Parse a jump-statement.
9786 return expression [opt] ;
9787 return braced-init-list ;
9795 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9798 cp_parser_jump_statement (cp_parser* parser)
9800 tree statement = error_mark_node;
9803 unsigned char in_statement;
9805 /* Peek at the next token. */
9806 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9808 return error_mark_node;
9810 /* See what kind of keyword it is. */
9811 keyword = token->keyword;
9815 in_statement = parser->in_statement & ~IN_IF_STMT;
9816 switch (in_statement)
9819 error_at (token->location, "break statement not within loop or switch");
9822 gcc_assert ((in_statement & IN_SWITCH_STMT)
9823 || in_statement == IN_ITERATION_STMT);
9824 statement = finish_break_stmt ();
9827 error_at (token->location, "invalid exit from OpenMP structured block");
9830 error_at (token->location, "break statement used with OpenMP for loop");
9833 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9837 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9840 error_at (token->location, "continue statement not within a loop");
9842 case IN_ITERATION_STMT:
9844 statement = finish_continue_stmt ();
9847 error_at (token->location, "invalid exit from OpenMP structured block");
9852 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9858 bool expr_non_constant_p;
9860 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9862 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9863 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9865 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9866 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9868 /* If the next token is a `;', then there is no
9871 /* Build the return-statement. */
9872 statement = finish_return_stmt (expr);
9873 /* Look for the final `;'. */
9874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9879 /* Create the goto-statement. */
9880 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9882 /* Issue a warning about this use of a GNU extension. */
9883 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9884 /* Consume the '*' token. */
9885 cp_lexer_consume_token (parser->lexer);
9886 /* Parse the dependent expression. */
9887 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9890 finish_goto_stmt (cp_parser_identifier (parser));
9891 /* Look for the final `;'. */
9892 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9896 cp_parser_error (parser, "expected jump-statement");
9903 /* Parse a declaration-statement.
9905 declaration-statement:
9906 block-declaration */
9909 cp_parser_declaration_statement (cp_parser* parser)
9913 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9914 p = obstack_alloc (&declarator_obstack, 0);
9916 /* Parse the block-declaration. */
9917 cp_parser_block_declaration (parser, /*statement_p=*/true);
9919 /* Free any declarators allocated. */
9920 obstack_free (&declarator_obstack, p);
9922 /* Finish off the statement. */
9926 /* Some dependent statements (like `if (cond) statement'), are
9927 implicitly in their own scope. In other words, if the statement is
9928 a single statement (as opposed to a compound-statement), it is
9929 none-the-less treated as if it were enclosed in braces. Any
9930 declarations appearing in the dependent statement are out of scope
9931 after control passes that point. This function parses a statement,
9932 but ensures that is in its own scope, even if it is not a
9935 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9936 is a (possibly labeled) if statement which is not enclosed in
9937 braces and has an else clause. This is used to implement
9940 Returns the new statement. */
9943 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9950 /* Mark if () ; with a special NOP_EXPR. */
9951 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9953 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9954 cp_lexer_consume_token (parser->lexer);
9955 statement = add_stmt (build_empty_stmt (loc));
9957 /* if a compound is opened, we simply parse the statement directly. */
9958 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9959 statement = cp_parser_compound_statement (parser, NULL, false, false);
9960 /* If the token is not a `{', then we must take special action. */
9963 /* Create a compound-statement. */
9964 statement = begin_compound_stmt (0);
9965 /* Parse the dependent-statement. */
9966 cp_parser_statement (parser, NULL_TREE, false, if_p);
9967 /* Finish the dummy compound-statement. */
9968 finish_compound_stmt (statement);
9971 /* Return the statement. */
9975 /* For some dependent statements (like `while (cond) statement'), we
9976 have already created a scope. Therefore, even if the dependent
9977 statement is a compound-statement, we do not want to create another
9981 cp_parser_already_scoped_statement (cp_parser* parser)
9983 /* If the token is a `{', then we must take special action. */
9984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9985 cp_parser_statement (parser, NULL_TREE, false, NULL);
9988 /* Avoid calling cp_parser_compound_statement, so that we
9989 don't create a new scope. Do everything else by hand. */
9990 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9991 /* If the next keyword is `__label__' we have a label declaration. */
9992 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9993 cp_parser_label_declaration (parser);
9994 /* Parse an (optional) statement-seq. */
9995 cp_parser_statement_seq_opt (parser, NULL_TREE);
9996 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10000 /* Declarations [gram.dcl.dcl] */
10002 /* Parse an optional declaration-sequence.
10006 declaration-seq declaration */
10009 cp_parser_declaration_seq_opt (cp_parser* parser)
10015 token = cp_lexer_peek_token (parser->lexer);
10017 if (token->type == CPP_CLOSE_BRACE
10018 || token->type == CPP_EOF
10019 || token->type == CPP_PRAGMA_EOL)
10022 if (token->type == CPP_SEMICOLON)
10024 /* A declaration consisting of a single semicolon is
10025 invalid. Allow it unless we're being pedantic. */
10026 cp_lexer_consume_token (parser->lexer);
10027 if (!in_system_header)
10028 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10032 /* If we're entering or exiting a region that's implicitly
10033 extern "C", modify the lang context appropriately. */
10034 if (!parser->implicit_extern_c && token->implicit_extern_c)
10036 push_lang_context (lang_name_c);
10037 parser->implicit_extern_c = true;
10039 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10041 pop_lang_context ();
10042 parser->implicit_extern_c = false;
10045 if (token->type == CPP_PRAGMA)
10047 /* A top-level declaration can consist solely of a #pragma.
10048 A nested declaration cannot, so this is done here and not
10049 in cp_parser_declaration. (A #pragma at block scope is
10050 handled in cp_parser_statement.) */
10051 cp_parser_pragma (parser, pragma_external);
10055 /* Parse the declaration itself. */
10056 cp_parser_declaration (parser);
10060 /* Parse a declaration.
10064 function-definition
10065 template-declaration
10066 explicit-instantiation
10067 explicit-specialization
10068 linkage-specification
10069 namespace-definition
10074 __extension__ declaration */
10077 cp_parser_declaration (cp_parser* parser)
10081 int saved_pedantic;
10083 tree attributes = NULL_TREE;
10085 /* Check for the `__extension__' keyword. */
10086 if (cp_parser_extension_opt (parser, &saved_pedantic))
10088 /* Parse the qualified declaration. */
10089 cp_parser_declaration (parser);
10090 /* Restore the PEDANTIC flag. */
10091 pedantic = saved_pedantic;
10096 /* Try to figure out what kind of declaration is present. */
10097 token1 = *cp_lexer_peek_token (parser->lexer);
10099 if (token1.type != CPP_EOF)
10100 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10103 token2.type = CPP_EOF;
10104 token2.keyword = RID_MAX;
10107 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10108 p = obstack_alloc (&declarator_obstack, 0);
10110 /* If the next token is `extern' and the following token is a string
10111 literal, then we have a linkage specification. */
10112 if (token1.keyword == RID_EXTERN
10113 && cp_parser_is_pure_string_literal (&token2))
10114 cp_parser_linkage_specification (parser);
10115 /* If the next token is `template', then we have either a template
10116 declaration, an explicit instantiation, or an explicit
10118 else if (token1.keyword == RID_TEMPLATE)
10120 /* `template <>' indicates a template specialization. */
10121 if (token2.type == CPP_LESS
10122 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10123 cp_parser_explicit_specialization (parser);
10124 /* `template <' indicates a template declaration. */
10125 else if (token2.type == CPP_LESS)
10126 cp_parser_template_declaration (parser, /*member_p=*/false);
10127 /* Anything else must be an explicit instantiation. */
10129 cp_parser_explicit_instantiation (parser);
10131 /* If the next token is `export', then we have a template
10133 else if (token1.keyword == RID_EXPORT)
10134 cp_parser_template_declaration (parser, /*member_p=*/false);
10135 /* If the next token is `extern', 'static' or 'inline' and the one
10136 after that is `template', we have a GNU extended explicit
10137 instantiation directive. */
10138 else if (cp_parser_allow_gnu_extensions_p (parser)
10139 && (token1.keyword == RID_EXTERN
10140 || token1.keyword == RID_STATIC
10141 || token1.keyword == RID_INLINE)
10142 && token2.keyword == RID_TEMPLATE)
10143 cp_parser_explicit_instantiation (parser);
10144 /* If the next token is `namespace', check for a named or unnamed
10145 namespace definition. */
10146 else if (token1.keyword == RID_NAMESPACE
10147 && (/* A named namespace definition. */
10148 (token2.type == CPP_NAME
10149 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10151 /* An unnamed namespace definition. */
10152 || token2.type == CPP_OPEN_BRACE
10153 || token2.keyword == RID_ATTRIBUTE))
10154 cp_parser_namespace_definition (parser);
10155 /* An inline (associated) namespace definition. */
10156 else if (token1.keyword == RID_INLINE
10157 && token2.keyword == RID_NAMESPACE)
10158 cp_parser_namespace_definition (parser);
10159 /* Objective-C++ declaration/definition. */
10160 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10161 cp_parser_objc_declaration (parser, NULL_TREE);
10162 else if (c_dialect_objc ()
10163 && token1.keyword == RID_ATTRIBUTE
10164 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10165 cp_parser_objc_declaration (parser, attributes);
10166 /* We must have either a block declaration or a function
10169 /* Try to parse a block-declaration, or a function-definition. */
10170 cp_parser_block_declaration (parser, /*statement_p=*/false);
10172 /* Free any declarators allocated. */
10173 obstack_free (&declarator_obstack, p);
10176 /* Parse a block-declaration.
10181 namespace-alias-definition
10188 __extension__ block-declaration
10193 static_assert-declaration
10195 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10196 part of a declaration-statement. */
10199 cp_parser_block_declaration (cp_parser *parser,
10203 int saved_pedantic;
10205 /* Check for the `__extension__' keyword. */
10206 if (cp_parser_extension_opt (parser, &saved_pedantic))
10208 /* Parse the qualified declaration. */
10209 cp_parser_block_declaration (parser, statement_p);
10210 /* Restore the PEDANTIC flag. */
10211 pedantic = saved_pedantic;
10216 /* Peek at the next token to figure out which kind of declaration is
10218 token1 = cp_lexer_peek_token (parser->lexer);
10220 /* If the next keyword is `asm', we have an asm-definition. */
10221 if (token1->keyword == RID_ASM)
10224 cp_parser_commit_to_tentative_parse (parser);
10225 cp_parser_asm_definition (parser);
10227 /* If the next keyword is `namespace', we have a
10228 namespace-alias-definition. */
10229 else if (token1->keyword == RID_NAMESPACE)
10230 cp_parser_namespace_alias_definition (parser);
10231 /* If the next keyword is `using', we have a
10232 using-declaration, a using-directive, or an alias-declaration. */
10233 else if (token1->keyword == RID_USING)
10238 cp_parser_commit_to_tentative_parse (parser);
10239 /* If the token after `using' is `namespace', then we have a
10240 using-directive. */
10241 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10242 if (token2->keyword == RID_NAMESPACE)
10243 cp_parser_using_directive (parser);
10244 /* If the second token after 'using' is '=', then we have an
10245 alias-declaration. */
10246 else if (cxx_dialect >= cxx0x
10247 && token2->type == CPP_NAME
10248 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10249 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10250 == RID_ATTRIBUTE)))
10251 cp_parser_alias_declaration (parser);
10252 /* Otherwise, it's a using-declaration. */
10254 cp_parser_using_declaration (parser,
10255 /*access_declaration_p=*/false);
10257 /* If the next keyword is `__label__' we have a misplaced label
10259 else if (token1->keyword == RID_LABEL)
10261 cp_lexer_consume_token (parser->lexer);
10262 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10263 cp_parser_skip_to_end_of_statement (parser);
10264 /* If the next token is now a `;', consume it. */
10265 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10266 cp_lexer_consume_token (parser->lexer);
10268 /* If the next token is `static_assert' we have a static assertion. */
10269 else if (token1->keyword == RID_STATIC_ASSERT)
10270 cp_parser_static_assert (parser, /*member_p=*/false);
10271 /* Anything else must be a simple-declaration. */
10273 cp_parser_simple_declaration (parser, !statement_p,
10274 /*maybe_range_for_decl*/NULL);
10277 /* Parse a simple-declaration.
10279 simple-declaration:
10280 decl-specifier-seq [opt] init-declarator-list [opt] ;
10282 init-declarator-list:
10284 init-declarator-list , init-declarator
10286 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10287 function-definition as a simple-declaration.
10289 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10290 parsed declaration if it is an uninitialized single declarator not followed
10291 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10292 if present, will not be consumed. */
10295 cp_parser_simple_declaration (cp_parser* parser,
10296 bool function_definition_allowed_p,
10297 tree *maybe_range_for_decl)
10299 cp_decl_specifier_seq decl_specifiers;
10300 int declares_class_or_enum;
10301 bool saw_declarator;
10303 if (maybe_range_for_decl)
10304 *maybe_range_for_decl = NULL_TREE;
10306 /* Defer access checks until we know what is being declared; the
10307 checks for names appearing in the decl-specifier-seq should be
10308 done as if we were in the scope of the thing being declared. */
10309 push_deferring_access_checks (dk_deferred);
10311 /* Parse the decl-specifier-seq. We have to keep track of whether
10312 or not the decl-specifier-seq declares a named class or
10313 enumeration type, since that is the only case in which the
10314 init-declarator-list is allowed to be empty.
10318 In a simple-declaration, the optional init-declarator-list can be
10319 omitted only when declaring a class or enumeration, that is when
10320 the decl-specifier-seq contains either a class-specifier, an
10321 elaborated-type-specifier, or an enum-specifier. */
10322 cp_parser_decl_specifier_seq (parser,
10323 CP_PARSER_FLAGS_OPTIONAL,
10325 &declares_class_or_enum);
10326 /* We no longer need to defer access checks. */
10327 stop_deferring_access_checks ();
10329 /* In a block scope, a valid declaration must always have a
10330 decl-specifier-seq. By not trying to parse declarators, we can
10331 resolve the declaration/expression ambiguity more quickly. */
10332 if (!function_definition_allowed_p
10333 && !decl_specifiers.any_specifiers_p)
10335 cp_parser_error (parser, "expected declaration");
10339 /* If the next two tokens are both identifiers, the code is
10340 erroneous. The usual cause of this situation is code like:
10344 where "T" should name a type -- but does not. */
10345 if (!decl_specifiers.any_type_specifiers_p
10346 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10348 /* If parsing tentatively, we should commit; we really are
10349 looking at a declaration. */
10350 cp_parser_commit_to_tentative_parse (parser);
10355 /* If we have seen at least one decl-specifier, and the next token
10356 is not a parenthesis, then we must be looking at a declaration.
10357 (After "int (" we might be looking at a functional cast.) */
10358 if (decl_specifiers.any_specifiers_p
10359 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10360 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10361 && !cp_parser_error_occurred (parser))
10362 cp_parser_commit_to_tentative_parse (parser);
10364 /* Keep going until we hit the `;' at the end of the simple
10366 saw_declarator = false;
10367 while (cp_lexer_next_token_is_not (parser->lexer,
10371 bool function_definition_p;
10374 if (saw_declarator)
10376 /* If we are processing next declarator, coma is expected */
10377 token = cp_lexer_peek_token (parser->lexer);
10378 gcc_assert (token->type == CPP_COMMA);
10379 cp_lexer_consume_token (parser->lexer);
10380 if (maybe_range_for_decl)
10381 *maybe_range_for_decl = error_mark_node;
10384 saw_declarator = true;
10386 /* Parse the init-declarator. */
10387 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10389 function_definition_allowed_p,
10390 /*member_p=*/false,
10391 declares_class_or_enum,
10392 &function_definition_p,
10393 maybe_range_for_decl);
10394 /* If an error occurred while parsing tentatively, exit quickly.
10395 (That usually happens when in the body of a function; each
10396 statement is treated as a declaration-statement until proven
10398 if (cp_parser_error_occurred (parser))
10400 /* Handle function definitions specially. */
10401 if (function_definition_p)
10403 /* If the next token is a `,', then we are probably
10404 processing something like:
10408 which is erroneous. */
10409 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10411 cp_token *token = cp_lexer_peek_token (parser->lexer);
10412 error_at (token->location,
10414 " declarations and function-definitions is forbidden");
10416 /* Otherwise, we're done with the list of declarators. */
10419 pop_deferring_access_checks ();
10423 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10424 *maybe_range_for_decl = decl;
10425 /* The next token should be either a `,' or a `;'. */
10426 token = cp_lexer_peek_token (parser->lexer);
10427 /* If it's a `,', there are more declarators to come. */
10428 if (token->type == CPP_COMMA)
10429 /* will be consumed next time around */;
10430 /* If it's a `;', we are done. */
10431 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10433 /* Anything else is an error. */
10436 /* If we have already issued an error message we don't need
10437 to issue another one. */
10438 if (decl != error_mark_node
10439 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10440 cp_parser_error (parser, "expected %<,%> or %<;%>");
10441 /* Skip tokens until we reach the end of the statement. */
10442 cp_parser_skip_to_end_of_statement (parser);
10443 /* If the next token is now a `;', consume it. */
10444 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10445 cp_lexer_consume_token (parser->lexer);
10448 /* After the first time around, a function-definition is not
10449 allowed -- even if it was OK at first. For example:
10454 function_definition_allowed_p = false;
10457 /* Issue an error message if no declarators are present, and the
10458 decl-specifier-seq does not itself declare a class or
10460 if (!saw_declarator)
10462 if (cp_parser_declares_only_class_p (parser))
10463 shadow_tag (&decl_specifiers);
10464 /* Perform any deferred access checks. */
10465 perform_deferred_access_checks ();
10468 /* Consume the `;'. */
10469 if (!maybe_range_for_decl)
10470 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10473 pop_deferring_access_checks ();
10476 /* Parse a decl-specifier-seq.
10478 decl-specifier-seq:
10479 decl-specifier-seq [opt] decl-specifier
10482 storage-class-specifier
10493 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10495 The parser flags FLAGS is used to control type-specifier parsing.
10497 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10500 1: one of the decl-specifiers is an elaborated-type-specifier
10501 (i.e., a type declaration)
10502 2: one of the decl-specifiers is an enum-specifier or a
10503 class-specifier (i.e., a type definition)
10508 cp_parser_decl_specifier_seq (cp_parser* parser,
10509 cp_parser_flags flags,
10510 cp_decl_specifier_seq *decl_specs,
10511 int* declares_class_or_enum)
10513 bool constructor_possible_p = !parser->in_declarator_p;
10514 cp_token *start_token = NULL;
10516 /* Clear DECL_SPECS. */
10517 clear_decl_specs (decl_specs);
10519 /* Assume no class or enumeration type is declared. */
10520 *declares_class_or_enum = 0;
10522 /* Keep reading specifiers until there are no more to read. */
10525 bool constructor_p;
10526 bool found_decl_spec;
10529 /* Peek at the next token. */
10530 token = cp_lexer_peek_token (parser->lexer);
10532 /* Save the first token of the decl spec list for error
10535 start_token = token;
10536 /* Handle attributes. */
10537 if (token->keyword == RID_ATTRIBUTE)
10539 /* Parse the attributes. */
10540 decl_specs->attributes
10541 = chainon (decl_specs->attributes,
10542 cp_parser_attributes_opt (parser));
10545 /* Assume we will find a decl-specifier keyword. */
10546 found_decl_spec = true;
10547 /* If the next token is an appropriate keyword, we can simply
10548 add it to the list. */
10549 switch (token->keyword)
10555 if (!at_class_scope_p ())
10557 error_at (token->location, "%<friend%> used outside of class");
10558 cp_lexer_purge_token (parser->lexer);
10562 ++decl_specs->specs[(int) ds_friend];
10563 /* Consume the token. */
10564 cp_lexer_consume_token (parser->lexer);
10568 case RID_CONSTEXPR:
10569 ++decl_specs->specs[(int) ds_constexpr];
10570 cp_lexer_consume_token (parser->lexer);
10573 /* function-specifier:
10580 cp_parser_function_specifier_opt (parser, decl_specs);
10586 ++decl_specs->specs[(int) ds_typedef];
10587 /* Consume the token. */
10588 cp_lexer_consume_token (parser->lexer);
10589 /* A constructor declarator cannot appear in a typedef. */
10590 constructor_possible_p = false;
10591 /* The "typedef" keyword can only occur in a declaration; we
10592 may as well commit at this point. */
10593 cp_parser_commit_to_tentative_parse (parser);
10595 if (decl_specs->storage_class != sc_none)
10596 decl_specs->conflicting_specifiers_p = true;
10599 /* storage-class-specifier:
10609 if (cxx_dialect == cxx98)
10611 /* Consume the token. */
10612 cp_lexer_consume_token (parser->lexer);
10614 /* Complain about `auto' as a storage specifier, if
10615 we're complaining about C++0x compatibility. */
10616 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10617 " changes meaning in C++11; please remove it");
10619 /* Set the storage class anyway. */
10620 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10624 /* C++0x auto type-specifier. */
10625 found_decl_spec = false;
10632 /* Consume the token. */
10633 cp_lexer_consume_token (parser->lexer);
10634 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10638 /* Consume the token. */
10639 cp_lexer_consume_token (parser->lexer);
10640 ++decl_specs->specs[(int) ds_thread];
10644 /* We did not yet find a decl-specifier yet. */
10645 found_decl_spec = false;
10649 if (found_decl_spec
10650 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10651 && token->keyword != RID_CONSTEXPR)
10652 error ("decl-specifier invalid in condition");
10654 /* Constructors are a special case. The `S' in `S()' is not a
10655 decl-specifier; it is the beginning of the declarator. */
10657 = (!found_decl_spec
10658 && constructor_possible_p
10659 && (cp_parser_constructor_declarator_p
10660 (parser, decl_specs->specs[(int) ds_friend] != 0)));
10662 /* If we don't have a DECL_SPEC yet, then we must be looking at
10663 a type-specifier. */
10664 if (!found_decl_spec && !constructor_p)
10666 int decl_spec_declares_class_or_enum;
10667 bool is_cv_qualifier;
10671 = cp_parser_type_specifier (parser, flags,
10673 /*is_declaration=*/true,
10674 &decl_spec_declares_class_or_enum,
10676 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10678 /* If this type-specifier referenced a user-defined type
10679 (a typedef, class-name, etc.), then we can't allow any
10680 more such type-specifiers henceforth.
10684 The longest sequence of decl-specifiers that could
10685 possibly be a type name is taken as the
10686 decl-specifier-seq of a declaration. The sequence shall
10687 be self-consistent as described below.
10691 As a general rule, at most one type-specifier is allowed
10692 in the complete decl-specifier-seq of a declaration. The
10693 only exceptions are the following:
10695 -- const or volatile can be combined with any other
10698 -- signed or unsigned can be combined with char, long,
10706 void g (const int Pc);
10708 Here, Pc is *not* part of the decl-specifier seq; it's
10709 the declarator. Therefore, once we see a type-specifier
10710 (other than a cv-qualifier), we forbid any additional
10711 user-defined types. We *do* still allow things like `int
10712 int' to be considered a decl-specifier-seq, and issue the
10713 error message later. */
10714 if (type_spec && !is_cv_qualifier)
10715 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10716 /* A constructor declarator cannot follow a type-specifier. */
10719 constructor_possible_p = false;
10720 found_decl_spec = true;
10721 if (!is_cv_qualifier)
10722 decl_specs->any_type_specifiers_p = true;
10726 /* If we still do not have a DECL_SPEC, then there are no more
10727 decl-specifiers. */
10728 if (!found_decl_spec)
10731 decl_specs->any_specifiers_p = true;
10732 /* After we see one decl-specifier, further decl-specifiers are
10733 always optional. */
10734 flags |= CP_PARSER_FLAGS_OPTIONAL;
10737 cp_parser_check_decl_spec (decl_specs, start_token->location);
10739 /* Don't allow a friend specifier with a class definition. */
10740 if (decl_specs->specs[(int) ds_friend] != 0
10741 && (*declares_class_or_enum & 2))
10742 error_at (start_token->location,
10743 "class definition may not be declared a friend");
10746 /* Parse an (optional) storage-class-specifier.
10748 storage-class-specifier:
10757 storage-class-specifier:
10760 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10763 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10765 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10768 if (cxx_dialect != cxx98)
10770 /* Fall through for C++98. */
10777 /* Consume the token. */
10778 return cp_lexer_consume_token (parser->lexer)->u.value;
10785 /* Parse an (optional) function-specifier.
10787 function-specifier:
10792 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10793 Updates DECL_SPECS, if it is non-NULL. */
10796 cp_parser_function_specifier_opt (cp_parser* parser,
10797 cp_decl_specifier_seq *decl_specs)
10799 cp_token *token = cp_lexer_peek_token (parser->lexer);
10800 switch (token->keyword)
10804 ++decl_specs->specs[(int) ds_inline];
10808 /* 14.5.2.3 [temp.mem]
10810 A member function template shall not be virtual. */
10811 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10812 error_at (token->location, "templates may not be %<virtual%>");
10813 else if (decl_specs)
10814 ++decl_specs->specs[(int) ds_virtual];
10819 ++decl_specs->specs[(int) ds_explicit];
10826 /* Consume the token. */
10827 return cp_lexer_consume_token (parser->lexer)->u.value;
10830 /* Parse a linkage-specification.
10832 linkage-specification:
10833 extern string-literal { declaration-seq [opt] }
10834 extern string-literal declaration */
10837 cp_parser_linkage_specification (cp_parser* parser)
10841 /* Look for the `extern' keyword. */
10842 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10844 /* Look for the string-literal. */
10845 linkage = cp_parser_string_literal (parser, false, false);
10847 /* Transform the literal into an identifier. If the literal is a
10848 wide-character string, or contains embedded NULs, then we can't
10849 handle it as the user wants. */
10850 if (strlen (TREE_STRING_POINTER (linkage))
10851 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10853 cp_parser_error (parser, "invalid linkage-specification");
10854 /* Assume C++ linkage. */
10855 linkage = lang_name_cplusplus;
10858 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10860 /* We're now using the new linkage. */
10861 push_lang_context (linkage);
10863 /* If the next token is a `{', then we're using the first
10865 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10867 /* Consume the `{' token. */
10868 cp_lexer_consume_token (parser->lexer);
10869 /* Parse the declarations. */
10870 cp_parser_declaration_seq_opt (parser);
10871 /* Look for the closing `}'. */
10872 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10874 /* Otherwise, there's just one declaration. */
10877 bool saved_in_unbraced_linkage_specification_p;
10879 saved_in_unbraced_linkage_specification_p
10880 = parser->in_unbraced_linkage_specification_p;
10881 parser->in_unbraced_linkage_specification_p = true;
10882 cp_parser_declaration (parser);
10883 parser->in_unbraced_linkage_specification_p
10884 = saved_in_unbraced_linkage_specification_p;
10887 /* We're done with the linkage-specification. */
10888 pop_lang_context ();
10891 /* Parse a static_assert-declaration.
10893 static_assert-declaration:
10894 static_assert ( constant-expression , string-literal ) ;
10896 If MEMBER_P, this static_assert is a class member. */
10899 cp_parser_static_assert(cp_parser *parser, bool member_p)
10904 location_t saved_loc;
10907 /* Peek at the `static_assert' token so we can keep track of exactly
10908 where the static assertion started. */
10909 token = cp_lexer_peek_token (parser->lexer);
10910 saved_loc = token->location;
10912 /* Look for the `static_assert' keyword. */
10913 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10917 /* We know we are in a static assertion; commit to any tentative
10919 if (cp_parser_parsing_tentatively (parser))
10920 cp_parser_commit_to_tentative_parse (parser);
10922 /* Parse the `(' starting the static assertion condition. */
10923 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10925 /* Parse the constant-expression. Allow a non-constant expression
10926 here in order to give better diagnostics in finish_static_assert. */
10928 cp_parser_constant_expression (parser,
10929 /*allow_non_constant_p=*/true,
10930 /*non_constant_p=*/&dummy);
10932 /* Parse the separating `,'. */
10933 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10935 /* Parse the string-literal message. */
10936 message = cp_parser_string_literal (parser,
10937 /*translate=*/false,
10940 /* A `)' completes the static assertion. */
10941 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10942 cp_parser_skip_to_closing_parenthesis (parser,
10943 /*recovering=*/true,
10944 /*or_comma=*/false,
10945 /*consume_paren=*/true);
10947 /* A semicolon terminates the declaration. */
10948 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10950 /* Complete the static assertion, which may mean either processing
10951 the static assert now or saving it for template instantiation. */
10952 finish_static_assert (condition, message, saved_loc, member_p);
10955 /* Parse a `decltype' type. Returns the type.
10957 simple-type-specifier:
10958 decltype ( expression ) */
10961 cp_parser_decltype (cp_parser *parser)
10964 bool id_expression_or_member_access_p = false;
10965 const char *saved_message;
10966 bool saved_integral_constant_expression_p;
10967 bool saved_non_integral_constant_expression_p;
10968 cp_token *id_expr_start_token;
10969 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10971 if (start_token->type == CPP_DECLTYPE)
10973 /* Already parsed. */
10974 cp_lexer_consume_token (parser->lexer);
10975 return start_token->u.value;
10978 /* Look for the `decltype' token. */
10979 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10980 return error_mark_node;
10982 /* Types cannot be defined in a `decltype' expression. Save away the
10984 saved_message = parser->type_definition_forbidden_message;
10986 /* And create the new one. */
10987 parser->type_definition_forbidden_message
10988 = G_("types may not be defined in %<decltype%> expressions");
10990 /* The restrictions on constant-expressions do not apply inside
10991 decltype expressions. */
10992 saved_integral_constant_expression_p
10993 = parser->integral_constant_expression_p;
10994 saved_non_integral_constant_expression_p
10995 = parser->non_integral_constant_expression_p;
10996 parser->integral_constant_expression_p = false;
10998 /* Do not actually evaluate the expression. */
10999 ++cp_unevaluated_operand;
11001 /* Do not warn about problems with the expression. */
11002 ++c_inhibit_evaluation_warnings;
11004 /* Parse the opening `('. */
11005 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11006 return error_mark_node;
11008 /* First, try parsing an id-expression. */
11009 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11010 cp_parser_parse_tentatively (parser);
11011 expr = cp_parser_id_expression (parser,
11012 /*template_keyword_p=*/false,
11013 /*check_dependency_p=*/true,
11014 /*template_p=*/NULL,
11015 /*declarator_p=*/false,
11016 /*optional_p=*/false);
11018 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11020 bool non_integral_constant_expression_p = false;
11021 tree id_expression = expr;
11023 const char *error_msg;
11025 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11026 /* Lookup the name we got back from the id-expression. */
11027 expr = cp_parser_lookup_name (parser, expr,
11029 /*is_template=*/false,
11030 /*is_namespace=*/false,
11031 /*check_dependency=*/true,
11032 /*ambiguous_decls=*/NULL,
11033 id_expr_start_token->location);
11036 && expr != error_mark_node
11037 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11038 && TREE_CODE (expr) != TYPE_DECL
11039 && (TREE_CODE (expr) != BIT_NOT_EXPR
11040 || !TYPE_P (TREE_OPERAND (expr, 0)))
11041 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11043 /* Complete lookup of the id-expression. */
11044 expr = (finish_id_expression
11045 (id_expression, expr, parser->scope, &idk,
11046 /*integral_constant_expression_p=*/false,
11047 /*allow_non_integral_constant_expression_p=*/true,
11048 &non_integral_constant_expression_p,
11049 /*template_p=*/false,
11051 /*address_p=*/false,
11052 /*template_arg_p=*/false,
11054 id_expr_start_token->location));
11056 if (expr == error_mark_node)
11057 /* We found an id-expression, but it was something that we
11058 should not have found. This is an error, not something
11059 we can recover from, so note that we found an
11060 id-expression and we'll recover as gracefully as
11062 id_expression_or_member_access_p = true;
11066 && expr != error_mark_node
11067 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11068 /* We have an id-expression. */
11069 id_expression_or_member_access_p = true;
11072 if (!id_expression_or_member_access_p)
11074 /* Abort the id-expression parse. */
11075 cp_parser_abort_tentative_parse (parser);
11077 /* Parsing tentatively, again. */
11078 cp_parser_parse_tentatively (parser);
11080 /* Parse a class member access. */
11081 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11083 /*member_access_only_p=*/true, NULL);
11086 && expr != error_mark_node
11087 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11088 /* We have an id-expression. */
11089 id_expression_or_member_access_p = true;
11092 if (id_expression_or_member_access_p)
11093 /* We have parsed the complete id-expression or member access. */
11094 cp_parser_parse_definitely (parser);
11097 bool saved_greater_than_is_operator_p;
11099 /* Abort our attempt to parse an id-expression or member access
11101 cp_parser_abort_tentative_parse (parser);
11103 /* Within a parenthesized expression, a `>' token is always
11104 the greater-than operator. */
11105 saved_greater_than_is_operator_p
11106 = parser->greater_than_is_operator_p;
11107 parser->greater_than_is_operator_p = true;
11109 /* Parse a full expression. */
11110 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11112 /* The `>' token might be the end of a template-id or
11113 template-parameter-list now. */
11114 parser->greater_than_is_operator_p
11115 = saved_greater_than_is_operator_p;
11118 /* Go back to evaluating expressions. */
11119 --cp_unevaluated_operand;
11120 --c_inhibit_evaluation_warnings;
11122 /* Restore the old message and the integral constant expression
11124 parser->type_definition_forbidden_message = saved_message;
11125 parser->integral_constant_expression_p
11126 = saved_integral_constant_expression_p;
11127 parser->non_integral_constant_expression_p
11128 = saved_non_integral_constant_expression_p;
11130 /* Parse to the closing `)'. */
11131 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11133 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11134 /*consume_paren=*/true);
11135 return error_mark_node;
11138 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11139 tf_warning_or_error);
11141 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11143 start_token->type = CPP_DECLTYPE;
11144 start_token->u.value = expr;
11145 start_token->keyword = RID_MAX;
11146 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11151 /* Special member functions [gram.special] */
11153 /* Parse a conversion-function-id.
11155 conversion-function-id:
11156 operator conversion-type-id
11158 Returns an IDENTIFIER_NODE representing the operator. */
11161 cp_parser_conversion_function_id (cp_parser* parser)
11165 tree saved_qualifying_scope;
11166 tree saved_object_scope;
11167 tree pushed_scope = NULL_TREE;
11169 /* Look for the `operator' token. */
11170 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11171 return error_mark_node;
11172 /* When we parse the conversion-type-id, the current scope will be
11173 reset. However, we need that information in able to look up the
11174 conversion function later, so we save it here. */
11175 saved_scope = parser->scope;
11176 saved_qualifying_scope = parser->qualifying_scope;
11177 saved_object_scope = parser->object_scope;
11178 /* We must enter the scope of the class so that the names of
11179 entities declared within the class are available in the
11180 conversion-type-id. For example, consider:
11187 S::operator I() { ... }
11189 In order to see that `I' is a type-name in the definition, we
11190 must be in the scope of `S'. */
11192 pushed_scope = push_scope (saved_scope);
11193 /* Parse the conversion-type-id. */
11194 type = cp_parser_conversion_type_id (parser);
11195 /* Leave the scope of the class, if any. */
11197 pop_scope (pushed_scope);
11198 /* Restore the saved scope. */
11199 parser->scope = saved_scope;
11200 parser->qualifying_scope = saved_qualifying_scope;
11201 parser->object_scope = saved_object_scope;
11202 /* If the TYPE is invalid, indicate failure. */
11203 if (type == error_mark_node)
11204 return error_mark_node;
11205 return mangle_conv_op_name_for_type (type);
11208 /* Parse a conversion-type-id:
11210 conversion-type-id:
11211 type-specifier-seq conversion-declarator [opt]
11213 Returns the TYPE specified. */
11216 cp_parser_conversion_type_id (cp_parser* parser)
11219 cp_decl_specifier_seq type_specifiers;
11220 cp_declarator *declarator;
11221 tree type_specified;
11223 /* Parse the attributes. */
11224 attributes = cp_parser_attributes_opt (parser);
11225 /* Parse the type-specifiers. */
11226 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11227 /*is_trailing_return=*/false,
11229 /* If that didn't work, stop. */
11230 if (type_specifiers.type == error_mark_node)
11231 return error_mark_node;
11232 /* Parse the conversion-declarator. */
11233 declarator = cp_parser_conversion_declarator_opt (parser);
11235 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11236 /*initialized=*/0, &attributes);
11238 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11240 /* Don't give this error when parsing tentatively. This happens to
11241 work because we always parse this definitively once. */
11242 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11243 && type_uses_auto (type_specified))
11245 error ("invalid use of %<auto%> in conversion operator");
11246 return error_mark_node;
11249 return type_specified;
11252 /* Parse an (optional) conversion-declarator.
11254 conversion-declarator:
11255 ptr-operator conversion-declarator [opt]
11259 static cp_declarator *
11260 cp_parser_conversion_declarator_opt (cp_parser* parser)
11262 enum tree_code code;
11264 cp_cv_quals cv_quals;
11266 /* We don't know if there's a ptr-operator next, or not. */
11267 cp_parser_parse_tentatively (parser);
11268 /* Try the ptr-operator. */
11269 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11270 /* If it worked, look for more conversion-declarators. */
11271 if (cp_parser_parse_definitely (parser))
11273 cp_declarator *declarator;
11275 /* Parse another optional declarator. */
11276 declarator = cp_parser_conversion_declarator_opt (parser);
11278 return cp_parser_make_indirect_declarator
11279 (code, class_type, cv_quals, declarator);
11285 /* Parse an (optional) ctor-initializer.
11288 : mem-initializer-list
11290 Returns TRUE iff the ctor-initializer was actually present. */
11293 cp_parser_ctor_initializer_opt (cp_parser* parser)
11295 /* If the next token is not a `:', then there is no
11296 ctor-initializer. */
11297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11299 /* Do default initialization of any bases and members. */
11300 if (DECL_CONSTRUCTOR_P (current_function_decl))
11301 finish_mem_initializers (NULL_TREE);
11306 /* Consume the `:' token. */
11307 cp_lexer_consume_token (parser->lexer);
11308 /* And the mem-initializer-list. */
11309 cp_parser_mem_initializer_list (parser);
11314 /* Parse a mem-initializer-list.
11316 mem-initializer-list:
11317 mem-initializer ... [opt]
11318 mem-initializer ... [opt] , mem-initializer-list */
11321 cp_parser_mem_initializer_list (cp_parser* parser)
11323 tree mem_initializer_list = NULL_TREE;
11324 cp_token *token = cp_lexer_peek_token (parser->lexer);
11326 /* Let the semantic analysis code know that we are starting the
11327 mem-initializer-list. */
11328 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11329 error_at (token->location,
11330 "only constructors take member initializers");
11332 /* Loop through the list. */
11335 tree mem_initializer;
11337 token = cp_lexer_peek_token (parser->lexer);
11338 /* Parse the mem-initializer. */
11339 mem_initializer = cp_parser_mem_initializer (parser);
11340 /* If the next token is a `...', we're expanding member initializers. */
11341 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11343 /* Consume the `...'. */
11344 cp_lexer_consume_token (parser->lexer);
11346 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11347 can be expanded but members cannot. */
11348 if (mem_initializer != error_mark_node
11349 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11351 error_at (token->location,
11352 "cannot expand initializer for member %<%D%>",
11353 TREE_PURPOSE (mem_initializer));
11354 mem_initializer = error_mark_node;
11357 /* Construct the pack expansion type. */
11358 if (mem_initializer != error_mark_node)
11359 mem_initializer = make_pack_expansion (mem_initializer);
11361 /* Add it to the list, unless it was erroneous. */
11362 if (mem_initializer != error_mark_node)
11364 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11365 mem_initializer_list = mem_initializer;
11367 /* If the next token is not a `,', we're done. */
11368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11370 /* Consume the `,' token. */
11371 cp_lexer_consume_token (parser->lexer);
11374 /* Perform semantic analysis. */
11375 if (DECL_CONSTRUCTOR_P (current_function_decl))
11376 finish_mem_initializers (mem_initializer_list);
11379 /* Parse a mem-initializer.
11382 mem-initializer-id ( expression-list [opt] )
11383 mem-initializer-id braced-init-list
11388 ( expression-list [opt] )
11390 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11391 class) or FIELD_DECL (for a non-static data member) to initialize;
11392 the TREE_VALUE is the expression-list. An empty initialization
11393 list is represented by void_list_node. */
11396 cp_parser_mem_initializer (cp_parser* parser)
11398 tree mem_initializer_id;
11399 tree expression_list;
11401 cp_token *token = cp_lexer_peek_token (parser->lexer);
11403 /* Find out what is being initialized. */
11404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11406 permerror (token->location,
11407 "anachronistic old-style base class initializer");
11408 mem_initializer_id = NULL_TREE;
11412 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11413 if (mem_initializer_id == error_mark_node)
11414 return mem_initializer_id;
11416 member = expand_member_init (mem_initializer_id);
11417 if (member && !DECL_P (member))
11418 in_base_initializer = 1;
11420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11422 bool expr_non_constant_p;
11423 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11424 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11425 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11426 expression_list = build_tree_list (NULL_TREE, expression_list);
11431 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11433 /*allow_expansion_p=*/true,
11434 /*non_constant_p=*/NULL);
11436 return error_mark_node;
11437 expression_list = build_tree_list_vec (vec);
11438 release_tree_vector (vec);
11441 if (expression_list == error_mark_node)
11442 return error_mark_node;
11443 if (!expression_list)
11444 expression_list = void_type_node;
11446 in_base_initializer = 0;
11448 return member ? build_tree_list (member, expression_list) : error_mark_node;
11451 /* Parse a mem-initializer-id.
11453 mem-initializer-id:
11454 :: [opt] nested-name-specifier [opt] class-name
11457 Returns a TYPE indicating the class to be initializer for the first
11458 production. Returns an IDENTIFIER_NODE indicating the data member
11459 to be initialized for the second production. */
11462 cp_parser_mem_initializer_id (cp_parser* parser)
11464 bool global_scope_p;
11465 bool nested_name_specifier_p;
11466 bool template_p = false;
11469 cp_token *token = cp_lexer_peek_token (parser->lexer);
11471 /* `typename' is not allowed in this context ([temp.res]). */
11472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11474 error_at (token->location,
11475 "keyword %<typename%> not allowed in this context (a qualified "
11476 "member initializer is implicitly a type)");
11477 cp_lexer_consume_token (parser->lexer);
11479 /* Look for the optional `::' operator. */
11481 = (cp_parser_global_scope_opt (parser,
11482 /*current_scope_valid_p=*/false)
11484 /* Look for the optional nested-name-specifier. The simplest way to
11489 The keyword `typename' is not permitted in a base-specifier or
11490 mem-initializer; in these contexts a qualified name that
11491 depends on a template-parameter is implicitly assumed to be a
11494 is to assume that we have seen the `typename' keyword at this
11496 nested_name_specifier_p
11497 = (cp_parser_nested_name_specifier_opt (parser,
11498 /*typename_keyword_p=*/true,
11499 /*check_dependency_p=*/true,
11501 /*is_declaration=*/true)
11503 if (nested_name_specifier_p)
11504 template_p = cp_parser_optional_template_keyword (parser);
11505 /* If there is a `::' operator or a nested-name-specifier, then we
11506 are definitely looking for a class-name. */
11507 if (global_scope_p || nested_name_specifier_p)
11508 return cp_parser_class_name (parser,
11509 /*typename_keyword_p=*/true,
11510 /*template_keyword_p=*/template_p,
11512 /*check_dependency_p=*/true,
11513 /*class_head_p=*/false,
11514 /*is_declaration=*/true);
11515 /* Otherwise, we could also be looking for an ordinary identifier. */
11516 cp_parser_parse_tentatively (parser);
11517 /* Try a class-name. */
11518 id = cp_parser_class_name (parser,
11519 /*typename_keyword_p=*/true,
11520 /*template_keyword_p=*/false,
11522 /*check_dependency_p=*/true,
11523 /*class_head_p=*/false,
11524 /*is_declaration=*/true);
11525 /* If we found one, we're done. */
11526 if (cp_parser_parse_definitely (parser))
11528 /* Otherwise, look for an ordinary identifier. */
11529 return cp_parser_identifier (parser);
11532 /* Overloading [gram.over] */
11534 /* Parse an operator-function-id.
11536 operator-function-id:
11539 Returns an IDENTIFIER_NODE for the operator which is a
11540 human-readable spelling of the identifier, e.g., `operator +'. */
11543 cp_parser_operator_function_id (cp_parser* parser)
11545 /* Look for the `operator' keyword. */
11546 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11547 return error_mark_node;
11548 /* And then the name of the operator itself. */
11549 return cp_parser_operator (parser);
11552 /* Return an identifier node for a user-defined literal operator.
11553 The suffix identifier is chained to the operator name identifier. */
11556 cp_literal_operator_id (const char* name)
11559 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11560 + strlen (name) + 10);
11561 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11562 identifier = get_identifier (buffer);
11563 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11568 /* Parse an operator.
11571 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11572 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11573 || ++ -- , ->* -> () []
11580 Returns an IDENTIFIER_NODE for the operator which is a
11581 human-readable spelling of the identifier, e.g., `operator +'. */
11584 cp_parser_operator (cp_parser* parser)
11586 tree id = NULL_TREE;
11589 /* Peek at the next token. */
11590 token = cp_lexer_peek_token (parser->lexer);
11591 /* Figure out which operator we have. */
11592 switch (token->type)
11598 /* The keyword should be either `new' or `delete'. */
11599 if (token->keyword == RID_NEW)
11601 else if (token->keyword == RID_DELETE)
11606 /* Consume the `new' or `delete' token. */
11607 cp_lexer_consume_token (parser->lexer);
11609 /* Peek at the next token. */
11610 token = cp_lexer_peek_token (parser->lexer);
11611 /* If it's a `[' token then this is the array variant of the
11613 if (token->type == CPP_OPEN_SQUARE)
11615 /* Consume the `[' token. */
11616 cp_lexer_consume_token (parser->lexer);
11617 /* Look for the `]' token. */
11618 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11619 id = ansi_opname (op == NEW_EXPR
11620 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11622 /* Otherwise, we have the non-array variant. */
11624 id = ansi_opname (op);
11630 id = ansi_opname (PLUS_EXPR);
11634 id = ansi_opname (MINUS_EXPR);
11638 id = ansi_opname (MULT_EXPR);
11642 id = ansi_opname (TRUNC_DIV_EXPR);
11646 id = ansi_opname (TRUNC_MOD_EXPR);
11650 id = ansi_opname (BIT_XOR_EXPR);
11654 id = ansi_opname (BIT_AND_EXPR);
11658 id = ansi_opname (BIT_IOR_EXPR);
11662 id = ansi_opname (BIT_NOT_EXPR);
11666 id = ansi_opname (TRUTH_NOT_EXPR);
11670 id = ansi_assopname (NOP_EXPR);
11674 id = ansi_opname (LT_EXPR);
11678 id = ansi_opname (GT_EXPR);
11682 id = ansi_assopname (PLUS_EXPR);
11686 id = ansi_assopname (MINUS_EXPR);
11690 id = ansi_assopname (MULT_EXPR);
11694 id = ansi_assopname (TRUNC_DIV_EXPR);
11698 id = ansi_assopname (TRUNC_MOD_EXPR);
11702 id = ansi_assopname (BIT_XOR_EXPR);
11706 id = ansi_assopname (BIT_AND_EXPR);
11710 id = ansi_assopname (BIT_IOR_EXPR);
11714 id = ansi_opname (LSHIFT_EXPR);
11718 id = ansi_opname (RSHIFT_EXPR);
11721 case CPP_LSHIFT_EQ:
11722 id = ansi_assopname (LSHIFT_EXPR);
11725 case CPP_RSHIFT_EQ:
11726 id = ansi_assopname (RSHIFT_EXPR);
11730 id = ansi_opname (EQ_EXPR);
11734 id = ansi_opname (NE_EXPR);
11738 id = ansi_opname (LE_EXPR);
11741 case CPP_GREATER_EQ:
11742 id = ansi_opname (GE_EXPR);
11746 id = ansi_opname (TRUTH_ANDIF_EXPR);
11750 id = ansi_opname (TRUTH_ORIF_EXPR);
11753 case CPP_PLUS_PLUS:
11754 id = ansi_opname (POSTINCREMENT_EXPR);
11757 case CPP_MINUS_MINUS:
11758 id = ansi_opname (PREDECREMENT_EXPR);
11762 id = ansi_opname (COMPOUND_EXPR);
11765 case CPP_DEREF_STAR:
11766 id = ansi_opname (MEMBER_REF);
11770 id = ansi_opname (COMPONENT_REF);
11773 case CPP_OPEN_PAREN:
11774 /* Consume the `('. */
11775 cp_lexer_consume_token (parser->lexer);
11776 /* Look for the matching `)'. */
11777 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11778 return ansi_opname (CALL_EXPR);
11780 case CPP_OPEN_SQUARE:
11781 /* Consume the `['. */
11782 cp_lexer_consume_token (parser->lexer);
11783 /* Look for the matching `]'. */
11784 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11785 return ansi_opname (ARRAY_REF);
11788 if (cxx_dialect == cxx98)
11789 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11790 if (TREE_STRING_LENGTH (token->u.value) > 2)
11792 error ("expected empty string after %<operator%> keyword");
11793 return error_mark_node;
11795 /* Consume the string. */
11796 cp_lexer_consume_token (parser->lexer);
11797 /* Look for the suffix identifier. */
11798 token = cp_lexer_peek_token (parser->lexer);
11799 if (token->type == CPP_NAME)
11801 id = cp_parser_identifier (parser);
11802 if (id != error_mark_node)
11804 const char *name = IDENTIFIER_POINTER (id);
11805 return cp_literal_operator_id (name);
11810 error ("expected suffix identifier");
11811 return error_mark_node;
11814 case CPP_STRING_USERDEF:
11815 error ("missing space between %<\"\"%> and suffix identifier");
11816 return error_mark_node;
11819 /* Anything else is an error. */
11823 /* If we have selected an identifier, we need to consume the
11826 cp_lexer_consume_token (parser->lexer);
11827 /* Otherwise, no valid operator name was present. */
11830 cp_parser_error (parser, "expected operator");
11831 id = error_mark_node;
11837 /* Parse a template-declaration.
11839 template-declaration:
11840 export [opt] template < template-parameter-list > declaration
11842 If MEMBER_P is TRUE, this template-declaration occurs within a
11845 The grammar rule given by the standard isn't correct. What
11846 is really meant is:
11848 template-declaration:
11849 export [opt] template-parameter-list-seq
11850 decl-specifier-seq [opt] init-declarator [opt] ;
11851 export [opt] template-parameter-list-seq
11852 function-definition
11854 template-parameter-list-seq:
11855 template-parameter-list-seq [opt]
11856 template < template-parameter-list > */
11859 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11861 /* Check for `export'. */
11862 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11864 /* Consume the `export' token. */
11865 cp_lexer_consume_token (parser->lexer);
11866 /* Warn that we do not support `export'. */
11867 warning (0, "keyword %<export%> not implemented, and will be ignored");
11870 cp_parser_template_declaration_after_export (parser, member_p);
11873 /* Parse a template-parameter-list.
11875 template-parameter-list:
11877 template-parameter-list , template-parameter
11879 Returns a TREE_LIST. Each node represents a template parameter.
11880 The nodes are connected via their TREE_CHAINs. */
11883 cp_parser_template_parameter_list (cp_parser* parser)
11885 tree parameter_list = NULL_TREE;
11887 begin_template_parm_list ();
11889 /* The loop below parses the template parms. We first need to know
11890 the total number of template parms to be able to compute proper
11891 canonical types of each dependent type. So after the loop, when
11892 we know the total number of template parms,
11893 end_template_parm_list computes the proper canonical types and
11894 fixes up the dependent types accordingly. */
11899 bool is_parameter_pack;
11900 location_t parm_loc;
11902 /* Parse the template-parameter. */
11903 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11904 parameter = cp_parser_template_parameter (parser,
11906 &is_parameter_pack);
11907 /* Add it to the list. */
11908 if (parameter != error_mark_node)
11909 parameter_list = process_template_parm (parameter_list,
11917 tree err_parm = build_tree_list (parameter, parameter);
11918 parameter_list = chainon (parameter_list, err_parm);
11921 /* If the next token is not a `,', we're done. */
11922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11924 /* Otherwise, consume the `,' token. */
11925 cp_lexer_consume_token (parser->lexer);
11928 return end_template_parm_list (parameter_list);
11931 /* Parse a template-parameter.
11933 template-parameter:
11935 parameter-declaration
11937 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11938 the parameter. The TREE_PURPOSE is the default value, if any.
11939 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11940 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11941 set to true iff this parameter is a parameter pack. */
11944 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11945 bool *is_parameter_pack)
11948 cp_parameter_declarator *parameter_declarator;
11949 cp_declarator *id_declarator;
11952 /* Assume it is a type parameter or a template parameter. */
11953 *is_non_type = false;
11954 /* Assume it not a parameter pack. */
11955 *is_parameter_pack = false;
11956 /* Peek at the next token. */
11957 token = cp_lexer_peek_token (parser->lexer);
11958 /* If it is `class' or `template', we have a type-parameter. */
11959 if (token->keyword == RID_TEMPLATE)
11960 return cp_parser_type_parameter (parser, is_parameter_pack);
11961 /* If it is `class' or `typename' we do not know yet whether it is a
11962 type parameter or a non-type parameter. Consider:
11964 template <typename T, typename T::X X> ...
11968 template <class C, class D*> ...
11970 Here, the first parameter is a type parameter, and the second is
11971 a non-type parameter. We can tell by looking at the token after
11972 the identifier -- if it is a `,', `=', or `>' then we have a type
11974 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11976 /* Peek at the token after `class' or `typename'. */
11977 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11978 /* If it's an ellipsis, we have a template type parameter
11980 if (token->type == CPP_ELLIPSIS)
11981 return cp_parser_type_parameter (parser, is_parameter_pack);
11982 /* If it's an identifier, skip it. */
11983 if (token->type == CPP_NAME)
11984 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11985 /* Now, see if the token looks like the end of a template
11987 if (token->type == CPP_COMMA
11988 || token->type == CPP_EQ
11989 || token->type == CPP_GREATER)
11990 return cp_parser_type_parameter (parser, is_parameter_pack);
11993 /* Otherwise, it is a non-type parameter.
11997 When parsing a default template-argument for a non-type
11998 template-parameter, the first non-nested `>' is taken as the end
11999 of the template parameter-list rather than a greater-than
12001 *is_non_type = true;
12002 parameter_declarator
12003 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12004 /*parenthesized_p=*/NULL);
12006 /* If the parameter declaration is marked as a parameter pack, set
12007 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12008 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12010 if (parameter_declarator
12011 && parameter_declarator->declarator
12012 && parameter_declarator->declarator->parameter_pack_p)
12014 *is_parameter_pack = true;
12015 parameter_declarator->declarator->parameter_pack_p = false;
12018 /* If the next token is an ellipsis, and we don't already have it
12019 marked as a parameter pack, then we have a parameter pack (that
12020 has no declarator). */
12021 if (!*is_parameter_pack
12022 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12023 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12025 /* Consume the `...'. */
12026 cp_lexer_consume_token (parser->lexer);
12027 maybe_warn_variadic_templates ();
12029 *is_parameter_pack = true;
12031 /* We might end up with a pack expansion as the type of the non-type
12032 template parameter, in which case this is a non-type template
12034 else if (parameter_declarator
12035 && parameter_declarator->decl_specifiers.type
12036 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12038 *is_parameter_pack = true;
12039 parameter_declarator->decl_specifiers.type =
12040 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12043 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12045 /* Parameter packs cannot have default arguments. However, a
12046 user may try to do so, so we'll parse them and give an
12047 appropriate diagnostic here. */
12049 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12051 /* Find the name of the parameter pack. */
12052 id_declarator = parameter_declarator->declarator;
12053 while (id_declarator && id_declarator->kind != cdk_id)
12054 id_declarator = id_declarator->declarator;
12056 if (id_declarator && id_declarator->kind == cdk_id)
12057 error_at (start_token->location,
12058 "template parameter pack %qD cannot have a default argument",
12059 id_declarator->u.id.unqualified_name);
12061 error_at (start_token->location,
12062 "template parameter pack cannot have a default argument");
12064 /* Parse the default argument, but throw away the result. */
12065 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12068 parm = grokdeclarator (parameter_declarator->declarator,
12069 ¶meter_declarator->decl_specifiers,
12070 TPARM, /*initialized=*/0,
12071 /*attrlist=*/NULL);
12072 if (parm == error_mark_node)
12073 return error_mark_node;
12075 return build_tree_list (parameter_declarator->default_argument, parm);
12078 /* Parse a type-parameter.
12081 class identifier [opt]
12082 class identifier [opt] = type-id
12083 typename identifier [opt]
12084 typename identifier [opt] = type-id
12085 template < template-parameter-list > class identifier [opt]
12086 template < template-parameter-list > class identifier [opt]
12089 GNU Extension (variadic templates):
12092 class ... identifier [opt]
12093 typename ... identifier [opt]
12095 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12096 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12097 the declaration of the parameter.
12099 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12102 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12107 /* Look for a keyword to tell us what kind of parameter this is. */
12108 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12110 return error_mark_node;
12112 switch (token->keyword)
12118 tree default_argument;
12120 /* If the next token is an ellipsis, we have a template
12122 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12124 /* Consume the `...' token. */
12125 cp_lexer_consume_token (parser->lexer);
12126 maybe_warn_variadic_templates ();
12128 *is_parameter_pack = true;
12131 /* If the next token is an identifier, then it names the
12133 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12134 identifier = cp_parser_identifier (parser);
12136 identifier = NULL_TREE;
12138 /* Create the parameter. */
12139 parameter = finish_template_type_parm (class_type_node, identifier);
12141 /* If the next token is an `=', we have a default argument. */
12142 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12144 /* Consume the `=' token. */
12145 cp_lexer_consume_token (parser->lexer);
12146 /* Parse the default-argument. */
12147 push_deferring_access_checks (dk_no_deferred);
12148 default_argument = cp_parser_type_id (parser);
12150 /* Template parameter packs cannot have default
12152 if (*is_parameter_pack)
12155 error_at (token->location,
12156 "template parameter pack %qD cannot have a "
12157 "default argument", identifier);
12159 error_at (token->location,
12160 "template parameter packs cannot have "
12161 "default arguments");
12162 default_argument = NULL_TREE;
12164 pop_deferring_access_checks ();
12167 default_argument = NULL_TREE;
12169 /* Create the combined representation of the parameter and the
12170 default argument. */
12171 parameter = build_tree_list (default_argument, parameter);
12178 tree default_argument;
12180 /* Look for the `<'. */
12181 cp_parser_require (parser, CPP_LESS, RT_LESS);
12182 /* Parse the template-parameter-list. */
12183 cp_parser_template_parameter_list (parser);
12184 /* Look for the `>'. */
12185 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12186 /* Look for the `class' keyword. */
12187 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12188 /* If the next token is an ellipsis, we have a template
12190 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12192 /* Consume the `...' token. */
12193 cp_lexer_consume_token (parser->lexer);
12194 maybe_warn_variadic_templates ();
12196 *is_parameter_pack = true;
12198 /* If the next token is an `=', then there is a
12199 default-argument. If the next token is a `>', we are at
12200 the end of the parameter-list. If the next token is a `,',
12201 then we are at the end of this parameter. */
12202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12203 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12204 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12206 identifier = cp_parser_identifier (parser);
12207 /* Treat invalid names as if the parameter were nameless. */
12208 if (identifier == error_mark_node)
12209 identifier = NULL_TREE;
12212 identifier = NULL_TREE;
12214 /* Create the template parameter. */
12215 parameter = finish_template_template_parm (class_type_node,
12218 /* If the next token is an `=', then there is a
12219 default-argument. */
12220 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12224 /* Consume the `='. */
12225 cp_lexer_consume_token (parser->lexer);
12226 /* Parse the id-expression. */
12227 push_deferring_access_checks (dk_no_deferred);
12228 /* save token before parsing the id-expression, for error
12230 token = cp_lexer_peek_token (parser->lexer);
12232 = cp_parser_id_expression (parser,
12233 /*template_keyword_p=*/false,
12234 /*check_dependency_p=*/true,
12235 /*template_p=*/&is_template,
12236 /*declarator_p=*/false,
12237 /*optional_p=*/false);
12238 if (TREE_CODE (default_argument) == TYPE_DECL)
12239 /* If the id-expression was a template-id that refers to
12240 a template-class, we already have the declaration here,
12241 so no further lookup is needed. */
12244 /* Look up the name. */
12246 = cp_parser_lookup_name (parser, default_argument,
12248 /*is_template=*/is_template,
12249 /*is_namespace=*/false,
12250 /*check_dependency=*/true,
12251 /*ambiguous_decls=*/NULL,
12253 /* See if the default argument is valid. */
12255 = check_template_template_default_arg (default_argument);
12257 /* Template parameter packs cannot have default
12259 if (*is_parameter_pack)
12262 error_at (token->location,
12263 "template parameter pack %qD cannot "
12264 "have a default argument",
12267 error_at (token->location, "template parameter packs cannot "
12268 "have default arguments");
12269 default_argument = NULL_TREE;
12271 pop_deferring_access_checks ();
12274 default_argument = NULL_TREE;
12276 /* Create the combined representation of the parameter and the
12277 default argument. */
12278 parameter = build_tree_list (default_argument, parameter);
12283 gcc_unreachable ();
12290 /* Parse a template-id.
12293 template-name < template-argument-list [opt] >
12295 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12296 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12297 returned. Otherwise, if the template-name names a function, or set
12298 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12299 names a class, returns a TYPE_DECL for the specialization.
12301 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12302 uninstantiated templates. */
12305 cp_parser_template_id (cp_parser *parser,
12306 bool template_keyword_p,
12307 bool check_dependency_p,
12308 bool is_declaration)
12314 cp_token_position start_of_id = 0;
12315 deferred_access_check *chk;
12316 VEC (deferred_access_check,gc) *access_check;
12317 cp_token *next_token = NULL, *next_token_2 = NULL;
12318 bool is_identifier;
12320 /* If the next token corresponds to a template-id, there is no need
12322 next_token = cp_lexer_peek_token (parser->lexer);
12323 if (next_token->type == CPP_TEMPLATE_ID)
12325 struct tree_check *check_value;
12327 /* Get the stored value. */
12328 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12329 /* Perform any access checks that were deferred. */
12330 access_check = check_value->checks;
12333 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12334 perform_or_defer_access_check (chk->binfo,
12338 /* Return the stored value. */
12339 return check_value->value;
12342 /* Avoid performing name lookup if there is no possibility of
12343 finding a template-id. */
12344 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12345 || (next_token->type == CPP_NAME
12346 && !cp_parser_nth_token_starts_template_argument_list_p
12349 cp_parser_error (parser, "expected template-id");
12350 return error_mark_node;
12353 /* Remember where the template-id starts. */
12354 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12355 start_of_id = cp_lexer_token_position (parser->lexer, false);
12357 push_deferring_access_checks (dk_deferred);
12359 /* Parse the template-name. */
12360 is_identifier = false;
12361 templ = cp_parser_template_name (parser, template_keyword_p,
12362 check_dependency_p,
12365 if (templ == error_mark_node || is_identifier)
12367 pop_deferring_access_checks ();
12371 /* If we find the sequence `[:' after a template-name, it's probably
12372 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12373 parse correctly the argument list. */
12374 next_token = cp_lexer_peek_token (parser->lexer);
12375 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12376 if (next_token->type == CPP_OPEN_SQUARE
12377 && next_token->flags & DIGRAPH
12378 && next_token_2->type == CPP_COLON
12379 && !(next_token_2->flags & PREV_WHITE))
12381 cp_parser_parse_tentatively (parser);
12382 /* Change `:' into `::'. */
12383 next_token_2->type = CPP_SCOPE;
12384 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12386 cp_lexer_consume_token (parser->lexer);
12388 /* Parse the arguments. */
12389 arguments = cp_parser_enclosed_template_argument_list (parser);
12390 if (!cp_parser_parse_definitely (parser))
12392 /* If we couldn't parse an argument list, then we revert our changes
12393 and return simply an error. Maybe this is not a template-id
12395 next_token_2->type = CPP_COLON;
12396 cp_parser_error (parser, "expected %<<%>");
12397 pop_deferring_access_checks ();
12398 return error_mark_node;
12400 /* Otherwise, emit an error about the invalid digraph, but continue
12401 parsing because we got our argument list. */
12402 if (permerror (next_token->location,
12403 "%<<::%> cannot begin a template-argument list"))
12405 static bool hint = false;
12406 inform (next_token->location,
12407 "%<<:%> is an alternate spelling for %<[%>."
12408 " Insert whitespace between %<<%> and %<::%>");
12409 if (!hint && !flag_permissive)
12411 inform (next_token->location, "(if you use %<-fpermissive%>"
12412 " G++ will accept your code)");
12419 /* Look for the `<' that starts the template-argument-list. */
12420 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12422 pop_deferring_access_checks ();
12423 return error_mark_node;
12425 /* Parse the arguments. */
12426 arguments = cp_parser_enclosed_template_argument_list (parser);
12429 /* Build a representation of the specialization. */
12430 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12431 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12432 else if (DECL_TYPE_TEMPLATE_P (templ)
12433 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12435 bool entering_scope;
12436 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12437 template (rather than some instantiation thereof) only if
12438 is not nested within some other construct. For example, in
12439 "template <typename T> void f(T) { A<T>::", A<T> is just an
12440 instantiation of A. */
12441 entering_scope = (template_parm_scope_p ()
12442 && cp_lexer_next_token_is (parser->lexer,
12445 = finish_template_type (templ, arguments, entering_scope);
12449 /* If it's not a class-template or a template-template, it should be
12450 a function-template. */
12451 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12452 || TREE_CODE (templ) == OVERLOAD
12453 || BASELINK_P (templ)));
12455 template_id = lookup_template_function (templ, arguments);
12458 /* If parsing tentatively, replace the sequence of tokens that makes
12459 up the template-id with a CPP_TEMPLATE_ID token. That way,
12460 should we re-parse the token stream, we will not have to repeat
12461 the effort required to do the parse, nor will we issue duplicate
12462 error messages about problems during instantiation of the
12466 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12468 /* Reset the contents of the START_OF_ID token. */
12469 token->type = CPP_TEMPLATE_ID;
12470 /* Retrieve any deferred checks. Do not pop this access checks yet
12471 so the memory will not be reclaimed during token replacing below. */
12472 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12473 token->u.tree_check_value->value = template_id;
12474 token->u.tree_check_value->checks = get_deferred_access_checks ();
12475 token->keyword = RID_MAX;
12477 /* Purge all subsequent tokens. */
12478 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12480 /* ??? Can we actually assume that, if template_id ==
12481 error_mark_node, we will have issued a diagnostic to the
12482 user, as opposed to simply marking the tentative parse as
12484 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12485 error_at (token->location, "parse error in template argument list");
12488 pop_deferring_access_checks ();
12489 return template_id;
12492 /* Parse a template-name.
12497 The standard should actually say:
12501 operator-function-id
12503 A defect report has been filed about this issue.
12505 A conversion-function-id cannot be a template name because they cannot
12506 be part of a template-id. In fact, looking at this code:
12508 a.operator K<int>()
12510 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12511 It is impossible to call a templated conversion-function-id with an
12512 explicit argument list, since the only allowed template parameter is
12513 the type to which it is converting.
12515 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12516 `template' keyword, in a construction like:
12520 In that case `f' is taken to be a template-name, even though there
12521 is no way of knowing for sure.
12523 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12524 name refers to a set of overloaded functions, at least one of which
12525 is a template, or an IDENTIFIER_NODE with the name of the template,
12526 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12527 names are looked up inside uninstantiated templates. */
12530 cp_parser_template_name (cp_parser* parser,
12531 bool template_keyword_p,
12532 bool check_dependency_p,
12533 bool is_declaration,
12534 bool *is_identifier)
12539 cp_token *token = cp_lexer_peek_token (parser->lexer);
12541 /* If the next token is `operator', then we have either an
12542 operator-function-id or a conversion-function-id. */
12543 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12545 /* We don't know whether we're looking at an
12546 operator-function-id or a conversion-function-id. */
12547 cp_parser_parse_tentatively (parser);
12548 /* Try an operator-function-id. */
12549 identifier = cp_parser_operator_function_id (parser);
12550 /* If that didn't work, try a conversion-function-id. */
12551 if (!cp_parser_parse_definitely (parser))
12553 cp_parser_error (parser, "expected template-name");
12554 return error_mark_node;
12557 /* Look for the identifier. */
12559 identifier = cp_parser_identifier (parser);
12561 /* If we didn't find an identifier, we don't have a template-id. */
12562 if (identifier == error_mark_node)
12563 return error_mark_node;
12565 /* If the name immediately followed the `template' keyword, then it
12566 is a template-name. However, if the next token is not `<', then
12567 we do not treat it as a template-name, since it is not being used
12568 as part of a template-id. This enables us to handle constructs
12571 template <typename T> struct S { S(); };
12572 template <typename T> S<T>::S();
12574 correctly. We would treat `S' as a template -- if it were `S<T>'
12575 -- but we do not if there is no `<'. */
12577 if (processing_template_decl
12578 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12580 /* In a declaration, in a dependent context, we pretend that the
12581 "template" keyword was present in order to improve error
12582 recovery. For example, given:
12584 template <typename T> void f(T::X<int>);
12586 we want to treat "X<int>" as a template-id. */
12588 && !template_keyword_p
12589 && parser->scope && TYPE_P (parser->scope)
12590 && check_dependency_p
12591 && dependent_scope_p (parser->scope)
12592 /* Do not do this for dtors (or ctors), since they never
12593 need the template keyword before their name. */
12594 && !constructor_name_p (identifier, parser->scope))
12596 cp_token_position start = 0;
12598 /* Explain what went wrong. */
12599 error_at (token->location, "non-template %qD used as template",
12601 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12602 parser->scope, identifier);
12603 /* If parsing tentatively, find the location of the "<" token. */
12604 if (cp_parser_simulate_error (parser))
12605 start = cp_lexer_token_position (parser->lexer, true);
12606 /* Parse the template arguments so that we can issue error
12607 messages about them. */
12608 cp_lexer_consume_token (parser->lexer);
12609 cp_parser_enclosed_template_argument_list (parser);
12610 /* Skip tokens until we find a good place from which to
12611 continue parsing. */
12612 cp_parser_skip_to_closing_parenthesis (parser,
12613 /*recovering=*/true,
12615 /*consume_paren=*/false);
12616 /* If parsing tentatively, permanently remove the
12617 template argument list. That will prevent duplicate
12618 error messages from being issued about the missing
12619 "template" keyword. */
12621 cp_lexer_purge_tokens_after (parser->lexer, start);
12623 *is_identifier = true;
12627 /* If the "template" keyword is present, then there is generally
12628 no point in doing name-lookup, so we just return IDENTIFIER.
12629 But, if the qualifying scope is non-dependent then we can
12630 (and must) do name-lookup normally. */
12631 if (template_keyword_p
12633 || (TYPE_P (parser->scope)
12634 && dependent_type_p (parser->scope))))
12638 /* Look up the name. */
12639 decl = cp_parser_lookup_name (parser, identifier,
12641 /*is_template=*/true,
12642 /*is_namespace=*/false,
12643 check_dependency_p,
12644 /*ambiguous_decls=*/NULL,
12647 /* If DECL is a template, then the name was a template-name. */
12648 if (TREE_CODE (decl) == TEMPLATE_DECL)
12652 tree fn = NULL_TREE;
12654 /* The standard does not explicitly indicate whether a name that
12655 names a set of overloaded declarations, some of which are
12656 templates, is a template-name. However, such a name should
12657 be a template-name; otherwise, there is no way to form a
12658 template-id for the overloaded templates. */
12659 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12660 if (TREE_CODE (fns) == OVERLOAD)
12661 for (fn = fns; fn; fn = OVL_NEXT (fn))
12662 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12667 /* The name does not name a template. */
12668 cp_parser_error (parser, "expected template-name");
12669 return error_mark_node;
12673 /* If DECL is dependent, and refers to a function, then just return
12674 its name; we will look it up again during template instantiation. */
12675 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12677 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12678 if (TYPE_P (scope) && dependent_type_p (scope))
12685 /* Parse a template-argument-list.
12687 template-argument-list:
12688 template-argument ... [opt]
12689 template-argument-list , template-argument ... [opt]
12691 Returns a TREE_VEC containing the arguments. */
12694 cp_parser_template_argument_list (cp_parser* parser)
12696 tree fixed_args[10];
12697 unsigned n_args = 0;
12698 unsigned alloced = 10;
12699 tree *arg_ary = fixed_args;
12701 bool saved_in_template_argument_list_p;
12703 bool saved_non_ice_p;
12705 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12706 parser->in_template_argument_list_p = true;
12707 /* Even if the template-id appears in an integral
12708 constant-expression, the contents of the argument list do
12710 saved_ice_p = parser->integral_constant_expression_p;
12711 parser->integral_constant_expression_p = false;
12712 saved_non_ice_p = parser->non_integral_constant_expression_p;
12713 parser->non_integral_constant_expression_p = false;
12715 /* Parse the arguments. */
12721 /* Consume the comma. */
12722 cp_lexer_consume_token (parser->lexer);
12724 /* Parse the template-argument. */
12725 argument = cp_parser_template_argument (parser);
12727 /* If the next token is an ellipsis, we're expanding a template
12729 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12731 if (argument == error_mark_node)
12733 cp_token *token = cp_lexer_peek_token (parser->lexer);
12734 error_at (token->location,
12735 "expected parameter pack before %<...%>");
12737 /* Consume the `...' token. */
12738 cp_lexer_consume_token (parser->lexer);
12740 /* Make the argument into a TYPE_PACK_EXPANSION or
12741 EXPR_PACK_EXPANSION. */
12742 argument = make_pack_expansion (argument);
12745 if (n_args == alloced)
12749 if (arg_ary == fixed_args)
12751 arg_ary = XNEWVEC (tree, alloced);
12752 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12755 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12757 arg_ary[n_args++] = argument;
12759 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12761 vec = make_tree_vec (n_args);
12764 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12766 if (arg_ary != fixed_args)
12768 parser->non_integral_constant_expression_p = saved_non_ice_p;
12769 parser->integral_constant_expression_p = saved_ice_p;
12770 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12771 #ifdef ENABLE_CHECKING
12772 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12777 /* Parse a template-argument.
12780 assignment-expression
12784 The representation is that of an assignment-expression, type-id, or
12785 id-expression -- except that the qualified id-expression is
12786 evaluated, so that the value returned is either a DECL or an
12789 Although the standard says "assignment-expression", it forbids
12790 throw-expressions or assignments in the template argument.
12791 Therefore, we use "conditional-expression" instead. */
12794 cp_parser_template_argument (cp_parser* parser)
12799 bool maybe_type_id = false;
12800 cp_token *token = NULL, *argument_start_token = NULL;
12803 /* There's really no way to know what we're looking at, so we just
12804 try each alternative in order.
12808 In a template-argument, an ambiguity between a type-id and an
12809 expression is resolved to a type-id, regardless of the form of
12810 the corresponding template-parameter.
12812 Therefore, we try a type-id first. */
12813 cp_parser_parse_tentatively (parser);
12814 argument = cp_parser_template_type_arg (parser);
12815 /* If there was no error parsing the type-id but the next token is a
12816 '>>', our behavior depends on which dialect of C++ we're
12817 parsing. In C++98, we probably found a typo for '> >'. But there
12818 are type-id which are also valid expressions. For instance:
12820 struct X { int operator >> (int); };
12821 template <int V> struct Foo {};
12824 Here 'X()' is a valid type-id of a function type, but the user just
12825 wanted to write the expression "X() >> 5". Thus, we remember that we
12826 found a valid type-id, but we still try to parse the argument as an
12827 expression to see what happens.
12829 In C++0x, the '>>' will be considered two separate '>'
12831 if (!cp_parser_error_occurred (parser)
12832 && cxx_dialect == cxx98
12833 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12835 maybe_type_id = true;
12836 cp_parser_abort_tentative_parse (parser);
12840 /* If the next token isn't a `,' or a `>', then this argument wasn't
12841 really finished. This means that the argument is not a valid
12843 if (!cp_parser_next_token_ends_template_argument_p (parser))
12844 cp_parser_error (parser, "expected template-argument");
12845 /* If that worked, we're done. */
12846 if (cp_parser_parse_definitely (parser))
12849 /* We're still not sure what the argument will be. */
12850 cp_parser_parse_tentatively (parser);
12851 /* Try a template. */
12852 argument_start_token = cp_lexer_peek_token (parser->lexer);
12853 argument = cp_parser_id_expression (parser,
12854 /*template_keyword_p=*/false,
12855 /*check_dependency_p=*/true,
12857 /*declarator_p=*/false,
12858 /*optional_p=*/false);
12859 /* If the next token isn't a `,' or a `>', then this argument wasn't
12860 really finished. */
12861 if (!cp_parser_next_token_ends_template_argument_p (parser))
12862 cp_parser_error (parser, "expected template-argument");
12863 if (!cp_parser_error_occurred (parser))
12865 /* Figure out what is being referred to. If the id-expression
12866 was for a class template specialization, then we will have a
12867 TYPE_DECL at this point. There is no need to do name lookup
12868 at this point in that case. */
12869 if (TREE_CODE (argument) != TYPE_DECL)
12870 argument = cp_parser_lookup_name (parser, argument,
12872 /*is_template=*/template_p,
12873 /*is_namespace=*/false,
12874 /*check_dependency=*/true,
12875 /*ambiguous_decls=*/NULL,
12876 argument_start_token->location);
12877 if (TREE_CODE (argument) != TEMPLATE_DECL
12878 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12879 cp_parser_error (parser, "expected template-name");
12881 if (cp_parser_parse_definitely (parser))
12883 /* It must be a non-type argument. There permitted cases are given
12884 in [temp.arg.nontype]:
12886 -- an integral constant-expression of integral or enumeration
12889 -- the name of a non-type template-parameter; or
12891 -- the name of an object or function with external linkage...
12893 -- the address of an object or function with external linkage...
12895 -- a pointer to member... */
12896 /* Look for a non-type template parameter. */
12897 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12899 cp_parser_parse_tentatively (parser);
12900 argument = cp_parser_primary_expression (parser,
12901 /*address_p=*/false,
12903 /*template_arg_p=*/true,
12905 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12906 || !cp_parser_next_token_ends_template_argument_p (parser))
12907 cp_parser_simulate_error (parser);
12908 if (cp_parser_parse_definitely (parser))
12912 /* If the next token is "&", the argument must be the address of an
12913 object or function with external linkage. */
12914 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12916 cp_lexer_consume_token (parser->lexer);
12917 /* See if we might have an id-expression. */
12918 token = cp_lexer_peek_token (parser->lexer);
12919 if (token->type == CPP_NAME
12920 || token->keyword == RID_OPERATOR
12921 || token->type == CPP_SCOPE
12922 || token->type == CPP_TEMPLATE_ID
12923 || token->type == CPP_NESTED_NAME_SPECIFIER)
12925 cp_parser_parse_tentatively (parser);
12926 argument = cp_parser_primary_expression (parser,
12929 /*template_arg_p=*/true,
12931 if (cp_parser_error_occurred (parser)
12932 || !cp_parser_next_token_ends_template_argument_p (parser))
12933 cp_parser_abort_tentative_parse (parser);
12938 if (TREE_CODE (argument) == INDIRECT_REF)
12940 gcc_assert (REFERENCE_REF_P (argument));
12941 argument = TREE_OPERAND (argument, 0);
12944 /* If we're in a template, we represent a qualified-id referring
12945 to a static data member as a SCOPE_REF even if the scope isn't
12946 dependent so that we can check access control later. */
12948 if (TREE_CODE (probe) == SCOPE_REF)
12949 probe = TREE_OPERAND (probe, 1);
12950 if (TREE_CODE (probe) == VAR_DECL)
12952 /* A variable without external linkage might still be a
12953 valid constant-expression, so no error is issued here
12954 if the external-linkage check fails. */
12955 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12956 cp_parser_simulate_error (parser);
12958 else if (is_overloaded_fn (argument))
12959 /* All overloaded functions are allowed; if the external
12960 linkage test does not pass, an error will be issued
12964 && (TREE_CODE (argument) == OFFSET_REF
12965 || TREE_CODE (argument) == SCOPE_REF))
12966 /* A pointer-to-member. */
12968 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12971 cp_parser_simulate_error (parser);
12973 if (cp_parser_parse_definitely (parser))
12976 argument = build_x_unary_op (ADDR_EXPR, argument,
12977 tf_warning_or_error);
12982 /* If the argument started with "&", there are no other valid
12983 alternatives at this point. */
12986 cp_parser_error (parser, "invalid non-type template argument");
12987 return error_mark_node;
12990 /* If the argument wasn't successfully parsed as a type-id followed
12991 by '>>', the argument can only be a constant expression now.
12992 Otherwise, we try parsing the constant-expression tentatively,
12993 because the argument could really be a type-id. */
12995 cp_parser_parse_tentatively (parser);
12996 argument = cp_parser_constant_expression (parser,
12997 /*allow_non_constant_p=*/false,
12998 /*non_constant_p=*/NULL);
12999 argument = fold_non_dependent_expr (argument);
13000 if (!maybe_type_id)
13002 if (!cp_parser_next_token_ends_template_argument_p (parser))
13003 cp_parser_error (parser, "expected template-argument");
13004 if (cp_parser_parse_definitely (parser))
13006 /* We did our best to parse the argument as a non type-id, but that
13007 was the only alternative that matched (albeit with a '>' after
13008 it). We can assume it's just a typo from the user, and a
13009 diagnostic will then be issued. */
13010 return cp_parser_template_type_arg (parser);
13013 /* Parse an explicit-instantiation.
13015 explicit-instantiation:
13016 template declaration
13018 Although the standard says `declaration', what it really means is:
13020 explicit-instantiation:
13021 template decl-specifier-seq [opt] declarator [opt] ;
13023 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13024 supposed to be allowed. A defect report has been filed about this
13029 explicit-instantiation:
13030 storage-class-specifier template
13031 decl-specifier-seq [opt] declarator [opt] ;
13032 function-specifier template
13033 decl-specifier-seq [opt] declarator [opt] ; */
13036 cp_parser_explicit_instantiation (cp_parser* parser)
13038 int declares_class_or_enum;
13039 cp_decl_specifier_seq decl_specifiers;
13040 tree extension_specifier = NULL_TREE;
13042 timevar_push (TV_TEMPLATE_INST);
13044 /* Look for an (optional) storage-class-specifier or
13045 function-specifier. */
13046 if (cp_parser_allow_gnu_extensions_p (parser))
13048 extension_specifier
13049 = cp_parser_storage_class_specifier_opt (parser);
13050 if (!extension_specifier)
13051 extension_specifier
13052 = cp_parser_function_specifier_opt (parser,
13053 /*decl_specs=*/NULL);
13056 /* Look for the `template' keyword. */
13057 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13058 /* Let the front end know that we are processing an explicit
13060 begin_explicit_instantiation ();
13061 /* [temp.explicit] says that we are supposed to ignore access
13062 control while processing explicit instantiation directives. */
13063 push_deferring_access_checks (dk_no_check);
13064 /* Parse a decl-specifier-seq. */
13065 cp_parser_decl_specifier_seq (parser,
13066 CP_PARSER_FLAGS_OPTIONAL,
13068 &declares_class_or_enum);
13069 /* If there was exactly one decl-specifier, and it declared a class,
13070 and there's no declarator, then we have an explicit type
13072 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13076 type = check_tag_decl (&decl_specifiers);
13077 /* Turn access control back on for names used during
13078 template instantiation. */
13079 pop_deferring_access_checks ();
13081 do_type_instantiation (type, extension_specifier,
13082 /*complain=*/tf_error);
13086 cp_declarator *declarator;
13089 /* Parse the declarator. */
13091 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13092 /*ctor_dtor_or_conv_p=*/NULL,
13093 /*parenthesized_p=*/NULL,
13094 /*member_p=*/false);
13095 if (declares_class_or_enum & 2)
13096 cp_parser_check_for_definition_in_return_type (declarator,
13097 decl_specifiers.type,
13098 decl_specifiers.type_location);
13099 if (declarator != cp_error_declarator)
13101 if (decl_specifiers.specs[(int)ds_inline])
13102 permerror (input_location, "explicit instantiation shall not use"
13103 " %<inline%> specifier");
13104 if (decl_specifiers.specs[(int)ds_constexpr])
13105 permerror (input_location, "explicit instantiation shall not use"
13106 " %<constexpr%> specifier");
13108 decl = grokdeclarator (declarator, &decl_specifiers,
13109 NORMAL, 0, &decl_specifiers.attributes);
13110 /* Turn access control back on for names used during
13111 template instantiation. */
13112 pop_deferring_access_checks ();
13113 /* Do the explicit instantiation. */
13114 do_decl_instantiation (decl, extension_specifier);
13118 pop_deferring_access_checks ();
13119 /* Skip the body of the explicit instantiation. */
13120 cp_parser_skip_to_end_of_statement (parser);
13123 /* We're done with the instantiation. */
13124 end_explicit_instantiation ();
13126 cp_parser_consume_semicolon_at_end_of_statement (parser);
13128 timevar_pop (TV_TEMPLATE_INST);
13131 /* Parse an explicit-specialization.
13133 explicit-specialization:
13134 template < > declaration
13136 Although the standard says `declaration', what it really means is:
13138 explicit-specialization:
13139 template <> decl-specifier [opt] init-declarator [opt] ;
13140 template <> function-definition
13141 template <> explicit-specialization
13142 template <> template-declaration */
13145 cp_parser_explicit_specialization (cp_parser* parser)
13147 bool need_lang_pop;
13148 cp_token *token = cp_lexer_peek_token (parser->lexer);
13150 /* Look for the `template' keyword. */
13151 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13152 /* Look for the `<'. */
13153 cp_parser_require (parser, CPP_LESS, RT_LESS);
13154 /* Look for the `>'. */
13155 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13156 /* We have processed another parameter list. */
13157 ++parser->num_template_parameter_lists;
13160 A template ... explicit specialization ... shall not have C
13162 if (current_lang_name == lang_name_c)
13164 error_at (token->location, "template specialization with C linkage");
13165 /* Give it C++ linkage to avoid confusing other parts of the
13167 push_lang_context (lang_name_cplusplus);
13168 need_lang_pop = true;
13171 need_lang_pop = false;
13172 /* Let the front end know that we are beginning a specialization. */
13173 if (!begin_specialization ())
13175 end_specialization ();
13179 /* If the next keyword is `template', we need to figure out whether
13180 or not we're looking a template-declaration. */
13181 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13183 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13184 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13185 cp_parser_template_declaration_after_export (parser,
13186 /*member_p=*/false);
13188 cp_parser_explicit_specialization (parser);
13191 /* Parse the dependent declaration. */
13192 cp_parser_single_declaration (parser,
13194 /*member_p=*/false,
13195 /*explicit_specialization_p=*/true,
13196 /*friend_p=*/NULL);
13197 /* We're done with the specialization. */
13198 end_specialization ();
13199 /* For the erroneous case of a template with C linkage, we pushed an
13200 implicit C++ linkage scope; exit that scope now. */
13202 pop_lang_context ();
13203 /* We're done with this parameter list. */
13204 --parser->num_template_parameter_lists;
13207 /* Parse a type-specifier.
13210 simple-type-specifier
13213 elaborated-type-specifier
13221 Returns a representation of the type-specifier. For a
13222 class-specifier, enum-specifier, or elaborated-type-specifier, a
13223 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13225 The parser flags FLAGS is used to control type-specifier parsing.
13227 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13228 in a decl-specifier-seq.
13230 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13231 class-specifier, enum-specifier, or elaborated-type-specifier, then
13232 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13233 if a type is declared; 2 if it is defined. Otherwise, it is set to
13236 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13237 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13238 is set to FALSE. */
13241 cp_parser_type_specifier (cp_parser* parser,
13242 cp_parser_flags flags,
13243 cp_decl_specifier_seq *decl_specs,
13244 bool is_declaration,
13245 int* declares_class_or_enum,
13246 bool* is_cv_qualifier)
13248 tree type_spec = NULL_TREE;
13251 cp_decl_spec ds = ds_last;
13253 /* Assume this type-specifier does not declare a new type. */
13254 if (declares_class_or_enum)
13255 *declares_class_or_enum = 0;
13256 /* And that it does not specify a cv-qualifier. */
13257 if (is_cv_qualifier)
13258 *is_cv_qualifier = false;
13259 /* Peek at the next token. */
13260 token = cp_lexer_peek_token (parser->lexer);
13262 /* If we're looking at a keyword, we can use that to guide the
13263 production we choose. */
13264 keyword = token->keyword;
13268 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13269 goto elaborated_type_specifier;
13271 /* Look for the enum-specifier. */
13272 type_spec = cp_parser_enum_specifier (parser);
13273 /* If that worked, we're done. */
13276 if (declares_class_or_enum)
13277 *declares_class_or_enum = 2;
13279 cp_parser_set_decl_spec_type (decl_specs,
13282 /*type_definition_p=*/true);
13286 goto elaborated_type_specifier;
13288 /* Any of these indicate either a class-specifier, or an
13289 elaborated-type-specifier. */
13293 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13294 goto elaborated_type_specifier;
13296 /* Parse tentatively so that we can back up if we don't find a
13297 class-specifier. */
13298 cp_parser_parse_tentatively (parser);
13299 /* Look for the class-specifier. */
13300 type_spec = cp_parser_class_specifier (parser);
13301 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13302 /* If that worked, we're done. */
13303 if (cp_parser_parse_definitely (parser))
13305 if (declares_class_or_enum)
13306 *declares_class_or_enum = 2;
13308 cp_parser_set_decl_spec_type (decl_specs,
13311 /*type_definition_p=*/true);
13315 /* Fall through. */
13316 elaborated_type_specifier:
13317 /* We're declaring (not defining) a class or enum. */
13318 if (declares_class_or_enum)
13319 *declares_class_or_enum = 1;
13321 /* Fall through. */
13323 /* Look for an elaborated-type-specifier. */
13325 = (cp_parser_elaborated_type_specifier
13327 decl_specs && decl_specs->specs[(int) ds_friend],
13330 cp_parser_set_decl_spec_type (decl_specs,
13333 /*type_definition_p=*/false);
13338 if (is_cv_qualifier)
13339 *is_cv_qualifier = true;
13344 if (is_cv_qualifier)
13345 *is_cv_qualifier = true;
13350 if (is_cv_qualifier)
13351 *is_cv_qualifier = true;
13355 /* The `__complex__' keyword is a GNU extension. */
13363 /* Handle simple keywords. */
13368 ++decl_specs->specs[(int)ds];
13369 decl_specs->any_specifiers_p = true;
13371 return cp_lexer_consume_token (parser->lexer)->u.value;
13374 /* If we do not already have a type-specifier, assume we are looking
13375 at a simple-type-specifier. */
13376 type_spec = cp_parser_simple_type_specifier (parser,
13380 /* If we didn't find a type-specifier, and a type-specifier was not
13381 optional in this context, issue an error message. */
13382 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13384 cp_parser_error (parser, "expected type specifier");
13385 return error_mark_node;
13391 /* Parse a simple-type-specifier.
13393 simple-type-specifier:
13394 :: [opt] nested-name-specifier [opt] type-name
13395 :: [opt] nested-name-specifier template template-id
13410 simple-type-specifier:
13412 decltype ( expression )
13415 __underlying_type ( type-id )
13419 simple-type-specifier:
13421 __typeof__ unary-expression
13422 __typeof__ ( type-id )
13424 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13425 appropriately updated. */
13428 cp_parser_simple_type_specifier (cp_parser* parser,
13429 cp_decl_specifier_seq *decl_specs,
13430 cp_parser_flags flags)
13432 tree type = NULL_TREE;
13435 /* Peek at the next token. */
13436 token = cp_lexer_peek_token (parser->lexer);
13438 /* If we're looking at a keyword, things are easy. */
13439 switch (token->keyword)
13443 decl_specs->explicit_char_p = true;
13444 type = char_type_node;
13447 type = char16_type_node;
13450 type = char32_type_node;
13453 type = wchar_type_node;
13456 type = boolean_type_node;
13460 ++decl_specs->specs[(int) ds_short];
13461 type = short_integer_type_node;
13465 decl_specs->explicit_int_p = true;
13466 type = integer_type_node;
13469 if (!int128_integer_type_node)
13472 decl_specs->explicit_int128_p = true;
13473 type = int128_integer_type_node;
13477 ++decl_specs->specs[(int) ds_long];
13478 type = long_integer_type_node;
13482 ++decl_specs->specs[(int) ds_signed];
13483 type = integer_type_node;
13487 ++decl_specs->specs[(int) ds_unsigned];
13488 type = unsigned_type_node;
13491 type = float_type_node;
13494 type = double_type_node;
13497 type = void_type_node;
13501 maybe_warn_cpp0x (CPP0X_AUTO);
13502 type = make_auto ();
13506 /* Since DR 743, decltype can either be a simple-type-specifier by
13507 itself or begin a nested-name-specifier. Parsing it will replace
13508 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13509 handling below decide what to do. */
13510 cp_parser_decltype (parser);
13511 cp_lexer_set_token_position (parser->lexer, token);
13515 /* Consume the `typeof' token. */
13516 cp_lexer_consume_token (parser->lexer);
13517 /* Parse the operand to `typeof'. */
13518 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13519 /* If it is not already a TYPE, take its type. */
13520 if (!TYPE_P (type))
13521 type = finish_typeof (type);
13524 cp_parser_set_decl_spec_type (decl_specs, type,
13526 /*type_definition_p=*/false);
13530 case RID_UNDERLYING_TYPE:
13531 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13533 cp_parser_set_decl_spec_type (decl_specs, type,
13535 /*type_definition_p=*/false);
13540 case RID_DIRECT_BASES:
13541 type = cp_parser_trait_expr (parser, token->keyword);
13543 cp_parser_set_decl_spec_type (decl_specs, type,
13545 /*type_definition_p=*/false);
13551 /* If token is an already-parsed decltype not followed by ::,
13552 it's a simple-type-specifier. */
13553 if (token->type == CPP_DECLTYPE
13554 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13556 type = token->u.value;
13558 cp_parser_set_decl_spec_type (decl_specs, type,
13560 /*type_definition_p=*/false);
13561 cp_lexer_consume_token (parser->lexer);
13565 /* If the type-specifier was for a built-in type, we're done. */
13568 /* Record the type. */
13570 && (token->keyword != RID_SIGNED
13571 && token->keyword != RID_UNSIGNED
13572 && token->keyword != RID_SHORT
13573 && token->keyword != RID_LONG))
13574 cp_parser_set_decl_spec_type (decl_specs,
13577 /*type_definition_p=*/false);
13579 decl_specs->any_specifiers_p = true;
13581 /* Consume the token. */
13582 cp_lexer_consume_token (parser->lexer);
13584 /* There is no valid C++ program where a non-template type is
13585 followed by a "<". That usually indicates that the user thought
13586 that the type was a template. */
13587 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13589 return TYPE_NAME (type);
13592 /* The type-specifier must be a user-defined type. */
13593 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13598 /* Don't gobble tokens or issue error messages if this is an
13599 optional type-specifier. */
13600 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13601 cp_parser_parse_tentatively (parser);
13603 /* Look for the optional `::' operator. */
13605 = (cp_parser_global_scope_opt (parser,
13606 /*current_scope_valid_p=*/false)
13608 /* Look for the nested-name specifier. */
13610 = (cp_parser_nested_name_specifier_opt (parser,
13611 /*typename_keyword_p=*/false,
13612 /*check_dependency_p=*/true,
13614 /*is_declaration=*/false)
13616 token = cp_lexer_peek_token (parser->lexer);
13617 /* If we have seen a nested-name-specifier, and the next token
13618 is `template', then we are using the template-id production. */
13620 && cp_parser_optional_template_keyword (parser))
13622 /* Look for the template-id. */
13623 type = cp_parser_template_id (parser,
13624 /*template_keyword_p=*/true,
13625 /*check_dependency_p=*/true,
13626 /*is_declaration=*/false);
13627 /* If the template-id did not name a type, we are out of
13629 if (TREE_CODE (type) != TYPE_DECL)
13631 cp_parser_error (parser, "expected template-id for type");
13635 /* Otherwise, look for a type-name. */
13637 type = cp_parser_type_name (parser);
13638 /* Keep track of all name-lookups performed in class scopes. */
13642 && TREE_CODE (type) == TYPE_DECL
13643 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13644 maybe_note_name_used_in_class (DECL_NAME (type), type);
13645 /* If it didn't work out, we don't have a TYPE. */
13646 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13647 && !cp_parser_parse_definitely (parser))
13649 if (type && decl_specs)
13650 cp_parser_set_decl_spec_type (decl_specs, type,
13652 /*type_definition_p=*/false);
13655 /* If we didn't get a type-name, issue an error message. */
13656 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13658 cp_parser_error (parser, "expected type-name");
13659 return error_mark_node;
13662 if (type && type != error_mark_node)
13664 /* See if TYPE is an Objective-C type, and if so, parse and
13665 accept any protocol references following it. Do this before
13666 the cp_parser_check_for_invalid_template_id() call, because
13667 Objective-C types can be followed by '<...>' which would
13668 enclose protocol names rather than template arguments, and so
13669 everything is fine. */
13670 if (c_dialect_objc () && !parser->scope
13671 && (objc_is_id (type) || objc_is_class_name (type)))
13673 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13674 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13676 /* Clobber the "unqualified" type previously entered into
13677 DECL_SPECS with the new, improved protocol-qualified version. */
13679 decl_specs->type = qual_type;
13684 /* There is no valid C++ program where a non-template type is
13685 followed by a "<". That usually indicates that the user
13686 thought that the type was a template. */
13687 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13694 /* Parse a type-name.
13700 simple-template-id [in c++0x]
13708 Returns a TYPE_DECL for the type. */
13711 cp_parser_type_name (cp_parser* parser)
13715 /* We can't know yet whether it is a class-name or not. */
13716 cp_parser_parse_tentatively (parser);
13717 /* Try a class-name. */
13718 type_decl = cp_parser_class_name (parser,
13719 /*typename_keyword_p=*/false,
13720 /*template_keyword_p=*/false,
13722 /*check_dependency_p=*/true,
13723 /*class_head_p=*/false,
13724 /*is_declaration=*/false);
13725 /* If it's not a class-name, keep looking. */
13726 if (!cp_parser_parse_definitely (parser))
13728 if (cxx_dialect < cxx0x)
13729 /* It must be a typedef-name or an enum-name. */
13730 return cp_parser_nonclass_name (parser);
13732 cp_parser_parse_tentatively (parser);
13733 /* It is either a simple-template-id representing an
13734 instantiation of an alias template... */
13735 type_decl = cp_parser_template_id (parser,
13736 /*template_keyword_p=*/false,
13737 /*check_dependency_p=*/false,
13738 /*is_declaration=*/false);
13739 /* Note that this must be an instantiation of an alias template
13740 because [temp.names]/6 says:
13742 A template-id that names an alias template specialization
13745 Whereas [temp.names]/7 says:
13747 A simple-template-id that names a class template
13748 specialization is a class-name. */
13749 if (type_decl != NULL_TREE
13750 && TREE_CODE (type_decl) == TYPE_DECL
13751 && TYPE_DECL_ALIAS_P (type_decl))
13752 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13754 cp_parser_simulate_error (parser);
13756 if (!cp_parser_parse_definitely (parser))
13757 /* ... Or a typedef-name or an enum-name. */
13758 return cp_parser_nonclass_name (parser);
13764 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13772 Returns a TYPE_DECL for the type. */
13775 cp_parser_nonclass_name (cp_parser* parser)
13780 cp_token *token = cp_lexer_peek_token (parser->lexer);
13781 identifier = cp_parser_identifier (parser);
13782 if (identifier == error_mark_node)
13783 return error_mark_node;
13785 /* Look up the type-name. */
13786 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13788 /* If it is a using decl, use its underlying decl. */
13789 type_decl = strip_using_decl (type_decl);
13791 if (TREE_CODE (type_decl) != TYPE_DECL
13792 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13794 /* See if this is an Objective-C type. */
13795 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13796 tree type = objc_get_protocol_qualified_type (identifier, protos);
13798 type_decl = TYPE_NAME (type);
13801 /* Issue an error if we did not find a type-name. */
13802 if (TREE_CODE (type_decl) != TYPE_DECL
13803 /* In Objective-C, we have the complication that class names are
13804 normally type names and start declarations (eg, the
13805 "NSObject" in "NSObject *object;"), but can be used in an
13806 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13807 is an expression. So, a classname followed by a dot is not a
13808 valid type-name. */
13809 || (objc_is_class_name (TREE_TYPE (type_decl))
13810 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13812 if (!cp_parser_simulate_error (parser))
13813 cp_parser_name_lookup_error (parser, identifier, type_decl,
13814 NLE_TYPE, token->location);
13815 return error_mark_node;
13817 /* Remember that the name was used in the definition of the
13818 current class so that we can check later to see if the
13819 meaning would have been different after the class was
13820 entirely defined. */
13821 else if (type_decl != error_mark_node
13823 maybe_note_name_used_in_class (identifier, type_decl);
13828 /* Parse an elaborated-type-specifier. Note that the grammar given
13829 here incorporates the resolution to DR68.
13831 elaborated-type-specifier:
13832 class-key :: [opt] nested-name-specifier [opt] identifier
13833 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13834 enum-key :: [opt] nested-name-specifier [opt] identifier
13835 typename :: [opt] nested-name-specifier identifier
13836 typename :: [opt] nested-name-specifier template [opt]
13841 elaborated-type-specifier:
13842 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13843 class-key attributes :: [opt] nested-name-specifier [opt]
13844 template [opt] template-id
13845 enum attributes :: [opt] nested-name-specifier [opt] identifier
13847 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13848 declared `friend'. If IS_DECLARATION is TRUE, then this
13849 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13850 something is being declared.
13852 Returns the TYPE specified. */
13855 cp_parser_elaborated_type_specifier (cp_parser* parser,
13857 bool is_declaration)
13859 enum tag_types tag_type;
13861 tree type = NULL_TREE;
13862 tree attributes = NULL_TREE;
13864 cp_token *token = NULL;
13866 /* See if we're looking at the `enum' keyword. */
13867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13869 /* Consume the `enum' token. */
13870 cp_lexer_consume_token (parser->lexer);
13871 /* Remember that it's an enumeration type. */
13872 tag_type = enum_type;
13873 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13874 enums) is used here. */
13875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13876 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13878 pedwarn (input_location, 0, "elaborated-type-specifier "
13879 "for a scoped enum must not use the %<%D%> keyword",
13880 cp_lexer_peek_token (parser->lexer)->u.value);
13881 /* Consume the `struct' or `class' and parse it anyway. */
13882 cp_lexer_consume_token (parser->lexer);
13884 /* Parse the attributes. */
13885 attributes = cp_parser_attributes_opt (parser);
13887 /* Or, it might be `typename'. */
13888 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13891 /* Consume the `typename' token. */
13892 cp_lexer_consume_token (parser->lexer);
13893 /* Remember that it's a `typename' type. */
13894 tag_type = typename_type;
13896 /* Otherwise it must be a class-key. */
13899 tag_type = cp_parser_class_key (parser);
13900 if (tag_type == none_type)
13901 return error_mark_node;
13902 /* Parse the attributes. */
13903 attributes = cp_parser_attributes_opt (parser);
13906 /* Look for the `::' operator. */
13907 globalscope = cp_parser_global_scope_opt (parser,
13908 /*current_scope_valid_p=*/false);
13909 /* Look for the nested-name-specifier. */
13910 if (tag_type == typename_type && !globalscope)
13912 if (!cp_parser_nested_name_specifier (parser,
13913 /*typename_keyword_p=*/true,
13914 /*check_dependency_p=*/true,
13917 return error_mark_node;
13920 /* Even though `typename' is not present, the proposed resolution
13921 to Core Issue 180 says that in `class A<T>::B', `B' should be
13922 considered a type-name, even if `A<T>' is dependent. */
13923 cp_parser_nested_name_specifier_opt (parser,
13924 /*typename_keyword_p=*/true,
13925 /*check_dependency_p=*/true,
13928 /* For everything but enumeration types, consider a template-id.
13929 For an enumeration type, consider only a plain identifier. */
13930 if (tag_type != enum_type)
13932 bool template_p = false;
13935 /* Allow the `template' keyword. */
13936 template_p = cp_parser_optional_template_keyword (parser);
13937 /* If we didn't see `template', we don't know if there's a
13938 template-id or not. */
13940 cp_parser_parse_tentatively (parser);
13941 /* Parse the template-id. */
13942 token = cp_lexer_peek_token (parser->lexer);
13943 decl = cp_parser_template_id (parser, template_p,
13944 /*check_dependency_p=*/true,
13946 /* If we didn't find a template-id, look for an ordinary
13948 if (!template_p && !cp_parser_parse_definitely (parser))
13950 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13951 in effect, then we must assume that, upon instantiation, the
13952 template will correspond to a class. */
13953 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13954 && tag_type == typename_type)
13955 type = make_typename_type (parser->scope, decl,
13957 /*complain=*/tf_error);
13958 /* If the `typename' keyword is in effect and DECL is not a type
13959 decl. Then type is non existant. */
13960 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13963 type = TREE_TYPE (decl);
13968 token = cp_lexer_peek_token (parser->lexer);
13969 identifier = cp_parser_identifier (parser);
13971 if (identifier == error_mark_node)
13973 parser->scope = NULL_TREE;
13974 return error_mark_node;
13977 /* For a `typename', we needn't call xref_tag. */
13978 if (tag_type == typename_type
13979 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13980 return cp_parser_make_typename_type (parser, parser->scope,
13983 /* Look up a qualified name in the usual way. */
13987 tree ambiguous_decls;
13989 decl = cp_parser_lookup_name (parser, identifier,
13991 /*is_template=*/false,
13992 /*is_namespace=*/false,
13993 /*check_dependency=*/true,
13997 /* If the lookup was ambiguous, an error will already have been
13999 if (ambiguous_decls)
14000 return error_mark_node;
14002 /* If we are parsing friend declaration, DECL may be a
14003 TEMPLATE_DECL tree node here. However, we need to check
14004 whether this TEMPLATE_DECL results in valid code. Consider
14005 the following example:
14008 template <class T> class C {};
14011 template <class T> friend class N::C; // #1, valid code
14013 template <class T> class Y {
14014 friend class N::C; // #2, invalid code
14017 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14018 name lookup of `N::C'. We see that friend declaration must
14019 be template for the code to be valid. Note that
14020 processing_template_decl does not work here since it is
14021 always 1 for the above two cases. */
14023 decl = (cp_parser_maybe_treat_template_as_class
14024 (decl, /*tag_name_p=*/is_friend
14025 && parser->num_template_parameter_lists));
14027 if (TREE_CODE (decl) != TYPE_DECL)
14029 cp_parser_diagnose_invalid_type_name (parser,
14033 return error_mark_node;
14036 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14038 bool allow_template = (parser->num_template_parameter_lists
14039 || DECL_SELF_REFERENCE_P (decl));
14040 type = check_elaborated_type_specifier (tag_type, decl,
14043 if (type == error_mark_node)
14044 return error_mark_node;
14047 /* Forward declarations of nested types, such as
14052 are invalid unless all components preceding the final '::'
14053 are complete. If all enclosing types are complete, these
14054 declarations become merely pointless.
14056 Invalid forward declarations of nested types are errors
14057 caught elsewhere in parsing. Those that are pointless arrive
14060 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14061 && !is_friend && !processing_explicit_instantiation)
14062 warning (0, "declaration %qD does not declare anything", decl);
14064 type = TREE_TYPE (decl);
14068 /* An elaborated-type-specifier sometimes introduces a new type and
14069 sometimes names an existing type. Normally, the rule is that it
14070 introduces a new type only if there is not an existing type of
14071 the same name already in scope. For example, given:
14074 void f() { struct S s; }
14076 the `struct S' in the body of `f' is the same `struct S' as in
14077 the global scope; the existing definition is used. However, if
14078 there were no global declaration, this would introduce a new
14079 local class named `S'.
14081 An exception to this rule applies to the following code:
14083 namespace N { struct S; }
14085 Here, the elaborated-type-specifier names a new type
14086 unconditionally; even if there is already an `S' in the
14087 containing scope this declaration names a new type.
14088 This exception only applies if the elaborated-type-specifier
14089 forms the complete declaration:
14093 A declaration consisting solely of `class-key identifier ;' is
14094 either a redeclaration of the name in the current scope or a
14095 forward declaration of the identifier as a class name. It
14096 introduces the name into the current scope.
14098 We are in this situation precisely when the next token is a `;'.
14100 An exception to the exception is that a `friend' declaration does
14101 *not* name a new type; i.e., given:
14103 struct S { friend struct T; };
14105 `T' is not a new type in the scope of `S'.
14107 Also, `new struct S' or `sizeof (struct S)' never results in the
14108 definition of a new type; a new type can only be declared in a
14109 declaration context. */
14115 /* Friends have special name lookup rules. */
14116 ts = ts_within_enclosing_non_class;
14117 else if (is_declaration
14118 && cp_lexer_next_token_is (parser->lexer,
14120 /* This is a `class-key identifier ;' */
14126 (parser->num_template_parameter_lists
14127 && (cp_parser_next_token_starts_class_definition_p (parser)
14128 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14129 /* An unqualified name was used to reference this type, so
14130 there were no qualifying templates. */
14131 if (!cp_parser_check_template_parameters (parser,
14132 /*num_templates=*/0,
14134 /*declarator=*/NULL))
14135 return error_mark_node;
14136 type = xref_tag (tag_type, identifier, ts, template_p);
14140 if (type == error_mark_node)
14141 return error_mark_node;
14143 /* Allow attributes on forward declarations of classes. */
14146 if (TREE_CODE (type) == TYPENAME_TYPE)
14147 warning (OPT_Wattributes,
14148 "attributes ignored on uninstantiated type");
14149 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14150 && ! processing_explicit_instantiation)
14151 warning (OPT_Wattributes,
14152 "attributes ignored on template instantiation");
14153 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14154 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14156 warning (OPT_Wattributes,
14157 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14160 if (tag_type != enum_type)
14162 /* Indicate whether this class was declared as a `class' or as a
14164 if (TREE_CODE (type) == RECORD_TYPE)
14165 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14166 cp_parser_check_class_key (tag_type, type);
14169 /* A "<" cannot follow an elaborated type specifier. If that
14170 happens, the user was probably trying to form a template-id. */
14171 cp_parser_check_for_invalid_template_id (parser, type, token->location);
14176 /* Parse an enum-specifier.
14179 enum-head { enumerator-list [opt] }
14180 enum-head { enumerator-list , } [C++0x]
14183 enum-key identifier [opt] enum-base [opt]
14184 enum-key nested-name-specifier identifier enum-base [opt]
14189 enum struct [C++0x]
14192 : type-specifier-seq
14194 opaque-enum-specifier:
14195 enum-key identifier enum-base [opt] ;
14198 enum-key attributes[opt] identifier [opt] enum-base [opt]
14199 { enumerator-list [opt] }attributes[opt]
14200 enum-key attributes[opt] identifier [opt] enum-base [opt]
14201 { enumerator-list, }attributes[opt] [C++0x]
14203 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14204 if the token stream isn't an enum-specifier after all. */
14207 cp_parser_enum_specifier (cp_parser* parser)
14210 tree type = NULL_TREE;
14212 tree nested_name_specifier = NULL_TREE;
14214 bool scoped_enum_p = false;
14215 bool has_underlying_type = false;
14216 bool nested_being_defined = false;
14217 bool new_value_list = false;
14218 bool is_new_type = false;
14219 bool is_anonymous = false;
14220 tree underlying_type = NULL_TREE;
14221 cp_token *type_start_token = NULL;
14222 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14224 parser->colon_corrects_to_scope_p = false;
14226 /* Parse tentatively so that we can back up if we don't find a
14228 cp_parser_parse_tentatively (parser);
14230 /* Caller guarantees that the current token is 'enum', an identifier
14231 possibly follows, and the token after that is an opening brace.
14232 If we don't have an identifier, fabricate an anonymous name for
14233 the enumeration being defined. */
14234 cp_lexer_consume_token (parser->lexer);
14236 /* Parse the "class" or "struct", which indicates a scoped
14237 enumeration type in C++0x. */
14238 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14239 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14241 if (cxx_dialect < cxx0x)
14242 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14244 /* Consume the `struct' or `class' token. */
14245 cp_lexer_consume_token (parser->lexer);
14247 scoped_enum_p = true;
14250 attributes = cp_parser_attributes_opt (parser);
14252 /* Clear the qualification. */
14253 parser->scope = NULL_TREE;
14254 parser->qualifying_scope = NULL_TREE;
14255 parser->object_scope = NULL_TREE;
14257 /* Figure out in what scope the declaration is being placed. */
14258 prev_scope = current_scope ();
14260 type_start_token = cp_lexer_peek_token (parser->lexer);
14262 push_deferring_access_checks (dk_no_check);
14263 nested_name_specifier
14264 = cp_parser_nested_name_specifier_opt (parser,
14265 /*typename_keyword_p=*/true,
14266 /*check_dependency_p=*/false,
14268 /*is_declaration=*/false);
14270 if (nested_name_specifier)
14274 identifier = cp_parser_identifier (parser);
14275 name = cp_parser_lookup_name (parser, identifier,
14277 /*is_template=*/false,
14278 /*is_namespace=*/false,
14279 /*check_dependency=*/true,
14280 /*ambiguous_decls=*/NULL,
14284 type = TREE_TYPE (name);
14285 if (TREE_CODE (type) == TYPENAME_TYPE)
14287 /* Are template enums allowed in ISO? */
14288 if (template_parm_scope_p ())
14289 pedwarn (type_start_token->location, OPT_pedantic,
14290 "%qD is an enumeration template", name);
14291 /* ignore a typename reference, for it will be solved by name
14297 error_at (type_start_token->location,
14298 "%qD is not an enumerator-name", identifier);
14302 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14303 identifier = cp_parser_identifier (parser);
14306 identifier = make_anon_name ();
14307 is_anonymous = true;
14310 pop_deferring_access_checks ();
14312 /* Check for the `:' that denotes a specified underlying type in C++0x.
14313 Note that a ':' could also indicate a bitfield width, however. */
14314 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14316 cp_decl_specifier_seq type_specifiers;
14318 /* Consume the `:'. */
14319 cp_lexer_consume_token (parser->lexer);
14321 /* Parse the type-specifier-seq. */
14322 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14323 /*is_trailing_return=*/false,
14326 /* At this point this is surely not elaborated type specifier. */
14327 if (!cp_parser_parse_definitely (parser))
14330 if (cxx_dialect < cxx0x)
14331 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14333 has_underlying_type = true;
14335 /* If that didn't work, stop. */
14336 if (type_specifiers.type != error_mark_node)
14338 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14339 /*initialized=*/0, NULL);
14340 if (underlying_type == error_mark_node)
14341 underlying_type = NULL_TREE;
14345 /* Look for the `{' but don't consume it yet. */
14346 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14348 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14350 cp_parser_error (parser, "expected %<{%>");
14351 if (has_underlying_type)
14357 /* An opaque-enum-specifier must have a ';' here. */
14358 if ((scoped_enum_p || underlying_type)
14359 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14361 cp_parser_error (parser, "expected %<;%> or %<{%>");
14362 if (has_underlying_type)
14370 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14373 if (nested_name_specifier)
14375 if (CLASS_TYPE_P (nested_name_specifier))
14377 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14378 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14379 push_scope (nested_name_specifier);
14381 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14383 push_nested_namespace (nested_name_specifier);
14387 /* Issue an error message if type-definitions are forbidden here. */
14388 if (!cp_parser_check_type_definition (parser))
14389 type = error_mark_node;
14391 /* Create the new type. We do this before consuming the opening
14392 brace so the enum will be recorded as being on the line of its
14393 tag (or the 'enum' keyword, if there is no tag). */
14394 type = start_enum (identifier, type, underlying_type,
14395 scoped_enum_p, &is_new_type);
14397 /* If the next token is not '{' it is an opaque-enum-specifier or an
14398 elaborated-type-specifier. */
14399 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14401 timevar_push (TV_PARSE_ENUM);
14402 if (nested_name_specifier)
14404 /* The following catches invalid code such as:
14405 enum class S<int>::E { A, B, C }; */
14406 if (!processing_specialization
14407 && CLASS_TYPE_P (nested_name_specifier)
14408 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14409 error_at (type_start_token->location, "cannot add an enumerator "
14410 "list to a template instantiation");
14412 /* If that scope does not contain the scope in which the
14413 class was originally declared, the program is invalid. */
14414 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14416 if (at_namespace_scope_p ())
14417 error_at (type_start_token->location,
14418 "declaration of %qD in namespace %qD which does not "
14420 type, prev_scope, nested_name_specifier);
14422 error_at (type_start_token->location,
14423 "declaration of %qD in %qD which does not enclose %qD",
14424 type, prev_scope, nested_name_specifier);
14425 type = error_mark_node;
14430 begin_scope (sk_scoped_enum, type);
14432 /* Consume the opening brace. */
14433 cp_lexer_consume_token (parser->lexer);
14435 if (type == error_mark_node)
14436 ; /* Nothing to add */
14437 else if (OPAQUE_ENUM_P (type)
14438 || (cxx_dialect > cxx98 && processing_specialization))
14440 new_value_list = true;
14441 SET_OPAQUE_ENUM_P (type, false);
14442 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14446 error_at (type_start_token->location, "multiple definition of %q#T", type);
14447 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14448 "previous definition here");
14449 type = error_mark_node;
14452 if (type == error_mark_node)
14453 cp_parser_skip_to_end_of_block_or_statement (parser);
14454 /* If the next token is not '}', then there are some enumerators. */
14455 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14456 cp_parser_enumerator_list (parser, type);
14458 /* Consume the final '}'. */
14459 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14463 timevar_pop (TV_PARSE_ENUM);
14467 /* If a ';' follows, then it is an opaque-enum-specifier
14468 and additional restrictions apply. */
14469 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14472 error_at (type_start_token->location,
14473 "opaque-enum-specifier without name");
14474 else if (nested_name_specifier)
14475 error_at (type_start_token->location,
14476 "opaque-enum-specifier must use a simple identifier");
14480 /* Look for trailing attributes to apply to this enumeration, and
14481 apply them if appropriate. */
14482 if (cp_parser_allow_gnu_extensions_p (parser))
14484 tree trailing_attr = cp_parser_attributes_opt (parser);
14485 trailing_attr = chainon (trailing_attr, attributes);
14486 cplus_decl_attributes (&type,
14488 (int) ATTR_FLAG_TYPE_IN_PLACE);
14491 /* Finish up the enumeration. */
14492 if (type != error_mark_node)
14494 if (new_value_list)
14495 finish_enum_value_list (type);
14497 finish_enum (type);
14500 if (nested_name_specifier)
14502 if (CLASS_TYPE_P (nested_name_specifier))
14504 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14505 pop_scope (nested_name_specifier);
14507 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14509 pop_nested_namespace (nested_name_specifier);
14513 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14517 /* Parse an enumerator-list. The enumerators all have the indicated
14521 enumerator-definition
14522 enumerator-list , enumerator-definition */
14525 cp_parser_enumerator_list (cp_parser* parser, tree type)
14529 /* Parse an enumerator-definition. */
14530 cp_parser_enumerator_definition (parser, type);
14532 /* If the next token is not a ',', we've reached the end of
14534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14536 /* Otherwise, consume the `,' and keep going. */
14537 cp_lexer_consume_token (parser->lexer);
14538 /* If the next token is a `}', there is a trailing comma. */
14539 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14541 if (cxx_dialect < cxx0x && !in_system_header)
14542 pedwarn (input_location, OPT_pedantic,
14543 "comma at end of enumerator list");
14549 /* Parse an enumerator-definition. The enumerator has the indicated
14552 enumerator-definition:
14554 enumerator = constant-expression
14560 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14566 /* Save the input location because we are interested in the location
14567 of the identifier and not the location of the explicit value. */
14568 loc = cp_lexer_peek_token (parser->lexer)->location;
14570 /* Look for the identifier. */
14571 identifier = cp_parser_identifier (parser);
14572 if (identifier == error_mark_node)
14575 /* If the next token is an '=', then there is an explicit value. */
14576 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14578 /* Consume the `=' token. */
14579 cp_lexer_consume_token (parser->lexer);
14580 /* Parse the value. */
14581 value = cp_parser_constant_expression (parser,
14582 /*allow_non_constant_p=*/false,
14588 /* If we are processing a template, make sure the initializer of the
14589 enumerator doesn't contain any bare template parameter pack. */
14590 if (check_for_bare_parameter_packs (value))
14591 value = error_mark_node;
14593 /* integral_constant_value will pull out this expression, so make sure
14594 it's folded as appropriate. */
14595 value = fold_non_dependent_expr (value);
14597 /* Create the enumerator. */
14598 build_enumerator (identifier, value, type, loc);
14601 /* Parse a namespace-name.
14604 original-namespace-name
14607 Returns the NAMESPACE_DECL for the namespace. */
14610 cp_parser_namespace_name (cp_parser* parser)
14613 tree namespace_decl;
14615 cp_token *token = cp_lexer_peek_token (parser->lexer);
14617 /* Get the name of the namespace. */
14618 identifier = cp_parser_identifier (parser);
14619 if (identifier == error_mark_node)
14620 return error_mark_node;
14622 /* Look up the identifier in the currently active scope. Look only
14623 for namespaces, due to:
14625 [basic.lookup.udir]
14627 When looking up a namespace-name in a using-directive or alias
14628 definition, only namespace names are considered.
14632 [basic.lookup.qual]
14634 During the lookup of a name preceding the :: scope resolution
14635 operator, object, function, and enumerator names are ignored.
14637 (Note that cp_parser_qualifying_entity only calls this
14638 function if the token after the name is the scope resolution
14640 namespace_decl = cp_parser_lookup_name (parser, identifier,
14642 /*is_template=*/false,
14643 /*is_namespace=*/true,
14644 /*check_dependency=*/true,
14645 /*ambiguous_decls=*/NULL,
14647 /* If it's not a namespace, issue an error. */
14648 if (namespace_decl == error_mark_node
14649 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14651 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14652 error_at (token->location, "%qD is not a namespace-name", identifier);
14653 cp_parser_error (parser, "expected namespace-name");
14654 namespace_decl = error_mark_node;
14657 return namespace_decl;
14660 /* Parse a namespace-definition.
14662 namespace-definition:
14663 named-namespace-definition
14664 unnamed-namespace-definition
14666 named-namespace-definition:
14667 original-namespace-definition
14668 extension-namespace-definition
14670 original-namespace-definition:
14671 namespace identifier { namespace-body }
14673 extension-namespace-definition:
14674 namespace original-namespace-name { namespace-body }
14676 unnamed-namespace-definition:
14677 namespace { namespace-body } */
14680 cp_parser_namespace_definition (cp_parser* parser)
14682 tree identifier, attribs;
14683 bool has_visibility;
14686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14688 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14690 cp_lexer_consume_token (parser->lexer);
14695 /* Look for the `namespace' keyword. */
14696 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14698 /* Get the name of the namespace. We do not attempt to distinguish
14699 between an original-namespace-definition and an
14700 extension-namespace-definition at this point. The semantic
14701 analysis routines are responsible for that. */
14702 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14703 identifier = cp_parser_identifier (parser);
14705 identifier = NULL_TREE;
14707 /* Parse any specified attributes. */
14708 attribs = cp_parser_attributes_opt (parser);
14710 /* Look for the `{' to start the namespace. */
14711 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14712 /* Start the namespace. */
14713 push_namespace (identifier);
14715 /* "inline namespace" is equivalent to a stub namespace definition
14716 followed by a strong using directive. */
14719 tree name_space = current_namespace;
14720 /* Set up namespace association. */
14721 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14722 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14723 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14724 /* Import the contents of the inline namespace. */
14726 do_using_directive (name_space);
14727 push_namespace (identifier);
14730 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14732 /* Parse the body of the namespace. */
14733 cp_parser_namespace_body (parser);
14735 if (has_visibility)
14736 pop_visibility (1);
14738 /* Finish the namespace. */
14740 /* Look for the final `}'. */
14741 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14744 /* Parse a namespace-body.
14747 declaration-seq [opt] */
14750 cp_parser_namespace_body (cp_parser* parser)
14752 cp_parser_declaration_seq_opt (parser);
14755 /* Parse a namespace-alias-definition.
14757 namespace-alias-definition:
14758 namespace identifier = qualified-namespace-specifier ; */
14761 cp_parser_namespace_alias_definition (cp_parser* parser)
14764 tree namespace_specifier;
14766 cp_token *token = cp_lexer_peek_token (parser->lexer);
14768 /* Look for the `namespace' keyword. */
14769 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14770 /* Look for the identifier. */
14771 identifier = cp_parser_identifier (parser);
14772 if (identifier == error_mark_node)
14774 /* Look for the `=' token. */
14775 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14776 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14778 error_at (token->location, "%<namespace%> definition is not allowed here");
14779 /* Skip the definition. */
14780 cp_lexer_consume_token (parser->lexer);
14781 if (cp_parser_skip_to_closing_brace (parser))
14782 cp_lexer_consume_token (parser->lexer);
14785 cp_parser_require (parser, CPP_EQ, RT_EQ);
14786 /* Look for the qualified-namespace-specifier. */
14787 namespace_specifier
14788 = cp_parser_qualified_namespace_specifier (parser);
14789 /* Look for the `;' token. */
14790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14792 /* Register the alias in the symbol table. */
14793 do_namespace_alias (identifier, namespace_specifier);
14796 /* Parse a qualified-namespace-specifier.
14798 qualified-namespace-specifier:
14799 :: [opt] nested-name-specifier [opt] namespace-name
14801 Returns a NAMESPACE_DECL corresponding to the specified
14805 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14807 /* Look for the optional `::'. */
14808 cp_parser_global_scope_opt (parser,
14809 /*current_scope_valid_p=*/false);
14811 /* Look for the optional nested-name-specifier. */
14812 cp_parser_nested_name_specifier_opt (parser,
14813 /*typename_keyword_p=*/false,
14814 /*check_dependency_p=*/true,
14816 /*is_declaration=*/true);
14818 return cp_parser_namespace_name (parser);
14821 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14822 access declaration.
14825 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14826 using :: unqualified-id ;
14828 access-declaration:
14834 cp_parser_using_declaration (cp_parser* parser,
14835 bool access_declaration_p)
14838 bool typename_p = false;
14839 bool global_scope_p;
14844 if (access_declaration_p)
14845 cp_parser_parse_tentatively (parser);
14848 /* Look for the `using' keyword. */
14849 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14851 /* Peek at the next token. */
14852 token = cp_lexer_peek_token (parser->lexer);
14853 /* See if it's `typename'. */
14854 if (token->keyword == RID_TYPENAME)
14856 /* Remember that we've seen it. */
14858 /* Consume the `typename' token. */
14859 cp_lexer_consume_token (parser->lexer);
14863 /* Look for the optional global scope qualification. */
14865 = (cp_parser_global_scope_opt (parser,
14866 /*current_scope_valid_p=*/false)
14869 /* If we saw `typename', or didn't see `::', then there must be a
14870 nested-name-specifier present. */
14871 if (typename_p || !global_scope_p)
14872 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14873 /*check_dependency_p=*/true,
14875 /*is_declaration=*/true);
14876 /* Otherwise, we could be in either of the two productions. In that
14877 case, treat the nested-name-specifier as optional. */
14879 qscope = cp_parser_nested_name_specifier_opt (parser,
14880 /*typename_keyword_p=*/false,
14881 /*check_dependency_p=*/true,
14883 /*is_declaration=*/true);
14885 qscope = global_namespace;
14887 if (access_declaration_p && cp_parser_error_occurred (parser))
14888 /* Something has already gone wrong; there's no need to parse
14889 further. Since an error has occurred, the return value of
14890 cp_parser_parse_definitely will be false, as required. */
14891 return cp_parser_parse_definitely (parser);
14893 token = cp_lexer_peek_token (parser->lexer);
14894 /* Parse the unqualified-id. */
14895 identifier = cp_parser_unqualified_id (parser,
14896 /*template_keyword_p=*/false,
14897 /*check_dependency_p=*/true,
14898 /*declarator_p=*/true,
14899 /*optional_p=*/false);
14901 if (access_declaration_p)
14903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14904 cp_parser_simulate_error (parser);
14905 if (!cp_parser_parse_definitely (parser))
14909 /* The function we call to handle a using-declaration is different
14910 depending on what scope we are in. */
14911 if (qscope == error_mark_node || identifier == error_mark_node)
14913 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14914 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14915 /* [namespace.udecl]
14917 A using declaration shall not name a template-id. */
14918 error_at (token->location,
14919 "a template-id may not appear in a using-declaration");
14922 if (at_class_scope_p ())
14924 /* Create the USING_DECL. */
14925 decl = do_class_using_decl (parser->scope, identifier);
14927 if (check_for_bare_parameter_packs (decl))
14930 /* Add it to the list of members in this class. */
14931 finish_member_declaration (decl);
14935 decl = cp_parser_lookup_name_simple (parser,
14938 if (decl == error_mark_node)
14939 cp_parser_name_lookup_error (parser, identifier,
14942 else if (check_for_bare_parameter_packs (decl))
14944 else if (!at_namespace_scope_p ())
14945 do_local_using_decl (decl, qscope, identifier);
14947 do_toplevel_using_decl (decl, qscope, identifier);
14951 /* Look for the final `;'. */
14952 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14957 /* Parse an alias-declaration.
14960 using identifier attribute-specifier-seq [opt] = type-id */
14963 cp_parser_alias_declaration (cp_parser* parser)
14965 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
14966 location_t id_location;
14967 cp_declarator *declarator;
14968 cp_decl_specifier_seq decl_specs;
14971 /* Look for the `using' keyword. */
14972 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14973 id_location = cp_lexer_peek_token (parser->lexer)->location;
14974 id = cp_parser_identifier (parser);
14975 attributes = cp_parser_attributes_opt (parser);
14976 cp_parser_require (parser, CPP_EQ, RT_EQ);
14978 type = cp_parser_type_id (parser);
14979 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14981 if (cp_parser_error_occurred (parser))
14982 return error_mark_node;
14984 /* A typedef-name can also be introduced by an alias-declaration. The
14985 identifier following the using keyword becomes a typedef-name. It has
14986 the same semantics as if it were introduced by the typedef
14987 specifier. In particular, it does not define a new type and it shall
14988 not appear in the type-id. */
14990 clear_decl_specs (&decl_specs);
14991 decl_specs.type = type;
14992 decl_specs.attributes = attributes;
14993 ++decl_specs.specs[(int) ds_typedef];
14994 ++decl_specs.specs[(int) ds_alias];
14996 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
14997 declarator->id_loc = id_location;
14999 member_p = at_class_scope_p ();
15001 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15002 NULL_TREE, attributes);
15004 decl = start_decl (declarator, &decl_specs, 0,
15005 attributes, NULL_TREE, &pushed_scope);
15006 if (decl == error_mark_node)
15009 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15012 pop_scope (pushed_scope);
15014 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15015 added into the symbol table; otherwise, return the TYPE_DECL. */
15016 if (DECL_LANG_SPECIFIC (decl)
15017 && DECL_TEMPLATE_INFO (decl)
15018 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15020 decl = DECL_TI_TEMPLATE (decl);
15022 check_member_template (decl);
15028 /* Parse a using-directive.
15031 using namespace :: [opt] nested-name-specifier [opt]
15032 namespace-name ; */
15035 cp_parser_using_directive (cp_parser* parser)
15037 tree namespace_decl;
15040 /* Look for the `using' keyword. */
15041 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15042 /* And the `namespace' keyword. */
15043 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15044 /* Look for the optional `::' operator. */
15045 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15046 /* And the optional nested-name-specifier. */
15047 cp_parser_nested_name_specifier_opt (parser,
15048 /*typename_keyword_p=*/false,
15049 /*check_dependency_p=*/true,
15051 /*is_declaration=*/true);
15052 /* Get the namespace being used. */
15053 namespace_decl = cp_parser_namespace_name (parser);
15054 /* And any specified attributes. */
15055 attribs = cp_parser_attributes_opt (parser);
15056 /* Update the symbol table. */
15057 parse_using_directive (namespace_decl, attribs);
15058 /* Look for the final `;'. */
15059 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15062 /* Parse an asm-definition.
15065 asm ( string-literal ) ;
15070 asm volatile [opt] ( string-literal ) ;
15071 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15072 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15073 : asm-operand-list [opt] ) ;
15074 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15075 : asm-operand-list [opt]
15076 : asm-clobber-list [opt] ) ;
15077 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15078 : asm-clobber-list [opt]
15079 : asm-goto-list ) ; */
15082 cp_parser_asm_definition (cp_parser* parser)
15085 tree outputs = NULL_TREE;
15086 tree inputs = NULL_TREE;
15087 tree clobbers = NULL_TREE;
15088 tree labels = NULL_TREE;
15090 bool volatile_p = false;
15091 bool extended_p = false;
15092 bool invalid_inputs_p = false;
15093 bool invalid_outputs_p = false;
15094 bool goto_p = false;
15095 required_token missing = RT_NONE;
15097 /* Look for the `asm' keyword. */
15098 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15099 /* See if the next token is `volatile'. */
15100 if (cp_parser_allow_gnu_extensions_p (parser)
15101 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15103 /* Remember that we saw the `volatile' keyword. */
15105 /* Consume the token. */
15106 cp_lexer_consume_token (parser->lexer);
15108 if (cp_parser_allow_gnu_extensions_p (parser)
15109 && parser->in_function_body
15110 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15112 /* Remember that we saw the `goto' keyword. */
15114 /* Consume the token. */
15115 cp_lexer_consume_token (parser->lexer);
15117 /* Look for the opening `('. */
15118 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15120 /* Look for the string. */
15121 string = cp_parser_string_literal (parser, false, false);
15122 if (string == error_mark_node)
15124 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15125 /*consume_paren=*/true);
15129 /* If we're allowing GNU extensions, check for the extended assembly
15130 syntax. Unfortunately, the `:' tokens need not be separated by
15131 a space in C, and so, for compatibility, we tolerate that here
15132 too. Doing that means that we have to treat the `::' operator as
15134 if (cp_parser_allow_gnu_extensions_p (parser)
15135 && parser->in_function_body
15136 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15137 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15139 bool inputs_p = false;
15140 bool clobbers_p = false;
15141 bool labels_p = false;
15143 /* The extended syntax was used. */
15146 /* Look for outputs. */
15147 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15149 /* Consume the `:'. */
15150 cp_lexer_consume_token (parser->lexer);
15151 /* Parse the output-operands. */
15152 if (cp_lexer_next_token_is_not (parser->lexer,
15154 && cp_lexer_next_token_is_not (parser->lexer,
15156 && cp_lexer_next_token_is_not (parser->lexer,
15159 outputs = cp_parser_asm_operand_list (parser);
15161 if (outputs == error_mark_node)
15162 invalid_outputs_p = true;
15164 /* If the next token is `::', there are no outputs, and the
15165 next token is the beginning of the inputs. */
15166 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15167 /* The inputs are coming next. */
15170 /* Look for inputs. */
15172 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15174 /* Consume the `:' or `::'. */
15175 cp_lexer_consume_token (parser->lexer);
15176 /* Parse the output-operands. */
15177 if (cp_lexer_next_token_is_not (parser->lexer,
15179 && cp_lexer_next_token_is_not (parser->lexer,
15181 && cp_lexer_next_token_is_not (parser->lexer,
15183 inputs = cp_parser_asm_operand_list (parser);
15185 if (inputs == error_mark_node)
15186 invalid_inputs_p = true;
15188 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15189 /* The clobbers are coming next. */
15192 /* Look for clobbers. */
15194 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15197 /* Consume the `:' or `::'. */
15198 cp_lexer_consume_token (parser->lexer);
15199 /* Parse the clobbers. */
15200 if (cp_lexer_next_token_is_not (parser->lexer,
15202 && cp_lexer_next_token_is_not (parser->lexer,
15204 clobbers = cp_parser_asm_clobber_list (parser);
15207 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15208 /* The labels are coming next. */
15211 /* Look for labels. */
15213 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15216 /* Consume the `:' or `::'. */
15217 cp_lexer_consume_token (parser->lexer);
15218 /* Parse the labels. */
15219 labels = cp_parser_asm_label_list (parser);
15222 if (goto_p && !labels_p)
15223 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15226 missing = RT_COLON_SCOPE;
15228 /* Look for the closing `)'. */
15229 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15230 missing ? missing : RT_CLOSE_PAREN))
15231 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15232 /*consume_paren=*/true);
15233 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15235 if (!invalid_inputs_p && !invalid_outputs_p)
15237 /* Create the ASM_EXPR. */
15238 if (parser->in_function_body)
15240 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15241 inputs, clobbers, labels);
15242 /* If the extended syntax was not used, mark the ASM_EXPR. */
15245 tree temp = asm_stmt;
15246 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15247 temp = TREE_OPERAND (temp, 0);
15249 ASM_INPUT_P (temp) = 1;
15253 cgraph_add_asm_node (string);
15257 /* Declarators [gram.dcl.decl] */
15259 /* Parse an init-declarator.
15262 declarator initializer [opt]
15267 declarator asm-specification [opt] attributes [opt] initializer [opt]
15269 function-definition:
15270 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15272 decl-specifier-seq [opt] declarator function-try-block
15276 function-definition:
15277 __extension__ function-definition
15281 function-definition:
15282 decl-specifier-seq [opt] declarator function-transaction-block
15284 The DECL_SPECIFIERS apply to this declarator. Returns a
15285 representation of the entity declared. If MEMBER_P is TRUE, then
15286 this declarator appears in a class scope. The new DECL created by
15287 this declarator is returned.
15289 The CHECKS are access checks that should be performed once we know
15290 what entity is being declared (and, therefore, what classes have
15293 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15294 for a function-definition here as well. If the declarator is a
15295 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15296 be TRUE upon return. By that point, the function-definition will
15297 have been completely parsed.
15299 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15302 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15303 parsed declaration if it is an uninitialized single declarator not followed
15304 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15305 if present, will not be consumed. If returned, this declarator will be
15306 created with SD_INITIALIZED but will not call cp_finish_decl. */
15309 cp_parser_init_declarator (cp_parser* parser,
15310 cp_decl_specifier_seq *decl_specifiers,
15311 VEC (deferred_access_check,gc)* checks,
15312 bool function_definition_allowed_p,
15314 int declares_class_or_enum,
15315 bool* function_definition_p,
15316 tree* maybe_range_for_decl)
15318 cp_token *token = NULL, *asm_spec_start_token = NULL,
15319 *attributes_start_token = NULL;
15320 cp_declarator *declarator;
15321 tree prefix_attributes;
15323 tree asm_specification;
15325 tree decl = NULL_TREE;
15327 int is_initialized;
15328 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15329 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15331 enum cpp_ttype initialization_kind;
15332 bool is_direct_init = false;
15333 bool is_non_constant_init;
15334 int ctor_dtor_or_conv_p;
15336 tree pushed_scope = NULL_TREE;
15337 bool range_for_decl_p = false;
15339 /* Gather the attributes that were provided with the
15340 decl-specifiers. */
15341 prefix_attributes = decl_specifiers->attributes;
15343 /* Assume that this is not the declarator for a function
15345 if (function_definition_p)
15346 *function_definition_p = false;
15348 /* Defer access checks while parsing the declarator; we cannot know
15349 what names are accessible until we know what is being
15351 resume_deferring_access_checks ();
15353 /* Parse the declarator. */
15354 token = cp_lexer_peek_token (parser->lexer);
15356 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15357 &ctor_dtor_or_conv_p,
15358 /*parenthesized_p=*/NULL,
15360 /* Gather up the deferred checks. */
15361 stop_deferring_access_checks ();
15363 /* If the DECLARATOR was erroneous, there's no need to go
15365 if (declarator == cp_error_declarator)
15366 return error_mark_node;
15368 /* Check that the number of template-parameter-lists is OK. */
15369 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15371 return error_mark_node;
15373 if (declares_class_or_enum & 2)
15374 cp_parser_check_for_definition_in_return_type (declarator,
15375 decl_specifiers->type,
15376 decl_specifiers->type_location);
15378 /* Figure out what scope the entity declared by the DECLARATOR is
15379 located in. `grokdeclarator' sometimes changes the scope, so
15380 we compute it now. */
15381 scope = get_scope_of_declarator (declarator);
15383 /* Perform any lookups in the declared type which were thought to be
15384 dependent, but are not in the scope of the declarator. */
15385 decl_specifiers->type
15386 = maybe_update_decl_type (decl_specifiers->type, scope);
15388 /* If we're allowing GNU extensions, look for an asm-specification
15390 if (cp_parser_allow_gnu_extensions_p (parser))
15392 /* Look for an asm-specification. */
15393 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15394 asm_specification = cp_parser_asm_specification_opt (parser);
15395 /* And attributes. */
15396 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15397 attributes = cp_parser_attributes_opt (parser);
15401 asm_specification = NULL_TREE;
15402 attributes = NULL_TREE;
15405 /* Peek at the next token. */
15406 token = cp_lexer_peek_token (parser->lexer);
15407 /* Check to see if the token indicates the start of a
15408 function-definition. */
15409 if (function_declarator_p (declarator)
15410 && cp_parser_token_starts_function_definition_p (token))
15412 if (!function_definition_allowed_p)
15414 /* If a function-definition should not appear here, issue an
15416 cp_parser_error (parser,
15417 "a function-definition is not allowed here");
15418 return error_mark_node;
15422 location_t func_brace_location
15423 = cp_lexer_peek_token (parser->lexer)->location;
15425 /* Neither attributes nor an asm-specification are allowed
15426 on a function-definition. */
15427 if (asm_specification)
15428 error_at (asm_spec_start_token->location,
15429 "an asm-specification is not allowed "
15430 "on a function-definition");
15432 error_at (attributes_start_token->location,
15433 "attributes are not allowed on a function-definition");
15434 /* This is a function-definition. */
15435 *function_definition_p = true;
15437 /* Parse the function definition. */
15439 decl = cp_parser_save_member_function_body (parser,
15442 prefix_attributes);
15445 = (cp_parser_function_definition_from_specifiers_and_declarator
15446 (parser, decl_specifiers, prefix_attributes, declarator));
15448 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15450 /* This is where the prologue starts... */
15451 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15452 = func_brace_location;
15461 Only in function declarations for constructors, destructors, and
15462 type conversions can the decl-specifier-seq be omitted.
15464 We explicitly postpone this check past the point where we handle
15465 function-definitions because we tolerate function-definitions
15466 that are missing their return types in some modes. */
15467 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15469 cp_parser_error (parser,
15470 "expected constructor, destructor, or type conversion");
15471 return error_mark_node;
15474 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15475 if (token->type == CPP_EQ
15476 || token->type == CPP_OPEN_PAREN
15477 || token->type == CPP_OPEN_BRACE)
15479 is_initialized = SD_INITIALIZED;
15480 initialization_kind = token->type;
15481 if (maybe_range_for_decl)
15482 *maybe_range_for_decl = error_mark_node;
15484 if (token->type == CPP_EQ
15485 && function_declarator_p (declarator))
15487 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15488 if (t2->keyword == RID_DEFAULT)
15489 is_initialized = SD_DEFAULTED;
15490 else if (t2->keyword == RID_DELETE)
15491 is_initialized = SD_DELETED;
15496 /* If the init-declarator isn't initialized and isn't followed by a
15497 `,' or `;', it's not a valid init-declarator. */
15498 if (token->type != CPP_COMMA
15499 && token->type != CPP_SEMICOLON)
15501 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15502 range_for_decl_p = true;
15505 cp_parser_error (parser, "expected initializer");
15506 return error_mark_node;
15509 is_initialized = SD_UNINITIALIZED;
15510 initialization_kind = CPP_EOF;
15513 /* Because start_decl has side-effects, we should only call it if we
15514 know we're going ahead. By this point, we know that we cannot
15515 possibly be looking at any other construct. */
15516 cp_parser_commit_to_tentative_parse (parser);
15518 /* If the decl specifiers were bad, issue an error now that we're
15519 sure this was intended to be a declarator. Then continue
15520 declaring the variable(s), as int, to try to cut down on further
15522 if (decl_specifiers->any_specifiers_p
15523 && decl_specifiers->type == error_mark_node)
15525 cp_parser_error (parser, "invalid type in declaration");
15526 decl_specifiers->type = integer_type_node;
15529 /* Check to see whether or not this declaration is a friend. */
15530 friend_p = cp_parser_friend_p (decl_specifiers);
15532 /* Enter the newly declared entry in the symbol table. If we're
15533 processing a declaration in a class-specifier, we wait until
15534 after processing the initializer. */
15537 if (parser->in_unbraced_linkage_specification_p)
15538 decl_specifiers->storage_class = sc_extern;
15539 decl = start_decl (declarator, decl_specifiers,
15540 range_for_decl_p? SD_INITIALIZED : is_initialized,
15541 attributes, prefix_attributes,
15543 /* Adjust location of decl if declarator->id_loc is more appropriate:
15544 set, and decl wasn't merged with another decl, in which case its
15545 location would be different from input_location, and more accurate. */
15547 && declarator->id_loc != UNKNOWN_LOCATION
15548 && DECL_SOURCE_LOCATION (decl) == input_location)
15549 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15552 /* Enter the SCOPE. That way unqualified names appearing in the
15553 initializer will be looked up in SCOPE. */
15554 pushed_scope = push_scope (scope);
15556 /* Perform deferred access control checks, now that we know in which
15557 SCOPE the declared entity resides. */
15558 if (!member_p && decl)
15560 tree saved_current_function_decl = NULL_TREE;
15562 /* If the entity being declared is a function, pretend that we
15563 are in its scope. If it is a `friend', it may have access to
15564 things that would not otherwise be accessible. */
15565 if (TREE_CODE (decl) == FUNCTION_DECL)
15567 saved_current_function_decl = current_function_decl;
15568 current_function_decl = decl;
15571 /* Perform access checks for template parameters. */
15572 cp_parser_perform_template_parameter_access_checks (checks);
15574 /* Perform the access control checks for the declarator and the
15575 decl-specifiers. */
15576 perform_deferred_access_checks ();
15578 /* Restore the saved value. */
15579 if (TREE_CODE (decl) == FUNCTION_DECL)
15580 current_function_decl = saved_current_function_decl;
15583 /* Parse the initializer. */
15584 initializer = NULL_TREE;
15585 is_direct_init = false;
15586 is_non_constant_init = true;
15587 if (is_initialized)
15589 if (function_declarator_p (declarator))
15591 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15592 if (initialization_kind == CPP_EQ)
15593 initializer = cp_parser_pure_specifier (parser);
15596 /* If the declaration was erroneous, we don't really
15597 know what the user intended, so just silently
15598 consume the initializer. */
15599 if (decl != error_mark_node)
15600 error_at (initializer_start_token->location,
15601 "initializer provided for function");
15602 cp_parser_skip_to_closing_parenthesis (parser,
15603 /*recovering=*/true,
15604 /*or_comma=*/false,
15605 /*consume_paren=*/true);
15610 /* We want to record the extra mangling scope for in-class
15611 initializers of class members and initializers of static data
15612 member templates. The former is a C++0x feature which isn't
15613 implemented yet, and I expect it will involve deferring
15614 parsing of the initializer until end of class as with default
15615 arguments. So right here we only handle the latter. */
15616 if (!member_p && processing_template_decl)
15617 start_lambda_scope (decl);
15618 initializer = cp_parser_initializer (parser,
15620 &is_non_constant_init);
15621 if (!member_p && processing_template_decl)
15622 finish_lambda_scope ();
15626 /* The old parser allows attributes to appear after a parenthesized
15627 initializer. Mark Mitchell proposed removing this functionality
15628 on the GCC mailing lists on 2002-08-13. This parser accepts the
15629 attributes -- but ignores them. */
15630 if (cp_parser_allow_gnu_extensions_p (parser)
15631 && initialization_kind == CPP_OPEN_PAREN)
15632 if (cp_parser_attributes_opt (parser))
15633 warning (OPT_Wattributes,
15634 "attributes after parenthesized initializer ignored");
15636 /* For an in-class declaration, use `grokfield' to create the
15642 pop_scope (pushed_scope);
15643 pushed_scope = NULL_TREE;
15645 decl = grokfield (declarator, decl_specifiers,
15646 initializer, !is_non_constant_init,
15647 /*asmspec=*/NULL_TREE,
15648 prefix_attributes);
15649 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15650 cp_parser_save_default_args (parser, decl);
15653 /* Finish processing the declaration. But, skip member
15655 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15657 cp_finish_decl (decl,
15658 initializer, !is_non_constant_init,
15660 /* If the initializer is in parentheses, then this is
15661 a direct-initialization, which means that an
15662 `explicit' constructor is OK. Otherwise, an
15663 `explicit' constructor cannot be used. */
15664 ((is_direct_init || !is_initialized)
15665 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15667 else if ((cxx_dialect != cxx98) && friend_p
15668 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15669 /* Core issue #226 (C++0x only): A default template-argument
15670 shall not be specified in a friend class template
15672 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15673 /*is_partial=*/0, /*is_friend_decl=*/1);
15675 if (!friend_p && pushed_scope)
15676 pop_scope (pushed_scope);
15681 /* Parse a declarator.
15685 ptr-operator declarator
15687 abstract-declarator:
15688 ptr-operator abstract-declarator [opt]
15689 direct-abstract-declarator
15694 attributes [opt] direct-declarator
15695 attributes [opt] ptr-operator declarator
15697 abstract-declarator:
15698 attributes [opt] ptr-operator abstract-declarator [opt]
15699 attributes [opt] direct-abstract-declarator
15701 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15702 detect constructor, destructor or conversion operators. It is set
15703 to -1 if the declarator is a name, and +1 if it is a
15704 function. Otherwise it is set to zero. Usually you just want to
15705 test for >0, but internally the negative value is used.
15707 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15708 a decl-specifier-seq unless it declares a constructor, destructor,
15709 or conversion. It might seem that we could check this condition in
15710 semantic analysis, rather than parsing, but that makes it difficult
15711 to handle something like `f()'. We want to notice that there are
15712 no decl-specifiers, and therefore realize that this is an
15713 expression, not a declaration.)
15715 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15716 the declarator is a direct-declarator of the form "(...)".
15718 MEMBER_P is true iff this declarator is a member-declarator. */
15720 static cp_declarator *
15721 cp_parser_declarator (cp_parser* parser,
15722 cp_parser_declarator_kind dcl_kind,
15723 int* ctor_dtor_or_conv_p,
15724 bool* parenthesized_p,
15727 cp_declarator *declarator;
15728 enum tree_code code;
15729 cp_cv_quals cv_quals;
15731 tree attributes = NULL_TREE;
15733 /* Assume this is not a constructor, destructor, or type-conversion
15735 if (ctor_dtor_or_conv_p)
15736 *ctor_dtor_or_conv_p = 0;
15738 if (cp_parser_allow_gnu_extensions_p (parser))
15739 attributes = cp_parser_attributes_opt (parser);
15741 /* Check for the ptr-operator production. */
15742 cp_parser_parse_tentatively (parser);
15743 /* Parse the ptr-operator. */
15744 code = cp_parser_ptr_operator (parser,
15747 /* If that worked, then we have a ptr-operator. */
15748 if (cp_parser_parse_definitely (parser))
15750 /* If a ptr-operator was found, then this declarator was not
15752 if (parenthesized_p)
15753 *parenthesized_p = true;
15754 /* The dependent declarator is optional if we are parsing an
15755 abstract-declarator. */
15756 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15757 cp_parser_parse_tentatively (parser);
15759 /* Parse the dependent declarator. */
15760 declarator = cp_parser_declarator (parser, dcl_kind,
15761 /*ctor_dtor_or_conv_p=*/NULL,
15762 /*parenthesized_p=*/NULL,
15763 /*member_p=*/false);
15765 /* If we are parsing an abstract-declarator, we must handle the
15766 case where the dependent declarator is absent. */
15767 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15768 && !cp_parser_parse_definitely (parser))
15771 declarator = cp_parser_make_indirect_declarator
15772 (code, class_type, cv_quals, declarator);
15774 /* Everything else is a direct-declarator. */
15777 if (parenthesized_p)
15778 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15780 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15781 ctor_dtor_or_conv_p,
15785 if (attributes && declarator && declarator != cp_error_declarator)
15786 declarator->attributes = attributes;
15791 /* Parse a direct-declarator or direct-abstract-declarator.
15795 direct-declarator ( parameter-declaration-clause )
15796 cv-qualifier-seq [opt]
15797 exception-specification [opt]
15798 direct-declarator [ constant-expression [opt] ]
15801 direct-abstract-declarator:
15802 direct-abstract-declarator [opt]
15803 ( parameter-declaration-clause )
15804 cv-qualifier-seq [opt]
15805 exception-specification [opt]
15806 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15807 ( abstract-declarator )
15809 Returns a representation of the declarator. DCL_KIND is
15810 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15811 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15812 we are parsing a direct-declarator. It is
15813 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15814 of ambiguity we prefer an abstract declarator, as per
15815 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15816 cp_parser_declarator. */
15818 static cp_declarator *
15819 cp_parser_direct_declarator (cp_parser* parser,
15820 cp_parser_declarator_kind dcl_kind,
15821 int* ctor_dtor_or_conv_p,
15825 cp_declarator *declarator = NULL;
15826 tree scope = NULL_TREE;
15827 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15828 bool saved_in_declarator_p = parser->in_declarator_p;
15830 tree pushed_scope = NULL_TREE;
15834 /* Peek at the next token. */
15835 token = cp_lexer_peek_token (parser->lexer);
15836 if (token->type == CPP_OPEN_PAREN)
15838 /* This is either a parameter-declaration-clause, or a
15839 parenthesized declarator. When we know we are parsing a
15840 named declarator, it must be a parenthesized declarator
15841 if FIRST is true. For instance, `(int)' is a
15842 parameter-declaration-clause, with an omitted
15843 direct-abstract-declarator. But `((*))', is a
15844 parenthesized abstract declarator. Finally, when T is a
15845 template parameter `(T)' is a
15846 parameter-declaration-clause, and not a parenthesized
15849 We first try and parse a parameter-declaration-clause,
15850 and then try a nested declarator (if FIRST is true).
15852 It is not an error for it not to be a
15853 parameter-declaration-clause, even when FIRST is
15859 The first is the declaration of a function while the
15860 second is the definition of a variable, including its
15863 Having seen only the parenthesis, we cannot know which of
15864 these two alternatives should be selected. Even more
15865 complex are examples like:
15870 The former is a function-declaration; the latter is a
15871 variable initialization.
15873 Thus again, we try a parameter-declaration-clause, and if
15874 that fails, we back out and return. */
15876 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15879 unsigned saved_num_template_parameter_lists;
15880 bool is_declarator = false;
15883 /* In a member-declarator, the only valid interpretation
15884 of a parenthesis is the start of a
15885 parameter-declaration-clause. (It is invalid to
15886 initialize a static data member with a parenthesized
15887 initializer; only the "=" form of initialization is
15890 cp_parser_parse_tentatively (parser);
15892 /* Consume the `('. */
15893 cp_lexer_consume_token (parser->lexer);
15896 /* If this is going to be an abstract declarator, we're
15897 in a declarator and we can't have default args. */
15898 parser->default_arg_ok_p = false;
15899 parser->in_declarator_p = true;
15902 /* Inside the function parameter list, surrounding
15903 template-parameter-lists do not apply. */
15904 saved_num_template_parameter_lists
15905 = parser->num_template_parameter_lists;
15906 parser->num_template_parameter_lists = 0;
15908 begin_scope (sk_function_parms, NULL_TREE);
15910 /* Parse the parameter-declaration-clause. */
15911 params = cp_parser_parameter_declaration_clause (parser);
15913 parser->num_template_parameter_lists
15914 = saved_num_template_parameter_lists;
15916 /* Consume the `)'. */
15917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15919 /* If all went well, parse the cv-qualifier-seq and the
15920 exception-specification. */
15921 if (member_p || cp_parser_parse_definitely (parser))
15923 cp_cv_quals cv_quals;
15924 cp_virt_specifiers virt_specifiers;
15925 tree exception_specification;
15928 is_declarator = true;
15930 if (ctor_dtor_or_conv_p)
15931 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15934 /* Parse the cv-qualifier-seq. */
15935 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15936 /* And the exception-specification. */
15937 exception_specification
15938 = cp_parser_exception_specification_opt (parser);
15939 /* Parse the virt-specifier-seq. */
15940 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15942 late_return = (cp_parser_late_return_type_opt
15943 (parser, member_p ? cv_quals : -1));
15945 /* Create the function-declarator. */
15946 declarator = make_call_declarator (declarator,
15950 exception_specification,
15952 /* Any subsequent parameter lists are to do with
15953 return type, so are not those of the declared
15955 parser->default_arg_ok_p = false;
15958 /* Remove the function parms from scope. */
15959 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15960 pop_binding (DECL_NAME (t), t);
15964 /* Repeat the main loop. */
15968 /* If this is the first, we can try a parenthesized
15972 bool saved_in_type_id_in_expr_p;
15974 parser->default_arg_ok_p = saved_default_arg_ok_p;
15975 parser->in_declarator_p = saved_in_declarator_p;
15977 /* Consume the `('. */
15978 cp_lexer_consume_token (parser->lexer);
15979 /* Parse the nested declarator. */
15980 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15981 parser->in_type_id_in_expr_p = true;
15983 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15984 /*parenthesized_p=*/NULL,
15986 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15988 /* Expect a `)'. */
15989 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15990 declarator = cp_error_declarator;
15991 if (declarator == cp_error_declarator)
15994 goto handle_declarator;
15996 /* Otherwise, we must be done. */
16000 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16001 && token->type == CPP_OPEN_SQUARE)
16003 /* Parse an array-declarator. */
16006 if (ctor_dtor_or_conv_p)
16007 *ctor_dtor_or_conv_p = 0;
16010 parser->default_arg_ok_p = false;
16011 parser->in_declarator_p = true;
16012 /* Consume the `['. */
16013 cp_lexer_consume_token (parser->lexer);
16014 /* Peek at the next token. */
16015 token = cp_lexer_peek_token (parser->lexer);
16016 /* If the next token is `]', then there is no
16017 constant-expression. */
16018 if (token->type != CPP_CLOSE_SQUARE)
16020 bool non_constant_p;
16023 = cp_parser_constant_expression (parser,
16024 /*allow_non_constant=*/true,
16026 if (!non_constant_p)
16028 /* Normally, the array bound must be an integral constant
16029 expression. However, as an extension, we allow VLAs
16030 in function scopes as long as they aren't part of a
16031 parameter declaration. */
16032 else if (!parser->in_function_body
16033 || current_binding_level->kind == sk_function_parms)
16035 cp_parser_error (parser,
16036 "array bound is not an integer constant");
16037 bounds = error_mark_node;
16039 else if (processing_template_decl && !error_operand_p (bounds))
16041 /* Remember this wasn't a constant-expression. */
16042 bounds = build_nop (TREE_TYPE (bounds), bounds);
16043 TREE_SIDE_EFFECTS (bounds) = 1;
16047 bounds = NULL_TREE;
16048 /* Look for the closing `]'. */
16049 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16051 declarator = cp_error_declarator;
16055 declarator = make_array_declarator (declarator, bounds);
16057 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16060 tree qualifying_scope;
16061 tree unqualified_name;
16062 special_function_kind sfk;
16064 bool pack_expansion_p = false;
16065 cp_token *declarator_id_start_token;
16067 /* Parse a declarator-id */
16068 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16071 cp_parser_parse_tentatively (parser);
16073 /* If we see an ellipsis, we should be looking at a
16075 if (token->type == CPP_ELLIPSIS)
16077 /* Consume the `...' */
16078 cp_lexer_consume_token (parser->lexer);
16080 pack_expansion_p = true;
16084 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16086 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16087 qualifying_scope = parser->scope;
16092 if (!unqualified_name && pack_expansion_p)
16094 /* Check whether an error occurred. */
16095 okay = !cp_parser_error_occurred (parser);
16097 /* We already consumed the ellipsis to mark a
16098 parameter pack, but we have no way to report it,
16099 so abort the tentative parse. We will be exiting
16100 immediately anyway. */
16101 cp_parser_abort_tentative_parse (parser);
16104 okay = cp_parser_parse_definitely (parser);
16107 unqualified_name = error_mark_node;
16108 else if (unqualified_name
16109 && (qualifying_scope
16110 || (TREE_CODE (unqualified_name)
16111 != IDENTIFIER_NODE)))
16113 cp_parser_error (parser, "expected unqualified-id");
16114 unqualified_name = error_mark_node;
16118 if (!unqualified_name)
16120 if (unqualified_name == error_mark_node)
16122 declarator = cp_error_declarator;
16123 pack_expansion_p = false;
16124 declarator->parameter_pack_p = false;
16128 if (qualifying_scope && at_namespace_scope_p ()
16129 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16131 /* In the declaration of a member of a template class
16132 outside of the class itself, the SCOPE will sometimes
16133 be a TYPENAME_TYPE. For example, given:
16135 template <typename T>
16136 int S<T>::R::i = 3;
16138 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16139 this context, we must resolve S<T>::R to an ordinary
16140 type, rather than a typename type.
16142 The reason we normally avoid resolving TYPENAME_TYPEs
16143 is that a specialization of `S' might render
16144 `S<T>::R' not a type. However, if `S' is
16145 specialized, then this `i' will not be used, so there
16146 is no harm in resolving the types here. */
16149 /* Resolve the TYPENAME_TYPE. */
16150 type = resolve_typename_type (qualifying_scope,
16151 /*only_current_p=*/false);
16152 /* If that failed, the declarator is invalid. */
16153 if (TREE_CODE (type) == TYPENAME_TYPE)
16155 if (typedef_variant_p (type))
16156 error_at (declarator_id_start_token->location,
16157 "cannot define member of dependent typedef "
16160 error_at (declarator_id_start_token->location,
16161 "%<%T::%E%> is not a type",
16162 TYPE_CONTEXT (qualifying_scope),
16163 TYPE_IDENTIFIER (qualifying_scope));
16165 qualifying_scope = type;
16170 if (unqualified_name)
16174 if (qualifying_scope
16175 && CLASS_TYPE_P (qualifying_scope))
16176 class_type = qualifying_scope;
16178 class_type = current_class_type;
16180 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16182 tree name_type = TREE_TYPE (unqualified_name);
16183 if (class_type && same_type_p (name_type, class_type))
16185 if (qualifying_scope
16186 && CLASSTYPE_USE_TEMPLATE (name_type))
16188 error_at (declarator_id_start_token->location,
16189 "invalid use of constructor as a template");
16190 inform (declarator_id_start_token->location,
16191 "use %<%T::%D%> instead of %<%T::%D%> to "
16192 "name the constructor in a qualified name",
16194 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16195 class_type, name_type);
16196 declarator = cp_error_declarator;
16200 unqualified_name = constructor_name (class_type);
16204 /* We do not attempt to print the declarator
16205 here because we do not have enough
16206 information about its original syntactic
16208 cp_parser_error (parser, "invalid declarator");
16209 declarator = cp_error_declarator;
16216 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16217 sfk = sfk_destructor;
16218 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16219 sfk = sfk_conversion;
16220 else if (/* There's no way to declare a constructor
16221 for an anonymous type, even if the type
16222 got a name for linkage purposes. */
16223 !TYPE_WAS_ANONYMOUS (class_type)
16224 && constructor_name_p (unqualified_name,
16227 unqualified_name = constructor_name (class_type);
16228 sfk = sfk_constructor;
16230 else if (is_overloaded_fn (unqualified_name)
16231 && DECL_CONSTRUCTOR_P (get_first_fn
16232 (unqualified_name)))
16233 sfk = sfk_constructor;
16235 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16236 *ctor_dtor_or_conv_p = -1;
16239 declarator = make_id_declarator (qualifying_scope,
16242 declarator->id_loc = token->location;
16243 declarator->parameter_pack_p = pack_expansion_p;
16245 if (pack_expansion_p)
16246 maybe_warn_variadic_templates ();
16249 handle_declarator:;
16250 scope = get_scope_of_declarator (declarator);
16252 /* Any names that appear after the declarator-id for a
16253 member are looked up in the containing scope. */
16254 pushed_scope = push_scope (scope);
16255 parser->in_declarator_p = true;
16256 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16257 || (declarator && declarator->kind == cdk_id))
16258 /* Default args are only allowed on function
16260 parser->default_arg_ok_p = saved_default_arg_ok_p;
16262 parser->default_arg_ok_p = false;
16271 /* For an abstract declarator, we might wind up with nothing at this
16272 point. That's an error; the declarator is not optional. */
16274 cp_parser_error (parser, "expected declarator");
16276 /* If we entered a scope, we must exit it now. */
16278 pop_scope (pushed_scope);
16280 parser->default_arg_ok_p = saved_default_arg_ok_p;
16281 parser->in_declarator_p = saved_in_declarator_p;
16286 /* Parse a ptr-operator.
16289 * cv-qualifier-seq [opt]
16291 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16296 & cv-qualifier-seq [opt]
16298 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16299 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16300 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16301 filled in with the TYPE containing the member. *CV_QUALS is
16302 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16303 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16304 Note that the tree codes returned by this function have nothing
16305 to do with the types of trees that will be eventually be created
16306 to represent the pointer or reference type being parsed. They are
16307 just constants with suggestive names. */
16308 static enum tree_code
16309 cp_parser_ptr_operator (cp_parser* parser,
16311 cp_cv_quals *cv_quals)
16313 enum tree_code code = ERROR_MARK;
16316 /* Assume that it's not a pointer-to-member. */
16318 /* And that there are no cv-qualifiers. */
16319 *cv_quals = TYPE_UNQUALIFIED;
16321 /* Peek at the next token. */
16322 token = cp_lexer_peek_token (parser->lexer);
16324 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16325 if (token->type == CPP_MULT)
16326 code = INDIRECT_REF;
16327 else if (token->type == CPP_AND)
16329 else if ((cxx_dialect != cxx98) &&
16330 token->type == CPP_AND_AND) /* C++0x only */
16331 code = NON_LVALUE_EXPR;
16333 if (code != ERROR_MARK)
16335 /* Consume the `*', `&' or `&&'. */
16336 cp_lexer_consume_token (parser->lexer);
16338 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16339 `&', if we are allowing GNU extensions. (The only qualifier
16340 that can legally appear after `&' is `restrict', but that is
16341 enforced during semantic analysis. */
16342 if (code == INDIRECT_REF
16343 || cp_parser_allow_gnu_extensions_p (parser))
16344 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16348 /* Try the pointer-to-member case. */
16349 cp_parser_parse_tentatively (parser);
16350 /* Look for the optional `::' operator. */
16351 cp_parser_global_scope_opt (parser,
16352 /*current_scope_valid_p=*/false);
16353 /* Look for the nested-name specifier. */
16354 token = cp_lexer_peek_token (parser->lexer);
16355 cp_parser_nested_name_specifier (parser,
16356 /*typename_keyword_p=*/false,
16357 /*check_dependency_p=*/true,
16359 /*is_declaration=*/false);
16360 /* If we found it, and the next token is a `*', then we are
16361 indeed looking at a pointer-to-member operator. */
16362 if (!cp_parser_error_occurred (parser)
16363 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16365 /* Indicate that the `*' operator was used. */
16366 code = INDIRECT_REF;
16368 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16369 error_at (token->location, "%qD is a namespace", parser->scope);
16372 /* The type of which the member is a member is given by the
16374 *type = parser->scope;
16375 /* The next name will not be qualified. */
16376 parser->scope = NULL_TREE;
16377 parser->qualifying_scope = NULL_TREE;
16378 parser->object_scope = NULL_TREE;
16379 /* Look for the optional cv-qualifier-seq. */
16380 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16383 /* If that didn't work we don't have a ptr-operator. */
16384 if (!cp_parser_parse_definitely (parser))
16385 cp_parser_error (parser, "expected ptr-operator");
16391 /* Parse an (optional) cv-qualifier-seq.
16394 cv-qualifier cv-qualifier-seq [opt]
16405 Returns a bitmask representing the cv-qualifiers. */
16408 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16410 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16415 cp_cv_quals cv_qualifier;
16417 /* Peek at the next token. */
16418 token = cp_lexer_peek_token (parser->lexer);
16419 /* See if it's a cv-qualifier. */
16420 switch (token->keyword)
16423 cv_qualifier = TYPE_QUAL_CONST;
16427 cv_qualifier = TYPE_QUAL_VOLATILE;
16431 cv_qualifier = TYPE_QUAL_RESTRICT;
16435 cv_qualifier = TYPE_UNQUALIFIED;
16442 if (cv_quals & cv_qualifier)
16444 error_at (token->location, "duplicate cv-qualifier");
16445 cp_lexer_purge_token (parser->lexer);
16449 cp_lexer_consume_token (parser->lexer);
16450 cv_quals |= cv_qualifier;
16457 /* Parse an (optional) virt-specifier-seq.
16459 virt-specifier-seq:
16460 virt-specifier virt-specifier-seq [opt]
16466 Returns a bitmask representing the virt-specifiers. */
16468 static cp_virt_specifiers
16469 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16471 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16476 cp_virt_specifiers virt_specifier;
16478 /* Peek at the next token. */
16479 token = cp_lexer_peek_token (parser->lexer);
16480 /* See if it's a virt-specifier-qualifier. */
16481 if (token->type != CPP_NAME)
16483 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16485 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16486 virt_specifier = VIRT_SPEC_OVERRIDE;
16488 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16490 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16491 virt_specifier = VIRT_SPEC_FINAL;
16493 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16495 virt_specifier = VIRT_SPEC_FINAL;
16500 if (virt_specifiers & virt_specifier)
16502 error_at (token->location, "duplicate virt-specifier");
16503 cp_lexer_purge_token (parser->lexer);
16507 cp_lexer_consume_token (parser->lexer);
16508 virt_specifiers |= virt_specifier;
16511 return virt_specifiers;
16514 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16515 is in scope even though it isn't real. */
16518 inject_this_parameter (tree ctype, cp_cv_quals quals)
16522 if (current_class_ptr)
16524 /* We don't clear this between NSDMIs. Is it already what we want? */
16525 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16526 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16527 && cp_type_quals (type) == quals)
16531 this_parm = build_this_parm (ctype, quals);
16532 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16533 current_class_ptr = NULL_TREE;
16535 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16536 current_class_ptr = this_parm;
16539 /* Parse a late-specified return type, if any. This is not a separate
16540 non-terminal, but part of a function declarator, which looks like
16542 -> trailing-type-specifier-seq abstract-declarator(opt)
16544 Returns the type indicated by the type-id.
16546 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16550 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16555 /* Peek at the next token. */
16556 token = cp_lexer_peek_token (parser->lexer);
16557 /* A late-specified return type is indicated by an initial '->'. */
16558 if (token->type != CPP_DEREF)
16561 /* Consume the ->. */
16562 cp_lexer_consume_token (parser->lexer);
16566 /* DR 1207: 'this' is in scope in the trailing return type. */
16567 gcc_assert (current_class_ptr == NULL_TREE);
16568 inject_this_parameter (current_class_type, quals);
16571 type = cp_parser_trailing_type_id (parser);
16574 current_class_ptr = current_class_ref = NULL_TREE;
16579 /* Parse a declarator-id.
16583 :: [opt] nested-name-specifier [opt] type-name
16585 In the `id-expression' case, the value returned is as for
16586 cp_parser_id_expression if the id-expression was an unqualified-id.
16587 If the id-expression was a qualified-id, then a SCOPE_REF is
16588 returned. The first operand is the scope (either a NAMESPACE_DECL
16589 or TREE_TYPE), but the second is still just a representation of an
16593 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16596 /* The expression must be an id-expression. Assume that qualified
16597 names are the names of types so that:
16600 int S<T>::R::i = 3;
16602 will work; we must treat `S<T>::R' as the name of a type.
16603 Similarly, assume that qualified names are templates, where
16607 int S<T>::R<T>::i = 3;
16610 id = cp_parser_id_expression (parser,
16611 /*template_keyword_p=*/false,
16612 /*check_dependency_p=*/false,
16613 /*template_p=*/NULL,
16614 /*declarator_p=*/true,
16616 if (id && BASELINK_P (id))
16617 id = BASELINK_FUNCTIONS (id);
16621 /* Parse a type-id.
16624 type-specifier-seq abstract-declarator [opt]
16626 Returns the TYPE specified. */
16629 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16630 bool is_trailing_return)
16632 cp_decl_specifier_seq type_specifier_seq;
16633 cp_declarator *abstract_declarator;
16635 /* Parse the type-specifier-seq. */
16636 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16637 is_trailing_return,
16638 &type_specifier_seq);
16639 if (type_specifier_seq.type == error_mark_node)
16640 return error_mark_node;
16642 /* There might or might not be an abstract declarator. */
16643 cp_parser_parse_tentatively (parser);
16644 /* Look for the declarator. */
16645 abstract_declarator
16646 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16647 /*parenthesized_p=*/NULL,
16648 /*member_p=*/false);
16649 /* Check to see if there really was a declarator. */
16650 if (!cp_parser_parse_definitely (parser))
16651 abstract_declarator = NULL;
16653 if (type_specifier_seq.type
16654 && type_uses_auto (type_specifier_seq.type))
16656 /* A type-id with type 'auto' is only ok if the abstract declarator
16657 is a function declarator with a late-specified return type. */
16658 if (abstract_declarator
16659 && abstract_declarator->kind == cdk_function
16660 && abstract_declarator->u.function.late_return_type)
16664 error ("invalid use of %<auto%>");
16665 return error_mark_node;
16669 return groktypename (&type_specifier_seq, abstract_declarator,
16673 static tree cp_parser_type_id (cp_parser *parser)
16675 return cp_parser_type_id_1 (parser, false, false);
16678 static tree cp_parser_template_type_arg (cp_parser *parser)
16681 const char *saved_message = parser->type_definition_forbidden_message;
16682 parser->type_definition_forbidden_message
16683 = G_("types may not be defined in template arguments");
16684 r = cp_parser_type_id_1 (parser, true, false);
16685 parser->type_definition_forbidden_message = saved_message;
16689 static tree cp_parser_trailing_type_id (cp_parser *parser)
16691 return cp_parser_type_id_1 (parser, false, true);
16694 /* Parse a type-specifier-seq.
16696 type-specifier-seq:
16697 type-specifier type-specifier-seq [opt]
16701 type-specifier-seq:
16702 attributes type-specifier-seq [opt]
16704 If IS_DECLARATION is true, we are at the start of a "condition" or
16705 exception-declaration, so we might be followed by a declarator-id.
16707 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16708 i.e. we've just seen "->".
16710 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16713 cp_parser_type_specifier_seq (cp_parser* parser,
16714 bool is_declaration,
16715 bool is_trailing_return,
16716 cp_decl_specifier_seq *type_specifier_seq)
16718 bool seen_type_specifier = false;
16719 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16720 cp_token *start_token = NULL;
16722 /* Clear the TYPE_SPECIFIER_SEQ. */
16723 clear_decl_specs (type_specifier_seq);
16725 /* In the context of a trailing return type, enum E { } is an
16726 elaborated-type-specifier followed by a function-body, not an
16728 if (is_trailing_return)
16729 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16731 /* Parse the type-specifiers and attributes. */
16734 tree type_specifier;
16735 bool is_cv_qualifier;
16737 /* Check for attributes first. */
16738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16740 type_specifier_seq->attributes =
16741 chainon (type_specifier_seq->attributes,
16742 cp_parser_attributes_opt (parser));
16746 /* record the token of the beginning of the type specifier seq,
16747 for error reporting purposes*/
16749 start_token = cp_lexer_peek_token (parser->lexer);
16751 /* Look for the type-specifier. */
16752 type_specifier = cp_parser_type_specifier (parser,
16754 type_specifier_seq,
16755 /*is_declaration=*/false,
16758 if (!type_specifier)
16760 /* If the first type-specifier could not be found, this is not a
16761 type-specifier-seq at all. */
16762 if (!seen_type_specifier)
16764 cp_parser_error (parser, "expected type-specifier");
16765 type_specifier_seq->type = error_mark_node;
16768 /* If subsequent type-specifiers could not be found, the
16769 type-specifier-seq is complete. */
16773 seen_type_specifier = true;
16774 /* The standard says that a condition can be:
16776 type-specifier-seq declarator = assignment-expression
16783 we should treat the "S" as a declarator, not as a
16784 type-specifier. The standard doesn't say that explicitly for
16785 type-specifier-seq, but it does say that for
16786 decl-specifier-seq in an ordinary declaration. Perhaps it
16787 would be clearer just to allow a decl-specifier-seq here, and
16788 then add a semantic restriction that if any decl-specifiers
16789 that are not type-specifiers appear, the program is invalid. */
16790 if (is_declaration && !is_cv_qualifier)
16791 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16794 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16797 /* Parse a parameter-declaration-clause.
16799 parameter-declaration-clause:
16800 parameter-declaration-list [opt] ... [opt]
16801 parameter-declaration-list , ...
16803 Returns a representation for the parameter declarations. A return
16804 value of NULL indicates a parameter-declaration-clause consisting
16805 only of an ellipsis. */
16808 cp_parser_parameter_declaration_clause (cp_parser* parser)
16815 /* Peek at the next token. */
16816 token = cp_lexer_peek_token (parser->lexer);
16817 /* Check for trivial parameter-declaration-clauses. */
16818 if (token->type == CPP_ELLIPSIS)
16820 /* Consume the `...' token. */
16821 cp_lexer_consume_token (parser->lexer);
16824 else if (token->type == CPP_CLOSE_PAREN)
16825 /* There are no parameters. */
16827 #ifndef NO_IMPLICIT_EXTERN_C
16828 if (in_system_header && current_class_type == NULL
16829 && current_lang_name == lang_name_c)
16833 return void_list_node;
16835 /* Check for `(void)', too, which is a special case. */
16836 else if (token->keyword == RID_VOID
16837 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16838 == CPP_CLOSE_PAREN))
16840 /* Consume the `void' token. */
16841 cp_lexer_consume_token (parser->lexer);
16842 /* There are no parameters. */
16843 return void_list_node;
16846 /* Parse the parameter-declaration-list. */
16847 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16848 /* If a parse error occurred while parsing the
16849 parameter-declaration-list, then the entire
16850 parameter-declaration-clause is erroneous. */
16854 /* Peek at the next token. */
16855 token = cp_lexer_peek_token (parser->lexer);
16856 /* If it's a `,', the clause should terminate with an ellipsis. */
16857 if (token->type == CPP_COMMA)
16859 /* Consume the `,'. */
16860 cp_lexer_consume_token (parser->lexer);
16861 /* Expect an ellipsis. */
16863 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16865 /* It might also be `...' if the optional trailing `,' was
16867 else if (token->type == CPP_ELLIPSIS)
16869 /* Consume the `...' token. */
16870 cp_lexer_consume_token (parser->lexer);
16871 /* And remember that we saw it. */
16875 ellipsis_p = false;
16877 /* Finish the parameter list. */
16879 parameters = chainon (parameters, void_list_node);
16884 /* Parse a parameter-declaration-list.
16886 parameter-declaration-list:
16887 parameter-declaration
16888 parameter-declaration-list , parameter-declaration
16890 Returns a representation of the parameter-declaration-list, as for
16891 cp_parser_parameter_declaration_clause. However, the
16892 `void_list_node' is never appended to the list. Upon return,
16893 *IS_ERROR will be true iff an error occurred. */
16896 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16898 tree parameters = NULL_TREE;
16899 tree *tail = ¶meters;
16900 bool saved_in_unbraced_linkage_specification_p;
16903 /* Assume all will go well. */
16905 /* The special considerations that apply to a function within an
16906 unbraced linkage specifications do not apply to the parameters
16907 to the function. */
16908 saved_in_unbraced_linkage_specification_p
16909 = parser->in_unbraced_linkage_specification_p;
16910 parser->in_unbraced_linkage_specification_p = false;
16912 /* Look for more parameters. */
16915 cp_parameter_declarator *parameter;
16916 tree decl = error_mark_node;
16917 bool parenthesized_p = false;
16918 /* Parse the parameter. */
16920 = cp_parser_parameter_declaration (parser,
16921 /*template_parm_p=*/false,
16924 /* We don't know yet if the enclosing context is deprecated, so wait
16925 and warn in grokparms if appropriate. */
16926 deprecated_state = DEPRECATED_SUPPRESS;
16929 decl = grokdeclarator (parameter->declarator,
16930 ¶meter->decl_specifiers,
16932 parameter->default_argument != NULL_TREE,
16933 ¶meter->decl_specifiers.attributes);
16935 deprecated_state = DEPRECATED_NORMAL;
16937 /* If a parse error occurred parsing the parameter declaration,
16938 then the entire parameter-declaration-list is erroneous. */
16939 if (decl == error_mark_node)
16942 parameters = error_mark_node;
16946 if (parameter->decl_specifiers.attributes)
16947 cplus_decl_attributes (&decl,
16948 parameter->decl_specifiers.attributes,
16950 if (DECL_NAME (decl))
16951 decl = pushdecl (decl);
16953 if (decl != error_mark_node)
16955 retrofit_lang_decl (decl);
16956 DECL_PARM_INDEX (decl) = ++index;
16957 DECL_PARM_LEVEL (decl) = function_parm_depth ();
16960 /* Add the new parameter to the list. */
16961 *tail = build_tree_list (parameter->default_argument, decl);
16962 tail = &TREE_CHAIN (*tail);
16964 /* Peek at the next token. */
16965 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16966 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16967 /* These are for Objective-C++ */
16968 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16969 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16970 /* The parameter-declaration-list is complete. */
16972 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16976 /* Peek at the next token. */
16977 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16978 /* If it's an ellipsis, then the list is complete. */
16979 if (token->type == CPP_ELLIPSIS)
16981 /* Otherwise, there must be more parameters. Consume the
16983 cp_lexer_consume_token (parser->lexer);
16984 /* When parsing something like:
16986 int i(float f, double d)
16988 we can tell after seeing the declaration for "f" that we
16989 are not looking at an initialization of a variable "i",
16990 but rather at the declaration of a function "i".
16992 Due to the fact that the parsing of template arguments
16993 (as specified to a template-id) requires backtracking we
16994 cannot use this technique when inside a template argument
16996 if (!parser->in_template_argument_list_p
16997 && !parser->in_type_id_in_expr_p
16998 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16999 /* However, a parameter-declaration of the form
17000 "foat(f)" (which is a valid declaration of a
17001 parameter "f") can also be interpreted as an
17002 expression (the conversion of "f" to "float"). */
17003 && !parenthesized_p)
17004 cp_parser_commit_to_tentative_parse (parser);
17008 cp_parser_error (parser, "expected %<,%> or %<...%>");
17009 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17010 cp_parser_skip_to_closing_parenthesis (parser,
17011 /*recovering=*/true,
17012 /*or_comma=*/false,
17013 /*consume_paren=*/false);
17018 parser->in_unbraced_linkage_specification_p
17019 = saved_in_unbraced_linkage_specification_p;
17024 /* Parse a parameter declaration.
17026 parameter-declaration:
17027 decl-specifier-seq ... [opt] declarator
17028 decl-specifier-seq declarator = assignment-expression
17029 decl-specifier-seq ... [opt] abstract-declarator [opt]
17030 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17032 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17033 declares a template parameter. (In that case, a non-nested `>'
17034 token encountered during the parsing of the assignment-expression
17035 is not interpreted as a greater-than operator.)
17037 Returns a representation of the parameter, or NULL if an error
17038 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17039 true iff the declarator is of the form "(p)". */
17041 static cp_parameter_declarator *
17042 cp_parser_parameter_declaration (cp_parser *parser,
17043 bool template_parm_p,
17044 bool *parenthesized_p)
17046 int declares_class_or_enum;
17047 cp_decl_specifier_seq decl_specifiers;
17048 cp_declarator *declarator;
17049 tree default_argument;
17050 cp_token *token = NULL, *declarator_token_start = NULL;
17051 const char *saved_message;
17053 /* In a template parameter, `>' is not an operator.
17057 When parsing a default template-argument for a non-type
17058 template-parameter, the first non-nested `>' is taken as the end
17059 of the template parameter-list rather than a greater-than
17062 /* Type definitions may not appear in parameter types. */
17063 saved_message = parser->type_definition_forbidden_message;
17064 parser->type_definition_forbidden_message
17065 = G_("types may not be defined in parameter types");
17067 /* Parse the declaration-specifiers. */
17068 cp_parser_decl_specifier_seq (parser,
17069 CP_PARSER_FLAGS_NONE,
17071 &declares_class_or_enum);
17073 /* Complain about missing 'typename' or other invalid type names. */
17074 if (!decl_specifiers.any_type_specifiers_p)
17075 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17077 /* If an error occurred, there's no reason to attempt to parse the
17078 rest of the declaration. */
17079 if (cp_parser_error_occurred (parser))
17081 parser->type_definition_forbidden_message = saved_message;
17085 /* Peek at the next token. */
17086 token = cp_lexer_peek_token (parser->lexer);
17088 /* If the next token is a `)', `,', `=', `>', or `...', then there
17089 is no declarator. However, when variadic templates are enabled,
17090 there may be a declarator following `...'. */
17091 if (token->type == CPP_CLOSE_PAREN
17092 || token->type == CPP_COMMA
17093 || token->type == CPP_EQ
17094 || token->type == CPP_GREATER)
17097 if (parenthesized_p)
17098 *parenthesized_p = false;
17100 /* Otherwise, there should be a declarator. */
17103 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17104 parser->default_arg_ok_p = false;
17106 /* After seeing a decl-specifier-seq, if the next token is not a
17107 "(", there is no possibility that the code is a valid
17108 expression. Therefore, if parsing tentatively, we commit at
17110 if (!parser->in_template_argument_list_p
17111 /* In an expression context, having seen:
17115 we cannot be sure whether we are looking at a
17116 function-type (taking a "char" as a parameter) or a cast
17117 of some object of type "char" to "int". */
17118 && !parser->in_type_id_in_expr_p
17119 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17120 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17121 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17122 cp_parser_commit_to_tentative_parse (parser);
17123 /* Parse the declarator. */
17124 declarator_token_start = token;
17125 declarator = cp_parser_declarator (parser,
17126 CP_PARSER_DECLARATOR_EITHER,
17127 /*ctor_dtor_or_conv_p=*/NULL,
17129 /*member_p=*/false);
17130 parser->default_arg_ok_p = saved_default_arg_ok_p;
17131 /* After the declarator, allow more attributes. */
17132 decl_specifiers.attributes
17133 = chainon (decl_specifiers.attributes,
17134 cp_parser_attributes_opt (parser));
17137 /* If the next token is an ellipsis, and we have not seen a
17138 declarator name, and the type of the declarator contains parameter
17139 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17140 a parameter pack expansion expression. Otherwise, leave the
17141 ellipsis for a C-style variadic function. */
17142 token = cp_lexer_peek_token (parser->lexer);
17143 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17145 tree type = decl_specifiers.type;
17147 if (type && DECL_P (type))
17148 type = TREE_TYPE (type);
17151 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17152 && declarator_can_be_parameter_pack (declarator)
17153 && (!declarator || !declarator->parameter_pack_p)
17154 && uses_parameter_packs (type))
17156 /* Consume the `...'. */
17157 cp_lexer_consume_token (parser->lexer);
17158 maybe_warn_variadic_templates ();
17160 /* Build a pack expansion type */
17162 declarator->parameter_pack_p = true;
17164 decl_specifiers.type = make_pack_expansion (type);
17168 /* The restriction on defining new types applies only to the type
17169 of the parameter, not to the default argument. */
17170 parser->type_definition_forbidden_message = saved_message;
17172 /* If the next token is `=', then process a default argument. */
17173 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17175 /* If we are defining a class, then the tokens that make up the
17176 default argument must be saved and processed later. */
17177 if (!template_parm_p && at_class_scope_p ()
17178 && TYPE_BEING_DEFINED (current_class_type)
17179 && !LAMBDA_TYPE_P (current_class_type))
17181 unsigned depth = 0;
17182 int maybe_template_id = 0;
17183 cp_token *first_token;
17186 /* Add tokens until we have processed the entire default
17187 argument. We add the range [first_token, token). */
17188 first_token = cp_lexer_peek_token (parser->lexer);
17193 /* Peek at the next token. */
17194 token = cp_lexer_peek_token (parser->lexer);
17195 /* What we do depends on what token we have. */
17196 switch (token->type)
17198 /* In valid code, a default argument must be
17199 immediately followed by a `,' `)', or `...'. */
17201 if (depth == 0 && maybe_template_id)
17203 /* If we've seen a '<', we might be in a
17204 template-argument-list. Until Core issue 325 is
17205 resolved, we don't know how this situation ought
17206 to be handled, so try to DTRT. We check whether
17207 what comes after the comma is a valid parameter
17208 declaration list. If it is, then the comma ends
17209 the default argument; otherwise the default
17210 argument continues. */
17211 bool error = false;
17214 /* Set ITALP so cp_parser_parameter_declaration_list
17215 doesn't decide to commit to this parse. */
17216 bool saved_italp = parser->in_template_argument_list_p;
17217 parser->in_template_argument_list_p = true;
17219 cp_parser_parse_tentatively (parser);
17220 cp_lexer_consume_token (parser->lexer);
17221 begin_scope (sk_function_parms, NULL_TREE);
17222 cp_parser_parameter_declaration_list (parser, &error);
17223 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17224 pop_binding (DECL_NAME (t), t);
17226 if (!cp_parser_error_occurred (parser) && !error)
17228 cp_parser_abort_tentative_parse (parser);
17230 parser->in_template_argument_list_p = saved_italp;
17233 case CPP_CLOSE_PAREN:
17235 /* If we run into a non-nested `;', `}', or `]',
17236 then the code is invalid -- but the default
17237 argument is certainly over. */
17238 case CPP_SEMICOLON:
17239 case CPP_CLOSE_BRACE:
17240 case CPP_CLOSE_SQUARE:
17243 /* Update DEPTH, if necessary. */
17244 else if (token->type == CPP_CLOSE_PAREN
17245 || token->type == CPP_CLOSE_BRACE
17246 || token->type == CPP_CLOSE_SQUARE)
17250 case CPP_OPEN_PAREN:
17251 case CPP_OPEN_SQUARE:
17252 case CPP_OPEN_BRACE:
17258 /* This might be the comparison operator, or it might
17259 start a template argument list. */
17260 ++maybe_template_id;
17264 if (cxx_dialect == cxx98)
17266 /* Fall through for C++0x, which treats the `>>'
17267 operator like two `>' tokens in certain
17273 /* This might be an operator, or it might close a
17274 template argument list. But if a previous '<'
17275 started a template argument list, this will have
17276 closed it, so we can't be in one anymore. */
17277 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17278 if (maybe_template_id < 0)
17279 maybe_template_id = 0;
17283 /* If we run out of tokens, issue an error message. */
17285 case CPP_PRAGMA_EOL:
17286 error_at (token->location, "file ends in default argument");
17292 /* In these cases, we should look for template-ids.
17293 For example, if the default argument is
17294 `X<int, double>()', we need to do name lookup to
17295 figure out whether or not `X' is a template; if
17296 so, the `,' does not end the default argument.
17298 That is not yet done. */
17305 /* If we've reached the end, stop. */
17309 /* Add the token to the token block. */
17310 token = cp_lexer_consume_token (parser->lexer);
17313 /* Create a DEFAULT_ARG to represent the unparsed default
17315 default_argument = make_node (DEFAULT_ARG);
17316 DEFARG_TOKENS (default_argument)
17317 = cp_token_cache_new (first_token, token);
17318 DEFARG_INSTANTIATIONS (default_argument) = NULL;
17320 /* Outside of a class definition, we can just parse the
17321 assignment-expression. */
17324 token = cp_lexer_peek_token (parser->lexer);
17326 = cp_parser_default_argument (parser, template_parm_p);
17329 if (!parser->default_arg_ok_p)
17331 if (flag_permissive)
17332 warning (0, "deprecated use of default argument for parameter of non-function");
17335 error_at (token->location,
17336 "default arguments are only "
17337 "permitted for function parameters");
17338 default_argument = NULL_TREE;
17341 else if ((declarator && declarator->parameter_pack_p)
17342 || (decl_specifiers.type
17343 && PACK_EXPANSION_P (decl_specifiers.type)))
17345 /* Find the name of the parameter pack. */
17346 cp_declarator *id_declarator = declarator;
17347 while (id_declarator && id_declarator->kind != cdk_id)
17348 id_declarator = id_declarator->declarator;
17350 if (id_declarator && id_declarator->kind == cdk_id)
17351 error_at (declarator_token_start->location,
17353 ? G_("template parameter pack %qD "
17354 "cannot have a default argument")
17355 : G_("parameter pack %qD cannot have "
17356 "a default argument"),
17357 id_declarator->u.id.unqualified_name);
17359 error_at (declarator_token_start->location,
17361 ? G_("template parameter pack cannot have "
17362 "a default argument")
17363 : G_("parameter pack cannot have a "
17364 "default argument"));
17366 default_argument = NULL_TREE;
17370 default_argument = NULL_TREE;
17372 return make_parameter_declarator (&decl_specifiers,
17377 /* Parse a default argument and return it.
17379 TEMPLATE_PARM_P is true if this is a default argument for a
17380 non-type template parameter. */
17382 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17384 tree default_argument = NULL_TREE;
17385 bool saved_greater_than_is_operator_p;
17386 bool saved_local_variables_forbidden_p;
17387 bool non_constant_p, is_direct_init;
17389 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17391 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17392 parser->greater_than_is_operator_p = !template_parm_p;
17393 /* Local variable names (and the `this' keyword) may not
17394 appear in a default argument. */
17395 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17396 parser->local_variables_forbidden_p = true;
17397 /* Parse the assignment-expression. */
17398 if (template_parm_p)
17399 push_deferring_access_checks (dk_no_deferred);
17401 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17402 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17403 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17404 if (template_parm_p)
17405 pop_deferring_access_checks ();
17406 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17407 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17409 return default_argument;
17412 /* Parse a function-body.
17415 compound_statement */
17418 cp_parser_function_body (cp_parser *parser)
17420 cp_parser_compound_statement (parser, NULL, false, true);
17423 /* Parse a ctor-initializer-opt followed by a function-body. Return
17424 true if a ctor-initializer was present. */
17427 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17430 bool ctor_initializer_p;
17431 const bool check_body_p =
17432 DECL_CONSTRUCTOR_P (current_function_decl)
17433 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17436 /* Begin the function body. */
17437 body = begin_function_body ();
17438 /* Parse the optional ctor-initializer. */
17439 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17441 /* If we're parsing a constexpr constructor definition, we need
17442 to check that the constructor body is indeed empty. However,
17443 before we get to cp_parser_function_body lot of junk has been
17444 generated, so we can't just check that we have an empty block.
17445 Rather we take a snapshot of the outermost block, and check whether
17446 cp_parser_function_body changed its state. */
17450 if (TREE_CODE (list) == BIND_EXPR)
17451 list = BIND_EXPR_BODY (list);
17452 if (TREE_CODE (list) == STATEMENT_LIST
17453 && STATEMENT_LIST_TAIL (list) != NULL)
17454 last = STATEMENT_LIST_TAIL (list)->stmt;
17456 /* Parse the function-body. */
17457 cp_parser_function_body (parser);
17459 check_constexpr_ctor_body (last, list);
17460 /* Finish the function body. */
17461 finish_function_body (body);
17463 return ctor_initializer_p;
17466 /* Parse an initializer.
17469 = initializer-clause
17470 ( expression-list )
17472 Returns an expression representing the initializer. If no
17473 initializer is present, NULL_TREE is returned.
17475 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17476 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17477 set to TRUE if there is no initializer present. If there is an
17478 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17479 is set to true; otherwise it is set to false. */
17482 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17483 bool* non_constant_p)
17488 /* Peek at the next token. */
17489 token = cp_lexer_peek_token (parser->lexer);
17491 /* Let our caller know whether or not this initializer was
17493 *is_direct_init = (token->type != CPP_EQ);
17494 /* Assume that the initializer is constant. */
17495 *non_constant_p = false;
17497 if (token->type == CPP_EQ)
17499 /* Consume the `='. */
17500 cp_lexer_consume_token (parser->lexer);
17501 /* Parse the initializer-clause. */
17502 init = cp_parser_initializer_clause (parser, non_constant_p);
17504 else if (token->type == CPP_OPEN_PAREN)
17507 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17509 /*allow_expansion_p=*/true,
17512 return error_mark_node;
17513 init = build_tree_list_vec (vec);
17514 release_tree_vector (vec);
17516 else if (token->type == CPP_OPEN_BRACE)
17518 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17519 init = cp_parser_braced_list (parser, non_constant_p);
17520 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17524 /* Anything else is an error. */
17525 cp_parser_error (parser, "expected initializer");
17526 init = error_mark_node;
17532 /* Parse an initializer-clause.
17534 initializer-clause:
17535 assignment-expression
17538 Returns an expression representing the initializer.
17540 If the `assignment-expression' production is used the value
17541 returned is simply a representation for the expression.
17543 Otherwise, calls cp_parser_braced_list. */
17546 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17550 /* Assume the expression is constant. */
17551 *non_constant_p = false;
17553 /* If it is not a `{', then we are looking at an
17554 assignment-expression. */
17555 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17558 = cp_parser_constant_expression (parser,
17559 /*allow_non_constant_p=*/true,
17563 initializer = cp_parser_braced_list (parser, non_constant_p);
17565 return initializer;
17568 /* Parse a brace-enclosed initializer list.
17571 { initializer-list , [opt] }
17574 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17575 the elements of the initializer-list (or NULL, if the last
17576 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17577 NULL_TREE. There is no way to detect whether or not the optional
17578 trailing `,' was provided. NON_CONSTANT_P is as for
17579 cp_parser_initializer. */
17582 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17586 /* Consume the `{' token. */
17587 cp_lexer_consume_token (parser->lexer);
17588 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17589 initializer = make_node (CONSTRUCTOR);
17590 /* If it's not a `}', then there is a non-trivial initializer. */
17591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17593 /* Parse the initializer list. */
17594 CONSTRUCTOR_ELTS (initializer)
17595 = cp_parser_initializer_list (parser, non_constant_p);
17596 /* A trailing `,' token is allowed. */
17597 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17598 cp_lexer_consume_token (parser->lexer);
17600 /* Now, there should be a trailing `}'. */
17601 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17602 TREE_TYPE (initializer) = init_list_type_node;
17603 return initializer;
17606 /* Parse an initializer-list.
17609 initializer-clause ... [opt]
17610 initializer-list , initializer-clause ... [opt]
17615 designation initializer-clause ...[opt]
17616 initializer-list , designation initializer-clause ...[opt]
17621 [ constant-expression ] =
17623 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17624 for the initializer. If the INDEX of the elt is non-NULL, it is the
17625 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17626 as for cp_parser_initializer. */
17628 static VEC(constructor_elt,gc) *
17629 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17631 VEC(constructor_elt,gc) *v = NULL;
17633 /* Assume all of the expressions are constant. */
17634 *non_constant_p = false;
17636 /* Parse the rest of the list. */
17642 bool clause_non_constant_p;
17644 /* If the next token is an identifier and the following one is a
17645 colon, we are looking at the GNU designated-initializer
17647 if (cp_parser_allow_gnu_extensions_p (parser)
17648 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17649 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17651 /* Warn the user that they are using an extension. */
17652 pedwarn (input_location, OPT_pedantic,
17653 "ISO C++ does not allow designated initializers");
17654 /* Consume the identifier. */
17655 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17656 /* Consume the `:'. */
17657 cp_lexer_consume_token (parser->lexer);
17659 /* Also handle the C99 syntax, '. id ='. */
17660 else if (cp_parser_allow_gnu_extensions_p (parser)
17661 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17662 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17663 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17665 /* Warn the user that they are using an extension. */
17666 pedwarn (input_location, OPT_pedantic,
17667 "ISO C++ does not allow C99 designated initializers");
17668 /* Consume the `.'. */
17669 cp_lexer_consume_token (parser->lexer);
17670 /* Consume the identifier. */
17671 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17672 /* Consume the `='. */
17673 cp_lexer_consume_token (parser->lexer);
17675 /* Also handle C99 array designators, '[ const ] ='. */
17676 else if (cp_parser_allow_gnu_extensions_p (parser)
17677 && !c_dialect_objc ()
17678 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17680 /* In C++11, [ could start a lambda-introducer. */
17681 cp_parser_parse_tentatively (parser);
17682 cp_lexer_consume_token (parser->lexer);
17683 designator = cp_parser_constant_expression (parser, false, NULL);
17684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17685 cp_parser_require (parser, CPP_EQ, RT_EQ);
17686 cp_parser_parse_definitely (parser);
17689 designator = NULL_TREE;
17691 /* Parse the initializer. */
17692 initializer = cp_parser_initializer_clause (parser,
17693 &clause_non_constant_p);
17694 /* If any clause is non-constant, so is the entire initializer. */
17695 if (clause_non_constant_p)
17696 *non_constant_p = true;
17698 /* If we have an ellipsis, this is an initializer pack
17700 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17702 /* Consume the `...'. */
17703 cp_lexer_consume_token (parser->lexer);
17705 /* Turn the initializer into an initializer expansion. */
17706 initializer = make_pack_expansion (initializer);
17709 /* Add it to the vector. */
17710 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17712 /* If the next token is not a comma, we have reached the end of
17714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17717 /* Peek at the next token. */
17718 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17719 /* If the next token is a `}', then we're still done. An
17720 initializer-clause can have a trailing `,' after the
17721 initializer-list and before the closing `}'. */
17722 if (token->type == CPP_CLOSE_BRACE)
17725 /* Consume the `,' token. */
17726 cp_lexer_consume_token (parser->lexer);
17732 /* Classes [gram.class] */
17734 /* Parse a class-name.
17740 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17741 to indicate that names looked up in dependent types should be
17742 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17743 keyword has been used to indicate that the name that appears next
17744 is a template. TAG_TYPE indicates the explicit tag given before
17745 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17746 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17747 is the class being defined in a class-head.
17749 Returns the TYPE_DECL representing the class. */
17752 cp_parser_class_name (cp_parser *parser,
17753 bool typename_keyword_p,
17754 bool template_keyword_p,
17755 enum tag_types tag_type,
17756 bool check_dependency_p,
17758 bool is_declaration)
17764 tree identifier = NULL_TREE;
17766 /* All class-names start with an identifier. */
17767 token = cp_lexer_peek_token (parser->lexer);
17768 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17770 cp_parser_error (parser, "expected class-name");
17771 return error_mark_node;
17774 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17775 to a template-id, so we save it here. */
17776 scope = parser->scope;
17777 if (scope == error_mark_node)
17778 return error_mark_node;
17780 /* Any name names a type if we're following the `typename' keyword
17781 in a qualified name where the enclosing scope is type-dependent. */
17782 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17783 && dependent_type_p (scope));
17784 /* Handle the common case (an identifier, but not a template-id)
17786 if (token->type == CPP_NAME
17787 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17789 cp_token *identifier_token;
17792 /* Look for the identifier. */
17793 identifier_token = cp_lexer_peek_token (parser->lexer);
17794 ambiguous_p = identifier_token->ambiguous_p;
17795 identifier = cp_parser_identifier (parser);
17796 /* If the next token isn't an identifier, we are certainly not
17797 looking at a class-name. */
17798 if (identifier == error_mark_node)
17799 decl = error_mark_node;
17800 /* If we know this is a type-name, there's no need to look it
17802 else if (typename_p)
17806 tree ambiguous_decls;
17807 /* If we already know that this lookup is ambiguous, then
17808 we've already issued an error message; there's no reason
17812 cp_parser_simulate_error (parser);
17813 return error_mark_node;
17815 /* If the next token is a `::', then the name must be a type
17818 [basic.lookup.qual]
17820 During the lookup for a name preceding the :: scope
17821 resolution operator, object, function, and enumerator
17822 names are ignored. */
17823 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17824 tag_type = typename_type;
17825 /* Look up the name. */
17826 decl = cp_parser_lookup_name (parser, identifier,
17828 /*is_template=*/false,
17829 /*is_namespace=*/false,
17830 check_dependency_p,
17832 identifier_token->location);
17833 if (ambiguous_decls)
17835 if (cp_parser_parsing_tentatively (parser))
17836 cp_parser_simulate_error (parser);
17837 return error_mark_node;
17843 /* Try a template-id. */
17844 decl = cp_parser_template_id (parser, template_keyword_p,
17845 check_dependency_p,
17847 if (decl == error_mark_node)
17848 return error_mark_node;
17851 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17853 /* If this is a typename, create a TYPENAME_TYPE. */
17854 if (typename_p && decl != error_mark_node)
17856 decl = make_typename_type (scope, decl, typename_type,
17857 /*complain=*/tf_error);
17858 if (decl != error_mark_node)
17859 decl = TYPE_NAME (decl);
17862 /* Check to see that it is really the name of a class. */
17863 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17864 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17865 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17866 /* Situations like this:
17868 template <typename T> struct A {
17869 typename T::template X<int>::I i;
17872 are problematic. Is `T::template X<int>' a class-name? The
17873 standard does not seem to be definitive, but there is no other
17874 valid interpretation of the following `::'. Therefore, those
17875 names are considered class-names. */
17877 decl = make_typename_type (scope, decl, tag_type, tf_error);
17878 if (decl != error_mark_node)
17879 decl = TYPE_NAME (decl);
17881 else if (TREE_CODE (decl) != TYPE_DECL
17882 || TREE_TYPE (decl) == error_mark_node
17883 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17884 /* In Objective-C 2.0, a classname followed by '.' starts a
17885 dot-syntax expression, and it's not a type-name. */
17886 || (c_dialect_objc ()
17887 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17888 && objc_is_class_name (decl)))
17889 decl = error_mark_node;
17891 if (decl == error_mark_node)
17892 cp_parser_error (parser, "expected class-name");
17893 else if (identifier && !parser->scope)
17894 maybe_note_name_used_in_class (identifier, decl);
17899 /* Parse a class-specifier.
17902 class-head { member-specification [opt] }
17904 Returns the TREE_TYPE representing the class. */
17907 cp_parser_class_specifier_1 (cp_parser* parser)
17910 tree attributes = NULL_TREE;
17911 bool nested_name_specifier_p;
17912 unsigned saved_num_template_parameter_lists;
17913 bool saved_in_function_body;
17914 unsigned char in_statement;
17915 bool in_switch_statement_p;
17916 bool saved_in_unbraced_linkage_specification_p;
17917 tree old_scope = NULL_TREE;
17918 tree scope = NULL_TREE;
17920 cp_token *closing_brace;
17922 push_deferring_access_checks (dk_no_deferred);
17924 /* Parse the class-head. */
17925 type = cp_parser_class_head (parser,
17926 &nested_name_specifier_p,
17929 /* If the class-head was a semantic disaster, skip the entire body
17933 cp_parser_skip_to_end_of_block_or_statement (parser);
17934 pop_deferring_access_checks ();
17935 return error_mark_node;
17938 /* Look for the `{'. */
17939 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17941 pop_deferring_access_checks ();
17942 return error_mark_node;
17945 /* Process the base classes. If they're invalid, skip the
17946 entire class body. */
17947 if (!xref_basetypes (type, bases))
17949 /* Consuming the closing brace yields better error messages
17951 if (cp_parser_skip_to_closing_brace (parser))
17952 cp_lexer_consume_token (parser->lexer);
17953 pop_deferring_access_checks ();
17954 return error_mark_node;
17957 /* Issue an error message if type-definitions are forbidden here. */
17958 cp_parser_check_type_definition (parser);
17959 /* Remember that we are defining one more class. */
17960 ++parser->num_classes_being_defined;
17961 /* Inside the class, surrounding template-parameter-lists do not
17963 saved_num_template_parameter_lists
17964 = parser->num_template_parameter_lists;
17965 parser->num_template_parameter_lists = 0;
17966 /* We are not in a function body. */
17967 saved_in_function_body = parser->in_function_body;
17968 parser->in_function_body = false;
17969 /* Or in a loop. */
17970 in_statement = parser->in_statement;
17971 parser->in_statement = 0;
17972 /* Or in a switch. */
17973 in_switch_statement_p = parser->in_switch_statement_p;
17974 parser->in_switch_statement_p = false;
17975 /* We are not immediately inside an extern "lang" block. */
17976 saved_in_unbraced_linkage_specification_p
17977 = parser->in_unbraced_linkage_specification_p;
17978 parser->in_unbraced_linkage_specification_p = false;
17980 /* Start the class. */
17981 if (nested_name_specifier_p)
17983 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17984 old_scope = push_inner_scope (scope);
17986 type = begin_class_definition (type, attributes);
17988 if (type == error_mark_node)
17989 /* If the type is erroneous, skip the entire body of the class. */
17990 cp_parser_skip_to_closing_brace (parser);
17992 /* Parse the member-specification. */
17993 cp_parser_member_specification_opt (parser);
17995 /* Look for the trailing `}'. */
17996 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17997 /* Look for trailing attributes to apply to this class. */
17998 if (cp_parser_allow_gnu_extensions_p (parser))
17999 attributes = cp_parser_attributes_opt (parser);
18000 if (type != error_mark_node)
18001 type = finish_struct (type, attributes);
18002 if (nested_name_specifier_p)
18003 pop_inner_scope (old_scope, scope);
18005 /* We've finished a type definition. Check for the common syntax
18006 error of forgetting a semicolon after the definition. We need to
18007 be careful, as we can't just check for not-a-semicolon and be done
18008 with it; the user might have typed:
18010 class X { } c = ...;
18011 class X { } *p = ...;
18013 and so forth. Instead, enumerate all the possible tokens that
18014 might follow this production; if we don't see one of them, then
18015 complain and silently insert the semicolon. */
18017 cp_token *token = cp_lexer_peek_token (parser->lexer);
18018 bool want_semicolon = true;
18020 switch (token->type)
18023 case CPP_SEMICOLON:
18026 case CPP_OPEN_PAREN:
18027 case CPP_CLOSE_PAREN:
18029 want_semicolon = false;
18032 /* While it's legal for type qualifiers and storage class
18033 specifiers to follow type definitions in the grammar, only
18034 compiler testsuites contain code like that. Assume that if
18035 we see such code, then what we're really seeing is a case
18039 const <type> var = ...;
18044 static <type> func (...) ...
18046 i.e. the qualifier or specifier applies to the next
18047 declaration. To do so, however, we need to look ahead one
18048 more token to see if *that* token is a type specifier.
18050 This code could be improved to handle:
18053 static const <type> var = ...; */
18055 if (keyword_is_decl_specifier (token->keyword))
18057 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18059 /* Handling user-defined types here would be nice, but very
18062 = (lookahead->type == CPP_KEYWORD
18063 && keyword_begins_type_specifier (lookahead->keyword));
18070 /* If we don't have a type, then something is very wrong and we
18071 shouldn't try to do anything clever. Likewise for not seeing the
18073 if (closing_brace && TYPE_P (type) && want_semicolon)
18075 cp_token_position prev
18076 = cp_lexer_previous_token_position (parser->lexer);
18077 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18078 location_t loc = prev_token->location;
18080 if (CLASSTYPE_DECLARED_CLASS (type))
18081 error_at (loc, "expected %<;%> after class definition");
18082 else if (TREE_CODE (type) == RECORD_TYPE)
18083 error_at (loc, "expected %<;%> after struct definition");
18084 else if (TREE_CODE (type) == UNION_TYPE)
18085 error_at (loc, "expected %<;%> after union definition");
18087 gcc_unreachable ();
18089 /* Unget one token and smash it to look as though we encountered
18090 a semicolon in the input stream. */
18091 cp_lexer_set_token_position (parser->lexer, prev);
18092 token = cp_lexer_peek_token (parser->lexer);
18093 token->type = CPP_SEMICOLON;
18094 token->keyword = RID_MAX;
18098 /* If this class is not itself within the scope of another class,
18099 then we need to parse the bodies of all of the queued function
18100 definitions. Note that the queued functions defined in a class
18101 are not always processed immediately following the
18102 class-specifier for that class. Consider:
18105 struct B { void f() { sizeof (A); } };
18108 If `f' were processed before the processing of `A' were
18109 completed, there would be no way to compute the size of `A'.
18110 Note that the nesting we are interested in here is lexical --
18111 not the semantic nesting given by TYPE_CONTEXT. In particular,
18114 struct A { struct B; };
18115 struct A::B { void f() { } };
18117 there is no need to delay the parsing of `A::B::f'. */
18118 if (--parser->num_classes_being_defined == 0)
18121 tree class_type = NULL_TREE;
18122 tree pushed_scope = NULL_TREE;
18124 cp_default_arg_entry *e;
18125 tree save_ccp, save_ccr;
18127 /* In a first pass, parse default arguments to the functions.
18128 Then, in a second pass, parse the bodies of the functions.
18129 This two-phased approach handles cases like:
18137 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18141 /* If there are default arguments that have not yet been processed,
18142 take care of them now. */
18143 if (class_type != e->class_type)
18146 pop_scope (pushed_scope);
18147 class_type = e->class_type;
18148 pushed_scope = push_scope (class_type);
18150 /* Make sure that any template parameters are in scope. */
18151 maybe_begin_member_template_processing (decl);
18152 /* Parse the default argument expressions. */
18153 cp_parser_late_parsing_default_args (parser, decl);
18154 /* Remove any template parameters from the symbol table. */
18155 maybe_end_member_template_processing ();
18157 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18158 /* Now parse any NSDMIs. */
18159 save_ccp = current_class_ptr;
18160 save_ccr = current_class_ref;
18161 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18163 if (class_type != DECL_CONTEXT (decl))
18166 pop_scope (pushed_scope);
18167 class_type = DECL_CONTEXT (decl);
18168 pushed_scope = push_scope (class_type);
18170 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18171 cp_parser_late_parsing_nsdmi (parser, decl);
18173 VEC_truncate (tree, unparsed_nsdmis, 0);
18174 current_class_ptr = save_ccp;
18175 current_class_ref = save_ccr;
18177 pop_scope (pushed_scope);
18178 /* Now parse the body of the functions. */
18179 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18180 cp_parser_late_parsing_for_member (parser, decl);
18181 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18184 /* Put back any saved access checks. */
18185 pop_deferring_access_checks ();
18187 /* Restore saved state. */
18188 parser->in_switch_statement_p = in_switch_statement_p;
18189 parser->in_statement = in_statement;
18190 parser->in_function_body = saved_in_function_body;
18191 parser->num_template_parameter_lists
18192 = saved_num_template_parameter_lists;
18193 parser->in_unbraced_linkage_specification_p
18194 = saved_in_unbraced_linkage_specification_p;
18200 cp_parser_class_specifier (cp_parser* parser)
18203 timevar_push (TV_PARSE_STRUCT);
18204 ret = cp_parser_class_specifier_1 (parser);
18205 timevar_pop (TV_PARSE_STRUCT);
18209 /* Parse a class-head.
18212 class-key identifier [opt] base-clause [opt]
18213 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18214 class-key nested-name-specifier [opt] template-id
18217 class-virt-specifier:
18221 class-key attributes identifier [opt] base-clause [opt]
18222 class-key attributes nested-name-specifier identifier base-clause [opt]
18223 class-key attributes nested-name-specifier [opt] template-id
18226 Upon return BASES is initialized to the list of base classes (or
18227 NULL, if there are none) in the same form returned by
18228 cp_parser_base_clause.
18230 Returns the TYPE of the indicated class. Sets
18231 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18232 involving a nested-name-specifier was used, and FALSE otherwise.
18234 Returns error_mark_node if this is not a class-head.
18236 Returns NULL_TREE if the class-head is syntactically valid, but
18237 semantically invalid in a way that means we should skip the entire
18238 body of the class. */
18241 cp_parser_class_head (cp_parser* parser,
18242 bool* nested_name_specifier_p,
18243 tree *attributes_p,
18246 tree nested_name_specifier;
18247 enum tag_types class_key;
18248 tree id = NULL_TREE;
18249 tree type = NULL_TREE;
18251 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18252 bool template_id_p = false;
18253 bool qualified_p = false;
18254 bool invalid_nested_name_p = false;
18255 bool invalid_explicit_specialization_p = false;
18256 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18257 tree pushed_scope = NULL_TREE;
18258 unsigned num_templates;
18259 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18260 /* Assume no nested-name-specifier will be present. */
18261 *nested_name_specifier_p = false;
18262 /* Assume no template parameter lists will be used in defining the
18265 parser->colon_corrects_to_scope_p = false;
18267 *bases = NULL_TREE;
18269 /* Look for the class-key. */
18270 class_key = cp_parser_class_key (parser);
18271 if (class_key == none_type)
18272 return error_mark_node;
18274 /* Parse the attributes. */
18275 attributes = cp_parser_attributes_opt (parser);
18277 /* If the next token is `::', that is invalid -- but sometimes
18278 people do try to write:
18282 Handle this gracefully by accepting the extra qualifier, and then
18283 issuing an error about it later if this really is a
18284 class-head. If it turns out just to be an elaborated type
18285 specifier, remain silent. */
18286 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18287 qualified_p = true;
18289 push_deferring_access_checks (dk_no_check);
18291 /* Determine the name of the class. Begin by looking for an
18292 optional nested-name-specifier. */
18293 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18294 nested_name_specifier
18295 = cp_parser_nested_name_specifier_opt (parser,
18296 /*typename_keyword_p=*/false,
18297 /*check_dependency_p=*/false,
18299 /*is_declaration=*/false);
18300 /* If there was a nested-name-specifier, then there *must* be an
18302 if (nested_name_specifier)
18304 type_start_token = cp_lexer_peek_token (parser->lexer);
18305 /* Although the grammar says `identifier', it really means
18306 `class-name' or `template-name'. You are only allowed to
18307 define a class that has already been declared with this
18310 The proposed resolution for Core Issue 180 says that wherever
18311 you see `class T::X' you should treat `X' as a type-name.
18313 It is OK to define an inaccessible class; for example:
18315 class A { class B; };
18318 We do not know if we will see a class-name, or a
18319 template-name. We look for a class-name first, in case the
18320 class-name is a template-id; if we looked for the
18321 template-name first we would stop after the template-name. */
18322 cp_parser_parse_tentatively (parser);
18323 type = cp_parser_class_name (parser,
18324 /*typename_keyword_p=*/false,
18325 /*template_keyword_p=*/false,
18327 /*check_dependency_p=*/false,
18328 /*class_head_p=*/true,
18329 /*is_declaration=*/false);
18330 /* If that didn't work, ignore the nested-name-specifier. */
18331 if (!cp_parser_parse_definitely (parser))
18333 invalid_nested_name_p = true;
18334 type_start_token = cp_lexer_peek_token (parser->lexer);
18335 id = cp_parser_identifier (parser);
18336 if (id == error_mark_node)
18339 /* If we could not find a corresponding TYPE, treat this
18340 declaration like an unqualified declaration. */
18341 if (type == error_mark_node)
18342 nested_name_specifier = NULL_TREE;
18343 /* Otherwise, count the number of templates used in TYPE and its
18344 containing scopes. */
18349 for (scope = TREE_TYPE (type);
18350 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18351 scope = (TYPE_P (scope)
18352 ? TYPE_CONTEXT (scope)
18353 : DECL_CONTEXT (scope)))
18355 && CLASS_TYPE_P (scope)
18356 && CLASSTYPE_TEMPLATE_INFO (scope)
18357 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18358 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18362 /* Otherwise, the identifier is optional. */
18365 /* We don't know whether what comes next is a template-id,
18366 an identifier, or nothing at all. */
18367 cp_parser_parse_tentatively (parser);
18368 /* Check for a template-id. */
18369 type_start_token = cp_lexer_peek_token (parser->lexer);
18370 id = cp_parser_template_id (parser,
18371 /*template_keyword_p=*/false,
18372 /*check_dependency_p=*/true,
18373 /*is_declaration=*/true);
18374 /* If that didn't work, it could still be an identifier. */
18375 if (!cp_parser_parse_definitely (parser))
18377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18379 type_start_token = cp_lexer_peek_token (parser->lexer);
18380 id = cp_parser_identifier (parser);
18387 template_id_p = true;
18392 pop_deferring_access_checks ();
18396 cp_parser_check_for_invalid_template_id (parser, id,
18397 type_start_token->location);
18399 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18401 /* If it's not a `:' or a `{' then we can't really be looking at a
18402 class-head, since a class-head only appears as part of a
18403 class-specifier. We have to detect this situation before calling
18404 xref_tag, since that has irreversible side-effects. */
18405 if (!cp_parser_next_token_starts_class_definition_p (parser))
18407 cp_parser_error (parser, "expected %<{%> or %<:%>");
18408 type = error_mark_node;
18412 /* At this point, we're going ahead with the class-specifier, even
18413 if some other problem occurs. */
18414 cp_parser_commit_to_tentative_parse (parser);
18415 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18417 cp_parser_error (parser,
18418 "cannot specify %<override%> for a class");
18419 type = error_mark_node;
18422 /* Issue the error about the overly-qualified name now. */
18425 cp_parser_error (parser,
18426 "global qualification of class name is invalid");
18427 type = error_mark_node;
18430 else if (invalid_nested_name_p)
18432 cp_parser_error (parser,
18433 "qualified name does not name a class");
18434 type = error_mark_node;
18437 else if (nested_name_specifier)
18441 /* Reject typedef-names in class heads. */
18442 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18444 error_at (type_start_token->location,
18445 "invalid class name in declaration of %qD",
18451 /* Figure out in what scope the declaration is being placed. */
18452 scope = current_scope ();
18453 /* If that scope does not contain the scope in which the
18454 class was originally declared, the program is invalid. */
18455 if (scope && !is_ancestor (scope, nested_name_specifier))
18457 if (at_namespace_scope_p ())
18458 error_at (type_start_token->location,
18459 "declaration of %qD in namespace %qD which does not "
18461 type, scope, nested_name_specifier);
18463 error_at (type_start_token->location,
18464 "declaration of %qD in %qD which does not enclose %qD",
18465 type, scope, nested_name_specifier);
18471 A declarator-id shall not be qualified except for the
18472 definition of a ... nested class outside of its class
18473 ... [or] the definition or explicit instantiation of a
18474 class member of a namespace outside of its namespace. */
18475 if (scope == nested_name_specifier)
18477 permerror (nested_name_specifier_token_start->location,
18478 "extra qualification not allowed");
18479 nested_name_specifier = NULL_TREE;
18483 /* An explicit-specialization must be preceded by "template <>". If
18484 it is not, try to recover gracefully. */
18485 if (at_namespace_scope_p ()
18486 && parser->num_template_parameter_lists == 0
18489 error_at (type_start_token->location,
18490 "an explicit specialization must be preceded by %<template <>%>");
18491 invalid_explicit_specialization_p = true;
18492 /* Take the same action that would have been taken by
18493 cp_parser_explicit_specialization. */
18494 ++parser->num_template_parameter_lists;
18495 begin_specialization ();
18497 /* There must be no "return" statements between this point and the
18498 end of this function; set "type "to the correct return value and
18499 use "goto done;" to return. */
18500 /* Make sure that the right number of template parameters were
18502 if (!cp_parser_check_template_parameters (parser, num_templates,
18503 type_start_token->location,
18504 /*declarator=*/NULL))
18506 /* If something went wrong, there is no point in even trying to
18507 process the class-definition. */
18512 /* Look up the type. */
18515 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18516 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18517 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18519 error_at (type_start_token->location,
18520 "function template %qD redeclared as a class template", id);
18521 type = error_mark_node;
18525 type = TREE_TYPE (id);
18526 type = maybe_process_partial_specialization (type);
18528 if (nested_name_specifier)
18529 pushed_scope = push_scope (nested_name_specifier);
18531 else if (nested_name_specifier)
18537 template <typename T> struct S { struct T };
18538 template <typename T> struct S<T>::T { };
18540 we will get a TYPENAME_TYPE when processing the definition of
18541 `S::T'. We need to resolve it to the actual type before we
18542 try to define it. */
18543 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18545 class_type = resolve_typename_type (TREE_TYPE (type),
18546 /*only_current_p=*/false);
18547 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18548 type = TYPE_NAME (class_type);
18551 cp_parser_error (parser, "could not resolve typename type");
18552 type = error_mark_node;
18556 if (maybe_process_partial_specialization (TREE_TYPE (type))
18557 == error_mark_node)
18563 class_type = current_class_type;
18564 /* Enter the scope indicated by the nested-name-specifier. */
18565 pushed_scope = push_scope (nested_name_specifier);
18566 /* Get the canonical version of this type. */
18567 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18568 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18569 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18571 type = push_template_decl (type);
18572 if (type == error_mark_node)
18579 type = TREE_TYPE (type);
18580 *nested_name_specifier_p = true;
18582 else /* The name is not a nested name. */
18584 /* If the class was unnamed, create a dummy name. */
18586 id = make_anon_name ();
18587 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18588 parser->num_template_parameter_lists);
18591 /* Indicate whether this class was declared as a `class' or as a
18593 if (TREE_CODE (type) == RECORD_TYPE)
18594 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18595 cp_parser_check_class_key (class_key, type);
18597 /* If this type was already complete, and we see another definition,
18598 that's an error. */
18599 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18601 error_at (type_start_token->location, "redefinition of %q#T",
18603 error_at (type_start_token->location, "previous definition of %q+#T",
18608 else if (type == error_mark_node)
18611 /* We will have entered the scope containing the class; the names of
18612 base classes should be looked up in that context. For example:
18614 struct A { struct B {}; struct C; };
18615 struct A::C : B {};
18619 /* Get the list of base-classes, if there is one. */
18620 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18621 *bases = cp_parser_base_clause (parser);
18624 /* Leave the scope given by the nested-name-specifier. We will
18625 enter the class scope itself while processing the members. */
18627 pop_scope (pushed_scope);
18629 if (invalid_explicit_specialization_p)
18631 end_specialization ();
18632 --parser->num_template_parameter_lists;
18636 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18637 *attributes_p = attributes;
18638 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18639 CLASSTYPE_FINAL (type) = 1;
18641 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18645 /* Parse a class-key.
18652 Returns the kind of class-key specified, or none_type to indicate
18655 static enum tag_types
18656 cp_parser_class_key (cp_parser* parser)
18659 enum tag_types tag_type;
18661 /* Look for the class-key. */
18662 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18666 /* Check to see if the TOKEN is a class-key. */
18667 tag_type = cp_parser_token_is_class_key (token);
18669 cp_parser_error (parser, "expected class-key");
18673 /* Parse an (optional) member-specification.
18675 member-specification:
18676 member-declaration member-specification [opt]
18677 access-specifier : member-specification [opt] */
18680 cp_parser_member_specification_opt (cp_parser* parser)
18687 /* Peek at the next token. */
18688 token = cp_lexer_peek_token (parser->lexer);
18689 /* If it's a `}', or EOF then we've seen all the members. */
18690 if (token->type == CPP_CLOSE_BRACE
18691 || token->type == CPP_EOF
18692 || token->type == CPP_PRAGMA_EOL)
18695 /* See if this token is a keyword. */
18696 keyword = token->keyword;
18700 case RID_PROTECTED:
18702 /* Consume the access-specifier. */
18703 cp_lexer_consume_token (parser->lexer);
18704 /* Remember which access-specifier is active. */
18705 current_access_specifier = token->u.value;
18706 /* Look for the `:'. */
18707 cp_parser_require (parser, CPP_COLON, RT_COLON);
18711 /* Accept #pragmas at class scope. */
18712 if (token->type == CPP_PRAGMA)
18714 cp_parser_pragma (parser, pragma_external);
18718 /* Otherwise, the next construction must be a
18719 member-declaration. */
18720 cp_parser_member_declaration (parser);
18725 /* Parse a member-declaration.
18727 member-declaration:
18728 decl-specifier-seq [opt] member-declarator-list [opt] ;
18729 function-definition ; [opt]
18730 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18732 template-declaration
18735 member-declarator-list:
18737 member-declarator-list , member-declarator
18740 declarator pure-specifier [opt]
18741 declarator constant-initializer [opt]
18742 identifier [opt] : constant-expression
18746 member-declaration:
18747 __extension__ member-declaration
18750 declarator attributes [opt] pure-specifier [opt]
18751 declarator attributes [opt] constant-initializer [opt]
18752 identifier [opt] attributes [opt] : constant-expression
18756 member-declaration:
18757 static_assert-declaration */
18760 cp_parser_member_declaration (cp_parser* parser)
18762 cp_decl_specifier_seq decl_specifiers;
18763 tree prefix_attributes;
18765 int declares_class_or_enum;
18767 cp_token *token = NULL;
18768 cp_token *decl_spec_token_start = NULL;
18769 cp_token *initializer_token_start = NULL;
18770 int saved_pedantic;
18771 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18773 /* Check for the `__extension__' keyword. */
18774 if (cp_parser_extension_opt (parser, &saved_pedantic))
18777 cp_parser_member_declaration (parser);
18778 /* Restore the old value of the PEDANTIC flag. */
18779 pedantic = saved_pedantic;
18784 /* Check for a template-declaration. */
18785 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18787 /* An explicit specialization here is an error condition, and we
18788 expect the specialization handler to detect and report this. */
18789 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18790 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18791 cp_parser_explicit_specialization (parser);
18793 cp_parser_template_declaration (parser, /*member_p=*/true);
18798 /* Check for a using-declaration. */
18799 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18801 if (cxx_dialect < cxx0x)
18803 /* Parse the using-declaration. */
18804 cp_parser_using_declaration (parser,
18805 /*access_declaration_p=*/false);
18811 cp_parser_parse_tentatively (parser);
18812 decl = cp_parser_alias_declaration (parser);
18813 if (cp_parser_parse_definitely (parser))
18814 finish_member_declaration (decl);
18816 cp_parser_using_declaration (parser,
18817 /*access_declaration_p=*/false);
18822 /* Check for @defs. */
18823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18826 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18827 ivar = ivar_chains;
18831 ivar = TREE_CHAIN (member);
18832 TREE_CHAIN (member) = NULL_TREE;
18833 finish_member_declaration (member);
18838 /* If the next token is `static_assert' we have a static assertion. */
18839 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18841 cp_parser_static_assert (parser, /*member_p=*/true);
18845 parser->colon_corrects_to_scope_p = false;
18847 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18850 /* Parse the decl-specifier-seq. */
18851 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18852 cp_parser_decl_specifier_seq (parser,
18853 CP_PARSER_FLAGS_OPTIONAL,
18855 &declares_class_or_enum);
18856 prefix_attributes = decl_specifiers.attributes;
18857 decl_specifiers.attributes = NULL_TREE;
18858 /* Check for an invalid type-name. */
18859 if (!decl_specifiers.any_type_specifiers_p
18860 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18862 /* If there is no declarator, then the decl-specifier-seq should
18864 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18866 /* If there was no decl-specifier-seq, and the next token is a
18867 `;', then we have something like:
18873 Each member-declaration shall declare at least one member
18874 name of the class. */
18875 if (!decl_specifiers.any_specifiers_p)
18877 cp_token *token = cp_lexer_peek_token (parser->lexer);
18878 if (!in_system_header_at (token->location))
18879 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18885 /* See if this declaration is a friend. */
18886 friend_p = cp_parser_friend_p (&decl_specifiers);
18887 /* If there were decl-specifiers, check to see if there was
18888 a class-declaration. */
18889 type = check_tag_decl (&decl_specifiers);
18890 /* Nested classes have already been added to the class, but
18891 a `friend' needs to be explicitly registered. */
18894 /* If the `friend' keyword was present, the friend must
18895 be introduced with a class-key. */
18896 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18897 pedwarn (decl_spec_token_start->location, OPT_pedantic,
18898 "in C++03 a class-key must be used "
18899 "when declaring a friend");
18902 template <typename T> struct A {
18903 friend struct A<T>::B;
18906 A<T>::B will be represented by a TYPENAME_TYPE, and
18907 therefore not recognized by check_tag_decl. */
18910 type = decl_specifiers.type;
18911 if (type && TREE_CODE (type) == TYPE_DECL)
18912 type = TREE_TYPE (type);
18914 if (!type || !TYPE_P (type))
18915 error_at (decl_spec_token_start->location,
18916 "friend declaration does not name a class or "
18919 make_friend_class (current_class_type, type,
18920 /*complain=*/true);
18922 /* If there is no TYPE, an error message will already have
18924 else if (!type || type == error_mark_node)
18926 /* An anonymous aggregate has to be handled specially; such
18927 a declaration really declares a data member (with a
18928 particular type), as opposed to a nested class. */
18929 else if (ANON_AGGR_TYPE_P (type))
18931 /* Remove constructors and such from TYPE, now that we
18932 know it is an anonymous aggregate. */
18933 fixup_anonymous_aggr (type);
18934 /* And make the corresponding data member. */
18935 decl = build_decl (decl_spec_token_start->location,
18936 FIELD_DECL, NULL_TREE, type);
18937 /* Add it to the class. */
18938 finish_member_declaration (decl);
18941 cp_parser_check_access_in_redeclaration
18943 decl_spec_token_start->location);
18948 bool assume_semicolon = false;
18950 /* See if these declarations will be friends. */
18951 friend_p = cp_parser_friend_p (&decl_specifiers);
18953 /* Keep going until we hit the `;' at the end of the
18955 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18957 tree attributes = NULL_TREE;
18958 tree first_attribute;
18960 /* Peek at the next token. */
18961 token = cp_lexer_peek_token (parser->lexer);
18963 /* Check for a bitfield declaration. */
18964 if (token->type == CPP_COLON
18965 || (token->type == CPP_NAME
18966 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18972 /* Get the name of the bitfield. Note that we cannot just
18973 check TOKEN here because it may have been invalidated by
18974 the call to cp_lexer_peek_nth_token above. */
18975 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18976 identifier = cp_parser_identifier (parser);
18978 identifier = NULL_TREE;
18980 /* Consume the `:' token. */
18981 cp_lexer_consume_token (parser->lexer);
18982 /* Get the width of the bitfield. */
18984 = cp_parser_constant_expression (parser,
18985 /*allow_non_constant=*/false,
18988 /* Look for attributes that apply to the bitfield. */
18989 attributes = cp_parser_attributes_opt (parser);
18990 /* Remember which attributes are prefix attributes and
18992 first_attribute = attributes;
18993 /* Combine the attributes. */
18994 attributes = chainon (prefix_attributes, attributes);
18996 /* Create the bitfield declaration. */
18997 decl = grokbitfield (identifier
18998 ? make_id_declarator (NULL_TREE,
19008 cp_declarator *declarator;
19010 tree asm_specification;
19011 int ctor_dtor_or_conv_p;
19013 /* Parse the declarator. */
19015 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19016 &ctor_dtor_or_conv_p,
19017 /*parenthesized_p=*/NULL,
19018 /*member_p=*/true);
19020 /* If something went wrong parsing the declarator, make sure
19021 that we at least consume some tokens. */
19022 if (declarator == cp_error_declarator)
19024 /* Skip to the end of the statement. */
19025 cp_parser_skip_to_end_of_statement (parser);
19026 /* If the next token is not a semicolon, that is
19027 probably because we just skipped over the body of
19028 a function. So, we consume a semicolon if
19029 present, but do not issue an error message if it
19031 if (cp_lexer_next_token_is (parser->lexer,
19033 cp_lexer_consume_token (parser->lexer);
19037 if (declares_class_or_enum & 2)
19038 cp_parser_check_for_definition_in_return_type
19039 (declarator, decl_specifiers.type,
19040 decl_specifiers.type_location);
19042 /* Look for an asm-specification. */
19043 asm_specification = cp_parser_asm_specification_opt (parser);
19044 /* Look for attributes that apply to the declaration. */
19045 attributes = cp_parser_attributes_opt (parser);
19046 /* Remember which attributes are prefix attributes and
19048 first_attribute = attributes;
19049 /* Combine the attributes. */
19050 attributes = chainon (prefix_attributes, attributes);
19052 /* If it's an `=', then we have a constant-initializer or a
19053 pure-specifier. It is not correct to parse the
19054 initializer before registering the member declaration
19055 since the member declaration should be in scope while
19056 its initializer is processed. However, the rest of the
19057 front end does not yet provide an interface that allows
19058 us to handle this correctly. */
19059 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19063 A pure-specifier shall be used only in the declaration of
19064 a virtual function.
19066 A member-declarator can contain a constant-initializer
19067 only if it declares a static member of integral or
19070 Therefore, if the DECLARATOR is for a function, we look
19071 for a pure-specifier; otherwise, we look for a
19072 constant-initializer. When we call `grokfield', it will
19073 perform more stringent semantics checks. */
19074 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19075 if (function_declarator_p (declarator)
19076 || (decl_specifiers.type
19077 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19078 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19079 == FUNCTION_TYPE)))
19080 initializer = cp_parser_pure_specifier (parser);
19081 else if (decl_specifiers.storage_class != sc_static)
19082 initializer = cp_parser_save_nsdmi (parser);
19083 else if (cxx_dialect >= cxx0x)
19086 /* Don't require a constant rvalue in C++11, since we
19087 might want a reference constant. We'll enforce
19088 constancy later. */
19089 cp_lexer_consume_token (parser->lexer);
19090 /* Parse the initializer. */
19091 initializer = cp_parser_initializer_clause (parser,
19095 /* Parse the initializer. */
19096 initializer = cp_parser_constant_initializer (parser);
19098 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19099 && !function_declarator_p (declarator))
19102 if (decl_specifiers.storage_class != sc_static)
19103 initializer = cp_parser_save_nsdmi (parser);
19105 initializer = cp_parser_initializer (parser, &x, &x);
19107 /* Otherwise, there is no initializer. */
19109 initializer = NULL_TREE;
19111 /* See if we are probably looking at a function
19112 definition. We are certainly not looking at a
19113 member-declarator. Calling `grokfield' has
19114 side-effects, so we must not do it unless we are sure
19115 that we are looking at a member-declarator. */
19116 if (cp_parser_token_starts_function_definition_p
19117 (cp_lexer_peek_token (parser->lexer)))
19119 /* The grammar does not allow a pure-specifier to be
19120 used when a member function is defined. (It is
19121 possible that this fact is an oversight in the
19122 standard, since a pure function may be defined
19123 outside of the class-specifier. */
19125 error_at (initializer_token_start->location,
19126 "pure-specifier on function-definition");
19127 decl = cp_parser_save_member_function_body (parser,
19131 /* If the member was not a friend, declare it here. */
19133 finish_member_declaration (decl);
19134 /* Peek at the next token. */
19135 token = cp_lexer_peek_token (parser->lexer);
19136 /* If the next token is a semicolon, consume it. */
19137 if (token->type == CPP_SEMICOLON)
19138 cp_lexer_consume_token (parser->lexer);
19142 if (declarator->kind == cdk_function)
19143 declarator->id_loc = token->location;
19144 /* Create the declaration. */
19145 decl = grokfield (declarator, &decl_specifiers,
19146 initializer, /*init_const_expr_p=*/true,
19151 /* Reset PREFIX_ATTRIBUTES. */
19152 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19153 attributes = TREE_CHAIN (attributes);
19155 TREE_CHAIN (attributes) = NULL_TREE;
19157 /* If there is any qualification still in effect, clear it
19158 now; we will be starting fresh with the next declarator. */
19159 parser->scope = NULL_TREE;
19160 parser->qualifying_scope = NULL_TREE;
19161 parser->object_scope = NULL_TREE;
19162 /* If it's a `,', then there are more declarators. */
19163 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19164 cp_lexer_consume_token (parser->lexer);
19165 /* If the next token isn't a `;', then we have a parse error. */
19166 else if (cp_lexer_next_token_is_not (parser->lexer,
19169 /* The next token might be a ways away from where the
19170 actual semicolon is missing. Find the previous token
19171 and use that for our error position. */
19172 cp_token *token = cp_lexer_previous_token (parser->lexer);
19173 error_at (token->location,
19174 "expected %<;%> at end of member declaration");
19176 /* Assume that the user meant to provide a semicolon. If
19177 we were to cp_parser_skip_to_end_of_statement, we might
19178 skip to a semicolon inside a member function definition
19179 and issue nonsensical error messages. */
19180 assume_semicolon = true;
19185 /* Add DECL to the list of members. */
19187 finish_member_declaration (decl);
19189 if (TREE_CODE (decl) == FUNCTION_DECL)
19190 cp_parser_save_default_args (parser, decl);
19191 else if (TREE_CODE (decl) == FIELD_DECL
19192 && !DECL_C_BIT_FIELD (decl)
19193 && DECL_INITIAL (decl))
19194 /* Add DECL to the queue of NSDMI to be parsed later. */
19195 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19198 if (assume_semicolon)
19203 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19205 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19208 /* Parse a pure-specifier.
19213 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19214 Otherwise, ERROR_MARK_NODE is returned. */
19217 cp_parser_pure_specifier (cp_parser* parser)
19221 /* Look for the `=' token. */
19222 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19223 return error_mark_node;
19224 /* Look for the `0' token. */
19225 token = cp_lexer_peek_token (parser->lexer);
19227 if (token->type == CPP_EOF
19228 || token->type == CPP_PRAGMA_EOL)
19229 return error_mark_node;
19231 cp_lexer_consume_token (parser->lexer);
19233 /* Accept = default or = delete in c++0x mode. */
19234 if (token->keyword == RID_DEFAULT
19235 || token->keyword == RID_DELETE)
19237 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19238 return token->u.value;
19241 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19242 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19244 cp_parser_error (parser,
19245 "invalid pure specifier (only %<= 0%> is allowed)");
19246 cp_parser_skip_to_end_of_statement (parser);
19247 return error_mark_node;
19249 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19251 error_at (token->location, "templates may not be %<virtual%>");
19252 return error_mark_node;
19255 return integer_zero_node;
19258 /* Parse a constant-initializer.
19260 constant-initializer:
19261 = constant-expression
19263 Returns a representation of the constant-expression. */
19266 cp_parser_constant_initializer (cp_parser* parser)
19268 /* Look for the `=' token. */
19269 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19270 return error_mark_node;
19272 /* It is invalid to write:
19274 struct S { static const int i = { 7 }; };
19277 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19279 cp_parser_error (parser,
19280 "a brace-enclosed initializer is not allowed here");
19281 /* Consume the opening brace. */
19282 cp_lexer_consume_token (parser->lexer);
19283 /* Skip the initializer. */
19284 cp_parser_skip_to_closing_brace (parser);
19285 /* Look for the trailing `}'. */
19286 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19288 return error_mark_node;
19291 return cp_parser_constant_expression (parser,
19292 /*allow_non_constant=*/false,
19296 /* Derived classes [gram.class.derived] */
19298 /* Parse a base-clause.
19301 : base-specifier-list
19303 base-specifier-list:
19304 base-specifier ... [opt]
19305 base-specifier-list , base-specifier ... [opt]
19307 Returns a TREE_LIST representing the base-classes, in the order in
19308 which they were declared. The representation of each node is as
19309 described by cp_parser_base_specifier.
19311 In the case that no bases are specified, this function will return
19312 NULL_TREE, not ERROR_MARK_NODE. */
19315 cp_parser_base_clause (cp_parser* parser)
19317 tree bases = NULL_TREE;
19319 /* Look for the `:' that begins the list. */
19320 cp_parser_require (parser, CPP_COLON, RT_COLON);
19322 /* Scan the base-specifier-list. */
19327 bool pack_expansion_p = false;
19329 /* Look for the base-specifier. */
19330 base = cp_parser_base_specifier (parser);
19331 /* Look for the (optional) ellipsis. */
19332 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19334 /* Consume the `...'. */
19335 cp_lexer_consume_token (parser->lexer);
19337 pack_expansion_p = true;
19340 /* Add BASE to the front of the list. */
19341 if (base && base != error_mark_node)
19343 if (pack_expansion_p)
19344 /* Make this a pack expansion type. */
19345 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19347 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19349 TREE_CHAIN (base) = bases;
19353 /* Peek at the next token. */
19354 token = cp_lexer_peek_token (parser->lexer);
19355 /* If it's not a comma, then the list is complete. */
19356 if (token->type != CPP_COMMA)
19358 /* Consume the `,'. */
19359 cp_lexer_consume_token (parser->lexer);
19362 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19363 base class had a qualified name. However, the next name that
19364 appears is certainly not qualified. */
19365 parser->scope = NULL_TREE;
19366 parser->qualifying_scope = NULL_TREE;
19367 parser->object_scope = NULL_TREE;
19369 return nreverse (bases);
19372 /* Parse a base-specifier.
19375 :: [opt] nested-name-specifier [opt] class-name
19376 virtual access-specifier [opt] :: [opt] nested-name-specifier
19378 access-specifier virtual [opt] :: [opt] nested-name-specifier
19381 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19382 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19383 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19384 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19387 cp_parser_base_specifier (cp_parser* parser)
19391 bool virtual_p = false;
19392 bool duplicate_virtual_error_issued_p = false;
19393 bool duplicate_access_error_issued_p = false;
19394 bool class_scope_p, template_p;
19395 tree access = access_default_node;
19398 /* Process the optional `virtual' and `access-specifier'. */
19401 /* Peek at the next token. */
19402 token = cp_lexer_peek_token (parser->lexer);
19403 /* Process `virtual'. */
19404 switch (token->keyword)
19407 /* If `virtual' appears more than once, issue an error. */
19408 if (virtual_p && !duplicate_virtual_error_issued_p)
19410 cp_parser_error (parser,
19411 "%<virtual%> specified more than once in base-specified");
19412 duplicate_virtual_error_issued_p = true;
19417 /* Consume the `virtual' token. */
19418 cp_lexer_consume_token (parser->lexer);
19423 case RID_PROTECTED:
19425 /* If more than one access specifier appears, issue an
19427 if (access != access_default_node
19428 && !duplicate_access_error_issued_p)
19430 cp_parser_error (parser,
19431 "more than one access specifier in base-specified");
19432 duplicate_access_error_issued_p = true;
19435 access = ridpointers[(int) token->keyword];
19437 /* Consume the access-specifier. */
19438 cp_lexer_consume_token (parser->lexer);
19447 /* It is not uncommon to see programs mechanically, erroneously, use
19448 the 'typename' keyword to denote (dependent) qualified types
19449 as base classes. */
19450 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19452 token = cp_lexer_peek_token (parser->lexer);
19453 if (!processing_template_decl)
19454 error_at (token->location,
19455 "keyword %<typename%> not allowed outside of templates");
19457 error_at (token->location,
19458 "keyword %<typename%> not allowed in this context "
19459 "(the base class is implicitly a type)");
19460 cp_lexer_consume_token (parser->lexer);
19463 /* Look for the optional `::' operator. */
19464 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19465 /* Look for the nested-name-specifier. The simplest way to
19470 The keyword `typename' is not permitted in a base-specifier or
19471 mem-initializer; in these contexts a qualified name that
19472 depends on a template-parameter is implicitly assumed to be a
19475 is to pretend that we have seen the `typename' keyword at this
19477 cp_parser_nested_name_specifier_opt (parser,
19478 /*typename_keyword_p=*/true,
19479 /*check_dependency_p=*/true,
19481 /*is_declaration=*/true);
19482 /* If the base class is given by a qualified name, assume that names
19483 we see are type names or templates, as appropriate. */
19484 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19485 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19488 && cp_lexer_next_token_is_decltype (parser->lexer))
19489 /* DR 950 allows decltype as a base-specifier. */
19490 type = cp_parser_decltype (parser);
19493 /* Otherwise, look for the class-name. */
19494 type = cp_parser_class_name (parser,
19498 /*check_dependency_p=*/true,
19499 /*class_head_p=*/false,
19500 /*is_declaration=*/true);
19501 type = TREE_TYPE (type);
19504 if (type == error_mark_node)
19505 return error_mark_node;
19507 return finish_base_specifier (type, access, virtual_p);
19510 /* Exception handling [gram.exception] */
19512 /* Parse an (optional) exception-specification.
19514 exception-specification:
19515 throw ( type-id-list [opt] )
19517 Returns a TREE_LIST representing the exception-specification. The
19518 TREE_VALUE of each node is a type. */
19521 cp_parser_exception_specification_opt (cp_parser* parser)
19525 const char *saved_message;
19527 /* Peek at the next token. */
19528 token = cp_lexer_peek_token (parser->lexer);
19530 /* Is it a noexcept-specification? */
19531 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19534 cp_lexer_consume_token (parser->lexer);
19536 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19538 cp_lexer_consume_token (parser->lexer);
19540 /* Types may not be defined in an exception-specification. */
19541 saved_message = parser->type_definition_forbidden_message;
19542 parser->type_definition_forbidden_message
19543 = G_("types may not be defined in an exception-specification");
19545 expr = cp_parser_constant_expression (parser, false, NULL);
19547 /* Restore the saved message. */
19548 parser->type_definition_forbidden_message = saved_message;
19550 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19553 expr = boolean_true_node;
19555 return build_noexcept_spec (expr, tf_warning_or_error);
19558 /* If it's not `throw', then there's no exception-specification. */
19559 if (!cp_parser_is_keyword (token, RID_THROW))
19563 /* Enable this once a lot of code has transitioned to noexcept? */
19564 if (cxx_dialect == cxx0x && !in_system_header)
19565 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19566 "deprecated in C++0x; use %<noexcept%> instead");
19569 /* Consume the `throw'. */
19570 cp_lexer_consume_token (parser->lexer);
19572 /* Look for the `('. */
19573 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19575 /* Peek at the next token. */
19576 token = cp_lexer_peek_token (parser->lexer);
19577 /* If it's not a `)', then there is a type-id-list. */
19578 if (token->type != CPP_CLOSE_PAREN)
19580 /* Types may not be defined in an exception-specification. */
19581 saved_message = parser->type_definition_forbidden_message;
19582 parser->type_definition_forbidden_message
19583 = G_("types may not be defined in an exception-specification");
19584 /* Parse the type-id-list. */
19585 type_id_list = cp_parser_type_id_list (parser);
19586 /* Restore the saved message. */
19587 parser->type_definition_forbidden_message = saved_message;
19590 type_id_list = empty_except_spec;
19592 /* Look for the `)'. */
19593 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19595 return type_id_list;
19598 /* Parse an (optional) type-id-list.
19602 type-id-list , type-id ... [opt]
19604 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19605 in the order that the types were presented. */
19608 cp_parser_type_id_list (cp_parser* parser)
19610 tree types = NULL_TREE;
19617 /* Get the next type-id. */
19618 type = cp_parser_type_id (parser);
19619 /* Parse the optional ellipsis. */
19620 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19622 /* Consume the `...'. */
19623 cp_lexer_consume_token (parser->lexer);
19625 /* Turn the type into a pack expansion expression. */
19626 type = make_pack_expansion (type);
19628 /* Add it to the list. */
19629 types = add_exception_specifier (types, type, /*complain=*/1);
19630 /* Peek at the next token. */
19631 token = cp_lexer_peek_token (parser->lexer);
19632 /* If it is not a `,', we are done. */
19633 if (token->type != CPP_COMMA)
19635 /* Consume the `,'. */
19636 cp_lexer_consume_token (parser->lexer);
19639 return nreverse (types);
19642 /* Parse a try-block.
19645 try compound-statement handler-seq */
19648 cp_parser_try_block (cp_parser* parser)
19652 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19653 try_block = begin_try_block ();
19654 cp_parser_compound_statement (parser, NULL, true, false);
19655 finish_try_block (try_block);
19656 cp_parser_handler_seq (parser);
19657 finish_handler_sequence (try_block);
19662 /* Parse a function-try-block.
19664 function-try-block:
19665 try ctor-initializer [opt] function-body handler-seq */
19668 cp_parser_function_try_block (cp_parser* parser)
19670 tree compound_stmt;
19672 bool ctor_initializer_p;
19674 /* Look for the `try' keyword. */
19675 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19677 /* Let the rest of the front end know where we are. */
19678 try_block = begin_function_try_block (&compound_stmt);
19679 /* Parse the function-body. */
19681 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19682 /* We're done with the `try' part. */
19683 finish_function_try_block (try_block);
19684 /* Parse the handlers. */
19685 cp_parser_handler_seq (parser);
19686 /* We're done with the handlers. */
19687 finish_function_handler_sequence (try_block, compound_stmt);
19689 return ctor_initializer_p;
19692 /* Parse a handler-seq.
19695 handler handler-seq [opt] */
19698 cp_parser_handler_seq (cp_parser* parser)
19704 /* Parse the handler. */
19705 cp_parser_handler (parser);
19706 /* Peek at the next token. */
19707 token = cp_lexer_peek_token (parser->lexer);
19708 /* If it's not `catch' then there are no more handlers. */
19709 if (!cp_parser_is_keyword (token, RID_CATCH))
19714 /* Parse a handler.
19717 catch ( exception-declaration ) compound-statement */
19720 cp_parser_handler (cp_parser* parser)
19725 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19726 handler = begin_handler ();
19727 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19728 declaration = cp_parser_exception_declaration (parser);
19729 finish_handler_parms (declaration, handler);
19730 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19731 cp_parser_compound_statement (parser, NULL, false, false);
19732 finish_handler (handler);
19735 /* Parse an exception-declaration.
19737 exception-declaration:
19738 type-specifier-seq declarator
19739 type-specifier-seq abstract-declarator
19743 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19744 ellipsis variant is used. */
19747 cp_parser_exception_declaration (cp_parser* parser)
19749 cp_decl_specifier_seq type_specifiers;
19750 cp_declarator *declarator;
19751 const char *saved_message;
19753 /* If it's an ellipsis, it's easy to handle. */
19754 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19756 /* Consume the `...' token. */
19757 cp_lexer_consume_token (parser->lexer);
19761 /* Types may not be defined in exception-declarations. */
19762 saved_message = parser->type_definition_forbidden_message;
19763 parser->type_definition_forbidden_message
19764 = G_("types may not be defined in exception-declarations");
19766 /* Parse the type-specifier-seq. */
19767 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19768 /*is_trailing_return=*/false,
19770 /* If it's a `)', then there is no declarator. */
19771 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19774 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19775 /*ctor_dtor_or_conv_p=*/NULL,
19776 /*parenthesized_p=*/NULL,
19777 /*member_p=*/false);
19779 /* Restore the saved message. */
19780 parser->type_definition_forbidden_message = saved_message;
19782 if (!type_specifiers.any_specifiers_p)
19783 return error_mark_node;
19785 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19788 /* Parse a throw-expression.
19791 throw assignment-expression [opt]
19793 Returns a THROW_EXPR representing the throw-expression. */
19796 cp_parser_throw_expression (cp_parser* parser)
19801 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19802 token = cp_lexer_peek_token (parser->lexer);
19803 /* Figure out whether or not there is an assignment-expression
19804 following the "throw" keyword. */
19805 if (token->type == CPP_COMMA
19806 || token->type == CPP_SEMICOLON
19807 || token->type == CPP_CLOSE_PAREN
19808 || token->type == CPP_CLOSE_SQUARE
19809 || token->type == CPP_CLOSE_BRACE
19810 || token->type == CPP_COLON)
19811 expression = NULL_TREE;
19813 expression = cp_parser_assignment_expression (parser,
19814 /*cast_p=*/false, NULL);
19816 return build_throw (expression);
19819 /* GNU Extensions */
19821 /* Parse an (optional) asm-specification.
19824 asm ( string-literal )
19826 If the asm-specification is present, returns a STRING_CST
19827 corresponding to the string-literal. Otherwise, returns
19831 cp_parser_asm_specification_opt (cp_parser* parser)
19834 tree asm_specification;
19836 /* Peek at the next token. */
19837 token = cp_lexer_peek_token (parser->lexer);
19838 /* If the next token isn't the `asm' keyword, then there's no
19839 asm-specification. */
19840 if (!cp_parser_is_keyword (token, RID_ASM))
19843 /* Consume the `asm' token. */
19844 cp_lexer_consume_token (parser->lexer);
19845 /* Look for the `('. */
19846 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19848 /* Look for the string-literal. */
19849 asm_specification = cp_parser_string_literal (parser, false, false);
19851 /* Look for the `)'. */
19852 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19854 return asm_specification;
19857 /* Parse an asm-operand-list.
19861 asm-operand-list , asm-operand
19864 string-literal ( expression )
19865 [ string-literal ] string-literal ( expression )
19867 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19868 each node is the expression. The TREE_PURPOSE is itself a
19869 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19870 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19871 is a STRING_CST for the string literal before the parenthesis. Returns
19872 ERROR_MARK_NODE if any of the operands are invalid. */
19875 cp_parser_asm_operand_list (cp_parser* parser)
19877 tree asm_operands = NULL_TREE;
19878 bool invalid_operands = false;
19882 tree string_literal;
19886 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19888 /* Consume the `[' token. */
19889 cp_lexer_consume_token (parser->lexer);
19890 /* Read the operand name. */
19891 name = cp_parser_identifier (parser);
19892 if (name != error_mark_node)
19893 name = build_string (IDENTIFIER_LENGTH (name),
19894 IDENTIFIER_POINTER (name));
19895 /* Look for the closing `]'. */
19896 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19900 /* Look for the string-literal. */
19901 string_literal = cp_parser_string_literal (parser, false, false);
19903 /* Look for the `('. */
19904 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19905 /* Parse the expression. */
19906 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19907 /* Look for the `)'. */
19908 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19910 if (name == error_mark_node
19911 || string_literal == error_mark_node
19912 || expression == error_mark_node)
19913 invalid_operands = true;
19915 /* Add this operand to the list. */
19916 asm_operands = tree_cons (build_tree_list (name, string_literal),
19919 /* If the next token is not a `,', there are no more
19921 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19923 /* Consume the `,'. */
19924 cp_lexer_consume_token (parser->lexer);
19927 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19930 /* Parse an asm-clobber-list.
19934 asm-clobber-list , string-literal
19936 Returns a TREE_LIST, indicating the clobbers in the order that they
19937 appeared. The TREE_VALUE of each node is a STRING_CST. */
19940 cp_parser_asm_clobber_list (cp_parser* parser)
19942 tree clobbers = NULL_TREE;
19946 tree string_literal;
19948 /* Look for the string literal. */
19949 string_literal = cp_parser_string_literal (parser, false, false);
19950 /* Add it to the list. */
19951 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19952 /* If the next token is not a `,', then the list is
19954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19956 /* Consume the `,' token. */
19957 cp_lexer_consume_token (parser->lexer);
19963 /* Parse an asm-label-list.
19967 asm-label-list , identifier
19969 Returns a TREE_LIST, indicating the labels in the order that they
19970 appeared. The TREE_VALUE of each node is a label. */
19973 cp_parser_asm_label_list (cp_parser* parser)
19975 tree labels = NULL_TREE;
19979 tree identifier, label, name;
19981 /* Look for the identifier. */
19982 identifier = cp_parser_identifier (parser);
19983 if (!error_operand_p (identifier))
19985 label = lookup_label (identifier);
19986 if (TREE_CODE (label) == LABEL_DECL)
19988 TREE_USED (label) = 1;
19989 check_goto (label);
19990 name = build_string (IDENTIFIER_LENGTH (identifier),
19991 IDENTIFIER_POINTER (identifier));
19992 labels = tree_cons (name, label, labels);
19995 /* If the next token is not a `,', then the list is
19997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19999 /* Consume the `,' token. */
20000 cp_lexer_consume_token (parser->lexer);
20003 return nreverse (labels);
20006 /* Parse an (optional) series of attributes.
20009 attributes attribute
20012 __attribute__ (( attribute-list [opt] ))
20014 The return value is as for cp_parser_attribute_list. */
20017 cp_parser_attributes_opt (cp_parser* parser)
20019 tree attributes = NULL_TREE;
20024 tree attribute_list;
20026 /* Peek at the next token. */
20027 token = cp_lexer_peek_token (parser->lexer);
20028 /* If it's not `__attribute__', then we're done. */
20029 if (token->keyword != RID_ATTRIBUTE)
20032 /* Consume the `__attribute__' keyword. */
20033 cp_lexer_consume_token (parser->lexer);
20034 /* Look for the two `(' tokens. */
20035 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20036 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20038 /* Peek at the next token. */
20039 token = cp_lexer_peek_token (parser->lexer);
20040 if (token->type != CPP_CLOSE_PAREN)
20041 /* Parse the attribute-list. */
20042 attribute_list = cp_parser_attribute_list (parser);
20044 /* If the next token is a `)', then there is no attribute
20046 attribute_list = NULL;
20048 /* Look for the two `)' tokens. */
20049 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20050 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20052 /* Add these new attributes to the list. */
20053 attributes = chainon (attributes, attribute_list);
20059 /* Parse an attribute-list.
20063 attribute-list , attribute
20067 identifier ( identifier )
20068 identifier ( identifier , expression-list )
20069 identifier ( expression-list )
20071 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20072 to an attribute. The TREE_PURPOSE of each node is the identifier
20073 indicating which attribute is in use. The TREE_VALUE represents
20074 the arguments, if any. */
20077 cp_parser_attribute_list (cp_parser* parser)
20079 tree attribute_list = NULL_TREE;
20080 bool save_translate_strings_p = parser->translate_strings_p;
20082 parser->translate_strings_p = false;
20089 /* Look for the identifier. We also allow keywords here; for
20090 example `__attribute__ ((const))' is legal. */
20091 token = cp_lexer_peek_token (parser->lexer);
20092 if (token->type == CPP_NAME
20093 || token->type == CPP_KEYWORD)
20095 tree arguments = NULL_TREE;
20097 /* Consume the token. */
20098 token = cp_lexer_consume_token (parser->lexer);
20100 /* Save away the identifier that indicates which attribute
20102 identifier = (token->type == CPP_KEYWORD)
20103 /* For keywords, use the canonical spelling, not the
20104 parsed identifier. */
20105 ? ridpointers[(int) token->keyword]
20108 attribute = build_tree_list (identifier, NULL_TREE);
20110 /* Peek at the next token. */
20111 token = cp_lexer_peek_token (parser->lexer);
20112 /* If it's an `(', then parse the attribute arguments. */
20113 if (token->type == CPP_OPEN_PAREN)
20116 int attr_flag = (attribute_takes_identifier_p (identifier)
20117 ? id_attr : normal_attr);
20118 vec = cp_parser_parenthesized_expression_list
20119 (parser, attr_flag, /*cast_p=*/false,
20120 /*allow_expansion_p=*/false,
20121 /*non_constant_p=*/NULL);
20123 arguments = error_mark_node;
20126 arguments = build_tree_list_vec (vec);
20127 release_tree_vector (vec);
20129 /* Save the arguments away. */
20130 TREE_VALUE (attribute) = arguments;
20133 if (arguments != error_mark_node)
20135 /* Add this attribute to the list. */
20136 TREE_CHAIN (attribute) = attribute_list;
20137 attribute_list = attribute;
20140 token = cp_lexer_peek_token (parser->lexer);
20142 /* Now, look for more attributes. If the next token isn't a
20143 `,', we're done. */
20144 if (token->type != CPP_COMMA)
20147 /* Consume the comma and keep going. */
20148 cp_lexer_consume_token (parser->lexer);
20150 parser->translate_strings_p = save_translate_strings_p;
20152 /* We built up the list in reverse order. */
20153 return nreverse (attribute_list);
20156 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20157 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20158 current value of the PEDANTIC flag, regardless of whether or not
20159 the `__extension__' keyword is present. The caller is responsible
20160 for restoring the value of the PEDANTIC flag. */
20163 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20165 /* Save the old value of the PEDANTIC flag. */
20166 *saved_pedantic = pedantic;
20168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20170 /* Consume the `__extension__' token. */
20171 cp_lexer_consume_token (parser->lexer);
20172 /* We're not being pedantic while the `__extension__' keyword is
20182 /* Parse a label declaration.
20185 __label__ label-declarator-seq ;
20187 label-declarator-seq:
20188 identifier , label-declarator-seq
20192 cp_parser_label_declaration (cp_parser* parser)
20194 /* Look for the `__label__' keyword. */
20195 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20201 /* Look for an identifier. */
20202 identifier = cp_parser_identifier (parser);
20203 /* If we failed, stop. */
20204 if (identifier == error_mark_node)
20206 /* Declare it as a label. */
20207 finish_label_decl (identifier);
20208 /* If the next token is a `;', stop. */
20209 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20211 /* Look for the `,' separating the label declarations. */
20212 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20215 /* Look for the final `;'. */
20216 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20219 /* Support Functions */
20221 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20222 NAME should have one of the representations used for an
20223 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20224 is returned. If PARSER->SCOPE is a dependent type, then a
20225 SCOPE_REF is returned.
20227 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20228 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20229 was formed. Abstractly, such entities should not be passed to this
20230 function, because they do not need to be looked up, but it is
20231 simpler to check for this special case here, rather than at the
20234 In cases not explicitly covered above, this function returns a
20235 DECL, OVERLOAD, or baselink representing the result of the lookup.
20236 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20239 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20240 (e.g., "struct") that was used. In that case bindings that do not
20241 refer to types are ignored.
20243 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20246 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20249 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20252 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20253 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20254 NULL_TREE otherwise. */
20257 cp_parser_lookup_name (cp_parser *parser, tree name,
20258 enum tag_types tag_type,
20261 bool check_dependency,
20262 tree *ambiguous_decls,
20263 location_t name_location)
20267 tree object_type = parser->context->object_type;
20269 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20270 flags |= LOOKUP_COMPLAIN;
20272 /* Assume that the lookup will be unambiguous. */
20273 if (ambiguous_decls)
20274 *ambiguous_decls = NULL_TREE;
20276 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20277 no longer valid. Note that if we are parsing tentatively, and
20278 the parse fails, OBJECT_TYPE will be automatically restored. */
20279 parser->context->object_type = NULL_TREE;
20281 if (name == error_mark_node)
20282 return error_mark_node;
20284 /* A template-id has already been resolved; there is no lookup to
20286 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20288 if (BASELINK_P (name))
20290 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20291 == TEMPLATE_ID_EXPR);
20295 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20296 it should already have been checked to make sure that the name
20297 used matches the type being destroyed. */
20298 if (TREE_CODE (name) == BIT_NOT_EXPR)
20302 /* Figure out to which type this destructor applies. */
20304 type = parser->scope;
20305 else if (object_type)
20306 type = object_type;
20308 type = current_class_type;
20309 /* If that's not a class type, there is no destructor. */
20310 if (!type || !CLASS_TYPE_P (type))
20311 return error_mark_node;
20312 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20313 lazily_declare_fn (sfk_destructor, type);
20314 if (!CLASSTYPE_DESTRUCTORS (type))
20315 return error_mark_node;
20316 /* If it was a class type, return the destructor. */
20317 return CLASSTYPE_DESTRUCTORS (type);
20320 /* By this point, the NAME should be an ordinary identifier. If
20321 the id-expression was a qualified name, the qualifying scope is
20322 stored in PARSER->SCOPE at this point. */
20323 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20325 /* Perform the lookup. */
20330 if (parser->scope == error_mark_node)
20331 return error_mark_node;
20333 /* If the SCOPE is dependent, the lookup must be deferred until
20334 the template is instantiated -- unless we are explicitly
20335 looking up names in uninstantiated templates. Even then, we
20336 cannot look up the name if the scope is not a class type; it
20337 might, for example, be a template type parameter. */
20338 dependent_p = (TYPE_P (parser->scope)
20339 && dependent_scope_p (parser->scope));
20340 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20342 /* Defer lookup. */
20343 decl = error_mark_node;
20346 tree pushed_scope = NULL_TREE;
20348 /* If PARSER->SCOPE is a dependent type, then it must be a
20349 class type, and we must not be checking dependencies;
20350 otherwise, we would have processed this lookup above. So
20351 that PARSER->SCOPE is not considered a dependent base by
20352 lookup_member, we must enter the scope here. */
20354 pushed_scope = push_scope (parser->scope);
20356 /* If the PARSER->SCOPE is a template specialization, it
20357 may be instantiated during name lookup. In that case,
20358 errors may be issued. Even if we rollback the current
20359 tentative parse, those errors are valid. */
20360 decl = lookup_qualified_name (parser->scope, name,
20361 tag_type != none_type,
20362 /*complain=*/true);
20364 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20365 lookup result and the nested-name-specifier nominates a class C:
20366 * if the name specified after the nested-name-specifier, when
20367 looked up in C, is the injected-class-name of C (Clause 9), or
20368 * if the name specified after the nested-name-specifier is the
20369 same as the identifier or the simple-template-id's template-
20370 name in the last component of the nested-name-specifier,
20371 the name is instead considered to name the constructor of
20372 class C. [ Note: for example, the constructor is not an
20373 acceptable lookup result in an elaborated-type-specifier so
20374 the constructor would not be used in place of the
20375 injected-class-name. --end note ] Such a constructor name
20376 shall be used only in the declarator-id of a declaration that
20377 names a constructor or in a using-declaration. */
20378 if (tag_type == none_type
20379 && DECL_SELF_REFERENCE_P (decl)
20380 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20381 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20382 tag_type != none_type,
20383 /*complain=*/true);
20385 /* If we have a single function from a using decl, pull it out. */
20386 if (TREE_CODE (decl) == OVERLOAD
20387 && !really_overloaded_fn (decl))
20388 decl = OVL_FUNCTION (decl);
20391 pop_scope (pushed_scope);
20394 /* If the scope is a dependent type and either we deferred lookup or
20395 we did lookup but didn't find the name, rememeber the name. */
20396 if (decl == error_mark_node && TYPE_P (parser->scope)
20397 && dependent_type_p (parser->scope))
20403 /* The resolution to Core Issue 180 says that `struct
20404 A::B' should be considered a type-name, even if `A'
20406 type = make_typename_type (parser->scope, name, tag_type,
20407 /*complain=*/tf_error);
20408 decl = TYPE_NAME (type);
20410 else if (is_template
20411 && (cp_parser_next_token_ends_template_argument_p (parser)
20412 || cp_lexer_next_token_is (parser->lexer,
20414 decl = make_unbound_class_template (parser->scope,
20416 /*complain=*/tf_error);
20418 decl = build_qualified_name (/*type=*/NULL_TREE,
20419 parser->scope, name,
20422 parser->qualifying_scope = parser->scope;
20423 parser->object_scope = NULL_TREE;
20425 else if (object_type)
20427 tree object_decl = NULL_TREE;
20428 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20429 OBJECT_TYPE is not a class. */
20430 if (CLASS_TYPE_P (object_type))
20431 /* If the OBJECT_TYPE is a template specialization, it may
20432 be instantiated during name lookup. In that case, errors
20433 may be issued. Even if we rollback the current tentative
20434 parse, those errors are valid. */
20435 object_decl = lookup_member (object_type,
20438 tag_type != none_type,
20439 tf_warning_or_error);
20440 /* Look it up in the enclosing context, too. */
20441 decl = lookup_name_real (name, tag_type != none_type,
20443 /*block_p=*/true, is_namespace, flags);
20444 parser->object_scope = object_type;
20445 parser->qualifying_scope = NULL_TREE;
20447 decl = object_decl;
20451 decl = lookup_name_real (name, tag_type != none_type,
20453 /*block_p=*/true, is_namespace, flags);
20454 parser->qualifying_scope = NULL_TREE;
20455 parser->object_scope = NULL_TREE;
20458 /* If the lookup failed, let our caller know. */
20459 if (!decl || decl == error_mark_node)
20460 return error_mark_node;
20462 /* Pull out the template from an injected-class-name (or multiple). */
20464 decl = maybe_get_template_decl_from_type_decl (decl);
20466 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20467 if (TREE_CODE (decl) == TREE_LIST)
20469 if (ambiguous_decls)
20470 *ambiguous_decls = decl;
20471 /* The error message we have to print is too complicated for
20472 cp_parser_error, so we incorporate its actions directly. */
20473 if (!cp_parser_simulate_error (parser))
20475 error_at (name_location, "reference to %qD is ambiguous",
20477 print_candidates (decl);
20479 return error_mark_node;
20482 gcc_assert (DECL_P (decl)
20483 || TREE_CODE (decl) == OVERLOAD
20484 || TREE_CODE (decl) == SCOPE_REF
20485 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20486 || BASELINK_P (decl));
20488 /* If we have resolved the name of a member declaration, check to
20489 see if the declaration is accessible. When the name resolves to
20490 set of overloaded functions, accessibility is checked when
20491 overload resolution is done.
20493 During an explicit instantiation, access is not checked at all,
20494 as per [temp.explicit]. */
20496 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20498 maybe_record_typedef_use (decl);
20503 /* Like cp_parser_lookup_name, but for use in the typical case where
20504 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20505 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20508 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20510 return cp_parser_lookup_name (parser, name,
20512 /*is_template=*/false,
20513 /*is_namespace=*/false,
20514 /*check_dependency=*/true,
20515 /*ambiguous_decls=*/NULL,
20519 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20520 the current context, return the TYPE_DECL. If TAG_NAME_P is
20521 true, the DECL indicates the class being defined in a class-head,
20522 or declared in an elaborated-type-specifier.
20524 Otherwise, return DECL. */
20527 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20529 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20530 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20533 template <typename T> struct B;
20536 template <typename T> struct A::B {};
20538 Similarly, in an elaborated-type-specifier:
20540 namespace N { struct X{}; }
20543 template <typename T> friend struct N::X;
20546 However, if the DECL refers to a class type, and we are in
20547 the scope of the class, then the name lookup automatically
20548 finds the TYPE_DECL created by build_self_reference rather
20549 than a TEMPLATE_DECL. For example, in:
20551 template <class T> struct S {
20555 there is no need to handle such case. */
20557 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20558 return DECL_TEMPLATE_RESULT (decl);
20563 /* If too many, or too few, template-parameter lists apply to the
20564 declarator, issue an error message. Returns TRUE if all went well,
20565 and FALSE otherwise. */
20568 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20569 cp_declarator *declarator,
20570 location_t declarator_location)
20572 unsigned num_templates;
20574 /* We haven't seen any classes that involve template parameters yet. */
20577 switch (declarator->kind)
20580 if (declarator->u.id.qualifying_scope)
20584 scope = declarator->u.id.qualifying_scope;
20586 while (scope && CLASS_TYPE_P (scope))
20588 /* You're supposed to have one `template <...>'
20589 for every template class, but you don't need one
20590 for a full specialization. For example:
20592 template <class T> struct S{};
20593 template <> struct S<int> { void f(); };
20594 void S<int>::f () {}
20596 is correct; there shouldn't be a `template <>' for
20597 the definition of `S<int>::f'. */
20598 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20599 /* If SCOPE does not have template information of any
20600 kind, then it is not a template, nor is it nested
20601 within a template. */
20603 if (explicit_class_specialization_p (scope))
20605 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20608 scope = TYPE_CONTEXT (scope);
20611 else if (TREE_CODE (declarator->u.id.unqualified_name)
20612 == TEMPLATE_ID_EXPR)
20613 /* If the DECLARATOR has the form `X<y>' then it uses one
20614 additional level of template parameters. */
20617 return cp_parser_check_template_parameters
20618 (parser, num_templates, declarator_location, declarator);
20624 case cdk_reference:
20626 return (cp_parser_check_declarator_template_parameters
20627 (parser, declarator->declarator, declarator_location));
20633 gcc_unreachable ();
20638 /* NUM_TEMPLATES were used in the current declaration. If that is
20639 invalid, return FALSE and issue an error messages. Otherwise,
20640 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20641 declarator and we can print more accurate diagnostics. */
20644 cp_parser_check_template_parameters (cp_parser* parser,
20645 unsigned num_templates,
20646 location_t location,
20647 cp_declarator *declarator)
20649 /* If there are the same number of template classes and parameter
20650 lists, that's OK. */
20651 if (parser->num_template_parameter_lists == num_templates)
20653 /* If there are more, but only one more, then we are referring to a
20654 member template. That's OK too. */
20655 if (parser->num_template_parameter_lists == num_templates + 1)
20657 /* If there are more template classes than parameter lists, we have
20660 template <class T> void S<T>::R<T>::f (); */
20661 if (parser->num_template_parameter_lists < num_templates)
20663 if (declarator && !current_function_decl)
20664 error_at (location, "specializing member %<%T::%E%> "
20665 "requires %<template<>%> syntax",
20666 declarator->u.id.qualifying_scope,
20667 declarator->u.id.unqualified_name);
20668 else if (declarator)
20669 error_at (location, "invalid declaration of %<%T::%E%>",
20670 declarator->u.id.qualifying_scope,
20671 declarator->u.id.unqualified_name);
20673 error_at (location, "too few template-parameter-lists");
20676 /* Otherwise, there are too many template parameter lists. We have
20679 template <class T> template <class U> void S::f(); */
20680 error_at (location, "too many template-parameter-lists");
20684 /* Parse an optional `::' token indicating that the following name is
20685 from the global namespace. If so, PARSER->SCOPE is set to the
20686 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20687 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20688 Returns the new value of PARSER->SCOPE, if the `::' token is
20689 present, and NULL_TREE otherwise. */
20692 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20696 /* Peek at the next token. */
20697 token = cp_lexer_peek_token (parser->lexer);
20698 /* If we're looking at a `::' token then we're starting from the
20699 global namespace, not our current location. */
20700 if (token->type == CPP_SCOPE)
20702 /* Consume the `::' token. */
20703 cp_lexer_consume_token (parser->lexer);
20704 /* Set the SCOPE so that we know where to start the lookup. */
20705 parser->scope = global_namespace;
20706 parser->qualifying_scope = global_namespace;
20707 parser->object_scope = NULL_TREE;
20709 return parser->scope;
20711 else if (!current_scope_valid_p)
20713 parser->scope = NULL_TREE;
20714 parser->qualifying_scope = NULL_TREE;
20715 parser->object_scope = NULL_TREE;
20721 /* Returns TRUE if the upcoming token sequence is the start of a
20722 constructor declarator. If FRIEND_P is true, the declarator is
20723 preceded by the `friend' specifier. */
20726 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20728 bool constructor_p;
20729 tree nested_name_specifier;
20730 cp_token *next_token;
20732 /* The common case is that this is not a constructor declarator, so
20733 try to avoid doing lots of work if at all possible. It's not
20734 valid declare a constructor at function scope. */
20735 if (parser->in_function_body)
20737 /* And only certain tokens can begin a constructor declarator. */
20738 next_token = cp_lexer_peek_token (parser->lexer);
20739 if (next_token->type != CPP_NAME
20740 && next_token->type != CPP_SCOPE
20741 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20742 && next_token->type != CPP_TEMPLATE_ID)
20745 /* Parse tentatively; we are going to roll back all of the tokens
20747 cp_parser_parse_tentatively (parser);
20748 /* Assume that we are looking at a constructor declarator. */
20749 constructor_p = true;
20751 /* Look for the optional `::' operator. */
20752 cp_parser_global_scope_opt (parser,
20753 /*current_scope_valid_p=*/false);
20754 /* Look for the nested-name-specifier. */
20755 nested_name_specifier
20756 = (cp_parser_nested_name_specifier_opt (parser,
20757 /*typename_keyword_p=*/false,
20758 /*check_dependency_p=*/false,
20760 /*is_declaration=*/false));
20761 /* Outside of a class-specifier, there must be a
20762 nested-name-specifier. */
20763 if (!nested_name_specifier &&
20764 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20766 constructor_p = false;
20767 else if (nested_name_specifier == error_mark_node)
20768 constructor_p = false;
20770 /* If we have a class scope, this is easy; DR 147 says that S::S always
20771 names the constructor, and no other qualified name could. */
20772 if (constructor_p && nested_name_specifier
20773 && CLASS_TYPE_P (nested_name_specifier))
20775 tree id = cp_parser_unqualified_id (parser,
20776 /*template_keyword_p=*/false,
20777 /*check_dependency_p=*/false,
20778 /*declarator_p=*/true,
20779 /*optional_p=*/false);
20780 if (is_overloaded_fn (id))
20781 id = DECL_NAME (get_first_fn (id));
20782 if (!constructor_name_p (id, nested_name_specifier))
20783 constructor_p = false;
20785 /* If we still think that this might be a constructor-declarator,
20786 look for a class-name. */
20787 else if (constructor_p)
20791 template <typename T> struct S {
20795 we must recognize that the nested `S' names a class. */
20797 type_decl = cp_parser_class_name (parser,
20798 /*typename_keyword_p=*/false,
20799 /*template_keyword_p=*/false,
20801 /*check_dependency_p=*/false,
20802 /*class_head_p=*/false,
20803 /*is_declaration=*/false);
20804 /* If there was no class-name, then this is not a constructor. */
20805 constructor_p = !cp_parser_error_occurred (parser);
20807 /* If we're still considering a constructor, we have to see a `(',
20808 to begin the parameter-declaration-clause, followed by either a
20809 `)', an `...', or a decl-specifier. We need to check for a
20810 type-specifier to avoid being fooled into thinking that:
20814 is a constructor. (It is actually a function named `f' that
20815 takes one parameter (of type `int') and returns a value of type
20818 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20819 constructor_p = false;
20822 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20823 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20824 /* A parameter declaration begins with a decl-specifier,
20825 which is either the "attribute" keyword, a storage class
20826 specifier, or (usually) a type-specifier. */
20827 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20830 tree pushed_scope = NULL_TREE;
20831 unsigned saved_num_template_parameter_lists;
20833 /* Names appearing in the type-specifier should be looked up
20834 in the scope of the class. */
20835 if (current_class_type)
20839 type = TREE_TYPE (type_decl);
20840 if (TREE_CODE (type) == TYPENAME_TYPE)
20842 type = resolve_typename_type (type,
20843 /*only_current_p=*/false);
20844 if (TREE_CODE (type) == TYPENAME_TYPE)
20846 cp_parser_abort_tentative_parse (parser);
20850 pushed_scope = push_scope (type);
20853 /* Inside the constructor parameter list, surrounding
20854 template-parameter-lists do not apply. */
20855 saved_num_template_parameter_lists
20856 = parser->num_template_parameter_lists;
20857 parser->num_template_parameter_lists = 0;
20859 /* Look for the type-specifier. */
20860 cp_parser_type_specifier (parser,
20861 CP_PARSER_FLAGS_NONE,
20862 /*decl_specs=*/NULL,
20863 /*is_declarator=*/true,
20864 /*declares_class_or_enum=*/NULL,
20865 /*is_cv_qualifier=*/NULL);
20867 parser->num_template_parameter_lists
20868 = saved_num_template_parameter_lists;
20870 /* Leave the scope of the class. */
20872 pop_scope (pushed_scope);
20874 constructor_p = !cp_parser_error_occurred (parser);
20878 /* We did not really want to consume any tokens. */
20879 cp_parser_abort_tentative_parse (parser);
20881 return constructor_p;
20884 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20885 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20886 they must be performed once we are in the scope of the function.
20888 Returns the function defined. */
20891 cp_parser_function_definition_from_specifiers_and_declarator
20892 (cp_parser* parser,
20893 cp_decl_specifier_seq *decl_specifiers,
20895 const cp_declarator *declarator)
20900 /* Begin the function-definition. */
20901 success_p = start_function (decl_specifiers, declarator, attributes);
20903 /* The things we're about to see are not directly qualified by any
20904 template headers we've seen thus far. */
20905 reset_specialization ();
20907 /* If there were names looked up in the decl-specifier-seq that we
20908 did not check, check them now. We must wait until we are in the
20909 scope of the function to perform the checks, since the function
20910 might be a friend. */
20911 perform_deferred_access_checks ();
20915 /* Skip the entire function. */
20916 cp_parser_skip_to_end_of_block_or_statement (parser);
20917 fn = error_mark_node;
20919 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20921 /* Seen already, skip it. An error message has already been output. */
20922 cp_parser_skip_to_end_of_block_or_statement (parser);
20923 fn = current_function_decl;
20924 current_function_decl = NULL_TREE;
20925 /* If this is a function from a class, pop the nested class. */
20926 if (current_class_name)
20927 pop_nested_class ();
20932 if (DECL_DECLARED_INLINE_P (current_function_decl))
20933 tv = TV_PARSE_INLINE;
20935 tv = TV_PARSE_FUNC;
20937 fn = cp_parser_function_definition_after_declarator (parser,
20938 /*inline_p=*/false);
20945 /* Parse the part of a function-definition that follows the
20946 declarator. INLINE_P is TRUE iff this function is an inline
20947 function defined within a class-specifier.
20949 Returns the function defined. */
20952 cp_parser_function_definition_after_declarator (cp_parser* parser,
20956 bool ctor_initializer_p = false;
20957 bool saved_in_unbraced_linkage_specification_p;
20958 bool saved_in_function_body;
20959 unsigned saved_num_template_parameter_lists;
20962 saved_in_function_body = parser->in_function_body;
20963 parser->in_function_body = true;
20964 /* If the next token is `return', then the code may be trying to
20965 make use of the "named return value" extension that G++ used to
20967 token = cp_lexer_peek_token (parser->lexer);
20968 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20970 /* Consume the `return' keyword. */
20971 cp_lexer_consume_token (parser->lexer);
20972 /* Look for the identifier that indicates what value is to be
20974 cp_parser_identifier (parser);
20975 /* Issue an error message. */
20976 error_at (token->location,
20977 "named return values are no longer supported");
20978 /* Skip tokens until we reach the start of the function body. */
20981 cp_token *token = cp_lexer_peek_token (parser->lexer);
20982 if (token->type == CPP_OPEN_BRACE
20983 || token->type == CPP_EOF
20984 || token->type == CPP_PRAGMA_EOL)
20986 cp_lexer_consume_token (parser->lexer);
20989 /* The `extern' in `extern "C" void f () { ... }' does not apply to
20990 anything declared inside `f'. */
20991 saved_in_unbraced_linkage_specification_p
20992 = parser->in_unbraced_linkage_specification_p;
20993 parser->in_unbraced_linkage_specification_p = false;
20994 /* Inside the function, surrounding template-parameter-lists do not
20996 saved_num_template_parameter_lists
20997 = parser->num_template_parameter_lists;
20998 parser->num_template_parameter_lists = 0;
21000 start_lambda_scope (current_function_decl);
21002 /* If the next token is `try', `__transaction_atomic', or
21003 `__transaction_relaxed`, then we are looking at either function-try-block
21004 or function-transaction-block. Note that all of these include the
21006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21007 ctor_initializer_p = cp_parser_function_transaction (parser,
21008 RID_TRANSACTION_ATOMIC);
21009 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21010 RID_TRANSACTION_RELAXED))
21011 ctor_initializer_p = cp_parser_function_transaction (parser,
21012 RID_TRANSACTION_RELAXED);
21013 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21014 ctor_initializer_p = cp_parser_function_try_block (parser);
21017 = cp_parser_ctor_initializer_opt_and_function_body (parser);
21019 finish_lambda_scope ();
21021 /* Finish the function. */
21022 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21023 (inline_p ? 2 : 0));
21024 /* Generate code for it, if necessary. */
21025 expand_or_defer_fn (fn);
21026 /* Restore the saved values. */
21027 parser->in_unbraced_linkage_specification_p
21028 = saved_in_unbraced_linkage_specification_p;
21029 parser->num_template_parameter_lists
21030 = saved_num_template_parameter_lists;
21031 parser->in_function_body = saved_in_function_body;
21036 /* Parse a template-declaration, assuming that the `export' (and
21037 `extern') keywords, if present, has already been scanned. MEMBER_P
21038 is as for cp_parser_template_declaration. */
21041 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21043 tree decl = NULL_TREE;
21044 VEC (deferred_access_check,gc) *checks;
21045 tree parameter_list;
21046 bool friend_p = false;
21047 bool need_lang_pop;
21050 /* Look for the `template' keyword. */
21051 token = cp_lexer_peek_token (parser->lexer);
21052 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21056 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21058 if (at_class_scope_p () && current_function_decl)
21060 /* 14.5.2.2 [temp.mem]
21062 A local class shall not have member templates. */
21063 error_at (token->location,
21064 "invalid declaration of member template in local class");
21065 cp_parser_skip_to_end_of_block_or_statement (parser);
21070 A template ... shall not have C linkage. */
21071 if (current_lang_name == lang_name_c)
21073 error_at (token->location, "template with C linkage");
21074 /* Give it C++ linkage to avoid confusing other parts of the
21076 push_lang_context (lang_name_cplusplus);
21077 need_lang_pop = true;
21080 need_lang_pop = false;
21082 /* We cannot perform access checks on the template parameter
21083 declarations until we know what is being declared, just as we
21084 cannot check the decl-specifier list. */
21085 push_deferring_access_checks (dk_deferred);
21087 /* If the next token is `>', then we have an invalid
21088 specialization. Rather than complain about an invalid template
21089 parameter, issue an error message here. */
21090 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21092 cp_parser_error (parser, "invalid explicit specialization");
21093 begin_specialization ();
21094 parameter_list = NULL_TREE;
21098 /* Parse the template parameters. */
21099 parameter_list = cp_parser_template_parameter_list (parser);
21100 fixup_template_parms ();
21103 /* Get the deferred access checks from the parameter list. These
21104 will be checked once we know what is being declared, as for a
21105 member template the checks must be performed in the scope of the
21106 class containing the member. */
21107 checks = get_deferred_access_checks ();
21109 /* Look for the `>'. */
21110 cp_parser_skip_to_end_of_template_parameter_list (parser);
21111 /* We just processed one more parameter list. */
21112 ++parser->num_template_parameter_lists;
21113 /* If the next token is `template', there are more template
21115 if (cp_lexer_next_token_is_keyword (parser->lexer,
21117 cp_parser_template_declaration_after_export (parser, member_p);
21118 else if (cxx_dialect >= cxx0x
21119 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21120 decl = cp_parser_alias_declaration (parser);
21123 /* There are no access checks when parsing a template, as we do not
21124 know if a specialization will be a friend. */
21125 push_deferring_access_checks (dk_no_check);
21126 token = cp_lexer_peek_token (parser->lexer);
21127 decl = cp_parser_single_declaration (parser,
21130 /*explicit_specialization_p=*/false,
21132 pop_deferring_access_checks ();
21134 /* If this is a member template declaration, let the front
21136 if (member_p && !friend_p && decl)
21138 if (TREE_CODE (decl) == TYPE_DECL)
21139 cp_parser_check_access_in_redeclaration (decl, token->location);
21141 decl = finish_member_template_decl (decl);
21143 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21144 make_friend_class (current_class_type, TREE_TYPE (decl),
21145 /*complain=*/true);
21147 /* We are done with the current parameter list. */
21148 --parser->num_template_parameter_lists;
21150 pop_deferring_access_checks ();
21153 finish_template_decl (parameter_list);
21155 /* Check the template arguments for a literal operator template. */
21157 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21158 && UDLIT_OPER_P (DECL_NAME (decl)))
21161 if (parameter_list == NULL_TREE)
21165 int num_parms = TREE_VEC_LENGTH (parameter_list);
21166 if (num_parms != 1)
21170 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21171 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21172 if (TREE_TYPE (parm) != char_type_node
21173 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21178 error ("literal operator template %qD has invalid parameter list."
21179 " Expected non-type template argument pack <char...>",
21182 /* Register member declarations. */
21183 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21184 finish_member_declaration (decl);
21185 /* For the erroneous case of a template with C linkage, we pushed an
21186 implicit C++ linkage scope; exit that scope now. */
21188 pop_lang_context ();
21189 /* If DECL is a function template, we must return to parse it later.
21190 (Even though there is no definition, there might be default
21191 arguments that need handling.) */
21192 if (member_p && decl
21193 && (TREE_CODE (decl) == FUNCTION_DECL
21194 || DECL_FUNCTION_TEMPLATE_P (decl)))
21195 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21198 /* Perform the deferred access checks from a template-parameter-list.
21199 CHECKS is a TREE_LIST of access checks, as returned by
21200 get_deferred_access_checks. */
21203 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21205 ++processing_template_parmlist;
21206 perform_access_checks (checks);
21207 --processing_template_parmlist;
21210 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21211 `function-definition' sequence. MEMBER_P is true, this declaration
21212 appears in a class scope.
21214 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21215 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21218 cp_parser_single_declaration (cp_parser* parser,
21219 VEC (deferred_access_check,gc)* checks,
21221 bool explicit_specialization_p,
21224 int declares_class_or_enum;
21225 tree decl = NULL_TREE;
21226 cp_decl_specifier_seq decl_specifiers;
21227 bool function_definition_p = false;
21228 cp_token *decl_spec_token_start;
21230 /* This function is only used when processing a template
21232 gcc_assert (innermost_scope_kind () == sk_template_parms
21233 || innermost_scope_kind () == sk_template_spec);
21235 /* Defer access checks until we know what is being declared. */
21236 push_deferring_access_checks (dk_deferred);
21238 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21240 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21241 cp_parser_decl_specifier_seq (parser,
21242 CP_PARSER_FLAGS_OPTIONAL,
21244 &declares_class_or_enum);
21246 *friend_p = cp_parser_friend_p (&decl_specifiers);
21248 /* There are no template typedefs. */
21249 if (decl_specifiers.specs[(int) ds_typedef])
21251 error_at (decl_spec_token_start->location,
21252 "template declaration of %<typedef%>");
21253 decl = error_mark_node;
21256 /* Gather up the access checks that occurred the
21257 decl-specifier-seq. */
21258 stop_deferring_access_checks ();
21260 /* Check for the declaration of a template class. */
21261 if (declares_class_or_enum)
21263 if (cp_parser_declares_only_class_p (parser))
21265 decl = shadow_tag (&decl_specifiers);
21270 friend template <typename T> struct A<T>::B;
21273 A<T>::B will be represented by a TYPENAME_TYPE, and
21274 therefore not recognized by shadow_tag. */
21275 if (friend_p && *friend_p
21277 && decl_specifiers.type
21278 && TYPE_P (decl_specifiers.type))
21279 decl = decl_specifiers.type;
21281 if (decl && decl != error_mark_node)
21282 decl = TYPE_NAME (decl);
21284 decl = error_mark_node;
21286 /* Perform access checks for template parameters. */
21287 cp_parser_perform_template_parameter_access_checks (checks);
21291 /* Complain about missing 'typename' or other invalid type names. */
21292 if (!decl_specifiers.any_type_specifiers_p
21293 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21295 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21296 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21297 the rest of this declaration. */
21298 decl = error_mark_node;
21302 /* If it's not a template class, try for a template function. If
21303 the next token is a `;', then this declaration does not declare
21304 anything. But, if there were errors in the decl-specifiers, then
21305 the error might well have come from an attempted class-specifier.
21306 In that case, there's no need to warn about a missing declarator. */
21308 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21309 || decl_specifiers.type != error_mark_node))
21311 decl = cp_parser_init_declarator (parser,
21314 /*function_definition_allowed_p=*/true,
21316 declares_class_or_enum,
21317 &function_definition_p,
21320 /* 7.1.1-1 [dcl.stc]
21322 A storage-class-specifier shall not be specified in an explicit
21323 specialization... */
21325 && explicit_specialization_p
21326 && decl_specifiers.storage_class != sc_none)
21328 error_at (decl_spec_token_start->location,
21329 "explicit template specialization cannot have a storage class");
21330 decl = error_mark_node;
21334 /* Look for a trailing `;' after the declaration. */
21335 if (!function_definition_p
21336 && (decl == error_mark_node
21337 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21338 cp_parser_skip_to_end_of_block_or_statement (parser);
21341 pop_deferring_access_checks ();
21343 /* Clear any current qualification; whatever comes next is the start
21344 of something new. */
21345 parser->scope = NULL_TREE;
21346 parser->qualifying_scope = NULL_TREE;
21347 parser->object_scope = NULL_TREE;
21352 /* Parse a cast-expression that is not the operand of a unary "&". */
21355 cp_parser_simple_cast_expression (cp_parser *parser)
21357 return cp_parser_cast_expression (parser, /*address_p=*/false,
21358 /*cast_p=*/false, NULL);
21361 /* Parse a functional cast to TYPE. Returns an expression
21362 representing the cast. */
21365 cp_parser_functional_cast (cp_parser* parser, tree type)
21368 tree expression_list;
21372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21374 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21375 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21376 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21377 if (TREE_CODE (type) == TYPE_DECL)
21378 type = TREE_TYPE (type);
21379 return finish_compound_literal (type, expression_list,
21380 tf_warning_or_error);
21384 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21386 /*allow_expansion_p=*/true,
21387 /*non_constant_p=*/NULL);
21389 expression_list = error_mark_node;
21392 expression_list = build_tree_list_vec (vec);
21393 release_tree_vector (vec);
21396 cast = build_functional_cast (type, expression_list,
21397 tf_warning_or_error);
21398 /* [expr.const]/1: In an integral constant expression "only type
21399 conversions to integral or enumeration type can be used". */
21400 if (TREE_CODE (type) == TYPE_DECL)
21401 type = TREE_TYPE (type);
21402 if (cast != error_mark_node
21403 && !cast_valid_in_integral_constant_expression_p (type)
21404 && cp_parser_non_integral_constant_expression (parser,
21406 return error_mark_node;
21410 /* Save the tokens that make up the body of a member function defined
21411 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21412 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21413 specifiers applied to the declaration. Returns the FUNCTION_DECL
21414 for the member function. */
21417 cp_parser_save_member_function_body (cp_parser* parser,
21418 cp_decl_specifier_seq *decl_specifiers,
21419 cp_declarator *declarator,
21426 /* Create the FUNCTION_DECL. */
21427 fn = grokmethod (decl_specifiers, declarator, attributes);
21428 /* If something went badly wrong, bail out now. */
21429 if (fn == error_mark_node)
21431 /* If there's a function-body, skip it. */
21432 if (cp_parser_token_starts_function_definition_p
21433 (cp_lexer_peek_token (parser->lexer)))
21434 cp_parser_skip_to_end_of_block_or_statement (parser);
21435 return error_mark_node;
21438 /* Remember it, if there default args to post process. */
21439 cp_parser_save_default_args (parser, fn);
21441 /* Save away the tokens that make up the body of the
21443 first = parser->lexer->next_token;
21444 /* We can have braced-init-list mem-initializers before the fn body. */
21445 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21447 cp_lexer_consume_token (parser->lexer);
21448 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21449 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21451 /* cache_group will stop after an un-nested { } pair, too. */
21452 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21455 /* variadic mem-inits have ... after the ')'. */
21456 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21457 cp_lexer_consume_token (parser->lexer);
21460 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21461 /* Handle function try blocks. */
21462 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21463 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21464 last = parser->lexer->next_token;
21466 /* Save away the inline definition; we will process it when the
21467 class is complete. */
21468 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21469 DECL_PENDING_INLINE_P (fn) = 1;
21471 /* We need to know that this was defined in the class, so that
21472 friend templates are handled correctly. */
21473 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21475 /* Add FN to the queue of functions to be parsed later. */
21476 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21481 /* Save the tokens that make up the in-class initializer for a non-static
21482 data member. Returns a DEFAULT_ARG. */
21485 cp_parser_save_nsdmi (cp_parser* parser)
21487 /* Save away the tokens that make up the body of the
21489 cp_token *first = parser->lexer->next_token;
21493 /* Save tokens until the next comma or semicolon. */
21494 cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21496 last = parser->lexer->next_token;
21498 node = make_node (DEFAULT_ARG);
21499 DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21500 DEFARG_INSTANTIATIONS (node) = NULL;
21506 /* Parse a template-argument-list, as well as the trailing ">" (but
21507 not the opening "<"). See cp_parser_template_argument_list for the
21511 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21515 tree saved_qualifying_scope;
21516 tree saved_object_scope;
21517 bool saved_greater_than_is_operator_p;
21518 int saved_unevaluated_operand;
21519 int saved_inhibit_evaluation_warnings;
21523 When parsing a template-id, the first non-nested `>' is taken as
21524 the end of the template-argument-list rather than a greater-than
21526 saved_greater_than_is_operator_p
21527 = parser->greater_than_is_operator_p;
21528 parser->greater_than_is_operator_p = false;
21529 /* Parsing the argument list may modify SCOPE, so we save it
21531 saved_scope = parser->scope;
21532 saved_qualifying_scope = parser->qualifying_scope;
21533 saved_object_scope = parser->object_scope;
21534 /* We need to evaluate the template arguments, even though this
21535 template-id may be nested within a "sizeof". */
21536 saved_unevaluated_operand = cp_unevaluated_operand;
21537 cp_unevaluated_operand = 0;
21538 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21539 c_inhibit_evaluation_warnings = 0;
21540 /* Parse the template-argument-list itself. */
21541 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21542 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21543 arguments = NULL_TREE;
21545 arguments = cp_parser_template_argument_list (parser);
21546 /* Look for the `>' that ends the template-argument-list. If we find
21547 a '>>' instead, it's probably just a typo. */
21548 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21550 if (cxx_dialect != cxx98)
21552 /* In C++0x, a `>>' in a template argument list or cast
21553 expression is considered to be two separate `>'
21554 tokens. So, change the current token to a `>', but don't
21555 consume it: it will be consumed later when the outer
21556 template argument list (or cast expression) is parsed.
21557 Note that this replacement of `>' for `>>' is necessary
21558 even if we are parsing tentatively: in the tentative
21559 case, after calling
21560 cp_parser_enclosed_template_argument_list we will always
21561 throw away all of the template arguments and the first
21562 closing `>', either because the template argument list
21563 was erroneous or because we are replacing those tokens
21564 with a CPP_TEMPLATE_ID token. The second `>' (which will
21565 not have been thrown away) is needed either to close an
21566 outer template argument list or to complete a new-style
21568 cp_token *token = cp_lexer_peek_token (parser->lexer);
21569 token->type = CPP_GREATER;
21571 else if (!saved_greater_than_is_operator_p)
21573 /* If we're in a nested template argument list, the '>>' has
21574 to be a typo for '> >'. We emit the error message, but we
21575 continue parsing and we push a '>' as next token, so that
21576 the argument list will be parsed correctly. Note that the
21577 global source location is still on the token before the
21578 '>>', so we need to say explicitly where we want it. */
21579 cp_token *token = cp_lexer_peek_token (parser->lexer);
21580 error_at (token->location, "%<>>%> should be %<> >%> "
21581 "within a nested template argument list");
21583 token->type = CPP_GREATER;
21587 /* If this is not a nested template argument list, the '>>'
21588 is a typo for '>'. Emit an error message and continue.
21589 Same deal about the token location, but here we can get it
21590 right by consuming the '>>' before issuing the diagnostic. */
21591 cp_token *token = cp_lexer_consume_token (parser->lexer);
21592 error_at (token->location,
21593 "spurious %<>>%>, use %<>%> to terminate "
21594 "a template argument list");
21598 cp_parser_skip_to_end_of_template_parameter_list (parser);
21599 /* The `>' token might be a greater-than operator again now. */
21600 parser->greater_than_is_operator_p
21601 = saved_greater_than_is_operator_p;
21602 /* Restore the SAVED_SCOPE. */
21603 parser->scope = saved_scope;
21604 parser->qualifying_scope = saved_qualifying_scope;
21605 parser->object_scope = saved_object_scope;
21606 cp_unevaluated_operand = saved_unevaluated_operand;
21607 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21612 /* MEMBER_FUNCTION is a member function, or a friend. If default
21613 arguments, or the body of the function have not yet been parsed,
21617 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21619 timevar_push (TV_PARSE_INMETH);
21620 /* If this member is a template, get the underlying
21622 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21623 member_function = DECL_TEMPLATE_RESULT (member_function);
21625 /* There should not be any class definitions in progress at this
21626 point; the bodies of members are only parsed outside of all class
21628 gcc_assert (parser->num_classes_being_defined == 0);
21629 /* While we're parsing the member functions we might encounter more
21630 classes. We want to handle them right away, but we don't want
21631 them getting mixed up with functions that are currently in the
21633 push_unparsed_function_queues (parser);
21635 /* Make sure that any template parameters are in scope. */
21636 maybe_begin_member_template_processing (member_function);
21638 /* If the body of the function has not yet been parsed, parse it
21640 if (DECL_PENDING_INLINE_P (member_function))
21642 tree function_scope;
21643 cp_token_cache *tokens;
21645 /* The function is no longer pending; we are processing it. */
21646 tokens = DECL_PENDING_INLINE_INFO (member_function);
21647 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21648 DECL_PENDING_INLINE_P (member_function) = 0;
21650 /* If this is a local class, enter the scope of the containing
21652 function_scope = current_function_decl;
21653 if (function_scope)
21654 push_function_context ();
21656 /* Push the body of the function onto the lexer stack. */
21657 cp_parser_push_lexer_for_tokens (parser, tokens);
21659 /* Let the front end know that we going to be defining this
21661 start_preparsed_function (member_function, NULL_TREE,
21662 SF_PRE_PARSED | SF_INCLASS_INLINE);
21664 /* Don't do access checking if it is a templated function. */
21665 if (processing_template_decl)
21666 push_deferring_access_checks (dk_no_check);
21668 /* Now, parse the body of the function. */
21669 cp_parser_function_definition_after_declarator (parser,
21670 /*inline_p=*/true);
21672 if (processing_template_decl)
21673 pop_deferring_access_checks ();
21675 /* Leave the scope of the containing function. */
21676 if (function_scope)
21677 pop_function_context ();
21678 cp_parser_pop_lexer (parser);
21681 /* Remove any template parameters from the symbol table. */
21682 maybe_end_member_template_processing ();
21684 /* Restore the queue. */
21685 pop_unparsed_function_queues (parser);
21686 timevar_pop (TV_PARSE_INMETH);
21689 /* If DECL contains any default args, remember it on the unparsed
21690 functions queue. */
21693 cp_parser_save_default_args (cp_parser* parser, tree decl)
21697 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21699 probe = TREE_CHAIN (probe))
21700 if (TREE_PURPOSE (probe))
21702 cp_default_arg_entry *entry
21703 = VEC_safe_push (cp_default_arg_entry, gc,
21704 unparsed_funs_with_default_args, NULL);
21705 entry->class_type = current_class_type;
21706 entry->decl = decl;
21711 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21712 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21713 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21714 from the parameter-type-list. */
21717 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21718 tree default_arg, tree parmtype)
21720 cp_token_cache *tokens;
21724 /* Push the saved tokens for the default argument onto the parser's
21726 tokens = DEFARG_TOKENS (default_arg);
21727 cp_parser_push_lexer_for_tokens (parser, tokens);
21729 start_lambda_scope (decl);
21731 /* Parse the default argument. */
21732 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21733 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21734 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21736 finish_lambda_scope ();
21738 if (!processing_template_decl)
21740 /* In a non-template class, check conversions now. In a template,
21741 we'll wait and instantiate these as needed. */
21742 if (TREE_CODE (decl) == PARM_DECL)
21743 parsed_arg = check_default_argument (parmtype, parsed_arg);
21746 int flags = LOOKUP_IMPLICIT;
21747 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21748 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21749 flags = LOOKUP_NORMAL;
21750 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21754 /* If the token stream has not been completely used up, then
21755 there was extra junk after the end of the default
21757 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21759 if (TREE_CODE (decl) == PARM_DECL)
21760 cp_parser_error (parser, "expected %<,%>");
21762 cp_parser_error (parser, "expected %<;%>");
21765 /* Revert to the main lexer. */
21766 cp_parser_pop_lexer (parser);
21771 /* FIELD is a non-static data member with an initializer which we saved for
21772 later; parse it now. */
21775 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21779 push_unparsed_function_queues (parser);
21780 def = cp_parser_late_parse_one_default_arg (parser, field,
21781 DECL_INITIAL (field),
21783 pop_unparsed_function_queues (parser);
21785 DECL_INITIAL (field) = def;
21788 /* FN is a FUNCTION_DECL which may contains a parameter with an
21789 unparsed DEFAULT_ARG. Parse the default args now. This function
21790 assumes that the current scope is the scope in which the default
21791 argument should be processed. */
21794 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21796 bool saved_local_variables_forbidden_p;
21797 tree parm, parmdecl;
21799 /* While we're parsing the default args, we might (due to the
21800 statement expression extension) encounter more classes. We want
21801 to handle them right away, but we don't want them getting mixed
21802 up with default args that are currently in the queue. */
21803 push_unparsed_function_queues (parser);
21805 /* Local variable names (and the `this' keyword) may not appear
21806 in a default argument. */
21807 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21808 parser->local_variables_forbidden_p = true;
21810 push_defarg_context (fn);
21812 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21813 parmdecl = DECL_ARGUMENTS (fn);
21814 parm && parm != void_list_node;
21815 parm = TREE_CHAIN (parm),
21816 parmdecl = DECL_CHAIN (parmdecl))
21818 tree default_arg = TREE_PURPOSE (parm);
21820 VEC(tree,gc) *insts;
21827 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21828 /* This can happen for a friend declaration for a function
21829 already declared with default arguments. */
21833 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21835 TREE_VALUE (parm));
21836 if (parsed_arg == error_mark_node)
21841 TREE_PURPOSE (parm) = parsed_arg;
21843 /* Update any instantiations we've already created. */
21844 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21845 VEC_iterate (tree, insts, ix, copy); ix++)
21846 TREE_PURPOSE (copy) = parsed_arg;
21849 pop_defarg_context ();
21851 /* Make sure no default arg is missing. */
21852 check_default_args (fn);
21854 /* Restore the state of local_variables_forbidden_p. */
21855 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21857 /* Restore the queue. */
21858 pop_unparsed_function_queues (parser);
21861 /* Parse the operand of `sizeof' (or a similar operator). Returns
21862 either a TYPE or an expression, depending on the form of the
21863 input. The KEYWORD indicates which kind of expression we have
21867 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21869 tree expr = NULL_TREE;
21870 const char *saved_message;
21872 bool saved_integral_constant_expression_p;
21873 bool saved_non_integral_constant_expression_p;
21874 bool pack_expansion_p = false;
21876 /* Types cannot be defined in a `sizeof' expression. Save away the
21878 saved_message = parser->type_definition_forbidden_message;
21879 /* And create the new one. */
21880 tmp = concat ("types may not be defined in %<",
21881 IDENTIFIER_POINTER (ridpointers[keyword]),
21882 "%> expressions", NULL);
21883 parser->type_definition_forbidden_message = tmp;
21885 /* The restrictions on constant-expressions do not apply inside
21886 sizeof expressions. */
21887 saved_integral_constant_expression_p
21888 = parser->integral_constant_expression_p;
21889 saved_non_integral_constant_expression_p
21890 = parser->non_integral_constant_expression_p;
21891 parser->integral_constant_expression_p = false;
21893 /* If it's a `...', then we are computing the length of a parameter
21895 if (keyword == RID_SIZEOF
21896 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21898 /* Consume the `...'. */
21899 cp_lexer_consume_token (parser->lexer);
21900 maybe_warn_variadic_templates ();
21902 /* Note that this is an expansion. */
21903 pack_expansion_p = true;
21906 /* Do not actually evaluate the expression. */
21907 ++cp_unevaluated_operand;
21908 ++c_inhibit_evaluation_warnings;
21909 /* If it's a `(', then we might be looking at the type-id
21911 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21914 bool saved_in_type_id_in_expr_p;
21916 /* We can't be sure yet whether we're looking at a type-id or an
21918 cp_parser_parse_tentatively (parser);
21919 /* Consume the `('. */
21920 cp_lexer_consume_token (parser->lexer);
21921 /* Parse the type-id. */
21922 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21923 parser->in_type_id_in_expr_p = true;
21924 type = cp_parser_type_id (parser);
21925 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21926 /* Now, look for the trailing `)'. */
21927 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21928 /* If all went well, then we're done. */
21929 if (cp_parser_parse_definitely (parser))
21931 cp_decl_specifier_seq decl_specs;
21933 /* Build a trivial decl-specifier-seq. */
21934 clear_decl_specs (&decl_specs);
21935 decl_specs.type = type;
21937 /* Call grokdeclarator to figure out what type this is. */
21938 expr = grokdeclarator (NULL,
21942 /*attrlist=*/NULL);
21946 /* If the type-id production did not work out, then we must be
21947 looking at the unary-expression production. */
21949 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21950 /*cast_p=*/false, NULL);
21952 if (pack_expansion_p)
21953 /* Build a pack expansion. */
21954 expr = make_pack_expansion (expr);
21956 /* Go back to evaluating expressions. */
21957 --cp_unevaluated_operand;
21958 --c_inhibit_evaluation_warnings;
21960 /* Free the message we created. */
21962 /* And restore the old one. */
21963 parser->type_definition_forbidden_message = saved_message;
21964 parser->integral_constant_expression_p
21965 = saved_integral_constant_expression_p;
21966 parser->non_integral_constant_expression_p
21967 = saved_non_integral_constant_expression_p;
21972 /* If the current declaration has no declarator, return true. */
21975 cp_parser_declares_only_class_p (cp_parser *parser)
21977 /* If the next token is a `;' or a `,' then there is no
21979 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21980 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21983 /* Update the DECL_SPECS to reflect the storage class indicated by
21987 cp_parser_set_storage_class (cp_parser *parser,
21988 cp_decl_specifier_seq *decl_specs,
21990 location_t location)
21992 cp_storage_class storage_class;
21994 if (parser->in_unbraced_linkage_specification_p)
21996 error_at (location, "invalid use of %qD in linkage specification",
21997 ridpointers[keyword]);
22000 else if (decl_specs->storage_class != sc_none)
22002 decl_specs->conflicting_specifiers_p = true;
22006 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22007 && decl_specs->specs[(int) ds_thread])
22009 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22010 decl_specs->specs[(int) ds_thread] = 0;
22016 storage_class = sc_auto;
22019 storage_class = sc_register;
22022 storage_class = sc_static;
22025 storage_class = sc_extern;
22028 storage_class = sc_mutable;
22031 gcc_unreachable ();
22033 decl_specs->storage_class = storage_class;
22035 /* A storage class specifier cannot be applied alongside a typedef
22036 specifier. If there is a typedef specifier present then set
22037 conflicting_specifiers_p which will trigger an error later
22038 on in grokdeclarator. */
22039 if (decl_specs->specs[(int)ds_typedef])
22040 decl_specs->conflicting_specifiers_p = true;
22043 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22044 is true, the type is a class or enum definition. */
22047 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22049 location_t location,
22050 bool type_definition_p)
22052 decl_specs->any_specifiers_p = true;
22054 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22055 (with, for example, in "typedef int wchar_t;") we remember that
22056 this is what happened. In system headers, we ignore these
22057 declarations so that G++ can work with system headers that are not
22059 if (decl_specs->specs[(int) ds_typedef]
22060 && !type_definition_p
22061 && (type_spec == boolean_type_node
22062 || type_spec == char16_type_node
22063 || type_spec == char32_type_node
22064 || type_spec == wchar_type_node)
22065 && (decl_specs->type
22066 || decl_specs->specs[(int) ds_long]
22067 || decl_specs->specs[(int) ds_short]
22068 || decl_specs->specs[(int) ds_unsigned]
22069 || decl_specs->specs[(int) ds_signed]))
22071 decl_specs->redefined_builtin_type = type_spec;
22072 if (!decl_specs->type)
22074 decl_specs->type = type_spec;
22075 decl_specs->type_definition_p = false;
22076 decl_specs->type_location = location;
22079 else if (decl_specs->type)
22080 decl_specs->multiple_types_p = true;
22083 decl_specs->type = type_spec;
22084 decl_specs->type_definition_p = type_definition_p;
22085 decl_specs->redefined_builtin_type = NULL_TREE;
22086 decl_specs->type_location = location;
22090 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22091 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22094 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22096 return decl_specifiers->specs[(int) ds_friend] != 0;
22099 /* Issue an error message indicating that TOKEN_DESC was expected.
22100 If KEYWORD is true, it indicated this function is called by
22101 cp_parser_require_keword and the required token can only be
22102 a indicated keyword. */
22105 cp_parser_required_error (cp_parser *parser,
22106 required_token token_desc,
22109 switch (token_desc)
22112 cp_parser_error (parser, "expected %<new%>");
22115 cp_parser_error (parser, "expected %<delete%>");
22118 cp_parser_error (parser, "expected %<return%>");
22121 cp_parser_error (parser, "expected %<while%>");
22124 cp_parser_error (parser, "expected %<extern%>");
22126 case RT_STATIC_ASSERT:
22127 cp_parser_error (parser, "expected %<static_assert%>");
22130 cp_parser_error (parser, "expected %<decltype%>");
22133 cp_parser_error (parser, "expected %<operator%>");
22136 cp_parser_error (parser, "expected %<class%>");
22139 cp_parser_error (parser, "expected %<template%>");
22142 cp_parser_error (parser, "expected %<namespace%>");
22145 cp_parser_error (parser, "expected %<using%>");
22148 cp_parser_error (parser, "expected %<asm%>");
22151 cp_parser_error (parser, "expected %<try%>");
22154 cp_parser_error (parser, "expected %<catch%>");
22157 cp_parser_error (parser, "expected %<throw%>");
22160 cp_parser_error (parser, "expected %<__label__%>");
22163 cp_parser_error (parser, "expected %<@try%>");
22165 case RT_AT_SYNCHRONIZED:
22166 cp_parser_error (parser, "expected %<@synchronized%>");
22169 cp_parser_error (parser, "expected %<@throw%>");
22171 case RT_TRANSACTION_ATOMIC:
22172 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22174 case RT_TRANSACTION_RELAXED:
22175 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22182 switch (token_desc)
22185 cp_parser_error (parser, "expected %<;%>");
22187 case RT_OPEN_PAREN:
22188 cp_parser_error (parser, "expected %<(%>");
22190 case RT_CLOSE_BRACE:
22191 cp_parser_error (parser, "expected %<}%>");
22193 case RT_OPEN_BRACE:
22194 cp_parser_error (parser, "expected %<{%>");
22196 case RT_CLOSE_SQUARE:
22197 cp_parser_error (parser, "expected %<]%>");
22199 case RT_OPEN_SQUARE:
22200 cp_parser_error (parser, "expected %<[%>");
22203 cp_parser_error (parser, "expected %<,%>");
22206 cp_parser_error (parser, "expected %<::%>");
22209 cp_parser_error (parser, "expected %<<%>");
22212 cp_parser_error (parser, "expected %<>%>");
22215 cp_parser_error (parser, "expected %<=%>");
22218 cp_parser_error (parser, "expected %<...%>");
22221 cp_parser_error (parser, "expected %<*%>");
22224 cp_parser_error (parser, "expected %<~%>");
22227 cp_parser_error (parser, "expected %<:%>");
22229 case RT_COLON_SCOPE:
22230 cp_parser_error (parser, "expected %<:%> or %<::%>");
22232 case RT_CLOSE_PAREN:
22233 cp_parser_error (parser, "expected %<)%>");
22235 case RT_COMMA_CLOSE_PAREN:
22236 cp_parser_error (parser, "expected %<,%> or %<)%>");
22238 case RT_PRAGMA_EOL:
22239 cp_parser_error (parser, "expected end of line");
22242 cp_parser_error (parser, "expected identifier");
22245 cp_parser_error (parser, "expected selection-statement");
22247 case RT_INTERATION:
22248 cp_parser_error (parser, "expected iteration-statement");
22251 cp_parser_error (parser, "expected jump-statement");
22254 cp_parser_error (parser, "expected class-key");
22256 case RT_CLASS_TYPENAME_TEMPLATE:
22257 cp_parser_error (parser,
22258 "expected %<class%>, %<typename%>, or %<template%>");
22261 gcc_unreachable ();
22265 gcc_unreachable ();
22270 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22271 issue an error message indicating that TOKEN_DESC was expected.
22273 Returns the token consumed, if the token had the appropriate type.
22274 Otherwise, returns NULL. */
22277 cp_parser_require (cp_parser* parser,
22278 enum cpp_ttype type,
22279 required_token token_desc)
22281 if (cp_lexer_next_token_is (parser->lexer, type))
22282 return cp_lexer_consume_token (parser->lexer);
22285 /* Output the MESSAGE -- unless we're parsing tentatively. */
22286 if (!cp_parser_simulate_error (parser))
22287 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22292 /* An error message is produced if the next token is not '>'.
22293 All further tokens are skipped until the desired token is
22294 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22297 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22299 /* Current level of '< ... >'. */
22300 unsigned level = 0;
22301 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22302 unsigned nesting_depth = 0;
22304 /* Are we ready, yet? If not, issue error message. */
22305 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22308 /* Skip tokens until the desired token is found. */
22311 /* Peek at the next token. */
22312 switch (cp_lexer_peek_token (parser->lexer)->type)
22315 if (!nesting_depth)
22320 if (cxx_dialect == cxx98)
22321 /* C++0x views the `>>' operator as two `>' tokens, but
22324 else if (!nesting_depth && level-- == 0)
22326 /* We've hit a `>>' where the first `>' closes the
22327 template argument list, and the second `>' is
22328 spurious. Just consume the `>>' and stop; we've
22329 already produced at least one error. */
22330 cp_lexer_consume_token (parser->lexer);
22333 /* Fall through for C++0x, so we handle the second `>' in
22337 if (!nesting_depth && level-- == 0)
22339 /* We've reached the token we want, consume it and stop. */
22340 cp_lexer_consume_token (parser->lexer);
22345 case CPP_OPEN_PAREN:
22346 case CPP_OPEN_SQUARE:
22350 case CPP_CLOSE_PAREN:
22351 case CPP_CLOSE_SQUARE:
22352 if (nesting_depth-- == 0)
22357 case CPP_PRAGMA_EOL:
22358 case CPP_SEMICOLON:
22359 case CPP_OPEN_BRACE:
22360 case CPP_CLOSE_BRACE:
22361 /* The '>' was probably forgotten, don't look further. */
22368 /* Consume this token. */
22369 cp_lexer_consume_token (parser->lexer);
22373 /* If the next token is the indicated keyword, consume it. Otherwise,
22374 issue an error message indicating that TOKEN_DESC was expected.
22376 Returns the token consumed, if the token had the appropriate type.
22377 Otherwise, returns NULL. */
22380 cp_parser_require_keyword (cp_parser* parser,
22382 required_token token_desc)
22384 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22386 if (token && token->keyword != keyword)
22388 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22395 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22396 function-definition. */
22399 cp_parser_token_starts_function_definition_p (cp_token* token)
22401 return (/* An ordinary function-body begins with an `{'. */
22402 token->type == CPP_OPEN_BRACE
22403 /* A ctor-initializer begins with a `:'. */
22404 || token->type == CPP_COLON
22405 /* A function-try-block begins with `try'. */
22406 || token->keyword == RID_TRY
22407 /* A function-transaction-block begins with `__transaction_atomic'
22408 or `__transaction_relaxed'. */
22409 || token->keyword == RID_TRANSACTION_ATOMIC
22410 || token->keyword == RID_TRANSACTION_RELAXED
22411 /* The named return value extension begins with `return'. */
22412 || token->keyword == RID_RETURN);
22415 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22419 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22423 token = cp_lexer_peek_token (parser->lexer);
22424 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22427 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22428 C++0x) ending a template-argument. */
22431 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22435 token = cp_lexer_peek_token (parser->lexer);
22436 return (token->type == CPP_COMMA
22437 || token->type == CPP_GREATER
22438 || token->type == CPP_ELLIPSIS
22439 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22442 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22443 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22446 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22451 token = cp_lexer_peek_nth_token (parser->lexer, n);
22452 if (token->type == CPP_LESS)
22454 /* Check for the sequence `<::' in the original code. It would be lexed as
22455 `[:', where `[' is a digraph, and there is no whitespace before
22457 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22460 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22461 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22467 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22468 or none_type otherwise. */
22470 static enum tag_types
22471 cp_parser_token_is_class_key (cp_token* token)
22473 switch (token->keyword)
22478 return record_type;
22487 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22490 cp_parser_check_class_key (enum tag_types class_key, tree type)
22492 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22493 permerror (input_location, "%qs tag used in naming %q#T",
22494 class_key == union_type ? "union"
22495 : class_key == record_type ? "struct" : "class",
22499 /* Issue an error message if DECL is redeclared with different
22500 access than its original declaration [class.access.spec/3].
22501 This applies to nested classes and nested class templates.
22505 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22507 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22510 if ((TREE_PRIVATE (decl)
22511 != (current_access_specifier == access_private_node))
22512 || (TREE_PROTECTED (decl)
22513 != (current_access_specifier == access_protected_node)))
22514 error_at (location, "%qD redeclared with different access", decl);
22517 /* Look for the `template' keyword, as a syntactic disambiguator.
22518 Return TRUE iff it is present, in which case it will be
22522 cp_parser_optional_template_keyword (cp_parser *parser)
22524 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22526 /* The `template' keyword can only be used within templates;
22527 outside templates the parser can always figure out what is a
22528 template and what is not. */
22529 if (!processing_template_decl)
22531 cp_token *token = cp_lexer_peek_token (parser->lexer);
22532 error_at (token->location,
22533 "%<template%> (as a disambiguator) is only allowed "
22534 "within templates");
22535 /* If this part of the token stream is rescanned, the same
22536 error message would be generated. So, we purge the token
22537 from the stream. */
22538 cp_lexer_purge_token (parser->lexer);
22543 /* Consume the `template' keyword. */
22544 cp_lexer_consume_token (parser->lexer);
22552 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22553 set PARSER->SCOPE, and perform other related actions. */
22556 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22559 struct tree_check *check_value;
22560 deferred_access_check *chk;
22561 VEC (deferred_access_check,gc) *checks;
22563 /* Get the stored value. */
22564 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22565 /* Perform any access checks that were deferred. */
22566 checks = check_value->checks;
22569 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22570 perform_or_defer_access_check (chk->binfo,
22574 /* Set the scope from the stored value. */
22575 parser->scope = check_value->value;
22576 parser->qualifying_scope = check_value->qualifying_scope;
22577 parser->object_scope = NULL_TREE;
22580 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22581 encounter the end of a block before what we were looking for. */
22584 cp_parser_cache_group (cp_parser *parser,
22585 enum cpp_ttype end,
22590 cp_token *token = cp_lexer_peek_token (parser->lexer);
22592 /* Abort a parenthesized expression if we encounter a semicolon. */
22593 if ((end == CPP_CLOSE_PAREN || depth == 0)
22594 && token->type == CPP_SEMICOLON)
22596 /* If we've reached the end of the file, stop. */
22597 if (token->type == CPP_EOF
22598 || (end != CPP_PRAGMA_EOL
22599 && token->type == CPP_PRAGMA_EOL))
22601 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22602 /* We've hit the end of an enclosing block, so there's been some
22603 kind of syntax error. */
22606 /* If we're caching something finished by a comma (or semicolon),
22607 such as an NSDMI, don't consume the comma. */
22608 if (end == CPP_COMMA
22609 && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22612 /* Consume the token. */
22613 cp_lexer_consume_token (parser->lexer);
22614 /* See if it starts a new group. */
22615 if (token->type == CPP_OPEN_BRACE)
22617 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22618 /* In theory this should probably check end == '}', but
22619 cp_parser_save_member_function_body needs it to exit
22620 after either '}' or ')' when called with ')'. */
22624 else if (token->type == CPP_OPEN_PAREN)
22626 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22627 if (depth == 0 && end == CPP_CLOSE_PAREN)
22630 else if (token->type == CPP_PRAGMA)
22631 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22632 else if (token->type == end)
22637 /* Begin parsing tentatively. We always save tokens while parsing
22638 tentatively so that if the tentative parsing fails we can restore the
22642 cp_parser_parse_tentatively (cp_parser* parser)
22644 /* Enter a new parsing context. */
22645 parser->context = cp_parser_context_new (parser->context);
22646 /* Begin saving tokens. */
22647 cp_lexer_save_tokens (parser->lexer);
22648 /* In order to avoid repetitive access control error messages,
22649 access checks are queued up until we are no longer parsing
22651 push_deferring_access_checks (dk_deferred);
22654 /* Commit to the currently active tentative parse. */
22657 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22659 cp_parser_context *context;
22662 /* Mark all of the levels as committed. */
22663 lexer = parser->lexer;
22664 for (context = parser->context; context->next; context = context->next)
22666 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22668 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22669 while (!cp_lexer_saving_tokens (lexer))
22670 lexer = lexer->next;
22671 cp_lexer_commit_tokens (lexer);
22675 /* Abort the currently active tentative parse. All consumed tokens
22676 will be rolled back, and no diagnostics will be issued. */
22679 cp_parser_abort_tentative_parse (cp_parser* parser)
22681 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22682 || errorcount > 0);
22683 cp_parser_simulate_error (parser);
22684 /* Now, pretend that we want to see if the construct was
22685 successfully parsed. */
22686 cp_parser_parse_definitely (parser);
22689 /* Stop parsing tentatively. If a parse error has occurred, restore the
22690 token stream. Otherwise, commit to the tokens we have consumed.
22691 Returns true if no error occurred; false otherwise. */
22694 cp_parser_parse_definitely (cp_parser* parser)
22696 bool error_occurred;
22697 cp_parser_context *context;
22699 /* Remember whether or not an error occurred, since we are about to
22700 destroy that information. */
22701 error_occurred = cp_parser_error_occurred (parser);
22702 /* Remove the topmost context from the stack. */
22703 context = parser->context;
22704 parser->context = context->next;
22705 /* If no parse errors occurred, commit to the tentative parse. */
22706 if (!error_occurred)
22708 /* Commit to the tokens read tentatively, unless that was
22710 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22711 cp_lexer_commit_tokens (parser->lexer);
22713 pop_to_parent_deferring_access_checks ();
22715 /* Otherwise, if errors occurred, roll back our state so that things
22716 are just as they were before we began the tentative parse. */
22719 cp_lexer_rollback_tokens (parser->lexer);
22720 pop_deferring_access_checks ();
22722 /* Add the context to the front of the free list. */
22723 context->next = cp_parser_context_free_list;
22724 cp_parser_context_free_list = context;
22726 return !error_occurred;
22729 /* Returns true if we are parsing tentatively and are not committed to
22730 this tentative parse. */
22733 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22735 return (cp_parser_parsing_tentatively (parser)
22736 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22739 /* Returns nonzero iff an error has occurred during the most recent
22740 tentative parse. */
22743 cp_parser_error_occurred (cp_parser* parser)
22745 return (cp_parser_parsing_tentatively (parser)
22746 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22749 /* Returns nonzero if GNU extensions are allowed. */
22752 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22754 return parser->allow_gnu_extensions_p;
22757 /* Objective-C++ Productions */
22760 /* Parse an Objective-C expression, which feeds into a primary-expression
22764 objc-message-expression
22765 objc-string-literal
22766 objc-encode-expression
22767 objc-protocol-expression
22768 objc-selector-expression
22770 Returns a tree representation of the expression. */
22773 cp_parser_objc_expression (cp_parser* parser)
22775 /* Try to figure out what kind of declaration is present. */
22776 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22780 case CPP_OPEN_SQUARE:
22781 return cp_parser_objc_message_expression (parser);
22783 case CPP_OBJC_STRING:
22784 kwd = cp_lexer_consume_token (parser->lexer);
22785 return objc_build_string_object (kwd->u.value);
22788 switch (kwd->keyword)
22790 case RID_AT_ENCODE:
22791 return cp_parser_objc_encode_expression (parser);
22793 case RID_AT_PROTOCOL:
22794 return cp_parser_objc_protocol_expression (parser);
22796 case RID_AT_SELECTOR:
22797 return cp_parser_objc_selector_expression (parser);
22803 error_at (kwd->location,
22804 "misplaced %<@%D%> Objective-C++ construct",
22806 cp_parser_skip_to_end_of_block_or_statement (parser);
22809 return error_mark_node;
22812 /* Parse an Objective-C message expression.
22814 objc-message-expression:
22815 [ objc-message-receiver objc-message-args ]
22817 Returns a representation of an Objective-C message. */
22820 cp_parser_objc_message_expression (cp_parser* parser)
22822 tree receiver, messageargs;
22824 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
22825 receiver = cp_parser_objc_message_receiver (parser);
22826 messageargs = cp_parser_objc_message_args (parser);
22827 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22829 return objc_build_message_expr (receiver, messageargs);
22832 /* Parse an objc-message-receiver.
22834 objc-message-receiver:
22836 simple-type-specifier
22838 Returns a representation of the type or expression. */
22841 cp_parser_objc_message_receiver (cp_parser* parser)
22845 /* An Objective-C message receiver may be either (1) a type
22846 or (2) an expression. */
22847 cp_parser_parse_tentatively (parser);
22848 rcv = cp_parser_expression (parser, false, NULL);
22850 if (cp_parser_parse_definitely (parser))
22853 rcv = cp_parser_simple_type_specifier (parser,
22854 /*decl_specs=*/NULL,
22855 CP_PARSER_FLAGS_NONE);
22857 return objc_get_class_reference (rcv);
22860 /* Parse the arguments and selectors comprising an Objective-C message.
22865 objc-selector-args , objc-comma-args
22867 objc-selector-args:
22868 objc-selector [opt] : assignment-expression
22869 objc-selector-args objc-selector [opt] : assignment-expression
22872 assignment-expression
22873 objc-comma-args , assignment-expression
22875 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22876 selector arguments and TREE_VALUE containing a list of comma
22880 cp_parser_objc_message_args (cp_parser* parser)
22882 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22883 bool maybe_unary_selector_p = true;
22884 cp_token *token = cp_lexer_peek_token (parser->lexer);
22886 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22888 tree selector = NULL_TREE, arg;
22890 if (token->type != CPP_COLON)
22891 selector = cp_parser_objc_selector (parser);
22893 /* Detect if we have a unary selector. */
22894 if (maybe_unary_selector_p
22895 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22896 return build_tree_list (selector, NULL_TREE);
22898 maybe_unary_selector_p = false;
22899 cp_parser_require (parser, CPP_COLON, RT_COLON);
22900 arg = cp_parser_assignment_expression (parser, false, NULL);
22903 = chainon (sel_args,
22904 build_tree_list (selector, arg));
22906 token = cp_lexer_peek_token (parser->lexer);
22909 /* Handle non-selector arguments, if any. */
22910 while (token->type == CPP_COMMA)
22914 cp_lexer_consume_token (parser->lexer);
22915 arg = cp_parser_assignment_expression (parser, false, NULL);
22918 = chainon (addl_args,
22919 build_tree_list (NULL_TREE, arg));
22921 token = cp_lexer_peek_token (parser->lexer);
22924 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22926 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22927 return build_tree_list (error_mark_node, error_mark_node);
22930 return build_tree_list (sel_args, addl_args);
22933 /* Parse an Objective-C encode expression.
22935 objc-encode-expression:
22936 @encode objc-typename
22938 Returns an encoded representation of the type argument. */
22941 cp_parser_objc_encode_expression (cp_parser* parser)
22946 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
22947 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22948 token = cp_lexer_peek_token (parser->lexer);
22949 type = complete_type (cp_parser_type_id (parser));
22950 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22954 error_at (token->location,
22955 "%<@encode%> must specify a type as an argument");
22956 return error_mark_node;
22959 /* This happens if we find @encode(T) (where T is a template
22960 typename or something dependent on a template typename) when
22961 parsing a template. In that case, we can't compile it
22962 immediately, but we rather create an AT_ENCODE_EXPR which will
22963 need to be instantiated when the template is used.
22965 if (dependent_type_p (type))
22967 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22968 TREE_READONLY (value) = 1;
22972 return objc_build_encode_expr (type);
22975 /* Parse an Objective-C @defs expression. */
22978 cp_parser_objc_defs_expression (cp_parser *parser)
22982 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
22983 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22984 name = cp_parser_identifier (parser);
22985 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22987 return objc_get_class_ivars (name);
22990 /* Parse an Objective-C protocol expression.
22992 objc-protocol-expression:
22993 @protocol ( identifier )
22995 Returns a representation of the protocol expression. */
22998 cp_parser_objc_protocol_expression (cp_parser* parser)
23002 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23003 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23004 proto = cp_parser_identifier (parser);
23005 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23007 return objc_build_protocol_expr (proto);
23010 /* Parse an Objective-C selector expression.
23012 objc-selector-expression:
23013 @selector ( objc-method-signature )
23015 objc-method-signature:
23021 objc-selector-seq objc-selector :
23023 Returns a representation of the method selector. */
23026 cp_parser_objc_selector_expression (cp_parser* parser)
23028 tree sel_seq = NULL_TREE;
23029 bool maybe_unary_selector_p = true;
23031 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23033 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23034 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23035 token = cp_lexer_peek_token (parser->lexer);
23037 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23038 || token->type == CPP_SCOPE)
23040 tree selector = NULL_TREE;
23042 if (token->type != CPP_COLON
23043 || token->type == CPP_SCOPE)
23044 selector = cp_parser_objc_selector (parser);
23046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23047 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23049 /* Detect if we have a unary selector. */
23050 if (maybe_unary_selector_p)
23052 sel_seq = selector;
23053 goto finish_selector;
23057 cp_parser_error (parser, "expected %<:%>");
23060 maybe_unary_selector_p = false;
23061 token = cp_lexer_consume_token (parser->lexer);
23063 if (token->type == CPP_SCOPE)
23066 = chainon (sel_seq,
23067 build_tree_list (selector, NULL_TREE));
23069 = chainon (sel_seq,
23070 build_tree_list (NULL_TREE, NULL_TREE));
23074 = chainon (sel_seq,
23075 build_tree_list (selector, NULL_TREE));
23077 token = cp_lexer_peek_token (parser->lexer);
23081 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23083 return objc_build_selector_expr (loc, sel_seq);
23086 /* Parse a list of identifiers.
23088 objc-identifier-list:
23090 objc-identifier-list , identifier
23092 Returns a TREE_LIST of identifier nodes. */
23095 cp_parser_objc_identifier_list (cp_parser* parser)
23101 identifier = cp_parser_identifier (parser);
23102 if (identifier == error_mark_node)
23103 return error_mark_node;
23105 list = build_tree_list (NULL_TREE, identifier);
23106 sep = cp_lexer_peek_token (parser->lexer);
23108 while (sep->type == CPP_COMMA)
23110 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23111 identifier = cp_parser_identifier (parser);
23112 if (identifier == error_mark_node)
23115 list = chainon (list, build_tree_list (NULL_TREE,
23117 sep = cp_lexer_peek_token (parser->lexer);
23123 /* Parse an Objective-C alias declaration.
23125 objc-alias-declaration:
23126 @compatibility_alias identifier identifier ;
23128 This function registers the alias mapping with the Objective-C front end.
23129 It returns nothing. */
23132 cp_parser_objc_alias_declaration (cp_parser* parser)
23136 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23137 alias = cp_parser_identifier (parser);
23138 orig = cp_parser_identifier (parser);
23139 objc_declare_alias (alias, orig);
23140 cp_parser_consume_semicolon_at_end_of_statement (parser);
23143 /* Parse an Objective-C class forward-declaration.
23145 objc-class-declaration:
23146 @class objc-identifier-list ;
23148 The function registers the forward declarations with the Objective-C
23149 front end. It returns nothing. */
23152 cp_parser_objc_class_declaration (cp_parser* parser)
23154 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23159 id = cp_parser_identifier (parser);
23160 if (id == error_mark_node)
23163 objc_declare_class (id);
23165 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23166 cp_lexer_consume_token (parser->lexer);
23170 cp_parser_consume_semicolon_at_end_of_statement (parser);
23173 /* Parse a list of Objective-C protocol references.
23175 objc-protocol-refs-opt:
23176 objc-protocol-refs [opt]
23178 objc-protocol-refs:
23179 < objc-identifier-list >
23181 Returns a TREE_LIST of identifiers, if any. */
23184 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23186 tree protorefs = NULL_TREE;
23188 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23190 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23191 protorefs = cp_parser_objc_identifier_list (parser);
23192 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23198 /* Parse a Objective-C visibility specification. */
23201 cp_parser_objc_visibility_spec (cp_parser* parser)
23203 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23205 switch (vis->keyword)
23207 case RID_AT_PRIVATE:
23208 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23210 case RID_AT_PROTECTED:
23211 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23213 case RID_AT_PUBLIC:
23214 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23216 case RID_AT_PACKAGE:
23217 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23223 /* Eat '@private'/'@protected'/'@public'. */
23224 cp_lexer_consume_token (parser->lexer);
23227 /* Parse an Objective-C method type. Return 'true' if it is a class
23228 (+) method, and 'false' if it is an instance (-) method. */
23231 cp_parser_objc_method_type (cp_parser* parser)
23233 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23239 /* Parse an Objective-C protocol qualifier. */
23242 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23244 tree quals = NULL_TREE, node;
23245 cp_token *token = cp_lexer_peek_token (parser->lexer);
23247 node = token->u.value;
23249 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23250 && (node == ridpointers [(int) RID_IN]
23251 || node == ridpointers [(int) RID_OUT]
23252 || node == ridpointers [(int) RID_INOUT]
23253 || node == ridpointers [(int) RID_BYCOPY]
23254 || node == ridpointers [(int) RID_BYREF]
23255 || node == ridpointers [(int) RID_ONEWAY]))
23257 quals = tree_cons (NULL_TREE, node, quals);
23258 cp_lexer_consume_token (parser->lexer);
23259 token = cp_lexer_peek_token (parser->lexer);
23260 node = token->u.value;
23266 /* Parse an Objective-C typename. */
23269 cp_parser_objc_typename (cp_parser* parser)
23271 tree type_name = NULL_TREE;
23273 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23275 tree proto_quals, cp_type = NULL_TREE;
23277 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23278 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23280 /* An ObjC type name may consist of just protocol qualifiers, in which
23281 case the type shall default to 'id'. */
23282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23284 cp_type = cp_parser_type_id (parser);
23286 /* If the type could not be parsed, an error has already
23287 been produced. For error recovery, behave as if it had
23288 not been specified, which will use the default type
23290 if (cp_type == error_mark_node)
23292 cp_type = NULL_TREE;
23293 /* We need to skip to the closing parenthesis as
23294 cp_parser_type_id() does not seem to do it for
23296 cp_parser_skip_to_closing_parenthesis (parser,
23297 /*recovering=*/true,
23298 /*or_comma=*/false,
23299 /*consume_paren=*/false);
23303 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23304 type_name = build_tree_list (proto_quals, cp_type);
23310 /* Check to see if TYPE refers to an Objective-C selector name. */
23313 cp_parser_objc_selector_p (enum cpp_ttype type)
23315 return (type == CPP_NAME || type == CPP_KEYWORD
23316 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23317 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23318 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23319 || type == CPP_XOR || type == CPP_XOR_EQ);
23322 /* Parse an Objective-C selector. */
23325 cp_parser_objc_selector (cp_parser* parser)
23327 cp_token *token = cp_lexer_consume_token (parser->lexer);
23329 if (!cp_parser_objc_selector_p (token->type))
23331 error_at (token->location, "invalid Objective-C++ selector name");
23332 return error_mark_node;
23335 /* C++ operator names are allowed to appear in ObjC selectors. */
23336 switch (token->type)
23338 case CPP_AND_AND: return get_identifier ("and");
23339 case CPP_AND_EQ: return get_identifier ("and_eq");
23340 case CPP_AND: return get_identifier ("bitand");
23341 case CPP_OR: return get_identifier ("bitor");
23342 case CPP_COMPL: return get_identifier ("compl");
23343 case CPP_NOT: return get_identifier ("not");
23344 case CPP_NOT_EQ: return get_identifier ("not_eq");
23345 case CPP_OR_OR: return get_identifier ("or");
23346 case CPP_OR_EQ: return get_identifier ("or_eq");
23347 case CPP_XOR: return get_identifier ("xor");
23348 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23349 default: return token->u.value;
23353 /* Parse an Objective-C params list. */
23356 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23358 tree params = NULL_TREE;
23359 bool maybe_unary_selector_p = true;
23360 cp_token *token = cp_lexer_peek_token (parser->lexer);
23362 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23364 tree selector = NULL_TREE, type_name, identifier;
23365 tree parm_attr = NULL_TREE;
23367 if (token->keyword == RID_ATTRIBUTE)
23370 if (token->type != CPP_COLON)
23371 selector = cp_parser_objc_selector (parser);
23373 /* Detect if we have a unary selector. */
23374 if (maybe_unary_selector_p
23375 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23377 params = selector; /* Might be followed by attributes. */
23381 maybe_unary_selector_p = false;
23382 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23384 /* Something went quite wrong. There should be a colon
23385 here, but there is not. Stop parsing parameters. */
23388 type_name = cp_parser_objc_typename (parser);
23389 /* New ObjC allows attributes on parameters too. */
23390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23391 parm_attr = cp_parser_attributes_opt (parser);
23392 identifier = cp_parser_identifier (parser);
23396 objc_build_keyword_decl (selector,
23401 token = cp_lexer_peek_token (parser->lexer);
23404 if (params == NULL_TREE)
23406 cp_parser_error (parser, "objective-c++ method declaration is expected");
23407 return error_mark_node;
23410 /* We allow tail attributes for the method. */
23411 if (token->keyword == RID_ATTRIBUTE)
23413 *attributes = cp_parser_attributes_opt (parser);
23414 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23415 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23417 cp_parser_error (parser,
23418 "method attributes must be specified at the end");
23419 return error_mark_node;
23422 if (params == NULL_TREE)
23424 cp_parser_error (parser, "objective-c++ method declaration is expected");
23425 return error_mark_node;
23430 /* Parse the non-keyword Objective-C params. */
23433 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23436 tree params = make_node (TREE_LIST);
23437 cp_token *token = cp_lexer_peek_token (parser->lexer);
23438 *ellipsisp = false; /* Initially, assume no ellipsis. */
23440 while (token->type == CPP_COMMA)
23442 cp_parameter_declarator *parmdecl;
23445 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23446 token = cp_lexer_peek_token (parser->lexer);
23448 if (token->type == CPP_ELLIPSIS)
23450 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23452 token = cp_lexer_peek_token (parser->lexer);
23456 /* TODO: parse attributes for tail parameters. */
23457 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23458 parm = grokdeclarator (parmdecl->declarator,
23459 &parmdecl->decl_specifiers,
23460 PARM, /*initialized=*/0,
23461 /*attrlist=*/NULL);
23463 chainon (params, build_tree_list (NULL_TREE, parm));
23464 token = cp_lexer_peek_token (parser->lexer);
23467 /* We allow tail attributes for the method. */
23468 if (token->keyword == RID_ATTRIBUTE)
23470 if (*attributes == NULL_TREE)
23472 *attributes = cp_parser_attributes_opt (parser);
23473 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23474 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23478 /* We have an error, but parse the attributes, so that we can
23480 *attributes = cp_parser_attributes_opt (parser);
23482 cp_parser_error (parser,
23483 "method attributes must be specified at the end");
23484 return error_mark_node;
23490 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23493 cp_parser_objc_interstitial_code (cp_parser* parser)
23495 cp_token *token = cp_lexer_peek_token (parser->lexer);
23497 /* If the next token is `extern' and the following token is a string
23498 literal, then we have a linkage specification. */
23499 if (token->keyword == RID_EXTERN
23500 && cp_parser_is_pure_string_literal
23501 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23502 cp_parser_linkage_specification (parser);
23503 /* Handle #pragma, if any. */
23504 else if (token->type == CPP_PRAGMA)
23505 cp_parser_pragma (parser, pragma_external);
23506 /* Allow stray semicolons. */
23507 else if (token->type == CPP_SEMICOLON)
23508 cp_lexer_consume_token (parser->lexer);
23509 /* Mark methods as optional or required, when building protocols. */
23510 else if (token->keyword == RID_AT_OPTIONAL)
23512 cp_lexer_consume_token (parser->lexer);
23513 objc_set_method_opt (true);
23515 else if (token->keyword == RID_AT_REQUIRED)
23517 cp_lexer_consume_token (parser->lexer);
23518 objc_set_method_opt (false);
23520 else if (token->keyword == RID_NAMESPACE)
23521 cp_parser_namespace_definition (parser);
23522 /* Other stray characters must generate errors. */
23523 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23525 cp_lexer_consume_token (parser->lexer);
23526 error ("stray %qs between Objective-C++ methods",
23527 token->type == CPP_OPEN_BRACE ? "{" : "}");
23529 /* Finally, try to parse a block-declaration, or a function-definition. */
23531 cp_parser_block_declaration (parser, /*statement_p=*/false);
23534 /* Parse a method signature. */
23537 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23539 tree rettype, kwdparms, optparms;
23540 bool ellipsis = false;
23541 bool is_class_method;
23543 is_class_method = cp_parser_objc_method_type (parser);
23544 rettype = cp_parser_objc_typename (parser);
23545 *attributes = NULL_TREE;
23546 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23547 if (kwdparms == error_mark_node)
23548 return error_mark_node;
23549 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23550 if (optparms == error_mark_node)
23551 return error_mark_node;
23553 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23557 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23560 cp_lexer_save_tokens (parser->lexer);
23561 tattr = cp_parser_attributes_opt (parser);
23562 gcc_assert (tattr) ;
23564 /* If the attributes are followed by a method introducer, this is not allowed.
23565 Dump the attributes and flag the situation. */
23566 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23567 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23570 /* Otherwise, the attributes introduce some interstitial code, possibly so
23571 rewind to allow that check. */
23572 cp_lexer_rollback_tokens (parser->lexer);
23576 /* Parse an Objective-C method prototype list. */
23579 cp_parser_objc_method_prototype_list (cp_parser* parser)
23581 cp_token *token = cp_lexer_peek_token (parser->lexer);
23583 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23585 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23587 tree attributes, sig;
23588 bool is_class_method;
23589 if (token->type == CPP_PLUS)
23590 is_class_method = true;
23592 is_class_method = false;
23593 sig = cp_parser_objc_method_signature (parser, &attributes);
23594 if (sig == error_mark_node)
23596 cp_parser_skip_to_end_of_block_or_statement (parser);
23597 token = cp_lexer_peek_token (parser->lexer);
23600 objc_add_method_declaration (is_class_method, sig, attributes);
23601 cp_parser_consume_semicolon_at_end_of_statement (parser);
23603 else if (token->keyword == RID_AT_PROPERTY)
23604 cp_parser_objc_at_property_declaration (parser);
23605 else if (token->keyword == RID_ATTRIBUTE
23606 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23607 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23609 "prefix attributes are ignored for methods");
23611 /* Allow for interspersed non-ObjC++ code. */
23612 cp_parser_objc_interstitial_code (parser);
23614 token = cp_lexer_peek_token (parser->lexer);
23617 if (token->type != CPP_EOF)
23618 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23620 cp_parser_error (parser, "expected %<@end%>");
23622 objc_finish_interface ();
23625 /* Parse an Objective-C method definition list. */
23628 cp_parser_objc_method_definition_list (cp_parser* parser)
23630 cp_token *token = cp_lexer_peek_token (parser->lexer);
23632 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23636 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23639 tree sig, attribute;
23640 bool is_class_method;
23641 if (token->type == CPP_PLUS)
23642 is_class_method = true;
23644 is_class_method = false;
23645 push_deferring_access_checks (dk_deferred);
23646 sig = cp_parser_objc_method_signature (parser, &attribute);
23647 if (sig == error_mark_node)
23649 cp_parser_skip_to_end_of_block_or_statement (parser);
23650 token = cp_lexer_peek_token (parser->lexer);
23653 objc_start_method_definition (is_class_method, sig, attribute,
23656 /* For historical reasons, we accept an optional semicolon. */
23657 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23658 cp_lexer_consume_token (parser->lexer);
23660 ptk = cp_lexer_peek_token (parser->lexer);
23661 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23662 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23664 perform_deferred_access_checks ();
23665 stop_deferring_access_checks ();
23666 meth = cp_parser_function_definition_after_declarator (parser,
23668 pop_deferring_access_checks ();
23669 objc_finish_method_definition (meth);
23672 /* The following case will be removed once @synthesize is
23673 completely implemented. */
23674 else if (token->keyword == RID_AT_PROPERTY)
23675 cp_parser_objc_at_property_declaration (parser);
23676 else if (token->keyword == RID_AT_SYNTHESIZE)
23677 cp_parser_objc_at_synthesize_declaration (parser);
23678 else if (token->keyword == RID_AT_DYNAMIC)
23679 cp_parser_objc_at_dynamic_declaration (parser);
23680 else if (token->keyword == RID_ATTRIBUTE
23681 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23682 warning_at (token->location, OPT_Wattributes,
23683 "prefix attributes are ignored for methods");
23685 /* Allow for interspersed non-ObjC++ code. */
23686 cp_parser_objc_interstitial_code (parser);
23688 token = cp_lexer_peek_token (parser->lexer);
23691 if (token->type != CPP_EOF)
23692 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23694 cp_parser_error (parser, "expected %<@end%>");
23696 objc_finish_implementation ();
23699 /* Parse Objective-C ivars. */
23702 cp_parser_objc_class_ivars (cp_parser* parser)
23704 cp_token *token = cp_lexer_peek_token (parser->lexer);
23706 if (token->type != CPP_OPEN_BRACE)
23707 return; /* No ivars specified. */
23709 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
23710 token = cp_lexer_peek_token (parser->lexer);
23712 while (token->type != CPP_CLOSE_BRACE
23713 && token->keyword != RID_AT_END && token->type != CPP_EOF)
23715 cp_decl_specifier_seq declspecs;
23716 int decl_class_or_enum_p;
23717 tree prefix_attributes;
23719 cp_parser_objc_visibility_spec (parser);
23721 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23724 cp_parser_decl_specifier_seq (parser,
23725 CP_PARSER_FLAGS_OPTIONAL,
23727 &decl_class_or_enum_p);
23729 /* auto, register, static, extern, mutable. */
23730 if (declspecs.storage_class != sc_none)
23732 cp_parser_error (parser, "invalid type for instance variable");
23733 declspecs.storage_class = sc_none;
23737 if (declspecs.specs[(int) ds_thread])
23739 cp_parser_error (parser, "invalid type for instance variable");
23740 declspecs.specs[(int) ds_thread] = 0;
23744 if (declspecs.specs[(int) ds_typedef])
23746 cp_parser_error (parser, "invalid type for instance variable");
23747 declspecs.specs[(int) ds_typedef] = 0;
23750 prefix_attributes = declspecs.attributes;
23751 declspecs.attributes = NULL_TREE;
23753 /* Keep going until we hit the `;' at the end of the
23755 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23757 tree width = NULL_TREE, attributes, first_attribute, decl;
23758 cp_declarator *declarator = NULL;
23759 int ctor_dtor_or_conv_p;
23761 /* Check for a (possibly unnamed) bitfield declaration. */
23762 token = cp_lexer_peek_token (parser->lexer);
23763 if (token->type == CPP_COLON)
23766 if (token->type == CPP_NAME
23767 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23770 /* Get the name of the bitfield. */
23771 declarator = make_id_declarator (NULL_TREE,
23772 cp_parser_identifier (parser),
23776 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23777 /* Get the width of the bitfield. */
23779 = cp_parser_constant_expression (parser,
23780 /*allow_non_constant=*/false,
23785 /* Parse the declarator. */
23787 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23788 &ctor_dtor_or_conv_p,
23789 /*parenthesized_p=*/NULL,
23790 /*member_p=*/false);
23793 /* Look for attributes that apply to the ivar. */
23794 attributes = cp_parser_attributes_opt (parser);
23795 /* Remember which attributes are prefix attributes and
23797 first_attribute = attributes;
23798 /* Combine the attributes. */
23799 attributes = chainon (prefix_attributes, attributes);
23802 /* Create the bitfield declaration. */
23803 decl = grokbitfield (declarator, &declspecs,
23807 decl = grokfield (declarator, &declspecs,
23808 NULL_TREE, /*init_const_expr_p=*/false,
23809 NULL_TREE, attributes);
23811 /* Add the instance variable. */
23812 if (decl != error_mark_node && decl != NULL_TREE)
23813 objc_add_instance_variable (decl);
23815 /* Reset PREFIX_ATTRIBUTES. */
23816 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23817 attributes = TREE_CHAIN (attributes);
23819 TREE_CHAIN (attributes) = NULL_TREE;
23821 token = cp_lexer_peek_token (parser->lexer);
23823 if (token->type == CPP_COMMA)
23825 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23831 cp_parser_consume_semicolon_at_end_of_statement (parser);
23832 token = cp_lexer_peek_token (parser->lexer);
23835 if (token->keyword == RID_AT_END)
23836 cp_parser_error (parser, "expected %<}%>");
23838 /* Do not consume the RID_AT_END, so it will be read again as terminating
23839 the @interface of @implementation. */
23840 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23841 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
23843 /* For historical reasons, we accept an optional semicolon. */
23844 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23845 cp_lexer_consume_token (parser->lexer);
23848 /* Parse an Objective-C protocol declaration. */
23851 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23853 tree proto, protorefs;
23856 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23859 tok = cp_lexer_peek_token (parser->lexer);
23860 error_at (tok->location, "identifier expected after %<@protocol%>");
23861 cp_parser_consume_semicolon_at_end_of_statement (parser);
23865 /* See if we have a forward declaration or a definition. */
23866 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23868 /* Try a forward declaration first. */
23869 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23875 id = cp_parser_identifier (parser);
23876 if (id == error_mark_node)
23879 objc_declare_protocol (id, attributes);
23881 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23882 cp_lexer_consume_token (parser->lexer);
23886 cp_parser_consume_semicolon_at_end_of_statement (parser);
23889 /* Ok, we got a full-fledged definition (or at least should). */
23892 proto = cp_parser_identifier (parser);
23893 protorefs = cp_parser_objc_protocol_refs_opt (parser);
23894 objc_start_protocol (proto, protorefs, attributes);
23895 cp_parser_objc_method_prototype_list (parser);
23899 /* Parse an Objective-C superclass or category. */
23902 cp_parser_objc_superclass_or_category (cp_parser *parser,
23905 tree *categ, bool *is_class_extension)
23907 cp_token *next = cp_lexer_peek_token (parser->lexer);
23909 *super = *categ = NULL_TREE;
23910 *is_class_extension = false;
23911 if (next->type == CPP_COLON)
23913 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23914 *super = cp_parser_identifier (parser);
23916 else if (next->type == CPP_OPEN_PAREN)
23918 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23920 /* If there is no category name, and this is an @interface, we
23921 have a class extension. */
23922 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23924 *categ = NULL_TREE;
23925 *is_class_extension = true;
23928 *categ = cp_parser_identifier (parser);
23930 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23934 /* Parse an Objective-C class interface. */
23937 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23939 tree name, super, categ, protos;
23940 bool is_class_extension;
23942 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
23943 name = cp_parser_identifier (parser);
23944 if (name == error_mark_node)
23946 /* It's hard to recover because even if valid @interface stuff
23947 is to follow, we can't compile it (or validate it) if we
23948 don't even know which class it refers to. Let's assume this
23949 was a stray '@interface' token in the stream and skip it.
23953 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23954 &is_class_extension);
23955 protos = cp_parser_objc_protocol_refs_opt (parser);
23957 /* We have either a class or a category on our hands. */
23958 if (categ || is_class_extension)
23959 objc_start_category_interface (name, categ, protos, attributes);
23962 objc_start_class_interface (name, super, protos, attributes);
23963 /* Handle instance variable declarations, if any. */
23964 cp_parser_objc_class_ivars (parser);
23965 objc_continue_interface ();
23968 cp_parser_objc_method_prototype_list (parser);
23971 /* Parse an Objective-C class implementation. */
23974 cp_parser_objc_class_implementation (cp_parser* parser)
23976 tree name, super, categ;
23977 bool is_class_extension;
23979 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
23980 name = cp_parser_identifier (parser);
23981 if (name == error_mark_node)
23983 /* It's hard to recover because even if valid @implementation
23984 stuff is to follow, we can't compile it (or validate it) if
23985 we don't even know which class it refers to. Let's assume
23986 this was a stray '@implementation' token in the stream and
23991 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23992 &is_class_extension);
23994 /* We have either a class or a category on our hands. */
23996 objc_start_category_implementation (name, categ);
23999 objc_start_class_implementation (name, super);
24000 /* Handle instance variable declarations, if any. */
24001 cp_parser_objc_class_ivars (parser);
24002 objc_continue_implementation ();
24005 cp_parser_objc_method_definition_list (parser);
24008 /* Consume the @end token and finish off the implementation. */
24011 cp_parser_objc_end_implementation (cp_parser* parser)
24013 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24014 objc_finish_implementation ();
24017 /* Parse an Objective-C declaration. */
24020 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24022 /* Try to figure out what kind of declaration is present. */
24023 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24026 switch (kwd->keyword)
24031 error_at (kwd->location, "attributes may not be specified before"
24032 " the %<@%D%> Objective-C++ keyword",
24036 case RID_AT_IMPLEMENTATION:
24037 warning_at (kwd->location, OPT_Wattributes,
24038 "prefix attributes are ignored before %<@%D%>",
24045 switch (kwd->keyword)
24048 cp_parser_objc_alias_declaration (parser);
24051 cp_parser_objc_class_declaration (parser);
24053 case RID_AT_PROTOCOL:
24054 cp_parser_objc_protocol_declaration (parser, attributes);
24056 case RID_AT_INTERFACE:
24057 cp_parser_objc_class_interface (parser, attributes);
24059 case RID_AT_IMPLEMENTATION:
24060 cp_parser_objc_class_implementation (parser);
24063 cp_parser_objc_end_implementation (parser);
24066 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24068 cp_parser_skip_to_end_of_block_or_statement (parser);
24072 /* Parse an Objective-C try-catch-finally statement.
24074 objc-try-catch-finally-stmt:
24075 @try compound-statement objc-catch-clause-seq [opt]
24076 objc-finally-clause [opt]
24078 objc-catch-clause-seq:
24079 objc-catch-clause objc-catch-clause-seq [opt]
24082 @catch ( objc-exception-declaration ) compound-statement
24084 objc-finally-clause:
24085 @finally compound-statement
24087 objc-exception-declaration:
24088 parameter-declaration
24091 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24095 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24096 for C. Keep them in sync. */
24099 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24101 location_t location;
24104 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24105 location = cp_lexer_peek_token (parser->lexer)->location;
24106 objc_maybe_warn_exceptions (location);
24107 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24108 node, lest it get absorbed into the surrounding block. */
24109 stmt = push_stmt_list ();
24110 cp_parser_compound_statement (parser, NULL, false, false);
24111 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24113 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24115 cp_parameter_declarator *parm;
24116 tree parameter_declaration = error_mark_node;
24117 bool seen_open_paren = false;
24119 cp_lexer_consume_token (parser->lexer);
24120 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24121 seen_open_paren = true;
24122 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24124 /* We have "@catch (...)" (where the '...' are literally
24125 what is in the code). Skip the '...'.
24126 parameter_declaration is set to NULL_TREE, and
24127 objc_being_catch_clauses() knows that that means
24129 cp_lexer_consume_token (parser->lexer);
24130 parameter_declaration = NULL_TREE;
24134 /* We have "@catch (NSException *exception)" or something
24135 like that. Parse the parameter declaration. */
24136 parm = cp_parser_parameter_declaration (parser, false, NULL);
24138 parameter_declaration = error_mark_node;
24140 parameter_declaration = grokdeclarator (parm->declarator,
24141 &parm->decl_specifiers,
24142 PARM, /*initialized=*/0,
24143 /*attrlist=*/NULL);
24145 if (seen_open_paren)
24146 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24149 /* If there was no open parenthesis, we are recovering from
24150 an error, and we are trying to figure out what mistake
24151 the user has made. */
24153 /* If there is an immediate closing parenthesis, the user
24154 probably forgot the opening one (ie, they typed "@catch
24155 NSException *e)". Parse the closing parenthesis and keep
24157 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24158 cp_lexer_consume_token (parser->lexer);
24160 /* If these is no immediate closing parenthesis, the user
24161 probably doesn't know that parenthesis are required at
24162 all (ie, they typed "@catch NSException *e"). So, just
24163 forget about the closing parenthesis and keep going. */
24165 objc_begin_catch_clause (parameter_declaration);
24166 cp_parser_compound_statement (parser, NULL, false, false);
24167 objc_finish_catch_clause ();
24169 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24171 cp_lexer_consume_token (parser->lexer);
24172 location = cp_lexer_peek_token (parser->lexer)->location;
24173 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24174 node, lest it get absorbed into the surrounding block. */
24175 stmt = push_stmt_list ();
24176 cp_parser_compound_statement (parser, NULL, false, false);
24177 objc_build_finally_clause (location, pop_stmt_list (stmt));
24180 return objc_finish_try_stmt ();
24183 /* Parse an Objective-C synchronized statement.
24185 objc-synchronized-stmt:
24186 @synchronized ( expression ) compound-statement
24188 Returns NULL_TREE. */
24191 cp_parser_objc_synchronized_statement (cp_parser *parser)
24193 location_t location;
24196 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24198 location = cp_lexer_peek_token (parser->lexer)->location;
24199 objc_maybe_warn_exceptions (location);
24200 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24201 lock = cp_parser_expression (parser, false, NULL);
24202 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24204 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24205 node, lest it get absorbed into the surrounding block. */
24206 stmt = push_stmt_list ();
24207 cp_parser_compound_statement (parser, NULL, false, false);
24209 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24212 /* Parse an Objective-C throw statement.
24215 @throw assignment-expression [opt] ;
24217 Returns a constructed '@throw' statement. */
24220 cp_parser_objc_throw_statement (cp_parser *parser)
24222 tree expr = NULL_TREE;
24223 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24225 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24228 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24230 cp_parser_consume_semicolon_at_end_of_statement (parser);
24232 return objc_build_throw_stmt (loc, expr);
24235 /* Parse an Objective-C statement. */
24238 cp_parser_objc_statement (cp_parser * parser)
24240 /* Try to figure out what kind of declaration is present. */
24241 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24243 switch (kwd->keyword)
24246 return cp_parser_objc_try_catch_finally_statement (parser);
24247 case RID_AT_SYNCHRONIZED:
24248 return cp_parser_objc_synchronized_statement (parser);
24250 return cp_parser_objc_throw_statement (parser);
24252 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24254 cp_parser_skip_to_end_of_block_or_statement (parser);
24257 return error_mark_node;
24260 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24261 look ahead to see if an objc keyword follows the attributes. This
24262 is to detect the use of prefix attributes on ObjC @interface and
24266 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24268 cp_lexer_save_tokens (parser->lexer);
24269 *attrib = cp_parser_attributes_opt (parser);
24270 gcc_assert (*attrib);
24271 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24273 cp_lexer_commit_tokens (parser->lexer);
24276 cp_lexer_rollback_tokens (parser->lexer);
24280 /* This routine is a minimal replacement for
24281 c_parser_struct_declaration () used when parsing the list of
24282 types/names or ObjC++ properties. For example, when parsing the
24285 @property (readonly) int a, b, c;
24287 this function is responsible for parsing "int a, int b, int c" and
24288 returning the declarations as CHAIN of DECLs.
24290 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24291 similar parsing. */
24293 cp_parser_objc_struct_declaration (cp_parser *parser)
24295 tree decls = NULL_TREE;
24296 cp_decl_specifier_seq declspecs;
24297 int decl_class_or_enum_p;
24298 tree prefix_attributes;
24300 cp_parser_decl_specifier_seq (parser,
24301 CP_PARSER_FLAGS_NONE,
24303 &decl_class_or_enum_p);
24305 if (declspecs.type == error_mark_node)
24306 return error_mark_node;
24308 /* auto, register, static, extern, mutable. */
24309 if (declspecs.storage_class != sc_none)
24311 cp_parser_error (parser, "invalid type for property");
24312 declspecs.storage_class = sc_none;
24316 if (declspecs.specs[(int) ds_thread])
24318 cp_parser_error (parser, "invalid type for property");
24319 declspecs.specs[(int) ds_thread] = 0;
24323 if (declspecs.specs[(int) ds_typedef])
24325 cp_parser_error (parser, "invalid type for property");
24326 declspecs.specs[(int) ds_typedef] = 0;
24329 prefix_attributes = declspecs.attributes;
24330 declspecs.attributes = NULL_TREE;
24332 /* Keep going until we hit the `;' at the end of the declaration. */
24333 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24335 tree attributes, first_attribute, decl;
24336 cp_declarator *declarator;
24339 /* Parse the declarator. */
24340 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24341 NULL, NULL, false);
24343 /* Look for attributes that apply to the ivar. */
24344 attributes = cp_parser_attributes_opt (parser);
24345 /* Remember which attributes are prefix attributes and
24347 first_attribute = attributes;
24348 /* Combine the attributes. */
24349 attributes = chainon (prefix_attributes, attributes);
24351 decl = grokfield (declarator, &declspecs,
24352 NULL_TREE, /*init_const_expr_p=*/false,
24353 NULL_TREE, attributes);
24355 if (decl == error_mark_node || decl == NULL_TREE)
24356 return error_mark_node;
24358 /* Reset PREFIX_ATTRIBUTES. */
24359 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24360 attributes = TREE_CHAIN (attributes);
24362 TREE_CHAIN (attributes) = NULL_TREE;
24364 DECL_CHAIN (decl) = decls;
24367 token = cp_lexer_peek_token (parser->lexer);
24368 if (token->type == CPP_COMMA)
24370 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24379 /* Parse an Objective-C @property declaration. The syntax is:
24381 objc-property-declaration:
24382 '@property' objc-property-attributes[opt] struct-declaration ;
24384 objc-property-attributes:
24385 '(' objc-property-attribute-list ')'
24387 objc-property-attribute-list:
24388 objc-property-attribute
24389 objc-property-attribute-list, objc-property-attribute
24391 objc-property-attribute
24392 'getter' = identifier
24393 'setter' = identifier
24402 @property NSString *name;
24403 @property (readonly) id object;
24404 @property (retain, nonatomic, getter=getTheName) id name;
24405 @property int a, b, c;
24407 PS: This function is identical to
24408 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24410 cp_parser_objc_at_property_declaration (cp_parser *parser)
24412 /* The following variables hold the attributes of the properties as
24413 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24414 seen. When we see an attribute, we set them to 'true' (if they
24415 are boolean properties) or to the identifier (if they have an
24416 argument, ie, for getter and setter). Note that here we only
24417 parse the list of attributes, check the syntax and accumulate the
24418 attributes that we find. objc_add_property_declaration() will
24419 then process the information. */
24420 bool property_assign = false;
24421 bool property_copy = false;
24422 tree property_getter_ident = NULL_TREE;
24423 bool property_nonatomic = false;
24424 bool property_readonly = false;
24425 bool property_readwrite = false;
24426 bool property_retain = false;
24427 tree property_setter_ident = NULL_TREE;
24429 /* 'properties' is the list of properties that we read. Usually a
24430 single one, but maybe more (eg, in "@property int a, b, c;" there
24435 loc = cp_lexer_peek_token (parser->lexer)->location;
24437 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24439 /* Parse the optional attribute list... */
24440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24443 cp_lexer_consume_token (parser->lexer);
24447 bool syntax_error = false;
24448 cp_token *token = cp_lexer_peek_token (parser->lexer);
24451 if (token->type != CPP_NAME)
24453 cp_parser_error (parser, "expected identifier");
24456 keyword = C_RID_CODE (token->u.value);
24457 cp_lexer_consume_token (parser->lexer);
24460 case RID_ASSIGN: property_assign = true; break;
24461 case RID_COPY: property_copy = true; break;
24462 case RID_NONATOMIC: property_nonatomic = true; break;
24463 case RID_READONLY: property_readonly = true; break;
24464 case RID_READWRITE: property_readwrite = true; break;
24465 case RID_RETAIN: property_retain = true; break;
24469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24471 if (keyword == RID_GETTER)
24472 cp_parser_error (parser,
24473 "missing %<=%> (after %<getter%> attribute)");
24475 cp_parser_error (parser,
24476 "missing %<=%> (after %<setter%> attribute)");
24477 syntax_error = true;
24480 cp_lexer_consume_token (parser->lexer); /* eat the = */
24481 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24483 cp_parser_error (parser, "expected identifier");
24484 syntax_error = true;
24487 if (keyword == RID_SETTER)
24489 if (property_setter_ident != NULL_TREE)
24491 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24492 cp_lexer_consume_token (parser->lexer);
24495 property_setter_ident = cp_parser_objc_selector (parser);
24496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24497 cp_parser_error (parser, "setter name must terminate with %<:%>");
24499 cp_lexer_consume_token (parser->lexer);
24503 if (property_getter_ident != NULL_TREE)
24505 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24506 cp_lexer_consume_token (parser->lexer);
24509 property_getter_ident = cp_parser_objc_selector (parser);
24513 cp_parser_error (parser, "unknown property attribute");
24514 syntax_error = true;
24521 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24522 cp_lexer_consume_token (parser->lexer);
24527 /* FIXME: "@property (setter, assign);" will generate a spurious
24528 "error: expected ‘)’ before ‘,’ token". This is because
24529 cp_parser_require, unlike the C counterpart, will produce an
24530 error even if we are in error recovery. */
24531 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24533 cp_parser_skip_to_closing_parenthesis (parser,
24534 /*recovering=*/true,
24535 /*or_comma=*/false,
24536 /*consume_paren=*/true);
24540 /* ... and the property declaration(s). */
24541 properties = cp_parser_objc_struct_declaration (parser);
24543 if (properties == error_mark_node)
24545 cp_parser_skip_to_end_of_statement (parser);
24546 /* If the next token is now a `;', consume it. */
24547 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24548 cp_lexer_consume_token (parser->lexer);
24552 if (properties == NULL_TREE)
24553 cp_parser_error (parser, "expected identifier");
24556 /* Comma-separated properties are chained together in
24557 reverse order; add them one by one. */
24558 properties = nreverse (properties);
24560 for (; properties; properties = TREE_CHAIN (properties))
24561 objc_add_property_declaration (loc, copy_node (properties),
24562 property_readonly, property_readwrite,
24563 property_assign, property_retain,
24564 property_copy, property_nonatomic,
24565 property_getter_ident, property_setter_ident);
24568 cp_parser_consume_semicolon_at_end_of_statement (parser);
24571 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24573 objc-synthesize-declaration:
24574 @synthesize objc-synthesize-identifier-list ;
24576 objc-synthesize-identifier-list:
24577 objc-synthesize-identifier
24578 objc-synthesize-identifier-list, objc-synthesize-identifier
24580 objc-synthesize-identifier
24582 identifier = identifier
24585 @synthesize MyProperty;
24586 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24588 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24589 for C. Keep them in sync.
24592 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24594 tree list = NULL_TREE;
24596 loc = cp_lexer_peek_token (parser->lexer)->location;
24598 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24601 tree property, ivar;
24602 property = cp_parser_identifier (parser);
24603 if (property == error_mark_node)
24605 cp_parser_consume_semicolon_at_end_of_statement (parser);
24608 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24610 cp_lexer_consume_token (parser->lexer);
24611 ivar = cp_parser_identifier (parser);
24612 if (ivar == error_mark_node)
24614 cp_parser_consume_semicolon_at_end_of_statement (parser);
24620 list = chainon (list, build_tree_list (ivar, property));
24621 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24622 cp_lexer_consume_token (parser->lexer);
24626 cp_parser_consume_semicolon_at_end_of_statement (parser);
24627 objc_add_synthesize_declaration (loc, list);
24630 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24632 objc-dynamic-declaration:
24633 @dynamic identifier-list ;
24636 @dynamic MyProperty;
24637 @dynamic MyProperty, AnotherProperty;
24639 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24640 for C. Keep them in sync.
24643 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24645 tree list = NULL_TREE;
24647 loc = cp_lexer_peek_token (parser->lexer)->location;
24649 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24653 property = cp_parser_identifier (parser);
24654 if (property == error_mark_node)
24656 cp_parser_consume_semicolon_at_end_of_statement (parser);
24659 list = chainon (list, build_tree_list (NULL, property));
24660 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24661 cp_lexer_consume_token (parser->lexer);
24665 cp_parser_consume_semicolon_at_end_of_statement (parser);
24666 objc_add_dynamic_declaration (loc, list);
24670 /* OpenMP 2.5 parsing routines. */
24672 /* Returns name of the next clause.
24673 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24674 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24675 returned and the token is consumed. */
24677 static pragma_omp_clause
24678 cp_parser_omp_clause_name (cp_parser *parser)
24680 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24683 result = PRAGMA_OMP_CLAUSE_IF;
24684 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24685 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24686 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24687 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24688 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24690 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24691 const char *p = IDENTIFIER_POINTER (id);
24696 if (!strcmp ("collapse", p))
24697 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24698 else if (!strcmp ("copyin", p))
24699 result = PRAGMA_OMP_CLAUSE_COPYIN;
24700 else if (!strcmp ("copyprivate", p))
24701 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24704 if (!strcmp ("final", p))
24705 result = PRAGMA_OMP_CLAUSE_FINAL;
24706 else if (!strcmp ("firstprivate", p))
24707 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24710 if (!strcmp ("lastprivate", p))
24711 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24714 if (!strcmp ("mergeable", p))
24715 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24718 if (!strcmp ("nowait", p))
24719 result = PRAGMA_OMP_CLAUSE_NOWAIT;
24720 else if (!strcmp ("num_threads", p))
24721 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24724 if (!strcmp ("ordered", p))
24725 result = PRAGMA_OMP_CLAUSE_ORDERED;
24728 if (!strcmp ("reduction", p))
24729 result = PRAGMA_OMP_CLAUSE_REDUCTION;
24732 if (!strcmp ("schedule", p))
24733 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24734 else if (!strcmp ("shared", p))
24735 result = PRAGMA_OMP_CLAUSE_SHARED;
24738 if (!strcmp ("untied", p))
24739 result = PRAGMA_OMP_CLAUSE_UNTIED;
24744 if (result != PRAGMA_OMP_CLAUSE_NONE)
24745 cp_lexer_consume_token (parser->lexer);
24750 /* Validate that a clause of the given type does not already exist. */
24753 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24754 const char *name, location_t location)
24758 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24759 if (OMP_CLAUSE_CODE (c) == code)
24761 error_at (location, "too many %qs clauses", name);
24769 variable-list , identifier
24771 In addition, we match a closing parenthesis. An opening parenthesis
24772 will have been consumed by the caller.
24774 If KIND is nonzero, create the appropriate node and install the decl
24775 in OMP_CLAUSE_DECL and add the node to the head of the list.
24777 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24778 return the list created. */
24781 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24789 token = cp_lexer_peek_token (parser->lexer);
24790 name = cp_parser_id_expression (parser, /*template_p=*/false,
24791 /*check_dependency_p=*/true,
24792 /*template_p=*/NULL,
24793 /*declarator_p=*/false,
24794 /*optional_p=*/false);
24795 if (name == error_mark_node)
24798 decl = cp_parser_lookup_name_simple (parser, name, token->location);
24799 if (decl == error_mark_node)
24800 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24802 else if (kind != 0)
24804 tree u = build_omp_clause (token->location, kind);
24805 OMP_CLAUSE_DECL (u) = decl;
24806 OMP_CLAUSE_CHAIN (u) = list;
24810 list = tree_cons (decl, NULL_TREE, list);
24813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24815 cp_lexer_consume_token (parser->lexer);
24818 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24822 /* Try to resync to an unnested comma. Copied from
24823 cp_parser_parenthesized_expression_list. */
24825 ending = cp_parser_skip_to_closing_parenthesis (parser,
24826 /*recovering=*/true,
24828 /*consume_paren=*/true);
24836 /* Similarly, but expect leading and trailing parenthesis. This is a very
24837 common case for omp clauses. */
24840 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24842 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24843 return cp_parser_omp_var_list_no_open (parser, kind, list);
24848 collapse ( constant-expression ) */
24851 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24857 loc = cp_lexer_peek_token (parser->lexer)->location;
24858 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24861 num = cp_parser_constant_expression (parser, false, NULL);
24863 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24864 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24865 /*or_comma=*/false,
24866 /*consume_paren=*/true);
24868 if (num == error_mark_node)
24870 num = fold_non_dependent_expr (num);
24871 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24872 || !host_integerp (num, 0)
24873 || (n = tree_low_cst (num, 0)) <= 0
24876 error_at (loc, "collapse argument needs positive constant integer expression");
24880 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24881 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24882 OMP_CLAUSE_CHAIN (c) = list;
24883 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24889 default ( shared | none ) */
24892 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24894 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24897 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24899 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24901 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24902 const char *p = IDENTIFIER_POINTER (id);
24907 if (strcmp ("none", p) != 0)
24909 kind = OMP_CLAUSE_DEFAULT_NONE;
24913 if (strcmp ("shared", p) != 0)
24915 kind = OMP_CLAUSE_DEFAULT_SHARED;
24922 cp_lexer_consume_token (parser->lexer);
24927 cp_parser_error (parser, "expected %<none%> or %<shared%>");
24930 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24931 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24932 /*or_comma=*/false,
24933 /*consume_paren=*/true);
24935 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24938 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24939 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24940 OMP_CLAUSE_CHAIN (c) = list;
24941 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24947 final ( expression ) */
24950 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24954 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24957 t = cp_parser_condition (parser);
24959 if (t == error_mark_node
24960 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24961 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24962 /*or_comma=*/false,
24963 /*consume_paren=*/true);
24965 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24967 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24968 OMP_CLAUSE_FINAL_EXPR (c) = t;
24969 OMP_CLAUSE_CHAIN (c) = list;
24975 if ( expression ) */
24978 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24982 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24985 t = cp_parser_condition (parser);
24987 if (t == error_mark_node
24988 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24989 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24990 /*or_comma=*/false,
24991 /*consume_paren=*/true);
24993 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24995 c = build_omp_clause (location, OMP_CLAUSE_IF);
24996 OMP_CLAUSE_IF_EXPR (c) = t;
24997 OMP_CLAUSE_CHAIN (c) = list;
25006 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25007 tree list, location_t location)
25011 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25014 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25015 OMP_CLAUSE_CHAIN (c) = list;
25023 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25024 tree list, location_t location)
25028 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25030 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25031 OMP_CLAUSE_CHAIN (c) = list;
25036 num_threads ( expression ) */
25039 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25040 location_t location)
25044 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25047 t = cp_parser_expression (parser, false, NULL);
25049 if (t == error_mark_node
25050 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25051 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25052 /*or_comma=*/false,
25053 /*consume_paren=*/true);
25055 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25056 "num_threads", location);
25058 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25059 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25060 OMP_CLAUSE_CHAIN (c) = list;
25069 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25070 tree list, location_t location)
25074 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25075 "ordered", location);
25077 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25078 OMP_CLAUSE_CHAIN (c) = list;
25083 reduction ( reduction-operator : variable-list )
25085 reduction-operator:
25086 One of: + * - & ^ | && ||
25090 reduction-operator:
25091 One of: + * - & ^ | && || min max */
25094 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25096 enum tree_code code;
25099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25102 switch (cp_lexer_peek_token (parser->lexer)->type)
25114 code = BIT_AND_EXPR;
25117 code = BIT_XOR_EXPR;
25120 code = BIT_IOR_EXPR;
25123 code = TRUTH_ANDIF_EXPR;
25126 code = TRUTH_ORIF_EXPR;
25130 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25131 const char *p = IDENTIFIER_POINTER (id);
25133 if (strcmp (p, "min") == 0)
25138 if (strcmp (p, "max") == 0)
25146 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25147 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25149 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25150 /*or_comma=*/false,
25151 /*consume_paren=*/true);
25154 cp_lexer_consume_token (parser->lexer);
25156 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25159 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25160 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25161 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25167 schedule ( schedule-kind )
25168 schedule ( schedule-kind , expression )
25171 static | dynamic | guided | runtime | auto */
25174 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25178 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25181 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25183 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25185 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25186 const char *p = IDENTIFIER_POINTER (id);
25191 if (strcmp ("dynamic", p) != 0)
25193 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25197 if (strcmp ("guided", p) != 0)
25199 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25203 if (strcmp ("runtime", p) != 0)
25205 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25212 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25213 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25214 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25215 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25218 cp_lexer_consume_token (parser->lexer);
25220 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25223 cp_lexer_consume_token (parser->lexer);
25225 token = cp_lexer_peek_token (parser->lexer);
25226 t = cp_parser_assignment_expression (parser, false, NULL);
25228 if (t == error_mark_node)
25230 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25231 error_at (token->location, "schedule %<runtime%> does not take "
25232 "a %<chunk_size%> parameter");
25233 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25234 error_at (token->location, "schedule %<auto%> does not take "
25235 "a %<chunk_size%> parameter");
25237 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25239 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25242 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25245 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25246 OMP_CLAUSE_CHAIN (c) = list;
25250 cp_parser_error (parser, "invalid schedule kind");
25252 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25253 /*or_comma=*/false,
25254 /*consume_paren=*/true);
25262 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25263 tree list, location_t location)
25267 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25269 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25270 OMP_CLAUSE_CHAIN (c) = list;
25274 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25275 is a bitmask in MASK. Return the list of clauses found; the result
25276 of clause default goes in *pdefault. */
25279 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25280 const char *where, cp_token *pragma_tok)
25282 tree clauses = NULL;
25284 cp_token *token = NULL;
25286 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25288 pragma_omp_clause c_kind;
25289 const char *c_name;
25290 tree prev = clauses;
25292 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25293 cp_lexer_consume_token (parser->lexer);
25295 token = cp_lexer_peek_token (parser->lexer);
25296 c_kind = cp_parser_omp_clause_name (parser);
25301 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25302 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25304 c_name = "collapse";
25306 case PRAGMA_OMP_CLAUSE_COPYIN:
25307 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25310 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25313 c_name = "copyprivate";
25315 case PRAGMA_OMP_CLAUSE_DEFAULT:
25316 clauses = cp_parser_omp_clause_default (parser, clauses,
25318 c_name = "default";
25320 case PRAGMA_OMP_CLAUSE_FINAL:
25321 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25324 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25325 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25327 c_name = "firstprivate";
25329 case PRAGMA_OMP_CLAUSE_IF:
25330 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25333 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25334 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25336 c_name = "lastprivate";
25338 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25339 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25341 c_name = "mergeable";
25343 case PRAGMA_OMP_CLAUSE_NOWAIT:
25344 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25347 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25348 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25350 c_name = "num_threads";
25352 case PRAGMA_OMP_CLAUSE_ORDERED:
25353 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25355 c_name = "ordered";
25357 case PRAGMA_OMP_CLAUSE_PRIVATE:
25358 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25360 c_name = "private";
25362 case PRAGMA_OMP_CLAUSE_REDUCTION:
25363 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25364 c_name = "reduction";
25366 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25367 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25369 c_name = "schedule";
25371 case PRAGMA_OMP_CLAUSE_SHARED:
25372 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25376 case PRAGMA_OMP_CLAUSE_UNTIED:
25377 clauses = cp_parser_omp_clause_untied (parser, clauses,
25382 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25386 if (((mask >> c_kind) & 1) == 0)
25388 /* Remove the invalid clause(s) from the list to avoid
25389 confusing the rest of the compiler. */
25391 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25395 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25396 return finish_omp_clauses (clauses);
25403 In practice, we're also interested in adding the statement to an
25404 outer node. So it is convenient if we work around the fact that
25405 cp_parser_statement calls add_stmt. */
25408 cp_parser_begin_omp_structured_block (cp_parser *parser)
25410 unsigned save = parser->in_statement;
25412 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25413 This preserves the "not within loop or switch" style error messages
25414 for nonsense cases like
25420 if (parser->in_statement)
25421 parser->in_statement = IN_OMP_BLOCK;
25427 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25429 parser->in_statement = save;
25433 cp_parser_omp_structured_block (cp_parser *parser)
25435 tree stmt = begin_omp_structured_block ();
25436 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25438 cp_parser_statement (parser, NULL_TREE, false, NULL);
25440 cp_parser_end_omp_structured_block (parser, save);
25441 return finish_omp_structured_block (stmt);
25445 # pragma omp atomic new-line
25449 x binop= expr | x++ | ++x | x-- | --x
25451 +, *, -, /, &, ^, |, <<, >>
25453 where x is an lvalue expression with scalar type.
25456 # pragma omp atomic new-line
25459 # pragma omp atomic read new-line
25462 # pragma omp atomic write new-line
25465 # pragma omp atomic update new-line
25468 # pragma omp atomic capture new-line
25471 # pragma omp atomic capture new-line
25479 expression-stmt | x = x binop expr
25481 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25483 { v = x; update-stmt; } | { update-stmt; v = x; }
25485 where x and v are lvalue expressions with scalar type. */
25488 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25490 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25491 tree rhs1 = NULL_TREE, orig_lhs;
25492 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25493 bool structured_block = false;
25495 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25497 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25498 const char *p = IDENTIFIER_POINTER (id);
25500 if (!strcmp (p, "read"))
25501 code = OMP_ATOMIC_READ;
25502 else if (!strcmp (p, "write"))
25504 else if (!strcmp (p, "update"))
25506 else if (!strcmp (p, "capture"))
25507 code = OMP_ATOMIC_CAPTURE_NEW;
25511 cp_lexer_consume_token (parser->lexer);
25513 cp_parser_require_pragma_eol (parser, pragma_tok);
25517 case OMP_ATOMIC_READ:
25518 case NOP_EXPR: /* atomic write */
25519 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25520 /*cast_p=*/false, NULL);
25521 if (v == error_mark_node)
25523 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25525 if (code == NOP_EXPR)
25526 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25528 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25529 /*cast_p=*/false, NULL);
25530 if (lhs == error_mark_node)
25532 if (code == NOP_EXPR)
25534 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25542 case OMP_ATOMIC_CAPTURE_NEW:
25543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25545 cp_lexer_consume_token (parser->lexer);
25546 structured_block = true;
25550 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25551 /*cast_p=*/false, NULL);
25552 if (v == error_mark_node)
25554 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25562 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25563 /*cast_p=*/false, NULL);
25565 switch (TREE_CODE (lhs))
25570 case POSTINCREMENT_EXPR:
25571 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25572 code = OMP_ATOMIC_CAPTURE_OLD;
25574 case PREINCREMENT_EXPR:
25575 lhs = TREE_OPERAND (lhs, 0);
25576 opcode = PLUS_EXPR;
25577 rhs = integer_one_node;
25580 case POSTDECREMENT_EXPR:
25581 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25582 code = OMP_ATOMIC_CAPTURE_OLD;
25584 case PREDECREMENT_EXPR:
25585 lhs = TREE_OPERAND (lhs, 0);
25586 opcode = MINUS_EXPR;
25587 rhs = integer_one_node;
25590 case COMPOUND_EXPR:
25591 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25592 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25593 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25594 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25595 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25596 (TREE_OPERAND (lhs, 1), 0), 0)))
25598 /* Undo effects of boolean_increment for post {in,de}crement. */
25599 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25602 if (TREE_CODE (lhs) == MODIFY_EXPR
25603 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25605 /* Undo effects of boolean_increment. */
25606 if (integer_onep (TREE_OPERAND (lhs, 1)))
25608 /* This is pre or post increment. */
25609 rhs = TREE_OPERAND (lhs, 1);
25610 lhs = TREE_OPERAND (lhs, 0);
25612 if (code == OMP_ATOMIC_CAPTURE_NEW
25613 && !structured_block
25614 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25615 code = OMP_ATOMIC_CAPTURE_OLD;
25621 switch (cp_lexer_peek_token (parser->lexer)->type)
25624 opcode = MULT_EXPR;
25627 opcode = TRUNC_DIV_EXPR;
25630 opcode = PLUS_EXPR;
25633 opcode = MINUS_EXPR;
25635 case CPP_LSHIFT_EQ:
25636 opcode = LSHIFT_EXPR;
25638 case CPP_RSHIFT_EQ:
25639 opcode = RSHIFT_EXPR;
25642 opcode = BIT_AND_EXPR;
25645 opcode = BIT_IOR_EXPR;
25648 opcode = BIT_XOR_EXPR;
25651 if (structured_block || code == OMP_ATOMIC)
25653 enum cp_parser_prec oprec;
25655 cp_lexer_consume_token (parser->lexer);
25656 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25657 /*cast_p=*/false, NULL);
25658 if (rhs1 == error_mark_node)
25660 token = cp_lexer_peek_token (parser->lexer);
25661 switch (token->type)
25663 case CPP_SEMICOLON:
25664 if (code == OMP_ATOMIC_CAPTURE_NEW)
25666 code = OMP_ATOMIC_CAPTURE_OLD;
25671 cp_lexer_consume_token (parser->lexer);
25674 cp_parser_error (parser,
25675 "invalid form of %<#pragma omp atomic%>");
25678 opcode = MULT_EXPR;
25681 opcode = TRUNC_DIV_EXPR;
25684 opcode = PLUS_EXPR;
25687 opcode = MINUS_EXPR;
25690 opcode = LSHIFT_EXPR;
25693 opcode = RSHIFT_EXPR;
25696 opcode = BIT_AND_EXPR;
25699 opcode = BIT_IOR_EXPR;
25702 opcode = BIT_XOR_EXPR;
25705 cp_parser_error (parser,
25706 "invalid operator for %<#pragma omp atomic%>");
25709 oprec = TOKEN_PRECEDENCE (token);
25710 gcc_assert (oprec != PREC_NOT_OPERATOR);
25711 if (commutative_tree_code (opcode))
25712 oprec = (enum cp_parser_prec) (oprec - 1);
25713 cp_lexer_consume_token (parser->lexer);
25714 rhs = cp_parser_binary_expression (parser, false, false,
25716 if (rhs == error_mark_node)
25722 cp_parser_error (parser,
25723 "invalid operator for %<#pragma omp atomic%>");
25726 cp_lexer_consume_token (parser->lexer);
25728 rhs = cp_parser_expression (parser, false, NULL);
25729 if (rhs == error_mark_node)
25734 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25736 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25738 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25739 /*cast_p=*/false, NULL);
25740 if (v == error_mark_node)
25742 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25744 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25745 /*cast_p=*/false, NULL);
25746 if (lhs1 == error_mark_node)
25749 if (structured_block)
25751 cp_parser_consume_semicolon_at_end_of_statement (parser);
25752 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25755 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25756 if (!structured_block)
25757 cp_parser_consume_semicolon_at_end_of_statement (parser);
25761 cp_parser_skip_to_end_of_block_or_statement (parser);
25762 if (structured_block)
25764 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25765 cp_lexer_consume_token (parser->lexer);
25766 else if (code == OMP_ATOMIC_CAPTURE_NEW)
25768 cp_parser_skip_to_end_of_block_or_statement (parser);
25769 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25770 cp_lexer_consume_token (parser->lexer);
25777 # pragma omp barrier new-line */
25780 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25782 cp_parser_require_pragma_eol (parser, pragma_tok);
25783 finish_omp_barrier ();
25787 # pragma omp critical [(name)] new-line
25788 structured-block */
25791 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25793 tree stmt, name = NULL;
25795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25797 cp_lexer_consume_token (parser->lexer);
25799 name = cp_parser_identifier (parser);
25801 if (name == error_mark_node
25802 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25803 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25804 /*or_comma=*/false,
25805 /*consume_paren=*/true);
25806 if (name == error_mark_node)
25809 cp_parser_require_pragma_eol (parser, pragma_tok);
25811 stmt = cp_parser_omp_structured_block (parser);
25812 return c_finish_omp_critical (input_location, stmt, name);
25816 # pragma omp flush flush-vars[opt] new-line
25819 ( variable-list ) */
25822 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25825 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25826 cp_parser_require_pragma_eol (parser, pragma_tok);
25828 finish_omp_flush ();
25831 /* Helper function, to parse omp for increment expression. */
25834 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25836 tree cond = cp_parser_binary_expression (parser, false, true,
25837 PREC_NOT_OPERATOR, NULL);
25838 if (cond == error_mark_node
25839 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25841 cp_parser_skip_to_end_of_statement (parser);
25842 return error_mark_node;
25845 switch (TREE_CODE (cond))
25853 return error_mark_node;
25856 /* If decl is an iterator, preserve LHS and RHS of the relational
25857 expr until finish_omp_for. */
25859 && (type_dependent_expression_p (decl)
25860 || CLASS_TYPE_P (TREE_TYPE (decl))))
25863 return build_x_binary_op (TREE_CODE (cond),
25864 TREE_OPERAND (cond, 0), ERROR_MARK,
25865 TREE_OPERAND (cond, 1), ERROR_MARK,
25866 /*overload=*/NULL, tf_warning_or_error);
25869 /* Helper function, to parse omp for increment expression. */
25872 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25874 cp_token *token = cp_lexer_peek_token (parser->lexer);
25880 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25882 op = (token->type == CPP_PLUS_PLUS
25883 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25884 cp_lexer_consume_token (parser->lexer);
25885 lhs = cp_parser_cast_expression (parser, false, false, NULL);
25887 return error_mark_node;
25888 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25891 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25893 return error_mark_node;
25895 token = cp_lexer_peek_token (parser->lexer);
25896 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25898 op = (token->type == CPP_PLUS_PLUS
25899 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25900 cp_lexer_consume_token (parser->lexer);
25901 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25904 op = cp_parser_assignment_operator_opt (parser);
25905 if (op == ERROR_MARK)
25906 return error_mark_node;
25908 if (op != NOP_EXPR)
25910 rhs = cp_parser_assignment_expression (parser, false, NULL);
25911 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25912 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25915 lhs = cp_parser_binary_expression (parser, false, false,
25916 PREC_ADDITIVE_EXPRESSION, NULL);
25917 token = cp_lexer_peek_token (parser->lexer);
25918 decl_first = lhs == decl;
25921 if (token->type != CPP_PLUS
25922 && token->type != CPP_MINUS)
25923 return error_mark_node;
25927 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25928 cp_lexer_consume_token (parser->lexer);
25929 rhs = cp_parser_binary_expression (parser, false, false,
25930 PREC_ADDITIVE_EXPRESSION, NULL);
25931 token = cp_lexer_peek_token (parser->lexer);
25932 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25934 if (lhs == NULL_TREE)
25936 if (op == PLUS_EXPR)
25939 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25942 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25943 NULL, tf_warning_or_error);
25946 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25950 if (rhs != decl || op == MINUS_EXPR)
25951 return error_mark_node;
25952 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25955 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25957 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25960 /* Parse the restricted form of the for statement allowed by OpenMP. */
25963 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25965 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25966 tree real_decl, initv, condv, incrv, declv;
25967 tree this_pre_body, cl;
25968 location_t loc_first;
25969 bool collapse_err = false;
25970 int i, collapse = 1, nbraces = 0;
25971 VEC(tree,gc) *for_block = make_tree_vector ();
25973 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25974 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25975 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25977 gcc_assert (collapse >= 1);
25979 declv = make_tree_vec (collapse);
25980 initv = make_tree_vec (collapse);
25981 condv = make_tree_vec (collapse);
25982 incrv = make_tree_vec (collapse);
25984 loc_first = cp_lexer_peek_token (parser->lexer)->location;
25986 for (i = 0; i < collapse; i++)
25988 int bracecount = 0;
25989 bool add_private_clause = false;
25992 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25994 cp_parser_error (parser, "for statement expected");
25997 loc = cp_lexer_consume_token (parser->lexer)->location;
25999 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26002 init = decl = real_decl = NULL;
26003 this_pre_body = push_stmt_list ();
26004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26006 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26010 integer-type var = lb
26011 random-access-iterator-type var = lb
26012 pointer-type var = lb
26014 cp_decl_specifier_seq type_specifiers;
26016 /* First, try to parse as an initialized declaration. See
26017 cp_parser_condition, from whence the bulk of this is copied. */
26019 cp_parser_parse_tentatively (parser);
26020 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26021 /*is_trailing_return=*/false,
26023 if (cp_parser_parse_definitely (parser))
26025 /* If parsing a type specifier seq succeeded, then this
26026 MUST be a initialized declaration. */
26027 tree asm_specification, attributes;
26028 cp_declarator *declarator;
26030 declarator = cp_parser_declarator (parser,
26031 CP_PARSER_DECLARATOR_NAMED,
26032 /*ctor_dtor_or_conv_p=*/NULL,
26033 /*parenthesized_p=*/NULL,
26034 /*member_p=*/false);
26035 attributes = cp_parser_attributes_opt (parser);
26036 asm_specification = cp_parser_asm_specification_opt (parser);
26038 if (declarator == cp_error_declarator)
26039 cp_parser_skip_to_end_of_statement (parser);
26043 tree pushed_scope, auto_node;
26045 decl = start_decl (declarator, &type_specifiers,
26046 SD_INITIALIZED, attributes,
26047 /*prefix_attributes=*/NULL_TREE,
26050 auto_node = type_uses_auto (TREE_TYPE (decl));
26051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26053 if (cp_lexer_next_token_is (parser->lexer,
26055 error ("parenthesized initialization is not allowed in "
26056 "OpenMP %<for%> loop");
26058 /* Trigger an error. */
26059 cp_parser_require (parser, CPP_EQ, RT_EQ);
26061 init = error_mark_node;
26062 cp_parser_skip_to_end_of_statement (parser);
26064 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26065 || type_dependent_expression_p (decl)
26068 bool is_direct_init, is_non_constant_init;
26070 init = cp_parser_initializer (parser,
26072 &is_non_constant_init);
26077 = do_auto_deduction (TREE_TYPE (decl), init,
26080 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26081 && !type_dependent_expression_p (decl))
26085 cp_finish_decl (decl, init, !is_non_constant_init,
26087 LOOKUP_ONLYCONVERTING);
26088 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26090 VEC_safe_push (tree, gc, for_block, this_pre_body);
26094 init = pop_stmt_list (this_pre_body);
26095 this_pre_body = NULL_TREE;
26100 cp_lexer_consume_token (parser->lexer);
26101 init = cp_parser_assignment_expression (parser, false, NULL);
26104 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26105 init = error_mark_node;
26107 cp_finish_decl (decl, NULL_TREE,
26108 /*init_const_expr_p=*/false,
26110 LOOKUP_ONLYCONVERTING);
26114 pop_scope (pushed_scope);
26120 /* If parsing a type specifier sequence failed, then
26121 this MUST be a simple expression. */
26122 cp_parser_parse_tentatively (parser);
26123 decl = cp_parser_primary_expression (parser, false, false,
26125 if (!cp_parser_error_occurred (parser)
26128 && CLASS_TYPE_P (TREE_TYPE (decl)))
26132 cp_parser_parse_definitely (parser);
26133 cp_parser_require (parser, CPP_EQ, RT_EQ);
26134 rhs = cp_parser_assignment_expression (parser, false, NULL);
26135 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26137 tf_warning_or_error));
26138 add_private_clause = true;
26143 cp_parser_abort_tentative_parse (parser);
26144 init = cp_parser_expression (parser, false, NULL);
26147 if (TREE_CODE (init) == MODIFY_EXPR
26148 || TREE_CODE (init) == MODOP_EXPR)
26149 real_decl = TREE_OPERAND (init, 0);
26154 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26157 this_pre_body = pop_stmt_list (this_pre_body);
26161 pre_body = push_stmt_list ();
26163 add_stmt (this_pre_body);
26164 pre_body = pop_stmt_list (pre_body);
26167 pre_body = this_pre_body;
26172 if (par_clauses != NULL && real_decl != NULL_TREE)
26175 for (c = par_clauses; *c ; )
26176 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26177 && OMP_CLAUSE_DECL (*c) == real_decl)
26179 error_at (loc, "iteration variable %qD"
26180 " should not be firstprivate", real_decl);
26181 *c = OMP_CLAUSE_CHAIN (*c);
26183 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26184 && OMP_CLAUSE_DECL (*c) == real_decl)
26186 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26187 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26188 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26189 OMP_CLAUSE_DECL (l) = real_decl;
26190 OMP_CLAUSE_CHAIN (l) = clauses;
26191 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26193 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26194 CP_OMP_CLAUSE_INFO (*c) = NULL;
26195 add_private_clause = false;
26199 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26200 && OMP_CLAUSE_DECL (*c) == real_decl)
26201 add_private_clause = false;
26202 c = &OMP_CLAUSE_CHAIN (*c);
26206 if (add_private_clause)
26209 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26211 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26212 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26213 && OMP_CLAUSE_DECL (c) == decl)
26215 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26216 && OMP_CLAUSE_DECL (c) == decl)
26217 error_at (loc, "iteration variable %qD "
26218 "should not be firstprivate",
26220 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26221 && OMP_CLAUSE_DECL (c) == decl)
26222 error_at (loc, "iteration variable %qD should not be reduction",
26227 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26228 OMP_CLAUSE_DECL (c) = decl;
26229 c = finish_omp_clauses (c);
26232 OMP_CLAUSE_CHAIN (c) = clauses;
26239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26240 cond = cp_parser_omp_for_cond (parser, decl);
26241 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26246 /* If decl is an iterator, preserve the operator on decl
26247 until finish_omp_for. */
26249 && ((type_dependent_expression_p (decl)
26250 && !POINTER_TYPE_P (TREE_TYPE (decl)))
26251 || CLASS_TYPE_P (TREE_TYPE (decl))))
26252 incr = cp_parser_omp_for_incr (parser, decl);
26254 incr = cp_parser_expression (parser, false, NULL);
26257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26258 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26259 /*or_comma=*/false,
26260 /*consume_paren=*/true);
26262 TREE_VEC_ELT (declv, i) = decl;
26263 TREE_VEC_ELT (initv, i) = init;
26264 TREE_VEC_ELT (condv, i) = cond;
26265 TREE_VEC_ELT (incrv, i) = incr;
26267 if (i == collapse - 1)
26270 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26271 in between the collapsed for loops to be still considered perfectly
26272 nested. Hopefully the final version clarifies this.
26273 For now handle (multiple) {'s and empty statements. */
26274 cp_parser_parse_tentatively (parser);
26277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26279 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26281 cp_lexer_consume_token (parser->lexer);
26284 else if (bracecount
26285 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26286 cp_lexer_consume_token (parser->lexer);
26289 loc = cp_lexer_peek_token (parser->lexer)->location;
26290 error_at (loc, "not enough collapsed for loops");
26291 collapse_err = true;
26292 cp_parser_abort_tentative_parse (parser);
26301 cp_parser_parse_definitely (parser);
26302 nbraces += bracecount;
26306 /* Note that we saved the original contents of this flag when we entered
26307 the structured block, and so we don't need to re-save it here. */
26308 parser->in_statement = IN_OMP_FOR;
26310 /* Note that the grammar doesn't call for a structured block here,
26311 though the loop as a whole is a structured block. */
26312 body = push_stmt_list ();
26313 cp_parser_statement (parser, NULL_TREE, false, NULL);
26314 body = pop_stmt_list (body);
26316 if (declv == NULL_TREE)
26319 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26320 pre_body, clauses);
26324 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26326 cp_lexer_consume_token (parser->lexer);
26329 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26330 cp_lexer_consume_token (parser->lexer);
26335 error_at (cp_lexer_peek_token (parser->lexer)->location,
26336 "collapsed loops not perfectly nested");
26338 collapse_err = true;
26339 cp_parser_statement_seq_opt (parser, NULL);
26340 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26345 while (!VEC_empty (tree, for_block))
26346 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26347 release_tree_vector (for_block);
26353 #pragma omp for for-clause[optseq] new-line
26356 #define OMP_FOR_CLAUSE_MASK \
26357 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26358 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26359 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26360 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26361 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26362 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26363 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26364 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26367 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26369 tree clauses, sb, ret;
26372 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26373 "#pragma omp for", pragma_tok);
26375 sb = begin_omp_structured_block ();
26376 save = cp_parser_begin_omp_structured_block (parser);
26378 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26380 cp_parser_end_omp_structured_block (parser, save);
26381 add_stmt (finish_omp_structured_block (sb));
26387 # pragma omp master new-line
26388 structured-block */
26391 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26393 cp_parser_require_pragma_eol (parser, pragma_tok);
26394 return c_finish_omp_master (input_location,
26395 cp_parser_omp_structured_block (parser));
26399 # pragma omp ordered new-line
26400 structured-block */
26403 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26405 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26406 cp_parser_require_pragma_eol (parser, pragma_tok);
26407 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26413 { section-sequence }
26416 section-directive[opt] structured-block
26417 section-sequence section-directive structured-block */
26420 cp_parser_omp_sections_scope (cp_parser *parser)
26422 tree stmt, substmt;
26423 bool error_suppress = false;
26426 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26429 stmt = push_stmt_list ();
26431 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26435 substmt = begin_omp_structured_block ();
26436 save = cp_parser_begin_omp_structured_block (parser);
26440 cp_parser_statement (parser, NULL_TREE, false, NULL);
26442 tok = cp_lexer_peek_token (parser->lexer);
26443 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26445 if (tok->type == CPP_CLOSE_BRACE)
26447 if (tok->type == CPP_EOF)
26451 cp_parser_end_omp_structured_block (parser, save);
26452 substmt = finish_omp_structured_block (substmt);
26453 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26454 add_stmt (substmt);
26459 tok = cp_lexer_peek_token (parser->lexer);
26460 if (tok->type == CPP_CLOSE_BRACE)
26462 if (tok->type == CPP_EOF)
26465 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26467 cp_lexer_consume_token (parser->lexer);
26468 cp_parser_require_pragma_eol (parser, tok);
26469 error_suppress = false;
26471 else if (!error_suppress)
26473 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26474 error_suppress = true;
26477 substmt = cp_parser_omp_structured_block (parser);
26478 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26479 add_stmt (substmt);
26481 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26483 substmt = pop_stmt_list (stmt);
26485 stmt = make_node (OMP_SECTIONS);
26486 TREE_TYPE (stmt) = void_type_node;
26487 OMP_SECTIONS_BODY (stmt) = substmt;
26494 # pragma omp sections sections-clause[optseq] newline
26497 #define OMP_SECTIONS_CLAUSE_MASK \
26498 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26499 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26500 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26501 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26502 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26505 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26509 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26510 "#pragma omp sections", pragma_tok);
26512 ret = cp_parser_omp_sections_scope (parser);
26514 OMP_SECTIONS_CLAUSES (ret) = clauses;
26520 # pragma parallel parallel-clause new-line
26521 # pragma parallel for parallel-for-clause new-line
26522 # pragma parallel sections parallel-sections-clause new-line */
26524 #define OMP_PARALLEL_CLAUSE_MASK \
26525 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26526 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26527 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26528 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26529 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26530 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26531 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26532 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26535 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26537 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26538 const char *p_name = "#pragma omp parallel";
26539 tree stmt, clauses, par_clause, ws_clause, block;
26540 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26542 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26546 cp_lexer_consume_token (parser->lexer);
26547 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26548 p_name = "#pragma omp parallel for";
26549 mask |= OMP_FOR_CLAUSE_MASK;
26550 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26552 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26554 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26555 const char *p = IDENTIFIER_POINTER (id);
26556 if (strcmp (p, "sections") == 0)
26558 cp_lexer_consume_token (parser->lexer);
26559 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26560 p_name = "#pragma omp parallel sections";
26561 mask |= OMP_SECTIONS_CLAUSE_MASK;
26562 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26566 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26567 block = begin_omp_parallel ();
26568 save = cp_parser_begin_omp_structured_block (parser);
26572 case PRAGMA_OMP_PARALLEL:
26573 cp_parser_statement (parser, NULL_TREE, false, NULL);
26574 par_clause = clauses;
26577 case PRAGMA_OMP_PARALLEL_FOR:
26578 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26579 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26582 case PRAGMA_OMP_PARALLEL_SECTIONS:
26583 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26584 stmt = cp_parser_omp_sections_scope (parser);
26586 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26590 gcc_unreachable ();
26593 cp_parser_end_omp_structured_block (parser, save);
26594 stmt = finish_omp_parallel (par_clause, block);
26595 if (p_kind != PRAGMA_OMP_PARALLEL)
26596 OMP_PARALLEL_COMBINED (stmt) = 1;
26601 # pragma omp single single-clause[optseq] new-line
26602 structured-block */
26604 #define OMP_SINGLE_CLAUSE_MASK \
26605 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26606 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26607 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26608 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26611 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26613 tree stmt = make_node (OMP_SINGLE);
26614 TREE_TYPE (stmt) = void_type_node;
26616 OMP_SINGLE_CLAUSES (stmt)
26617 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26618 "#pragma omp single", pragma_tok);
26619 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26621 return add_stmt (stmt);
26625 # pragma omp task task-clause[optseq] new-line
26626 structured-block */
26628 #define OMP_TASK_CLAUSE_MASK \
26629 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26630 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26631 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26632 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26633 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26634 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26635 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26636 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26639 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26641 tree clauses, block;
26644 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26645 "#pragma omp task", pragma_tok);
26646 block = begin_omp_task ();
26647 save = cp_parser_begin_omp_structured_block (parser);
26648 cp_parser_statement (parser, NULL_TREE, false, NULL);
26649 cp_parser_end_omp_structured_block (parser, save);
26650 return finish_omp_task (clauses, block);
26654 # pragma omp taskwait new-line */
26657 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26659 cp_parser_require_pragma_eol (parser, pragma_tok);
26660 finish_omp_taskwait ();
26664 # pragma omp taskyield new-line */
26667 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26669 cp_parser_require_pragma_eol (parser, pragma_tok);
26670 finish_omp_taskyield ();
26674 # pragma omp threadprivate (variable-list) */
26677 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26681 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26682 cp_parser_require_pragma_eol (parser, pragma_tok);
26684 finish_omp_threadprivate (vars);
26687 /* Main entry point to OpenMP statement pragmas. */
26690 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26694 switch (pragma_tok->pragma_kind)
26696 case PRAGMA_OMP_ATOMIC:
26697 cp_parser_omp_atomic (parser, pragma_tok);
26699 case PRAGMA_OMP_CRITICAL:
26700 stmt = cp_parser_omp_critical (parser, pragma_tok);
26702 case PRAGMA_OMP_FOR:
26703 stmt = cp_parser_omp_for (parser, pragma_tok);
26705 case PRAGMA_OMP_MASTER:
26706 stmt = cp_parser_omp_master (parser, pragma_tok);
26708 case PRAGMA_OMP_ORDERED:
26709 stmt = cp_parser_omp_ordered (parser, pragma_tok);
26711 case PRAGMA_OMP_PARALLEL:
26712 stmt = cp_parser_omp_parallel (parser, pragma_tok);
26714 case PRAGMA_OMP_SECTIONS:
26715 stmt = cp_parser_omp_sections (parser, pragma_tok);
26717 case PRAGMA_OMP_SINGLE:
26718 stmt = cp_parser_omp_single (parser, pragma_tok);
26720 case PRAGMA_OMP_TASK:
26721 stmt = cp_parser_omp_task (parser, pragma_tok);
26724 gcc_unreachable ();
26728 SET_EXPR_LOCATION (stmt, pragma_tok->location);
26731 /* Transactional Memory parsing routines. */
26733 /* Parse a transaction attribute.
26739 ??? Simplify this when C++0x bracket attributes are
26740 implemented properly. */
26743 cp_parser_txn_attribute_opt (cp_parser *parser)
26746 tree attr_name, attr = NULL;
26748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26749 return cp_parser_attributes_opt (parser);
26751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26753 cp_lexer_consume_token (parser->lexer);
26754 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26757 token = cp_lexer_peek_token (parser->lexer);
26758 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26760 token = cp_lexer_consume_token (parser->lexer);
26762 attr_name = (token->type == CPP_KEYWORD
26763 /* For keywords, use the canonical spelling,
26764 not the parsed identifier. */
26765 ? ridpointers[(int) token->keyword]
26767 attr = build_tree_list (attr_name, NULL_TREE);
26770 cp_parser_error (parser, "expected identifier");
26772 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26774 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26778 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26780 transaction-statement:
26781 __transaction_atomic txn-attribute[opt] txn-exception-spec[opt]
26783 __transaction_relaxed txn-exception-spec[opt] compound-statement
26785 ??? The exception specification is not yet implemented.
26789 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26791 unsigned char old_in = parser->in_transaction;
26792 unsigned char this_in = 1, new_in;
26796 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26797 || keyword == RID_TRANSACTION_RELAXED);
26798 token = cp_parser_require_keyword (parser, keyword,
26799 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26800 : RT_TRANSACTION_RELAXED));
26801 gcc_assert (token != NULL);
26803 if (keyword == RID_TRANSACTION_RELAXED)
26804 this_in |= TM_STMT_ATTR_RELAXED;
26807 attrs = cp_parser_txn_attribute_opt (parser);
26809 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26812 /* Keep track if we're in the lexical scope of an outer transaction. */
26813 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
26815 stmt = begin_transaction_stmt (token->location, NULL, this_in);
26817 parser->in_transaction = new_in;
26818 cp_parser_compound_statement (parser, NULL, false, false);
26819 parser->in_transaction = old_in;
26821 finish_transaction_stmt (stmt, NULL, this_in);
26826 /* Parse a __transaction_atomic or __transaction_relaxed expression.
26828 transaction-expression:
26829 __transaction_atomic txn-exception-spec[opt] ( expression )
26830 __transaction_relaxed txn-exception-spec[opt] ( expression )
26832 ??? The exception specification is not yet implemented.
26836 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
26838 unsigned char old_in = parser->in_transaction;
26839 unsigned char this_in = 1;
26843 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26844 || keyword == RID_TRANSACTION_RELAXED);
26847 error (keyword == RID_TRANSACTION_RELAXED
26848 ? G_("%<__transaction_relaxed%> without transactional memory "
26850 : G_("%<__transaction_atomic%> without transactional memory "
26851 "support enabled"));
26853 token = cp_parser_require_keyword (parser, keyword,
26854 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26855 : RT_TRANSACTION_RELAXED));
26856 gcc_assert (token != NULL);
26858 if (keyword == RID_TRANSACTION_RELAXED)
26859 this_in |= TM_STMT_ATTR_RELAXED;
26861 parser->in_transaction = this_in;
26862 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26864 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26865 finish_parenthesized_expr (expr);
26866 expr = build_transaction_expr (token->location, expr, this_in);
26868 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26869 parser->in_transaction = old_in;
26871 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
26872 return error_mark_node;
26874 return (flag_tm ? expr : error_mark_node);
26877 /* Parse a function-transaction-block.
26879 function-transaction-block:
26880 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
26882 __transaction_atomic txn-attribute[opt] function-try-block
26883 __transaction_relaxed ctor-initializer[opt] function-body
26884 __transaction_relaxed function-try-block
26888 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
26890 unsigned char old_in = parser->in_transaction;
26891 unsigned char new_in = 1;
26892 tree compound_stmt, stmt, attrs;
26893 bool ctor_initializer_p;
26896 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26897 || keyword == RID_TRANSACTION_RELAXED);
26898 token = cp_parser_require_keyword (parser, keyword,
26899 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26900 : RT_TRANSACTION_RELAXED));
26901 gcc_assert (token != NULL);
26903 if (keyword == RID_TRANSACTION_RELAXED)
26904 new_in |= TM_STMT_ATTR_RELAXED;
26907 attrs = cp_parser_txn_attribute_opt (parser);
26909 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26912 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
26914 parser->in_transaction = new_in;
26916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26917 ctor_initializer_p = cp_parser_function_try_block (parser);
26920 = cp_parser_ctor_initializer_opt_and_function_body (parser);
26922 parser->in_transaction = old_in;
26924 finish_transaction_stmt (stmt, compound_stmt, new_in);
26926 return ctor_initializer_p;
26929 /* Parse a __transaction_cancel statement.
26932 __transaction_cancel txn-attribute[opt] ;
26933 __transaction_cancel txn-attribute[opt] throw-expression ;
26935 ??? Cancel and throw is not yet implemented. */
26938 cp_parser_transaction_cancel (cp_parser *parser)
26941 bool is_outer = false;
26944 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
26945 RT_TRANSACTION_CANCEL);
26946 gcc_assert (token != NULL);
26948 attrs = cp_parser_txn_attribute_opt (parser);
26950 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
26952 /* ??? Parse cancel-and-throw here. */
26954 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26958 error_at (token->location, "%<__transaction_cancel%> without "
26959 "transactional memory support enabled");
26960 return error_mark_node;
26962 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
26964 error_at (token->location, "%<__transaction_cancel%> within a "
26965 "%<__transaction_relaxed%>");
26966 return error_mark_node;
26970 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
26971 && !is_tm_may_cancel_outer (current_function_decl))
26973 error_at (token->location, "outer %<__transaction_cancel%> not "
26974 "within outer %<__transaction_atomic%>");
26975 error_at (token->location,
26976 " or a %<transaction_may_cancel_outer%> function");
26977 return error_mark_node;
26980 else if (parser->in_transaction == 0)
26982 error_at (token->location, "%<__transaction_cancel%> not within "
26983 "%<__transaction_atomic%>");
26984 return error_mark_node;
26987 stmt = build_tm_abort_call (token->location, is_outer);
26996 static GTY (()) cp_parser *the_parser;
26999 /* Special handling for the first token or line in the file. The first
27000 thing in the file might be #pragma GCC pch_preprocess, which loads a
27001 PCH file, which is a GC collection point. So we need to handle this
27002 first pragma without benefit of an existing lexer structure.
27004 Always returns one token to the caller in *FIRST_TOKEN. This is
27005 either the true first token of the file, or the first token after
27006 the initial pragma. */
27009 cp_parser_initial_pragma (cp_token *first_token)
27013 cp_lexer_get_preprocessor_token (NULL, first_token);
27014 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27017 cp_lexer_get_preprocessor_token (NULL, first_token);
27018 if (first_token->type == CPP_STRING)
27020 name = first_token->u.value;
27022 cp_lexer_get_preprocessor_token (NULL, first_token);
27023 if (first_token->type != CPP_PRAGMA_EOL)
27024 error_at (first_token->location,
27025 "junk at end of %<#pragma GCC pch_preprocess%>");
27028 error_at (first_token->location, "expected string literal");
27030 /* Skip to the end of the pragma. */
27031 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27032 cp_lexer_get_preprocessor_token (NULL, first_token);
27034 /* Now actually load the PCH file. */
27036 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27038 /* Read one more token to return to our caller. We have to do this
27039 after reading the PCH file in, since its pointers have to be
27041 cp_lexer_get_preprocessor_token (NULL, first_token);
27044 /* Normal parsing of a pragma token. Here we can (and must) use the
27048 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27050 cp_token *pragma_tok;
27053 pragma_tok = cp_lexer_consume_token (parser->lexer);
27054 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27055 parser->lexer->in_pragma = true;
27057 id = pragma_tok->pragma_kind;
27060 case PRAGMA_GCC_PCH_PREPROCESS:
27061 error_at (pragma_tok->location,
27062 "%<#pragma GCC pch_preprocess%> must be first");
27065 case PRAGMA_OMP_BARRIER:
27068 case pragma_compound:
27069 cp_parser_omp_barrier (parser, pragma_tok);
27072 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27073 "used in compound statements");
27080 case PRAGMA_OMP_FLUSH:
27083 case pragma_compound:
27084 cp_parser_omp_flush (parser, pragma_tok);
27087 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27088 "used in compound statements");
27095 case PRAGMA_OMP_TASKWAIT:
27098 case pragma_compound:
27099 cp_parser_omp_taskwait (parser, pragma_tok);
27102 error_at (pragma_tok->location,
27103 "%<#pragma omp taskwait%> may only be "
27104 "used in compound statements");
27111 case PRAGMA_OMP_TASKYIELD:
27114 case pragma_compound:
27115 cp_parser_omp_taskyield (parser, pragma_tok);
27118 error_at (pragma_tok->location,
27119 "%<#pragma omp taskyield%> may only be "
27120 "used in compound statements");
27127 case PRAGMA_OMP_THREADPRIVATE:
27128 cp_parser_omp_threadprivate (parser, pragma_tok);
27131 case PRAGMA_OMP_ATOMIC:
27132 case PRAGMA_OMP_CRITICAL:
27133 case PRAGMA_OMP_FOR:
27134 case PRAGMA_OMP_MASTER:
27135 case PRAGMA_OMP_ORDERED:
27136 case PRAGMA_OMP_PARALLEL:
27137 case PRAGMA_OMP_SECTIONS:
27138 case PRAGMA_OMP_SINGLE:
27139 case PRAGMA_OMP_TASK:
27140 if (context == pragma_external)
27142 cp_parser_omp_construct (parser, pragma_tok);
27145 case PRAGMA_OMP_SECTION:
27146 error_at (pragma_tok->location,
27147 "%<#pragma omp section%> may only be used in "
27148 "%<#pragma omp sections%> construct");
27152 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27153 c_invoke_pragma_handler (id);
27157 cp_parser_error (parser, "expected declaration specifiers");
27161 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27165 /* The interface the pragma parsers have to the lexer. */
27168 pragma_lex (tree *value)
27171 enum cpp_ttype ret;
27173 tok = cp_lexer_peek_token (the_parser->lexer);
27176 *value = tok->u.value;
27178 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27180 else if (ret == CPP_STRING)
27181 *value = cp_parser_string_literal (the_parser, false, false);
27184 cp_lexer_consume_token (the_parser->lexer);
27185 if (ret == CPP_KEYWORD)
27193 /* External interface. */
27195 /* Parse one entire translation unit. */
27198 c_parse_file (void)
27200 static bool already_called = false;
27202 if (already_called)
27204 sorry ("inter-module optimizations not implemented for C++");
27207 already_called = true;
27209 the_parser = cp_parser_new ();
27210 push_deferring_access_checks (flag_access_control
27211 ? dk_no_deferred : dk_no_check);
27212 cp_parser_translation_unit (the_parser);
27216 #include "gt-cp-parser.h"