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 /* Parse a user-defined char constant. Returns a call to a user-defined
3551 literal operator taking the character as an argument. */
3554 cp_parser_userdef_char_literal (cp_parser *parser)
3556 cp_token *token = NULL;
3557 tree literal, suffix_id, value;
3562 token = cp_lexer_consume_token (parser->lexer);
3563 literal = token->u.value;
3564 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3565 value = USERDEF_LITERAL_VALUE (literal);
3566 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3568 /* Build up a call to the user-defined operator */
3569 /* Lookup the name we got back from the id-expression. */
3570 vec = make_tree_vector ();
3571 VEC_safe_push (tree, gc, vec, value);
3572 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3573 if (!decl || decl == error_mark_node)
3575 error ("unable to find user-defined character literal operator %qD",
3577 release_tree_vector (vec);
3578 return error_mark_node;
3580 result = finish_call_expr (decl, &vec, false, true, tf_warning_or_error);
3581 release_tree_vector (vec);
3586 /* A subroutine of cp_parser_userdef_numeric_literal to
3587 create a char... template parameter pack from a string node. */
3590 make_char_string_pack (tree value)
3593 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3594 const char *str = TREE_STRING_POINTER (value);
3595 int i, len = TREE_STRING_LENGTH (value) - 1;
3596 tree argvec = make_tree_vec (1);
3598 /* Fill in CHARVEC with all of the parameters. */
3599 charvec = make_tree_vec (len);
3600 for (i = 0; i < len; ++i)
3601 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3603 /* Build the argument packs. */
3604 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3605 TREE_TYPE (argpack) = char_type_node;
3607 TREE_VEC_ELT (argvec, 0) = argpack;
3612 /* Parse a user-defined numeric constant. returns a call to a user-defined
3613 literal operator. */
3616 cp_parser_userdef_numeric_literal (cp_parser *parser)
3618 cp_token *token = NULL;
3619 tree literal, suffix_id, value, num_string;
3621 tree result = error_mark_node;
3624 token = cp_lexer_consume_token (parser->lexer);
3625 literal = token->u.value;
3626 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3627 value = USERDEF_LITERAL_VALUE (literal);
3628 num_string = USERDEF_LITERAL_NUM_STRING (literal);
3629 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3631 /* Build up a call to the user-defined operator */
3632 /* Lookup the name we got back from the id-expression. */
3633 /* Try to find the literal operator by finishing the call expression
3634 with the numeric argument. */
3635 args = make_tree_vector ();
3636 VEC_safe_push (tree, gc, args, value);
3637 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3638 if (decl && decl != error_mark_node)
3640 result = finish_call_expr (decl, &args, false, true, tf_none);
3641 if (result != error_mark_node)
3643 release_tree_vector (args);
3647 release_tree_vector (args);
3649 /* If the numeric argument didn't work, look for a raw literal
3650 operator taking a const char* argument consisting of the number
3651 in string format. */
3652 args = make_tree_vector ();
3653 VEC_safe_push (tree, gc, args, num_string);
3654 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3655 if (decl && decl != error_mark_node)
3657 result = finish_call_expr (decl, &args, false, true, tf_none);
3658 if (result != error_mark_node)
3660 release_tree_vector (args);
3664 release_tree_vector (args);
3666 /* If the raw literal didn't work, look for a non-type template
3667 function with parameter pack char.... Call the function with
3668 template parameter characters representing the number. */
3669 args = make_tree_vector ();
3670 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3671 if (decl && decl != error_mark_node)
3673 tree tmpl_args = make_char_string_pack (num_string);
3674 decl = lookup_template_function (decl, tmpl_args);
3675 result = finish_call_expr (decl, &args, false, true, tf_none);
3676 if (result != error_mark_node)
3678 release_tree_vector (args);
3682 release_tree_vector (args);
3684 if (result == error_mark_node)
3685 error ("unable to find user-defined numeric literal operator %qD", name);
3690 /* Parse a user-defined string constant. Returns a call to a user-defined
3691 literal operator taking a character pointer and the length of the string
3695 cp_parser_userdef_string_literal (cp_token *token)
3697 tree literal, suffix_id, value;
3703 literal = token->u.value;
3704 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3705 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3706 value = USERDEF_LITERAL_VALUE (literal);
3707 len = TREE_STRING_LENGTH (value)
3708 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3709 /* Build up a call to the user-defined operator */
3710 /* Lookup the name we got back from the id-expression. */
3711 vec = make_tree_vector ();
3712 VEC_safe_push (tree, gc, vec, value);
3713 VEC_safe_push (tree, gc, vec, build_int_cst (size_type_node, len));
3714 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3715 if (!decl || decl == error_mark_node)
3717 error ("unable to find user-defined string literal operator %qD", name);
3718 release_tree_vector (vec);
3719 return error_mark_node;
3721 result = finish_call_expr (decl, &vec, false, true, tf_none);
3722 if (result == error_mark_node)
3723 error ("unable to find valid user-defined string literal operator %qD."
3724 " Possible missing length argument in string literal operator.",
3726 release_tree_vector (vec);
3732 /* Basic concepts [gram.basic] */
3734 /* Parse a translation-unit.
3737 declaration-seq [opt]
3739 Returns TRUE if all went well. */
3742 cp_parser_translation_unit (cp_parser* parser)
3744 /* The address of the first non-permanent object on the declarator
3746 static void *declarator_obstack_base;
3750 /* Create the declarator obstack, if necessary. */
3751 if (!cp_error_declarator)
3753 gcc_obstack_init (&declarator_obstack);
3754 /* Create the error declarator. */
3755 cp_error_declarator = make_declarator (cdk_error);
3756 /* Create the empty parameter list. */
3757 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3758 /* Remember where the base of the declarator obstack lies. */
3759 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3762 cp_parser_declaration_seq_opt (parser);
3764 /* If there are no tokens left then all went well. */
3765 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3767 /* Get rid of the token array; we don't need it any more. */
3768 cp_lexer_destroy (parser->lexer);
3769 parser->lexer = NULL;
3771 /* This file might have been a context that's implicitly extern
3772 "C". If so, pop the lang context. (Only relevant for PCH.) */
3773 if (parser->implicit_extern_c)
3775 pop_lang_context ();
3776 parser->implicit_extern_c = false;
3780 finish_translation_unit ();
3786 cp_parser_error (parser, "expected declaration");
3790 /* Make sure the declarator obstack was fully cleaned up. */
3791 gcc_assert (obstack_next_free (&declarator_obstack)
3792 == declarator_obstack_base);
3794 /* All went well. */
3798 /* Expressions [gram.expr] */
3800 /* Parse a primary-expression.
3811 ( compound-statement )
3812 __builtin_va_arg ( assignment-expression , type-id )
3813 __builtin_offsetof ( type-id , offsetof-expression )
3816 __has_nothrow_assign ( type-id )
3817 __has_nothrow_constructor ( type-id )
3818 __has_nothrow_copy ( type-id )
3819 __has_trivial_assign ( type-id )
3820 __has_trivial_constructor ( type-id )
3821 __has_trivial_copy ( type-id )
3822 __has_trivial_destructor ( type-id )
3823 __has_virtual_destructor ( type-id )
3824 __is_abstract ( type-id )
3825 __is_base_of ( type-id , type-id )
3826 __is_class ( type-id )
3827 __is_convertible_to ( type-id , type-id )
3828 __is_empty ( type-id )
3829 __is_enum ( type-id )
3830 __is_literal_type ( type-id )
3831 __is_pod ( type-id )
3832 __is_polymorphic ( type-id )
3833 __is_std_layout ( type-id )
3834 __is_trivial ( type-id )
3835 __is_union ( type-id )
3837 Objective-C++ Extension:
3845 ADDRESS_P is true iff this expression was immediately preceded by
3846 "&" and therefore might denote a pointer-to-member. CAST_P is true
3847 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3848 true iff this expression is a template argument.
3850 Returns a representation of the expression. Upon return, *IDK
3851 indicates what kind of id-expression (if any) was present. */
3854 cp_parser_primary_expression (cp_parser *parser,
3857 bool template_arg_p,
3860 cp_token *token = NULL;
3862 /* Assume the primary expression is not an id-expression. */
3863 *idk = CP_ID_KIND_NONE;
3865 /* Peek at the next token. */
3866 token = cp_lexer_peek_token (parser->lexer);
3867 switch (token->type)
3876 user-defined-literal */
3882 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3883 return cp_parser_userdef_numeric_literal (parser);
3884 token = cp_lexer_consume_token (parser->lexer);
3885 if (TREE_CODE (token->u.value) == FIXED_CST)
3887 error_at (token->location,
3888 "fixed-point types not supported in C++");
3889 return error_mark_node;
3891 /* Floating-point literals are only allowed in an integral
3892 constant expression if they are cast to an integral or
3893 enumeration type. */
3894 if (TREE_CODE (token->u.value) == REAL_CST
3895 && parser->integral_constant_expression_p
3898 /* CAST_P will be set even in invalid code like "int(2.7 +
3899 ...)". Therefore, we have to check that the next token
3900 is sure to end the cast. */
3903 cp_token *next_token;
3905 next_token = cp_lexer_peek_token (parser->lexer);
3906 if (/* The comma at the end of an
3907 enumerator-definition. */
3908 next_token->type != CPP_COMMA
3909 /* The curly brace at the end of an enum-specifier. */
3910 && next_token->type != CPP_CLOSE_BRACE
3911 /* The end of a statement. */
3912 && next_token->type != CPP_SEMICOLON
3913 /* The end of the cast-expression. */
3914 && next_token->type != CPP_CLOSE_PAREN
3915 /* The end of an array bound. */
3916 && next_token->type != CPP_CLOSE_SQUARE
3917 /* The closing ">" in a template-argument-list. */
3918 && (next_token->type != CPP_GREATER
3919 || parser->greater_than_is_operator_p)
3920 /* C++0x only: A ">>" treated like two ">" tokens,
3921 in a template-argument-list. */
3922 && (next_token->type != CPP_RSHIFT
3923 || (cxx_dialect == cxx98)
3924 || parser->greater_than_is_operator_p))
3928 /* If we are within a cast, then the constraint that the
3929 cast is to an integral or enumeration type will be
3930 checked at that point. If we are not within a cast, then
3931 this code is invalid. */
3933 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3935 return token->u.value;
3937 case CPP_CHAR_USERDEF:
3938 case CPP_CHAR16_USERDEF:
3939 case CPP_CHAR32_USERDEF:
3940 case CPP_WCHAR_USERDEF:
3941 return cp_parser_userdef_char_literal (parser);
3947 case CPP_UTF8STRING:
3948 case CPP_STRING_USERDEF:
3949 case CPP_STRING16_USERDEF:
3950 case CPP_STRING32_USERDEF:
3951 case CPP_WSTRING_USERDEF:
3952 case CPP_UTF8STRING_USERDEF:
3953 /* ??? Should wide strings be allowed when parser->translate_strings_p
3954 is false (i.e. in attributes)? If not, we can kill the third
3955 argument to cp_parser_string_literal. */
3956 return cp_parser_string_literal (parser,
3957 parser->translate_strings_p,
3960 case CPP_OPEN_PAREN:
3963 bool saved_greater_than_is_operator_p;
3965 /* Consume the `('. */
3966 cp_lexer_consume_token (parser->lexer);
3967 /* Within a parenthesized expression, a `>' token is always
3968 the greater-than operator. */
3969 saved_greater_than_is_operator_p
3970 = parser->greater_than_is_operator_p;
3971 parser->greater_than_is_operator_p = true;
3972 /* If we see `( { ' then we are looking at the beginning of
3973 a GNU statement-expression. */
3974 if (cp_parser_allow_gnu_extensions_p (parser)
3975 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3977 /* Statement-expressions are not allowed by the standard. */
3978 pedwarn (token->location, OPT_pedantic,
3979 "ISO C++ forbids braced-groups within expressions");
3981 /* And they're not allowed outside of a function-body; you
3982 cannot, for example, write:
3984 int i = ({ int j = 3; j + 1; });
3986 at class or namespace scope. */
3987 if (!parser->in_function_body
3988 || parser->in_template_argument_list_p)
3990 error_at (token->location,
3991 "statement-expressions are not allowed outside "
3992 "functions nor in template-argument lists");
3993 cp_parser_skip_to_end_of_block_or_statement (parser);
3994 expr = error_mark_node;
3998 /* Start the statement-expression. */
3999 expr = begin_stmt_expr ();
4000 /* Parse the compound-statement. */
4001 cp_parser_compound_statement (parser, expr, false, false);
4003 expr = finish_stmt_expr (expr, false);
4008 /* Parse the parenthesized expression. */
4009 expr = cp_parser_expression (parser, cast_p, idk);
4010 /* Let the front end know that this expression was
4011 enclosed in parentheses. This matters in case, for
4012 example, the expression is of the form `A::B', since
4013 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4015 finish_parenthesized_expr (expr);
4016 /* DR 705: Wrapping an unqualified name in parentheses
4017 suppresses arg-dependent lookup. We want to pass back
4018 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4019 (c++/37862), but none of the others. */
4020 if (*idk != CP_ID_KIND_QUALIFIED)
4021 *idk = CP_ID_KIND_NONE;
4023 /* The `>' token might be the end of a template-id or
4024 template-parameter-list now. */
4025 parser->greater_than_is_operator_p
4026 = saved_greater_than_is_operator_p;
4027 /* Consume the `)'. */
4028 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4029 cp_parser_skip_to_end_of_statement (parser);
4034 case CPP_OPEN_SQUARE:
4035 if (c_dialect_objc ())
4036 /* We have an Objective-C++ message. */
4037 return cp_parser_objc_expression (parser);
4039 tree lam = cp_parser_lambda_expression (parser);
4040 /* Don't warn about a failed tentative parse. */
4041 if (cp_parser_error_occurred (parser))
4042 return error_mark_node;
4043 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4047 case CPP_OBJC_STRING:
4048 if (c_dialect_objc ())
4049 /* We have an Objective-C++ string literal. */
4050 return cp_parser_objc_expression (parser);
4051 cp_parser_error (parser, "expected primary-expression");
4052 return error_mark_node;
4055 switch (token->keyword)
4057 /* These two are the boolean literals. */
4059 cp_lexer_consume_token (parser->lexer);
4060 return boolean_true_node;
4062 cp_lexer_consume_token (parser->lexer);
4063 return boolean_false_node;
4065 /* The `__null' literal. */
4067 cp_lexer_consume_token (parser->lexer);
4070 /* The `nullptr' literal. */
4072 cp_lexer_consume_token (parser->lexer);
4073 return nullptr_node;
4075 /* Recognize the `this' keyword. */
4077 cp_lexer_consume_token (parser->lexer);
4078 if (parser->local_variables_forbidden_p)
4080 error_at (token->location,
4081 "%<this%> may not be used in this context");
4082 return error_mark_node;
4084 /* Pointers cannot appear in constant-expressions. */
4085 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4086 return error_mark_node;
4087 return finish_this_expr ();
4089 /* The `operator' keyword can be the beginning of an
4094 case RID_FUNCTION_NAME:
4095 case RID_PRETTY_FUNCTION_NAME:
4096 case RID_C99_FUNCTION_NAME:
4098 non_integral_constant name;
4100 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4101 __func__ are the names of variables -- but they are
4102 treated specially. Therefore, they are handled here,
4103 rather than relying on the generic id-expression logic
4104 below. Grammatically, these names are id-expressions.
4106 Consume the token. */
4107 token = cp_lexer_consume_token (parser->lexer);
4109 switch (token->keyword)
4111 case RID_FUNCTION_NAME:
4112 name = NIC_FUNC_NAME;
4114 case RID_PRETTY_FUNCTION_NAME:
4115 name = NIC_PRETTY_FUNC;
4117 case RID_C99_FUNCTION_NAME:
4118 name = NIC_C99_FUNC;
4124 if (cp_parser_non_integral_constant_expression (parser, name))
4125 return error_mark_node;
4127 /* Look up the name. */
4128 return finish_fname (token->u.value);
4136 /* The `__builtin_va_arg' construct is used to handle
4137 `va_arg'. Consume the `__builtin_va_arg' token. */
4138 cp_lexer_consume_token (parser->lexer);
4139 /* Look for the opening `('. */
4140 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4141 /* Now, parse the assignment-expression. */
4142 expression = cp_parser_assignment_expression (parser,
4143 /*cast_p=*/false, NULL);
4144 /* Look for the `,'. */
4145 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4146 /* Parse the type-id. */
4147 type = cp_parser_type_id (parser);
4148 /* Look for the closing `)'. */
4149 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4150 /* Using `va_arg' in a constant-expression is not
4152 if (cp_parser_non_integral_constant_expression (parser,
4154 return error_mark_node;
4155 return build_x_va_arg (expression, type);
4159 return cp_parser_builtin_offsetof (parser);
4161 case RID_HAS_NOTHROW_ASSIGN:
4162 case RID_HAS_NOTHROW_CONSTRUCTOR:
4163 case RID_HAS_NOTHROW_COPY:
4164 case RID_HAS_TRIVIAL_ASSIGN:
4165 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4166 case RID_HAS_TRIVIAL_COPY:
4167 case RID_HAS_TRIVIAL_DESTRUCTOR:
4168 case RID_HAS_VIRTUAL_DESTRUCTOR:
4169 case RID_IS_ABSTRACT:
4170 case RID_IS_BASE_OF:
4172 case RID_IS_CONVERTIBLE_TO:
4175 case RID_IS_LITERAL_TYPE:
4177 case RID_IS_POLYMORPHIC:
4178 case RID_IS_STD_LAYOUT:
4179 case RID_IS_TRIVIAL:
4181 return cp_parser_trait_expr (parser, token->keyword);
4183 /* Objective-C++ expressions. */
4185 case RID_AT_PROTOCOL:
4186 case RID_AT_SELECTOR:
4187 return cp_parser_objc_expression (parser);
4190 if (parser->in_function_body
4191 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4194 error_at (token->location,
4195 "a template declaration cannot appear at block scope");
4196 cp_parser_skip_to_end_of_block_or_statement (parser);
4197 return error_mark_node;
4200 cp_parser_error (parser, "expected primary-expression");
4201 return error_mark_node;
4204 /* An id-expression can start with either an identifier, a
4205 `::' as the beginning of a qualified-id, or the "operator"
4209 case CPP_TEMPLATE_ID:
4210 case CPP_NESTED_NAME_SPECIFIER:
4214 const char *error_msg;
4217 cp_token *id_expr_token;
4220 /* Parse the id-expression. */
4222 = cp_parser_id_expression (parser,
4223 /*template_keyword_p=*/false,
4224 /*check_dependency_p=*/true,
4226 /*declarator_p=*/false,
4227 /*optional_p=*/false);
4228 if (id_expression == error_mark_node)
4229 return error_mark_node;
4230 id_expr_token = token;
4231 token = cp_lexer_peek_token (parser->lexer);
4232 done = (token->type != CPP_OPEN_SQUARE
4233 && token->type != CPP_OPEN_PAREN
4234 && token->type != CPP_DOT
4235 && token->type != CPP_DEREF
4236 && token->type != CPP_PLUS_PLUS
4237 && token->type != CPP_MINUS_MINUS);
4238 /* If we have a template-id, then no further lookup is
4239 required. If the template-id was for a template-class, we
4240 will sometimes have a TYPE_DECL at this point. */
4241 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4242 || TREE_CODE (id_expression) == TYPE_DECL)
4243 decl = id_expression;
4244 /* Look up the name. */
4247 tree ambiguous_decls;
4249 /* If we already know that this lookup is ambiguous, then
4250 we've already issued an error message; there's no reason
4252 if (id_expr_token->type == CPP_NAME
4253 && id_expr_token->ambiguous_p)
4255 cp_parser_simulate_error (parser);
4256 return error_mark_node;
4259 decl = cp_parser_lookup_name (parser, id_expression,
4262 /*is_namespace=*/false,
4263 /*check_dependency=*/true,
4265 id_expr_token->location);
4266 /* If the lookup was ambiguous, an error will already have
4268 if (ambiguous_decls)
4269 return error_mark_node;
4271 /* In Objective-C++, we may have an Objective-C 2.0
4272 dot-syntax for classes here. */
4273 if (c_dialect_objc ()
4274 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4275 && TREE_CODE (decl) == TYPE_DECL
4276 && objc_is_class_name (decl))
4279 cp_lexer_consume_token (parser->lexer);
4280 component = cp_parser_identifier (parser);
4281 if (component == error_mark_node)
4282 return error_mark_node;
4284 return objc_build_class_component_ref (id_expression, component);
4287 /* In Objective-C++, an instance variable (ivar) may be preferred
4288 to whatever cp_parser_lookup_name() found. */
4289 decl = objc_lookup_ivar (decl, id_expression);
4291 /* If name lookup gives us a SCOPE_REF, then the
4292 qualifying scope was dependent. */
4293 if (TREE_CODE (decl) == SCOPE_REF)
4295 /* At this point, we do not know if DECL is a valid
4296 integral constant expression. We assume that it is
4297 in fact such an expression, so that code like:
4299 template <int N> struct A {
4303 is accepted. At template-instantiation time, we
4304 will check that B<N>::i is actually a constant. */
4307 /* Check to see if DECL is a local variable in a context
4308 where that is forbidden. */
4309 if (parser->local_variables_forbidden_p
4310 && local_variable_p (decl))
4312 /* It might be that we only found DECL because we are
4313 trying to be generous with pre-ISO scoping rules.
4314 For example, consider:
4318 for (int i = 0; i < 10; ++i) {}
4319 extern void f(int j = i);
4322 Here, name look up will originally find the out
4323 of scope `i'. We need to issue a warning message,
4324 but then use the global `i'. */
4325 decl = check_for_out_of_scope_variable (decl);
4326 if (local_variable_p (decl))
4328 error_at (id_expr_token->location,
4329 "local variable %qD may not appear in this context",
4331 return error_mark_node;
4336 decl = (finish_id_expression
4337 (id_expression, decl, parser->scope,
4339 parser->integral_constant_expression_p,
4340 parser->allow_non_integral_constant_expression_p,
4341 &parser->non_integral_constant_expression_p,
4342 template_p, done, address_p,
4345 id_expr_token->location));
4347 cp_parser_error (parser, error_msg);
4351 /* Anything else is an error. */
4353 cp_parser_error (parser, "expected primary-expression");
4354 return error_mark_node;
4358 /* Parse an id-expression.
4365 :: [opt] nested-name-specifier template [opt] unqualified-id
4367 :: operator-function-id
4370 Return a representation of the unqualified portion of the
4371 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4372 a `::' or nested-name-specifier.
4374 Often, if the id-expression was a qualified-id, the caller will
4375 want to make a SCOPE_REF to represent the qualified-id. This
4376 function does not do this in order to avoid wastefully creating
4377 SCOPE_REFs when they are not required.
4379 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4382 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4383 uninstantiated templates.
4385 If *TEMPLATE_P is non-NULL, it is set to true iff the
4386 `template' keyword is used to explicitly indicate that the entity
4387 named is a template.
4389 If DECLARATOR_P is true, the id-expression is appearing as part of
4390 a declarator, rather than as part of an expression. */
4393 cp_parser_id_expression (cp_parser *parser,
4394 bool template_keyword_p,
4395 bool check_dependency_p,
4400 bool global_scope_p;
4401 bool nested_name_specifier_p;
4403 /* Assume the `template' keyword was not used. */
4405 *template_p = template_keyword_p;
4407 /* Look for the optional `::' operator. */
4409 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4411 /* Look for the optional nested-name-specifier. */
4412 nested_name_specifier_p
4413 = (cp_parser_nested_name_specifier_opt (parser,
4414 /*typename_keyword_p=*/false,
4419 /* If there is a nested-name-specifier, then we are looking at
4420 the first qualified-id production. */
4421 if (nested_name_specifier_p)
4424 tree saved_object_scope;
4425 tree saved_qualifying_scope;
4426 tree unqualified_id;
4429 /* See if the next token is the `template' keyword. */
4431 template_p = &is_template;
4432 *template_p = cp_parser_optional_template_keyword (parser);
4433 /* Name lookup we do during the processing of the
4434 unqualified-id might obliterate SCOPE. */
4435 saved_scope = parser->scope;
4436 saved_object_scope = parser->object_scope;
4437 saved_qualifying_scope = parser->qualifying_scope;
4438 /* Process the final unqualified-id. */
4439 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4442 /*optional_p=*/false);
4443 /* Restore the SAVED_SCOPE for our caller. */
4444 parser->scope = saved_scope;
4445 parser->object_scope = saved_object_scope;
4446 parser->qualifying_scope = saved_qualifying_scope;
4448 return unqualified_id;
4450 /* Otherwise, if we are in global scope, then we are looking at one
4451 of the other qualified-id productions. */
4452 else if (global_scope_p)
4457 /* Peek at the next token. */
4458 token = cp_lexer_peek_token (parser->lexer);
4460 /* If it's an identifier, and the next token is not a "<", then
4461 we can avoid the template-id case. This is an optimization
4462 for this common case. */
4463 if (token->type == CPP_NAME
4464 && !cp_parser_nth_token_starts_template_argument_list_p
4466 return cp_parser_identifier (parser);
4468 cp_parser_parse_tentatively (parser);
4469 /* Try a template-id. */
4470 id = cp_parser_template_id (parser,
4471 /*template_keyword_p=*/false,
4472 /*check_dependency_p=*/true,
4474 /* If that worked, we're done. */
4475 if (cp_parser_parse_definitely (parser))
4478 /* Peek at the next token. (Changes in the token buffer may
4479 have invalidated the pointer obtained above.) */
4480 token = cp_lexer_peek_token (parser->lexer);
4482 switch (token->type)
4485 return cp_parser_identifier (parser);
4488 if (token->keyword == RID_OPERATOR)
4489 return cp_parser_operator_function_id (parser);
4493 cp_parser_error (parser, "expected id-expression");
4494 return error_mark_node;
4498 return cp_parser_unqualified_id (parser, template_keyword_p,
4499 /*check_dependency_p=*/true,
4504 /* Parse an unqualified-id.
4508 operator-function-id
4509 conversion-function-id
4513 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4514 keyword, in a construct like `A::template ...'.
4516 Returns a representation of unqualified-id. For the `identifier'
4517 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4518 production a BIT_NOT_EXPR is returned; the operand of the
4519 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4520 other productions, see the documentation accompanying the
4521 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4522 names are looked up in uninstantiated templates. If DECLARATOR_P
4523 is true, the unqualified-id is appearing as part of a declarator,
4524 rather than as part of an expression. */
4527 cp_parser_unqualified_id (cp_parser* parser,
4528 bool template_keyword_p,
4529 bool check_dependency_p,
4535 /* Peek at the next token. */
4536 token = cp_lexer_peek_token (parser->lexer);
4538 switch (token->type)
4544 /* We don't know yet whether or not this will be a
4546 cp_parser_parse_tentatively (parser);
4547 /* Try a template-id. */
4548 id = cp_parser_template_id (parser, template_keyword_p,
4551 /* If it worked, we're done. */
4552 if (cp_parser_parse_definitely (parser))
4554 /* Otherwise, it's an ordinary identifier. */
4555 return cp_parser_identifier (parser);
4558 case CPP_TEMPLATE_ID:
4559 return cp_parser_template_id (parser, template_keyword_p,
4566 tree qualifying_scope;
4571 /* Consume the `~' token. */
4572 cp_lexer_consume_token (parser->lexer);
4573 /* Parse the class-name. The standard, as written, seems to
4576 template <typename T> struct S { ~S (); };
4577 template <typename T> S<T>::~S() {}
4579 is invalid, since `~' must be followed by a class-name, but
4580 `S<T>' is dependent, and so not known to be a class.
4581 That's not right; we need to look in uninstantiated
4582 templates. A further complication arises from:
4584 template <typename T> void f(T t) {
4588 Here, it is not possible to look up `T' in the scope of `T'
4589 itself. We must look in both the current scope, and the
4590 scope of the containing complete expression.
4592 Yet another issue is:
4601 The standard does not seem to say that the `S' in `~S'
4602 should refer to the type `S' and not the data member
4605 /* DR 244 says that we look up the name after the "~" in the
4606 same scope as we looked up the qualifying name. That idea
4607 isn't fully worked out; it's more complicated than that. */
4608 scope = parser->scope;
4609 object_scope = parser->object_scope;
4610 qualifying_scope = parser->qualifying_scope;
4612 /* Check for invalid scopes. */
4613 if (scope == error_mark_node)
4615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4616 cp_lexer_consume_token (parser->lexer);
4617 return error_mark_node;
4619 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4621 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4622 error_at (token->location,
4623 "scope %qT before %<~%> is not a class-name",
4625 cp_parser_simulate_error (parser);
4626 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4627 cp_lexer_consume_token (parser->lexer);
4628 return error_mark_node;
4630 gcc_assert (!scope || TYPE_P (scope));
4632 /* If the name is of the form "X::~X" it's OK even if X is a
4634 token = cp_lexer_peek_token (parser->lexer);
4636 && token->type == CPP_NAME
4637 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4639 && (token->u.value == TYPE_IDENTIFIER (scope)
4640 || (CLASS_TYPE_P (scope)
4641 && constructor_name_p (token->u.value, scope))))
4643 cp_lexer_consume_token (parser->lexer);
4644 return build_nt (BIT_NOT_EXPR, scope);
4647 /* If there was an explicit qualification (S::~T), first look
4648 in the scope given by the qualification (i.e., S).
4650 Note: in the calls to cp_parser_class_name below we pass
4651 typename_type so that lookup finds the injected-class-name
4652 rather than the constructor. */
4654 type_decl = NULL_TREE;
4657 cp_parser_parse_tentatively (parser);
4658 type_decl = cp_parser_class_name (parser,
4659 /*typename_keyword_p=*/false,
4660 /*template_keyword_p=*/false,
4662 /*check_dependency=*/false,
4663 /*class_head_p=*/false,
4665 if (cp_parser_parse_definitely (parser))
4668 /* In "N::S::~S", look in "N" as well. */
4669 if (!done && scope && qualifying_scope)
4671 cp_parser_parse_tentatively (parser);
4672 parser->scope = qualifying_scope;
4673 parser->object_scope = NULL_TREE;
4674 parser->qualifying_scope = NULL_TREE;
4676 = cp_parser_class_name (parser,
4677 /*typename_keyword_p=*/false,
4678 /*template_keyword_p=*/false,
4680 /*check_dependency=*/false,
4681 /*class_head_p=*/false,
4683 if (cp_parser_parse_definitely (parser))
4686 /* In "p->S::~T", look in the scope given by "*p" as well. */
4687 else if (!done && object_scope)
4689 cp_parser_parse_tentatively (parser);
4690 parser->scope = object_scope;
4691 parser->object_scope = NULL_TREE;
4692 parser->qualifying_scope = NULL_TREE;
4694 = cp_parser_class_name (parser,
4695 /*typename_keyword_p=*/false,
4696 /*template_keyword_p=*/false,
4698 /*check_dependency=*/false,
4699 /*class_head_p=*/false,
4701 if (cp_parser_parse_definitely (parser))
4704 /* Look in the surrounding context. */
4707 parser->scope = NULL_TREE;
4708 parser->object_scope = NULL_TREE;
4709 parser->qualifying_scope = NULL_TREE;
4710 if (processing_template_decl)
4711 cp_parser_parse_tentatively (parser);
4713 = cp_parser_class_name (parser,
4714 /*typename_keyword_p=*/false,
4715 /*template_keyword_p=*/false,
4717 /*check_dependency=*/false,
4718 /*class_head_p=*/false,
4720 if (processing_template_decl
4721 && ! cp_parser_parse_definitely (parser))
4723 /* We couldn't find a type with this name, so just accept
4724 it and check for a match at instantiation time. */
4725 type_decl = cp_parser_identifier (parser);
4726 if (type_decl != error_mark_node)
4727 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4731 /* If an error occurred, assume that the name of the
4732 destructor is the same as the name of the qualifying
4733 class. That allows us to keep parsing after running
4734 into ill-formed destructor names. */
4735 if (type_decl == error_mark_node && scope)
4736 return build_nt (BIT_NOT_EXPR, scope);
4737 else if (type_decl == error_mark_node)
4738 return error_mark_node;
4740 /* Check that destructor name and scope match. */
4741 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4743 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4744 error_at (token->location,
4745 "declaration of %<~%T%> as member of %qT",
4747 cp_parser_simulate_error (parser);
4748 return error_mark_node;
4753 A typedef-name that names a class shall not be used as the
4754 identifier in the declarator for a destructor declaration. */
4756 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4757 && !DECL_SELF_REFERENCE_P (type_decl)
4758 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4759 error_at (token->location,
4760 "typedef-name %qD used as destructor declarator",
4763 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4767 if (token->keyword == RID_OPERATOR)
4771 /* This could be a template-id, so we try that first. */
4772 cp_parser_parse_tentatively (parser);
4773 /* Try a template-id. */
4774 id = cp_parser_template_id (parser, template_keyword_p,
4775 /*check_dependency_p=*/true,
4777 /* If that worked, we're done. */
4778 if (cp_parser_parse_definitely (parser))
4780 /* We still don't know whether we're looking at an
4781 operator-function-id or a conversion-function-id. */
4782 cp_parser_parse_tentatively (parser);
4783 /* Try an operator-function-id. */
4784 id = cp_parser_operator_function_id (parser);
4785 /* If that didn't work, try a conversion-function-id. */
4786 if (!cp_parser_parse_definitely (parser))
4787 id = cp_parser_conversion_function_id (parser);
4788 else if (UDLIT_OPER_P (id))
4791 const char *name = UDLIT_OP_SUFFIX (id);
4792 if (name[0] != '_' && !in_system_header)
4793 warning (0, "literal operator suffixes not preceded by %<_%>"
4794 " are reserved for future standardization");
4804 cp_parser_error (parser, "expected unqualified-id");
4805 return error_mark_node;
4809 /* Parse an (optional) nested-name-specifier.
4811 nested-name-specifier: [C++98]
4812 class-or-namespace-name :: nested-name-specifier [opt]
4813 class-or-namespace-name :: template nested-name-specifier [opt]
4815 nested-name-specifier: [C++0x]
4818 nested-name-specifier identifier ::
4819 nested-name-specifier template [opt] simple-template-id ::
4821 PARSER->SCOPE should be set appropriately before this function is
4822 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4823 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4826 Sets PARSER->SCOPE to the class (TYPE) or namespace
4827 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4828 it unchanged if there is no nested-name-specifier. Returns the new
4829 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4831 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4832 part of a declaration and/or decl-specifier. */
4835 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4836 bool typename_keyword_p,
4837 bool check_dependency_p,
4839 bool is_declaration)
4841 bool success = false;
4842 cp_token_position start = 0;
4845 /* Remember where the nested-name-specifier starts. */
4846 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4848 start = cp_lexer_token_position (parser->lexer, false);
4849 push_deferring_access_checks (dk_deferred);
4856 tree saved_qualifying_scope;
4857 bool template_keyword_p;
4859 /* Spot cases that cannot be the beginning of a
4860 nested-name-specifier. */
4861 token = cp_lexer_peek_token (parser->lexer);
4863 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4864 the already parsed nested-name-specifier. */
4865 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4867 /* Grab the nested-name-specifier and continue the loop. */
4868 cp_parser_pre_parsed_nested_name_specifier (parser);
4869 /* If we originally encountered this nested-name-specifier
4870 with IS_DECLARATION set to false, we will not have
4871 resolved TYPENAME_TYPEs, so we must do so here. */
4873 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4875 new_scope = resolve_typename_type (parser->scope,
4876 /*only_current_p=*/false);
4877 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4878 parser->scope = new_scope;
4884 /* Spot cases that cannot be the beginning of a
4885 nested-name-specifier. On the second and subsequent times
4886 through the loop, we look for the `template' keyword. */
4887 if (success && token->keyword == RID_TEMPLATE)
4889 /* A template-id can start a nested-name-specifier. */
4890 else if (token->type == CPP_TEMPLATE_ID)
4892 /* DR 743: decltype can be used in a nested-name-specifier. */
4893 else if (token_is_decltype (token))
4897 /* If the next token is not an identifier, then it is
4898 definitely not a type-name or namespace-name. */
4899 if (token->type != CPP_NAME)
4901 /* If the following token is neither a `<' (to begin a
4902 template-id), nor a `::', then we are not looking at a
4903 nested-name-specifier. */
4904 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4906 if (token->type == CPP_COLON
4907 && parser->colon_corrects_to_scope_p
4908 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4910 error_at (token->location,
4911 "found %<:%> in nested-name-specifier, expected %<::%>");
4912 token->type = CPP_SCOPE;
4915 if (token->type != CPP_SCOPE
4916 && !cp_parser_nth_token_starts_template_argument_list_p
4921 /* The nested-name-specifier is optional, so we parse
4923 cp_parser_parse_tentatively (parser);
4925 /* Look for the optional `template' keyword, if this isn't the
4926 first time through the loop. */
4928 template_keyword_p = cp_parser_optional_template_keyword (parser);
4930 template_keyword_p = false;
4932 /* Save the old scope since the name lookup we are about to do
4933 might destroy it. */
4934 old_scope = parser->scope;
4935 saved_qualifying_scope = parser->qualifying_scope;
4936 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4937 look up names in "X<T>::I" in order to determine that "Y" is
4938 a template. So, if we have a typename at this point, we make
4939 an effort to look through it. */
4941 && !typename_keyword_p
4943 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4944 parser->scope = resolve_typename_type (parser->scope,
4945 /*only_current_p=*/false);
4946 /* Parse the qualifying entity. */
4948 = cp_parser_qualifying_entity (parser,
4954 /* Look for the `::' token. */
4955 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4957 /* If we found what we wanted, we keep going; otherwise, we're
4959 if (!cp_parser_parse_definitely (parser))
4961 bool error_p = false;
4963 /* Restore the OLD_SCOPE since it was valid before the
4964 failed attempt at finding the last
4965 class-or-namespace-name. */
4966 parser->scope = old_scope;
4967 parser->qualifying_scope = saved_qualifying_scope;
4969 /* If the next token is a decltype, and the one after that is a
4970 `::', then the decltype has failed to resolve to a class or
4971 enumeration type. Give this error even when parsing
4972 tentatively since it can't possibly be valid--and we're going
4973 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4974 won't get another chance.*/
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4976 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4979 token = cp_lexer_consume_token (parser->lexer);
4980 error_at (token->location, "decltype evaluates to %qT, "
4981 "which is not a class or enumeration type",
4983 parser->scope = error_mark_node;
4987 cp_lexer_consume_token (parser->lexer);
4990 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4992 /* If the next token is an identifier, and the one after
4993 that is a `::', then any valid interpretation would have
4994 found a class-or-namespace-name. */
4995 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4996 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4998 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5001 token = cp_lexer_consume_token (parser->lexer);
5004 if (!token->ambiguous_p)
5007 tree ambiguous_decls;
5009 decl = cp_parser_lookup_name (parser, token->u.value,
5011 /*is_template=*/false,
5012 /*is_namespace=*/false,
5013 /*check_dependency=*/true,
5016 if (TREE_CODE (decl) == TEMPLATE_DECL)
5017 error_at (token->location,
5018 "%qD used without template parameters",
5020 else if (ambiguous_decls)
5022 error_at (token->location,
5023 "reference to %qD is ambiguous",
5025 print_candidates (ambiguous_decls);
5026 decl = error_mark_node;
5030 if (cxx_dialect != cxx98)
5031 cp_parser_name_lookup_error
5032 (parser, token->u.value, decl, NLE_NOT_CXX98,
5035 cp_parser_name_lookup_error
5036 (parser, token->u.value, decl, NLE_CXX98,
5040 parser->scope = error_mark_node;
5042 /* Treat this as a successful nested-name-specifier
5047 If the name found is not a class-name (clause
5048 _class_) or namespace-name (_namespace.def_), the
5049 program is ill-formed. */
5052 cp_lexer_consume_token (parser->lexer);
5056 /* We've found one valid nested-name-specifier. */
5058 /* Name lookup always gives us a DECL. */
5059 if (TREE_CODE (new_scope) == TYPE_DECL)
5060 new_scope = TREE_TYPE (new_scope);
5061 /* Uses of "template" must be followed by actual templates. */
5062 if (template_keyword_p
5063 && !(CLASS_TYPE_P (new_scope)
5064 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5065 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5066 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5067 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5068 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5069 == TEMPLATE_ID_EXPR)))
5070 permerror (input_location, TYPE_P (new_scope)
5071 ? G_("%qT is not a template")
5072 : G_("%qD is not a template"),
5074 /* If it is a class scope, try to complete it; we are about to
5075 be looking up names inside the class. */
5076 if (TYPE_P (new_scope)
5077 /* Since checking types for dependency can be expensive,
5078 avoid doing it if the type is already complete. */
5079 && !COMPLETE_TYPE_P (new_scope)
5080 /* Do not try to complete dependent types. */
5081 && !dependent_type_p (new_scope))
5083 new_scope = complete_type (new_scope);
5084 /* If it is a typedef to current class, use the current
5085 class instead, as the typedef won't have any names inside
5087 if (!COMPLETE_TYPE_P (new_scope)
5088 && currently_open_class (new_scope))
5089 new_scope = TYPE_MAIN_VARIANT (new_scope);
5091 /* Make sure we look in the right scope the next time through
5093 parser->scope = new_scope;
5096 /* If parsing tentatively, replace the sequence of tokens that makes
5097 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5098 token. That way, should we re-parse the token stream, we will
5099 not have to repeat the effort required to do the parse, nor will
5100 we issue duplicate error messages. */
5101 if (success && start)
5105 token = cp_lexer_token_at (parser->lexer, start);
5106 /* Reset the contents of the START token. */
5107 token->type = CPP_NESTED_NAME_SPECIFIER;
5108 /* Retrieve any deferred checks. Do not pop this access checks yet
5109 so the memory will not be reclaimed during token replacing below. */
5110 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5111 token->u.tree_check_value->value = parser->scope;
5112 token->u.tree_check_value->checks = get_deferred_access_checks ();
5113 token->u.tree_check_value->qualifying_scope =
5114 parser->qualifying_scope;
5115 token->keyword = RID_MAX;
5117 /* Purge all subsequent tokens. */
5118 cp_lexer_purge_tokens_after (parser->lexer, start);
5122 pop_to_parent_deferring_access_checks ();
5124 return success ? parser->scope : NULL_TREE;
5127 /* Parse a nested-name-specifier. See
5128 cp_parser_nested_name_specifier_opt for details. This function
5129 behaves identically, except that it will an issue an error if no
5130 nested-name-specifier is present. */
5133 cp_parser_nested_name_specifier (cp_parser *parser,
5134 bool typename_keyword_p,
5135 bool check_dependency_p,
5137 bool is_declaration)
5141 /* Look for the nested-name-specifier. */
5142 scope = cp_parser_nested_name_specifier_opt (parser,
5147 /* If it was not present, issue an error message. */
5150 cp_parser_error (parser, "expected nested-name-specifier");
5151 parser->scope = NULL_TREE;
5157 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5158 this is either a class-name or a namespace-name (which corresponds
5159 to the class-or-namespace-name production in the grammar). For
5160 C++0x, it can also be a type-name that refers to an enumeration
5161 type or a simple-template-id.
5163 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5164 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5165 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5166 TYPE_P is TRUE iff the next name should be taken as a class-name,
5167 even the same name is declared to be another entity in the same
5170 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5171 specified by the class-or-namespace-name. If neither is found the
5172 ERROR_MARK_NODE is returned. */
5175 cp_parser_qualifying_entity (cp_parser *parser,
5176 bool typename_keyword_p,
5177 bool template_keyword_p,
5178 bool check_dependency_p,
5180 bool is_declaration)
5183 tree saved_qualifying_scope;
5184 tree saved_object_scope;
5187 bool successful_parse_p;
5189 /* DR 743: decltype can appear in a nested-name-specifier. */
5190 if (cp_lexer_next_token_is_decltype (parser->lexer))
5192 scope = cp_parser_decltype (parser);
5193 if (TREE_CODE (scope) != ENUMERAL_TYPE
5194 && !MAYBE_CLASS_TYPE_P (scope))
5196 cp_parser_simulate_error (parser);
5197 return error_mark_node;
5199 if (TYPE_NAME (scope))
5200 scope = TYPE_NAME (scope);
5204 /* Before we try to parse the class-name, we must save away the
5205 current PARSER->SCOPE since cp_parser_class_name will destroy
5207 saved_scope = parser->scope;
5208 saved_qualifying_scope = parser->qualifying_scope;
5209 saved_object_scope = parser->object_scope;
5210 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5211 there is no need to look for a namespace-name. */
5212 only_class_p = template_keyword_p
5213 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5215 cp_parser_parse_tentatively (parser);
5216 scope = cp_parser_class_name (parser,
5219 type_p ? class_type : none_type,
5221 /*class_head_p=*/false,
5223 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5224 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5226 && cxx_dialect != cxx98
5227 && !successful_parse_p)
5229 /* Restore the saved scope. */
5230 parser->scope = saved_scope;
5231 parser->qualifying_scope = saved_qualifying_scope;
5232 parser->object_scope = saved_object_scope;
5234 /* Parse tentatively. */
5235 cp_parser_parse_tentatively (parser);
5237 /* Parse a type-name */
5238 scope = cp_parser_type_name (parser);
5240 /* "If the name found does not designate a namespace or a class,
5241 enumeration, or dependent type, the program is ill-formed."
5243 We cover classes and dependent types above and namespaces below,
5244 so this code is only looking for enums. */
5245 if (!scope || TREE_CODE (scope) != TYPE_DECL
5246 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5247 cp_parser_simulate_error (parser);
5249 successful_parse_p = cp_parser_parse_definitely (parser);
5251 /* If that didn't work, try for a namespace-name. */
5252 if (!only_class_p && !successful_parse_p)
5254 /* Restore the saved scope. */
5255 parser->scope = saved_scope;
5256 parser->qualifying_scope = saved_qualifying_scope;
5257 parser->object_scope = saved_object_scope;
5258 /* If we are not looking at an identifier followed by the scope
5259 resolution operator, then this is not part of a
5260 nested-name-specifier. (Note that this function is only used
5261 to parse the components of a nested-name-specifier.) */
5262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5263 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5264 return error_mark_node;
5265 scope = cp_parser_namespace_name (parser);
5271 /* Parse a postfix-expression.
5275 postfix-expression [ expression ]
5276 postfix-expression ( expression-list [opt] )
5277 simple-type-specifier ( expression-list [opt] )
5278 typename :: [opt] nested-name-specifier identifier
5279 ( expression-list [opt] )
5280 typename :: [opt] nested-name-specifier template [opt] template-id
5281 ( expression-list [opt] )
5282 postfix-expression . template [opt] id-expression
5283 postfix-expression -> template [opt] id-expression
5284 postfix-expression . pseudo-destructor-name
5285 postfix-expression -> pseudo-destructor-name
5286 postfix-expression ++
5287 postfix-expression --
5288 dynamic_cast < type-id > ( expression )
5289 static_cast < type-id > ( expression )
5290 reinterpret_cast < type-id > ( expression )
5291 const_cast < type-id > ( expression )
5292 typeid ( expression )
5298 ( type-id ) { initializer-list , [opt] }
5300 This extension is a GNU version of the C99 compound-literal
5301 construct. (The C99 grammar uses `type-name' instead of `type-id',
5302 but they are essentially the same concept.)
5304 If ADDRESS_P is true, the postfix expression is the operand of the
5305 `&' operator. CAST_P is true if this expression is the target of a
5308 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5309 class member access expressions [expr.ref].
5311 Returns a representation of the expression. */
5314 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5315 bool member_access_only_p,
5316 cp_id_kind * pidk_return)
5320 cp_id_kind idk = CP_ID_KIND_NONE;
5321 tree postfix_expression = NULL_TREE;
5322 bool is_member_access = false;
5324 /* Peek at the next token. */
5325 token = cp_lexer_peek_token (parser->lexer);
5326 /* Some of the productions are determined by keywords. */
5327 keyword = token->keyword;
5337 const char *saved_message;
5339 /* All of these can be handled in the same way from the point
5340 of view of parsing. Begin by consuming the token
5341 identifying the cast. */
5342 cp_lexer_consume_token (parser->lexer);
5344 /* New types cannot be defined in the cast. */
5345 saved_message = parser->type_definition_forbidden_message;
5346 parser->type_definition_forbidden_message
5347 = G_("types may not be defined in casts");
5349 /* Look for the opening `<'. */
5350 cp_parser_require (parser, CPP_LESS, RT_LESS);
5351 /* Parse the type to which we are casting. */
5352 type = cp_parser_type_id (parser);
5353 /* Look for the closing `>'. */
5354 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5355 /* Restore the old message. */
5356 parser->type_definition_forbidden_message = saved_message;
5358 /* And the expression which is being cast. */
5359 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5360 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5361 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5363 /* Only type conversions to integral or enumeration types
5364 can be used in constant-expressions. */
5365 if (!cast_valid_in_integral_constant_expression_p (type)
5366 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5367 return error_mark_node;
5373 = build_dynamic_cast (type, expression, tf_warning_or_error);
5377 = build_static_cast (type, expression, tf_warning_or_error);
5381 = build_reinterpret_cast (type, expression,
5382 tf_warning_or_error);
5386 = build_const_cast (type, expression, tf_warning_or_error);
5397 const char *saved_message;
5398 bool saved_in_type_id_in_expr_p;
5400 /* Consume the `typeid' token. */
5401 cp_lexer_consume_token (parser->lexer);
5402 /* Look for the `(' token. */
5403 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5404 /* Types cannot be defined in a `typeid' expression. */
5405 saved_message = parser->type_definition_forbidden_message;
5406 parser->type_definition_forbidden_message
5407 = G_("types may not be defined in a %<typeid%> expression");
5408 /* We can't be sure yet whether we're looking at a type-id or an
5410 cp_parser_parse_tentatively (parser);
5411 /* Try a type-id first. */
5412 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5413 parser->in_type_id_in_expr_p = true;
5414 type = cp_parser_type_id (parser);
5415 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5416 /* Look for the `)' token. Otherwise, we can't be sure that
5417 we're not looking at an expression: consider `typeid (int
5418 (3))', for example. */
5419 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5420 /* If all went well, simply lookup the type-id. */
5421 if (cp_parser_parse_definitely (parser))
5422 postfix_expression = get_typeid (type);
5423 /* Otherwise, fall back to the expression variant. */
5428 /* Look for an expression. */
5429 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5430 /* Compute its typeid. */
5431 postfix_expression = build_typeid (expression);
5432 /* Look for the `)' token. */
5433 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5435 /* Restore the saved message. */
5436 parser->type_definition_forbidden_message = saved_message;
5437 /* `typeid' may not appear in an integral constant expression. */
5438 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5439 return error_mark_node;
5446 /* The syntax permitted here is the same permitted for an
5447 elaborated-type-specifier. */
5448 type = cp_parser_elaborated_type_specifier (parser,
5449 /*is_friend=*/false,
5450 /*is_declaration=*/false);
5451 postfix_expression = cp_parser_functional_cast (parser, type);
5459 /* If the next thing is a simple-type-specifier, we may be
5460 looking at a functional cast. We could also be looking at
5461 an id-expression. So, we try the functional cast, and if
5462 that doesn't work we fall back to the primary-expression. */
5463 cp_parser_parse_tentatively (parser);
5464 /* Look for the simple-type-specifier. */
5465 type = cp_parser_simple_type_specifier (parser,
5466 /*decl_specs=*/NULL,
5467 CP_PARSER_FLAGS_NONE);
5468 /* Parse the cast itself. */
5469 if (!cp_parser_error_occurred (parser))
5471 = cp_parser_functional_cast (parser, type);
5472 /* If that worked, we're done. */
5473 if (cp_parser_parse_definitely (parser))
5476 /* If the functional-cast didn't work out, try a
5477 compound-literal. */
5478 if (cp_parser_allow_gnu_extensions_p (parser)
5479 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5481 VEC(constructor_elt,gc) *initializer_list = NULL;
5482 bool saved_in_type_id_in_expr_p;
5484 cp_parser_parse_tentatively (parser);
5485 /* Consume the `('. */
5486 cp_lexer_consume_token (parser->lexer);
5487 /* Parse the type. */
5488 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5489 parser->in_type_id_in_expr_p = true;
5490 type = cp_parser_type_id (parser);
5491 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5492 /* Look for the `)'. */
5493 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5494 /* Look for the `{'. */
5495 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5496 /* If things aren't going well, there's no need to
5498 if (!cp_parser_error_occurred (parser))
5500 bool non_constant_p;
5501 /* Parse the initializer-list. */
5503 = cp_parser_initializer_list (parser, &non_constant_p);
5504 /* Allow a trailing `,'. */
5505 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5506 cp_lexer_consume_token (parser->lexer);
5507 /* Look for the final `}'. */
5508 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5510 /* If that worked, we're definitely looking at a
5511 compound-literal expression. */
5512 if (cp_parser_parse_definitely (parser))
5514 /* Warn the user that a compound literal is not
5515 allowed in standard C++. */
5516 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5517 /* For simplicity, we disallow compound literals in
5518 constant-expressions. We could
5519 allow compound literals of integer type, whose
5520 initializer was a constant, in constant
5521 expressions. Permitting that usage, as a further
5522 extension, would not change the meaning of any
5523 currently accepted programs. (Of course, as
5524 compound literals are not part of ISO C++, the
5525 standard has nothing to say.) */
5526 if (cp_parser_non_integral_constant_expression (parser,
5529 postfix_expression = error_mark_node;
5532 /* Form the representation of the compound-literal. */
5534 = (finish_compound_literal
5535 (type, build_constructor (init_list_type_node,
5537 tf_warning_or_error));
5542 /* It must be a primary-expression. */
5544 = cp_parser_primary_expression (parser, address_p, cast_p,
5545 /*template_arg_p=*/false,
5551 /* Keep looping until the postfix-expression is complete. */
5554 if (idk == CP_ID_KIND_UNQUALIFIED
5555 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5556 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5557 /* It is not a Koenig lookup function call. */
5559 = unqualified_name_lookup_error (postfix_expression);
5561 /* Peek at the next token. */
5562 token = cp_lexer_peek_token (parser->lexer);
5564 switch (token->type)
5566 case CPP_OPEN_SQUARE:
5568 = cp_parser_postfix_open_square_expression (parser,
5571 idk = CP_ID_KIND_NONE;
5572 is_member_access = false;
5575 case CPP_OPEN_PAREN:
5576 /* postfix-expression ( expression-list [opt] ) */
5579 bool is_builtin_constant_p;
5580 bool saved_integral_constant_expression_p = false;
5581 bool saved_non_integral_constant_expression_p = false;
5584 is_member_access = false;
5586 is_builtin_constant_p
5587 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5588 if (is_builtin_constant_p)
5590 /* The whole point of __builtin_constant_p is to allow
5591 non-constant expressions to appear as arguments. */
5592 saved_integral_constant_expression_p
5593 = parser->integral_constant_expression_p;
5594 saved_non_integral_constant_expression_p
5595 = parser->non_integral_constant_expression_p;
5596 parser->integral_constant_expression_p = false;
5598 args = (cp_parser_parenthesized_expression_list
5600 /*cast_p=*/false, /*allow_expansion_p=*/true,
5601 /*non_constant_p=*/NULL));
5602 if (is_builtin_constant_p)
5604 parser->integral_constant_expression_p
5605 = saved_integral_constant_expression_p;
5606 parser->non_integral_constant_expression_p
5607 = saved_non_integral_constant_expression_p;
5612 postfix_expression = error_mark_node;
5616 /* Function calls are not permitted in
5617 constant-expressions. */
5618 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5619 && cp_parser_non_integral_constant_expression (parser,
5622 postfix_expression = error_mark_node;
5623 release_tree_vector (args);
5628 if (idk == CP_ID_KIND_UNQUALIFIED
5629 || idk == CP_ID_KIND_TEMPLATE_ID)
5631 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5633 if (!VEC_empty (tree, args))
5636 if (!any_type_dependent_arguments_p (args))
5638 = perform_koenig_lookup (postfix_expression, args,
5639 /*include_std=*/false,
5640 tf_warning_or_error);
5644 = unqualified_fn_lookup_error (postfix_expression);
5646 /* We do not perform argument-dependent lookup if
5647 normal lookup finds a non-function, in accordance
5648 with the expected resolution of DR 218. */
5649 else if (!VEC_empty (tree, args)
5650 && is_overloaded_fn (postfix_expression))
5652 tree fn = get_first_fn (postfix_expression);
5653 fn = STRIP_TEMPLATE (fn);
5655 /* Do not do argument dependent lookup if regular
5656 lookup finds a member function or a block-scope
5657 function declaration. [basic.lookup.argdep]/3 */
5658 if (!DECL_FUNCTION_MEMBER_P (fn)
5659 && !DECL_LOCAL_FUNCTION_P (fn))
5662 if (!any_type_dependent_arguments_p (args))
5664 = perform_koenig_lookup (postfix_expression, args,
5665 /*include_std=*/false,
5666 tf_warning_or_error);
5671 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5673 tree instance = TREE_OPERAND (postfix_expression, 0);
5674 tree fn = TREE_OPERAND (postfix_expression, 1);
5676 if (processing_template_decl
5677 && (type_dependent_expression_p (instance)
5678 || (!BASELINK_P (fn)
5679 && TREE_CODE (fn) != FIELD_DECL)
5680 || type_dependent_expression_p (fn)
5681 || any_type_dependent_arguments_p (args)))
5684 = build_nt_call_vec (postfix_expression, args);
5685 release_tree_vector (args);
5689 if (BASELINK_P (fn))
5692 = (build_new_method_call
5693 (instance, fn, &args, NULL_TREE,
5694 (idk == CP_ID_KIND_QUALIFIED
5695 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5698 tf_warning_or_error));
5702 = finish_call_expr (postfix_expression, &args,
5703 /*disallow_virtual=*/false,
5705 tf_warning_or_error);
5707 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5708 || TREE_CODE (postfix_expression) == MEMBER_REF
5709 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5710 postfix_expression = (build_offset_ref_call_from_tree
5711 (postfix_expression, &args));
5712 else if (idk == CP_ID_KIND_QUALIFIED)
5713 /* A call to a static class member, or a namespace-scope
5716 = finish_call_expr (postfix_expression, &args,
5717 /*disallow_virtual=*/true,
5719 tf_warning_or_error);
5721 /* All other function calls. */
5723 = finish_call_expr (postfix_expression, &args,
5724 /*disallow_virtual=*/false,
5726 tf_warning_or_error);
5728 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5729 idk = CP_ID_KIND_NONE;
5731 release_tree_vector (args);
5737 /* postfix-expression . template [opt] id-expression
5738 postfix-expression . pseudo-destructor-name
5739 postfix-expression -> template [opt] id-expression
5740 postfix-expression -> pseudo-destructor-name */
5742 /* Consume the `.' or `->' operator. */
5743 cp_lexer_consume_token (parser->lexer);
5746 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5751 is_member_access = true;
5755 /* postfix-expression ++ */
5756 /* Consume the `++' token. */
5757 cp_lexer_consume_token (parser->lexer);
5758 /* Generate a representation for the complete expression. */
5760 = finish_increment_expr (postfix_expression,
5761 POSTINCREMENT_EXPR);
5762 /* Increments may not appear in constant-expressions. */
5763 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5764 postfix_expression = error_mark_node;
5765 idk = CP_ID_KIND_NONE;
5766 is_member_access = false;
5769 case CPP_MINUS_MINUS:
5770 /* postfix-expression -- */
5771 /* Consume the `--' token. */
5772 cp_lexer_consume_token (parser->lexer);
5773 /* Generate a representation for the complete expression. */
5775 = finish_increment_expr (postfix_expression,
5776 POSTDECREMENT_EXPR);
5777 /* Decrements may not appear in constant-expressions. */
5778 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5779 postfix_expression = error_mark_node;
5780 idk = CP_ID_KIND_NONE;
5781 is_member_access = false;
5785 if (pidk_return != NULL)
5786 * pidk_return = idk;
5787 if (member_access_only_p)
5788 return is_member_access? postfix_expression : error_mark_node;
5790 return postfix_expression;
5794 /* We should never get here. */
5796 return error_mark_node;
5799 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5800 by cp_parser_builtin_offsetof. We're looking for
5802 postfix-expression [ expression ]
5804 FOR_OFFSETOF is set if we're being called in that context, which
5805 changes how we deal with integer constant expressions. */
5808 cp_parser_postfix_open_square_expression (cp_parser *parser,
5809 tree postfix_expression,
5814 /* Consume the `[' token. */
5815 cp_lexer_consume_token (parser->lexer);
5817 /* Parse the index expression. */
5818 /* ??? For offsetof, there is a question of what to allow here. If
5819 offsetof is not being used in an integral constant expression context,
5820 then we *could* get the right answer by computing the value at runtime.
5821 If we are in an integral constant expression context, then we might
5822 could accept any constant expression; hard to say without analysis.
5823 Rather than open the barn door too wide right away, allow only integer
5824 constant expressions here. */
5826 index = cp_parser_constant_expression (parser, false, NULL);
5828 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5830 /* Look for the closing `]'. */
5831 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5833 /* Build the ARRAY_REF. */
5834 postfix_expression = grok_array_decl (postfix_expression, index);
5836 /* When not doing offsetof, array references are not permitted in
5837 constant-expressions. */
5839 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5840 postfix_expression = error_mark_node;
5842 return postfix_expression;
5845 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5846 by cp_parser_builtin_offsetof. We're looking for
5848 postfix-expression . template [opt] id-expression
5849 postfix-expression . pseudo-destructor-name
5850 postfix-expression -> template [opt] id-expression
5851 postfix-expression -> pseudo-destructor-name
5853 FOR_OFFSETOF is set if we're being called in that context. That sorta
5854 limits what of the above we'll actually accept, but nevermind.
5855 TOKEN_TYPE is the "." or "->" token, which will already have been
5856 removed from the stream. */
5859 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5860 enum cpp_ttype token_type,
5861 tree postfix_expression,
5862 bool for_offsetof, cp_id_kind *idk,
5863 location_t location)
5867 bool pseudo_destructor_p;
5868 tree scope = NULL_TREE;
5870 /* If this is a `->' operator, dereference the pointer. */
5871 if (token_type == CPP_DEREF)
5872 postfix_expression = build_x_arrow (postfix_expression);
5873 /* Check to see whether or not the expression is type-dependent. */
5874 dependent_p = type_dependent_expression_p (postfix_expression);
5875 /* The identifier following the `->' or `.' is not qualified. */
5876 parser->scope = NULL_TREE;
5877 parser->qualifying_scope = NULL_TREE;
5878 parser->object_scope = NULL_TREE;
5879 *idk = CP_ID_KIND_NONE;
5881 /* Enter the scope corresponding to the type of the object
5882 given by the POSTFIX_EXPRESSION. */
5883 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5885 scope = TREE_TYPE (postfix_expression);
5886 /* According to the standard, no expression should ever have
5887 reference type. Unfortunately, we do not currently match
5888 the standard in this respect in that our internal representation
5889 of an expression may have reference type even when the standard
5890 says it does not. Therefore, we have to manually obtain the
5891 underlying type here. */
5892 scope = non_reference (scope);
5893 /* The type of the POSTFIX_EXPRESSION must be complete. */
5894 if (scope == unknown_type_node)
5896 error_at (location, "%qE does not have class type",
5897 postfix_expression);
5900 /* Unlike the object expression in other contexts, *this is not
5901 required to be of complete type for purposes of class member
5902 access (5.2.5) outside the member function body. */
5903 else if (scope != current_class_ref
5904 && !(processing_template_decl && scope == current_class_type))
5905 scope = complete_type_or_else (scope, NULL_TREE);
5906 /* Let the name lookup machinery know that we are processing a
5907 class member access expression. */
5908 parser->context->object_type = scope;
5909 /* If something went wrong, we want to be able to discern that case,
5910 as opposed to the case where there was no SCOPE due to the type
5911 of expression being dependent. */
5913 scope = error_mark_node;
5914 /* If the SCOPE was erroneous, make the various semantic analysis
5915 functions exit quickly -- and without issuing additional error
5917 if (scope == error_mark_node)
5918 postfix_expression = error_mark_node;
5921 /* Assume this expression is not a pseudo-destructor access. */
5922 pseudo_destructor_p = false;
5924 /* If the SCOPE is a scalar type, then, if this is a valid program,
5925 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5926 is type dependent, it can be pseudo-destructor-name or something else.
5927 Try to parse it as pseudo-destructor-name first. */
5928 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5933 cp_parser_parse_tentatively (parser);
5934 /* Parse the pseudo-destructor-name. */
5936 cp_parser_pseudo_destructor_name (parser, &s, &type);
5938 && (cp_parser_error_occurred (parser)
5939 || TREE_CODE (type) != TYPE_DECL
5940 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5941 cp_parser_abort_tentative_parse (parser);
5942 else if (cp_parser_parse_definitely (parser))
5944 pseudo_destructor_p = true;
5946 = finish_pseudo_destructor_expr (postfix_expression,
5947 s, TREE_TYPE (type));
5951 if (!pseudo_destructor_p)
5953 /* If the SCOPE is not a scalar type, we are looking at an
5954 ordinary class member access expression, rather than a
5955 pseudo-destructor-name. */
5957 cp_token *token = cp_lexer_peek_token (parser->lexer);
5958 /* Parse the id-expression. */
5959 name = (cp_parser_id_expression
5961 cp_parser_optional_template_keyword (parser),
5962 /*check_dependency_p=*/true,
5964 /*declarator_p=*/false,
5965 /*optional_p=*/false));
5966 /* In general, build a SCOPE_REF if the member name is qualified.
5967 However, if the name was not dependent and has already been
5968 resolved; there is no need to build the SCOPE_REF. For example;
5970 struct X { void f(); };
5971 template <typename T> void f(T* t) { t->X::f(); }
5973 Even though "t" is dependent, "X::f" is not and has been resolved
5974 to a BASELINK; there is no need to include scope information. */
5976 /* But we do need to remember that there was an explicit scope for
5977 virtual function calls. */
5979 *idk = CP_ID_KIND_QUALIFIED;
5981 /* If the name is a template-id that names a type, we will get a
5982 TYPE_DECL here. That is invalid code. */
5983 if (TREE_CODE (name) == TYPE_DECL)
5985 error_at (token->location, "invalid use of %qD", name);
5986 postfix_expression = error_mark_node;
5990 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5992 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
5994 error_at (token->location, "%<%D::%D%> is not a class member",
5995 parser->scope, name);
5996 postfix_expression = error_mark_node;
5999 name = build_qualified_name (/*type=*/NULL_TREE,
6003 parser->scope = NULL_TREE;
6004 parser->qualifying_scope = NULL_TREE;
6005 parser->object_scope = NULL_TREE;
6007 if (scope && name && BASELINK_P (name))
6008 adjust_result_of_qualified_name_lookup
6009 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6011 = finish_class_member_access_expr (postfix_expression, name,
6013 tf_warning_or_error);
6017 /* We no longer need to look up names in the scope of the object on
6018 the left-hand side of the `.' or `->' operator. */
6019 parser->context->object_type = NULL_TREE;
6021 /* Outside of offsetof, these operators may not appear in
6022 constant-expressions. */
6024 && (cp_parser_non_integral_constant_expression
6025 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6026 postfix_expression = error_mark_node;
6028 return postfix_expression;
6031 /* Parse a parenthesized expression-list.
6034 assignment-expression
6035 expression-list, assignment-expression
6040 identifier, expression-list
6042 CAST_P is true if this expression is the target of a cast.
6044 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6047 Returns a vector of trees. Each element is a representation of an
6048 assignment-expression. NULL is returned if the ( and or ) are
6049 missing. An empty, but allocated, vector is returned on no
6050 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6051 if we are parsing an attribute list for an attribute that wants a
6052 plain identifier argument, normal_attr for an attribute that wants
6053 an expression, or non_attr if we aren't parsing an attribute list. If
6054 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6055 not all of the expressions in the list were constant. */
6057 static VEC(tree,gc) *
6058 cp_parser_parenthesized_expression_list (cp_parser* parser,
6059 int is_attribute_list,
6061 bool allow_expansion_p,
6062 bool *non_constant_p)
6064 VEC(tree,gc) *expression_list;
6065 bool fold_expr_p = is_attribute_list != non_attr;
6066 tree identifier = NULL_TREE;
6067 bool saved_greater_than_is_operator_p;
6069 /* Assume all the expressions will be constant. */
6071 *non_constant_p = false;
6073 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6076 expression_list = make_tree_vector ();
6078 /* Within a parenthesized expression, a `>' token is always
6079 the greater-than operator. */
6080 saved_greater_than_is_operator_p
6081 = parser->greater_than_is_operator_p;
6082 parser->greater_than_is_operator_p = true;
6084 /* Consume expressions until there are no more. */
6085 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6090 /* At the beginning of attribute lists, check to see if the
6091 next token is an identifier. */
6092 if (is_attribute_list == id_attr
6093 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6097 /* Consume the identifier. */
6098 token = cp_lexer_consume_token (parser->lexer);
6099 /* Save the identifier. */
6100 identifier = token->u.value;
6104 bool expr_non_constant_p;
6106 /* Parse the next assignment-expression. */
6107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6109 /* A braced-init-list. */
6110 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6111 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6112 if (non_constant_p && expr_non_constant_p)
6113 *non_constant_p = true;
6115 else if (non_constant_p)
6117 expr = (cp_parser_constant_expression
6118 (parser, /*allow_non_constant_p=*/true,
6119 &expr_non_constant_p));
6120 if (expr_non_constant_p)
6121 *non_constant_p = true;
6124 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6127 expr = fold_non_dependent_expr (expr);
6129 /* If we have an ellipsis, then this is an expression
6131 if (allow_expansion_p
6132 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6134 /* Consume the `...'. */
6135 cp_lexer_consume_token (parser->lexer);
6137 /* Build the argument pack. */
6138 expr = make_pack_expansion (expr);
6141 /* Add it to the list. We add error_mark_node
6142 expressions to the list, so that we can still tell if
6143 the correct form for a parenthesized expression-list
6144 is found. That gives better errors. */
6145 VEC_safe_push (tree, gc, expression_list, expr);
6147 if (expr == error_mark_node)
6151 /* After the first item, attribute lists look the same as
6152 expression lists. */
6153 is_attribute_list = non_attr;
6156 /* If the next token isn't a `,', then we are done. */
6157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6160 /* Otherwise, consume the `,' and keep going. */
6161 cp_lexer_consume_token (parser->lexer);
6164 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6169 /* We try and resync to an unnested comma, as that will give the
6170 user better diagnostics. */
6171 ending = cp_parser_skip_to_closing_parenthesis (parser,
6172 /*recovering=*/true,
6174 /*consume_paren=*/true);
6179 parser->greater_than_is_operator_p
6180 = saved_greater_than_is_operator_p;
6185 parser->greater_than_is_operator_p
6186 = saved_greater_than_is_operator_p;
6189 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6191 return expression_list;
6194 /* Parse a pseudo-destructor-name.
6196 pseudo-destructor-name:
6197 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6198 :: [opt] nested-name-specifier template template-id :: ~ type-name
6199 :: [opt] nested-name-specifier [opt] ~ type-name
6201 If either of the first two productions is used, sets *SCOPE to the
6202 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6203 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6204 or ERROR_MARK_NODE if the parse fails. */
6207 cp_parser_pseudo_destructor_name (cp_parser* parser,
6211 bool nested_name_specifier_p;
6213 /* Assume that things will not work out. */
6214 *type = error_mark_node;
6216 /* Look for the optional `::' operator. */
6217 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6218 /* Look for the optional nested-name-specifier. */
6219 nested_name_specifier_p
6220 = (cp_parser_nested_name_specifier_opt (parser,
6221 /*typename_keyword_p=*/false,
6222 /*check_dependency_p=*/true,
6224 /*is_declaration=*/false)
6226 /* Now, if we saw a nested-name-specifier, we might be doing the
6227 second production. */
6228 if (nested_name_specifier_p
6229 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6231 /* Consume the `template' keyword. */
6232 cp_lexer_consume_token (parser->lexer);
6233 /* Parse the template-id. */
6234 cp_parser_template_id (parser,
6235 /*template_keyword_p=*/true,
6236 /*check_dependency_p=*/false,
6237 /*is_declaration=*/true);
6238 /* Look for the `::' token. */
6239 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6241 /* If the next token is not a `~', then there might be some
6242 additional qualification. */
6243 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6245 /* At this point, we're looking for "type-name :: ~". The type-name
6246 must not be a class-name, since this is a pseudo-destructor. So,
6247 it must be either an enum-name, or a typedef-name -- both of which
6248 are just identifiers. So, we peek ahead to check that the "::"
6249 and "~" tokens are present; if they are not, then we can avoid
6250 calling type_name. */
6251 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6252 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6253 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6255 cp_parser_error (parser, "non-scalar type");
6259 /* Look for the type-name. */
6260 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6261 if (*scope == error_mark_node)
6264 /* Look for the `::' token. */
6265 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6270 /* Look for the `~'. */
6271 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6273 /* Once we see the ~, this has to be a pseudo-destructor. */
6274 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6275 cp_parser_commit_to_tentative_parse (parser);
6277 /* Look for the type-name again. We are not responsible for
6278 checking that it matches the first type-name. */
6279 *type = cp_parser_nonclass_name (parser);
6282 /* Parse a unary-expression.
6288 unary-operator cast-expression
6289 sizeof unary-expression
6291 alignof ( type-id ) [C++0x]
6298 __extension__ cast-expression
6299 __alignof__ unary-expression
6300 __alignof__ ( type-id )
6301 alignof unary-expression [C++0x]
6302 __real__ cast-expression
6303 __imag__ cast-expression
6306 ADDRESS_P is true iff the unary-expression is appearing as the
6307 operand of the `&' operator. CAST_P is true if this expression is
6308 the target of a cast.
6310 Returns a representation of the expression. */
6313 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6317 enum tree_code unary_operator;
6319 /* Peek at the next token. */
6320 token = cp_lexer_peek_token (parser->lexer);
6321 /* Some keywords give away the kind of expression. */
6322 if (token->type == CPP_KEYWORD)
6324 enum rid keyword = token->keyword;
6334 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6335 /* Consume the token. */
6336 cp_lexer_consume_token (parser->lexer);
6337 /* Parse the operand. */
6338 operand = cp_parser_sizeof_operand (parser, keyword);
6340 if (TYPE_P (operand))
6341 return cxx_sizeof_or_alignof_type (operand, op, true);
6344 /* ISO C++ defines alignof only with types, not with
6345 expressions. So pedwarn if alignof is used with a non-
6346 type expression. However, __alignof__ is ok. */
6347 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6348 pedwarn (token->location, OPT_pedantic,
6349 "ISO C++ does not allow %<alignof%> "
6352 return cxx_sizeof_or_alignof_expr (operand, op, true);
6357 return cp_parser_new_expression (parser);
6360 return cp_parser_delete_expression (parser);
6364 /* The saved value of the PEDANTIC flag. */
6368 /* Save away the PEDANTIC flag. */
6369 cp_parser_extension_opt (parser, &saved_pedantic);
6370 /* Parse the cast-expression. */
6371 expr = cp_parser_simple_cast_expression (parser);
6372 /* Restore the PEDANTIC flag. */
6373 pedantic = saved_pedantic;
6383 /* Consume the `__real__' or `__imag__' token. */
6384 cp_lexer_consume_token (parser->lexer);
6385 /* Parse the cast-expression. */
6386 expression = cp_parser_simple_cast_expression (parser);
6387 /* Create the complete representation. */
6388 return build_x_unary_op ((keyword == RID_REALPART
6389 ? REALPART_EXPR : IMAGPART_EXPR),
6391 tf_warning_or_error);
6395 case RID_TRANSACTION_ATOMIC:
6396 case RID_TRANSACTION_RELAXED:
6397 return cp_parser_transaction_expression (parser, keyword);
6402 const char *saved_message;
6403 bool saved_integral_constant_expression_p;
6404 bool saved_non_integral_constant_expression_p;
6405 bool saved_greater_than_is_operator_p;
6407 cp_lexer_consume_token (parser->lexer);
6408 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6410 saved_message = parser->type_definition_forbidden_message;
6411 parser->type_definition_forbidden_message
6412 = G_("types may not be defined in %<noexcept%> expressions");
6414 saved_integral_constant_expression_p
6415 = parser->integral_constant_expression_p;
6416 saved_non_integral_constant_expression_p
6417 = parser->non_integral_constant_expression_p;
6418 parser->integral_constant_expression_p = false;
6420 saved_greater_than_is_operator_p
6421 = parser->greater_than_is_operator_p;
6422 parser->greater_than_is_operator_p = true;
6424 ++cp_unevaluated_operand;
6425 ++c_inhibit_evaluation_warnings;
6426 expr = cp_parser_expression (parser, false, NULL);
6427 --c_inhibit_evaluation_warnings;
6428 --cp_unevaluated_operand;
6430 parser->greater_than_is_operator_p
6431 = saved_greater_than_is_operator_p;
6433 parser->integral_constant_expression_p
6434 = saved_integral_constant_expression_p;
6435 parser->non_integral_constant_expression_p
6436 = saved_non_integral_constant_expression_p;
6438 parser->type_definition_forbidden_message = saved_message;
6440 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6441 return finish_noexcept_expr (expr, tf_warning_or_error);
6449 /* Look for the `:: new' and `:: delete', which also signal the
6450 beginning of a new-expression, or delete-expression,
6451 respectively. If the next token is `::', then it might be one of
6453 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6457 /* See if the token after the `::' is one of the keywords in
6458 which we're interested. */
6459 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6460 /* If it's `new', we have a new-expression. */
6461 if (keyword == RID_NEW)
6462 return cp_parser_new_expression (parser);
6463 /* Similarly, for `delete'. */
6464 else if (keyword == RID_DELETE)
6465 return cp_parser_delete_expression (parser);
6468 /* Look for a unary operator. */
6469 unary_operator = cp_parser_unary_operator (token);
6470 /* The `++' and `--' operators can be handled similarly, even though
6471 they are not technically unary-operators in the grammar. */
6472 if (unary_operator == ERROR_MARK)
6474 if (token->type == CPP_PLUS_PLUS)
6475 unary_operator = PREINCREMENT_EXPR;
6476 else if (token->type == CPP_MINUS_MINUS)
6477 unary_operator = PREDECREMENT_EXPR;
6478 /* Handle the GNU address-of-label extension. */
6479 else if (cp_parser_allow_gnu_extensions_p (parser)
6480 && token->type == CPP_AND_AND)
6484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6486 /* Consume the '&&' token. */
6487 cp_lexer_consume_token (parser->lexer);
6488 /* Look for the identifier. */
6489 identifier = cp_parser_identifier (parser);
6490 /* Create an expression representing the address. */
6491 expression = finish_label_address_expr (identifier, loc);
6492 if (cp_parser_non_integral_constant_expression (parser,
6494 expression = error_mark_node;
6498 if (unary_operator != ERROR_MARK)
6500 tree cast_expression;
6501 tree expression = error_mark_node;
6502 non_integral_constant non_constant_p = NIC_NONE;
6504 /* Consume the operator token. */
6505 token = cp_lexer_consume_token (parser->lexer);
6506 /* Parse the cast-expression. */
6508 = cp_parser_cast_expression (parser,
6509 unary_operator == ADDR_EXPR,
6510 /*cast_p=*/false, pidk);
6511 /* Now, build an appropriate representation. */
6512 switch (unary_operator)
6515 non_constant_p = NIC_STAR;
6516 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6517 tf_warning_or_error);
6521 non_constant_p = NIC_ADDR;
6524 expression = build_x_unary_op (unary_operator, cast_expression,
6525 tf_warning_or_error);
6528 case PREINCREMENT_EXPR:
6529 case PREDECREMENT_EXPR:
6530 non_constant_p = unary_operator == PREINCREMENT_EXPR
6531 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6533 case UNARY_PLUS_EXPR:
6535 case TRUTH_NOT_EXPR:
6536 expression = finish_unary_op_expr (unary_operator, cast_expression);
6543 if (non_constant_p != NIC_NONE
6544 && cp_parser_non_integral_constant_expression (parser,
6546 expression = error_mark_node;
6551 return cp_parser_postfix_expression (parser, address_p, cast_p,
6552 /*member_access_only_p=*/false,
6556 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6557 unary-operator, the corresponding tree code is returned. */
6559 static enum tree_code
6560 cp_parser_unary_operator (cp_token* token)
6562 switch (token->type)
6565 return INDIRECT_REF;
6571 return UNARY_PLUS_EXPR;
6577 return TRUTH_NOT_EXPR;
6580 return BIT_NOT_EXPR;
6587 /* Parse a new-expression.
6590 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6591 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6593 Returns a representation of the expression. */
6596 cp_parser_new_expression (cp_parser* parser)
6598 bool global_scope_p;
6599 VEC(tree,gc) *placement;
6601 VEC(tree,gc) *initializer;
6605 /* Look for the optional `::' operator. */
6607 = (cp_parser_global_scope_opt (parser,
6608 /*current_scope_valid_p=*/false)
6610 /* Look for the `new' operator. */
6611 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6612 /* There's no easy way to tell a new-placement from the
6613 `( type-id )' construct. */
6614 cp_parser_parse_tentatively (parser);
6615 /* Look for a new-placement. */
6616 placement = cp_parser_new_placement (parser);
6617 /* If that didn't work out, there's no new-placement. */
6618 if (!cp_parser_parse_definitely (parser))
6620 if (placement != NULL)
6621 release_tree_vector (placement);
6625 /* If the next token is a `(', then we have a parenthesized
6627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6630 /* Consume the `('. */
6631 cp_lexer_consume_token (parser->lexer);
6632 /* Parse the type-id. */
6633 type = cp_parser_type_id (parser);
6634 /* Look for the closing `)'. */
6635 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6636 token = cp_lexer_peek_token (parser->lexer);
6637 /* There should not be a direct-new-declarator in this production,
6638 but GCC used to allowed this, so we check and emit a sensible error
6639 message for this case. */
6640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6642 error_at (token->location,
6643 "array bound forbidden after parenthesized type-id");
6644 inform (token->location,
6645 "try removing the parentheses around the type-id");
6646 cp_parser_direct_new_declarator (parser);
6650 /* Otherwise, there must be a new-type-id. */
6652 type = cp_parser_new_type_id (parser, &nelts);
6654 /* If the next token is a `(' or '{', then we have a new-initializer. */
6655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6656 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6657 initializer = cp_parser_new_initializer (parser);
6661 /* A new-expression may not appear in an integral constant
6663 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6664 ret = error_mark_node;
6667 /* Create a representation of the new-expression. */
6668 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6669 tf_warning_or_error);
6672 if (placement != NULL)
6673 release_tree_vector (placement);
6674 if (initializer != NULL)
6675 release_tree_vector (initializer);
6680 /* Parse a new-placement.
6685 Returns the same representation as for an expression-list. */
6687 static VEC(tree,gc) *
6688 cp_parser_new_placement (cp_parser* parser)
6690 VEC(tree,gc) *expression_list;
6692 /* Parse the expression-list. */
6693 expression_list = (cp_parser_parenthesized_expression_list
6694 (parser, non_attr, /*cast_p=*/false,
6695 /*allow_expansion_p=*/true,
6696 /*non_constant_p=*/NULL));
6698 return expression_list;
6701 /* Parse a new-type-id.
6704 type-specifier-seq new-declarator [opt]
6706 Returns the TYPE allocated. If the new-type-id indicates an array
6707 type, *NELTS is set to the number of elements in the last array
6708 bound; the TYPE will not include the last array bound. */
6711 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6713 cp_decl_specifier_seq type_specifier_seq;
6714 cp_declarator *new_declarator;
6715 cp_declarator *declarator;
6716 cp_declarator *outer_declarator;
6717 const char *saved_message;
6720 /* The type-specifier sequence must not contain type definitions.
6721 (It cannot contain declarations of new types either, but if they
6722 are not definitions we will catch that because they are not
6724 saved_message = parser->type_definition_forbidden_message;
6725 parser->type_definition_forbidden_message
6726 = G_("types may not be defined in a new-type-id");
6727 /* Parse the type-specifier-seq. */
6728 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6729 /*is_trailing_return=*/false,
6730 &type_specifier_seq);
6731 /* Restore the old message. */
6732 parser->type_definition_forbidden_message = saved_message;
6733 /* Parse the new-declarator. */
6734 new_declarator = cp_parser_new_declarator_opt (parser);
6736 /* Determine the number of elements in the last array dimension, if
6739 /* Skip down to the last array dimension. */
6740 declarator = new_declarator;
6741 outer_declarator = NULL;
6742 while (declarator && (declarator->kind == cdk_pointer
6743 || declarator->kind == cdk_ptrmem))
6745 outer_declarator = declarator;
6746 declarator = declarator->declarator;
6749 && declarator->kind == cdk_array
6750 && declarator->declarator
6751 && declarator->declarator->kind == cdk_array)
6753 outer_declarator = declarator;
6754 declarator = declarator->declarator;
6757 if (declarator && declarator->kind == cdk_array)
6759 *nelts = declarator->u.array.bounds;
6760 if (*nelts == error_mark_node)
6761 *nelts = integer_one_node;
6763 if (outer_declarator)
6764 outer_declarator->declarator = declarator->declarator;
6766 new_declarator = NULL;
6769 type = groktypename (&type_specifier_seq, new_declarator, false);
6773 /* Parse an (optional) new-declarator.
6776 ptr-operator new-declarator [opt]
6777 direct-new-declarator
6779 Returns the declarator. */
6781 static cp_declarator *
6782 cp_parser_new_declarator_opt (cp_parser* parser)
6784 enum tree_code code;
6786 cp_cv_quals cv_quals;
6788 /* We don't know if there's a ptr-operator next, or not. */
6789 cp_parser_parse_tentatively (parser);
6790 /* Look for a ptr-operator. */
6791 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6792 /* If that worked, look for more new-declarators. */
6793 if (cp_parser_parse_definitely (parser))
6795 cp_declarator *declarator;
6797 /* Parse another optional declarator. */
6798 declarator = cp_parser_new_declarator_opt (parser);
6800 return cp_parser_make_indirect_declarator
6801 (code, type, cv_quals, declarator);
6804 /* If the next token is a `[', there is a direct-new-declarator. */
6805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6806 return cp_parser_direct_new_declarator (parser);
6811 /* Parse a direct-new-declarator.
6813 direct-new-declarator:
6815 direct-new-declarator [constant-expression]
6819 static cp_declarator *
6820 cp_parser_direct_new_declarator (cp_parser* parser)
6822 cp_declarator *declarator = NULL;
6828 /* Look for the opening `['. */
6829 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6830 /* The first expression is not required to be constant. */
6833 cp_token *token = cp_lexer_peek_token (parser->lexer);
6834 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6835 /* The standard requires that the expression have integral
6836 type. DR 74 adds enumeration types. We believe that the
6837 real intent is that these expressions be handled like the
6838 expression in a `switch' condition, which also allows
6839 classes with a single conversion to integral or
6840 enumeration type. */
6841 if (!processing_template_decl)
6844 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6849 error_at (token->location,
6850 "expression in new-declarator must have integral "
6851 "or enumeration type");
6852 expression = error_mark_node;
6856 /* But all the other expressions must be. */
6859 = cp_parser_constant_expression (parser,
6860 /*allow_non_constant=*/false,
6862 /* Look for the closing `]'. */
6863 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6865 /* Add this bound to the declarator. */
6866 declarator = make_array_declarator (declarator, expression);
6868 /* If the next token is not a `[', then there are no more
6870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6877 /* Parse a new-initializer.
6880 ( expression-list [opt] )
6883 Returns a representation of the expression-list. */
6885 static VEC(tree,gc) *
6886 cp_parser_new_initializer (cp_parser* parser)
6888 VEC(tree,gc) *expression_list;
6890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6893 bool expr_non_constant_p;
6894 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6895 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6896 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6897 expression_list = make_tree_vector_single (t);
6900 expression_list = (cp_parser_parenthesized_expression_list
6901 (parser, non_attr, /*cast_p=*/false,
6902 /*allow_expansion_p=*/true,
6903 /*non_constant_p=*/NULL));
6905 return expression_list;
6908 /* Parse a delete-expression.
6911 :: [opt] delete cast-expression
6912 :: [opt] delete [ ] cast-expression
6914 Returns a representation of the expression. */
6917 cp_parser_delete_expression (cp_parser* parser)
6919 bool global_scope_p;
6923 /* Look for the optional `::' operator. */
6925 = (cp_parser_global_scope_opt (parser,
6926 /*current_scope_valid_p=*/false)
6928 /* Look for the `delete' keyword. */
6929 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6930 /* See if the array syntax is in use. */
6931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6933 /* Consume the `[' token. */
6934 cp_lexer_consume_token (parser->lexer);
6935 /* Look for the `]' token. */
6936 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6937 /* Remember that this is the `[]' construct. */
6943 /* Parse the cast-expression. */
6944 expression = cp_parser_simple_cast_expression (parser);
6946 /* A delete-expression may not appear in an integral constant
6948 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6949 return error_mark_node;
6951 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6952 tf_warning_or_error);
6955 /* Returns true if TOKEN may start a cast-expression and false
6959 cp_parser_token_starts_cast_expression (cp_token *token)
6961 switch (token->type)
6967 case CPP_CLOSE_SQUARE:
6968 case CPP_CLOSE_PAREN:
6969 case CPP_CLOSE_BRACE:
6973 case CPP_DEREF_STAR:
6981 case CPP_GREATER_EQ:
7001 /* '[' may start a primary-expression in obj-c++. */
7002 case CPP_OPEN_SQUARE:
7003 return c_dialect_objc ();
7010 /* Parse a cast-expression.
7014 ( type-id ) cast-expression
7016 ADDRESS_P is true iff the unary-expression is appearing as the
7017 operand of the `&' operator. CAST_P is true if this expression is
7018 the target of a cast.
7020 Returns a representation of the expression. */
7023 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7026 /* If it's a `(', then we might be looking at a cast. */
7027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7029 tree type = NULL_TREE;
7030 tree expr = NULL_TREE;
7031 bool compound_literal_p;
7032 const char *saved_message;
7034 /* There's no way to know yet whether or not this is a cast.
7035 For example, `(int (3))' is a unary-expression, while `(int)
7036 3' is a cast. So, we resort to parsing tentatively. */
7037 cp_parser_parse_tentatively (parser);
7038 /* Types may not be defined in a cast. */
7039 saved_message = parser->type_definition_forbidden_message;
7040 parser->type_definition_forbidden_message
7041 = G_("types may not be defined in casts");
7042 /* Consume the `('. */
7043 cp_lexer_consume_token (parser->lexer);
7044 /* A very tricky bit is that `(struct S) { 3 }' is a
7045 compound-literal (which we permit in C++ as an extension).
7046 But, that construct is not a cast-expression -- it is a
7047 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7048 is legal; if the compound-literal were a cast-expression,
7049 you'd need an extra set of parentheses.) But, if we parse
7050 the type-id, and it happens to be a class-specifier, then we
7051 will commit to the parse at that point, because we cannot
7052 undo the action that is done when creating a new class. So,
7053 then we cannot back up and do a postfix-expression.
7055 Therefore, we scan ahead to the closing `)', and check to see
7056 if the token after the `)' is a `{'. If so, we are not
7057 looking at a cast-expression.
7059 Save tokens so that we can put them back. */
7060 cp_lexer_save_tokens (parser->lexer);
7061 /* Skip tokens until the next token is a closing parenthesis.
7062 If we find the closing `)', and the next token is a `{', then
7063 we are looking at a compound-literal. */
7065 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7066 /*consume_paren=*/true)
7067 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7068 /* Roll back the tokens we skipped. */
7069 cp_lexer_rollback_tokens (parser->lexer);
7070 /* If we were looking at a compound-literal, simulate an error
7071 so that the call to cp_parser_parse_definitely below will
7073 if (compound_literal_p)
7074 cp_parser_simulate_error (parser);
7077 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7078 parser->in_type_id_in_expr_p = true;
7079 /* Look for the type-id. */
7080 type = cp_parser_type_id (parser);
7081 /* Look for the closing `)'. */
7082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7083 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7086 /* Restore the saved message. */
7087 parser->type_definition_forbidden_message = saved_message;
7089 /* At this point this can only be either a cast or a
7090 parenthesized ctor such as `(T ())' that looks like a cast to
7091 function returning T. */
7092 if (!cp_parser_error_occurred (parser)
7093 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7096 cp_parser_parse_definitely (parser);
7097 expr = cp_parser_cast_expression (parser,
7098 /*address_p=*/false,
7099 /*cast_p=*/true, pidk);
7101 /* Warn about old-style casts, if so requested. */
7102 if (warn_old_style_cast
7103 && !in_system_header
7104 && !VOID_TYPE_P (type)
7105 && current_lang_name != lang_name_c)
7106 warning (OPT_Wold_style_cast, "use of old-style cast");
7108 /* Only type conversions to integral or enumeration types
7109 can be used in constant-expressions. */
7110 if (!cast_valid_in_integral_constant_expression_p (type)
7111 && cp_parser_non_integral_constant_expression (parser,
7113 return error_mark_node;
7115 /* Perform the cast. */
7116 expr = build_c_cast (input_location, type, expr);
7120 cp_parser_abort_tentative_parse (parser);
7123 /* If we get here, then it's not a cast, so it must be a
7124 unary-expression. */
7125 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7128 /* Parse a binary expression of the general form:
7132 pm-expression .* cast-expression
7133 pm-expression ->* cast-expression
7135 multiplicative-expression:
7137 multiplicative-expression * pm-expression
7138 multiplicative-expression / pm-expression
7139 multiplicative-expression % pm-expression
7141 additive-expression:
7142 multiplicative-expression
7143 additive-expression + multiplicative-expression
7144 additive-expression - multiplicative-expression
7148 shift-expression << additive-expression
7149 shift-expression >> additive-expression
7151 relational-expression:
7153 relational-expression < shift-expression
7154 relational-expression > shift-expression
7155 relational-expression <= shift-expression
7156 relational-expression >= shift-expression
7160 relational-expression:
7161 relational-expression <? shift-expression
7162 relational-expression >? shift-expression
7164 equality-expression:
7165 relational-expression
7166 equality-expression == relational-expression
7167 equality-expression != relational-expression
7171 and-expression & equality-expression
7173 exclusive-or-expression:
7175 exclusive-or-expression ^ and-expression
7177 inclusive-or-expression:
7178 exclusive-or-expression
7179 inclusive-or-expression | exclusive-or-expression
7181 logical-and-expression:
7182 inclusive-or-expression
7183 logical-and-expression && inclusive-or-expression
7185 logical-or-expression:
7186 logical-and-expression
7187 logical-or-expression || logical-and-expression
7189 All these are implemented with a single function like:
7192 simple-cast-expression
7193 binary-expression <token> binary-expression
7195 CAST_P is true if this expression is the target of a cast.
7197 The binops_by_token map is used to get the tree codes for each <token> type.
7198 binary-expressions are associated according to a precedence table. */
7200 #define TOKEN_PRECEDENCE(token) \
7201 (((token->type == CPP_GREATER \
7202 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7203 && !parser->greater_than_is_operator_p) \
7204 ? PREC_NOT_OPERATOR \
7205 : binops_by_token[token->type].prec)
7208 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7209 bool no_toplevel_fold_p,
7210 enum cp_parser_prec prec,
7213 cp_parser_expression_stack stack;
7214 cp_parser_expression_stack_entry *sp = &stack[0];
7217 enum tree_code tree_type, lhs_type, rhs_type;
7218 enum cp_parser_prec new_prec, lookahead_prec;
7221 /* Parse the first expression. */
7222 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7223 lhs_type = ERROR_MARK;
7227 /* Get an operator token. */
7228 token = cp_lexer_peek_token (parser->lexer);
7230 if (warn_cxx0x_compat
7231 && token->type == CPP_RSHIFT
7232 && !parser->greater_than_is_operator_p)
7234 if (warning_at (token->location, OPT_Wc__0x_compat,
7235 "%<>>%> operator is treated as"
7236 " two right angle brackets in C++11"))
7237 inform (token->location,
7238 "suggest parentheses around %<>>%> expression");
7241 new_prec = TOKEN_PRECEDENCE (token);
7243 /* Popping an entry off the stack means we completed a subexpression:
7244 - either we found a token which is not an operator (`>' where it is not
7245 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7246 will happen repeatedly;
7247 - or, we found an operator which has lower priority. This is the case
7248 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7250 if (new_prec <= prec)
7259 tree_type = binops_by_token[token->type].tree_type;
7261 /* We used the operator token. */
7262 cp_lexer_consume_token (parser->lexer);
7264 /* For "false && x" or "true || x", x will never be executed;
7265 disable warnings while evaluating it. */
7266 if (tree_type == TRUTH_ANDIF_EXPR)
7267 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7268 else if (tree_type == TRUTH_ORIF_EXPR)
7269 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7271 /* Extract another operand. It may be the RHS of this expression
7272 or the LHS of a new, higher priority expression. */
7273 rhs = cp_parser_simple_cast_expression (parser);
7274 rhs_type = ERROR_MARK;
7276 /* Get another operator token. Look up its precedence to avoid
7277 building a useless (immediately popped) stack entry for common
7278 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7279 token = cp_lexer_peek_token (parser->lexer);
7280 lookahead_prec = TOKEN_PRECEDENCE (token);
7281 if (lookahead_prec > new_prec)
7283 /* ... and prepare to parse the RHS of the new, higher priority
7284 expression. Since precedence levels on the stack are
7285 monotonically increasing, we do not have to care about
7288 sp->tree_type = tree_type;
7290 sp->lhs_type = lhs_type;
7293 lhs_type = rhs_type;
7295 new_prec = lookahead_prec;
7299 lookahead_prec = new_prec;
7300 /* If the stack is not empty, we have parsed into LHS the right side
7301 (`4' in the example above) of an expression we had suspended.
7302 We can use the information on the stack to recover the LHS (`3')
7303 from the stack together with the tree code (`MULT_EXPR'), and
7304 the precedence of the higher level subexpression
7305 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7306 which will be used to actually build the additive expression. */
7309 tree_type = sp->tree_type;
7311 rhs_type = lhs_type;
7313 lhs_type = sp->lhs_type;
7316 /* Undo the disabling of warnings done above. */
7317 if (tree_type == TRUTH_ANDIF_EXPR)
7318 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7319 else if (tree_type == TRUTH_ORIF_EXPR)
7320 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7323 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7324 ERROR_MARK for everything that is not a binary expression.
7325 This makes warn_about_parentheses miss some warnings that
7326 involve unary operators. For unary expressions we should
7327 pass the correct tree_code unless the unary expression was
7328 surrounded by parentheses.
7330 if (no_toplevel_fold_p
7331 && lookahead_prec <= prec
7333 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7334 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7336 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7337 &overload, tf_warning_or_error);
7338 lhs_type = tree_type;
7340 /* If the binary operator required the use of an overloaded operator,
7341 then this expression cannot be an integral constant-expression.
7342 An overloaded operator can be used even if both operands are
7343 otherwise permissible in an integral constant-expression if at
7344 least one of the operands is of enumeration type. */
7347 && cp_parser_non_integral_constant_expression (parser,
7349 return error_mark_node;
7356 /* Parse the `? expression : assignment-expression' part of a
7357 conditional-expression. The LOGICAL_OR_EXPR is the
7358 logical-or-expression that started the conditional-expression.
7359 Returns a representation of the entire conditional-expression.
7361 This routine is used by cp_parser_assignment_expression.
7363 ? expression : assignment-expression
7367 ? : assignment-expression */
7370 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7373 tree assignment_expr;
7374 struct cp_token *token;
7376 /* Consume the `?' token. */
7377 cp_lexer_consume_token (parser->lexer);
7378 token = cp_lexer_peek_token (parser->lexer);
7379 if (cp_parser_allow_gnu_extensions_p (parser)
7380 && token->type == CPP_COLON)
7382 pedwarn (token->location, OPT_pedantic,
7383 "ISO C++ does not allow ?: with omitted middle operand");
7384 /* Implicit true clause. */
7386 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7387 warn_for_omitted_condop (token->location, logical_or_expr);
7391 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7392 parser->colon_corrects_to_scope_p = false;
7393 /* Parse the expression. */
7394 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7395 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7396 c_inhibit_evaluation_warnings +=
7397 ((logical_or_expr == truthvalue_true_node)
7398 - (logical_or_expr == truthvalue_false_node));
7399 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7402 /* The next token should be a `:'. */
7403 cp_parser_require (parser, CPP_COLON, RT_COLON);
7404 /* Parse the assignment-expression. */
7405 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7406 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7408 /* Build the conditional-expression. */
7409 return build_x_conditional_expr (logical_or_expr,
7412 tf_warning_or_error);
7415 /* Parse an assignment-expression.
7417 assignment-expression:
7418 conditional-expression
7419 logical-or-expression assignment-operator assignment_expression
7422 CAST_P is true if this expression is the target of a cast.
7424 Returns a representation for the expression. */
7427 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7432 /* If the next token is the `throw' keyword, then we're looking at
7433 a throw-expression. */
7434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7435 expr = cp_parser_throw_expression (parser);
7436 /* Otherwise, it must be that we are looking at a
7437 logical-or-expression. */
7440 /* Parse the binary expressions (logical-or-expression). */
7441 expr = cp_parser_binary_expression (parser, cast_p, false,
7442 PREC_NOT_OPERATOR, pidk);
7443 /* If the next token is a `?' then we're actually looking at a
7444 conditional-expression. */
7445 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7446 return cp_parser_question_colon_clause (parser, expr);
7449 enum tree_code assignment_operator;
7451 /* If it's an assignment-operator, we're using the second
7454 = cp_parser_assignment_operator_opt (parser);
7455 if (assignment_operator != ERROR_MARK)
7457 bool non_constant_p;
7459 /* Parse the right-hand side of the assignment. */
7460 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7462 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7463 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7465 /* An assignment may not appear in a
7466 constant-expression. */
7467 if (cp_parser_non_integral_constant_expression (parser,
7469 return error_mark_node;
7470 /* Build the assignment expression. */
7471 expr = build_x_modify_expr (expr,
7472 assignment_operator,
7474 tf_warning_or_error);
7482 /* Parse an (optional) assignment-operator.
7484 assignment-operator: one of
7485 = *= /= %= += -= >>= <<= &= ^= |=
7489 assignment-operator: one of
7492 If the next token is an assignment operator, the corresponding tree
7493 code is returned, and the token is consumed. For example, for
7494 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7495 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7496 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7497 operator, ERROR_MARK is returned. */
7499 static enum tree_code
7500 cp_parser_assignment_operator_opt (cp_parser* parser)
7505 /* Peek at the next token. */
7506 token = cp_lexer_peek_token (parser->lexer);
7508 switch (token->type)
7519 op = TRUNC_DIV_EXPR;
7523 op = TRUNC_MOD_EXPR;
7555 /* Nothing else is an assignment operator. */
7559 /* If it was an assignment operator, consume it. */
7560 if (op != ERROR_MARK)
7561 cp_lexer_consume_token (parser->lexer);
7566 /* Parse an expression.
7569 assignment-expression
7570 expression , assignment-expression
7572 CAST_P is true if this expression is the target of a cast.
7574 Returns a representation of the expression. */
7577 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7579 tree expression = NULL_TREE;
7583 tree assignment_expression;
7585 /* Parse the next assignment-expression. */
7586 assignment_expression
7587 = cp_parser_assignment_expression (parser, cast_p, pidk);
7588 /* If this is the first assignment-expression, we can just
7591 expression = assignment_expression;
7593 expression = build_x_compound_expr (expression,
7594 assignment_expression,
7595 tf_warning_or_error);
7596 /* If the next token is not a comma, then we are done with the
7598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7600 /* Consume the `,'. */
7601 cp_lexer_consume_token (parser->lexer);
7602 /* A comma operator cannot appear in a constant-expression. */
7603 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7604 expression = error_mark_node;
7610 /* Parse a constant-expression.
7612 constant-expression:
7613 conditional-expression
7615 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7616 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7617 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7618 is false, NON_CONSTANT_P should be NULL. */
7621 cp_parser_constant_expression (cp_parser* parser,
7622 bool allow_non_constant_p,
7623 bool *non_constant_p)
7625 bool saved_integral_constant_expression_p;
7626 bool saved_allow_non_integral_constant_expression_p;
7627 bool saved_non_integral_constant_expression_p;
7630 /* It might seem that we could simply parse the
7631 conditional-expression, and then check to see if it were
7632 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7633 one that the compiler can figure out is constant, possibly after
7634 doing some simplifications or optimizations. The standard has a
7635 precise definition of constant-expression, and we must honor
7636 that, even though it is somewhat more restrictive.
7642 is not a legal declaration, because `(2, 3)' is not a
7643 constant-expression. The `,' operator is forbidden in a
7644 constant-expression. However, GCC's constant-folding machinery
7645 will fold this operation to an INTEGER_CST for `3'. */
7647 /* Save the old settings. */
7648 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7649 saved_allow_non_integral_constant_expression_p
7650 = parser->allow_non_integral_constant_expression_p;
7651 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7652 /* We are now parsing a constant-expression. */
7653 parser->integral_constant_expression_p = true;
7654 parser->allow_non_integral_constant_expression_p
7655 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7656 parser->non_integral_constant_expression_p = false;
7657 /* Although the grammar says "conditional-expression", we parse an
7658 "assignment-expression", which also permits "throw-expression"
7659 and the use of assignment operators. In the case that
7660 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7661 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7662 actually essential that we look for an assignment-expression.
7663 For example, cp_parser_initializer_clauses uses this function to
7664 determine whether a particular assignment-expression is in fact
7666 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7667 /* Restore the old settings. */
7668 parser->integral_constant_expression_p
7669 = saved_integral_constant_expression_p;
7670 parser->allow_non_integral_constant_expression_p
7671 = saved_allow_non_integral_constant_expression_p;
7672 if (cxx_dialect >= cxx0x)
7674 /* Require an rvalue constant expression here; that's what our
7675 callers expect. Reference constant expressions are handled
7676 separately in e.g. cp_parser_template_argument. */
7677 bool is_const = potential_rvalue_constant_expression (expression);
7678 parser->non_integral_constant_expression_p = !is_const;
7679 if (!is_const && !allow_non_constant_p)
7680 require_potential_rvalue_constant_expression (expression);
7682 if (allow_non_constant_p)
7683 *non_constant_p = parser->non_integral_constant_expression_p;
7684 parser->non_integral_constant_expression_p
7685 = saved_non_integral_constant_expression_p;
7690 /* Parse __builtin_offsetof.
7692 offsetof-expression:
7693 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7695 offsetof-member-designator:
7697 | offsetof-member-designator "." id-expression
7698 | offsetof-member-designator "[" expression "]"
7699 | offsetof-member-designator "->" id-expression */
7702 cp_parser_builtin_offsetof (cp_parser *parser)
7704 int save_ice_p, save_non_ice_p;
7709 /* We're about to accept non-integral-constant things, but will
7710 definitely yield an integral constant expression. Save and
7711 restore these values around our local parsing. */
7712 save_ice_p = parser->integral_constant_expression_p;
7713 save_non_ice_p = parser->non_integral_constant_expression_p;
7715 /* Consume the "__builtin_offsetof" token. */
7716 cp_lexer_consume_token (parser->lexer);
7717 /* Consume the opening `('. */
7718 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7719 /* Parse the type-id. */
7720 type = cp_parser_type_id (parser);
7721 /* Look for the `,'. */
7722 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7723 token = cp_lexer_peek_token (parser->lexer);
7725 /* Build the (type *)null that begins the traditional offsetof macro. */
7726 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7727 tf_warning_or_error);
7729 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7730 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7731 true, &dummy, token->location);
7734 token = cp_lexer_peek_token (parser->lexer);
7735 switch (token->type)
7737 case CPP_OPEN_SQUARE:
7738 /* offsetof-member-designator "[" expression "]" */
7739 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7743 /* offsetof-member-designator "->" identifier */
7744 expr = grok_array_decl (expr, integer_zero_node);
7748 /* offsetof-member-designator "." identifier */
7749 cp_lexer_consume_token (parser->lexer);
7750 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7755 case CPP_CLOSE_PAREN:
7756 /* Consume the ")" token. */
7757 cp_lexer_consume_token (parser->lexer);
7761 /* Error. We know the following require will fail, but
7762 that gives the proper error message. */
7763 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7764 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7765 expr = error_mark_node;
7771 /* If we're processing a template, we can't finish the semantics yet.
7772 Otherwise we can fold the entire expression now. */
7773 if (processing_template_decl)
7774 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7776 expr = finish_offsetof (expr);
7779 parser->integral_constant_expression_p = save_ice_p;
7780 parser->non_integral_constant_expression_p = save_non_ice_p;
7785 /* Parse a trait expression.
7787 Returns a representation of the expression, the underlying type
7788 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7791 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7794 tree type1, type2 = NULL_TREE;
7795 bool binary = false;
7796 cp_decl_specifier_seq decl_specs;
7800 case RID_HAS_NOTHROW_ASSIGN:
7801 kind = CPTK_HAS_NOTHROW_ASSIGN;
7803 case RID_HAS_NOTHROW_CONSTRUCTOR:
7804 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7806 case RID_HAS_NOTHROW_COPY:
7807 kind = CPTK_HAS_NOTHROW_COPY;
7809 case RID_HAS_TRIVIAL_ASSIGN:
7810 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7812 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7813 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7815 case RID_HAS_TRIVIAL_COPY:
7816 kind = CPTK_HAS_TRIVIAL_COPY;
7818 case RID_HAS_TRIVIAL_DESTRUCTOR:
7819 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7821 case RID_HAS_VIRTUAL_DESTRUCTOR:
7822 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7824 case RID_IS_ABSTRACT:
7825 kind = CPTK_IS_ABSTRACT;
7827 case RID_IS_BASE_OF:
7828 kind = CPTK_IS_BASE_OF;
7832 kind = CPTK_IS_CLASS;
7834 case RID_IS_CONVERTIBLE_TO:
7835 kind = CPTK_IS_CONVERTIBLE_TO;
7839 kind = CPTK_IS_EMPTY;
7842 kind = CPTK_IS_ENUM;
7844 case RID_IS_LITERAL_TYPE:
7845 kind = CPTK_IS_LITERAL_TYPE;
7850 case RID_IS_POLYMORPHIC:
7851 kind = CPTK_IS_POLYMORPHIC;
7853 case RID_IS_STD_LAYOUT:
7854 kind = CPTK_IS_STD_LAYOUT;
7856 case RID_IS_TRIVIAL:
7857 kind = CPTK_IS_TRIVIAL;
7860 kind = CPTK_IS_UNION;
7862 case RID_UNDERLYING_TYPE:
7863 kind = CPTK_UNDERLYING_TYPE;
7868 case RID_DIRECT_BASES:
7869 kind = CPTK_DIRECT_BASES;
7875 /* Consume the token. */
7876 cp_lexer_consume_token (parser->lexer);
7878 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7880 type1 = cp_parser_type_id (parser);
7882 if (type1 == error_mark_node)
7883 return error_mark_node;
7885 /* Build a trivial decl-specifier-seq. */
7886 clear_decl_specs (&decl_specs);
7887 decl_specs.type = type1;
7889 /* Call grokdeclarator to figure out what type this is. */
7890 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7891 /*initialized=*/0, /*attrlist=*/NULL);
7895 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7897 type2 = cp_parser_type_id (parser);
7899 if (type2 == error_mark_node)
7900 return error_mark_node;
7902 /* Build a trivial decl-specifier-seq. */
7903 clear_decl_specs (&decl_specs);
7904 decl_specs.type = type2;
7906 /* Call grokdeclarator to figure out what type this is. */
7907 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7908 /*initialized=*/0, /*attrlist=*/NULL);
7911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7913 /* Complete the trait expression, which may mean either processing
7914 the trait expr now or saving it for template instantiation. */
7917 case CPTK_UNDERLYING_TYPE:
7918 return finish_underlying_type (type1);
7920 return finish_bases (type1, false);
7921 case CPTK_DIRECT_BASES:
7922 return finish_bases (type1, true);
7924 return finish_trait_expr (kind, type1, type2);
7928 /* Lambdas that appear in variable initializer or default argument scope
7929 get that in their mangling, so we need to record it. We might as well
7930 use the count for function and namespace scopes as well. */
7931 static GTY(()) tree lambda_scope;
7932 static GTY(()) int lambda_count;
7933 typedef struct GTY(()) tree_int
7938 DEF_VEC_O(tree_int);
7939 DEF_VEC_ALLOC_O(tree_int,gc);
7940 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7943 start_lambda_scope (tree decl)
7947 /* Once we're inside a function, we ignore other scopes and just push
7948 the function again so that popping works properly. */
7949 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7950 decl = current_function_decl;
7951 ti.t = lambda_scope;
7952 ti.i = lambda_count;
7953 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7954 if (lambda_scope != decl)
7956 /* Don't reset the count if we're still in the same function. */
7957 lambda_scope = decl;
7963 record_lambda_scope (tree lambda)
7965 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7966 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7970 finish_lambda_scope (void)
7972 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7973 if (lambda_scope != p->t)
7975 lambda_scope = p->t;
7976 lambda_count = p->i;
7978 VEC_pop (tree_int, lambda_scope_stack);
7981 /* Parse a lambda expression.
7984 lambda-introducer lambda-declarator [opt] compound-statement
7986 Returns a representation of the expression. */
7989 cp_parser_lambda_expression (cp_parser* parser)
7991 tree lambda_expr = build_lambda_expr ();
7995 LAMBDA_EXPR_LOCATION (lambda_expr)
7996 = cp_lexer_peek_token (parser->lexer)->location;
7998 if (cp_unevaluated_operand)
7999 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8000 "lambda-expression in unevaluated context");
8002 /* We may be in the middle of deferred access check. Disable
8004 push_deferring_access_checks (dk_no_deferred);
8006 cp_parser_lambda_introducer (parser, lambda_expr);
8008 type = begin_lambda_type (lambda_expr);
8010 record_lambda_scope (lambda_expr);
8012 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8013 determine_visibility (TYPE_NAME (type));
8015 /* Now that we've started the type, add the capture fields for any
8016 explicit captures. */
8017 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8020 /* Inside the class, surrounding template-parameter-lists do not apply. */
8021 unsigned int saved_num_template_parameter_lists
8022 = parser->num_template_parameter_lists;
8023 unsigned char in_statement = parser->in_statement;
8024 bool in_switch_statement_p = parser->in_switch_statement_p;
8026 parser->num_template_parameter_lists = 0;
8027 parser->in_statement = 0;
8028 parser->in_switch_statement_p = false;
8030 /* By virtue of defining a local class, a lambda expression has access to
8031 the private variables of enclosing classes. */
8033 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8036 cp_parser_lambda_body (parser, lambda_expr);
8037 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8038 cp_parser_skip_to_end_of_block_or_statement (parser);
8040 /* The capture list was built up in reverse order; fix that now. */
8042 tree newlist = NULL_TREE;
8045 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8048 next = TREE_CHAIN (elt);
8049 TREE_CHAIN (elt) = newlist;
8052 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8056 maybe_add_lambda_conv_op (type);
8058 type = finish_struct (type, /*attributes=*/NULL_TREE);
8060 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8061 parser->in_statement = in_statement;
8062 parser->in_switch_statement_p = in_switch_statement_p;
8065 pop_deferring_access_checks ();
8067 /* This field is only used during parsing of the lambda. */
8068 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8070 /* This lambda shouldn't have any proxies left at this point. */
8071 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8072 /* And now that we're done, push proxies for an enclosing lambda. */
8073 insert_pending_capture_proxies ();
8076 return build_lambda_object (lambda_expr);
8078 return error_mark_node;
8081 /* Parse the beginning of a lambda expression.
8084 [ lambda-capture [opt] ]
8086 LAMBDA_EXPR is the current representation of the lambda expression. */
8089 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8091 /* Need commas after the first capture. */
8094 /* Eat the leading `['. */
8095 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8097 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8098 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8099 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8100 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8101 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8104 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8106 cp_lexer_consume_token (parser->lexer);
8110 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8112 cp_token* capture_token;
8114 tree capture_init_expr;
8115 cp_id_kind idk = CP_ID_KIND_NONE;
8116 bool explicit_init_p = false;
8118 enum capture_kind_type
8123 enum capture_kind_type capture_kind = BY_COPY;
8125 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8127 error ("expected end of capture-list");
8134 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8136 /* Possibly capture `this'. */
8137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8139 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8140 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8141 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8142 "with by-copy capture default");
8143 cp_lexer_consume_token (parser->lexer);
8144 add_capture (lambda_expr,
8145 /*id=*/this_identifier,
8146 /*initializer=*/finish_this_expr(),
8147 /*by_reference_p=*/false,
8152 /* Remember whether we want to capture as a reference or not. */
8153 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8155 capture_kind = BY_REFERENCE;
8156 cp_lexer_consume_token (parser->lexer);
8159 /* Get the identifier. */
8160 capture_token = cp_lexer_peek_token (parser->lexer);
8161 capture_id = cp_parser_identifier (parser);
8163 if (capture_id == error_mark_node)
8164 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8165 delimiters, but I modified this to stop on unnested ']' as well. It
8166 was already changed to stop on unnested '}', so the
8167 "closing_parenthesis" name is no more misleading with my change. */
8169 cp_parser_skip_to_closing_parenthesis (parser,
8170 /*recovering=*/true,
8172 /*consume_paren=*/true);
8176 /* Find the initializer for this capture. */
8177 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8179 /* An explicit expression exists. */
8180 cp_lexer_consume_token (parser->lexer);
8181 pedwarn (input_location, OPT_pedantic,
8182 "ISO C++ does not allow initializers "
8183 "in lambda expression capture lists");
8184 capture_init_expr = cp_parser_assignment_expression (parser,
8187 explicit_init_p = true;
8191 const char* error_msg;
8193 /* Turn the identifier into an id-expression. */
8195 = cp_parser_lookup_name
8199 /*is_template=*/false,
8200 /*is_namespace=*/false,
8201 /*check_dependency=*/true,
8202 /*ambiguous_decls=*/NULL,
8203 capture_token->location);
8205 if (capture_init_expr == error_mark_node)
8207 unqualified_name_lookup_error (capture_id);
8210 else if (DECL_P (capture_init_expr)
8211 && (TREE_CODE (capture_init_expr) != VAR_DECL
8212 && TREE_CODE (capture_init_expr) != PARM_DECL))
8214 error_at (capture_token->location,
8215 "capture of non-variable %qD ",
8217 inform (0, "%q+#D declared here", capture_init_expr);
8220 if (TREE_CODE (capture_init_expr) == VAR_DECL
8221 && decl_storage_duration (capture_init_expr) != dk_auto)
8223 pedwarn (capture_token->location, 0, "capture of variable "
8224 "%qD with non-automatic storage duration",
8226 inform (0, "%q+#D declared here", capture_init_expr);
8231 = finish_id_expression
8236 /*integral_constant_expression_p=*/false,
8237 /*allow_non_integral_constant_expression_p=*/false,
8238 /*non_integral_constant_expression_p=*/NULL,
8239 /*template_p=*/false,
8241 /*address_p=*/false,
8242 /*template_arg_p=*/false,
8244 capture_token->location);
8247 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8248 && !explicit_init_p)
8250 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8251 && capture_kind == BY_COPY)
8252 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8253 "of %qD redundant with by-copy capture default",
8255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8256 && capture_kind == BY_REFERENCE)
8257 pedwarn (capture_token->location, 0, "explicit by-reference "
8258 "capture of %qD redundant with by-reference capture "
8259 "default", capture_id);
8262 add_capture (lambda_expr,
8265 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8269 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8272 /* Parse the (optional) middle of a lambda expression.
8275 ( parameter-declaration-clause [opt] )
8276 attribute-specifier [opt]
8278 exception-specification [opt]
8279 lambda-return-type-clause [opt]
8281 LAMBDA_EXPR is the current representation of the lambda expression. */
8284 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8286 /* 5.1.1.4 of the standard says:
8287 If a lambda-expression does not include a lambda-declarator, it is as if
8288 the lambda-declarator were ().
8289 This means an empty parameter list, no attributes, and no exception
8291 tree param_list = void_list_node;
8292 tree attributes = NULL_TREE;
8293 tree exception_spec = NULL_TREE;
8296 /* The lambda-declarator is optional, but must begin with an opening
8297 parenthesis if present. */
8298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8300 cp_lexer_consume_token (parser->lexer);
8302 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8304 /* Parse parameters. */
8305 param_list = cp_parser_parameter_declaration_clause (parser);
8307 /* Default arguments shall not be specified in the
8308 parameter-declaration-clause of a lambda-declarator. */
8309 for (t = param_list; t; t = TREE_CHAIN (t))
8310 if (TREE_PURPOSE (t))
8311 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8312 "default argument specified for lambda parameter");
8314 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8316 attributes = cp_parser_attributes_opt (parser);
8318 /* Parse optional `mutable' keyword. */
8319 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8321 cp_lexer_consume_token (parser->lexer);
8322 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8325 /* Parse optional exception specification. */
8326 exception_spec = cp_parser_exception_specification_opt (parser);
8328 /* Parse optional trailing return type. */
8329 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8331 cp_lexer_consume_token (parser->lexer);
8332 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8335 /* The function parameters must be in scope all the way until after the
8336 trailing-return-type in case of decltype. */
8337 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8338 pop_binding (DECL_NAME (t), t);
8343 /* Create the function call operator.
8345 Messing with declarators like this is no uglier than building up the
8346 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8349 cp_decl_specifier_seq return_type_specs;
8350 cp_declarator* declarator;
8355 clear_decl_specs (&return_type_specs);
8356 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8357 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8359 /* Maybe we will deduce the return type later, but we can use void
8360 as a placeholder return type anyways. */
8361 return_type_specs.type = void_type_node;
8363 p = obstack_alloc (&declarator_obstack, 0);
8365 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8368 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8369 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8370 declarator = make_call_declarator (declarator, param_list, quals,
8371 VIRT_SPEC_UNSPECIFIED,
8373 /*late_return_type=*/NULL_TREE);
8374 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8376 fco = grokmethod (&return_type_specs,
8379 if (fco != error_mark_node)
8381 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8382 DECL_ARTIFICIAL (fco) = 1;
8383 /* Give the object parameter a different name. */
8384 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8387 finish_member_declaration (fco);
8389 obstack_free (&declarator_obstack, p);
8391 return (fco != error_mark_node);
8395 /* Parse the body of a lambda expression, which is simply
8399 but which requires special handling.
8400 LAMBDA_EXPR is the current representation of the lambda expression. */
8403 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8405 bool nested = (current_function_decl != NULL_TREE);
8406 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8408 push_function_context ();
8410 /* Still increment function_depth so that we don't GC in the
8411 middle of an expression. */
8413 /* Clear this in case we're in the middle of a default argument. */
8414 parser->local_variables_forbidden_p = false;
8416 /* Finish the function call operator
8418 + late_parsing_for_member
8419 + function_definition_after_declarator
8420 + ctor_initializer_opt_and_function_body */
8422 tree fco = lambda_function (lambda_expr);
8428 /* Let the front end know that we are going to be defining this
8430 start_preparsed_function (fco,
8432 SF_PRE_PARSED | SF_INCLASS_INLINE);
8434 start_lambda_scope (fco);
8435 body = begin_function_body ();
8437 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8440 /* Push the proxies for any explicit captures. */
8441 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8442 cap = TREE_CHAIN (cap))
8443 build_capture_proxy (TREE_PURPOSE (cap));
8445 compound_stmt = begin_compound_stmt (0);
8447 /* 5.1.1.4 of the standard says:
8448 If a lambda-expression does not include a trailing-return-type, it
8449 is as if the trailing-return-type denotes the following type:
8450 * if the compound-statement is of the form
8451 { return attribute-specifier [opt] expression ; }
8452 the type of the returned expression after lvalue-to-rvalue
8453 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8454 (_conv.array_ 4.2), and function-to-pointer conversion
8456 * otherwise, void. */
8458 /* In a lambda that has neither a lambda-return-type-clause
8459 nor a deducible form, errors should be reported for return statements
8460 in the body. Since we used void as the placeholder return type, parsing
8461 the body as usual will give such desired behavior. */
8462 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8463 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8464 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8466 tree expr = NULL_TREE;
8467 cp_id_kind idk = CP_ID_KIND_NONE;
8469 /* Parse tentatively in case there's more after the initial return
8471 cp_parser_parse_tentatively (parser);
8473 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8475 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8477 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8478 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8480 if (cp_parser_parse_definitely (parser))
8482 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8484 /* Will get error here if type not deduced yet. */
8485 finish_return_stmt (expr);
8493 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8494 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8495 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8496 cp_parser_label_declaration (parser);
8497 cp_parser_statement_seq_opt (parser, NULL_TREE);
8498 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8499 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8502 finish_compound_stmt (compound_stmt);
8505 finish_function_body (body);
8506 finish_lambda_scope ();
8508 /* Finish the function and generate code for it if necessary. */
8509 expand_or_defer_fn (finish_function (/*inline*/2));
8512 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8514 pop_function_context();
8519 /* Statements [gram.stmt.stmt] */
8521 /* Parse a statement.
8525 expression-statement
8530 declaration-statement
8538 IN_COMPOUND is true when the statement is nested inside a
8539 cp_parser_compound_statement; this matters for certain pragmas.
8541 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8542 is a (possibly labeled) if statement which is not enclosed in braces
8543 and has an else clause. This is used to implement -Wparentheses. */
8546 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8547 bool in_compound, bool *if_p)
8551 location_t statement_location;
8556 /* There is no statement yet. */
8557 statement = NULL_TREE;
8558 /* Peek at the next token. */
8559 token = cp_lexer_peek_token (parser->lexer);
8560 /* Remember the location of the first token in the statement. */
8561 statement_location = token->location;
8562 /* If this is a keyword, then that will often determine what kind of
8563 statement we have. */
8564 if (token->type == CPP_KEYWORD)
8566 enum rid keyword = token->keyword;
8572 /* Looks like a labeled-statement with a case label.
8573 Parse the label, and then use tail recursion to parse
8575 cp_parser_label_for_labeled_statement (parser);
8580 statement = cp_parser_selection_statement (parser, if_p);
8586 statement = cp_parser_iteration_statement (parser);
8593 statement = cp_parser_jump_statement (parser);
8596 /* Objective-C++ exception-handling constructs. */
8599 case RID_AT_FINALLY:
8600 case RID_AT_SYNCHRONIZED:
8602 statement = cp_parser_objc_statement (parser);
8606 statement = cp_parser_try_block (parser);
8610 /* This must be a namespace alias definition. */
8611 cp_parser_declaration_statement (parser);
8614 case RID_TRANSACTION_ATOMIC:
8615 case RID_TRANSACTION_RELAXED:
8616 statement = cp_parser_transaction (parser, keyword);
8618 case RID_TRANSACTION_CANCEL:
8619 statement = cp_parser_transaction_cancel (parser);
8623 /* It might be a keyword like `int' that can start a
8624 declaration-statement. */
8628 else if (token->type == CPP_NAME)
8630 /* If the next token is a `:', then we are looking at a
8631 labeled-statement. */
8632 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8633 if (token->type == CPP_COLON)
8635 /* Looks like a labeled-statement with an ordinary label.
8636 Parse the label, and then use tail recursion to parse
8638 cp_parser_label_for_labeled_statement (parser);
8642 /* Anything that starts with a `{' must be a compound-statement. */
8643 else if (token->type == CPP_OPEN_BRACE)
8644 statement = cp_parser_compound_statement (parser, NULL, false, false);
8645 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8646 a statement all its own. */
8647 else if (token->type == CPP_PRAGMA)
8649 /* Only certain OpenMP pragmas are attached to statements, and thus
8650 are considered statements themselves. All others are not. In
8651 the context of a compound, accept the pragma as a "statement" and
8652 return so that we can check for a close brace. Otherwise we
8653 require a real statement and must go back and read one. */
8655 cp_parser_pragma (parser, pragma_compound);
8656 else if (!cp_parser_pragma (parser, pragma_stmt))
8660 else if (token->type == CPP_EOF)
8662 cp_parser_error (parser, "expected statement");
8666 /* Everything else must be a declaration-statement or an
8667 expression-statement. Try for the declaration-statement
8668 first, unless we are looking at a `;', in which case we know that
8669 we have an expression-statement. */
8672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8674 cp_parser_parse_tentatively (parser);
8675 /* Try to parse the declaration-statement. */
8676 cp_parser_declaration_statement (parser);
8677 /* If that worked, we're done. */
8678 if (cp_parser_parse_definitely (parser))
8681 /* Look for an expression-statement instead. */
8682 statement = cp_parser_expression_statement (parser, in_statement_expr);
8685 /* Set the line number for the statement. */
8686 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8687 SET_EXPR_LOCATION (statement, statement_location);
8690 /* Parse the label for a labeled-statement, i.e.
8693 case constant-expression :
8697 case constant-expression ... constant-expression : statement
8699 When a label is parsed without errors, the label is added to the
8700 parse tree by the finish_* functions, so this function doesn't
8701 have to return the label. */
8704 cp_parser_label_for_labeled_statement (cp_parser* parser)
8707 tree label = NULL_TREE;
8708 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8710 /* The next token should be an identifier. */
8711 token = cp_lexer_peek_token (parser->lexer);
8712 if (token->type != CPP_NAME
8713 && token->type != CPP_KEYWORD)
8715 cp_parser_error (parser, "expected labeled-statement");
8719 parser->colon_corrects_to_scope_p = false;
8720 switch (token->keyword)
8727 /* Consume the `case' token. */
8728 cp_lexer_consume_token (parser->lexer);
8729 /* Parse the constant-expression. */
8730 expr = cp_parser_constant_expression (parser,
8731 /*allow_non_constant_p=*/false,
8734 ellipsis = cp_lexer_peek_token (parser->lexer);
8735 if (ellipsis->type == CPP_ELLIPSIS)
8737 /* Consume the `...' token. */
8738 cp_lexer_consume_token (parser->lexer);
8740 cp_parser_constant_expression (parser,
8741 /*allow_non_constant_p=*/false,
8743 /* We don't need to emit warnings here, as the common code
8744 will do this for us. */
8747 expr_hi = NULL_TREE;
8749 if (parser->in_switch_statement_p)
8750 finish_case_label (token->location, expr, expr_hi);
8752 error_at (token->location,
8753 "case label %qE not within a switch statement",
8759 /* Consume the `default' token. */
8760 cp_lexer_consume_token (parser->lexer);
8762 if (parser->in_switch_statement_p)
8763 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8765 error_at (token->location, "case label not within a switch statement");
8769 /* Anything else must be an ordinary label. */
8770 label = finish_label_stmt (cp_parser_identifier (parser));
8774 /* Require the `:' token. */
8775 cp_parser_require (parser, CPP_COLON, RT_COLON);
8777 /* An ordinary label may optionally be followed by attributes.
8778 However, this is only permitted if the attributes are then
8779 followed by a semicolon. This is because, for backward
8780 compatibility, when parsing
8781 lab: __attribute__ ((unused)) int i;
8782 we want the attribute to attach to "i", not "lab". */
8783 if (label != NULL_TREE
8784 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8788 cp_parser_parse_tentatively (parser);
8789 attrs = cp_parser_attributes_opt (parser);
8790 if (attrs == NULL_TREE
8791 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8792 cp_parser_abort_tentative_parse (parser);
8793 else if (!cp_parser_parse_definitely (parser))
8796 cplus_decl_attributes (&label, attrs, 0);
8799 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8802 /* Parse an expression-statement.
8804 expression-statement:
8807 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8808 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8809 indicates whether this expression-statement is part of an
8810 expression statement. */
8813 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8815 tree statement = NULL_TREE;
8816 cp_token *token = cp_lexer_peek_token (parser->lexer);
8818 /* If the next token is a ';', then there is no expression
8820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8821 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8823 /* Give a helpful message for "A<T>::type t;" and the like. */
8824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8825 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8827 if (TREE_CODE (statement) == SCOPE_REF)
8828 error_at (token->location, "need %<typename%> before %qE because "
8829 "%qT is a dependent scope",
8830 statement, TREE_OPERAND (statement, 0));
8831 else if (is_overloaded_fn (statement)
8832 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8835 tree fn = get_first_fn (statement);
8836 error_at (token->location,
8837 "%<%T::%D%> names the constructor, not the type",
8838 DECL_CONTEXT (fn), DECL_NAME (fn));
8842 /* Consume the final `;'. */
8843 cp_parser_consume_semicolon_at_end_of_statement (parser);
8845 if (in_statement_expr
8846 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8847 /* This is the final expression statement of a statement
8849 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8851 statement = finish_expr_stmt (statement);
8858 /* Parse a compound-statement.
8861 { statement-seq [opt] }
8866 { label-declaration-seq [opt] statement-seq [opt] }
8868 label-declaration-seq:
8870 label-declaration-seq label-declaration
8872 Returns a tree representing the statement. */
8875 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8876 bool in_try, bool function_body)
8880 /* Consume the `{'. */
8881 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8882 return error_mark_node;
8883 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8885 pedwarn (input_location, OPT_pedantic,
8886 "compound-statement in constexpr function");
8887 /* Begin the compound-statement. */
8888 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8889 /* If the next keyword is `__label__' we have a label declaration. */
8890 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8891 cp_parser_label_declaration (parser);
8892 /* Parse an (optional) statement-seq. */
8893 cp_parser_statement_seq_opt (parser, in_statement_expr);
8894 /* Finish the compound-statement. */
8895 finish_compound_stmt (compound_stmt);
8896 /* Consume the `}'. */
8897 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8899 return compound_stmt;
8902 /* Parse an (optional) statement-seq.
8906 statement-seq [opt] statement */
8909 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8911 /* Scan statements until there aren't any more. */
8914 cp_token *token = cp_lexer_peek_token (parser->lexer);
8916 /* If we are looking at a `}', then we have run out of
8917 statements; the same is true if we have reached the end
8918 of file, or have stumbled upon a stray '@end'. */
8919 if (token->type == CPP_CLOSE_BRACE
8920 || token->type == CPP_EOF
8921 || token->type == CPP_PRAGMA_EOL
8922 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8925 /* If we are in a compound statement and find 'else' then
8926 something went wrong. */
8927 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8929 if (parser->in_statement & IN_IF_STMT)
8933 token = cp_lexer_consume_token (parser->lexer);
8934 error_at (token->location, "%<else%> without a previous %<if%>");
8938 /* Parse the statement. */
8939 cp_parser_statement (parser, in_statement_expr, true, NULL);
8943 /* Parse a selection-statement.
8945 selection-statement:
8946 if ( condition ) statement
8947 if ( condition ) statement else statement
8948 switch ( condition ) statement
8950 Returns the new IF_STMT or SWITCH_STMT.
8952 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8953 is a (possibly labeled) if statement which is not enclosed in
8954 braces and has an else clause. This is used to implement
8958 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8966 /* Peek at the next token. */
8967 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8969 /* See what kind of keyword it is. */
8970 keyword = token->keyword;
8979 /* Look for the `('. */
8980 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8982 cp_parser_skip_to_end_of_statement (parser);
8983 return error_mark_node;
8986 /* Begin the selection-statement. */
8987 if (keyword == RID_IF)
8988 statement = begin_if_stmt ();
8990 statement = begin_switch_stmt ();
8992 /* Parse the condition. */
8993 condition = cp_parser_condition (parser);
8994 /* Look for the `)'. */
8995 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8996 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8997 /*consume_paren=*/true);
8999 if (keyword == RID_IF)
9002 unsigned char in_statement;
9004 /* Add the condition. */
9005 finish_if_stmt_cond (condition, statement);
9007 /* Parse the then-clause. */
9008 in_statement = parser->in_statement;
9009 parser->in_statement |= IN_IF_STMT;
9010 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9012 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9013 add_stmt (build_empty_stmt (loc));
9014 cp_lexer_consume_token (parser->lexer);
9015 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9016 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9017 "empty body in an %<if%> statement");
9021 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9022 parser->in_statement = in_statement;
9024 finish_then_clause (statement);
9026 /* If the next token is `else', parse the else-clause. */
9027 if (cp_lexer_next_token_is_keyword (parser->lexer,
9030 /* Consume the `else' keyword. */
9031 cp_lexer_consume_token (parser->lexer);
9032 begin_else_clause (statement);
9033 /* Parse the else-clause. */
9034 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9037 loc = cp_lexer_peek_token (parser->lexer)->location;
9039 OPT_Wempty_body, "suggest braces around "
9040 "empty body in an %<else%> statement");
9041 add_stmt (build_empty_stmt (loc));
9042 cp_lexer_consume_token (parser->lexer);
9045 cp_parser_implicitly_scoped_statement (parser, NULL);
9047 finish_else_clause (statement);
9049 /* If we are currently parsing a then-clause, then
9050 IF_P will not be NULL. We set it to true to
9051 indicate that this if statement has an else clause.
9052 This may trigger the Wparentheses warning below
9053 when we get back up to the parent if statement. */
9059 /* This if statement does not have an else clause. If
9060 NESTED_IF is true, then the then-clause is an if
9061 statement which does have an else clause. We warn
9062 about the potential ambiguity. */
9064 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9065 "suggest explicit braces to avoid ambiguous"
9069 /* Now we're all done with the if-statement. */
9070 finish_if_stmt (statement);
9074 bool in_switch_statement_p;
9075 unsigned char in_statement;
9077 /* Add the condition. */
9078 finish_switch_cond (condition, statement);
9080 /* Parse the body of the switch-statement. */
9081 in_switch_statement_p = parser->in_switch_statement_p;
9082 in_statement = parser->in_statement;
9083 parser->in_switch_statement_p = true;
9084 parser->in_statement |= IN_SWITCH_STMT;
9085 cp_parser_implicitly_scoped_statement (parser, NULL);
9086 parser->in_switch_statement_p = in_switch_statement_p;
9087 parser->in_statement = in_statement;
9089 /* Now we're all done with the switch-statement. */
9090 finish_switch_stmt (statement);
9098 cp_parser_error (parser, "expected selection-statement");
9099 return error_mark_node;
9103 /* Parse a condition.
9107 type-specifier-seq declarator = initializer-clause
9108 type-specifier-seq declarator braced-init-list
9113 type-specifier-seq declarator asm-specification [opt]
9114 attributes [opt] = assignment-expression
9116 Returns the expression that should be tested. */
9119 cp_parser_condition (cp_parser* parser)
9121 cp_decl_specifier_seq type_specifiers;
9122 const char *saved_message;
9123 int declares_class_or_enum;
9125 /* Try the declaration first. */
9126 cp_parser_parse_tentatively (parser);
9127 /* New types are not allowed in the type-specifier-seq for a
9129 saved_message = parser->type_definition_forbidden_message;
9130 parser->type_definition_forbidden_message
9131 = G_("types may not be defined in conditions");
9132 /* Parse the type-specifier-seq. */
9133 cp_parser_decl_specifier_seq (parser,
9134 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9136 &declares_class_or_enum);
9137 /* Restore the saved message. */
9138 parser->type_definition_forbidden_message = saved_message;
9139 /* If all is well, we might be looking at a declaration. */
9140 if (!cp_parser_error_occurred (parser))
9143 tree asm_specification;
9145 cp_declarator *declarator;
9146 tree initializer = NULL_TREE;
9148 /* Parse the declarator. */
9149 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9150 /*ctor_dtor_or_conv_p=*/NULL,
9151 /*parenthesized_p=*/NULL,
9152 /*member_p=*/false);
9153 /* Parse the attributes. */
9154 attributes = cp_parser_attributes_opt (parser);
9155 /* Parse the asm-specification. */
9156 asm_specification = cp_parser_asm_specification_opt (parser);
9157 /* If the next token is not an `=' or '{', then we might still be
9158 looking at an expression. For example:
9162 looks like a decl-specifier-seq and a declarator -- but then
9163 there is no `=', so this is an expression. */
9164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9165 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9166 cp_parser_simulate_error (parser);
9168 /* If we did see an `=' or '{', then we are looking at a declaration
9170 if (cp_parser_parse_definitely (parser))
9173 bool non_constant_p;
9174 bool flags = LOOKUP_ONLYCONVERTING;
9176 /* Create the declaration. */
9177 decl = start_decl (declarator, &type_specifiers,
9178 /*initialized_p=*/true,
9179 attributes, /*prefix_attributes=*/NULL_TREE,
9182 /* Parse the initializer. */
9183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9185 initializer = cp_parser_braced_list (parser, &non_constant_p);
9186 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9191 /* Consume the `='. */
9192 cp_parser_require (parser, CPP_EQ, RT_EQ);
9193 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9195 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9196 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9198 /* Process the initializer. */
9199 cp_finish_decl (decl,
9200 initializer, !non_constant_p,
9205 pop_scope (pushed_scope);
9207 return convert_from_reference (decl);
9210 /* If we didn't even get past the declarator successfully, we are
9211 definitely not looking at a declaration. */
9213 cp_parser_abort_tentative_parse (parser);
9215 /* Otherwise, we are looking at an expression. */
9216 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9219 /* Parses a for-statement or range-for-statement until the closing ')',
9223 cp_parser_for (cp_parser *parser)
9225 tree init, scope, decl;
9228 /* Begin the for-statement. */
9229 scope = begin_for_scope (&init);
9231 /* Parse the initialization. */
9232 is_range_for = cp_parser_for_init_statement (parser, &decl);
9235 return cp_parser_range_for (parser, scope, init, decl);
9237 return cp_parser_c_for (parser, scope, init);
9241 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9243 /* Normal for loop */
9244 tree condition = NULL_TREE;
9245 tree expression = NULL_TREE;
9248 stmt = begin_for_stmt (scope, init);
9249 /* The for-init-statement has already been parsed in
9250 cp_parser_for_init_statement, so no work is needed here. */
9251 finish_for_init_stmt (stmt);
9253 /* If there's a condition, process it. */
9254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9255 condition = cp_parser_condition (parser);
9256 finish_for_cond (condition, stmt);
9257 /* Look for the `;'. */
9258 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9260 /* If there's an expression, process it. */
9261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9262 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9263 finish_for_expr (expression, stmt);
9268 /* Tries to parse a range-based for-statement:
9271 decl-specifier-seq declarator : expression
9273 The decl-specifier-seq declarator and the `:' are already parsed by
9274 cp_parser_for_init_statement. If processing_template_decl it returns a
9275 newly created RANGE_FOR_STMT; if not, it is converted to a
9276 regular FOR_STMT. */
9279 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9281 tree stmt, range_expr;
9283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9285 bool expr_non_constant_p;
9286 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9289 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9291 /* If in template, STMT is converted to a normal for-statement
9292 at instantiation. If not, it is done just ahead. */
9293 if (processing_template_decl)
9295 stmt = begin_range_for_stmt (scope, init);
9296 finish_range_for_decl (stmt, range_decl, range_expr);
9297 if (!type_dependent_expression_p (range_expr)
9298 /* do_auto_deduction doesn't mess with template init-lists. */
9299 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9300 do_range_for_auto_deduction (range_decl, range_expr);
9304 stmt = begin_for_stmt (scope, init);
9305 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9310 /* Subroutine of cp_convert_range_for: given the initializer expression,
9311 builds up the range temporary. */
9314 build_range_temp (tree range_expr)
9316 tree range_type, range_temp;
9318 /* Find out the type deduced by the declaration
9319 `auto &&__range = range_expr'. */
9320 range_type = cp_build_reference_type (make_auto (), true);
9321 range_type = do_auto_deduction (range_type, range_expr,
9322 type_uses_auto (range_type));
9324 /* Create the __range variable. */
9325 range_temp = build_decl (input_location, VAR_DECL,
9326 get_identifier ("__for_range"), range_type);
9327 TREE_USED (range_temp) = 1;
9328 DECL_ARTIFICIAL (range_temp) = 1;
9333 /* Used by cp_parser_range_for in template context: we aren't going to
9334 do a full conversion yet, but we still need to resolve auto in the
9335 type of the for-range-declaration if present. This is basically
9336 a shortcut version of cp_convert_range_for. */
9339 do_range_for_auto_deduction (tree decl, tree range_expr)
9341 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9344 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9345 range_temp = convert_from_reference (build_range_temp (range_expr));
9346 iter_type = (cp_parser_perform_range_for_lookup
9347 (range_temp, &begin_dummy, &end_dummy));
9348 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9349 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9350 tf_warning_or_error);
9351 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9352 iter_decl, auto_node);
9356 /* Converts a range-based for-statement into a normal
9357 for-statement, as per the definition.
9359 for (RANGE_DECL : RANGE_EXPR)
9362 should be equivalent to:
9365 auto &&__range = RANGE_EXPR;
9366 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9370 RANGE_DECL = *__begin;
9375 If RANGE_EXPR is an array:
9376 BEGIN_EXPR = __range
9377 END_EXPR = __range + ARRAY_SIZE(__range)
9378 Else if RANGE_EXPR has a member 'begin' or 'end':
9379 BEGIN_EXPR = __range.begin()
9380 END_EXPR = __range.end()
9382 BEGIN_EXPR = begin(__range)
9383 END_EXPR = end(__range);
9385 If __range has a member 'begin' but not 'end', or vice versa, we must
9386 still use the second alternative (it will surely fail, however).
9387 When calling begin()/end() in the third alternative we must use
9388 argument dependent lookup, but always considering 'std' as an associated
9392 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9395 tree iter_type, begin_expr, end_expr;
9396 tree condition, expression;
9398 if (range_decl == error_mark_node || range_expr == error_mark_node)
9399 /* If an error happened previously do nothing or else a lot of
9400 unhelpful errors would be issued. */
9401 begin_expr = end_expr = iter_type = error_mark_node;
9404 tree range_temp = build_range_temp (range_expr);
9405 pushdecl (range_temp);
9406 cp_finish_decl (range_temp, range_expr,
9407 /*is_constant_init*/false, NULL_TREE,
9408 LOOKUP_ONLYCONVERTING);
9410 range_temp = convert_from_reference (range_temp);
9411 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9412 &begin_expr, &end_expr);
9415 /* The new for initialization statement. */
9416 begin = build_decl (input_location, VAR_DECL,
9417 get_identifier ("__for_begin"), iter_type);
9418 TREE_USED (begin) = 1;
9419 DECL_ARTIFICIAL (begin) = 1;
9421 cp_finish_decl (begin, begin_expr,
9422 /*is_constant_init*/false, NULL_TREE,
9423 LOOKUP_ONLYCONVERTING);
9425 end = build_decl (input_location, VAR_DECL,
9426 get_identifier ("__for_end"), iter_type);
9427 TREE_USED (end) = 1;
9428 DECL_ARTIFICIAL (end) = 1;
9430 cp_finish_decl (end, end_expr,
9431 /*is_constant_init*/false, NULL_TREE,
9432 LOOKUP_ONLYCONVERTING);
9434 finish_for_init_stmt (statement);
9436 /* The new for condition. */
9437 condition = build_x_binary_op (NE_EXPR,
9440 NULL, tf_warning_or_error);
9441 finish_for_cond (condition, statement);
9443 /* The new increment expression. */
9444 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9445 finish_for_expr (expression, statement);
9447 /* The declaration is initialized with *__begin inside the loop body. */
9448 cp_finish_decl (range_decl,
9449 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9450 /*is_constant_init*/false, NULL_TREE,
9451 LOOKUP_ONLYCONVERTING);
9456 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9457 We need to solve both at the same time because the method used
9458 depends on the existence of members begin or end.
9459 Returns the type deduced for the iterator expression. */
9462 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9464 if (error_operand_p (range))
9466 *begin = *end = error_mark_node;
9467 return error_mark_node;
9470 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9472 error ("range-based %<for%> expression of type %qT "
9473 "has incomplete type", TREE_TYPE (range));
9474 *begin = *end = error_mark_node;
9475 return error_mark_node;
9477 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9479 /* If RANGE is an array, we will use pointer arithmetic. */
9481 *end = build_binary_op (input_location, PLUS_EXPR,
9483 array_type_nelts_top (TREE_TYPE (range)),
9485 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9489 /* If it is not an array, we must do a bit of magic. */
9490 tree id_begin, id_end;
9491 tree member_begin, member_end;
9493 *begin = *end = error_mark_node;
9495 id_begin = get_identifier ("begin");
9496 id_end = get_identifier ("end");
9497 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9498 /*protect=*/2, /*want_type=*/false);
9499 member_end = lookup_member (TREE_TYPE (range), id_end,
9500 /*protect=*/2, /*want_type=*/false);
9502 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9504 /* Use the member functions. */
9505 if (member_begin != NULL_TREE)
9506 *begin = cp_parser_range_for_member_function (range, id_begin);
9508 error ("range-based %<for%> expression of type %qT has an "
9509 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9511 if (member_end != NULL_TREE)
9512 *end = cp_parser_range_for_member_function (range, id_end);
9514 error ("range-based %<for%> expression of type %qT has a "
9515 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9519 /* Use global functions with ADL. */
9521 vec = make_tree_vector ();
9523 VEC_safe_push (tree, gc, vec, range);
9525 member_begin = perform_koenig_lookup (id_begin, vec,
9526 /*include_std=*/true,
9527 tf_warning_or_error);
9528 *begin = finish_call_expr (member_begin, &vec, false, true,
9529 tf_warning_or_error);
9530 member_end = perform_koenig_lookup (id_end, vec,
9531 /*include_std=*/true,
9532 tf_warning_or_error);
9533 *end = finish_call_expr (member_end, &vec, false, true,
9534 tf_warning_or_error);
9536 release_tree_vector (vec);
9539 /* Last common checks. */
9540 if (*begin == error_mark_node || *end == error_mark_node)
9542 /* If one of the expressions is an error do no more checks. */
9543 *begin = *end = error_mark_node;
9544 return error_mark_node;
9548 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9549 /* The unqualified type of the __begin and __end temporaries should
9550 be the same, as required by the multiple auto declaration. */
9551 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9552 error ("inconsistent begin/end types in range-based %<for%> "
9553 "statement: %qT and %qT",
9554 TREE_TYPE (*begin), TREE_TYPE (*end));
9560 /* Helper function for cp_parser_perform_range_for_lookup.
9561 Builds a tree for RANGE.IDENTIFIER(). */
9564 cp_parser_range_for_member_function (tree range, tree identifier)
9569 member = finish_class_member_access_expr (range, identifier,
9570 false, tf_warning_or_error);
9571 if (member == error_mark_node)
9572 return error_mark_node;
9574 vec = make_tree_vector ();
9575 res = finish_call_expr (member, &vec,
9576 /*disallow_virtual=*/false,
9578 tf_warning_or_error);
9579 release_tree_vector (vec);
9583 /* Parse an iteration-statement.
9585 iteration-statement:
9586 while ( condition ) statement
9587 do statement while ( expression ) ;
9588 for ( for-init-statement condition [opt] ; expression [opt] )
9591 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9594 cp_parser_iteration_statement (cp_parser* parser)
9599 unsigned char in_statement;
9601 /* Peek at the next token. */
9602 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9604 return error_mark_node;
9606 /* Remember whether or not we are already within an iteration
9608 in_statement = parser->in_statement;
9610 /* See what kind of keyword it is. */
9611 keyword = token->keyword;
9618 /* Begin the while-statement. */
9619 statement = begin_while_stmt ();
9620 /* Look for the `('. */
9621 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9622 /* Parse the condition. */
9623 condition = cp_parser_condition (parser);
9624 finish_while_stmt_cond (condition, statement);
9625 /* Look for the `)'. */
9626 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9627 /* Parse the dependent statement. */
9628 parser->in_statement = IN_ITERATION_STMT;
9629 cp_parser_already_scoped_statement (parser);
9630 parser->in_statement = in_statement;
9631 /* We're done with the while-statement. */
9632 finish_while_stmt (statement);
9640 /* Begin the do-statement. */
9641 statement = begin_do_stmt ();
9642 /* Parse the body of the do-statement. */
9643 parser->in_statement = IN_ITERATION_STMT;
9644 cp_parser_implicitly_scoped_statement (parser, NULL);
9645 parser->in_statement = in_statement;
9646 finish_do_body (statement);
9647 /* Look for the `while' keyword. */
9648 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9649 /* Look for the `('. */
9650 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9651 /* Parse the expression. */
9652 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9653 /* We're done with the do-statement. */
9654 finish_do_stmt (expression, statement);
9655 /* Look for the `)'. */
9656 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9657 /* Look for the `;'. */
9658 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9664 /* Look for the `('. */
9665 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9667 statement = cp_parser_for (parser);
9669 /* Look for the `)'. */
9670 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9672 /* Parse the body of the for-statement. */
9673 parser->in_statement = IN_ITERATION_STMT;
9674 cp_parser_already_scoped_statement (parser);
9675 parser->in_statement = in_statement;
9677 /* We're done with the for-statement. */
9678 finish_for_stmt (statement);
9683 cp_parser_error (parser, "expected iteration-statement");
9684 statement = error_mark_node;
9691 /* Parse a for-init-statement or the declarator of a range-based-for.
9692 Returns true if a range-based-for declaration is seen.
9695 expression-statement
9696 simple-declaration */
9699 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9701 /* If the next token is a `;', then we have an empty
9702 expression-statement. Grammatically, this is also a
9703 simple-declaration, but an invalid one, because it does not
9704 declare anything. Therefore, if we did not handle this case
9705 specially, we would issue an error message about an invalid
9707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9709 bool is_range_for = false;
9710 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9712 parser->colon_corrects_to_scope_p = false;
9714 /* We're going to speculatively look for a declaration, falling back
9715 to an expression, if necessary. */
9716 cp_parser_parse_tentatively (parser);
9717 /* Parse the declaration. */
9718 cp_parser_simple_declaration (parser,
9719 /*function_definition_allowed_p=*/false,
9721 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9722 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9724 /* It is a range-for, consume the ':' */
9725 cp_lexer_consume_token (parser->lexer);
9726 is_range_for = true;
9727 if (cxx_dialect < cxx0x)
9729 error_at (cp_lexer_peek_token (parser->lexer)->location,
9730 "range-based %<for%> loops are not allowed "
9732 *decl = error_mark_node;
9736 /* The ';' is not consumed yet because we told
9737 cp_parser_simple_declaration not to. */
9738 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9740 if (cp_parser_parse_definitely (parser))
9741 return is_range_for;
9742 /* If the tentative parse failed, then we shall need to look for an
9743 expression-statement. */
9745 /* If we are here, it is an expression-statement. */
9746 cp_parser_expression_statement (parser, NULL_TREE);
9750 /* Parse a jump-statement.
9755 return expression [opt] ;
9756 return braced-init-list ;
9764 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9767 cp_parser_jump_statement (cp_parser* parser)
9769 tree statement = error_mark_node;
9772 unsigned char in_statement;
9774 /* Peek at the next token. */
9775 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9777 return error_mark_node;
9779 /* See what kind of keyword it is. */
9780 keyword = token->keyword;
9784 in_statement = parser->in_statement & ~IN_IF_STMT;
9785 switch (in_statement)
9788 error_at (token->location, "break statement not within loop or switch");
9791 gcc_assert ((in_statement & IN_SWITCH_STMT)
9792 || in_statement == IN_ITERATION_STMT);
9793 statement = finish_break_stmt ();
9796 error_at (token->location, "invalid exit from OpenMP structured block");
9799 error_at (token->location, "break statement used with OpenMP for loop");
9802 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9806 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9809 error_at (token->location, "continue statement not within a loop");
9811 case IN_ITERATION_STMT:
9813 statement = finish_continue_stmt ();
9816 error_at (token->location, "invalid exit from OpenMP structured block");
9821 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9827 bool expr_non_constant_p;
9829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9831 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9832 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9834 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9835 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9837 /* If the next token is a `;', then there is no
9840 /* Build the return-statement. */
9841 statement = finish_return_stmt (expr);
9842 /* Look for the final `;'. */
9843 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9848 /* Create the goto-statement. */
9849 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9851 /* Issue a warning about this use of a GNU extension. */
9852 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9853 /* Consume the '*' token. */
9854 cp_lexer_consume_token (parser->lexer);
9855 /* Parse the dependent expression. */
9856 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9859 finish_goto_stmt (cp_parser_identifier (parser));
9860 /* Look for the final `;'. */
9861 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9865 cp_parser_error (parser, "expected jump-statement");
9872 /* Parse a declaration-statement.
9874 declaration-statement:
9875 block-declaration */
9878 cp_parser_declaration_statement (cp_parser* parser)
9882 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9883 p = obstack_alloc (&declarator_obstack, 0);
9885 /* Parse the block-declaration. */
9886 cp_parser_block_declaration (parser, /*statement_p=*/true);
9888 /* Free any declarators allocated. */
9889 obstack_free (&declarator_obstack, p);
9891 /* Finish off the statement. */
9895 /* Some dependent statements (like `if (cond) statement'), are
9896 implicitly in their own scope. In other words, if the statement is
9897 a single statement (as opposed to a compound-statement), it is
9898 none-the-less treated as if it were enclosed in braces. Any
9899 declarations appearing in the dependent statement are out of scope
9900 after control passes that point. This function parses a statement,
9901 but ensures that is in its own scope, even if it is not a
9904 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9905 is a (possibly labeled) if statement which is not enclosed in
9906 braces and has an else clause. This is used to implement
9909 Returns the new statement. */
9912 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9919 /* Mark if () ; with a special NOP_EXPR. */
9920 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9922 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9923 cp_lexer_consume_token (parser->lexer);
9924 statement = add_stmt (build_empty_stmt (loc));
9926 /* if a compound is opened, we simply parse the statement directly. */
9927 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9928 statement = cp_parser_compound_statement (parser, NULL, false, false);
9929 /* If the token is not a `{', then we must take special action. */
9932 /* Create a compound-statement. */
9933 statement = begin_compound_stmt (0);
9934 /* Parse the dependent-statement. */
9935 cp_parser_statement (parser, NULL_TREE, false, if_p);
9936 /* Finish the dummy compound-statement. */
9937 finish_compound_stmt (statement);
9940 /* Return the statement. */
9944 /* For some dependent statements (like `while (cond) statement'), we
9945 have already created a scope. Therefore, even if the dependent
9946 statement is a compound-statement, we do not want to create another
9950 cp_parser_already_scoped_statement (cp_parser* parser)
9952 /* If the token is a `{', then we must take special action. */
9953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9954 cp_parser_statement (parser, NULL_TREE, false, NULL);
9957 /* Avoid calling cp_parser_compound_statement, so that we
9958 don't create a new scope. Do everything else by hand. */
9959 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9960 /* If the next keyword is `__label__' we have a label declaration. */
9961 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9962 cp_parser_label_declaration (parser);
9963 /* Parse an (optional) statement-seq. */
9964 cp_parser_statement_seq_opt (parser, NULL_TREE);
9965 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9969 /* Declarations [gram.dcl.dcl] */
9971 /* Parse an optional declaration-sequence.
9975 declaration-seq declaration */
9978 cp_parser_declaration_seq_opt (cp_parser* parser)
9984 token = cp_lexer_peek_token (parser->lexer);
9986 if (token->type == CPP_CLOSE_BRACE
9987 || token->type == CPP_EOF
9988 || token->type == CPP_PRAGMA_EOL)
9991 if (token->type == CPP_SEMICOLON)
9993 /* A declaration consisting of a single semicolon is
9994 invalid. Allow it unless we're being pedantic. */
9995 cp_lexer_consume_token (parser->lexer);
9996 if (!in_system_header)
9997 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10001 /* If we're entering or exiting a region that's implicitly
10002 extern "C", modify the lang context appropriately. */
10003 if (!parser->implicit_extern_c && token->implicit_extern_c)
10005 push_lang_context (lang_name_c);
10006 parser->implicit_extern_c = true;
10008 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10010 pop_lang_context ();
10011 parser->implicit_extern_c = false;
10014 if (token->type == CPP_PRAGMA)
10016 /* A top-level declaration can consist solely of a #pragma.
10017 A nested declaration cannot, so this is done here and not
10018 in cp_parser_declaration. (A #pragma at block scope is
10019 handled in cp_parser_statement.) */
10020 cp_parser_pragma (parser, pragma_external);
10024 /* Parse the declaration itself. */
10025 cp_parser_declaration (parser);
10029 /* Parse a declaration.
10033 function-definition
10034 template-declaration
10035 explicit-instantiation
10036 explicit-specialization
10037 linkage-specification
10038 namespace-definition
10043 __extension__ declaration */
10046 cp_parser_declaration (cp_parser* parser)
10050 int saved_pedantic;
10052 tree attributes = NULL_TREE;
10054 /* Check for the `__extension__' keyword. */
10055 if (cp_parser_extension_opt (parser, &saved_pedantic))
10057 /* Parse the qualified declaration. */
10058 cp_parser_declaration (parser);
10059 /* Restore the PEDANTIC flag. */
10060 pedantic = saved_pedantic;
10065 /* Try to figure out what kind of declaration is present. */
10066 token1 = *cp_lexer_peek_token (parser->lexer);
10068 if (token1.type != CPP_EOF)
10069 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10072 token2.type = CPP_EOF;
10073 token2.keyword = RID_MAX;
10076 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10077 p = obstack_alloc (&declarator_obstack, 0);
10079 /* If the next token is `extern' and the following token is a string
10080 literal, then we have a linkage specification. */
10081 if (token1.keyword == RID_EXTERN
10082 && cp_parser_is_pure_string_literal (&token2))
10083 cp_parser_linkage_specification (parser);
10084 /* If the next token is `template', then we have either a template
10085 declaration, an explicit instantiation, or an explicit
10087 else if (token1.keyword == RID_TEMPLATE)
10089 /* `template <>' indicates a template specialization. */
10090 if (token2.type == CPP_LESS
10091 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10092 cp_parser_explicit_specialization (parser);
10093 /* `template <' indicates a template declaration. */
10094 else if (token2.type == CPP_LESS)
10095 cp_parser_template_declaration (parser, /*member_p=*/false);
10096 /* Anything else must be an explicit instantiation. */
10098 cp_parser_explicit_instantiation (parser);
10100 /* If the next token is `export', then we have a template
10102 else if (token1.keyword == RID_EXPORT)
10103 cp_parser_template_declaration (parser, /*member_p=*/false);
10104 /* If the next token is `extern', 'static' or 'inline' and the one
10105 after that is `template', we have a GNU extended explicit
10106 instantiation directive. */
10107 else if (cp_parser_allow_gnu_extensions_p (parser)
10108 && (token1.keyword == RID_EXTERN
10109 || token1.keyword == RID_STATIC
10110 || token1.keyword == RID_INLINE)
10111 && token2.keyword == RID_TEMPLATE)
10112 cp_parser_explicit_instantiation (parser);
10113 /* If the next token is `namespace', check for a named or unnamed
10114 namespace definition. */
10115 else if (token1.keyword == RID_NAMESPACE
10116 && (/* A named namespace definition. */
10117 (token2.type == CPP_NAME
10118 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10120 /* An unnamed namespace definition. */
10121 || token2.type == CPP_OPEN_BRACE
10122 || token2.keyword == RID_ATTRIBUTE))
10123 cp_parser_namespace_definition (parser);
10124 /* An inline (associated) namespace definition. */
10125 else if (token1.keyword == RID_INLINE
10126 && token2.keyword == RID_NAMESPACE)
10127 cp_parser_namespace_definition (parser);
10128 /* Objective-C++ declaration/definition. */
10129 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10130 cp_parser_objc_declaration (parser, NULL_TREE);
10131 else if (c_dialect_objc ()
10132 && token1.keyword == RID_ATTRIBUTE
10133 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10134 cp_parser_objc_declaration (parser, attributes);
10135 /* We must have either a block declaration or a function
10138 /* Try to parse a block-declaration, or a function-definition. */
10139 cp_parser_block_declaration (parser, /*statement_p=*/false);
10141 /* Free any declarators allocated. */
10142 obstack_free (&declarator_obstack, p);
10145 /* Parse a block-declaration.
10150 namespace-alias-definition
10157 __extension__ block-declaration
10162 static_assert-declaration
10164 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10165 part of a declaration-statement. */
10168 cp_parser_block_declaration (cp_parser *parser,
10172 int saved_pedantic;
10174 /* Check for the `__extension__' keyword. */
10175 if (cp_parser_extension_opt (parser, &saved_pedantic))
10177 /* Parse the qualified declaration. */
10178 cp_parser_block_declaration (parser, statement_p);
10179 /* Restore the PEDANTIC flag. */
10180 pedantic = saved_pedantic;
10185 /* Peek at the next token to figure out which kind of declaration is
10187 token1 = cp_lexer_peek_token (parser->lexer);
10189 /* If the next keyword is `asm', we have an asm-definition. */
10190 if (token1->keyword == RID_ASM)
10193 cp_parser_commit_to_tentative_parse (parser);
10194 cp_parser_asm_definition (parser);
10196 /* If the next keyword is `namespace', we have a
10197 namespace-alias-definition. */
10198 else if (token1->keyword == RID_NAMESPACE)
10199 cp_parser_namespace_alias_definition (parser);
10200 /* If the next keyword is `using', we have a
10201 using-declaration, a using-directive, or an alias-declaration. */
10202 else if (token1->keyword == RID_USING)
10207 cp_parser_commit_to_tentative_parse (parser);
10208 /* If the token after `using' is `namespace', then we have a
10209 using-directive. */
10210 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10211 if (token2->keyword == RID_NAMESPACE)
10212 cp_parser_using_directive (parser);
10213 /* If the second token after 'using' is '=', then we have an
10214 alias-declaration. */
10215 else if (cxx_dialect >= cxx0x
10216 && token2->type == CPP_NAME
10217 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10218 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10219 == RID_ATTRIBUTE)))
10220 cp_parser_alias_declaration (parser);
10221 /* Otherwise, it's a using-declaration. */
10223 cp_parser_using_declaration (parser,
10224 /*access_declaration_p=*/false);
10226 /* If the next keyword is `__label__' we have a misplaced label
10228 else if (token1->keyword == RID_LABEL)
10230 cp_lexer_consume_token (parser->lexer);
10231 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10232 cp_parser_skip_to_end_of_statement (parser);
10233 /* If the next token is now a `;', consume it. */
10234 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10235 cp_lexer_consume_token (parser->lexer);
10237 /* If the next token is `static_assert' we have a static assertion. */
10238 else if (token1->keyword == RID_STATIC_ASSERT)
10239 cp_parser_static_assert (parser, /*member_p=*/false);
10240 /* Anything else must be a simple-declaration. */
10242 cp_parser_simple_declaration (parser, !statement_p,
10243 /*maybe_range_for_decl*/NULL);
10246 /* Parse a simple-declaration.
10248 simple-declaration:
10249 decl-specifier-seq [opt] init-declarator-list [opt] ;
10251 init-declarator-list:
10253 init-declarator-list , init-declarator
10255 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10256 function-definition as a simple-declaration.
10258 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10259 parsed declaration if it is an uninitialized single declarator not followed
10260 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10261 if present, will not be consumed. */
10264 cp_parser_simple_declaration (cp_parser* parser,
10265 bool function_definition_allowed_p,
10266 tree *maybe_range_for_decl)
10268 cp_decl_specifier_seq decl_specifiers;
10269 int declares_class_or_enum;
10270 bool saw_declarator;
10272 if (maybe_range_for_decl)
10273 *maybe_range_for_decl = NULL_TREE;
10275 /* Defer access checks until we know what is being declared; the
10276 checks for names appearing in the decl-specifier-seq should be
10277 done as if we were in the scope of the thing being declared. */
10278 push_deferring_access_checks (dk_deferred);
10280 /* Parse the decl-specifier-seq. We have to keep track of whether
10281 or not the decl-specifier-seq declares a named class or
10282 enumeration type, since that is the only case in which the
10283 init-declarator-list is allowed to be empty.
10287 In a simple-declaration, the optional init-declarator-list can be
10288 omitted only when declaring a class or enumeration, that is when
10289 the decl-specifier-seq contains either a class-specifier, an
10290 elaborated-type-specifier, or an enum-specifier. */
10291 cp_parser_decl_specifier_seq (parser,
10292 CP_PARSER_FLAGS_OPTIONAL,
10294 &declares_class_or_enum);
10295 /* We no longer need to defer access checks. */
10296 stop_deferring_access_checks ();
10298 /* In a block scope, a valid declaration must always have a
10299 decl-specifier-seq. By not trying to parse declarators, we can
10300 resolve the declaration/expression ambiguity more quickly. */
10301 if (!function_definition_allowed_p
10302 && !decl_specifiers.any_specifiers_p)
10304 cp_parser_error (parser, "expected declaration");
10308 /* If the next two tokens are both identifiers, the code is
10309 erroneous. The usual cause of this situation is code like:
10313 where "T" should name a type -- but does not. */
10314 if (!decl_specifiers.any_type_specifiers_p
10315 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10317 /* If parsing tentatively, we should commit; we really are
10318 looking at a declaration. */
10319 cp_parser_commit_to_tentative_parse (parser);
10324 /* If we have seen at least one decl-specifier, and the next token
10325 is not a parenthesis, then we must be looking at a declaration.
10326 (After "int (" we might be looking at a functional cast.) */
10327 if (decl_specifiers.any_specifiers_p
10328 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10329 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10330 && !cp_parser_error_occurred (parser))
10331 cp_parser_commit_to_tentative_parse (parser);
10333 /* Keep going until we hit the `;' at the end of the simple
10335 saw_declarator = false;
10336 while (cp_lexer_next_token_is_not (parser->lexer,
10340 bool function_definition_p;
10343 if (saw_declarator)
10345 /* If we are processing next declarator, coma is expected */
10346 token = cp_lexer_peek_token (parser->lexer);
10347 gcc_assert (token->type == CPP_COMMA);
10348 cp_lexer_consume_token (parser->lexer);
10349 if (maybe_range_for_decl)
10350 *maybe_range_for_decl = error_mark_node;
10353 saw_declarator = true;
10355 /* Parse the init-declarator. */
10356 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10358 function_definition_allowed_p,
10359 /*member_p=*/false,
10360 declares_class_or_enum,
10361 &function_definition_p,
10362 maybe_range_for_decl);
10363 /* If an error occurred while parsing tentatively, exit quickly.
10364 (That usually happens when in the body of a function; each
10365 statement is treated as a declaration-statement until proven
10367 if (cp_parser_error_occurred (parser))
10369 /* Handle function definitions specially. */
10370 if (function_definition_p)
10372 /* If the next token is a `,', then we are probably
10373 processing something like:
10377 which is erroneous. */
10378 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10380 cp_token *token = cp_lexer_peek_token (parser->lexer);
10381 error_at (token->location,
10383 " declarations and function-definitions is forbidden");
10385 /* Otherwise, we're done with the list of declarators. */
10388 pop_deferring_access_checks ();
10392 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10393 *maybe_range_for_decl = decl;
10394 /* The next token should be either a `,' or a `;'. */
10395 token = cp_lexer_peek_token (parser->lexer);
10396 /* If it's a `,', there are more declarators to come. */
10397 if (token->type == CPP_COMMA)
10398 /* will be consumed next time around */;
10399 /* If it's a `;', we are done. */
10400 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10402 /* Anything else is an error. */
10405 /* If we have already issued an error message we don't need
10406 to issue another one. */
10407 if (decl != error_mark_node
10408 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10409 cp_parser_error (parser, "expected %<,%> or %<;%>");
10410 /* Skip tokens until we reach the end of the statement. */
10411 cp_parser_skip_to_end_of_statement (parser);
10412 /* If the next token is now a `;', consume it. */
10413 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10414 cp_lexer_consume_token (parser->lexer);
10417 /* After the first time around, a function-definition is not
10418 allowed -- even if it was OK at first. For example:
10423 function_definition_allowed_p = false;
10426 /* Issue an error message if no declarators are present, and the
10427 decl-specifier-seq does not itself declare a class or
10429 if (!saw_declarator)
10431 if (cp_parser_declares_only_class_p (parser))
10432 shadow_tag (&decl_specifiers);
10433 /* Perform any deferred access checks. */
10434 perform_deferred_access_checks ();
10437 /* Consume the `;'. */
10438 if (!maybe_range_for_decl)
10439 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10442 pop_deferring_access_checks ();
10445 /* Parse a decl-specifier-seq.
10447 decl-specifier-seq:
10448 decl-specifier-seq [opt] decl-specifier
10451 storage-class-specifier
10462 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10464 The parser flags FLAGS is used to control type-specifier parsing.
10466 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10469 1: one of the decl-specifiers is an elaborated-type-specifier
10470 (i.e., a type declaration)
10471 2: one of the decl-specifiers is an enum-specifier or a
10472 class-specifier (i.e., a type definition)
10477 cp_parser_decl_specifier_seq (cp_parser* parser,
10478 cp_parser_flags flags,
10479 cp_decl_specifier_seq *decl_specs,
10480 int* declares_class_or_enum)
10482 bool constructor_possible_p = !parser->in_declarator_p;
10483 cp_token *start_token = NULL;
10485 /* Clear DECL_SPECS. */
10486 clear_decl_specs (decl_specs);
10488 /* Assume no class or enumeration type is declared. */
10489 *declares_class_or_enum = 0;
10491 /* Keep reading specifiers until there are no more to read. */
10494 bool constructor_p;
10495 bool found_decl_spec;
10498 /* Peek at the next token. */
10499 token = cp_lexer_peek_token (parser->lexer);
10501 /* Save the first token of the decl spec list for error
10504 start_token = token;
10505 /* Handle attributes. */
10506 if (token->keyword == RID_ATTRIBUTE)
10508 /* Parse the attributes. */
10509 decl_specs->attributes
10510 = chainon (decl_specs->attributes,
10511 cp_parser_attributes_opt (parser));
10514 /* Assume we will find a decl-specifier keyword. */
10515 found_decl_spec = true;
10516 /* If the next token is an appropriate keyword, we can simply
10517 add it to the list. */
10518 switch (token->keyword)
10524 if (!at_class_scope_p ())
10526 error_at (token->location, "%<friend%> used outside of class");
10527 cp_lexer_purge_token (parser->lexer);
10531 ++decl_specs->specs[(int) ds_friend];
10532 /* Consume the token. */
10533 cp_lexer_consume_token (parser->lexer);
10537 case RID_CONSTEXPR:
10538 ++decl_specs->specs[(int) ds_constexpr];
10539 cp_lexer_consume_token (parser->lexer);
10542 /* function-specifier:
10549 cp_parser_function_specifier_opt (parser, decl_specs);
10555 ++decl_specs->specs[(int) ds_typedef];
10556 /* Consume the token. */
10557 cp_lexer_consume_token (parser->lexer);
10558 /* A constructor declarator cannot appear in a typedef. */
10559 constructor_possible_p = false;
10560 /* The "typedef" keyword can only occur in a declaration; we
10561 may as well commit at this point. */
10562 cp_parser_commit_to_tentative_parse (parser);
10564 if (decl_specs->storage_class != sc_none)
10565 decl_specs->conflicting_specifiers_p = true;
10568 /* storage-class-specifier:
10578 if (cxx_dialect == cxx98)
10580 /* Consume the token. */
10581 cp_lexer_consume_token (parser->lexer);
10583 /* Complain about `auto' as a storage specifier, if
10584 we're complaining about C++0x compatibility. */
10585 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10586 " changes meaning in C++11; please remove it");
10588 /* Set the storage class anyway. */
10589 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10593 /* C++0x auto type-specifier. */
10594 found_decl_spec = false;
10601 /* Consume the token. */
10602 cp_lexer_consume_token (parser->lexer);
10603 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10607 /* Consume the token. */
10608 cp_lexer_consume_token (parser->lexer);
10609 ++decl_specs->specs[(int) ds_thread];
10613 /* We did not yet find a decl-specifier yet. */
10614 found_decl_spec = false;
10618 if (found_decl_spec
10619 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10620 && token->keyword != RID_CONSTEXPR)
10621 error ("decl-specifier invalid in condition");
10623 /* Constructors are a special case. The `S' in `S()' is not a
10624 decl-specifier; it is the beginning of the declarator. */
10626 = (!found_decl_spec
10627 && constructor_possible_p
10628 && (cp_parser_constructor_declarator_p
10629 (parser, decl_specs->specs[(int) ds_friend] != 0)));
10631 /* If we don't have a DECL_SPEC yet, then we must be looking at
10632 a type-specifier. */
10633 if (!found_decl_spec && !constructor_p)
10635 int decl_spec_declares_class_or_enum;
10636 bool is_cv_qualifier;
10640 = cp_parser_type_specifier (parser, flags,
10642 /*is_declaration=*/true,
10643 &decl_spec_declares_class_or_enum,
10645 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10647 /* If this type-specifier referenced a user-defined type
10648 (a typedef, class-name, etc.), then we can't allow any
10649 more such type-specifiers henceforth.
10653 The longest sequence of decl-specifiers that could
10654 possibly be a type name is taken as the
10655 decl-specifier-seq of a declaration. The sequence shall
10656 be self-consistent as described below.
10660 As a general rule, at most one type-specifier is allowed
10661 in the complete decl-specifier-seq of a declaration. The
10662 only exceptions are the following:
10664 -- const or volatile can be combined with any other
10667 -- signed or unsigned can be combined with char, long,
10675 void g (const int Pc);
10677 Here, Pc is *not* part of the decl-specifier seq; it's
10678 the declarator. Therefore, once we see a type-specifier
10679 (other than a cv-qualifier), we forbid any additional
10680 user-defined types. We *do* still allow things like `int
10681 int' to be considered a decl-specifier-seq, and issue the
10682 error message later. */
10683 if (type_spec && !is_cv_qualifier)
10684 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10685 /* A constructor declarator cannot follow a type-specifier. */
10688 constructor_possible_p = false;
10689 found_decl_spec = true;
10690 if (!is_cv_qualifier)
10691 decl_specs->any_type_specifiers_p = true;
10695 /* If we still do not have a DECL_SPEC, then there are no more
10696 decl-specifiers. */
10697 if (!found_decl_spec)
10700 decl_specs->any_specifiers_p = true;
10701 /* After we see one decl-specifier, further decl-specifiers are
10702 always optional. */
10703 flags |= CP_PARSER_FLAGS_OPTIONAL;
10706 cp_parser_check_decl_spec (decl_specs, start_token->location);
10708 /* Don't allow a friend specifier with a class definition. */
10709 if (decl_specs->specs[(int) ds_friend] != 0
10710 && (*declares_class_or_enum & 2))
10711 error_at (start_token->location,
10712 "class definition may not be declared a friend");
10715 /* Parse an (optional) storage-class-specifier.
10717 storage-class-specifier:
10726 storage-class-specifier:
10729 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10732 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10734 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10737 if (cxx_dialect != cxx98)
10739 /* Fall through for C++98. */
10746 /* Consume the token. */
10747 return cp_lexer_consume_token (parser->lexer)->u.value;
10754 /* Parse an (optional) function-specifier.
10756 function-specifier:
10761 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10762 Updates DECL_SPECS, if it is non-NULL. */
10765 cp_parser_function_specifier_opt (cp_parser* parser,
10766 cp_decl_specifier_seq *decl_specs)
10768 cp_token *token = cp_lexer_peek_token (parser->lexer);
10769 switch (token->keyword)
10773 ++decl_specs->specs[(int) ds_inline];
10777 /* 14.5.2.3 [temp.mem]
10779 A member function template shall not be virtual. */
10780 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10781 error_at (token->location, "templates may not be %<virtual%>");
10782 else if (decl_specs)
10783 ++decl_specs->specs[(int) ds_virtual];
10788 ++decl_specs->specs[(int) ds_explicit];
10795 /* Consume the token. */
10796 return cp_lexer_consume_token (parser->lexer)->u.value;
10799 /* Parse a linkage-specification.
10801 linkage-specification:
10802 extern string-literal { declaration-seq [opt] }
10803 extern string-literal declaration */
10806 cp_parser_linkage_specification (cp_parser* parser)
10810 /* Look for the `extern' keyword. */
10811 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10813 /* Look for the string-literal. */
10814 linkage = cp_parser_string_literal (parser, false, false);
10816 /* Transform the literal into an identifier. If the literal is a
10817 wide-character string, or contains embedded NULs, then we can't
10818 handle it as the user wants. */
10819 if (strlen (TREE_STRING_POINTER (linkage))
10820 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10822 cp_parser_error (parser, "invalid linkage-specification");
10823 /* Assume C++ linkage. */
10824 linkage = lang_name_cplusplus;
10827 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10829 /* We're now using the new linkage. */
10830 push_lang_context (linkage);
10832 /* If the next token is a `{', then we're using the first
10834 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10836 /* Consume the `{' token. */
10837 cp_lexer_consume_token (parser->lexer);
10838 /* Parse the declarations. */
10839 cp_parser_declaration_seq_opt (parser);
10840 /* Look for the closing `}'. */
10841 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10843 /* Otherwise, there's just one declaration. */
10846 bool saved_in_unbraced_linkage_specification_p;
10848 saved_in_unbraced_linkage_specification_p
10849 = parser->in_unbraced_linkage_specification_p;
10850 parser->in_unbraced_linkage_specification_p = true;
10851 cp_parser_declaration (parser);
10852 parser->in_unbraced_linkage_specification_p
10853 = saved_in_unbraced_linkage_specification_p;
10856 /* We're done with the linkage-specification. */
10857 pop_lang_context ();
10860 /* Parse a static_assert-declaration.
10862 static_assert-declaration:
10863 static_assert ( constant-expression , string-literal ) ;
10865 If MEMBER_P, this static_assert is a class member. */
10868 cp_parser_static_assert(cp_parser *parser, bool member_p)
10873 location_t saved_loc;
10876 /* Peek at the `static_assert' token so we can keep track of exactly
10877 where the static assertion started. */
10878 token = cp_lexer_peek_token (parser->lexer);
10879 saved_loc = token->location;
10881 /* Look for the `static_assert' keyword. */
10882 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10886 /* We know we are in a static assertion; commit to any tentative
10888 if (cp_parser_parsing_tentatively (parser))
10889 cp_parser_commit_to_tentative_parse (parser);
10891 /* Parse the `(' starting the static assertion condition. */
10892 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10894 /* Parse the constant-expression. Allow a non-constant expression
10895 here in order to give better diagnostics in finish_static_assert. */
10897 cp_parser_constant_expression (parser,
10898 /*allow_non_constant_p=*/true,
10899 /*non_constant_p=*/&dummy);
10901 /* Parse the separating `,'. */
10902 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10904 /* Parse the string-literal message. */
10905 message = cp_parser_string_literal (parser,
10906 /*translate=*/false,
10909 /* A `)' completes the static assertion. */
10910 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10911 cp_parser_skip_to_closing_parenthesis (parser,
10912 /*recovering=*/true,
10913 /*or_comma=*/false,
10914 /*consume_paren=*/true);
10916 /* A semicolon terminates the declaration. */
10917 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10919 /* Complete the static assertion, which may mean either processing
10920 the static assert now or saving it for template instantiation. */
10921 finish_static_assert (condition, message, saved_loc, member_p);
10924 /* Parse a `decltype' type. Returns the type.
10926 simple-type-specifier:
10927 decltype ( expression ) */
10930 cp_parser_decltype (cp_parser *parser)
10933 bool id_expression_or_member_access_p = false;
10934 const char *saved_message;
10935 bool saved_integral_constant_expression_p;
10936 bool saved_non_integral_constant_expression_p;
10937 cp_token *id_expr_start_token;
10938 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10940 if (start_token->type == CPP_DECLTYPE)
10942 /* Already parsed. */
10943 cp_lexer_consume_token (parser->lexer);
10944 return start_token->u.value;
10947 /* Look for the `decltype' token. */
10948 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10949 return error_mark_node;
10951 /* Types cannot be defined in a `decltype' expression. Save away the
10953 saved_message = parser->type_definition_forbidden_message;
10955 /* And create the new one. */
10956 parser->type_definition_forbidden_message
10957 = G_("types may not be defined in %<decltype%> expressions");
10959 /* The restrictions on constant-expressions do not apply inside
10960 decltype expressions. */
10961 saved_integral_constant_expression_p
10962 = parser->integral_constant_expression_p;
10963 saved_non_integral_constant_expression_p
10964 = parser->non_integral_constant_expression_p;
10965 parser->integral_constant_expression_p = false;
10967 /* Do not actually evaluate the expression. */
10968 ++cp_unevaluated_operand;
10970 /* Do not warn about problems with the expression. */
10971 ++c_inhibit_evaluation_warnings;
10973 /* Parse the opening `('. */
10974 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10975 return error_mark_node;
10977 /* First, try parsing an id-expression. */
10978 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10979 cp_parser_parse_tentatively (parser);
10980 expr = cp_parser_id_expression (parser,
10981 /*template_keyword_p=*/false,
10982 /*check_dependency_p=*/true,
10983 /*template_p=*/NULL,
10984 /*declarator_p=*/false,
10985 /*optional_p=*/false);
10987 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10989 bool non_integral_constant_expression_p = false;
10990 tree id_expression = expr;
10992 const char *error_msg;
10994 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10995 /* Lookup the name we got back from the id-expression. */
10996 expr = cp_parser_lookup_name (parser, expr,
10998 /*is_template=*/false,
10999 /*is_namespace=*/false,
11000 /*check_dependency=*/true,
11001 /*ambiguous_decls=*/NULL,
11002 id_expr_start_token->location);
11005 && expr != error_mark_node
11006 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11007 && TREE_CODE (expr) != TYPE_DECL
11008 && (TREE_CODE (expr) != BIT_NOT_EXPR
11009 || !TYPE_P (TREE_OPERAND (expr, 0)))
11010 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11012 /* Complete lookup of the id-expression. */
11013 expr = (finish_id_expression
11014 (id_expression, expr, parser->scope, &idk,
11015 /*integral_constant_expression_p=*/false,
11016 /*allow_non_integral_constant_expression_p=*/true,
11017 &non_integral_constant_expression_p,
11018 /*template_p=*/false,
11020 /*address_p=*/false,
11021 /*template_arg_p=*/false,
11023 id_expr_start_token->location));
11025 if (expr == error_mark_node)
11026 /* We found an id-expression, but it was something that we
11027 should not have found. This is an error, not something
11028 we can recover from, so note that we found an
11029 id-expression and we'll recover as gracefully as
11031 id_expression_or_member_access_p = true;
11035 && expr != error_mark_node
11036 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11037 /* We have an id-expression. */
11038 id_expression_or_member_access_p = true;
11041 if (!id_expression_or_member_access_p)
11043 /* Abort the id-expression parse. */
11044 cp_parser_abort_tentative_parse (parser);
11046 /* Parsing tentatively, again. */
11047 cp_parser_parse_tentatively (parser);
11049 /* Parse a class member access. */
11050 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11052 /*member_access_only_p=*/true, NULL);
11055 && expr != error_mark_node
11056 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11057 /* We have an id-expression. */
11058 id_expression_or_member_access_p = true;
11061 if (id_expression_or_member_access_p)
11062 /* We have parsed the complete id-expression or member access. */
11063 cp_parser_parse_definitely (parser);
11066 bool saved_greater_than_is_operator_p;
11068 /* Abort our attempt to parse an id-expression or member access
11070 cp_parser_abort_tentative_parse (parser);
11072 /* Within a parenthesized expression, a `>' token is always
11073 the greater-than operator. */
11074 saved_greater_than_is_operator_p
11075 = parser->greater_than_is_operator_p;
11076 parser->greater_than_is_operator_p = true;
11078 /* Parse a full expression. */
11079 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11081 /* The `>' token might be the end of a template-id or
11082 template-parameter-list now. */
11083 parser->greater_than_is_operator_p
11084 = saved_greater_than_is_operator_p;
11087 /* Go back to evaluating expressions. */
11088 --cp_unevaluated_operand;
11089 --c_inhibit_evaluation_warnings;
11091 /* Restore the old message and the integral constant expression
11093 parser->type_definition_forbidden_message = saved_message;
11094 parser->integral_constant_expression_p
11095 = saved_integral_constant_expression_p;
11096 parser->non_integral_constant_expression_p
11097 = saved_non_integral_constant_expression_p;
11099 /* Parse to the closing `)'. */
11100 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11102 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11103 /*consume_paren=*/true);
11104 return error_mark_node;
11107 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11108 tf_warning_or_error);
11110 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11112 start_token->type = CPP_DECLTYPE;
11113 start_token->u.value = expr;
11114 start_token->keyword = RID_MAX;
11115 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11120 /* Special member functions [gram.special] */
11122 /* Parse a conversion-function-id.
11124 conversion-function-id:
11125 operator conversion-type-id
11127 Returns an IDENTIFIER_NODE representing the operator. */
11130 cp_parser_conversion_function_id (cp_parser* parser)
11134 tree saved_qualifying_scope;
11135 tree saved_object_scope;
11136 tree pushed_scope = NULL_TREE;
11138 /* Look for the `operator' token. */
11139 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11140 return error_mark_node;
11141 /* When we parse the conversion-type-id, the current scope will be
11142 reset. However, we need that information in able to look up the
11143 conversion function later, so we save it here. */
11144 saved_scope = parser->scope;
11145 saved_qualifying_scope = parser->qualifying_scope;
11146 saved_object_scope = parser->object_scope;
11147 /* We must enter the scope of the class so that the names of
11148 entities declared within the class are available in the
11149 conversion-type-id. For example, consider:
11156 S::operator I() { ... }
11158 In order to see that `I' is a type-name in the definition, we
11159 must be in the scope of `S'. */
11161 pushed_scope = push_scope (saved_scope);
11162 /* Parse the conversion-type-id. */
11163 type = cp_parser_conversion_type_id (parser);
11164 /* Leave the scope of the class, if any. */
11166 pop_scope (pushed_scope);
11167 /* Restore the saved scope. */
11168 parser->scope = saved_scope;
11169 parser->qualifying_scope = saved_qualifying_scope;
11170 parser->object_scope = saved_object_scope;
11171 /* If the TYPE is invalid, indicate failure. */
11172 if (type == error_mark_node)
11173 return error_mark_node;
11174 return mangle_conv_op_name_for_type (type);
11177 /* Parse a conversion-type-id:
11179 conversion-type-id:
11180 type-specifier-seq conversion-declarator [opt]
11182 Returns the TYPE specified. */
11185 cp_parser_conversion_type_id (cp_parser* parser)
11188 cp_decl_specifier_seq type_specifiers;
11189 cp_declarator *declarator;
11190 tree type_specified;
11192 /* Parse the attributes. */
11193 attributes = cp_parser_attributes_opt (parser);
11194 /* Parse the type-specifiers. */
11195 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11196 /*is_trailing_return=*/false,
11198 /* If that didn't work, stop. */
11199 if (type_specifiers.type == error_mark_node)
11200 return error_mark_node;
11201 /* Parse the conversion-declarator. */
11202 declarator = cp_parser_conversion_declarator_opt (parser);
11204 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11205 /*initialized=*/0, &attributes);
11207 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11209 /* Don't give this error when parsing tentatively. This happens to
11210 work because we always parse this definitively once. */
11211 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11212 && type_uses_auto (type_specified))
11214 error ("invalid use of %<auto%> in conversion operator");
11215 return error_mark_node;
11218 return type_specified;
11221 /* Parse an (optional) conversion-declarator.
11223 conversion-declarator:
11224 ptr-operator conversion-declarator [opt]
11228 static cp_declarator *
11229 cp_parser_conversion_declarator_opt (cp_parser* parser)
11231 enum tree_code code;
11233 cp_cv_quals cv_quals;
11235 /* We don't know if there's a ptr-operator next, or not. */
11236 cp_parser_parse_tentatively (parser);
11237 /* Try the ptr-operator. */
11238 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11239 /* If it worked, look for more conversion-declarators. */
11240 if (cp_parser_parse_definitely (parser))
11242 cp_declarator *declarator;
11244 /* Parse another optional declarator. */
11245 declarator = cp_parser_conversion_declarator_opt (parser);
11247 return cp_parser_make_indirect_declarator
11248 (code, class_type, cv_quals, declarator);
11254 /* Parse an (optional) ctor-initializer.
11257 : mem-initializer-list
11259 Returns TRUE iff the ctor-initializer was actually present. */
11262 cp_parser_ctor_initializer_opt (cp_parser* parser)
11264 /* If the next token is not a `:', then there is no
11265 ctor-initializer. */
11266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11268 /* Do default initialization of any bases and members. */
11269 if (DECL_CONSTRUCTOR_P (current_function_decl))
11270 finish_mem_initializers (NULL_TREE);
11275 /* Consume the `:' token. */
11276 cp_lexer_consume_token (parser->lexer);
11277 /* And the mem-initializer-list. */
11278 cp_parser_mem_initializer_list (parser);
11283 /* Parse a mem-initializer-list.
11285 mem-initializer-list:
11286 mem-initializer ... [opt]
11287 mem-initializer ... [opt] , mem-initializer-list */
11290 cp_parser_mem_initializer_list (cp_parser* parser)
11292 tree mem_initializer_list = NULL_TREE;
11293 cp_token *token = cp_lexer_peek_token (parser->lexer);
11295 /* Let the semantic analysis code know that we are starting the
11296 mem-initializer-list. */
11297 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11298 error_at (token->location,
11299 "only constructors take member initializers");
11301 /* Loop through the list. */
11304 tree mem_initializer;
11306 token = cp_lexer_peek_token (parser->lexer);
11307 /* Parse the mem-initializer. */
11308 mem_initializer = cp_parser_mem_initializer (parser);
11309 /* If the next token is a `...', we're expanding member initializers. */
11310 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11312 /* Consume the `...'. */
11313 cp_lexer_consume_token (parser->lexer);
11315 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11316 can be expanded but members cannot. */
11317 if (mem_initializer != error_mark_node
11318 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11320 error_at (token->location,
11321 "cannot expand initializer for member %<%D%>",
11322 TREE_PURPOSE (mem_initializer));
11323 mem_initializer = error_mark_node;
11326 /* Construct the pack expansion type. */
11327 if (mem_initializer != error_mark_node)
11328 mem_initializer = make_pack_expansion (mem_initializer);
11330 /* Add it to the list, unless it was erroneous. */
11331 if (mem_initializer != error_mark_node)
11333 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11334 mem_initializer_list = mem_initializer;
11336 /* If the next token is not a `,', we're done. */
11337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11339 /* Consume the `,' token. */
11340 cp_lexer_consume_token (parser->lexer);
11343 /* Perform semantic analysis. */
11344 if (DECL_CONSTRUCTOR_P (current_function_decl))
11345 finish_mem_initializers (mem_initializer_list);
11348 /* Parse a mem-initializer.
11351 mem-initializer-id ( expression-list [opt] )
11352 mem-initializer-id braced-init-list
11357 ( expression-list [opt] )
11359 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11360 class) or FIELD_DECL (for a non-static data member) to initialize;
11361 the TREE_VALUE is the expression-list. An empty initialization
11362 list is represented by void_list_node. */
11365 cp_parser_mem_initializer (cp_parser* parser)
11367 tree mem_initializer_id;
11368 tree expression_list;
11370 cp_token *token = cp_lexer_peek_token (parser->lexer);
11372 /* Find out what is being initialized. */
11373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11375 permerror (token->location,
11376 "anachronistic old-style base class initializer");
11377 mem_initializer_id = NULL_TREE;
11381 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11382 if (mem_initializer_id == error_mark_node)
11383 return mem_initializer_id;
11385 member = expand_member_init (mem_initializer_id);
11386 if (member && !DECL_P (member))
11387 in_base_initializer = 1;
11389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11391 bool expr_non_constant_p;
11392 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11393 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11394 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11395 expression_list = build_tree_list (NULL_TREE, expression_list);
11400 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11402 /*allow_expansion_p=*/true,
11403 /*non_constant_p=*/NULL);
11405 return error_mark_node;
11406 expression_list = build_tree_list_vec (vec);
11407 release_tree_vector (vec);
11410 if (expression_list == error_mark_node)
11411 return error_mark_node;
11412 if (!expression_list)
11413 expression_list = void_type_node;
11415 in_base_initializer = 0;
11417 return member ? build_tree_list (member, expression_list) : error_mark_node;
11420 /* Parse a mem-initializer-id.
11422 mem-initializer-id:
11423 :: [opt] nested-name-specifier [opt] class-name
11426 Returns a TYPE indicating the class to be initializer for the first
11427 production. Returns an IDENTIFIER_NODE indicating the data member
11428 to be initialized for the second production. */
11431 cp_parser_mem_initializer_id (cp_parser* parser)
11433 bool global_scope_p;
11434 bool nested_name_specifier_p;
11435 bool template_p = false;
11438 cp_token *token = cp_lexer_peek_token (parser->lexer);
11440 /* `typename' is not allowed in this context ([temp.res]). */
11441 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11443 error_at (token->location,
11444 "keyword %<typename%> not allowed in this context (a qualified "
11445 "member initializer is implicitly a type)");
11446 cp_lexer_consume_token (parser->lexer);
11448 /* Look for the optional `::' operator. */
11450 = (cp_parser_global_scope_opt (parser,
11451 /*current_scope_valid_p=*/false)
11453 /* Look for the optional nested-name-specifier. The simplest way to
11458 The keyword `typename' is not permitted in a base-specifier or
11459 mem-initializer; in these contexts a qualified name that
11460 depends on a template-parameter is implicitly assumed to be a
11463 is to assume that we have seen the `typename' keyword at this
11465 nested_name_specifier_p
11466 = (cp_parser_nested_name_specifier_opt (parser,
11467 /*typename_keyword_p=*/true,
11468 /*check_dependency_p=*/true,
11470 /*is_declaration=*/true)
11472 if (nested_name_specifier_p)
11473 template_p = cp_parser_optional_template_keyword (parser);
11474 /* If there is a `::' operator or a nested-name-specifier, then we
11475 are definitely looking for a class-name. */
11476 if (global_scope_p || nested_name_specifier_p)
11477 return cp_parser_class_name (parser,
11478 /*typename_keyword_p=*/true,
11479 /*template_keyword_p=*/template_p,
11481 /*check_dependency_p=*/true,
11482 /*class_head_p=*/false,
11483 /*is_declaration=*/true);
11484 /* Otherwise, we could also be looking for an ordinary identifier. */
11485 cp_parser_parse_tentatively (parser);
11486 /* Try a class-name. */
11487 id = cp_parser_class_name (parser,
11488 /*typename_keyword_p=*/true,
11489 /*template_keyword_p=*/false,
11491 /*check_dependency_p=*/true,
11492 /*class_head_p=*/false,
11493 /*is_declaration=*/true);
11494 /* If we found one, we're done. */
11495 if (cp_parser_parse_definitely (parser))
11497 /* Otherwise, look for an ordinary identifier. */
11498 return cp_parser_identifier (parser);
11501 /* Overloading [gram.over] */
11503 /* Parse an operator-function-id.
11505 operator-function-id:
11508 Returns an IDENTIFIER_NODE for the operator which is a
11509 human-readable spelling of the identifier, e.g., `operator +'. */
11512 cp_parser_operator_function_id (cp_parser* parser)
11514 /* Look for the `operator' keyword. */
11515 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11516 return error_mark_node;
11517 /* And then the name of the operator itself. */
11518 return cp_parser_operator (parser);
11521 /* Return an identifier node for a user-defined literal operator.
11522 The suffix identifier is chained to the operator name identifier. */
11525 cp_literal_operator_id (const char* name)
11528 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11529 + strlen (name) + 10);
11530 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11531 identifier = get_identifier (buffer);
11532 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11537 /* Parse an operator.
11540 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11541 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11542 || ++ -- , ->* -> () []
11549 Returns an IDENTIFIER_NODE for the operator which is a
11550 human-readable spelling of the identifier, e.g., `operator +'. */
11553 cp_parser_operator (cp_parser* parser)
11555 tree id = NULL_TREE;
11558 /* Peek at the next token. */
11559 token = cp_lexer_peek_token (parser->lexer);
11560 /* Figure out which operator we have. */
11561 switch (token->type)
11567 /* The keyword should be either `new' or `delete'. */
11568 if (token->keyword == RID_NEW)
11570 else if (token->keyword == RID_DELETE)
11575 /* Consume the `new' or `delete' token. */
11576 cp_lexer_consume_token (parser->lexer);
11578 /* Peek at the next token. */
11579 token = cp_lexer_peek_token (parser->lexer);
11580 /* If it's a `[' token then this is the array variant of the
11582 if (token->type == CPP_OPEN_SQUARE)
11584 /* Consume the `[' token. */
11585 cp_lexer_consume_token (parser->lexer);
11586 /* Look for the `]' token. */
11587 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11588 id = ansi_opname (op == NEW_EXPR
11589 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11591 /* Otherwise, we have the non-array variant. */
11593 id = ansi_opname (op);
11599 id = ansi_opname (PLUS_EXPR);
11603 id = ansi_opname (MINUS_EXPR);
11607 id = ansi_opname (MULT_EXPR);
11611 id = ansi_opname (TRUNC_DIV_EXPR);
11615 id = ansi_opname (TRUNC_MOD_EXPR);
11619 id = ansi_opname (BIT_XOR_EXPR);
11623 id = ansi_opname (BIT_AND_EXPR);
11627 id = ansi_opname (BIT_IOR_EXPR);
11631 id = ansi_opname (BIT_NOT_EXPR);
11635 id = ansi_opname (TRUTH_NOT_EXPR);
11639 id = ansi_assopname (NOP_EXPR);
11643 id = ansi_opname (LT_EXPR);
11647 id = ansi_opname (GT_EXPR);
11651 id = ansi_assopname (PLUS_EXPR);
11655 id = ansi_assopname (MINUS_EXPR);
11659 id = ansi_assopname (MULT_EXPR);
11663 id = ansi_assopname (TRUNC_DIV_EXPR);
11667 id = ansi_assopname (TRUNC_MOD_EXPR);
11671 id = ansi_assopname (BIT_XOR_EXPR);
11675 id = ansi_assopname (BIT_AND_EXPR);
11679 id = ansi_assopname (BIT_IOR_EXPR);
11683 id = ansi_opname (LSHIFT_EXPR);
11687 id = ansi_opname (RSHIFT_EXPR);
11690 case CPP_LSHIFT_EQ:
11691 id = ansi_assopname (LSHIFT_EXPR);
11694 case CPP_RSHIFT_EQ:
11695 id = ansi_assopname (RSHIFT_EXPR);
11699 id = ansi_opname (EQ_EXPR);
11703 id = ansi_opname (NE_EXPR);
11707 id = ansi_opname (LE_EXPR);
11710 case CPP_GREATER_EQ:
11711 id = ansi_opname (GE_EXPR);
11715 id = ansi_opname (TRUTH_ANDIF_EXPR);
11719 id = ansi_opname (TRUTH_ORIF_EXPR);
11722 case CPP_PLUS_PLUS:
11723 id = ansi_opname (POSTINCREMENT_EXPR);
11726 case CPP_MINUS_MINUS:
11727 id = ansi_opname (PREDECREMENT_EXPR);
11731 id = ansi_opname (COMPOUND_EXPR);
11734 case CPP_DEREF_STAR:
11735 id = ansi_opname (MEMBER_REF);
11739 id = ansi_opname (COMPONENT_REF);
11742 case CPP_OPEN_PAREN:
11743 /* Consume the `('. */
11744 cp_lexer_consume_token (parser->lexer);
11745 /* Look for the matching `)'. */
11746 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11747 return ansi_opname (CALL_EXPR);
11749 case CPP_OPEN_SQUARE:
11750 /* Consume the `['. */
11751 cp_lexer_consume_token (parser->lexer);
11752 /* Look for the matching `]'. */
11753 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11754 return ansi_opname (ARRAY_REF);
11757 if (cxx_dialect == cxx98)
11758 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11759 if (TREE_STRING_LENGTH (token->u.value) > 2)
11761 error ("expected empty string after %<operator%> keyword");
11762 return error_mark_node;
11764 /* Consume the string. */
11765 cp_lexer_consume_token (parser->lexer);
11766 /* Look for the suffix identifier. */
11767 token = cp_lexer_peek_token (parser->lexer);
11768 if (token->type == CPP_NAME)
11770 id = cp_parser_identifier (parser);
11771 if (id != error_mark_node)
11773 const char *name = IDENTIFIER_POINTER (id);
11774 return cp_literal_operator_id (name);
11779 error ("expected suffix identifier");
11780 return error_mark_node;
11783 case CPP_STRING_USERDEF:
11784 error ("missing space between %<\"\"%> and suffix identifier");
11785 return error_mark_node;
11788 /* Anything else is an error. */
11792 /* If we have selected an identifier, we need to consume the
11795 cp_lexer_consume_token (parser->lexer);
11796 /* Otherwise, no valid operator name was present. */
11799 cp_parser_error (parser, "expected operator");
11800 id = error_mark_node;
11806 /* Parse a template-declaration.
11808 template-declaration:
11809 export [opt] template < template-parameter-list > declaration
11811 If MEMBER_P is TRUE, this template-declaration occurs within a
11814 The grammar rule given by the standard isn't correct. What
11815 is really meant is:
11817 template-declaration:
11818 export [opt] template-parameter-list-seq
11819 decl-specifier-seq [opt] init-declarator [opt] ;
11820 export [opt] template-parameter-list-seq
11821 function-definition
11823 template-parameter-list-seq:
11824 template-parameter-list-seq [opt]
11825 template < template-parameter-list > */
11828 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11830 /* Check for `export'. */
11831 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11833 /* Consume the `export' token. */
11834 cp_lexer_consume_token (parser->lexer);
11835 /* Warn that we do not support `export'. */
11836 warning (0, "keyword %<export%> not implemented, and will be ignored");
11839 cp_parser_template_declaration_after_export (parser, member_p);
11842 /* Parse a template-parameter-list.
11844 template-parameter-list:
11846 template-parameter-list , template-parameter
11848 Returns a TREE_LIST. Each node represents a template parameter.
11849 The nodes are connected via their TREE_CHAINs. */
11852 cp_parser_template_parameter_list (cp_parser* parser)
11854 tree parameter_list = NULL_TREE;
11856 begin_template_parm_list ();
11858 /* The loop below parses the template parms. We first need to know
11859 the total number of template parms to be able to compute proper
11860 canonical types of each dependent type. So after the loop, when
11861 we know the total number of template parms,
11862 end_template_parm_list computes the proper canonical types and
11863 fixes up the dependent types accordingly. */
11868 bool is_parameter_pack;
11869 location_t parm_loc;
11871 /* Parse the template-parameter. */
11872 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11873 parameter = cp_parser_template_parameter (parser,
11875 &is_parameter_pack);
11876 /* Add it to the list. */
11877 if (parameter != error_mark_node)
11878 parameter_list = process_template_parm (parameter_list,
11886 tree err_parm = build_tree_list (parameter, parameter);
11887 parameter_list = chainon (parameter_list, err_parm);
11890 /* If the next token is not a `,', we're done. */
11891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11893 /* Otherwise, consume the `,' token. */
11894 cp_lexer_consume_token (parser->lexer);
11897 return end_template_parm_list (parameter_list);
11900 /* Parse a template-parameter.
11902 template-parameter:
11904 parameter-declaration
11906 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11907 the parameter. The TREE_PURPOSE is the default value, if any.
11908 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11909 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11910 set to true iff this parameter is a parameter pack. */
11913 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11914 bool *is_parameter_pack)
11917 cp_parameter_declarator *parameter_declarator;
11918 cp_declarator *id_declarator;
11921 /* Assume it is a type parameter or a template parameter. */
11922 *is_non_type = false;
11923 /* Assume it not a parameter pack. */
11924 *is_parameter_pack = false;
11925 /* Peek at the next token. */
11926 token = cp_lexer_peek_token (parser->lexer);
11927 /* If it is `class' or `template', we have a type-parameter. */
11928 if (token->keyword == RID_TEMPLATE)
11929 return cp_parser_type_parameter (parser, is_parameter_pack);
11930 /* If it is `class' or `typename' we do not know yet whether it is a
11931 type parameter or a non-type parameter. Consider:
11933 template <typename T, typename T::X X> ...
11937 template <class C, class D*> ...
11939 Here, the first parameter is a type parameter, and the second is
11940 a non-type parameter. We can tell by looking at the token after
11941 the identifier -- if it is a `,', `=', or `>' then we have a type
11943 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11945 /* Peek at the token after `class' or `typename'. */
11946 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11947 /* If it's an ellipsis, we have a template type parameter
11949 if (token->type == CPP_ELLIPSIS)
11950 return cp_parser_type_parameter (parser, is_parameter_pack);
11951 /* If it's an identifier, skip it. */
11952 if (token->type == CPP_NAME)
11953 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11954 /* Now, see if the token looks like the end of a template
11956 if (token->type == CPP_COMMA
11957 || token->type == CPP_EQ
11958 || token->type == CPP_GREATER)
11959 return cp_parser_type_parameter (parser, is_parameter_pack);
11962 /* Otherwise, it is a non-type parameter.
11966 When parsing a default template-argument for a non-type
11967 template-parameter, the first non-nested `>' is taken as the end
11968 of the template parameter-list rather than a greater-than
11970 *is_non_type = true;
11971 parameter_declarator
11972 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11973 /*parenthesized_p=*/NULL);
11975 /* If the parameter declaration is marked as a parameter pack, set
11976 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11977 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11979 if (parameter_declarator
11980 && parameter_declarator->declarator
11981 && parameter_declarator->declarator->parameter_pack_p)
11983 *is_parameter_pack = true;
11984 parameter_declarator->declarator->parameter_pack_p = false;
11987 /* If the next token is an ellipsis, and we don't already have it
11988 marked as a parameter pack, then we have a parameter pack (that
11989 has no declarator). */
11990 if (!*is_parameter_pack
11991 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11992 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11994 /* Consume the `...'. */
11995 cp_lexer_consume_token (parser->lexer);
11996 maybe_warn_variadic_templates ();
11998 *is_parameter_pack = true;
12000 /* We might end up with a pack expansion as the type of the non-type
12001 template parameter, in which case this is a non-type template
12003 else if (parameter_declarator
12004 && parameter_declarator->decl_specifiers.type
12005 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12007 *is_parameter_pack = true;
12008 parameter_declarator->decl_specifiers.type =
12009 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12012 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12014 /* Parameter packs cannot have default arguments. However, a
12015 user may try to do so, so we'll parse them and give an
12016 appropriate diagnostic here. */
12018 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12020 /* Find the name of the parameter pack. */
12021 id_declarator = parameter_declarator->declarator;
12022 while (id_declarator && id_declarator->kind != cdk_id)
12023 id_declarator = id_declarator->declarator;
12025 if (id_declarator && id_declarator->kind == cdk_id)
12026 error_at (start_token->location,
12027 "template parameter pack %qD cannot have a default argument",
12028 id_declarator->u.id.unqualified_name);
12030 error_at (start_token->location,
12031 "template parameter pack cannot have a default argument");
12033 /* Parse the default argument, but throw away the result. */
12034 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12037 parm = grokdeclarator (parameter_declarator->declarator,
12038 ¶meter_declarator->decl_specifiers,
12039 TPARM, /*initialized=*/0,
12040 /*attrlist=*/NULL);
12041 if (parm == error_mark_node)
12042 return error_mark_node;
12044 return build_tree_list (parameter_declarator->default_argument, parm);
12047 /* Parse a type-parameter.
12050 class identifier [opt]
12051 class identifier [opt] = type-id
12052 typename identifier [opt]
12053 typename identifier [opt] = type-id
12054 template < template-parameter-list > class identifier [opt]
12055 template < template-parameter-list > class identifier [opt]
12058 GNU Extension (variadic templates):
12061 class ... identifier [opt]
12062 typename ... identifier [opt]
12064 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12065 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12066 the declaration of the parameter.
12068 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12071 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12076 /* Look for a keyword to tell us what kind of parameter this is. */
12077 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12079 return error_mark_node;
12081 switch (token->keyword)
12087 tree default_argument;
12089 /* If the next token is an ellipsis, we have a template
12091 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12093 /* Consume the `...' token. */
12094 cp_lexer_consume_token (parser->lexer);
12095 maybe_warn_variadic_templates ();
12097 *is_parameter_pack = true;
12100 /* If the next token is an identifier, then it names the
12102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12103 identifier = cp_parser_identifier (parser);
12105 identifier = NULL_TREE;
12107 /* Create the parameter. */
12108 parameter = finish_template_type_parm (class_type_node, identifier);
12110 /* If the next token is an `=', we have a default argument. */
12111 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12113 /* Consume the `=' token. */
12114 cp_lexer_consume_token (parser->lexer);
12115 /* Parse the default-argument. */
12116 push_deferring_access_checks (dk_no_deferred);
12117 default_argument = cp_parser_type_id (parser);
12119 /* Template parameter packs cannot have default
12121 if (*is_parameter_pack)
12124 error_at (token->location,
12125 "template parameter pack %qD cannot have a "
12126 "default argument", identifier);
12128 error_at (token->location,
12129 "template parameter packs cannot have "
12130 "default arguments");
12131 default_argument = NULL_TREE;
12133 pop_deferring_access_checks ();
12136 default_argument = NULL_TREE;
12138 /* Create the combined representation of the parameter and the
12139 default argument. */
12140 parameter = build_tree_list (default_argument, parameter);
12147 tree default_argument;
12149 /* Look for the `<'. */
12150 cp_parser_require (parser, CPP_LESS, RT_LESS);
12151 /* Parse the template-parameter-list. */
12152 cp_parser_template_parameter_list (parser);
12153 /* Look for the `>'. */
12154 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12155 /* Look for the `class' keyword. */
12156 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12157 /* If the next token is an ellipsis, we have a template
12159 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12161 /* Consume the `...' token. */
12162 cp_lexer_consume_token (parser->lexer);
12163 maybe_warn_variadic_templates ();
12165 *is_parameter_pack = true;
12167 /* If the next token is an `=', then there is a
12168 default-argument. If the next token is a `>', we are at
12169 the end of the parameter-list. If the next token is a `,',
12170 then we are at the end of this parameter. */
12171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12172 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12173 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12175 identifier = cp_parser_identifier (parser);
12176 /* Treat invalid names as if the parameter were nameless. */
12177 if (identifier == error_mark_node)
12178 identifier = NULL_TREE;
12181 identifier = NULL_TREE;
12183 /* Create the template parameter. */
12184 parameter = finish_template_template_parm (class_type_node,
12187 /* If the next token is an `=', then there is a
12188 default-argument. */
12189 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12193 /* Consume the `='. */
12194 cp_lexer_consume_token (parser->lexer);
12195 /* Parse the id-expression. */
12196 push_deferring_access_checks (dk_no_deferred);
12197 /* save token before parsing the id-expression, for error
12199 token = cp_lexer_peek_token (parser->lexer);
12201 = cp_parser_id_expression (parser,
12202 /*template_keyword_p=*/false,
12203 /*check_dependency_p=*/true,
12204 /*template_p=*/&is_template,
12205 /*declarator_p=*/false,
12206 /*optional_p=*/false);
12207 if (TREE_CODE (default_argument) == TYPE_DECL)
12208 /* If the id-expression was a template-id that refers to
12209 a template-class, we already have the declaration here,
12210 so no further lookup is needed. */
12213 /* Look up the name. */
12215 = cp_parser_lookup_name (parser, default_argument,
12217 /*is_template=*/is_template,
12218 /*is_namespace=*/false,
12219 /*check_dependency=*/true,
12220 /*ambiguous_decls=*/NULL,
12222 /* See if the default argument is valid. */
12224 = check_template_template_default_arg (default_argument);
12226 /* Template parameter packs cannot have default
12228 if (*is_parameter_pack)
12231 error_at (token->location,
12232 "template parameter pack %qD cannot "
12233 "have a default argument",
12236 error_at (token->location, "template parameter packs cannot "
12237 "have default arguments");
12238 default_argument = NULL_TREE;
12240 pop_deferring_access_checks ();
12243 default_argument = NULL_TREE;
12245 /* Create the combined representation of the parameter and the
12246 default argument. */
12247 parameter = build_tree_list (default_argument, parameter);
12252 gcc_unreachable ();
12259 /* Parse a template-id.
12262 template-name < template-argument-list [opt] >
12264 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12265 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12266 returned. Otherwise, if the template-name names a function, or set
12267 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12268 names a class, returns a TYPE_DECL for the specialization.
12270 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12271 uninstantiated templates. */
12274 cp_parser_template_id (cp_parser *parser,
12275 bool template_keyword_p,
12276 bool check_dependency_p,
12277 bool is_declaration)
12283 cp_token_position start_of_id = 0;
12284 deferred_access_check *chk;
12285 VEC (deferred_access_check,gc) *access_check;
12286 cp_token *next_token = NULL, *next_token_2 = NULL;
12287 bool is_identifier;
12289 /* If the next token corresponds to a template-id, there is no need
12291 next_token = cp_lexer_peek_token (parser->lexer);
12292 if (next_token->type == CPP_TEMPLATE_ID)
12294 struct tree_check *check_value;
12296 /* Get the stored value. */
12297 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12298 /* Perform any access checks that were deferred. */
12299 access_check = check_value->checks;
12302 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12303 perform_or_defer_access_check (chk->binfo,
12307 /* Return the stored value. */
12308 return check_value->value;
12311 /* Avoid performing name lookup if there is no possibility of
12312 finding a template-id. */
12313 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12314 || (next_token->type == CPP_NAME
12315 && !cp_parser_nth_token_starts_template_argument_list_p
12318 cp_parser_error (parser, "expected template-id");
12319 return error_mark_node;
12322 /* Remember where the template-id starts. */
12323 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12324 start_of_id = cp_lexer_token_position (parser->lexer, false);
12326 push_deferring_access_checks (dk_deferred);
12328 /* Parse the template-name. */
12329 is_identifier = false;
12330 templ = cp_parser_template_name (parser, template_keyword_p,
12331 check_dependency_p,
12334 if (templ == error_mark_node || is_identifier)
12336 pop_deferring_access_checks ();
12340 /* If we find the sequence `[:' after a template-name, it's probably
12341 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12342 parse correctly the argument list. */
12343 next_token = cp_lexer_peek_token (parser->lexer);
12344 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12345 if (next_token->type == CPP_OPEN_SQUARE
12346 && next_token->flags & DIGRAPH
12347 && next_token_2->type == CPP_COLON
12348 && !(next_token_2->flags & PREV_WHITE))
12350 cp_parser_parse_tentatively (parser);
12351 /* Change `:' into `::'. */
12352 next_token_2->type = CPP_SCOPE;
12353 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12355 cp_lexer_consume_token (parser->lexer);
12357 /* Parse the arguments. */
12358 arguments = cp_parser_enclosed_template_argument_list (parser);
12359 if (!cp_parser_parse_definitely (parser))
12361 /* If we couldn't parse an argument list, then we revert our changes
12362 and return simply an error. Maybe this is not a template-id
12364 next_token_2->type = CPP_COLON;
12365 cp_parser_error (parser, "expected %<<%>");
12366 pop_deferring_access_checks ();
12367 return error_mark_node;
12369 /* Otherwise, emit an error about the invalid digraph, but continue
12370 parsing because we got our argument list. */
12371 if (permerror (next_token->location,
12372 "%<<::%> cannot begin a template-argument list"))
12374 static bool hint = false;
12375 inform (next_token->location,
12376 "%<<:%> is an alternate spelling for %<[%>."
12377 " Insert whitespace between %<<%> and %<::%>");
12378 if (!hint && !flag_permissive)
12380 inform (next_token->location, "(if you use %<-fpermissive%>"
12381 " G++ will accept your code)");
12388 /* Look for the `<' that starts the template-argument-list. */
12389 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12391 pop_deferring_access_checks ();
12392 return error_mark_node;
12394 /* Parse the arguments. */
12395 arguments = cp_parser_enclosed_template_argument_list (parser);
12398 /* Build a representation of the specialization. */
12399 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12400 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12401 else if (DECL_TYPE_TEMPLATE_P (templ)
12402 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12404 bool entering_scope;
12405 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12406 template (rather than some instantiation thereof) only if
12407 is not nested within some other construct. For example, in
12408 "template <typename T> void f(T) { A<T>::", A<T> is just an
12409 instantiation of A. */
12410 entering_scope = (template_parm_scope_p ()
12411 && cp_lexer_next_token_is (parser->lexer,
12414 = finish_template_type (templ, arguments, entering_scope);
12418 /* If it's not a class-template or a template-template, it should be
12419 a function-template. */
12420 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12421 || TREE_CODE (templ) == OVERLOAD
12422 || BASELINK_P (templ)));
12424 template_id = lookup_template_function (templ, arguments);
12427 /* If parsing tentatively, replace the sequence of tokens that makes
12428 up the template-id with a CPP_TEMPLATE_ID token. That way,
12429 should we re-parse the token stream, we will not have to repeat
12430 the effort required to do the parse, nor will we issue duplicate
12431 error messages about problems during instantiation of the
12435 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12437 /* Reset the contents of the START_OF_ID token. */
12438 token->type = CPP_TEMPLATE_ID;
12439 /* Retrieve any deferred checks. Do not pop this access checks yet
12440 so the memory will not be reclaimed during token replacing below. */
12441 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12442 token->u.tree_check_value->value = template_id;
12443 token->u.tree_check_value->checks = get_deferred_access_checks ();
12444 token->keyword = RID_MAX;
12446 /* Purge all subsequent tokens. */
12447 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12449 /* ??? Can we actually assume that, if template_id ==
12450 error_mark_node, we will have issued a diagnostic to the
12451 user, as opposed to simply marking the tentative parse as
12453 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12454 error_at (token->location, "parse error in template argument list");
12457 pop_deferring_access_checks ();
12458 return template_id;
12461 /* Parse a template-name.
12466 The standard should actually say:
12470 operator-function-id
12472 A defect report has been filed about this issue.
12474 A conversion-function-id cannot be a template name because they cannot
12475 be part of a template-id. In fact, looking at this code:
12477 a.operator K<int>()
12479 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12480 It is impossible to call a templated conversion-function-id with an
12481 explicit argument list, since the only allowed template parameter is
12482 the type to which it is converting.
12484 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12485 `template' keyword, in a construction like:
12489 In that case `f' is taken to be a template-name, even though there
12490 is no way of knowing for sure.
12492 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12493 name refers to a set of overloaded functions, at least one of which
12494 is a template, or an IDENTIFIER_NODE with the name of the template,
12495 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12496 names are looked up inside uninstantiated templates. */
12499 cp_parser_template_name (cp_parser* parser,
12500 bool template_keyword_p,
12501 bool check_dependency_p,
12502 bool is_declaration,
12503 bool *is_identifier)
12508 cp_token *token = cp_lexer_peek_token (parser->lexer);
12510 /* If the next token is `operator', then we have either an
12511 operator-function-id or a conversion-function-id. */
12512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12514 /* We don't know whether we're looking at an
12515 operator-function-id or a conversion-function-id. */
12516 cp_parser_parse_tentatively (parser);
12517 /* Try an operator-function-id. */
12518 identifier = cp_parser_operator_function_id (parser);
12519 /* If that didn't work, try a conversion-function-id. */
12520 if (!cp_parser_parse_definitely (parser))
12522 cp_parser_error (parser, "expected template-name");
12523 return error_mark_node;
12526 /* Look for the identifier. */
12528 identifier = cp_parser_identifier (parser);
12530 /* If we didn't find an identifier, we don't have a template-id. */
12531 if (identifier == error_mark_node)
12532 return error_mark_node;
12534 /* If the name immediately followed the `template' keyword, then it
12535 is a template-name. However, if the next token is not `<', then
12536 we do not treat it as a template-name, since it is not being used
12537 as part of a template-id. This enables us to handle constructs
12540 template <typename T> struct S { S(); };
12541 template <typename T> S<T>::S();
12543 correctly. We would treat `S' as a template -- if it were `S<T>'
12544 -- but we do not if there is no `<'. */
12546 if (processing_template_decl
12547 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12549 /* In a declaration, in a dependent context, we pretend that the
12550 "template" keyword was present in order to improve error
12551 recovery. For example, given:
12553 template <typename T> void f(T::X<int>);
12555 we want to treat "X<int>" as a template-id. */
12557 && !template_keyword_p
12558 && parser->scope && TYPE_P (parser->scope)
12559 && check_dependency_p
12560 && dependent_scope_p (parser->scope)
12561 /* Do not do this for dtors (or ctors), since they never
12562 need the template keyword before their name. */
12563 && !constructor_name_p (identifier, parser->scope))
12565 cp_token_position start = 0;
12567 /* Explain what went wrong. */
12568 error_at (token->location, "non-template %qD used as template",
12570 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12571 parser->scope, identifier);
12572 /* If parsing tentatively, find the location of the "<" token. */
12573 if (cp_parser_simulate_error (parser))
12574 start = cp_lexer_token_position (parser->lexer, true);
12575 /* Parse the template arguments so that we can issue error
12576 messages about them. */
12577 cp_lexer_consume_token (parser->lexer);
12578 cp_parser_enclosed_template_argument_list (parser);
12579 /* Skip tokens until we find a good place from which to
12580 continue parsing. */
12581 cp_parser_skip_to_closing_parenthesis (parser,
12582 /*recovering=*/true,
12584 /*consume_paren=*/false);
12585 /* If parsing tentatively, permanently remove the
12586 template argument list. That will prevent duplicate
12587 error messages from being issued about the missing
12588 "template" keyword. */
12590 cp_lexer_purge_tokens_after (parser->lexer, start);
12592 *is_identifier = true;
12596 /* If the "template" keyword is present, then there is generally
12597 no point in doing name-lookup, so we just return IDENTIFIER.
12598 But, if the qualifying scope is non-dependent then we can
12599 (and must) do name-lookup normally. */
12600 if (template_keyword_p
12602 || (TYPE_P (parser->scope)
12603 && dependent_type_p (parser->scope))))
12607 /* Look up the name. */
12608 decl = cp_parser_lookup_name (parser, identifier,
12610 /*is_template=*/true,
12611 /*is_namespace=*/false,
12612 check_dependency_p,
12613 /*ambiguous_decls=*/NULL,
12616 /* If DECL is a template, then the name was a template-name. */
12617 if (TREE_CODE (decl) == TEMPLATE_DECL)
12621 tree fn = NULL_TREE;
12623 /* The standard does not explicitly indicate whether a name that
12624 names a set of overloaded declarations, some of which are
12625 templates, is a template-name. However, such a name should
12626 be a template-name; otherwise, there is no way to form a
12627 template-id for the overloaded templates. */
12628 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12629 if (TREE_CODE (fns) == OVERLOAD)
12630 for (fn = fns; fn; fn = OVL_NEXT (fn))
12631 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12636 /* The name does not name a template. */
12637 cp_parser_error (parser, "expected template-name");
12638 return error_mark_node;
12642 /* If DECL is dependent, and refers to a function, then just return
12643 its name; we will look it up again during template instantiation. */
12644 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12646 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12647 if (TYPE_P (scope) && dependent_type_p (scope))
12654 /* Parse a template-argument-list.
12656 template-argument-list:
12657 template-argument ... [opt]
12658 template-argument-list , template-argument ... [opt]
12660 Returns a TREE_VEC containing the arguments. */
12663 cp_parser_template_argument_list (cp_parser* parser)
12665 tree fixed_args[10];
12666 unsigned n_args = 0;
12667 unsigned alloced = 10;
12668 tree *arg_ary = fixed_args;
12670 bool saved_in_template_argument_list_p;
12672 bool saved_non_ice_p;
12674 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12675 parser->in_template_argument_list_p = true;
12676 /* Even if the template-id appears in an integral
12677 constant-expression, the contents of the argument list do
12679 saved_ice_p = parser->integral_constant_expression_p;
12680 parser->integral_constant_expression_p = false;
12681 saved_non_ice_p = parser->non_integral_constant_expression_p;
12682 parser->non_integral_constant_expression_p = false;
12684 /* Parse the arguments. */
12690 /* Consume the comma. */
12691 cp_lexer_consume_token (parser->lexer);
12693 /* Parse the template-argument. */
12694 argument = cp_parser_template_argument (parser);
12696 /* If the next token is an ellipsis, we're expanding a template
12698 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12700 if (argument == error_mark_node)
12702 cp_token *token = cp_lexer_peek_token (parser->lexer);
12703 error_at (token->location,
12704 "expected parameter pack before %<...%>");
12706 /* Consume the `...' token. */
12707 cp_lexer_consume_token (parser->lexer);
12709 /* Make the argument into a TYPE_PACK_EXPANSION or
12710 EXPR_PACK_EXPANSION. */
12711 argument = make_pack_expansion (argument);
12714 if (n_args == alloced)
12718 if (arg_ary == fixed_args)
12720 arg_ary = XNEWVEC (tree, alloced);
12721 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12724 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12726 arg_ary[n_args++] = argument;
12728 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12730 vec = make_tree_vec (n_args);
12733 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12735 if (arg_ary != fixed_args)
12737 parser->non_integral_constant_expression_p = saved_non_ice_p;
12738 parser->integral_constant_expression_p = saved_ice_p;
12739 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12740 #ifdef ENABLE_CHECKING
12741 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12746 /* Parse a template-argument.
12749 assignment-expression
12753 The representation is that of an assignment-expression, type-id, or
12754 id-expression -- except that the qualified id-expression is
12755 evaluated, so that the value returned is either a DECL or an
12758 Although the standard says "assignment-expression", it forbids
12759 throw-expressions or assignments in the template argument.
12760 Therefore, we use "conditional-expression" instead. */
12763 cp_parser_template_argument (cp_parser* parser)
12768 bool maybe_type_id = false;
12769 cp_token *token = NULL, *argument_start_token = NULL;
12772 /* There's really no way to know what we're looking at, so we just
12773 try each alternative in order.
12777 In a template-argument, an ambiguity between a type-id and an
12778 expression is resolved to a type-id, regardless of the form of
12779 the corresponding template-parameter.
12781 Therefore, we try a type-id first. */
12782 cp_parser_parse_tentatively (parser);
12783 argument = cp_parser_template_type_arg (parser);
12784 /* If there was no error parsing the type-id but the next token is a
12785 '>>', our behavior depends on which dialect of C++ we're
12786 parsing. In C++98, we probably found a typo for '> >'. But there
12787 are type-id which are also valid expressions. For instance:
12789 struct X { int operator >> (int); };
12790 template <int V> struct Foo {};
12793 Here 'X()' is a valid type-id of a function type, but the user just
12794 wanted to write the expression "X() >> 5". Thus, we remember that we
12795 found a valid type-id, but we still try to parse the argument as an
12796 expression to see what happens.
12798 In C++0x, the '>>' will be considered two separate '>'
12800 if (!cp_parser_error_occurred (parser)
12801 && cxx_dialect == cxx98
12802 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12804 maybe_type_id = true;
12805 cp_parser_abort_tentative_parse (parser);
12809 /* If the next token isn't a `,' or a `>', then this argument wasn't
12810 really finished. This means that the argument is not a valid
12812 if (!cp_parser_next_token_ends_template_argument_p (parser))
12813 cp_parser_error (parser, "expected template-argument");
12814 /* If that worked, we're done. */
12815 if (cp_parser_parse_definitely (parser))
12818 /* We're still not sure what the argument will be. */
12819 cp_parser_parse_tentatively (parser);
12820 /* Try a template. */
12821 argument_start_token = cp_lexer_peek_token (parser->lexer);
12822 argument = cp_parser_id_expression (parser,
12823 /*template_keyword_p=*/false,
12824 /*check_dependency_p=*/true,
12826 /*declarator_p=*/false,
12827 /*optional_p=*/false);
12828 /* If the next token isn't a `,' or a `>', then this argument wasn't
12829 really finished. */
12830 if (!cp_parser_next_token_ends_template_argument_p (parser))
12831 cp_parser_error (parser, "expected template-argument");
12832 if (!cp_parser_error_occurred (parser))
12834 /* Figure out what is being referred to. If the id-expression
12835 was for a class template specialization, then we will have a
12836 TYPE_DECL at this point. There is no need to do name lookup
12837 at this point in that case. */
12838 if (TREE_CODE (argument) != TYPE_DECL)
12839 argument = cp_parser_lookup_name (parser, argument,
12841 /*is_template=*/template_p,
12842 /*is_namespace=*/false,
12843 /*check_dependency=*/true,
12844 /*ambiguous_decls=*/NULL,
12845 argument_start_token->location);
12846 if (TREE_CODE (argument) != TEMPLATE_DECL
12847 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12848 cp_parser_error (parser, "expected template-name");
12850 if (cp_parser_parse_definitely (parser))
12852 /* It must be a non-type argument. There permitted cases are given
12853 in [temp.arg.nontype]:
12855 -- an integral constant-expression of integral or enumeration
12858 -- the name of a non-type template-parameter; or
12860 -- the name of an object or function with external linkage...
12862 -- the address of an object or function with external linkage...
12864 -- a pointer to member... */
12865 /* Look for a non-type template parameter. */
12866 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12868 cp_parser_parse_tentatively (parser);
12869 argument = cp_parser_primary_expression (parser,
12870 /*address_p=*/false,
12872 /*template_arg_p=*/true,
12874 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12875 || !cp_parser_next_token_ends_template_argument_p (parser))
12876 cp_parser_simulate_error (parser);
12877 if (cp_parser_parse_definitely (parser))
12881 /* If the next token is "&", the argument must be the address of an
12882 object or function with external linkage. */
12883 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12885 cp_lexer_consume_token (parser->lexer);
12886 /* See if we might have an id-expression. */
12887 token = cp_lexer_peek_token (parser->lexer);
12888 if (token->type == CPP_NAME
12889 || token->keyword == RID_OPERATOR
12890 || token->type == CPP_SCOPE
12891 || token->type == CPP_TEMPLATE_ID
12892 || token->type == CPP_NESTED_NAME_SPECIFIER)
12894 cp_parser_parse_tentatively (parser);
12895 argument = cp_parser_primary_expression (parser,
12898 /*template_arg_p=*/true,
12900 if (cp_parser_error_occurred (parser)
12901 || !cp_parser_next_token_ends_template_argument_p (parser))
12902 cp_parser_abort_tentative_parse (parser);
12907 if (TREE_CODE (argument) == INDIRECT_REF)
12909 gcc_assert (REFERENCE_REF_P (argument));
12910 argument = TREE_OPERAND (argument, 0);
12913 /* If we're in a template, we represent a qualified-id referring
12914 to a static data member as a SCOPE_REF even if the scope isn't
12915 dependent so that we can check access control later. */
12917 if (TREE_CODE (probe) == SCOPE_REF)
12918 probe = TREE_OPERAND (probe, 1);
12919 if (TREE_CODE (probe) == VAR_DECL)
12921 /* A variable without external linkage might still be a
12922 valid constant-expression, so no error is issued here
12923 if the external-linkage check fails. */
12924 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12925 cp_parser_simulate_error (parser);
12927 else if (is_overloaded_fn (argument))
12928 /* All overloaded functions are allowed; if the external
12929 linkage test does not pass, an error will be issued
12933 && (TREE_CODE (argument) == OFFSET_REF
12934 || TREE_CODE (argument) == SCOPE_REF))
12935 /* A pointer-to-member. */
12937 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12940 cp_parser_simulate_error (parser);
12942 if (cp_parser_parse_definitely (parser))
12945 argument = build_x_unary_op (ADDR_EXPR, argument,
12946 tf_warning_or_error);
12951 /* If the argument started with "&", there are no other valid
12952 alternatives at this point. */
12955 cp_parser_error (parser, "invalid non-type template argument");
12956 return error_mark_node;
12959 /* If the argument wasn't successfully parsed as a type-id followed
12960 by '>>', the argument can only be a constant expression now.
12961 Otherwise, we try parsing the constant-expression tentatively,
12962 because the argument could really be a type-id. */
12964 cp_parser_parse_tentatively (parser);
12965 argument = cp_parser_constant_expression (parser,
12966 /*allow_non_constant_p=*/false,
12967 /*non_constant_p=*/NULL);
12968 argument = fold_non_dependent_expr (argument);
12969 if (!maybe_type_id)
12971 if (!cp_parser_next_token_ends_template_argument_p (parser))
12972 cp_parser_error (parser, "expected template-argument");
12973 if (cp_parser_parse_definitely (parser))
12975 /* We did our best to parse the argument as a non type-id, but that
12976 was the only alternative that matched (albeit with a '>' after
12977 it). We can assume it's just a typo from the user, and a
12978 diagnostic will then be issued. */
12979 return cp_parser_template_type_arg (parser);
12982 /* Parse an explicit-instantiation.
12984 explicit-instantiation:
12985 template declaration
12987 Although the standard says `declaration', what it really means is:
12989 explicit-instantiation:
12990 template decl-specifier-seq [opt] declarator [opt] ;
12992 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12993 supposed to be allowed. A defect report has been filed about this
12998 explicit-instantiation:
12999 storage-class-specifier template
13000 decl-specifier-seq [opt] declarator [opt] ;
13001 function-specifier template
13002 decl-specifier-seq [opt] declarator [opt] ; */
13005 cp_parser_explicit_instantiation (cp_parser* parser)
13007 int declares_class_or_enum;
13008 cp_decl_specifier_seq decl_specifiers;
13009 tree extension_specifier = NULL_TREE;
13011 timevar_push (TV_TEMPLATE_INST);
13013 /* Look for an (optional) storage-class-specifier or
13014 function-specifier. */
13015 if (cp_parser_allow_gnu_extensions_p (parser))
13017 extension_specifier
13018 = cp_parser_storage_class_specifier_opt (parser);
13019 if (!extension_specifier)
13020 extension_specifier
13021 = cp_parser_function_specifier_opt (parser,
13022 /*decl_specs=*/NULL);
13025 /* Look for the `template' keyword. */
13026 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13027 /* Let the front end know that we are processing an explicit
13029 begin_explicit_instantiation ();
13030 /* [temp.explicit] says that we are supposed to ignore access
13031 control while processing explicit instantiation directives. */
13032 push_deferring_access_checks (dk_no_check);
13033 /* Parse a decl-specifier-seq. */
13034 cp_parser_decl_specifier_seq (parser,
13035 CP_PARSER_FLAGS_OPTIONAL,
13037 &declares_class_or_enum);
13038 /* If there was exactly one decl-specifier, and it declared a class,
13039 and there's no declarator, then we have an explicit type
13041 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13045 type = check_tag_decl (&decl_specifiers);
13046 /* Turn access control back on for names used during
13047 template instantiation. */
13048 pop_deferring_access_checks ();
13050 do_type_instantiation (type, extension_specifier,
13051 /*complain=*/tf_error);
13055 cp_declarator *declarator;
13058 /* Parse the declarator. */
13060 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13061 /*ctor_dtor_or_conv_p=*/NULL,
13062 /*parenthesized_p=*/NULL,
13063 /*member_p=*/false);
13064 if (declares_class_or_enum & 2)
13065 cp_parser_check_for_definition_in_return_type (declarator,
13066 decl_specifiers.type,
13067 decl_specifiers.type_location);
13068 if (declarator != cp_error_declarator)
13070 if (decl_specifiers.specs[(int)ds_inline])
13071 permerror (input_location, "explicit instantiation shall not use"
13072 " %<inline%> specifier");
13073 if (decl_specifiers.specs[(int)ds_constexpr])
13074 permerror (input_location, "explicit instantiation shall not use"
13075 " %<constexpr%> specifier");
13077 decl = grokdeclarator (declarator, &decl_specifiers,
13078 NORMAL, 0, &decl_specifiers.attributes);
13079 /* Turn access control back on for names used during
13080 template instantiation. */
13081 pop_deferring_access_checks ();
13082 /* Do the explicit instantiation. */
13083 do_decl_instantiation (decl, extension_specifier);
13087 pop_deferring_access_checks ();
13088 /* Skip the body of the explicit instantiation. */
13089 cp_parser_skip_to_end_of_statement (parser);
13092 /* We're done with the instantiation. */
13093 end_explicit_instantiation ();
13095 cp_parser_consume_semicolon_at_end_of_statement (parser);
13097 timevar_pop (TV_TEMPLATE_INST);
13100 /* Parse an explicit-specialization.
13102 explicit-specialization:
13103 template < > declaration
13105 Although the standard says `declaration', what it really means is:
13107 explicit-specialization:
13108 template <> decl-specifier [opt] init-declarator [opt] ;
13109 template <> function-definition
13110 template <> explicit-specialization
13111 template <> template-declaration */
13114 cp_parser_explicit_specialization (cp_parser* parser)
13116 bool need_lang_pop;
13117 cp_token *token = cp_lexer_peek_token (parser->lexer);
13119 /* Look for the `template' keyword. */
13120 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13121 /* Look for the `<'. */
13122 cp_parser_require (parser, CPP_LESS, RT_LESS);
13123 /* Look for the `>'. */
13124 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13125 /* We have processed another parameter list. */
13126 ++parser->num_template_parameter_lists;
13129 A template ... explicit specialization ... shall not have C
13131 if (current_lang_name == lang_name_c)
13133 error_at (token->location, "template specialization with C linkage");
13134 /* Give it C++ linkage to avoid confusing other parts of the
13136 push_lang_context (lang_name_cplusplus);
13137 need_lang_pop = true;
13140 need_lang_pop = false;
13141 /* Let the front end know that we are beginning a specialization. */
13142 if (!begin_specialization ())
13144 end_specialization ();
13148 /* If the next keyword is `template', we need to figure out whether
13149 or not we're looking a template-declaration. */
13150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13152 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13153 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13154 cp_parser_template_declaration_after_export (parser,
13155 /*member_p=*/false);
13157 cp_parser_explicit_specialization (parser);
13160 /* Parse the dependent declaration. */
13161 cp_parser_single_declaration (parser,
13163 /*member_p=*/false,
13164 /*explicit_specialization_p=*/true,
13165 /*friend_p=*/NULL);
13166 /* We're done with the specialization. */
13167 end_specialization ();
13168 /* For the erroneous case of a template with C linkage, we pushed an
13169 implicit C++ linkage scope; exit that scope now. */
13171 pop_lang_context ();
13172 /* We're done with this parameter list. */
13173 --parser->num_template_parameter_lists;
13176 /* Parse a type-specifier.
13179 simple-type-specifier
13182 elaborated-type-specifier
13190 Returns a representation of the type-specifier. For a
13191 class-specifier, enum-specifier, or elaborated-type-specifier, a
13192 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13194 The parser flags FLAGS is used to control type-specifier parsing.
13196 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13197 in a decl-specifier-seq.
13199 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13200 class-specifier, enum-specifier, or elaborated-type-specifier, then
13201 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13202 if a type is declared; 2 if it is defined. Otherwise, it is set to
13205 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13206 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13207 is set to FALSE. */
13210 cp_parser_type_specifier (cp_parser* parser,
13211 cp_parser_flags flags,
13212 cp_decl_specifier_seq *decl_specs,
13213 bool is_declaration,
13214 int* declares_class_or_enum,
13215 bool* is_cv_qualifier)
13217 tree type_spec = NULL_TREE;
13220 cp_decl_spec ds = ds_last;
13222 /* Assume this type-specifier does not declare a new type. */
13223 if (declares_class_or_enum)
13224 *declares_class_or_enum = 0;
13225 /* And that it does not specify a cv-qualifier. */
13226 if (is_cv_qualifier)
13227 *is_cv_qualifier = false;
13228 /* Peek at the next token. */
13229 token = cp_lexer_peek_token (parser->lexer);
13231 /* If we're looking at a keyword, we can use that to guide the
13232 production we choose. */
13233 keyword = token->keyword;
13237 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13238 goto elaborated_type_specifier;
13240 /* Look for the enum-specifier. */
13241 type_spec = cp_parser_enum_specifier (parser);
13242 /* If that worked, we're done. */
13245 if (declares_class_or_enum)
13246 *declares_class_or_enum = 2;
13248 cp_parser_set_decl_spec_type (decl_specs,
13251 /*type_definition_p=*/true);
13255 goto elaborated_type_specifier;
13257 /* Any of these indicate either a class-specifier, or an
13258 elaborated-type-specifier. */
13262 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13263 goto elaborated_type_specifier;
13265 /* Parse tentatively so that we can back up if we don't find a
13266 class-specifier. */
13267 cp_parser_parse_tentatively (parser);
13268 /* Look for the class-specifier. */
13269 type_spec = cp_parser_class_specifier (parser);
13270 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13271 /* If that worked, we're done. */
13272 if (cp_parser_parse_definitely (parser))
13274 if (declares_class_or_enum)
13275 *declares_class_or_enum = 2;
13277 cp_parser_set_decl_spec_type (decl_specs,
13280 /*type_definition_p=*/true);
13284 /* Fall through. */
13285 elaborated_type_specifier:
13286 /* We're declaring (not defining) a class or enum. */
13287 if (declares_class_or_enum)
13288 *declares_class_or_enum = 1;
13290 /* Fall through. */
13292 /* Look for an elaborated-type-specifier. */
13294 = (cp_parser_elaborated_type_specifier
13296 decl_specs && decl_specs->specs[(int) ds_friend],
13299 cp_parser_set_decl_spec_type (decl_specs,
13302 /*type_definition_p=*/false);
13307 if (is_cv_qualifier)
13308 *is_cv_qualifier = true;
13313 if (is_cv_qualifier)
13314 *is_cv_qualifier = true;
13319 if (is_cv_qualifier)
13320 *is_cv_qualifier = true;
13324 /* The `__complex__' keyword is a GNU extension. */
13332 /* Handle simple keywords. */
13337 ++decl_specs->specs[(int)ds];
13338 decl_specs->any_specifiers_p = true;
13340 return cp_lexer_consume_token (parser->lexer)->u.value;
13343 /* If we do not already have a type-specifier, assume we are looking
13344 at a simple-type-specifier. */
13345 type_spec = cp_parser_simple_type_specifier (parser,
13349 /* If we didn't find a type-specifier, and a type-specifier was not
13350 optional in this context, issue an error message. */
13351 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13353 cp_parser_error (parser, "expected type specifier");
13354 return error_mark_node;
13360 /* Parse a simple-type-specifier.
13362 simple-type-specifier:
13363 :: [opt] nested-name-specifier [opt] type-name
13364 :: [opt] nested-name-specifier template template-id
13379 simple-type-specifier:
13381 decltype ( expression )
13384 __underlying_type ( type-id )
13388 simple-type-specifier:
13390 __typeof__ unary-expression
13391 __typeof__ ( type-id )
13393 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13394 appropriately updated. */
13397 cp_parser_simple_type_specifier (cp_parser* parser,
13398 cp_decl_specifier_seq *decl_specs,
13399 cp_parser_flags flags)
13401 tree type = NULL_TREE;
13404 /* Peek at the next token. */
13405 token = cp_lexer_peek_token (parser->lexer);
13407 /* If we're looking at a keyword, things are easy. */
13408 switch (token->keyword)
13412 decl_specs->explicit_char_p = true;
13413 type = char_type_node;
13416 type = char16_type_node;
13419 type = char32_type_node;
13422 type = wchar_type_node;
13425 type = boolean_type_node;
13429 ++decl_specs->specs[(int) ds_short];
13430 type = short_integer_type_node;
13434 decl_specs->explicit_int_p = true;
13435 type = integer_type_node;
13438 if (!int128_integer_type_node)
13441 decl_specs->explicit_int128_p = true;
13442 type = int128_integer_type_node;
13446 ++decl_specs->specs[(int) ds_long];
13447 type = long_integer_type_node;
13451 ++decl_specs->specs[(int) ds_signed];
13452 type = integer_type_node;
13456 ++decl_specs->specs[(int) ds_unsigned];
13457 type = unsigned_type_node;
13460 type = float_type_node;
13463 type = double_type_node;
13466 type = void_type_node;
13470 maybe_warn_cpp0x (CPP0X_AUTO);
13471 type = make_auto ();
13475 /* Since DR 743, decltype can either be a simple-type-specifier by
13476 itself or begin a nested-name-specifier. Parsing it will replace
13477 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13478 handling below decide what to do. */
13479 cp_parser_decltype (parser);
13480 cp_lexer_set_token_position (parser->lexer, token);
13484 /* Consume the `typeof' token. */
13485 cp_lexer_consume_token (parser->lexer);
13486 /* Parse the operand to `typeof'. */
13487 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13488 /* If it is not already a TYPE, take its type. */
13489 if (!TYPE_P (type))
13490 type = finish_typeof (type);
13493 cp_parser_set_decl_spec_type (decl_specs, type,
13495 /*type_definition_p=*/false);
13499 case RID_UNDERLYING_TYPE:
13500 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13502 cp_parser_set_decl_spec_type (decl_specs, type,
13504 /*type_definition_p=*/false);
13509 case RID_DIRECT_BASES:
13510 type = cp_parser_trait_expr (parser, token->keyword);
13512 cp_parser_set_decl_spec_type (decl_specs, type,
13514 /*type_definition_p=*/false);
13520 /* If token is an already-parsed decltype not followed by ::,
13521 it's a simple-type-specifier. */
13522 if (token->type == CPP_DECLTYPE
13523 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13525 type = token->u.value;
13527 cp_parser_set_decl_spec_type (decl_specs, type,
13529 /*type_definition_p=*/false);
13530 cp_lexer_consume_token (parser->lexer);
13534 /* If the type-specifier was for a built-in type, we're done. */
13537 /* Record the type. */
13539 && (token->keyword != RID_SIGNED
13540 && token->keyword != RID_UNSIGNED
13541 && token->keyword != RID_SHORT
13542 && token->keyword != RID_LONG))
13543 cp_parser_set_decl_spec_type (decl_specs,
13546 /*type_definition_p=*/false);
13548 decl_specs->any_specifiers_p = true;
13550 /* Consume the token. */
13551 cp_lexer_consume_token (parser->lexer);
13553 /* There is no valid C++ program where a non-template type is
13554 followed by a "<". That usually indicates that the user thought
13555 that the type was a template. */
13556 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13558 return TYPE_NAME (type);
13561 /* The type-specifier must be a user-defined type. */
13562 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13567 /* Don't gobble tokens or issue error messages if this is an
13568 optional type-specifier. */
13569 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13570 cp_parser_parse_tentatively (parser);
13572 /* Look for the optional `::' operator. */
13574 = (cp_parser_global_scope_opt (parser,
13575 /*current_scope_valid_p=*/false)
13577 /* Look for the nested-name specifier. */
13579 = (cp_parser_nested_name_specifier_opt (parser,
13580 /*typename_keyword_p=*/false,
13581 /*check_dependency_p=*/true,
13583 /*is_declaration=*/false)
13585 token = cp_lexer_peek_token (parser->lexer);
13586 /* If we have seen a nested-name-specifier, and the next token
13587 is `template', then we are using the template-id production. */
13589 && cp_parser_optional_template_keyword (parser))
13591 /* Look for the template-id. */
13592 type = cp_parser_template_id (parser,
13593 /*template_keyword_p=*/true,
13594 /*check_dependency_p=*/true,
13595 /*is_declaration=*/false);
13596 /* If the template-id did not name a type, we are out of
13598 if (TREE_CODE (type) != TYPE_DECL)
13600 cp_parser_error (parser, "expected template-id for type");
13604 /* Otherwise, look for a type-name. */
13606 type = cp_parser_type_name (parser);
13607 /* Keep track of all name-lookups performed in class scopes. */
13611 && TREE_CODE (type) == TYPE_DECL
13612 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13613 maybe_note_name_used_in_class (DECL_NAME (type), type);
13614 /* If it didn't work out, we don't have a TYPE. */
13615 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13616 && !cp_parser_parse_definitely (parser))
13618 if (type && decl_specs)
13619 cp_parser_set_decl_spec_type (decl_specs, type,
13621 /*type_definition_p=*/false);
13624 /* If we didn't get a type-name, issue an error message. */
13625 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13627 cp_parser_error (parser, "expected type-name");
13628 return error_mark_node;
13631 if (type && type != error_mark_node)
13633 /* See if TYPE is an Objective-C type, and if so, parse and
13634 accept any protocol references following it. Do this before
13635 the cp_parser_check_for_invalid_template_id() call, because
13636 Objective-C types can be followed by '<...>' which would
13637 enclose protocol names rather than template arguments, and so
13638 everything is fine. */
13639 if (c_dialect_objc () && !parser->scope
13640 && (objc_is_id (type) || objc_is_class_name (type)))
13642 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13643 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13645 /* Clobber the "unqualified" type previously entered into
13646 DECL_SPECS with the new, improved protocol-qualified version. */
13648 decl_specs->type = qual_type;
13653 /* There is no valid C++ program where a non-template type is
13654 followed by a "<". That usually indicates that the user
13655 thought that the type was a template. */
13656 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13663 /* Parse a type-name.
13669 simple-template-id [in c++0x]
13677 Returns a TYPE_DECL for the type. */
13680 cp_parser_type_name (cp_parser* parser)
13684 /* We can't know yet whether it is a class-name or not. */
13685 cp_parser_parse_tentatively (parser);
13686 /* Try a class-name. */
13687 type_decl = cp_parser_class_name (parser,
13688 /*typename_keyword_p=*/false,
13689 /*template_keyword_p=*/false,
13691 /*check_dependency_p=*/true,
13692 /*class_head_p=*/false,
13693 /*is_declaration=*/false);
13694 /* If it's not a class-name, keep looking. */
13695 if (!cp_parser_parse_definitely (parser))
13697 if (cxx_dialect < cxx0x)
13698 /* It must be a typedef-name or an enum-name. */
13699 return cp_parser_nonclass_name (parser);
13701 cp_parser_parse_tentatively (parser);
13702 /* It is either a simple-template-id representing an
13703 instantiation of an alias template... */
13704 type_decl = cp_parser_template_id (parser,
13705 /*template_keyword_p=*/false,
13706 /*check_dependency_p=*/false,
13707 /*is_declaration=*/false);
13708 /* Note that this must be an instantiation of an alias template
13709 because [temp.names]/6 says:
13711 A template-id that names an alias template specialization
13714 Whereas [temp.names]/7 says:
13716 A simple-template-id that names a class template
13717 specialization is a class-name. */
13718 if (type_decl != NULL_TREE
13719 && TREE_CODE (type_decl) == TYPE_DECL
13720 && TYPE_DECL_ALIAS_P (type_decl))
13721 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13723 cp_parser_simulate_error (parser);
13725 if (!cp_parser_parse_definitely (parser))
13726 /* ... Or a typedef-name or an enum-name. */
13727 return cp_parser_nonclass_name (parser);
13733 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13741 Returns a TYPE_DECL for the type. */
13744 cp_parser_nonclass_name (cp_parser* parser)
13749 cp_token *token = cp_lexer_peek_token (parser->lexer);
13750 identifier = cp_parser_identifier (parser);
13751 if (identifier == error_mark_node)
13752 return error_mark_node;
13754 /* Look up the type-name. */
13755 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13757 if (TREE_CODE (type_decl) != TYPE_DECL
13758 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13760 /* See if this is an Objective-C type. */
13761 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13762 tree type = objc_get_protocol_qualified_type (identifier, protos);
13764 type_decl = TYPE_NAME (type);
13767 /* Issue an error if we did not find a type-name. */
13768 if (TREE_CODE (type_decl) != TYPE_DECL
13769 /* In Objective-C, we have the complication that class names are
13770 normally type names and start declarations (eg, the
13771 "NSObject" in "NSObject *object;"), but can be used in an
13772 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13773 is an expression. So, a classname followed by a dot is not a
13774 valid type-name. */
13775 || (objc_is_class_name (TREE_TYPE (type_decl))
13776 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13778 if (!cp_parser_simulate_error (parser))
13779 cp_parser_name_lookup_error (parser, identifier, type_decl,
13780 NLE_TYPE, token->location);
13781 return error_mark_node;
13783 /* Remember that the name was used in the definition of the
13784 current class so that we can check later to see if the
13785 meaning would have been different after the class was
13786 entirely defined. */
13787 else if (type_decl != error_mark_node
13789 maybe_note_name_used_in_class (identifier, type_decl);
13794 /* Parse an elaborated-type-specifier. Note that the grammar given
13795 here incorporates the resolution to DR68.
13797 elaborated-type-specifier:
13798 class-key :: [opt] nested-name-specifier [opt] identifier
13799 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13800 enum-key :: [opt] nested-name-specifier [opt] identifier
13801 typename :: [opt] nested-name-specifier identifier
13802 typename :: [opt] nested-name-specifier template [opt]
13807 elaborated-type-specifier:
13808 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13809 class-key attributes :: [opt] nested-name-specifier [opt]
13810 template [opt] template-id
13811 enum attributes :: [opt] nested-name-specifier [opt] identifier
13813 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13814 declared `friend'. If IS_DECLARATION is TRUE, then this
13815 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13816 something is being declared.
13818 Returns the TYPE specified. */
13821 cp_parser_elaborated_type_specifier (cp_parser* parser,
13823 bool is_declaration)
13825 enum tag_types tag_type;
13827 tree type = NULL_TREE;
13828 tree attributes = NULL_TREE;
13830 cp_token *token = NULL;
13832 /* See if we're looking at the `enum' keyword. */
13833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13835 /* Consume the `enum' token. */
13836 cp_lexer_consume_token (parser->lexer);
13837 /* Remember that it's an enumeration type. */
13838 tag_type = enum_type;
13839 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13840 enums) is used here. */
13841 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13842 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13844 pedwarn (input_location, 0, "elaborated-type-specifier "
13845 "for a scoped enum must not use the %<%D%> keyword",
13846 cp_lexer_peek_token (parser->lexer)->u.value);
13847 /* Consume the `struct' or `class' and parse it anyway. */
13848 cp_lexer_consume_token (parser->lexer);
13850 /* Parse the attributes. */
13851 attributes = cp_parser_attributes_opt (parser);
13853 /* Or, it might be `typename'. */
13854 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13857 /* Consume the `typename' token. */
13858 cp_lexer_consume_token (parser->lexer);
13859 /* Remember that it's a `typename' type. */
13860 tag_type = typename_type;
13862 /* Otherwise it must be a class-key. */
13865 tag_type = cp_parser_class_key (parser);
13866 if (tag_type == none_type)
13867 return error_mark_node;
13868 /* Parse the attributes. */
13869 attributes = cp_parser_attributes_opt (parser);
13872 /* Look for the `::' operator. */
13873 globalscope = cp_parser_global_scope_opt (parser,
13874 /*current_scope_valid_p=*/false);
13875 /* Look for the nested-name-specifier. */
13876 if (tag_type == typename_type && !globalscope)
13878 if (!cp_parser_nested_name_specifier (parser,
13879 /*typename_keyword_p=*/true,
13880 /*check_dependency_p=*/true,
13883 return error_mark_node;
13886 /* Even though `typename' is not present, the proposed resolution
13887 to Core Issue 180 says that in `class A<T>::B', `B' should be
13888 considered a type-name, even if `A<T>' is dependent. */
13889 cp_parser_nested_name_specifier_opt (parser,
13890 /*typename_keyword_p=*/true,
13891 /*check_dependency_p=*/true,
13894 /* For everything but enumeration types, consider a template-id.
13895 For an enumeration type, consider only a plain identifier. */
13896 if (tag_type != enum_type)
13898 bool template_p = false;
13901 /* Allow the `template' keyword. */
13902 template_p = cp_parser_optional_template_keyword (parser);
13903 /* If we didn't see `template', we don't know if there's a
13904 template-id or not. */
13906 cp_parser_parse_tentatively (parser);
13907 /* Parse the template-id. */
13908 token = cp_lexer_peek_token (parser->lexer);
13909 decl = cp_parser_template_id (parser, template_p,
13910 /*check_dependency_p=*/true,
13912 /* If we didn't find a template-id, look for an ordinary
13914 if (!template_p && !cp_parser_parse_definitely (parser))
13916 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13917 in effect, then we must assume that, upon instantiation, the
13918 template will correspond to a class. */
13919 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13920 && tag_type == typename_type)
13921 type = make_typename_type (parser->scope, decl,
13923 /*complain=*/tf_error);
13924 /* If the `typename' keyword is in effect and DECL is not a type
13925 decl. Then type is non existant. */
13926 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13929 type = TREE_TYPE (decl);
13934 token = cp_lexer_peek_token (parser->lexer);
13935 identifier = cp_parser_identifier (parser);
13937 if (identifier == error_mark_node)
13939 parser->scope = NULL_TREE;
13940 return error_mark_node;
13943 /* For a `typename', we needn't call xref_tag. */
13944 if (tag_type == typename_type
13945 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13946 return cp_parser_make_typename_type (parser, parser->scope,
13949 /* Look up a qualified name in the usual way. */
13953 tree ambiguous_decls;
13955 decl = cp_parser_lookup_name (parser, identifier,
13957 /*is_template=*/false,
13958 /*is_namespace=*/false,
13959 /*check_dependency=*/true,
13963 /* If the lookup was ambiguous, an error will already have been
13965 if (ambiguous_decls)
13966 return error_mark_node;
13968 /* If we are parsing friend declaration, DECL may be a
13969 TEMPLATE_DECL tree node here. However, we need to check
13970 whether this TEMPLATE_DECL results in valid code. Consider
13971 the following example:
13974 template <class T> class C {};
13977 template <class T> friend class N::C; // #1, valid code
13979 template <class T> class Y {
13980 friend class N::C; // #2, invalid code
13983 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13984 name lookup of `N::C'. We see that friend declaration must
13985 be template for the code to be valid. Note that
13986 processing_template_decl does not work here since it is
13987 always 1 for the above two cases. */
13989 decl = (cp_parser_maybe_treat_template_as_class
13990 (decl, /*tag_name_p=*/is_friend
13991 && parser->num_template_parameter_lists));
13993 if (TREE_CODE (decl) != TYPE_DECL)
13995 cp_parser_diagnose_invalid_type_name (parser,
13999 return error_mark_node;
14002 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14004 bool allow_template = (parser->num_template_parameter_lists
14005 || DECL_SELF_REFERENCE_P (decl));
14006 type = check_elaborated_type_specifier (tag_type, decl,
14009 if (type == error_mark_node)
14010 return error_mark_node;
14013 /* Forward declarations of nested types, such as
14018 are invalid unless all components preceding the final '::'
14019 are complete. If all enclosing types are complete, these
14020 declarations become merely pointless.
14022 Invalid forward declarations of nested types are errors
14023 caught elsewhere in parsing. Those that are pointless arrive
14026 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14027 && !is_friend && !processing_explicit_instantiation)
14028 warning (0, "declaration %qD does not declare anything", decl);
14030 type = TREE_TYPE (decl);
14034 /* An elaborated-type-specifier sometimes introduces a new type and
14035 sometimes names an existing type. Normally, the rule is that it
14036 introduces a new type only if there is not an existing type of
14037 the same name already in scope. For example, given:
14040 void f() { struct S s; }
14042 the `struct S' in the body of `f' is the same `struct S' as in
14043 the global scope; the existing definition is used. However, if
14044 there were no global declaration, this would introduce a new
14045 local class named `S'.
14047 An exception to this rule applies to the following code:
14049 namespace N { struct S; }
14051 Here, the elaborated-type-specifier names a new type
14052 unconditionally; even if there is already an `S' in the
14053 containing scope this declaration names a new type.
14054 This exception only applies if the elaborated-type-specifier
14055 forms the complete declaration:
14059 A declaration consisting solely of `class-key identifier ;' is
14060 either a redeclaration of the name in the current scope or a
14061 forward declaration of the identifier as a class name. It
14062 introduces the name into the current scope.
14064 We are in this situation precisely when the next token is a `;'.
14066 An exception to the exception is that a `friend' declaration does
14067 *not* name a new type; i.e., given:
14069 struct S { friend struct T; };
14071 `T' is not a new type in the scope of `S'.
14073 Also, `new struct S' or `sizeof (struct S)' never results in the
14074 definition of a new type; a new type can only be declared in a
14075 declaration context. */
14081 /* Friends have special name lookup rules. */
14082 ts = ts_within_enclosing_non_class;
14083 else if (is_declaration
14084 && cp_lexer_next_token_is (parser->lexer,
14086 /* This is a `class-key identifier ;' */
14092 (parser->num_template_parameter_lists
14093 && (cp_parser_next_token_starts_class_definition_p (parser)
14094 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14095 /* An unqualified name was used to reference this type, so
14096 there were no qualifying templates. */
14097 if (!cp_parser_check_template_parameters (parser,
14098 /*num_templates=*/0,
14100 /*declarator=*/NULL))
14101 return error_mark_node;
14102 type = xref_tag (tag_type, identifier, ts, template_p);
14106 if (type == error_mark_node)
14107 return error_mark_node;
14109 /* Allow attributes on forward declarations of classes. */
14112 if (TREE_CODE (type) == TYPENAME_TYPE)
14113 warning (OPT_Wattributes,
14114 "attributes ignored on uninstantiated type");
14115 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14116 && ! processing_explicit_instantiation)
14117 warning (OPT_Wattributes,
14118 "attributes ignored on template instantiation");
14119 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14120 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14122 warning (OPT_Wattributes,
14123 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14126 if (tag_type != enum_type)
14128 /* Indicate whether this class was declared as a `class' or as a
14130 if (TREE_CODE (type) == RECORD_TYPE)
14131 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14132 cp_parser_check_class_key (tag_type, type);
14135 /* A "<" cannot follow an elaborated type specifier. If that
14136 happens, the user was probably trying to form a template-id. */
14137 cp_parser_check_for_invalid_template_id (parser, type, token->location);
14142 /* Parse an enum-specifier.
14145 enum-head { enumerator-list [opt] }
14146 enum-head { enumerator-list , } [C++0x]
14149 enum-key identifier [opt] enum-base [opt]
14150 enum-key nested-name-specifier identifier enum-base [opt]
14155 enum struct [C++0x]
14158 : type-specifier-seq
14160 opaque-enum-specifier:
14161 enum-key identifier enum-base [opt] ;
14164 enum-key attributes[opt] identifier [opt] enum-base [opt]
14165 { enumerator-list [opt] }attributes[opt]
14166 enum-key attributes[opt] identifier [opt] enum-base [opt]
14167 { enumerator-list, }attributes[opt] [C++0x]
14169 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14170 if the token stream isn't an enum-specifier after all. */
14173 cp_parser_enum_specifier (cp_parser* parser)
14176 tree type = NULL_TREE;
14178 tree nested_name_specifier = NULL_TREE;
14180 bool scoped_enum_p = false;
14181 bool has_underlying_type = false;
14182 bool nested_being_defined = false;
14183 bool new_value_list = false;
14184 bool is_new_type = false;
14185 bool is_anonymous = false;
14186 tree underlying_type = NULL_TREE;
14187 cp_token *type_start_token = NULL;
14188 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14190 parser->colon_corrects_to_scope_p = false;
14192 /* Parse tentatively so that we can back up if we don't find a
14194 cp_parser_parse_tentatively (parser);
14196 /* Caller guarantees that the current token is 'enum', an identifier
14197 possibly follows, and the token after that is an opening brace.
14198 If we don't have an identifier, fabricate an anonymous name for
14199 the enumeration being defined. */
14200 cp_lexer_consume_token (parser->lexer);
14202 /* Parse the "class" or "struct", which indicates a scoped
14203 enumeration type in C++0x. */
14204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14205 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14207 if (cxx_dialect < cxx0x)
14208 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14210 /* Consume the `struct' or `class' token. */
14211 cp_lexer_consume_token (parser->lexer);
14213 scoped_enum_p = true;
14216 attributes = cp_parser_attributes_opt (parser);
14218 /* Clear the qualification. */
14219 parser->scope = NULL_TREE;
14220 parser->qualifying_scope = NULL_TREE;
14221 parser->object_scope = NULL_TREE;
14223 /* Figure out in what scope the declaration is being placed. */
14224 prev_scope = current_scope ();
14226 type_start_token = cp_lexer_peek_token (parser->lexer);
14228 push_deferring_access_checks (dk_no_check);
14229 nested_name_specifier
14230 = cp_parser_nested_name_specifier_opt (parser,
14231 /*typename_keyword_p=*/true,
14232 /*check_dependency_p=*/false,
14234 /*is_declaration=*/false);
14236 if (nested_name_specifier)
14240 identifier = cp_parser_identifier (parser);
14241 name = cp_parser_lookup_name (parser, identifier,
14243 /*is_template=*/false,
14244 /*is_namespace=*/false,
14245 /*check_dependency=*/true,
14246 /*ambiguous_decls=*/NULL,
14250 type = TREE_TYPE (name);
14251 if (TREE_CODE (type) == TYPENAME_TYPE)
14253 /* Are template enums allowed in ISO? */
14254 if (template_parm_scope_p ())
14255 pedwarn (type_start_token->location, OPT_pedantic,
14256 "%qD is an enumeration template", name);
14257 /* ignore a typename reference, for it will be solved by name
14263 error_at (type_start_token->location,
14264 "%qD is not an enumerator-name", identifier);
14268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14269 identifier = cp_parser_identifier (parser);
14272 identifier = make_anon_name ();
14273 is_anonymous = true;
14276 pop_deferring_access_checks ();
14278 /* Check for the `:' that denotes a specified underlying type in C++0x.
14279 Note that a ':' could also indicate a bitfield width, however. */
14280 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14282 cp_decl_specifier_seq type_specifiers;
14284 /* Consume the `:'. */
14285 cp_lexer_consume_token (parser->lexer);
14287 /* Parse the type-specifier-seq. */
14288 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14289 /*is_trailing_return=*/false,
14292 /* At this point this is surely not elaborated type specifier. */
14293 if (!cp_parser_parse_definitely (parser))
14296 if (cxx_dialect < cxx0x)
14297 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14299 has_underlying_type = true;
14301 /* If that didn't work, stop. */
14302 if (type_specifiers.type != error_mark_node)
14304 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14305 /*initialized=*/0, NULL);
14306 if (underlying_type == error_mark_node)
14307 underlying_type = NULL_TREE;
14311 /* Look for the `{' but don't consume it yet. */
14312 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14314 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14316 cp_parser_error (parser, "expected %<{%>");
14317 if (has_underlying_type)
14323 /* An opaque-enum-specifier must have a ';' here. */
14324 if ((scoped_enum_p || underlying_type)
14325 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14327 cp_parser_error (parser, "expected %<;%> or %<{%>");
14328 if (has_underlying_type)
14336 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14339 if (nested_name_specifier)
14341 if (CLASS_TYPE_P (nested_name_specifier))
14343 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14344 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14345 push_scope (nested_name_specifier);
14347 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14349 push_nested_namespace (nested_name_specifier);
14353 /* Issue an error message if type-definitions are forbidden here. */
14354 if (!cp_parser_check_type_definition (parser))
14355 type = error_mark_node;
14357 /* Create the new type. We do this before consuming the opening
14358 brace so the enum will be recorded as being on the line of its
14359 tag (or the 'enum' keyword, if there is no tag). */
14360 type = start_enum (identifier, type, underlying_type,
14361 scoped_enum_p, &is_new_type);
14363 /* If the next token is not '{' it is an opaque-enum-specifier or an
14364 elaborated-type-specifier. */
14365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14367 timevar_push (TV_PARSE_ENUM);
14368 if (nested_name_specifier)
14370 /* The following catches invalid code such as:
14371 enum class S<int>::E { A, B, C }; */
14372 if (!processing_specialization
14373 && CLASS_TYPE_P (nested_name_specifier)
14374 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14375 error_at (type_start_token->location, "cannot add an enumerator "
14376 "list to a template instantiation");
14378 /* If that scope does not contain the scope in which the
14379 class was originally declared, the program is invalid. */
14380 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14382 if (at_namespace_scope_p ())
14383 error_at (type_start_token->location,
14384 "declaration of %qD in namespace %qD which does not "
14386 type, prev_scope, nested_name_specifier);
14388 error_at (type_start_token->location,
14389 "declaration of %qD in %qD which does not enclose %qD",
14390 type, prev_scope, nested_name_specifier);
14391 type = error_mark_node;
14396 begin_scope (sk_scoped_enum, type);
14398 /* Consume the opening brace. */
14399 cp_lexer_consume_token (parser->lexer);
14401 if (type == error_mark_node)
14402 ; /* Nothing to add */
14403 else if (OPAQUE_ENUM_P (type)
14404 || (cxx_dialect > cxx98 && processing_specialization))
14406 new_value_list = true;
14407 SET_OPAQUE_ENUM_P (type, false);
14408 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14412 error_at (type_start_token->location, "multiple definition of %q#T", type);
14413 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14414 "previous definition here");
14415 type = error_mark_node;
14418 if (type == error_mark_node)
14419 cp_parser_skip_to_end_of_block_or_statement (parser);
14420 /* If the next token is not '}', then there are some enumerators. */
14421 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14422 cp_parser_enumerator_list (parser, type);
14424 /* Consume the final '}'. */
14425 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14429 timevar_pop (TV_PARSE_ENUM);
14433 /* If a ';' follows, then it is an opaque-enum-specifier
14434 and additional restrictions apply. */
14435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14438 error_at (type_start_token->location,
14439 "opaque-enum-specifier without name");
14440 else if (nested_name_specifier)
14441 error_at (type_start_token->location,
14442 "opaque-enum-specifier must use a simple identifier");
14446 /* Look for trailing attributes to apply to this enumeration, and
14447 apply them if appropriate. */
14448 if (cp_parser_allow_gnu_extensions_p (parser))
14450 tree trailing_attr = cp_parser_attributes_opt (parser);
14451 trailing_attr = chainon (trailing_attr, attributes);
14452 cplus_decl_attributes (&type,
14454 (int) ATTR_FLAG_TYPE_IN_PLACE);
14457 /* Finish up the enumeration. */
14458 if (type != error_mark_node)
14460 if (new_value_list)
14461 finish_enum_value_list (type);
14463 finish_enum (type);
14466 if (nested_name_specifier)
14468 if (CLASS_TYPE_P (nested_name_specifier))
14470 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14471 pop_scope (nested_name_specifier);
14473 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14475 pop_nested_namespace (nested_name_specifier);
14479 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14483 /* Parse an enumerator-list. The enumerators all have the indicated
14487 enumerator-definition
14488 enumerator-list , enumerator-definition */
14491 cp_parser_enumerator_list (cp_parser* parser, tree type)
14495 /* Parse an enumerator-definition. */
14496 cp_parser_enumerator_definition (parser, type);
14498 /* If the next token is not a ',', we've reached the end of
14500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14502 /* Otherwise, consume the `,' and keep going. */
14503 cp_lexer_consume_token (parser->lexer);
14504 /* If the next token is a `}', there is a trailing comma. */
14505 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14507 if (cxx_dialect < cxx0x && !in_system_header)
14508 pedwarn (input_location, OPT_pedantic,
14509 "comma at end of enumerator list");
14515 /* Parse an enumerator-definition. The enumerator has the indicated
14518 enumerator-definition:
14520 enumerator = constant-expression
14526 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14532 /* Save the input location because we are interested in the location
14533 of the identifier and not the location of the explicit value. */
14534 loc = cp_lexer_peek_token (parser->lexer)->location;
14536 /* Look for the identifier. */
14537 identifier = cp_parser_identifier (parser);
14538 if (identifier == error_mark_node)
14541 /* If the next token is an '=', then there is an explicit value. */
14542 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14544 /* Consume the `=' token. */
14545 cp_lexer_consume_token (parser->lexer);
14546 /* Parse the value. */
14547 value = cp_parser_constant_expression (parser,
14548 /*allow_non_constant_p=*/false,
14554 /* If we are processing a template, make sure the initializer of the
14555 enumerator doesn't contain any bare template parameter pack. */
14556 if (check_for_bare_parameter_packs (value))
14557 value = error_mark_node;
14559 /* integral_constant_value will pull out this expression, so make sure
14560 it's folded as appropriate. */
14561 value = fold_non_dependent_expr (value);
14563 /* Create the enumerator. */
14564 build_enumerator (identifier, value, type, loc);
14567 /* Parse a namespace-name.
14570 original-namespace-name
14573 Returns the NAMESPACE_DECL for the namespace. */
14576 cp_parser_namespace_name (cp_parser* parser)
14579 tree namespace_decl;
14581 cp_token *token = cp_lexer_peek_token (parser->lexer);
14583 /* Get the name of the namespace. */
14584 identifier = cp_parser_identifier (parser);
14585 if (identifier == error_mark_node)
14586 return error_mark_node;
14588 /* Look up the identifier in the currently active scope. Look only
14589 for namespaces, due to:
14591 [basic.lookup.udir]
14593 When looking up a namespace-name in a using-directive or alias
14594 definition, only namespace names are considered.
14598 [basic.lookup.qual]
14600 During the lookup of a name preceding the :: scope resolution
14601 operator, object, function, and enumerator names are ignored.
14603 (Note that cp_parser_qualifying_entity only calls this
14604 function if the token after the name is the scope resolution
14606 namespace_decl = cp_parser_lookup_name (parser, identifier,
14608 /*is_template=*/false,
14609 /*is_namespace=*/true,
14610 /*check_dependency=*/true,
14611 /*ambiguous_decls=*/NULL,
14613 /* If it's not a namespace, issue an error. */
14614 if (namespace_decl == error_mark_node
14615 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14617 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14618 error_at (token->location, "%qD is not a namespace-name", identifier);
14619 cp_parser_error (parser, "expected namespace-name");
14620 namespace_decl = error_mark_node;
14623 return namespace_decl;
14626 /* Parse a namespace-definition.
14628 namespace-definition:
14629 named-namespace-definition
14630 unnamed-namespace-definition
14632 named-namespace-definition:
14633 original-namespace-definition
14634 extension-namespace-definition
14636 original-namespace-definition:
14637 namespace identifier { namespace-body }
14639 extension-namespace-definition:
14640 namespace original-namespace-name { namespace-body }
14642 unnamed-namespace-definition:
14643 namespace { namespace-body } */
14646 cp_parser_namespace_definition (cp_parser* parser)
14648 tree identifier, attribs;
14649 bool has_visibility;
14652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14654 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14656 cp_lexer_consume_token (parser->lexer);
14661 /* Look for the `namespace' keyword. */
14662 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14664 /* Get the name of the namespace. We do not attempt to distinguish
14665 between an original-namespace-definition and an
14666 extension-namespace-definition at this point. The semantic
14667 analysis routines are responsible for that. */
14668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14669 identifier = cp_parser_identifier (parser);
14671 identifier = NULL_TREE;
14673 /* Parse any specified attributes. */
14674 attribs = cp_parser_attributes_opt (parser);
14676 /* Look for the `{' to start the namespace. */
14677 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14678 /* Start the namespace. */
14679 push_namespace (identifier);
14681 /* "inline namespace" is equivalent to a stub namespace definition
14682 followed by a strong using directive. */
14685 tree name_space = current_namespace;
14686 /* Set up namespace association. */
14687 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14688 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14689 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14690 /* Import the contents of the inline namespace. */
14692 do_using_directive (name_space);
14693 push_namespace (identifier);
14696 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14698 /* Parse the body of the namespace. */
14699 cp_parser_namespace_body (parser);
14701 if (has_visibility)
14702 pop_visibility (1);
14704 /* Finish the namespace. */
14706 /* Look for the final `}'. */
14707 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14710 /* Parse a namespace-body.
14713 declaration-seq [opt] */
14716 cp_parser_namespace_body (cp_parser* parser)
14718 cp_parser_declaration_seq_opt (parser);
14721 /* Parse a namespace-alias-definition.
14723 namespace-alias-definition:
14724 namespace identifier = qualified-namespace-specifier ; */
14727 cp_parser_namespace_alias_definition (cp_parser* parser)
14730 tree namespace_specifier;
14732 cp_token *token = cp_lexer_peek_token (parser->lexer);
14734 /* Look for the `namespace' keyword. */
14735 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14736 /* Look for the identifier. */
14737 identifier = cp_parser_identifier (parser);
14738 if (identifier == error_mark_node)
14740 /* Look for the `=' token. */
14741 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14742 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14744 error_at (token->location, "%<namespace%> definition is not allowed here");
14745 /* Skip the definition. */
14746 cp_lexer_consume_token (parser->lexer);
14747 if (cp_parser_skip_to_closing_brace (parser))
14748 cp_lexer_consume_token (parser->lexer);
14751 cp_parser_require (parser, CPP_EQ, RT_EQ);
14752 /* Look for the qualified-namespace-specifier. */
14753 namespace_specifier
14754 = cp_parser_qualified_namespace_specifier (parser);
14755 /* Look for the `;' token. */
14756 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14758 /* Register the alias in the symbol table. */
14759 do_namespace_alias (identifier, namespace_specifier);
14762 /* Parse a qualified-namespace-specifier.
14764 qualified-namespace-specifier:
14765 :: [opt] nested-name-specifier [opt] namespace-name
14767 Returns a NAMESPACE_DECL corresponding to the specified
14771 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14773 /* Look for the optional `::'. */
14774 cp_parser_global_scope_opt (parser,
14775 /*current_scope_valid_p=*/false);
14777 /* Look for the optional nested-name-specifier. */
14778 cp_parser_nested_name_specifier_opt (parser,
14779 /*typename_keyword_p=*/false,
14780 /*check_dependency_p=*/true,
14782 /*is_declaration=*/true);
14784 return cp_parser_namespace_name (parser);
14787 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14788 access declaration.
14791 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14792 using :: unqualified-id ;
14794 access-declaration:
14800 cp_parser_using_declaration (cp_parser* parser,
14801 bool access_declaration_p)
14804 bool typename_p = false;
14805 bool global_scope_p;
14810 if (access_declaration_p)
14811 cp_parser_parse_tentatively (parser);
14814 /* Look for the `using' keyword. */
14815 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14817 /* Peek at the next token. */
14818 token = cp_lexer_peek_token (parser->lexer);
14819 /* See if it's `typename'. */
14820 if (token->keyword == RID_TYPENAME)
14822 /* Remember that we've seen it. */
14824 /* Consume the `typename' token. */
14825 cp_lexer_consume_token (parser->lexer);
14829 /* Look for the optional global scope qualification. */
14831 = (cp_parser_global_scope_opt (parser,
14832 /*current_scope_valid_p=*/false)
14835 /* If we saw `typename', or didn't see `::', then there must be a
14836 nested-name-specifier present. */
14837 if (typename_p || !global_scope_p)
14838 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14839 /*check_dependency_p=*/true,
14841 /*is_declaration=*/true);
14842 /* Otherwise, we could be in either of the two productions. In that
14843 case, treat the nested-name-specifier as optional. */
14845 qscope = cp_parser_nested_name_specifier_opt (parser,
14846 /*typename_keyword_p=*/false,
14847 /*check_dependency_p=*/true,
14849 /*is_declaration=*/true);
14851 qscope = global_namespace;
14853 if (access_declaration_p && cp_parser_error_occurred (parser))
14854 /* Something has already gone wrong; there's no need to parse
14855 further. Since an error has occurred, the return value of
14856 cp_parser_parse_definitely will be false, as required. */
14857 return cp_parser_parse_definitely (parser);
14859 token = cp_lexer_peek_token (parser->lexer);
14860 /* Parse the unqualified-id. */
14861 identifier = cp_parser_unqualified_id (parser,
14862 /*template_keyword_p=*/false,
14863 /*check_dependency_p=*/true,
14864 /*declarator_p=*/true,
14865 /*optional_p=*/false);
14867 if (access_declaration_p)
14869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14870 cp_parser_simulate_error (parser);
14871 if (!cp_parser_parse_definitely (parser))
14875 /* The function we call to handle a using-declaration is different
14876 depending on what scope we are in. */
14877 if (qscope == error_mark_node || identifier == error_mark_node)
14879 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14880 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14881 /* [namespace.udecl]
14883 A using declaration shall not name a template-id. */
14884 error_at (token->location,
14885 "a template-id may not appear in a using-declaration");
14888 if (at_class_scope_p ())
14890 /* Create the USING_DECL. */
14891 decl = do_class_using_decl (parser->scope, identifier);
14893 if (check_for_bare_parameter_packs (decl))
14896 /* Add it to the list of members in this class. */
14897 finish_member_declaration (decl);
14901 decl = cp_parser_lookup_name_simple (parser,
14904 if (decl == error_mark_node)
14905 cp_parser_name_lookup_error (parser, identifier,
14908 else if (check_for_bare_parameter_packs (decl))
14910 else if (!at_namespace_scope_p ())
14911 do_local_using_decl (decl, qscope, identifier);
14913 do_toplevel_using_decl (decl, qscope, identifier);
14917 /* Look for the final `;'. */
14918 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14923 /* Parse an alias-declaration.
14926 using identifier attribute-specifier-seq [opt] = type-id */
14929 cp_parser_alias_declaration (cp_parser* parser)
14931 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
14932 location_t id_location;
14933 cp_declarator *declarator;
14934 cp_decl_specifier_seq decl_specs;
14937 /* Look for the `using' keyword. */
14938 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14939 id_location = cp_lexer_peek_token (parser->lexer)->location;
14940 id = cp_parser_identifier (parser);
14941 attributes = cp_parser_attributes_opt (parser);
14942 cp_parser_require (parser, CPP_EQ, RT_EQ);
14944 type = cp_parser_type_id (parser);
14945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14947 /* A typedef-name can also be introduced by an alias-declaration. The
14948 identifier following the using keyword becomes a typedef-name. It has
14949 the same semantics as if it were introduced by the typedef
14950 specifier. In particular, it does not define a new type and it shall
14951 not appear in the type-id. */
14953 clear_decl_specs (&decl_specs);
14954 decl_specs.type = type;
14955 decl_specs.attributes = attributes;
14956 ++decl_specs.specs[(int) ds_typedef];
14957 ++decl_specs.specs[(int) ds_alias];
14959 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
14960 declarator->id_loc = id_location;
14962 member_p = at_class_scope_p ();
14964 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
14965 NULL_TREE, attributes);
14967 decl = start_decl (declarator, &decl_specs, 0,
14968 attributes, NULL_TREE, &pushed_scope);
14969 if (decl == error_mark_node)
14972 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
14975 pop_scope (pushed_scope);
14977 /* If decl is a template, return its TEMPLATE_DECL so that it gets
14978 added into the symbol table; otherwise, return the TYPE_DECL. */
14979 if (DECL_LANG_SPECIFIC (decl)
14980 && DECL_TEMPLATE_INFO (decl)
14981 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
14983 decl = DECL_TI_TEMPLATE (decl);
14985 check_member_template (decl);
14991 /* Parse a using-directive.
14994 using namespace :: [opt] nested-name-specifier [opt]
14995 namespace-name ; */
14998 cp_parser_using_directive (cp_parser* parser)
15000 tree namespace_decl;
15003 /* Look for the `using' keyword. */
15004 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15005 /* And the `namespace' keyword. */
15006 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15007 /* Look for the optional `::' operator. */
15008 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15009 /* And the optional nested-name-specifier. */
15010 cp_parser_nested_name_specifier_opt (parser,
15011 /*typename_keyword_p=*/false,
15012 /*check_dependency_p=*/true,
15014 /*is_declaration=*/true);
15015 /* Get the namespace being used. */
15016 namespace_decl = cp_parser_namespace_name (parser);
15017 /* And any specified attributes. */
15018 attribs = cp_parser_attributes_opt (parser);
15019 /* Update the symbol table. */
15020 parse_using_directive (namespace_decl, attribs);
15021 /* Look for the final `;'. */
15022 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15025 /* Parse an asm-definition.
15028 asm ( string-literal ) ;
15033 asm volatile [opt] ( string-literal ) ;
15034 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15035 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15036 : asm-operand-list [opt] ) ;
15037 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15038 : asm-operand-list [opt]
15039 : asm-clobber-list [opt] ) ;
15040 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15041 : asm-clobber-list [opt]
15042 : asm-goto-list ) ; */
15045 cp_parser_asm_definition (cp_parser* parser)
15048 tree outputs = NULL_TREE;
15049 tree inputs = NULL_TREE;
15050 tree clobbers = NULL_TREE;
15051 tree labels = NULL_TREE;
15053 bool volatile_p = false;
15054 bool extended_p = false;
15055 bool invalid_inputs_p = false;
15056 bool invalid_outputs_p = false;
15057 bool goto_p = false;
15058 required_token missing = RT_NONE;
15060 /* Look for the `asm' keyword. */
15061 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15062 /* See if the next token is `volatile'. */
15063 if (cp_parser_allow_gnu_extensions_p (parser)
15064 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15066 /* Remember that we saw the `volatile' keyword. */
15068 /* Consume the token. */
15069 cp_lexer_consume_token (parser->lexer);
15071 if (cp_parser_allow_gnu_extensions_p (parser)
15072 && parser->in_function_body
15073 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15075 /* Remember that we saw the `goto' keyword. */
15077 /* Consume the token. */
15078 cp_lexer_consume_token (parser->lexer);
15080 /* Look for the opening `('. */
15081 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15083 /* Look for the string. */
15084 string = cp_parser_string_literal (parser, false, false);
15085 if (string == error_mark_node)
15087 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15088 /*consume_paren=*/true);
15092 /* If we're allowing GNU extensions, check for the extended assembly
15093 syntax. Unfortunately, the `:' tokens need not be separated by
15094 a space in C, and so, for compatibility, we tolerate that here
15095 too. Doing that means that we have to treat the `::' operator as
15097 if (cp_parser_allow_gnu_extensions_p (parser)
15098 && parser->in_function_body
15099 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15100 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15102 bool inputs_p = false;
15103 bool clobbers_p = false;
15104 bool labels_p = false;
15106 /* The extended syntax was used. */
15109 /* Look for outputs. */
15110 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15112 /* Consume the `:'. */
15113 cp_lexer_consume_token (parser->lexer);
15114 /* Parse the output-operands. */
15115 if (cp_lexer_next_token_is_not (parser->lexer,
15117 && cp_lexer_next_token_is_not (parser->lexer,
15119 && cp_lexer_next_token_is_not (parser->lexer,
15122 outputs = cp_parser_asm_operand_list (parser);
15124 if (outputs == error_mark_node)
15125 invalid_outputs_p = true;
15127 /* If the next token is `::', there are no outputs, and the
15128 next token is the beginning of the inputs. */
15129 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15130 /* The inputs are coming next. */
15133 /* Look for inputs. */
15135 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15137 /* Consume the `:' or `::'. */
15138 cp_lexer_consume_token (parser->lexer);
15139 /* Parse the output-operands. */
15140 if (cp_lexer_next_token_is_not (parser->lexer,
15142 && cp_lexer_next_token_is_not (parser->lexer,
15144 && cp_lexer_next_token_is_not (parser->lexer,
15146 inputs = cp_parser_asm_operand_list (parser);
15148 if (inputs == error_mark_node)
15149 invalid_inputs_p = true;
15151 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15152 /* The clobbers are coming next. */
15155 /* Look for clobbers. */
15157 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15160 /* Consume the `:' or `::'. */
15161 cp_lexer_consume_token (parser->lexer);
15162 /* Parse the clobbers. */
15163 if (cp_lexer_next_token_is_not (parser->lexer,
15165 && cp_lexer_next_token_is_not (parser->lexer,
15167 clobbers = cp_parser_asm_clobber_list (parser);
15170 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15171 /* The labels are coming next. */
15174 /* Look for labels. */
15176 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15179 /* Consume the `:' or `::'. */
15180 cp_lexer_consume_token (parser->lexer);
15181 /* Parse the labels. */
15182 labels = cp_parser_asm_label_list (parser);
15185 if (goto_p && !labels_p)
15186 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15189 missing = RT_COLON_SCOPE;
15191 /* Look for the closing `)'. */
15192 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15193 missing ? missing : RT_CLOSE_PAREN))
15194 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15195 /*consume_paren=*/true);
15196 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15198 if (!invalid_inputs_p && !invalid_outputs_p)
15200 /* Create the ASM_EXPR. */
15201 if (parser->in_function_body)
15203 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15204 inputs, clobbers, labels);
15205 /* If the extended syntax was not used, mark the ASM_EXPR. */
15208 tree temp = asm_stmt;
15209 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15210 temp = TREE_OPERAND (temp, 0);
15212 ASM_INPUT_P (temp) = 1;
15216 cgraph_add_asm_node (string);
15220 /* Declarators [gram.dcl.decl] */
15222 /* Parse an init-declarator.
15225 declarator initializer [opt]
15230 declarator asm-specification [opt] attributes [opt] initializer [opt]
15232 function-definition:
15233 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15235 decl-specifier-seq [opt] declarator function-try-block
15239 function-definition:
15240 __extension__ function-definition
15244 function-definition:
15245 decl-specifier-seq [opt] declarator function-transaction-block
15247 The DECL_SPECIFIERS apply to this declarator. Returns a
15248 representation of the entity declared. If MEMBER_P is TRUE, then
15249 this declarator appears in a class scope. The new DECL created by
15250 this declarator is returned.
15252 The CHECKS are access checks that should be performed once we know
15253 what entity is being declared (and, therefore, what classes have
15256 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15257 for a function-definition here as well. If the declarator is a
15258 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15259 be TRUE upon return. By that point, the function-definition will
15260 have been completely parsed.
15262 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15265 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15266 parsed declaration if it is an uninitialized single declarator not followed
15267 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15268 if present, will not be consumed. If returned, this declarator will be
15269 created with SD_INITIALIZED but will not call cp_finish_decl. */
15272 cp_parser_init_declarator (cp_parser* parser,
15273 cp_decl_specifier_seq *decl_specifiers,
15274 VEC (deferred_access_check,gc)* checks,
15275 bool function_definition_allowed_p,
15277 int declares_class_or_enum,
15278 bool* function_definition_p,
15279 tree* maybe_range_for_decl)
15281 cp_token *token = NULL, *asm_spec_start_token = NULL,
15282 *attributes_start_token = NULL;
15283 cp_declarator *declarator;
15284 tree prefix_attributes;
15286 tree asm_specification;
15288 tree decl = NULL_TREE;
15290 int is_initialized;
15291 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15292 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15294 enum cpp_ttype initialization_kind;
15295 bool is_direct_init = false;
15296 bool is_non_constant_init;
15297 int ctor_dtor_or_conv_p;
15299 tree pushed_scope = NULL_TREE;
15300 bool range_for_decl_p = false;
15302 /* Gather the attributes that were provided with the
15303 decl-specifiers. */
15304 prefix_attributes = decl_specifiers->attributes;
15306 /* Assume that this is not the declarator for a function
15308 if (function_definition_p)
15309 *function_definition_p = false;
15311 /* Defer access checks while parsing the declarator; we cannot know
15312 what names are accessible until we know what is being
15314 resume_deferring_access_checks ();
15316 /* Parse the declarator. */
15317 token = cp_lexer_peek_token (parser->lexer);
15319 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15320 &ctor_dtor_or_conv_p,
15321 /*parenthesized_p=*/NULL,
15323 /* Gather up the deferred checks. */
15324 stop_deferring_access_checks ();
15326 /* If the DECLARATOR was erroneous, there's no need to go
15328 if (declarator == cp_error_declarator)
15329 return error_mark_node;
15331 /* Check that the number of template-parameter-lists is OK. */
15332 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15334 return error_mark_node;
15336 if (declares_class_or_enum & 2)
15337 cp_parser_check_for_definition_in_return_type (declarator,
15338 decl_specifiers->type,
15339 decl_specifiers->type_location);
15341 /* Figure out what scope the entity declared by the DECLARATOR is
15342 located in. `grokdeclarator' sometimes changes the scope, so
15343 we compute it now. */
15344 scope = get_scope_of_declarator (declarator);
15346 /* Perform any lookups in the declared type which were thought to be
15347 dependent, but are not in the scope of the declarator. */
15348 decl_specifiers->type
15349 = maybe_update_decl_type (decl_specifiers->type, scope);
15351 /* If we're allowing GNU extensions, look for an asm-specification
15353 if (cp_parser_allow_gnu_extensions_p (parser))
15355 /* Look for an asm-specification. */
15356 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15357 asm_specification = cp_parser_asm_specification_opt (parser);
15358 /* And attributes. */
15359 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15360 attributes = cp_parser_attributes_opt (parser);
15364 asm_specification = NULL_TREE;
15365 attributes = NULL_TREE;
15368 /* Peek at the next token. */
15369 token = cp_lexer_peek_token (parser->lexer);
15370 /* Check to see if the token indicates the start of a
15371 function-definition. */
15372 if (function_declarator_p (declarator)
15373 && cp_parser_token_starts_function_definition_p (token))
15375 if (!function_definition_allowed_p)
15377 /* If a function-definition should not appear here, issue an
15379 cp_parser_error (parser,
15380 "a function-definition is not allowed here");
15381 return error_mark_node;
15385 location_t func_brace_location
15386 = cp_lexer_peek_token (parser->lexer)->location;
15388 /* Neither attributes nor an asm-specification are allowed
15389 on a function-definition. */
15390 if (asm_specification)
15391 error_at (asm_spec_start_token->location,
15392 "an asm-specification is not allowed "
15393 "on a function-definition");
15395 error_at (attributes_start_token->location,
15396 "attributes are not allowed on a function-definition");
15397 /* This is a function-definition. */
15398 *function_definition_p = true;
15400 /* Parse the function definition. */
15402 decl = cp_parser_save_member_function_body (parser,
15405 prefix_attributes);
15408 = (cp_parser_function_definition_from_specifiers_and_declarator
15409 (parser, decl_specifiers, prefix_attributes, declarator));
15411 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15413 /* This is where the prologue starts... */
15414 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15415 = func_brace_location;
15424 Only in function declarations for constructors, destructors, and
15425 type conversions can the decl-specifier-seq be omitted.
15427 We explicitly postpone this check past the point where we handle
15428 function-definitions because we tolerate function-definitions
15429 that are missing their return types in some modes. */
15430 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15432 cp_parser_error (parser,
15433 "expected constructor, destructor, or type conversion");
15434 return error_mark_node;
15437 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15438 if (token->type == CPP_EQ
15439 || token->type == CPP_OPEN_PAREN
15440 || token->type == CPP_OPEN_BRACE)
15442 is_initialized = SD_INITIALIZED;
15443 initialization_kind = token->type;
15444 if (maybe_range_for_decl)
15445 *maybe_range_for_decl = error_mark_node;
15447 if (token->type == CPP_EQ
15448 && function_declarator_p (declarator))
15450 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15451 if (t2->keyword == RID_DEFAULT)
15452 is_initialized = SD_DEFAULTED;
15453 else if (t2->keyword == RID_DELETE)
15454 is_initialized = SD_DELETED;
15459 /* If the init-declarator isn't initialized and isn't followed by a
15460 `,' or `;', it's not a valid init-declarator. */
15461 if (token->type != CPP_COMMA
15462 && token->type != CPP_SEMICOLON)
15464 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15465 range_for_decl_p = true;
15468 cp_parser_error (parser, "expected initializer");
15469 return error_mark_node;
15472 is_initialized = SD_UNINITIALIZED;
15473 initialization_kind = CPP_EOF;
15476 /* Because start_decl has side-effects, we should only call it if we
15477 know we're going ahead. By this point, we know that we cannot
15478 possibly be looking at any other construct. */
15479 cp_parser_commit_to_tentative_parse (parser);
15481 /* If the decl specifiers were bad, issue an error now that we're
15482 sure this was intended to be a declarator. Then continue
15483 declaring the variable(s), as int, to try to cut down on further
15485 if (decl_specifiers->any_specifiers_p
15486 && decl_specifiers->type == error_mark_node)
15488 cp_parser_error (parser, "invalid type in declaration");
15489 decl_specifiers->type = integer_type_node;
15492 /* Check to see whether or not this declaration is a friend. */
15493 friend_p = cp_parser_friend_p (decl_specifiers);
15495 /* Enter the newly declared entry in the symbol table. If we're
15496 processing a declaration in a class-specifier, we wait until
15497 after processing the initializer. */
15500 if (parser->in_unbraced_linkage_specification_p)
15501 decl_specifiers->storage_class = sc_extern;
15502 decl = start_decl (declarator, decl_specifiers,
15503 range_for_decl_p? SD_INITIALIZED : is_initialized,
15504 attributes, prefix_attributes,
15506 /* Adjust location of decl if declarator->id_loc is more appropriate:
15507 set, and decl wasn't merged with another decl, in which case its
15508 location would be different from input_location, and more accurate. */
15510 && declarator->id_loc != UNKNOWN_LOCATION
15511 && DECL_SOURCE_LOCATION (decl) == input_location)
15512 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15515 /* Enter the SCOPE. That way unqualified names appearing in the
15516 initializer will be looked up in SCOPE. */
15517 pushed_scope = push_scope (scope);
15519 /* Perform deferred access control checks, now that we know in which
15520 SCOPE the declared entity resides. */
15521 if (!member_p && decl)
15523 tree saved_current_function_decl = NULL_TREE;
15525 /* If the entity being declared is a function, pretend that we
15526 are in its scope. If it is a `friend', it may have access to
15527 things that would not otherwise be accessible. */
15528 if (TREE_CODE (decl) == FUNCTION_DECL)
15530 saved_current_function_decl = current_function_decl;
15531 current_function_decl = decl;
15534 /* Perform access checks for template parameters. */
15535 cp_parser_perform_template_parameter_access_checks (checks);
15537 /* Perform the access control checks for the declarator and the
15538 decl-specifiers. */
15539 perform_deferred_access_checks ();
15541 /* Restore the saved value. */
15542 if (TREE_CODE (decl) == FUNCTION_DECL)
15543 current_function_decl = saved_current_function_decl;
15546 /* Parse the initializer. */
15547 initializer = NULL_TREE;
15548 is_direct_init = false;
15549 is_non_constant_init = true;
15550 if (is_initialized)
15552 if (function_declarator_p (declarator))
15554 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15555 if (initialization_kind == CPP_EQ)
15556 initializer = cp_parser_pure_specifier (parser);
15559 /* If the declaration was erroneous, we don't really
15560 know what the user intended, so just silently
15561 consume the initializer. */
15562 if (decl != error_mark_node)
15563 error_at (initializer_start_token->location,
15564 "initializer provided for function");
15565 cp_parser_skip_to_closing_parenthesis (parser,
15566 /*recovering=*/true,
15567 /*or_comma=*/false,
15568 /*consume_paren=*/true);
15573 /* We want to record the extra mangling scope for in-class
15574 initializers of class members and initializers of static data
15575 member templates. The former is a C++0x feature which isn't
15576 implemented yet, and I expect it will involve deferring
15577 parsing of the initializer until end of class as with default
15578 arguments. So right here we only handle the latter. */
15579 if (!member_p && processing_template_decl)
15580 start_lambda_scope (decl);
15581 initializer = cp_parser_initializer (parser,
15583 &is_non_constant_init);
15584 if (!member_p && processing_template_decl)
15585 finish_lambda_scope ();
15589 /* The old parser allows attributes to appear after a parenthesized
15590 initializer. Mark Mitchell proposed removing this functionality
15591 on the GCC mailing lists on 2002-08-13. This parser accepts the
15592 attributes -- but ignores them. */
15593 if (cp_parser_allow_gnu_extensions_p (parser)
15594 && initialization_kind == CPP_OPEN_PAREN)
15595 if (cp_parser_attributes_opt (parser))
15596 warning (OPT_Wattributes,
15597 "attributes after parenthesized initializer ignored");
15599 /* For an in-class declaration, use `grokfield' to create the
15605 pop_scope (pushed_scope);
15606 pushed_scope = NULL_TREE;
15608 decl = grokfield (declarator, decl_specifiers,
15609 initializer, !is_non_constant_init,
15610 /*asmspec=*/NULL_TREE,
15611 prefix_attributes);
15612 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15613 cp_parser_save_default_args (parser, decl);
15616 /* Finish processing the declaration. But, skip member
15618 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15620 cp_finish_decl (decl,
15621 initializer, !is_non_constant_init,
15623 /* If the initializer is in parentheses, then this is
15624 a direct-initialization, which means that an
15625 `explicit' constructor is OK. Otherwise, an
15626 `explicit' constructor cannot be used. */
15627 ((is_direct_init || !is_initialized)
15628 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15630 else if ((cxx_dialect != cxx98) && friend_p
15631 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15632 /* Core issue #226 (C++0x only): A default template-argument
15633 shall not be specified in a friend class template
15635 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15636 /*is_partial=*/0, /*is_friend_decl=*/1);
15638 if (!friend_p && pushed_scope)
15639 pop_scope (pushed_scope);
15644 /* Parse a declarator.
15648 ptr-operator declarator
15650 abstract-declarator:
15651 ptr-operator abstract-declarator [opt]
15652 direct-abstract-declarator
15657 attributes [opt] direct-declarator
15658 attributes [opt] ptr-operator declarator
15660 abstract-declarator:
15661 attributes [opt] ptr-operator abstract-declarator [opt]
15662 attributes [opt] direct-abstract-declarator
15664 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15665 detect constructor, destructor or conversion operators. It is set
15666 to -1 if the declarator is a name, and +1 if it is a
15667 function. Otherwise it is set to zero. Usually you just want to
15668 test for >0, but internally the negative value is used.
15670 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15671 a decl-specifier-seq unless it declares a constructor, destructor,
15672 or conversion. It might seem that we could check this condition in
15673 semantic analysis, rather than parsing, but that makes it difficult
15674 to handle something like `f()'. We want to notice that there are
15675 no decl-specifiers, and therefore realize that this is an
15676 expression, not a declaration.)
15678 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15679 the declarator is a direct-declarator of the form "(...)".
15681 MEMBER_P is true iff this declarator is a member-declarator. */
15683 static cp_declarator *
15684 cp_parser_declarator (cp_parser* parser,
15685 cp_parser_declarator_kind dcl_kind,
15686 int* ctor_dtor_or_conv_p,
15687 bool* parenthesized_p,
15690 cp_declarator *declarator;
15691 enum tree_code code;
15692 cp_cv_quals cv_quals;
15694 tree attributes = NULL_TREE;
15696 /* Assume this is not a constructor, destructor, or type-conversion
15698 if (ctor_dtor_or_conv_p)
15699 *ctor_dtor_or_conv_p = 0;
15701 if (cp_parser_allow_gnu_extensions_p (parser))
15702 attributes = cp_parser_attributes_opt (parser);
15704 /* Check for the ptr-operator production. */
15705 cp_parser_parse_tentatively (parser);
15706 /* Parse the ptr-operator. */
15707 code = cp_parser_ptr_operator (parser,
15710 /* If that worked, then we have a ptr-operator. */
15711 if (cp_parser_parse_definitely (parser))
15713 /* If a ptr-operator was found, then this declarator was not
15715 if (parenthesized_p)
15716 *parenthesized_p = true;
15717 /* The dependent declarator is optional if we are parsing an
15718 abstract-declarator. */
15719 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15720 cp_parser_parse_tentatively (parser);
15722 /* Parse the dependent declarator. */
15723 declarator = cp_parser_declarator (parser, dcl_kind,
15724 /*ctor_dtor_or_conv_p=*/NULL,
15725 /*parenthesized_p=*/NULL,
15726 /*member_p=*/false);
15728 /* If we are parsing an abstract-declarator, we must handle the
15729 case where the dependent declarator is absent. */
15730 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15731 && !cp_parser_parse_definitely (parser))
15734 declarator = cp_parser_make_indirect_declarator
15735 (code, class_type, cv_quals, declarator);
15737 /* Everything else is a direct-declarator. */
15740 if (parenthesized_p)
15741 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15743 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15744 ctor_dtor_or_conv_p,
15748 if (attributes && declarator && declarator != cp_error_declarator)
15749 declarator->attributes = attributes;
15754 /* Parse a direct-declarator or direct-abstract-declarator.
15758 direct-declarator ( parameter-declaration-clause )
15759 cv-qualifier-seq [opt]
15760 exception-specification [opt]
15761 direct-declarator [ constant-expression [opt] ]
15764 direct-abstract-declarator:
15765 direct-abstract-declarator [opt]
15766 ( parameter-declaration-clause )
15767 cv-qualifier-seq [opt]
15768 exception-specification [opt]
15769 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15770 ( abstract-declarator )
15772 Returns a representation of the declarator. DCL_KIND is
15773 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15774 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15775 we are parsing a direct-declarator. It is
15776 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15777 of ambiguity we prefer an abstract declarator, as per
15778 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15779 cp_parser_declarator. */
15781 static cp_declarator *
15782 cp_parser_direct_declarator (cp_parser* parser,
15783 cp_parser_declarator_kind dcl_kind,
15784 int* ctor_dtor_or_conv_p,
15788 cp_declarator *declarator = NULL;
15789 tree scope = NULL_TREE;
15790 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15791 bool saved_in_declarator_p = parser->in_declarator_p;
15793 tree pushed_scope = NULL_TREE;
15797 /* Peek at the next token. */
15798 token = cp_lexer_peek_token (parser->lexer);
15799 if (token->type == CPP_OPEN_PAREN)
15801 /* This is either a parameter-declaration-clause, or a
15802 parenthesized declarator. When we know we are parsing a
15803 named declarator, it must be a parenthesized declarator
15804 if FIRST is true. For instance, `(int)' is a
15805 parameter-declaration-clause, with an omitted
15806 direct-abstract-declarator. But `((*))', is a
15807 parenthesized abstract declarator. Finally, when T is a
15808 template parameter `(T)' is a
15809 parameter-declaration-clause, and not a parenthesized
15812 We first try and parse a parameter-declaration-clause,
15813 and then try a nested declarator (if FIRST is true).
15815 It is not an error for it not to be a
15816 parameter-declaration-clause, even when FIRST is
15822 The first is the declaration of a function while the
15823 second is the definition of a variable, including its
15826 Having seen only the parenthesis, we cannot know which of
15827 these two alternatives should be selected. Even more
15828 complex are examples like:
15833 The former is a function-declaration; the latter is a
15834 variable initialization.
15836 Thus again, we try a parameter-declaration-clause, and if
15837 that fails, we back out and return. */
15839 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15842 unsigned saved_num_template_parameter_lists;
15843 bool is_declarator = false;
15846 /* In a member-declarator, the only valid interpretation
15847 of a parenthesis is the start of a
15848 parameter-declaration-clause. (It is invalid to
15849 initialize a static data member with a parenthesized
15850 initializer; only the "=" form of initialization is
15853 cp_parser_parse_tentatively (parser);
15855 /* Consume the `('. */
15856 cp_lexer_consume_token (parser->lexer);
15859 /* If this is going to be an abstract declarator, we're
15860 in a declarator and we can't have default args. */
15861 parser->default_arg_ok_p = false;
15862 parser->in_declarator_p = true;
15865 /* Inside the function parameter list, surrounding
15866 template-parameter-lists do not apply. */
15867 saved_num_template_parameter_lists
15868 = parser->num_template_parameter_lists;
15869 parser->num_template_parameter_lists = 0;
15871 begin_scope (sk_function_parms, NULL_TREE);
15873 /* Parse the parameter-declaration-clause. */
15874 params = cp_parser_parameter_declaration_clause (parser);
15876 parser->num_template_parameter_lists
15877 = saved_num_template_parameter_lists;
15879 /* Consume the `)'. */
15880 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15882 /* If all went well, parse the cv-qualifier-seq and the
15883 exception-specification. */
15884 if (member_p || cp_parser_parse_definitely (parser))
15886 cp_cv_quals cv_quals;
15887 cp_virt_specifiers virt_specifiers;
15888 tree exception_specification;
15891 is_declarator = true;
15893 if (ctor_dtor_or_conv_p)
15894 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15897 /* Parse the cv-qualifier-seq. */
15898 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15899 /* And the exception-specification. */
15900 exception_specification
15901 = cp_parser_exception_specification_opt (parser);
15902 /* Parse the virt-specifier-seq. */
15903 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15905 late_return = (cp_parser_late_return_type_opt
15906 (parser, member_p ? cv_quals : -1));
15908 /* Create the function-declarator. */
15909 declarator = make_call_declarator (declarator,
15913 exception_specification,
15915 /* Any subsequent parameter lists are to do with
15916 return type, so are not those of the declared
15918 parser->default_arg_ok_p = false;
15921 /* Remove the function parms from scope. */
15922 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15923 pop_binding (DECL_NAME (t), t);
15927 /* Repeat the main loop. */
15931 /* If this is the first, we can try a parenthesized
15935 bool saved_in_type_id_in_expr_p;
15937 parser->default_arg_ok_p = saved_default_arg_ok_p;
15938 parser->in_declarator_p = saved_in_declarator_p;
15940 /* Consume the `('. */
15941 cp_lexer_consume_token (parser->lexer);
15942 /* Parse the nested declarator. */
15943 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15944 parser->in_type_id_in_expr_p = true;
15946 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15947 /*parenthesized_p=*/NULL,
15949 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15951 /* Expect a `)'. */
15952 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15953 declarator = cp_error_declarator;
15954 if (declarator == cp_error_declarator)
15957 goto handle_declarator;
15959 /* Otherwise, we must be done. */
15963 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15964 && token->type == CPP_OPEN_SQUARE)
15966 /* Parse an array-declarator. */
15969 if (ctor_dtor_or_conv_p)
15970 *ctor_dtor_or_conv_p = 0;
15973 parser->default_arg_ok_p = false;
15974 parser->in_declarator_p = true;
15975 /* Consume the `['. */
15976 cp_lexer_consume_token (parser->lexer);
15977 /* Peek at the next token. */
15978 token = cp_lexer_peek_token (parser->lexer);
15979 /* If the next token is `]', then there is no
15980 constant-expression. */
15981 if (token->type != CPP_CLOSE_SQUARE)
15983 bool non_constant_p;
15986 = cp_parser_constant_expression (parser,
15987 /*allow_non_constant=*/true,
15989 if (!non_constant_p)
15991 /* Normally, the array bound must be an integral constant
15992 expression. However, as an extension, we allow VLAs
15993 in function scopes as long as they aren't part of a
15994 parameter declaration. */
15995 else if (!parser->in_function_body
15996 || current_binding_level->kind == sk_function_parms)
15998 cp_parser_error (parser,
15999 "array bound is not an integer constant");
16000 bounds = error_mark_node;
16002 else if (processing_template_decl && !error_operand_p (bounds))
16004 /* Remember this wasn't a constant-expression. */
16005 bounds = build_nop (TREE_TYPE (bounds), bounds);
16006 TREE_SIDE_EFFECTS (bounds) = 1;
16010 bounds = NULL_TREE;
16011 /* Look for the closing `]'. */
16012 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16014 declarator = cp_error_declarator;
16018 declarator = make_array_declarator (declarator, bounds);
16020 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16023 tree qualifying_scope;
16024 tree unqualified_name;
16025 special_function_kind sfk;
16027 bool pack_expansion_p = false;
16028 cp_token *declarator_id_start_token;
16030 /* Parse a declarator-id */
16031 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16034 cp_parser_parse_tentatively (parser);
16036 /* If we see an ellipsis, we should be looking at a
16038 if (token->type == CPP_ELLIPSIS)
16040 /* Consume the `...' */
16041 cp_lexer_consume_token (parser->lexer);
16043 pack_expansion_p = true;
16047 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16049 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16050 qualifying_scope = parser->scope;
16055 if (!unqualified_name && pack_expansion_p)
16057 /* Check whether an error occurred. */
16058 okay = !cp_parser_error_occurred (parser);
16060 /* We already consumed the ellipsis to mark a
16061 parameter pack, but we have no way to report it,
16062 so abort the tentative parse. We will be exiting
16063 immediately anyway. */
16064 cp_parser_abort_tentative_parse (parser);
16067 okay = cp_parser_parse_definitely (parser);
16070 unqualified_name = error_mark_node;
16071 else if (unqualified_name
16072 && (qualifying_scope
16073 || (TREE_CODE (unqualified_name)
16074 != IDENTIFIER_NODE)))
16076 cp_parser_error (parser, "expected unqualified-id");
16077 unqualified_name = error_mark_node;
16081 if (!unqualified_name)
16083 if (unqualified_name == error_mark_node)
16085 declarator = cp_error_declarator;
16086 pack_expansion_p = false;
16087 declarator->parameter_pack_p = false;
16091 if (qualifying_scope && at_namespace_scope_p ()
16092 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16094 /* In the declaration of a member of a template class
16095 outside of the class itself, the SCOPE will sometimes
16096 be a TYPENAME_TYPE. For example, given:
16098 template <typename T>
16099 int S<T>::R::i = 3;
16101 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16102 this context, we must resolve S<T>::R to an ordinary
16103 type, rather than a typename type.
16105 The reason we normally avoid resolving TYPENAME_TYPEs
16106 is that a specialization of `S' might render
16107 `S<T>::R' not a type. However, if `S' is
16108 specialized, then this `i' will not be used, so there
16109 is no harm in resolving the types here. */
16112 /* Resolve the TYPENAME_TYPE. */
16113 type = resolve_typename_type (qualifying_scope,
16114 /*only_current_p=*/false);
16115 /* If that failed, the declarator is invalid. */
16116 if (TREE_CODE (type) == TYPENAME_TYPE)
16118 if (typedef_variant_p (type))
16119 error_at (declarator_id_start_token->location,
16120 "cannot define member of dependent typedef "
16123 error_at (declarator_id_start_token->location,
16124 "%<%T::%E%> is not a type",
16125 TYPE_CONTEXT (qualifying_scope),
16126 TYPE_IDENTIFIER (qualifying_scope));
16128 qualifying_scope = type;
16133 if (unqualified_name)
16137 if (qualifying_scope
16138 && CLASS_TYPE_P (qualifying_scope))
16139 class_type = qualifying_scope;
16141 class_type = current_class_type;
16143 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16145 tree name_type = TREE_TYPE (unqualified_name);
16146 if (class_type && same_type_p (name_type, class_type))
16148 if (qualifying_scope
16149 && CLASSTYPE_USE_TEMPLATE (name_type))
16151 error_at (declarator_id_start_token->location,
16152 "invalid use of constructor as a template");
16153 inform (declarator_id_start_token->location,
16154 "use %<%T::%D%> instead of %<%T::%D%> to "
16155 "name the constructor in a qualified name",
16157 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16158 class_type, name_type);
16159 declarator = cp_error_declarator;
16163 unqualified_name = constructor_name (class_type);
16167 /* We do not attempt to print the declarator
16168 here because we do not have enough
16169 information about its original syntactic
16171 cp_parser_error (parser, "invalid declarator");
16172 declarator = cp_error_declarator;
16179 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16180 sfk = sfk_destructor;
16181 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16182 sfk = sfk_conversion;
16183 else if (/* There's no way to declare a constructor
16184 for an anonymous type, even if the type
16185 got a name for linkage purposes. */
16186 !TYPE_WAS_ANONYMOUS (class_type)
16187 && constructor_name_p (unqualified_name,
16190 unqualified_name = constructor_name (class_type);
16191 sfk = sfk_constructor;
16193 else if (is_overloaded_fn (unqualified_name)
16194 && DECL_CONSTRUCTOR_P (get_first_fn
16195 (unqualified_name)))
16196 sfk = sfk_constructor;
16198 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16199 *ctor_dtor_or_conv_p = -1;
16202 declarator = make_id_declarator (qualifying_scope,
16205 declarator->id_loc = token->location;
16206 declarator->parameter_pack_p = pack_expansion_p;
16208 if (pack_expansion_p)
16209 maybe_warn_variadic_templates ();
16212 handle_declarator:;
16213 scope = get_scope_of_declarator (declarator);
16215 /* Any names that appear after the declarator-id for a
16216 member are looked up in the containing scope. */
16217 pushed_scope = push_scope (scope);
16218 parser->in_declarator_p = true;
16219 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16220 || (declarator && declarator->kind == cdk_id))
16221 /* Default args are only allowed on function
16223 parser->default_arg_ok_p = saved_default_arg_ok_p;
16225 parser->default_arg_ok_p = false;
16234 /* For an abstract declarator, we might wind up with nothing at this
16235 point. That's an error; the declarator is not optional. */
16237 cp_parser_error (parser, "expected declarator");
16239 /* If we entered a scope, we must exit it now. */
16241 pop_scope (pushed_scope);
16243 parser->default_arg_ok_p = saved_default_arg_ok_p;
16244 parser->in_declarator_p = saved_in_declarator_p;
16249 /* Parse a ptr-operator.
16252 * cv-qualifier-seq [opt]
16254 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16259 & cv-qualifier-seq [opt]
16261 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16262 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16263 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16264 filled in with the TYPE containing the member. *CV_QUALS is
16265 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16266 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16267 Note that the tree codes returned by this function have nothing
16268 to do with the types of trees that will be eventually be created
16269 to represent the pointer or reference type being parsed. They are
16270 just constants with suggestive names. */
16271 static enum tree_code
16272 cp_parser_ptr_operator (cp_parser* parser,
16274 cp_cv_quals *cv_quals)
16276 enum tree_code code = ERROR_MARK;
16279 /* Assume that it's not a pointer-to-member. */
16281 /* And that there are no cv-qualifiers. */
16282 *cv_quals = TYPE_UNQUALIFIED;
16284 /* Peek at the next token. */
16285 token = cp_lexer_peek_token (parser->lexer);
16287 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16288 if (token->type == CPP_MULT)
16289 code = INDIRECT_REF;
16290 else if (token->type == CPP_AND)
16292 else if ((cxx_dialect != cxx98) &&
16293 token->type == CPP_AND_AND) /* C++0x only */
16294 code = NON_LVALUE_EXPR;
16296 if (code != ERROR_MARK)
16298 /* Consume the `*', `&' or `&&'. */
16299 cp_lexer_consume_token (parser->lexer);
16301 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16302 `&', if we are allowing GNU extensions. (The only qualifier
16303 that can legally appear after `&' is `restrict', but that is
16304 enforced during semantic analysis. */
16305 if (code == INDIRECT_REF
16306 || cp_parser_allow_gnu_extensions_p (parser))
16307 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16311 /* Try the pointer-to-member case. */
16312 cp_parser_parse_tentatively (parser);
16313 /* Look for the optional `::' operator. */
16314 cp_parser_global_scope_opt (parser,
16315 /*current_scope_valid_p=*/false);
16316 /* Look for the nested-name specifier. */
16317 token = cp_lexer_peek_token (parser->lexer);
16318 cp_parser_nested_name_specifier (parser,
16319 /*typename_keyword_p=*/false,
16320 /*check_dependency_p=*/true,
16322 /*is_declaration=*/false);
16323 /* If we found it, and the next token is a `*', then we are
16324 indeed looking at a pointer-to-member operator. */
16325 if (!cp_parser_error_occurred (parser)
16326 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16328 /* Indicate that the `*' operator was used. */
16329 code = INDIRECT_REF;
16331 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16332 error_at (token->location, "%qD is a namespace", parser->scope);
16335 /* The type of which the member is a member is given by the
16337 *type = parser->scope;
16338 /* The next name will not be qualified. */
16339 parser->scope = NULL_TREE;
16340 parser->qualifying_scope = NULL_TREE;
16341 parser->object_scope = NULL_TREE;
16342 /* Look for the optional cv-qualifier-seq. */
16343 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16346 /* If that didn't work we don't have a ptr-operator. */
16347 if (!cp_parser_parse_definitely (parser))
16348 cp_parser_error (parser, "expected ptr-operator");
16354 /* Parse an (optional) cv-qualifier-seq.
16357 cv-qualifier cv-qualifier-seq [opt]
16368 Returns a bitmask representing the cv-qualifiers. */
16371 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16373 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16378 cp_cv_quals cv_qualifier;
16380 /* Peek at the next token. */
16381 token = cp_lexer_peek_token (parser->lexer);
16382 /* See if it's a cv-qualifier. */
16383 switch (token->keyword)
16386 cv_qualifier = TYPE_QUAL_CONST;
16390 cv_qualifier = TYPE_QUAL_VOLATILE;
16394 cv_qualifier = TYPE_QUAL_RESTRICT;
16398 cv_qualifier = TYPE_UNQUALIFIED;
16405 if (cv_quals & cv_qualifier)
16407 error_at (token->location, "duplicate cv-qualifier");
16408 cp_lexer_purge_token (parser->lexer);
16412 cp_lexer_consume_token (parser->lexer);
16413 cv_quals |= cv_qualifier;
16420 /* Parse an (optional) virt-specifier-seq.
16422 virt-specifier-seq:
16423 virt-specifier virt-specifier-seq [opt]
16429 Returns a bitmask representing the virt-specifiers. */
16431 static cp_virt_specifiers
16432 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16434 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16439 cp_virt_specifiers virt_specifier;
16441 /* Peek at the next token. */
16442 token = cp_lexer_peek_token (parser->lexer);
16443 /* See if it's a virt-specifier-qualifier. */
16444 if (token->type != CPP_NAME)
16446 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16448 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16449 virt_specifier = VIRT_SPEC_OVERRIDE;
16451 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16453 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16454 virt_specifier = VIRT_SPEC_FINAL;
16456 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16458 virt_specifier = VIRT_SPEC_FINAL;
16463 if (virt_specifiers & virt_specifier)
16465 error_at (token->location, "duplicate virt-specifier");
16466 cp_lexer_purge_token (parser->lexer);
16470 cp_lexer_consume_token (parser->lexer);
16471 virt_specifiers |= virt_specifier;
16474 return virt_specifiers;
16477 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16478 is in scope even though it isn't real. */
16481 inject_this_parameter (tree ctype, cp_cv_quals quals)
16485 if (current_class_ptr)
16487 /* We don't clear this between NSDMIs. Is it already what we want? */
16488 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16489 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16490 && cp_type_quals (type) == quals)
16494 this_parm = build_this_parm (ctype, quals);
16495 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16496 current_class_ptr = NULL_TREE;
16498 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16499 current_class_ptr = this_parm;
16502 /* Parse a late-specified return type, if any. This is not a separate
16503 non-terminal, but part of a function declarator, which looks like
16505 -> trailing-type-specifier-seq abstract-declarator(opt)
16507 Returns the type indicated by the type-id.
16509 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16513 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16518 /* Peek at the next token. */
16519 token = cp_lexer_peek_token (parser->lexer);
16520 /* A late-specified return type is indicated by an initial '->'. */
16521 if (token->type != CPP_DEREF)
16524 /* Consume the ->. */
16525 cp_lexer_consume_token (parser->lexer);
16529 /* DR 1207: 'this' is in scope in the trailing return type. */
16530 gcc_assert (current_class_ptr == NULL_TREE);
16531 inject_this_parameter (current_class_type, quals);
16534 type = cp_parser_trailing_type_id (parser);
16537 current_class_ptr = current_class_ref = NULL_TREE;
16542 /* Parse a declarator-id.
16546 :: [opt] nested-name-specifier [opt] type-name
16548 In the `id-expression' case, the value returned is as for
16549 cp_parser_id_expression if the id-expression was an unqualified-id.
16550 If the id-expression was a qualified-id, then a SCOPE_REF is
16551 returned. The first operand is the scope (either a NAMESPACE_DECL
16552 or TREE_TYPE), but the second is still just a representation of an
16556 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16559 /* The expression must be an id-expression. Assume that qualified
16560 names are the names of types so that:
16563 int S<T>::R::i = 3;
16565 will work; we must treat `S<T>::R' as the name of a type.
16566 Similarly, assume that qualified names are templates, where
16570 int S<T>::R<T>::i = 3;
16573 id = cp_parser_id_expression (parser,
16574 /*template_keyword_p=*/false,
16575 /*check_dependency_p=*/false,
16576 /*template_p=*/NULL,
16577 /*declarator_p=*/true,
16579 if (id && BASELINK_P (id))
16580 id = BASELINK_FUNCTIONS (id);
16584 /* Parse a type-id.
16587 type-specifier-seq abstract-declarator [opt]
16589 Returns the TYPE specified. */
16592 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16593 bool is_trailing_return)
16595 cp_decl_specifier_seq type_specifier_seq;
16596 cp_declarator *abstract_declarator;
16598 /* Parse the type-specifier-seq. */
16599 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16600 is_trailing_return,
16601 &type_specifier_seq);
16602 if (type_specifier_seq.type == error_mark_node)
16603 return error_mark_node;
16605 /* There might or might not be an abstract declarator. */
16606 cp_parser_parse_tentatively (parser);
16607 /* Look for the declarator. */
16608 abstract_declarator
16609 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16610 /*parenthesized_p=*/NULL,
16611 /*member_p=*/false);
16612 /* Check to see if there really was a declarator. */
16613 if (!cp_parser_parse_definitely (parser))
16614 abstract_declarator = NULL;
16616 if (type_specifier_seq.type
16617 && type_uses_auto (type_specifier_seq.type))
16619 /* A type-id with type 'auto' is only ok if the abstract declarator
16620 is a function declarator with a late-specified return type. */
16621 if (abstract_declarator
16622 && abstract_declarator->kind == cdk_function
16623 && abstract_declarator->u.function.late_return_type)
16627 error ("invalid use of %<auto%>");
16628 return error_mark_node;
16632 return groktypename (&type_specifier_seq, abstract_declarator,
16636 static tree cp_parser_type_id (cp_parser *parser)
16638 return cp_parser_type_id_1 (parser, false, false);
16641 static tree cp_parser_template_type_arg (cp_parser *parser)
16644 const char *saved_message = parser->type_definition_forbidden_message;
16645 parser->type_definition_forbidden_message
16646 = G_("types may not be defined in template arguments");
16647 r = cp_parser_type_id_1 (parser, true, false);
16648 parser->type_definition_forbidden_message = saved_message;
16652 static tree cp_parser_trailing_type_id (cp_parser *parser)
16654 return cp_parser_type_id_1 (parser, false, true);
16657 /* Parse a type-specifier-seq.
16659 type-specifier-seq:
16660 type-specifier type-specifier-seq [opt]
16664 type-specifier-seq:
16665 attributes type-specifier-seq [opt]
16667 If IS_DECLARATION is true, we are at the start of a "condition" or
16668 exception-declaration, so we might be followed by a declarator-id.
16670 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16671 i.e. we've just seen "->".
16673 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16676 cp_parser_type_specifier_seq (cp_parser* parser,
16677 bool is_declaration,
16678 bool is_trailing_return,
16679 cp_decl_specifier_seq *type_specifier_seq)
16681 bool seen_type_specifier = false;
16682 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16683 cp_token *start_token = NULL;
16685 /* Clear the TYPE_SPECIFIER_SEQ. */
16686 clear_decl_specs (type_specifier_seq);
16688 /* In the context of a trailing return type, enum E { } is an
16689 elaborated-type-specifier followed by a function-body, not an
16691 if (is_trailing_return)
16692 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16694 /* Parse the type-specifiers and attributes. */
16697 tree type_specifier;
16698 bool is_cv_qualifier;
16700 /* Check for attributes first. */
16701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16703 type_specifier_seq->attributes =
16704 chainon (type_specifier_seq->attributes,
16705 cp_parser_attributes_opt (parser));
16709 /* record the token of the beginning of the type specifier seq,
16710 for error reporting purposes*/
16712 start_token = cp_lexer_peek_token (parser->lexer);
16714 /* Look for the type-specifier. */
16715 type_specifier = cp_parser_type_specifier (parser,
16717 type_specifier_seq,
16718 /*is_declaration=*/false,
16721 if (!type_specifier)
16723 /* If the first type-specifier could not be found, this is not a
16724 type-specifier-seq at all. */
16725 if (!seen_type_specifier)
16727 cp_parser_error (parser, "expected type-specifier");
16728 type_specifier_seq->type = error_mark_node;
16731 /* If subsequent type-specifiers could not be found, the
16732 type-specifier-seq is complete. */
16736 seen_type_specifier = true;
16737 /* The standard says that a condition can be:
16739 type-specifier-seq declarator = assignment-expression
16746 we should treat the "S" as a declarator, not as a
16747 type-specifier. The standard doesn't say that explicitly for
16748 type-specifier-seq, but it does say that for
16749 decl-specifier-seq in an ordinary declaration. Perhaps it
16750 would be clearer just to allow a decl-specifier-seq here, and
16751 then add a semantic restriction that if any decl-specifiers
16752 that are not type-specifiers appear, the program is invalid. */
16753 if (is_declaration && !is_cv_qualifier)
16754 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16757 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16760 /* Parse a parameter-declaration-clause.
16762 parameter-declaration-clause:
16763 parameter-declaration-list [opt] ... [opt]
16764 parameter-declaration-list , ...
16766 Returns a representation for the parameter declarations. A return
16767 value of NULL indicates a parameter-declaration-clause consisting
16768 only of an ellipsis. */
16771 cp_parser_parameter_declaration_clause (cp_parser* parser)
16778 /* Peek at the next token. */
16779 token = cp_lexer_peek_token (parser->lexer);
16780 /* Check for trivial parameter-declaration-clauses. */
16781 if (token->type == CPP_ELLIPSIS)
16783 /* Consume the `...' token. */
16784 cp_lexer_consume_token (parser->lexer);
16787 else if (token->type == CPP_CLOSE_PAREN)
16788 /* There are no parameters. */
16790 #ifndef NO_IMPLICIT_EXTERN_C
16791 if (in_system_header && current_class_type == NULL
16792 && current_lang_name == lang_name_c)
16796 return void_list_node;
16798 /* Check for `(void)', too, which is a special case. */
16799 else if (token->keyword == RID_VOID
16800 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16801 == CPP_CLOSE_PAREN))
16803 /* Consume the `void' token. */
16804 cp_lexer_consume_token (parser->lexer);
16805 /* There are no parameters. */
16806 return void_list_node;
16809 /* Parse the parameter-declaration-list. */
16810 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16811 /* If a parse error occurred while parsing the
16812 parameter-declaration-list, then the entire
16813 parameter-declaration-clause is erroneous. */
16817 /* Peek at the next token. */
16818 token = cp_lexer_peek_token (parser->lexer);
16819 /* If it's a `,', the clause should terminate with an ellipsis. */
16820 if (token->type == CPP_COMMA)
16822 /* Consume the `,'. */
16823 cp_lexer_consume_token (parser->lexer);
16824 /* Expect an ellipsis. */
16826 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16828 /* It might also be `...' if the optional trailing `,' was
16830 else if (token->type == CPP_ELLIPSIS)
16832 /* Consume the `...' token. */
16833 cp_lexer_consume_token (parser->lexer);
16834 /* And remember that we saw it. */
16838 ellipsis_p = false;
16840 /* Finish the parameter list. */
16842 parameters = chainon (parameters, void_list_node);
16847 /* Parse a parameter-declaration-list.
16849 parameter-declaration-list:
16850 parameter-declaration
16851 parameter-declaration-list , parameter-declaration
16853 Returns a representation of the parameter-declaration-list, as for
16854 cp_parser_parameter_declaration_clause. However, the
16855 `void_list_node' is never appended to the list. Upon return,
16856 *IS_ERROR will be true iff an error occurred. */
16859 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16861 tree parameters = NULL_TREE;
16862 tree *tail = ¶meters;
16863 bool saved_in_unbraced_linkage_specification_p;
16866 /* Assume all will go well. */
16868 /* The special considerations that apply to a function within an
16869 unbraced linkage specifications do not apply to the parameters
16870 to the function. */
16871 saved_in_unbraced_linkage_specification_p
16872 = parser->in_unbraced_linkage_specification_p;
16873 parser->in_unbraced_linkage_specification_p = false;
16875 /* Look for more parameters. */
16878 cp_parameter_declarator *parameter;
16879 tree decl = error_mark_node;
16880 bool parenthesized_p = false;
16881 /* Parse the parameter. */
16883 = cp_parser_parameter_declaration (parser,
16884 /*template_parm_p=*/false,
16887 /* We don't know yet if the enclosing context is deprecated, so wait
16888 and warn in grokparms if appropriate. */
16889 deprecated_state = DEPRECATED_SUPPRESS;
16892 decl = grokdeclarator (parameter->declarator,
16893 ¶meter->decl_specifiers,
16895 parameter->default_argument != NULL_TREE,
16896 ¶meter->decl_specifiers.attributes);
16898 deprecated_state = DEPRECATED_NORMAL;
16900 /* If a parse error occurred parsing the parameter declaration,
16901 then the entire parameter-declaration-list is erroneous. */
16902 if (decl == error_mark_node)
16905 parameters = error_mark_node;
16909 if (parameter->decl_specifiers.attributes)
16910 cplus_decl_attributes (&decl,
16911 parameter->decl_specifiers.attributes,
16913 if (DECL_NAME (decl))
16914 decl = pushdecl (decl);
16916 if (decl != error_mark_node)
16918 retrofit_lang_decl (decl);
16919 DECL_PARM_INDEX (decl) = ++index;
16920 DECL_PARM_LEVEL (decl) = function_parm_depth ();
16923 /* Add the new parameter to the list. */
16924 *tail = build_tree_list (parameter->default_argument, decl);
16925 tail = &TREE_CHAIN (*tail);
16927 /* Peek at the next token. */
16928 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16929 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16930 /* These are for Objective-C++ */
16931 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16932 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16933 /* The parameter-declaration-list is complete. */
16935 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16939 /* Peek at the next token. */
16940 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16941 /* If it's an ellipsis, then the list is complete. */
16942 if (token->type == CPP_ELLIPSIS)
16944 /* Otherwise, there must be more parameters. Consume the
16946 cp_lexer_consume_token (parser->lexer);
16947 /* When parsing something like:
16949 int i(float f, double d)
16951 we can tell after seeing the declaration for "f" that we
16952 are not looking at an initialization of a variable "i",
16953 but rather at the declaration of a function "i".
16955 Due to the fact that the parsing of template arguments
16956 (as specified to a template-id) requires backtracking we
16957 cannot use this technique when inside a template argument
16959 if (!parser->in_template_argument_list_p
16960 && !parser->in_type_id_in_expr_p
16961 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16962 /* However, a parameter-declaration of the form
16963 "foat(f)" (which is a valid declaration of a
16964 parameter "f") can also be interpreted as an
16965 expression (the conversion of "f" to "float"). */
16966 && !parenthesized_p)
16967 cp_parser_commit_to_tentative_parse (parser);
16971 cp_parser_error (parser, "expected %<,%> or %<...%>");
16972 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16973 cp_parser_skip_to_closing_parenthesis (parser,
16974 /*recovering=*/true,
16975 /*or_comma=*/false,
16976 /*consume_paren=*/false);
16981 parser->in_unbraced_linkage_specification_p
16982 = saved_in_unbraced_linkage_specification_p;
16987 /* Parse a parameter declaration.
16989 parameter-declaration:
16990 decl-specifier-seq ... [opt] declarator
16991 decl-specifier-seq declarator = assignment-expression
16992 decl-specifier-seq ... [opt] abstract-declarator [opt]
16993 decl-specifier-seq abstract-declarator [opt] = assignment-expression
16995 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16996 declares a template parameter. (In that case, a non-nested `>'
16997 token encountered during the parsing of the assignment-expression
16998 is not interpreted as a greater-than operator.)
17000 Returns a representation of the parameter, or NULL if an error
17001 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17002 true iff the declarator is of the form "(p)". */
17004 static cp_parameter_declarator *
17005 cp_parser_parameter_declaration (cp_parser *parser,
17006 bool template_parm_p,
17007 bool *parenthesized_p)
17009 int declares_class_or_enum;
17010 cp_decl_specifier_seq decl_specifiers;
17011 cp_declarator *declarator;
17012 tree default_argument;
17013 cp_token *token = NULL, *declarator_token_start = NULL;
17014 const char *saved_message;
17016 /* In a template parameter, `>' is not an operator.
17020 When parsing a default template-argument for a non-type
17021 template-parameter, the first non-nested `>' is taken as the end
17022 of the template parameter-list rather than a greater-than
17025 /* Type definitions may not appear in parameter types. */
17026 saved_message = parser->type_definition_forbidden_message;
17027 parser->type_definition_forbidden_message
17028 = G_("types may not be defined in parameter types");
17030 /* Parse the declaration-specifiers. */
17031 cp_parser_decl_specifier_seq (parser,
17032 CP_PARSER_FLAGS_NONE,
17034 &declares_class_or_enum);
17036 /* Complain about missing 'typename' or other invalid type names. */
17037 if (!decl_specifiers.any_type_specifiers_p)
17038 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17040 /* If an error occurred, there's no reason to attempt to parse the
17041 rest of the declaration. */
17042 if (cp_parser_error_occurred (parser))
17044 parser->type_definition_forbidden_message = saved_message;
17048 /* Peek at the next token. */
17049 token = cp_lexer_peek_token (parser->lexer);
17051 /* If the next token is a `)', `,', `=', `>', or `...', then there
17052 is no declarator. However, when variadic templates are enabled,
17053 there may be a declarator following `...'. */
17054 if (token->type == CPP_CLOSE_PAREN
17055 || token->type == CPP_COMMA
17056 || token->type == CPP_EQ
17057 || token->type == CPP_GREATER)
17060 if (parenthesized_p)
17061 *parenthesized_p = false;
17063 /* Otherwise, there should be a declarator. */
17066 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17067 parser->default_arg_ok_p = false;
17069 /* After seeing a decl-specifier-seq, if the next token is not a
17070 "(", there is no possibility that the code is a valid
17071 expression. Therefore, if parsing tentatively, we commit at
17073 if (!parser->in_template_argument_list_p
17074 /* In an expression context, having seen:
17078 we cannot be sure whether we are looking at a
17079 function-type (taking a "char" as a parameter) or a cast
17080 of some object of type "char" to "int". */
17081 && !parser->in_type_id_in_expr_p
17082 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17083 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17084 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17085 cp_parser_commit_to_tentative_parse (parser);
17086 /* Parse the declarator. */
17087 declarator_token_start = token;
17088 declarator = cp_parser_declarator (parser,
17089 CP_PARSER_DECLARATOR_EITHER,
17090 /*ctor_dtor_or_conv_p=*/NULL,
17092 /*member_p=*/false);
17093 parser->default_arg_ok_p = saved_default_arg_ok_p;
17094 /* After the declarator, allow more attributes. */
17095 decl_specifiers.attributes
17096 = chainon (decl_specifiers.attributes,
17097 cp_parser_attributes_opt (parser));
17100 /* If the next token is an ellipsis, and we have not seen a
17101 declarator name, and the type of the declarator contains parameter
17102 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17103 a parameter pack expansion expression. Otherwise, leave the
17104 ellipsis for a C-style variadic function. */
17105 token = cp_lexer_peek_token (parser->lexer);
17106 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17108 tree type = decl_specifiers.type;
17110 if (type && DECL_P (type))
17111 type = TREE_TYPE (type);
17114 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17115 && declarator_can_be_parameter_pack (declarator)
17116 && (!declarator || !declarator->parameter_pack_p)
17117 && uses_parameter_packs (type))
17119 /* Consume the `...'. */
17120 cp_lexer_consume_token (parser->lexer);
17121 maybe_warn_variadic_templates ();
17123 /* Build a pack expansion type */
17125 declarator->parameter_pack_p = true;
17127 decl_specifiers.type = make_pack_expansion (type);
17131 /* The restriction on defining new types applies only to the type
17132 of the parameter, not to the default argument. */
17133 parser->type_definition_forbidden_message = saved_message;
17135 /* If the next token is `=', then process a default argument. */
17136 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17138 /* If we are defining a class, then the tokens that make up the
17139 default argument must be saved and processed later. */
17140 if (!template_parm_p && at_class_scope_p ()
17141 && TYPE_BEING_DEFINED (current_class_type)
17142 && !LAMBDA_TYPE_P (current_class_type))
17144 unsigned depth = 0;
17145 int maybe_template_id = 0;
17146 cp_token *first_token;
17149 /* Add tokens until we have processed the entire default
17150 argument. We add the range [first_token, token). */
17151 first_token = cp_lexer_peek_token (parser->lexer);
17156 /* Peek at the next token. */
17157 token = cp_lexer_peek_token (parser->lexer);
17158 /* What we do depends on what token we have. */
17159 switch (token->type)
17161 /* In valid code, a default argument must be
17162 immediately followed by a `,' `)', or `...'. */
17164 if (depth == 0 && maybe_template_id)
17166 /* If we've seen a '<', we might be in a
17167 template-argument-list. Until Core issue 325 is
17168 resolved, we don't know how this situation ought
17169 to be handled, so try to DTRT. We check whether
17170 what comes after the comma is a valid parameter
17171 declaration list. If it is, then the comma ends
17172 the default argument; otherwise the default
17173 argument continues. */
17174 bool error = false;
17177 /* Set ITALP so cp_parser_parameter_declaration_list
17178 doesn't decide to commit to this parse. */
17179 bool saved_italp = parser->in_template_argument_list_p;
17180 parser->in_template_argument_list_p = true;
17182 cp_parser_parse_tentatively (parser);
17183 cp_lexer_consume_token (parser->lexer);
17184 begin_scope (sk_function_parms, NULL_TREE);
17185 cp_parser_parameter_declaration_list (parser, &error);
17186 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17187 pop_binding (DECL_NAME (t), t);
17189 if (!cp_parser_error_occurred (parser) && !error)
17191 cp_parser_abort_tentative_parse (parser);
17193 parser->in_template_argument_list_p = saved_italp;
17196 case CPP_CLOSE_PAREN:
17198 /* If we run into a non-nested `;', `}', or `]',
17199 then the code is invalid -- but the default
17200 argument is certainly over. */
17201 case CPP_SEMICOLON:
17202 case CPP_CLOSE_BRACE:
17203 case CPP_CLOSE_SQUARE:
17206 /* Update DEPTH, if necessary. */
17207 else if (token->type == CPP_CLOSE_PAREN
17208 || token->type == CPP_CLOSE_BRACE
17209 || token->type == CPP_CLOSE_SQUARE)
17213 case CPP_OPEN_PAREN:
17214 case CPP_OPEN_SQUARE:
17215 case CPP_OPEN_BRACE:
17221 /* This might be the comparison operator, or it might
17222 start a template argument list. */
17223 ++maybe_template_id;
17227 if (cxx_dialect == cxx98)
17229 /* Fall through for C++0x, which treats the `>>'
17230 operator like two `>' tokens in certain
17236 /* This might be an operator, or it might close a
17237 template argument list. But if a previous '<'
17238 started a template argument list, this will have
17239 closed it, so we can't be in one anymore. */
17240 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17241 if (maybe_template_id < 0)
17242 maybe_template_id = 0;
17246 /* If we run out of tokens, issue an error message. */
17248 case CPP_PRAGMA_EOL:
17249 error_at (token->location, "file ends in default argument");
17255 /* In these cases, we should look for template-ids.
17256 For example, if the default argument is
17257 `X<int, double>()', we need to do name lookup to
17258 figure out whether or not `X' is a template; if
17259 so, the `,' does not end the default argument.
17261 That is not yet done. */
17268 /* If we've reached the end, stop. */
17272 /* Add the token to the token block. */
17273 token = cp_lexer_consume_token (parser->lexer);
17276 /* Create a DEFAULT_ARG to represent the unparsed default
17278 default_argument = make_node (DEFAULT_ARG);
17279 DEFARG_TOKENS (default_argument)
17280 = cp_token_cache_new (first_token, token);
17281 DEFARG_INSTANTIATIONS (default_argument) = NULL;
17283 /* Outside of a class definition, we can just parse the
17284 assignment-expression. */
17287 token = cp_lexer_peek_token (parser->lexer);
17289 = cp_parser_default_argument (parser, template_parm_p);
17292 if (!parser->default_arg_ok_p)
17294 if (flag_permissive)
17295 warning (0, "deprecated use of default argument for parameter of non-function");
17298 error_at (token->location,
17299 "default arguments are only "
17300 "permitted for function parameters");
17301 default_argument = NULL_TREE;
17304 else if ((declarator && declarator->parameter_pack_p)
17305 || (decl_specifiers.type
17306 && PACK_EXPANSION_P (decl_specifiers.type)))
17308 /* Find the name of the parameter pack. */
17309 cp_declarator *id_declarator = declarator;
17310 while (id_declarator && id_declarator->kind != cdk_id)
17311 id_declarator = id_declarator->declarator;
17313 if (id_declarator && id_declarator->kind == cdk_id)
17314 error_at (declarator_token_start->location,
17316 ? G_("template parameter pack %qD "
17317 "cannot have a default argument")
17318 : G_("parameter pack %qD cannot have "
17319 "a default argument"),
17320 id_declarator->u.id.unqualified_name);
17322 error_at (declarator_token_start->location,
17324 ? G_("template parameter pack cannot have "
17325 "a default argument")
17326 : G_("parameter pack cannot have a "
17327 "default argument"));
17329 default_argument = NULL_TREE;
17333 default_argument = NULL_TREE;
17335 return make_parameter_declarator (&decl_specifiers,
17340 /* Parse a default argument and return it.
17342 TEMPLATE_PARM_P is true if this is a default argument for a
17343 non-type template parameter. */
17345 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17347 tree default_argument = NULL_TREE;
17348 bool saved_greater_than_is_operator_p;
17349 bool saved_local_variables_forbidden_p;
17350 bool non_constant_p, is_direct_init;
17352 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17354 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17355 parser->greater_than_is_operator_p = !template_parm_p;
17356 /* Local variable names (and the `this' keyword) may not
17357 appear in a default argument. */
17358 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17359 parser->local_variables_forbidden_p = true;
17360 /* Parse the assignment-expression. */
17361 if (template_parm_p)
17362 push_deferring_access_checks (dk_no_deferred);
17364 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17365 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17366 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17367 if (template_parm_p)
17368 pop_deferring_access_checks ();
17369 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17370 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17372 return default_argument;
17375 /* Parse a function-body.
17378 compound_statement */
17381 cp_parser_function_body (cp_parser *parser)
17383 cp_parser_compound_statement (parser, NULL, false, true);
17386 /* Parse a ctor-initializer-opt followed by a function-body. Return
17387 true if a ctor-initializer was present. */
17390 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17393 bool ctor_initializer_p;
17394 const bool check_body_p =
17395 DECL_CONSTRUCTOR_P (current_function_decl)
17396 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17399 /* Begin the function body. */
17400 body = begin_function_body ();
17401 /* Parse the optional ctor-initializer. */
17402 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17404 /* If we're parsing a constexpr constructor definition, we need
17405 to check that the constructor body is indeed empty. However,
17406 before we get to cp_parser_function_body lot of junk has been
17407 generated, so we can't just check that we have an empty block.
17408 Rather we take a snapshot of the outermost block, and check whether
17409 cp_parser_function_body changed its state. */
17413 if (TREE_CODE (list) == BIND_EXPR)
17414 list = BIND_EXPR_BODY (list);
17415 if (TREE_CODE (list) == STATEMENT_LIST
17416 && STATEMENT_LIST_TAIL (list) != NULL)
17417 last = STATEMENT_LIST_TAIL (list)->stmt;
17419 /* Parse the function-body. */
17420 cp_parser_function_body (parser);
17422 check_constexpr_ctor_body (last, list);
17423 /* Finish the function body. */
17424 finish_function_body (body);
17426 return ctor_initializer_p;
17429 /* Parse an initializer.
17432 = initializer-clause
17433 ( expression-list )
17435 Returns an expression representing the initializer. If no
17436 initializer is present, NULL_TREE is returned.
17438 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17439 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17440 set to TRUE if there is no initializer present. If there is an
17441 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17442 is set to true; otherwise it is set to false. */
17445 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17446 bool* non_constant_p)
17451 /* Peek at the next token. */
17452 token = cp_lexer_peek_token (parser->lexer);
17454 /* Let our caller know whether or not this initializer was
17456 *is_direct_init = (token->type != CPP_EQ);
17457 /* Assume that the initializer is constant. */
17458 *non_constant_p = false;
17460 if (token->type == CPP_EQ)
17462 /* Consume the `='. */
17463 cp_lexer_consume_token (parser->lexer);
17464 /* Parse the initializer-clause. */
17465 init = cp_parser_initializer_clause (parser, non_constant_p);
17467 else if (token->type == CPP_OPEN_PAREN)
17470 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17472 /*allow_expansion_p=*/true,
17475 return error_mark_node;
17476 init = build_tree_list_vec (vec);
17477 release_tree_vector (vec);
17479 else if (token->type == CPP_OPEN_BRACE)
17481 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17482 init = cp_parser_braced_list (parser, non_constant_p);
17483 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17487 /* Anything else is an error. */
17488 cp_parser_error (parser, "expected initializer");
17489 init = error_mark_node;
17495 /* Parse an initializer-clause.
17497 initializer-clause:
17498 assignment-expression
17501 Returns an expression representing the initializer.
17503 If the `assignment-expression' production is used the value
17504 returned is simply a representation for the expression.
17506 Otherwise, calls cp_parser_braced_list. */
17509 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17513 /* Assume the expression is constant. */
17514 *non_constant_p = false;
17516 /* If it is not a `{', then we are looking at an
17517 assignment-expression. */
17518 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17521 = cp_parser_constant_expression (parser,
17522 /*allow_non_constant_p=*/true,
17526 initializer = cp_parser_braced_list (parser, non_constant_p);
17528 return initializer;
17531 /* Parse a brace-enclosed initializer list.
17534 { initializer-list , [opt] }
17537 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17538 the elements of the initializer-list (or NULL, if the last
17539 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17540 NULL_TREE. There is no way to detect whether or not the optional
17541 trailing `,' was provided. NON_CONSTANT_P is as for
17542 cp_parser_initializer. */
17545 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17549 /* Consume the `{' token. */
17550 cp_lexer_consume_token (parser->lexer);
17551 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17552 initializer = make_node (CONSTRUCTOR);
17553 /* If it's not a `}', then there is a non-trivial initializer. */
17554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17556 /* Parse the initializer list. */
17557 CONSTRUCTOR_ELTS (initializer)
17558 = cp_parser_initializer_list (parser, non_constant_p);
17559 /* A trailing `,' token is allowed. */
17560 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17561 cp_lexer_consume_token (parser->lexer);
17563 /* Now, there should be a trailing `}'. */
17564 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17565 TREE_TYPE (initializer) = init_list_type_node;
17566 return initializer;
17569 /* Parse an initializer-list.
17572 initializer-clause ... [opt]
17573 initializer-list , initializer-clause ... [opt]
17578 designation initializer-clause ...[opt]
17579 initializer-list , designation initializer-clause ...[opt]
17584 [ constant-expression ] =
17586 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17587 for the initializer. If the INDEX of the elt is non-NULL, it is the
17588 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17589 as for cp_parser_initializer. */
17591 static VEC(constructor_elt,gc) *
17592 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17594 VEC(constructor_elt,gc) *v = NULL;
17596 /* Assume all of the expressions are constant. */
17597 *non_constant_p = false;
17599 /* Parse the rest of the list. */
17605 bool clause_non_constant_p;
17607 /* If the next token is an identifier and the following one is a
17608 colon, we are looking at the GNU designated-initializer
17610 if (cp_parser_allow_gnu_extensions_p (parser)
17611 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17612 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17614 /* Warn the user that they are using an extension. */
17615 pedwarn (input_location, OPT_pedantic,
17616 "ISO C++ does not allow designated initializers");
17617 /* Consume the identifier. */
17618 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17619 /* Consume the `:'. */
17620 cp_lexer_consume_token (parser->lexer);
17622 /* Also handle the C99 syntax, '. id ='. */
17623 else if (cp_parser_allow_gnu_extensions_p (parser)
17624 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17625 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17626 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17628 /* Warn the user that they are using an extension. */
17629 pedwarn (input_location, OPT_pedantic,
17630 "ISO C++ does not allow C99 designated initializers");
17631 /* Consume the `.'. */
17632 cp_lexer_consume_token (parser->lexer);
17633 /* Consume the identifier. */
17634 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17635 /* Consume the `='. */
17636 cp_lexer_consume_token (parser->lexer);
17638 /* Also handle C99 array designators, '[ const ] ='. */
17639 else if (cp_parser_allow_gnu_extensions_p (parser)
17640 && !c_dialect_objc ()
17641 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17643 /* In C++11, [ could start a lambda-introducer. */
17644 cp_parser_parse_tentatively (parser);
17645 cp_lexer_consume_token (parser->lexer);
17646 designator = cp_parser_constant_expression (parser, false, NULL);
17647 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17648 cp_parser_require (parser, CPP_EQ, RT_EQ);
17649 cp_parser_parse_definitely (parser);
17652 designator = NULL_TREE;
17654 /* Parse the initializer. */
17655 initializer = cp_parser_initializer_clause (parser,
17656 &clause_non_constant_p);
17657 /* If any clause is non-constant, so is the entire initializer. */
17658 if (clause_non_constant_p)
17659 *non_constant_p = true;
17661 /* If we have an ellipsis, this is an initializer pack
17663 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17665 /* Consume the `...'. */
17666 cp_lexer_consume_token (parser->lexer);
17668 /* Turn the initializer into an initializer expansion. */
17669 initializer = make_pack_expansion (initializer);
17672 /* Add it to the vector. */
17673 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17675 /* If the next token is not a comma, we have reached the end of
17677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17680 /* Peek at the next token. */
17681 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17682 /* If the next token is a `}', then we're still done. An
17683 initializer-clause can have a trailing `,' after the
17684 initializer-list and before the closing `}'. */
17685 if (token->type == CPP_CLOSE_BRACE)
17688 /* Consume the `,' token. */
17689 cp_lexer_consume_token (parser->lexer);
17695 /* Classes [gram.class] */
17697 /* Parse a class-name.
17703 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17704 to indicate that names looked up in dependent types should be
17705 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17706 keyword has been used to indicate that the name that appears next
17707 is a template. TAG_TYPE indicates the explicit tag given before
17708 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17709 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17710 is the class being defined in a class-head.
17712 Returns the TYPE_DECL representing the class. */
17715 cp_parser_class_name (cp_parser *parser,
17716 bool typename_keyword_p,
17717 bool template_keyword_p,
17718 enum tag_types tag_type,
17719 bool check_dependency_p,
17721 bool is_declaration)
17727 tree identifier = NULL_TREE;
17729 /* All class-names start with an identifier. */
17730 token = cp_lexer_peek_token (parser->lexer);
17731 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17733 cp_parser_error (parser, "expected class-name");
17734 return error_mark_node;
17737 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17738 to a template-id, so we save it here. */
17739 scope = parser->scope;
17740 if (scope == error_mark_node)
17741 return error_mark_node;
17743 /* Any name names a type if we're following the `typename' keyword
17744 in a qualified name where the enclosing scope is type-dependent. */
17745 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17746 && dependent_type_p (scope));
17747 /* Handle the common case (an identifier, but not a template-id)
17749 if (token->type == CPP_NAME
17750 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17752 cp_token *identifier_token;
17755 /* Look for the identifier. */
17756 identifier_token = cp_lexer_peek_token (parser->lexer);
17757 ambiguous_p = identifier_token->ambiguous_p;
17758 identifier = cp_parser_identifier (parser);
17759 /* If the next token isn't an identifier, we are certainly not
17760 looking at a class-name. */
17761 if (identifier == error_mark_node)
17762 decl = error_mark_node;
17763 /* If we know this is a type-name, there's no need to look it
17765 else if (typename_p)
17769 tree ambiguous_decls;
17770 /* If we already know that this lookup is ambiguous, then
17771 we've already issued an error message; there's no reason
17775 cp_parser_simulate_error (parser);
17776 return error_mark_node;
17778 /* If the next token is a `::', then the name must be a type
17781 [basic.lookup.qual]
17783 During the lookup for a name preceding the :: scope
17784 resolution operator, object, function, and enumerator
17785 names are ignored. */
17786 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17787 tag_type = typename_type;
17788 /* Look up the name. */
17789 decl = cp_parser_lookup_name (parser, identifier,
17791 /*is_template=*/false,
17792 /*is_namespace=*/false,
17793 check_dependency_p,
17795 identifier_token->location);
17796 if (ambiguous_decls)
17798 if (cp_parser_parsing_tentatively (parser))
17799 cp_parser_simulate_error (parser);
17800 return error_mark_node;
17806 /* Try a template-id. */
17807 decl = cp_parser_template_id (parser, template_keyword_p,
17808 check_dependency_p,
17810 if (decl == error_mark_node)
17811 return error_mark_node;
17814 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17816 /* If this is a typename, create a TYPENAME_TYPE. */
17817 if (typename_p && decl != error_mark_node)
17819 decl = make_typename_type (scope, decl, typename_type,
17820 /*complain=*/tf_error);
17821 if (decl != error_mark_node)
17822 decl = TYPE_NAME (decl);
17825 /* Check to see that it is really the name of a class. */
17826 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17827 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17828 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17829 /* Situations like this:
17831 template <typename T> struct A {
17832 typename T::template X<int>::I i;
17835 are problematic. Is `T::template X<int>' a class-name? The
17836 standard does not seem to be definitive, but there is no other
17837 valid interpretation of the following `::'. Therefore, those
17838 names are considered class-names. */
17840 decl = make_typename_type (scope, decl, tag_type, tf_error);
17841 if (decl != error_mark_node)
17842 decl = TYPE_NAME (decl);
17844 else if (TREE_CODE (decl) != TYPE_DECL
17845 || TREE_TYPE (decl) == error_mark_node
17846 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17847 /* In Objective-C 2.0, a classname followed by '.' starts a
17848 dot-syntax expression, and it's not a type-name. */
17849 || (c_dialect_objc ()
17850 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17851 && objc_is_class_name (decl)))
17852 decl = error_mark_node;
17854 if (decl == error_mark_node)
17855 cp_parser_error (parser, "expected class-name");
17856 else if (identifier && !parser->scope)
17857 maybe_note_name_used_in_class (identifier, decl);
17862 /* Parse a class-specifier.
17865 class-head { member-specification [opt] }
17867 Returns the TREE_TYPE representing the class. */
17870 cp_parser_class_specifier_1 (cp_parser* parser)
17873 tree attributes = NULL_TREE;
17874 bool nested_name_specifier_p;
17875 unsigned saved_num_template_parameter_lists;
17876 bool saved_in_function_body;
17877 unsigned char in_statement;
17878 bool in_switch_statement_p;
17879 bool saved_in_unbraced_linkage_specification_p;
17880 tree old_scope = NULL_TREE;
17881 tree scope = NULL_TREE;
17883 cp_token *closing_brace;
17885 push_deferring_access_checks (dk_no_deferred);
17887 /* Parse the class-head. */
17888 type = cp_parser_class_head (parser,
17889 &nested_name_specifier_p,
17892 /* If the class-head was a semantic disaster, skip the entire body
17896 cp_parser_skip_to_end_of_block_or_statement (parser);
17897 pop_deferring_access_checks ();
17898 return error_mark_node;
17901 /* Look for the `{'. */
17902 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17904 pop_deferring_access_checks ();
17905 return error_mark_node;
17908 /* Process the base classes. If they're invalid, skip the
17909 entire class body. */
17910 if (!xref_basetypes (type, bases))
17912 /* Consuming the closing brace yields better error messages
17914 if (cp_parser_skip_to_closing_brace (parser))
17915 cp_lexer_consume_token (parser->lexer);
17916 pop_deferring_access_checks ();
17917 return error_mark_node;
17920 /* Issue an error message if type-definitions are forbidden here. */
17921 cp_parser_check_type_definition (parser);
17922 /* Remember that we are defining one more class. */
17923 ++parser->num_classes_being_defined;
17924 /* Inside the class, surrounding template-parameter-lists do not
17926 saved_num_template_parameter_lists
17927 = parser->num_template_parameter_lists;
17928 parser->num_template_parameter_lists = 0;
17929 /* We are not in a function body. */
17930 saved_in_function_body = parser->in_function_body;
17931 parser->in_function_body = false;
17932 /* Or in a loop. */
17933 in_statement = parser->in_statement;
17934 parser->in_statement = 0;
17935 /* Or in a switch. */
17936 in_switch_statement_p = parser->in_switch_statement_p;
17937 parser->in_switch_statement_p = false;
17938 /* We are not immediately inside an extern "lang" block. */
17939 saved_in_unbraced_linkage_specification_p
17940 = parser->in_unbraced_linkage_specification_p;
17941 parser->in_unbraced_linkage_specification_p = false;
17943 /* Start the class. */
17944 if (nested_name_specifier_p)
17946 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17947 old_scope = push_inner_scope (scope);
17949 type = begin_class_definition (type, attributes);
17951 if (type == error_mark_node)
17952 /* If the type is erroneous, skip the entire body of the class. */
17953 cp_parser_skip_to_closing_brace (parser);
17955 /* Parse the member-specification. */
17956 cp_parser_member_specification_opt (parser);
17958 /* Look for the trailing `}'. */
17959 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17960 /* Look for trailing attributes to apply to this class. */
17961 if (cp_parser_allow_gnu_extensions_p (parser))
17962 attributes = cp_parser_attributes_opt (parser);
17963 if (type != error_mark_node)
17964 type = finish_struct (type, attributes);
17965 if (nested_name_specifier_p)
17966 pop_inner_scope (old_scope, scope);
17968 /* We've finished a type definition. Check for the common syntax
17969 error of forgetting a semicolon after the definition. We need to
17970 be careful, as we can't just check for not-a-semicolon and be done
17971 with it; the user might have typed:
17973 class X { } c = ...;
17974 class X { } *p = ...;
17976 and so forth. Instead, enumerate all the possible tokens that
17977 might follow this production; if we don't see one of them, then
17978 complain and silently insert the semicolon. */
17980 cp_token *token = cp_lexer_peek_token (parser->lexer);
17981 bool want_semicolon = true;
17983 switch (token->type)
17986 case CPP_SEMICOLON:
17989 case CPP_OPEN_PAREN:
17990 case CPP_CLOSE_PAREN:
17992 want_semicolon = false;
17995 /* While it's legal for type qualifiers and storage class
17996 specifiers to follow type definitions in the grammar, only
17997 compiler testsuites contain code like that. Assume that if
17998 we see such code, then what we're really seeing is a case
18002 const <type> var = ...;
18007 static <type> func (...) ...
18009 i.e. the qualifier or specifier applies to the next
18010 declaration. To do so, however, we need to look ahead one
18011 more token to see if *that* token is a type specifier.
18013 This code could be improved to handle:
18016 static const <type> var = ...; */
18018 if (keyword_is_decl_specifier (token->keyword))
18020 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18022 /* Handling user-defined types here would be nice, but very
18025 = (lookahead->type == CPP_KEYWORD
18026 && keyword_begins_type_specifier (lookahead->keyword));
18033 /* If we don't have a type, then something is very wrong and we
18034 shouldn't try to do anything clever. Likewise for not seeing the
18036 if (closing_brace && TYPE_P (type) && want_semicolon)
18038 cp_token_position prev
18039 = cp_lexer_previous_token_position (parser->lexer);
18040 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18041 location_t loc = prev_token->location;
18043 if (CLASSTYPE_DECLARED_CLASS (type))
18044 error_at (loc, "expected %<;%> after class definition");
18045 else if (TREE_CODE (type) == RECORD_TYPE)
18046 error_at (loc, "expected %<;%> after struct definition");
18047 else if (TREE_CODE (type) == UNION_TYPE)
18048 error_at (loc, "expected %<;%> after union definition");
18050 gcc_unreachable ();
18052 /* Unget one token and smash it to look as though we encountered
18053 a semicolon in the input stream. */
18054 cp_lexer_set_token_position (parser->lexer, prev);
18055 token = cp_lexer_peek_token (parser->lexer);
18056 token->type = CPP_SEMICOLON;
18057 token->keyword = RID_MAX;
18061 /* If this class is not itself within the scope of another class,
18062 then we need to parse the bodies of all of the queued function
18063 definitions. Note that the queued functions defined in a class
18064 are not always processed immediately following the
18065 class-specifier for that class. Consider:
18068 struct B { void f() { sizeof (A); } };
18071 If `f' were processed before the processing of `A' were
18072 completed, there would be no way to compute the size of `A'.
18073 Note that the nesting we are interested in here is lexical --
18074 not the semantic nesting given by TYPE_CONTEXT. In particular,
18077 struct A { struct B; };
18078 struct A::B { void f() { } };
18080 there is no need to delay the parsing of `A::B::f'. */
18081 if (--parser->num_classes_being_defined == 0)
18084 tree class_type = NULL_TREE;
18085 tree pushed_scope = NULL_TREE;
18087 cp_default_arg_entry *e;
18088 tree save_ccp, save_ccr;
18090 /* In a first pass, parse default arguments to the functions.
18091 Then, in a second pass, parse the bodies of the functions.
18092 This two-phased approach handles cases like:
18100 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18104 /* If there are default arguments that have not yet been processed,
18105 take care of them now. */
18106 if (class_type != e->class_type)
18109 pop_scope (pushed_scope);
18110 class_type = e->class_type;
18111 pushed_scope = push_scope (class_type);
18113 /* Make sure that any template parameters are in scope. */
18114 maybe_begin_member_template_processing (decl);
18115 /* Parse the default argument expressions. */
18116 cp_parser_late_parsing_default_args (parser, decl);
18117 /* Remove any template parameters from the symbol table. */
18118 maybe_end_member_template_processing ();
18120 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18121 /* Now parse any NSDMIs. */
18122 save_ccp = current_class_ptr;
18123 save_ccr = current_class_ref;
18124 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18126 if (class_type != DECL_CONTEXT (decl))
18129 pop_scope (pushed_scope);
18130 class_type = DECL_CONTEXT (decl);
18131 pushed_scope = push_scope (class_type);
18133 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18134 cp_parser_late_parsing_nsdmi (parser, decl);
18136 VEC_truncate (tree, unparsed_nsdmis, 0);
18137 current_class_ptr = save_ccp;
18138 current_class_ref = save_ccr;
18140 pop_scope (pushed_scope);
18141 /* Now parse the body of the functions. */
18142 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18143 cp_parser_late_parsing_for_member (parser, decl);
18144 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18147 /* Put back any saved access checks. */
18148 pop_deferring_access_checks ();
18150 /* Restore saved state. */
18151 parser->in_switch_statement_p = in_switch_statement_p;
18152 parser->in_statement = in_statement;
18153 parser->in_function_body = saved_in_function_body;
18154 parser->num_template_parameter_lists
18155 = saved_num_template_parameter_lists;
18156 parser->in_unbraced_linkage_specification_p
18157 = saved_in_unbraced_linkage_specification_p;
18163 cp_parser_class_specifier (cp_parser* parser)
18166 timevar_push (TV_PARSE_STRUCT);
18167 ret = cp_parser_class_specifier_1 (parser);
18168 timevar_pop (TV_PARSE_STRUCT);
18172 /* Parse a class-head.
18175 class-key identifier [opt] base-clause [opt]
18176 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18177 class-key nested-name-specifier [opt] template-id
18180 class-virt-specifier:
18184 class-key attributes identifier [opt] base-clause [opt]
18185 class-key attributes nested-name-specifier identifier base-clause [opt]
18186 class-key attributes nested-name-specifier [opt] template-id
18189 Upon return BASES is initialized to the list of base classes (or
18190 NULL, if there are none) in the same form returned by
18191 cp_parser_base_clause.
18193 Returns the TYPE of the indicated class. Sets
18194 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18195 involving a nested-name-specifier was used, and FALSE otherwise.
18197 Returns error_mark_node if this is not a class-head.
18199 Returns NULL_TREE if the class-head is syntactically valid, but
18200 semantically invalid in a way that means we should skip the entire
18201 body of the class. */
18204 cp_parser_class_head (cp_parser* parser,
18205 bool* nested_name_specifier_p,
18206 tree *attributes_p,
18209 tree nested_name_specifier;
18210 enum tag_types class_key;
18211 tree id = NULL_TREE;
18212 tree type = NULL_TREE;
18214 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18215 bool template_id_p = false;
18216 bool qualified_p = false;
18217 bool invalid_nested_name_p = false;
18218 bool invalid_explicit_specialization_p = false;
18219 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18220 tree pushed_scope = NULL_TREE;
18221 unsigned num_templates;
18222 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18223 /* Assume no nested-name-specifier will be present. */
18224 *nested_name_specifier_p = false;
18225 /* Assume no template parameter lists will be used in defining the
18228 parser->colon_corrects_to_scope_p = false;
18230 *bases = NULL_TREE;
18232 /* Look for the class-key. */
18233 class_key = cp_parser_class_key (parser);
18234 if (class_key == none_type)
18235 return error_mark_node;
18237 /* Parse the attributes. */
18238 attributes = cp_parser_attributes_opt (parser);
18240 /* If the next token is `::', that is invalid -- but sometimes
18241 people do try to write:
18245 Handle this gracefully by accepting the extra qualifier, and then
18246 issuing an error about it later if this really is a
18247 class-head. If it turns out just to be an elaborated type
18248 specifier, remain silent. */
18249 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18250 qualified_p = true;
18252 push_deferring_access_checks (dk_no_check);
18254 /* Determine the name of the class. Begin by looking for an
18255 optional nested-name-specifier. */
18256 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18257 nested_name_specifier
18258 = cp_parser_nested_name_specifier_opt (parser,
18259 /*typename_keyword_p=*/false,
18260 /*check_dependency_p=*/false,
18262 /*is_declaration=*/false);
18263 /* If there was a nested-name-specifier, then there *must* be an
18265 if (nested_name_specifier)
18267 type_start_token = cp_lexer_peek_token (parser->lexer);
18268 /* Although the grammar says `identifier', it really means
18269 `class-name' or `template-name'. You are only allowed to
18270 define a class that has already been declared with this
18273 The proposed resolution for Core Issue 180 says that wherever
18274 you see `class T::X' you should treat `X' as a type-name.
18276 It is OK to define an inaccessible class; for example:
18278 class A { class B; };
18281 We do not know if we will see a class-name, or a
18282 template-name. We look for a class-name first, in case the
18283 class-name is a template-id; if we looked for the
18284 template-name first we would stop after the template-name. */
18285 cp_parser_parse_tentatively (parser);
18286 type = cp_parser_class_name (parser,
18287 /*typename_keyword_p=*/false,
18288 /*template_keyword_p=*/false,
18290 /*check_dependency_p=*/false,
18291 /*class_head_p=*/true,
18292 /*is_declaration=*/false);
18293 /* If that didn't work, ignore the nested-name-specifier. */
18294 if (!cp_parser_parse_definitely (parser))
18296 invalid_nested_name_p = true;
18297 type_start_token = cp_lexer_peek_token (parser->lexer);
18298 id = cp_parser_identifier (parser);
18299 if (id == error_mark_node)
18302 /* If we could not find a corresponding TYPE, treat this
18303 declaration like an unqualified declaration. */
18304 if (type == error_mark_node)
18305 nested_name_specifier = NULL_TREE;
18306 /* Otherwise, count the number of templates used in TYPE and its
18307 containing scopes. */
18312 for (scope = TREE_TYPE (type);
18313 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18314 scope = (TYPE_P (scope)
18315 ? TYPE_CONTEXT (scope)
18316 : DECL_CONTEXT (scope)))
18318 && CLASS_TYPE_P (scope)
18319 && CLASSTYPE_TEMPLATE_INFO (scope)
18320 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18321 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18325 /* Otherwise, the identifier is optional. */
18328 /* We don't know whether what comes next is a template-id,
18329 an identifier, or nothing at all. */
18330 cp_parser_parse_tentatively (parser);
18331 /* Check for a template-id. */
18332 type_start_token = cp_lexer_peek_token (parser->lexer);
18333 id = cp_parser_template_id (parser,
18334 /*template_keyword_p=*/false,
18335 /*check_dependency_p=*/true,
18336 /*is_declaration=*/true);
18337 /* If that didn't work, it could still be an identifier. */
18338 if (!cp_parser_parse_definitely (parser))
18340 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18342 type_start_token = cp_lexer_peek_token (parser->lexer);
18343 id = cp_parser_identifier (parser);
18350 template_id_p = true;
18355 pop_deferring_access_checks ();
18359 cp_parser_check_for_invalid_template_id (parser, id,
18360 type_start_token->location);
18362 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18364 /* If it's not a `:' or a `{' then we can't really be looking at a
18365 class-head, since a class-head only appears as part of a
18366 class-specifier. We have to detect this situation before calling
18367 xref_tag, since that has irreversible side-effects. */
18368 if (!cp_parser_next_token_starts_class_definition_p (parser))
18370 cp_parser_error (parser, "expected %<{%> or %<:%>");
18371 type = error_mark_node;
18375 /* At this point, we're going ahead with the class-specifier, even
18376 if some other problem occurs. */
18377 cp_parser_commit_to_tentative_parse (parser);
18378 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18380 cp_parser_error (parser,
18381 "cannot specify %<override%> for a class");
18382 type = error_mark_node;
18385 /* Issue the error about the overly-qualified name now. */
18388 cp_parser_error (parser,
18389 "global qualification of class name is invalid");
18390 type = error_mark_node;
18393 else if (invalid_nested_name_p)
18395 cp_parser_error (parser,
18396 "qualified name does not name a class");
18397 type = error_mark_node;
18400 else if (nested_name_specifier)
18404 /* Reject typedef-names in class heads. */
18405 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18407 error_at (type_start_token->location,
18408 "invalid class name in declaration of %qD",
18414 /* Figure out in what scope the declaration is being placed. */
18415 scope = current_scope ();
18416 /* If that scope does not contain the scope in which the
18417 class was originally declared, the program is invalid. */
18418 if (scope && !is_ancestor (scope, nested_name_specifier))
18420 if (at_namespace_scope_p ())
18421 error_at (type_start_token->location,
18422 "declaration of %qD in namespace %qD which does not "
18424 type, scope, nested_name_specifier);
18426 error_at (type_start_token->location,
18427 "declaration of %qD in %qD which does not enclose %qD",
18428 type, scope, nested_name_specifier);
18434 A declarator-id shall not be qualified except for the
18435 definition of a ... nested class outside of its class
18436 ... [or] the definition or explicit instantiation of a
18437 class member of a namespace outside of its namespace. */
18438 if (scope == nested_name_specifier)
18440 permerror (nested_name_specifier_token_start->location,
18441 "extra qualification not allowed");
18442 nested_name_specifier = NULL_TREE;
18446 /* An explicit-specialization must be preceded by "template <>". If
18447 it is not, try to recover gracefully. */
18448 if (at_namespace_scope_p ()
18449 && parser->num_template_parameter_lists == 0
18452 error_at (type_start_token->location,
18453 "an explicit specialization must be preceded by %<template <>%>");
18454 invalid_explicit_specialization_p = true;
18455 /* Take the same action that would have been taken by
18456 cp_parser_explicit_specialization. */
18457 ++parser->num_template_parameter_lists;
18458 begin_specialization ();
18460 /* There must be no "return" statements between this point and the
18461 end of this function; set "type "to the correct return value and
18462 use "goto done;" to return. */
18463 /* Make sure that the right number of template parameters were
18465 if (!cp_parser_check_template_parameters (parser, num_templates,
18466 type_start_token->location,
18467 /*declarator=*/NULL))
18469 /* If something went wrong, there is no point in even trying to
18470 process the class-definition. */
18475 /* Look up the type. */
18478 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18479 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18480 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18482 error_at (type_start_token->location,
18483 "function template %qD redeclared as a class template", id);
18484 type = error_mark_node;
18488 type = TREE_TYPE (id);
18489 type = maybe_process_partial_specialization (type);
18491 if (nested_name_specifier)
18492 pushed_scope = push_scope (nested_name_specifier);
18494 else if (nested_name_specifier)
18500 template <typename T> struct S { struct T };
18501 template <typename T> struct S<T>::T { };
18503 we will get a TYPENAME_TYPE when processing the definition of
18504 `S::T'. We need to resolve it to the actual type before we
18505 try to define it. */
18506 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18508 class_type = resolve_typename_type (TREE_TYPE (type),
18509 /*only_current_p=*/false);
18510 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18511 type = TYPE_NAME (class_type);
18514 cp_parser_error (parser, "could not resolve typename type");
18515 type = error_mark_node;
18519 if (maybe_process_partial_specialization (TREE_TYPE (type))
18520 == error_mark_node)
18526 class_type = current_class_type;
18527 /* Enter the scope indicated by the nested-name-specifier. */
18528 pushed_scope = push_scope (nested_name_specifier);
18529 /* Get the canonical version of this type. */
18530 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18531 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18532 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18534 type = push_template_decl (type);
18535 if (type == error_mark_node)
18542 type = TREE_TYPE (type);
18543 *nested_name_specifier_p = true;
18545 else /* The name is not a nested name. */
18547 /* If the class was unnamed, create a dummy name. */
18549 id = make_anon_name ();
18550 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18551 parser->num_template_parameter_lists);
18554 /* Indicate whether this class was declared as a `class' or as a
18556 if (TREE_CODE (type) == RECORD_TYPE)
18557 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18558 cp_parser_check_class_key (class_key, type);
18560 /* If this type was already complete, and we see another definition,
18561 that's an error. */
18562 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18564 error_at (type_start_token->location, "redefinition of %q#T",
18566 error_at (type_start_token->location, "previous definition of %q+#T",
18571 else if (type == error_mark_node)
18574 /* We will have entered the scope containing the class; the names of
18575 base classes should be looked up in that context. For example:
18577 struct A { struct B {}; struct C; };
18578 struct A::C : B {};
18582 /* Get the list of base-classes, if there is one. */
18583 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18584 *bases = cp_parser_base_clause (parser);
18587 /* Leave the scope given by the nested-name-specifier. We will
18588 enter the class scope itself while processing the members. */
18590 pop_scope (pushed_scope);
18592 if (invalid_explicit_specialization_p)
18594 end_specialization ();
18595 --parser->num_template_parameter_lists;
18599 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18600 *attributes_p = attributes;
18601 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18602 CLASSTYPE_FINAL (type) = 1;
18604 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18608 /* Parse a class-key.
18615 Returns the kind of class-key specified, or none_type to indicate
18618 static enum tag_types
18619 cp_parser_class_key (cp_parser* parser)
18622 enum tag_types tag_type;
18624 /* Look for the class-key. */
18625 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18629 /* Check to see if the TOKEN is a class-key. */
18630 tag_type = cp_parser_token_is_class_key (token);
18632 cp_parser_error (parser, "expected class-key");
18636 /* Parse an (optional) member-specification.
18638 member-specification:
18639 member-declaration member-specification [opt]
18640 access-specifier : member-specification [opt] */
18643 cp_parser_member_specification_opt (cp_parser* parser)
18650 /* Peek at the next token. */
18651 token = cp_lexer_peek_token (parser->lexer);
18652 /* If it's a `}', or EOF then we've seen all the members. */
18653 if (token->type == CPP_CLOSE_BRACE
18654 || token->type == CPP_EOF
18655 || token->type == CPP_PRAGMA_EOL)
18658 /* See if this token is a keyword. */
18659 keyword = token->keyword;
18663 case RID_PROTECTED:
18665 /* Consume the access-specifier. */
18666 cp_lexer_consume_token (parser->lexer);
18667 /* Remember which access-specifier is active. */
18668 current_access_specifier = token->u.value;
18669 /* Look for the `:'. */
18670 cp_parser_require (parser, CPP_COLON, RT_COLON);
18674 /* Accept #pragmas at class scope. */
18675 if (token->type == CPP_PRAGMA)
18677 cp_parser_pragma (parser, pragma_external);
18681 /* Otherwise, the next construction must be a
18682 member-declaration. */
18683 cp_parser_member_declaration (parser);
18688 /* Parse a member-declaration.
18690 member-declaration:
18691 decl-specifier-seq [opt] member-declarator-list [opt] ;
18692 function-definition ; [opt]
18693 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18695 template-declaration
18698 member-declarator-list:
18700 member-declarator-list , member-declarator
18703 declarator pure-specifier [opt]
18704 declarator constant-initializer [opt]
18705 identifier [opt] : constant-expression
18709 member-declaration:
18710 __extension__ member-declaration
18713 declarator attributes [opt] pure-specifier [opt]
18714 declarator attributes [opt] constant-initializer [opt]
18715 identifier [opt] attributes [opt] : constant-expression
18719 member-declaration:
18720 static_assert-declaration */
18723 cp_parser_member_declaration (cp_parser* parser)
18725 cp_decl_specifier_seq decl_specifiers;
18726 tree prefix_attributes;
18728 int declares_class_or_enum;
18730 cp_token *token = NULL;
18731 cp_token *decl_spec_token_start = NULL;
18732 cp_token *initializer_token_start = NULL;
18733 int saved_pedantic;
18734 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18736 /* Check for the `__extension__' keyword. */
18737 if (cp_parser_extension_opt (parser, &saved_pedantic))
18740 cp_parser_member_declaration (parser);
18741 /* Restore the old value of the PEDANTIC flag. */
18742 pedantic = saved_pedantic;
18747 /* Check for a template-declaration. */
18748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18750 /* An explicit specialization here is an error condition, and we
18751 expect the specialization handler to detect and report this. */
18752 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18753 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18754 cp_parser_explicit_specialization (parser);
18756 cp_parser_template_declaration (parser, /*member_p=*/true);
18761 /* Check for a using-declaration. */
18762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18764 if (cxx_dialect < cxx0x)
18766 /* Parse the using-declaration. */
18767 cp_parser_using_declaration (parser,
18768 /*access_declaration_p=*/false);
18774 cp_parser_parse_tentatively (parser);
18775 decl = cp_parser_alias_declaration (parser);
18776 if (cp_parser_parse_definitely (parser))
18777 finish_member_declaration (decl);
18779 cp_parser_using_declaration (parser,
18780 /*access_declaration_p=*/false);
18785 /* Check for @defs. */
18786 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18789 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18790 ivar = ivar_chains;
18794 ivar = TREE_CHAIN (member);
18795 TREE_CHAIN (member) = NULL_TREE;
18796 finish_member_declaration (member);
18801 /* If the next token is `static_assert' we have a static assertion. */
18802 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18804 cp_parser_static_assert (parser, /*member_p=*/true);
18808 parser->colon_corrects_to_scope_p = false;
18810 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18813 /* Parse the decl-specifier-seq. */
18814 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18815 cp_parser_decl_specifier_seq (parser,
18816 CP_PARSER_FLAGS_OPTIONAL,
18818 &declares_class_or_enum);
18819 prefix_attributes = decl_specifiers.attributes;
18820 decl_specifiers.attributes = NULL_TREE;
18821 /* Check for an invalid type-name. */
18822 if (!decl_specifiers.any_type_specifiers_p
18823 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18825 /* If there is no declarator, then the decl-specifier-seq should
18827 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18829 /* If there was no decl-specifier-seq, and the next token is a
18830 `;', then we have something like:
18836 Each member-declaration shall declare at least one member
18837 name of the class. */
18838 if (!decl_specifiers.any_specifiers_p)
18840 cp_token *token = cp_lexer_peek_token (parser->lexer);
18841 if (!in_system_header_at (token->location))
18842 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18848 /* See if this declaration is a friend. */
18849 friend_p = cp_parser_friend_p (&decl_specifiers);
18850 /* If there were decl-specifiers, check to see if there was
18851 a class-declaration. */
18852 type = check_tag_decl (&decl_specifiers);
18853 /* Nested classes have already been added to the class, but
18854 a `friend' needs to be explicitly registered. */
18857 /* If the `friend' keyword was present, the friend must
18858 be introduced with a class-key. */
18859 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18860 pedwarn (decl_spec_token_start->location, OPT_pedantic,
18861 "in C++03 a class-key must be used "
18862 "when declaring a friend");
18865 template <typename T> struct A {
18866 friend struct A<T>::B;
18869 A<T>::B will be represented by a TYPENAME_TYPE, and
18870 therefore not recognized by check_tag_decl. */
18873 type = decl_specifiers.type;
18874 if (type && TREE_CODE (type) == TYPE_DECL)
18875 type = TREE_TYPE (type);
18877 if (!type || !TYPE_P (type))
18878 error_at (decl_spec_token_start->location,
18879 "friend declaration does not name a class or "
18882 make_friend_class (current_class_type, type,
18883 /*complain=*/true);
18885 /* If there is no TYPE, an error message will already have
18887 else if (!type || type == error_mark_node)
18889 /* An anonymous aggregate has to be handled specially; such
18890 a declaration really declares a data member (with a
18891 particular type), as opposed to a nested class. */
18892 else if (ANON_AGGR_TYPE_P (type))
18894 /* Remove constructors and such from TYPE, now that we
18895 know it is an anonymous aggregate. */
18896 fixup_anonymous_aggr (type);
18897 /* And make the corresponding data member. */
18898 decl = build_decl (decl_spec_token_start->location,
18899 FIELD_DECL, NULL_TREE, type);
18900 /* Add it to the class. */
18901 finish_member_declaration (decl);
18904 cp_parser_check_access_in_redeclaration
18906 decl_spec_token_start->location);
18911 bool assume_semicolon = false;
18913 /* See if these declarations will be friends. */
18914 friend_p = cp_parser_friend_p (&decl_specifiers);
18916 /* Keep going until we hit the `;' at the end of the
18918 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18920 tree attributes = NULL_TREE;
18921 tree first_attribute;
18923 /* Peek at the next token. */
18924 token = cp_lexer_peek_token (parser->lexer);
18926 /* Check for a bitfield declaration. */
18927 if (token->type == CPP_COLON
18928 || (token->type == CPP_NAME
18929 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18935 /* Get the name of the bitfield. Note that we cannot just
18936 check TOKEN here because it may have been invalidated by
18937 the call to cp_lexer_peek_nth_token above. */
18938 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18939 identifier = cp_parser_identifier (parser);
18941 identifier = NULL_TREE;
18943 /* Consume the `:' token. */
18944 cp_lexer_consume_token (parser->lexer);
18945 /* Get the width of the bitfield. */
18947 = cp_parser_constant_expression (parser,
18948 /*allow_non_constant=*/false,
18951 /* Look for attributes that apply to the bitfield. */
18952 attributes = cp_parser_attributes_opt (parser);
18953 /* Remember which attributes are prefix attributes and
18955 first_attribute = attributes;
18956 /* Combine the attributes. */
18957 attributes = chainon (prefix_attributes, attributes);
18959 /* Create the bitfield declaration. */
18960 decl = grokbitfield (identifier
18961 ? make_id_declarator (NULL_TREE,
18971 cp_declarator *declarator;
18973 tree asm_specification;
18974 int ctor_dtor_or_conv_p;
18976 /* Parse the declarator. */
18978 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18979 &ctor_dtor_or_conv_p,
18980 /*parenthesized_p=*/NULL,
18981 /*member_p=*/true);
18983 /* If something went wrong parsing the declarator, make sure
18984 that we at least consume some tokens. */
18985 if (declarator == cp_error_declarator)
18987 /* Skip to the end of the statement. */
18988 cp_parser_skip_to_end_of_statement (parser);
18989 /* If the next token is not a semicolon, that is
18990 probably because we just skipped over the body of
18991 a function. So, we consume a semicolon if
18992 present, but do not issue an error message if it
18994 if (cp_lexer_next_token_is (parser->lexer,
18996 cp_lexer_consume_token (parser->lexer);
19000 if (declares_class_or_enum & 2)
19001 cp_parser_check_for_definition_in_return_type
19002 (declarator, decl_specifiers.type,
19003 decl_specifiers.type_location);
19005 /* Look for an asm-specification. */
19006 asm_specification = cp_parser_asm_specification_opt (parser);
19007 /* Look for attributes that apply to the declaration. */
19008 attributes = cp_parser_attributes_opt (parser);
19009 /* Remember which attributes are prefix attributes and
19011 first_attribute = attributes;
19012 /* Combine the attributes. */
19013 attributes = chainon (prefix_attributes, attributes);
19015 /* If it's an `=', then we have a constant-initializer or a
19016 pure-specifier. It is not correct to parse the
19017 initializer before registering the member declaration
19018 since the member declaration should be in scope while
19019 its initializer is processed. However, the rest of the
19020 front end does not yet provide an interface that allows
19021 us to handle this correctly. */
19022 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19026 A pure-specifier shall be used only in the declaration of
19027 a virtual function.
19029 A member-declarator can contain a constant-initializer
19030 only if it declares a static member of integral or
19033 Therefore, if the DECLARATOR is for a function, we look
19034 for a pure-specifier; otherwise, we look for a
19035 constant-initializer. When we call `grokfield', it will
19036 perform more stringent semantics checks. */
19037 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19038 if (function_declarator_p (declarator)
19039 || (decl_specifiers.type
19040 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19041 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19042 == FUNCTION_TYPE)))
19043 initializer = cp_parser_pure_specifier (parser);
19044 else if (decl_specifiers.storage_class != sc_static)
19045 initializer = cp_parser_save_nsdmi (parser);
19046 else if (cxx_dialect >= cxx0x)
19049 /* Don't require a constant rvalue in C++11, since we
19050 might want a reference constant. We'll enforce
19051 constancy later. */
19052 cp_lexer_consume_token (parser->lexer);
19053 /* Parse the initializer. */
19054 initializer = cp_parser_initializer_clause (parser,
19058 /* Parse the initializer. */
19059 initializer = cp_parser_constant_initializer (parser);
19061 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19062 && !function_declarator_p (declarator))
19065 if (decl_specifiers.storage_class != sc_static)
19066 initializer = cp_parser_save_nsdmi (parser);
19068 initializer = cp_parser_initializer (parser, &x, &x);
19070 /* Otherwise, there is no initializer. */
19072 initializer = NULL_TREE;
19074 /* See if we are probably looking at a function
19075 definition. We are certainly not looking at a
19076 member-declarator. Calling `grokfield' has
19077 side-effects, so we must not do it unless we are sure
19078 that we are looking at a member-declarator. */
19079 if (cp_parser_token_starts_function_definition_p
19080 (cp_lexer_peek_token (parser->lexer)))
19082 /* The grammar does not allow a pure-specifier to be
19083 used when a member function is defined. (It is
19084 possible that this fact is an oversight in the
19085 standard, since a pure function may be defined
19086 outside of the class-specifier. */
19088 error_at (initializer_token_start->location,
19089 "pure-specifier on function-definition");
19090 decl = cp_parser_save_member_function_body (parser,
19094 /* If the member was not a friend, declare it here. */
19096 finish_member_declaration (decl);
19097 /* Peek at the next token. */
19098 token = cp_lexer_peek_token (parser->lexer);
19099 /* If the next token is a semicolon, consume it. */
19100 if (token->type == CPP_SEMICOLON)
19101 cp_lexer_consume_token (parser->lexer);
19105 if (declarator->kind == cdk_function)
19106 declarator->id_loc = token->location;
19107 /* Create the declaration. */
19108 decl = grokfield (declarator, &decl_specifiers,
19109 initializer, /*init_const_expr_p=*/true,
19114 /* Reset PREFIX_ATTRIBUTES. */
19115 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19116 attributes = TREE_CHAIN (attributes);
19118 TREE_CHAIN (attributes) = NULL_TREE;
19120 /* If there is any qualification still in effect, clear it
19121 now; we will be starting fresh with the next declarator. */
19122 parser->scope = NULL_TREE;
19123 parser->qualifying_scope = NULL_TREE;
19124 parser->object_scope = NULL_TREE;
19125 /* If it's a `,', then there are more declarators. */
19126 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19127 cp_lexer_consume_token (parser->lexer);
19128 /* If the next token isn't a `;', then we have a parse error. */
19129 else if (cp_lexer_next_token_is_not (parser->lexer,
19132 /* The next token might be a ways away from where the
19133 actual semicolon is missing. Find the previous token
19134 and use that for our error position. */
19135 cp_token *token = cp_lexer_previous_token (parser->lexer);
19136 error_at (token->location,
19137 "expected %<;%> at end of member declaration");
19139 /* Assume that the user meant to provide a semicolon. If
19140 we were to cp_parser_skip_to_end_of_statement, we might
19141 skip to a semicolon inside a member function definition
19142 and issue nonsensical error messages. */
19143 assume_semicolon = true;
19148 /* Add DECL to the list of members. */
19150 finish_member_declaration (decl);
19152 if (TREE_CODE (decl) == FUNCTION_DECL)
19153 cp_parser_save_default_args (parser, decl);
19154 else if (TREE_CODE (decl) == FIELD_DECL
19155 && !DECL_C_BIT_FIELD (decl)
19156 && DECL_INITIAL (decl))
19157 /* Add DECL to the queue of NSDMI to be parsed later. */
19158 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19161 if (assume_semicolon)
19166 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19168 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19171 /* Parse a pure-specifier.
19176 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19177 Otherwise, ERROR_MARK_NODE is returned. */
19180 cp_parser_pure_specifier (cp_parser* parser)
19184 /* Look for the `=' token. */
19185 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19186 return error_mark_node;
19187 /* Look for the `0' token. */
19188 token = cp_lexer_peek_token (parser->lexer);
19190 if (token->type == CPP_EOF
19191 || token->type == CPP_PRAGMA_EOL)
19192 return error_mark_node;
19194 cp_lexer_consume_token (parser->lexer);
19196 /* Accept = default or = delete in c++0x mode. */
19197 if (token->keyword == RID_DEFAULT
19198 || token->keyword == RID_DELETE)
19200 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19201 return token->u.value;
19204 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19205 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19207 cp_parser_error (parser,
19208 "invalid pure specifier (only %<= 0%> is allowed)");
19209 cp_parser_skip_to_end_of_statement (parser);
19210 return error_mark_node;
19212 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19214 error_at (token->location, "templates may not be %<virtual%>");
19215 return error_mark_node;
19218 return integer_zero_node;
19221 /* Parse a constant-initializer.
19223 constant-initializer:
19224 = constant-expression
19226 Returns a representation of the constant-expression. */
19229 cp_parser_constant_initializer (cp_parser* parser)
19231 /* Look for the `=' token. */
19232 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19233 return error_mark_node;
19235 /* It is invalid to write:
19237 struct S { static const int i = { 7 }; };
19240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19242 cp_parser_error (parser,
19243 "a brace-enclosed initializer is not allowed here");
19244 /* Consume the opening brace. */
19245 cp_lexer_consume_token (parser->lexer);
19246 /* Skip the initializer. */
19247 cp_parser_skip_to_closing_brace (parser);
19248 /* Look for the trailing `}'. */
19249 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19251 return error_mark_node;
19254 return cp_parser_constant_expression (parser,
19255 /*allow_non_constant=*/false,
19259 /* Derived classes [gram.class.derived] */
19261 /* Parse a base-clause.
19264 : base-specifier-list
19266 base-specifier-list:
19267 base-specifier ... [opt]
19268 base-specifier-list , base-specifier ... [opt]
19270 Returns a TREE_LIST representing the base-classes, in the order in
19271 which they were declared. The representation of each node is as
19272 described by cp_parser_base_specifier.
19274 In the case that no bases are specified, this function will return
19275 NULL_TREE, not ERROR_MARK_NODE. */
19278 cp_parser_base_clause (cp_parser* parser)
19280 tree bases = NULL_TREE;
19282 /* Look for the `:' that begins the list. */
19283 cp_parser_require (parser, CPP_COLON, RT_COLON);
19285 /* Scan the base-specifier-list. */
19290 bool pack_expansion_p = false;
19292 /* Look for the base-specifier. */
19293 base = cp_parser_base_specifier (parser);
19294 /* Look for the (optional) ellipsis. */
19295 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19297 /* Consume the `...'. */
19298 cp_lexer_consume_token (parser->lexer);
19300 pack_expansion_p = true;
19303 /* Add BASE to the front of the list. */
19304 if (base && base != error_mark_node)
19306 if (pack_expansion_p)
19307 /* Make this a pack expansion type. */
19308 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19310 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19312 TREE_CHAIN (base) = bases;
19316 /* Peek at the next token. */
19317 token = cp_lexer_peek_token (parser->lexer);
19318 /* If it's not a comma, then the list is complete. */
19319 if (token->type != CPP_COMMA)
19321 /* Consume the `,'. */
19322 cp_lexer_consume_token (parser->lexer);
19325 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19326 base class had a qualified name. However, the next name that
19327 appears is certainly not qualified. */
19328 parser->scope = NULL_TREE;
19329 parser->qualifying_scope = NULL_TREE;
19330 parser->object_scope = NULL_TREE;
19332 return nreverse (bases);
19335 /* Parse a base-specifier.
19338 :: [opt] nested-name-specifier [opt] class-name
19339 virtual access-specifier [opt] :: [opt] nested-name-specifier
19341 access-specifier virtual [opt] :: [opt] nested-name-specifier
19344 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19345 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19346 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19347 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19350 cp_parser_base_specifier (cp_parser* parser)
19354 bool virtual_p = false;
19355 bool duplicate_virtual_error_issued_p = false;
19356 bool duplicate_access_error_issued_p = false;
19357 bool class_scope_p, template_p;
19358 tree access = access_default_node;
19361 /* Process the optional `virtual' and `access-specifier'. */
19364 /* Peek at the next token. */
19365 token = cp_lexer_peek_token (parser->lexer);
19366 /* Process `virtual'. */
19367 switch (token->keyword)
19370 /* If `virtual' appears more than once, issue an error. */
19371 if (virtual_p && !duplicate_virtual_error_issued_p)
19373 cp_parser_error (parser,
19374 "%<virtual%> specified more than once in base-specified");
19375 duplicate_virtual_error_issued_p = true;
19380 /* Consume the `virtual' token. */
19381 cp_lexer_consume_token (parser->lexer);
19386 case RID_PROTECTED:
19388 /* If more than one access specifier appears, issue an
19390 if (access != access_default_node
19391 && !duplicate_access_error_issued_p)
19393 cp_parser_error (parser,
19394 "more than one access specifier in base-specified");
19395 duplicate_access_error_issued_p = true;
19398 access = ridpointers[(int) token->keyword];
19400 /* Consume the access-specifier. */
19401 cp_lexer_consume_token (parser->lexer);
19410 /* It is not uncommon to see programs mechanically, erroneously, use
19411 the 'typename' keyword to denote (dependent) qualified types
19412 as base classes. */
19413 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19415 token = cp_lexer_peek_token (parser->lexer);
19416 if (!processing_template_decl)
19417 error_at (token->location,
19418 "keyword %<typename%> not allowed outside of templates");
19420 error_at (token->location,
19421 "keyword %<typename%> not allowed in this context "
19422 "(the base class is implicitly a type)");
19423 cp_lexer_consume_token (parser->lexer);
19426 /* Look for the optional `::' operator. */
19427 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19428 /* Look for the nested-name-specifier. The simplest way to
19433 The keyword `typename' is not permitted in a base-specifier or
19434 mem-initializer; in these contexts a qualified name that
19435 depends on a template-parameter is implicitly assumed to be a
19438 is to pretend that we have seen the `typename' keyword at this
19440 cp_parser_nested_name_specifier_opt (parser,
19441 /*typename_keyword_p=*/true,
19442 /*check_dependency_p=*/true,
19444 /*is_declaration=*/true);
19445 /* If the base class is given by a qualified name, assume that names
19446 we see are type names or templates, as appropriate. */
19447 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19448 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19451 && cp_lexer_next_token_is_decltype (parser->lexer))
19452 /* DR 950 allows decltype as a base-specifier. */
19453 type = cp_parser_decltype (parser);
19456 /* Otherwise, look for the class-name. */
19457 type = cp_parser_class_name (parser,
19461 /*check_dependency_p=*/true,
19462 /*class_head_p=*/false,
19463 /*is_declaration=*/true);
19464 type = TREE_TYPE (type);
19467 if (type == error_mark_node)
19468 return error_mark_node;
19470 return finish_base_specifier (type, access, virtual_p);
19473 /* Exception handling [gram.exception] */
19475 /* Parse an (optional) exception-specification.
19477 exception-specification:
19478 throw ( type-id-list [opt] )
19480 Returns a TREE_LIST representing the exception-specification. The
19481 TREE_VALUE of each node is a type. */
19484 cp_parser_exception_specification_opt (cp_parser* parser)
19488 const char *saved_message;
19490 /* Peek at the next token. */
19491 token = cp_lexer_peek_token (parser->lexer);
19493 /* Is it a noexcept-specification? */
19494 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19497 cp_lexer_consume_token (parser->lexer);
19499 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19501 cp_lexer_consume_token (parser->lexer);
19503 /* Types may not be defined in an exception-specification. */
19504 saved_message = parser->type_definition_forbidden_message;
19505 parser->type_definition_forbidden_message
19506 = G_("types may not be defined in an exception-specification");
19508 expr = cp_parser_constant_expression (parser, false, NULL);
19510 /* Restore the saved message. */
19511 parser->type_definition_forbidden_message = saved_message;
19513 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19516 expr = boolean_true_node;
19518 return build_noexcept_spec (expr, tf_warning_or_error);
19521 /* If it's not `throw', then there's no exception-specification. */
19522 if (!cp_parser_is_keyword (token, RID_THROW))
19526 /* Enable this once a lot of code has transitioned to noexcept? */
19527 if (cxx_dialect == cxx0x && !in_system_header)
19528 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19529 "deprecated in C++0x; use %<noexcept%> instead");
19532 /* Consume the `throw'. */
19533 cp_lexer_consume_token (parser->lexer);
19535 /* Look for the `('. */
19536 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19538 /* Peek at the next token. */
19539 token = cp_lexer_peek_token (parser->lexer);
19540 /* If it's not a `)', then there is a type-id-list. */
19541 if (token->type != CPP_CLOSE_PAREN)
19543 /* Types may not be defined in an exception-specification. */
19544 saved_message = parser->type_definition_forbidden_message;
19545 parser->type_definition_forbidden_message
19546 = G_("types may not be defined in an exception-specification");
19547 /* Parse the type-id-list. */
19548 type_id_list = cp_parser_type_id_list (parser);
19549 /* Restore the saved message. */
19550 parser->type_definition_forbidden_message = saved_message;
19553 type_id_list = empty_except_spec;
19555 /* Look for the `)'. */
19556 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19558 return type_id_list;
19561 /* Parse an (optional) type-id-list.
19565 type-id-list , type-id ... [opt]
19567 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19568 in the order that the types were presented. */
19571 cp_parser_type_id_list (cp_parser* parser)
19573 tree types = NULL_TREE;
19580 /* Get the next type-id. */
19581 type = cp_parser_type_id (parser);
19582 /* Parse the optional ellipsis. */
19583 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19585 /* Consume the `...'. */
19586 cp_lexer_consume_token (parser->lexer);
19588 /* Turn the type into a pack expansion expression. */
19589 type = make_pack_expansion (type);
19591 /* Add it to the list. */
19592 types = add_exception_specifier (types, type, /*complain=*/1);
19593 /* Peek at the next token. */
19594 token = cp_lexer_peek_token (parser->lexer);
19595 /* If it is not a `,', we are done. */
19596 if (token->type != CPP_COMMA)
19598 /* Consume the `,'. */
19599 cp_lexer_consume_token (parser->lexer);
19602 return nreverse (types);
19605 /* Parse a try-block.
19608 try compound-statement handler-seq */
19611 cp_parser_try_block (cp_parser* parser)
19615 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19616 try_block = begin_try_block ();
19617 cp_parser_compound_statement (parser, NULL, true, false);
19618 finish_try_block (try_block);
19619 cp_parser_handler_seq (parser);
19620 finish_handler_sequence (try_block);
19625 /* Parse a function-try-block.
19627 function-try-block:
19628 try ctor-initializer [opt] function-body handler-seq */
19631 cp_parser_function_try_block (cp_parser* parser)
19633 tree compound_stmt;
19635 bool ctor_initializer_p;
19637 /* Look for the `try' keyword. */
19638 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19640 /* Let the rest of the front end know where we are. */
19641 try_block = begin_function_try_block (&compound_stmt);
19642 /* Parse the function-body. */
19644 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19645 /* We're done with the `try' part. */
19646 finish_function_try_block (try_block);
19647 /* Parse the handlers. */
19648 cp_parser_handler_seq (parser);
19649 /* We're done with the handlers. */
19650 finish_function_handler_sequence (try_block, compound_stmt);
19652 return ctor_initializer_p;
19655 /* Parse a handler-seq.
19658 handler handler-seq [opt] */
19661 cp_parser_handler_seq (cp_parser* parser)
19667 /* Parse the handler. */
19668 cp_parser_handler (parser);
19669 /* Peek at the next token. */
19670 token = cp_lexer_peek_token (parser->lexer);
19671 /* If it's not `catch' then there are no more handlers. */
19672 if (!cp_parser_is_keyword (token, RID_CATCH))
19677 /* Parse a handler.
19680 catch ( exception-declaration ) compound-statement */
19683 cp_parser_handler (cp_parser* parser)
19688 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19689 handler = begin_handler ();
19690 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19691 declaration = cp_parser_exception_declaration (parser);
19692 finish_handler_parms (declaration, handler);
19693 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19694 cp_parser_compound_statement (parser, NULL, false, false);
19695 finish_handler (handler);
19698 /* Parse an exception-declaration.
19700 exception-declaration:
19701 type-specifier-seq declarator
19702 type-specifier-seq abstract-declarator
19706 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19707 ellipsis variant is used. */
19710 cp_parser_exception_declaration (cp_parser* parser)
19712 cp_decl_specifier_seq type_specifiers;
19713 cp_declarator *declarator;
19714 const char *saved_message;
19716 /* If it's an ellipsis, it's easy to handle. */
19717 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19719 /* Consume the `...' token. */
19720 cp_lexer_consume_token (parser->lexer);
19724 /* Types may not be defined in exception-declarations. */
19725 saved_message = parser->type_definition_forbidden_message;
19726 parser->type_definition_forbidden_message
19727 = G_("types may not be defined in exception-declarations");
19729 /* Parse the type-specifier-seq. */
19730 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19731 /*is_trailing_return=*/false,
19733 /* If it's a `)', then there is no declarator. */
19734 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19737 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19738 /*ctor_dtor_or_conv_p=*/NULL,
19739 /*parenthesized_p=*/NULL,
19740 /*member_p=*/false);
19742 /* Restore the saved message. */
19743 parser->type_definition_forbidden_message = saved_message;
19745 if (!type_specifiers.any_specifiers_p)
19746 return error_mark_node;
19748 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19751 /* Parse a throw-expression.
19754 throw assignment-expression [opt]
19756 Returns a THROW_EXPR representing the throw-expression. */
19759 cp_parser_throw_expression (cp_parser* parser)
19764 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19765 token = cp_lexer_peek_token (parser->lexer);
19766 /* Figure out whether or not there is an assignment-expression
19767 following the "throw" keyword. */
19768 if (token->type == CPP_COMMA
19769 || token->type == CPP_SEMICOLON
19770 || token->type == CPP_CLOSE_PAREN
19771 || token->type == CPP_CLOSE_SQUARE
19772 || token->type == CPP_CLOSE_BRACE
19773 || token->type == CPP_COLON)
19774 expression = NULL_TREE;
19776 expression = cp_parser_assignment_expression (parser,
19777 /*cast_p=*/false, NULL);
19779 return build_throw (expression);
19782 /* GNU Extensions */
19784 /* Parse an (optional) asm-specification.
19787 asm ( string-literal )
19789 If the asm-specification is present, returns a STRING_CST
19790 corresponding to the string-literal. Otherwise, returns
19794 cp_parser_asm_specification_opt (cp_parser* parser)
19797 tree asm_specification;
19799 /* Peek at the next token. */
19800 token = cp_lexer_peek_token (parser->lexer);
19801 /* If the next token isn't the `asm' keyword, then there's no
19802 asm-specification. */
19803 if (!cp_parser_is_keyword (token, RID_ASM))
19806 /* Consume the `asm' token. */
19807 cp_lexer_consume_token (parser->lexer);
19808 /* Look for the `('. */
19809 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19811 /* Look for the string-literal. */
19812 asm_specification = cp_parser_string_literal (parser, false, false);
19814 /* Look for the `)'. */
19815 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19817 return asm_specification;
19820 /* Parse an asm-operand-list.
19824 asm-operand-list , asm-operand
19827 string-literal ( expression )
19828 [ string-literal ] string-literal ( expression )
19830 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19831 each node is the expression. The TREE_PURPOSE is itself a
19832 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19833 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19834 is a STRING_CST for the string literal before the parenthesis. Returns
19835 ERROR_MARK_NODE if any of the operands are invalid. */
19838 cp_parser_asm_operand_list (cp_parser* parser)
19840 tree asm_operands = NULL_TREE;
19841 bool invalid_operands = false;
19845 tree string_literal;
19849 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19851 /* Consume the `[' token. */
19852 cp_lexer_consume_token (parser->lexer);
19853 /* Read the operand name. */
19854 name = cp_parser_identifier (parser);
19855 if (name != error_mark_node)
19856 name = build_string (IDENTIFIER_LENGTH (name),
19857 IDENTIFIER_POINTER (name));
19858 /* Look for the closing `]'. */
19859 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19863 /* Look for the string-literal. */
19864 string_literal = cp_parser_string_literal (parser, false, false);
19866 /* Look for the `('. */
19867 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19868 /* Parse the expression. */
19869 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19870 /* Look for the `)'. */
19871 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19873 if (name == error_mark_node
19874 || string_literal == error_mark_node
19875 || expression == error_mark_node)
19876 invalid_operands = true;
19878 /* Add this operand to the list. */
19879 asm_operands = tree_cons (build_tree_list (name, string_literal),
19882 /* If the next token is not a `,', there are no more
19884 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19886 /* Consume the `,'. */
19887 cp_lexer_consume_token (parser->lexer);
19890 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19893 /* Parse an asm-clobber-list.
19897 asm-clobber-list , string-literal
19899 Returns a TREE_LIST, indicating the clobbers in the order that they
19900 appeared. The TREE_VALUE of each node is a STRING_CST. */
19903 cp_parser_asm_clobber_list (cp_parser* parser)
19905 tree clobbers = NULL_TREE;
19909 tree string_literal;
19911 /* Look for the string literal. */
19912 string_literal = cp_parser_string_literal (parser, false, false);
19913 /* Add it to the list. */
19914 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19915 /* If the next token is not a `,', then the list is
19917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19919 /* Consume the `,' token. */
19920 cp_lexer_consume_token (parser->lexer);
19926 /* Parse an asm-label-list.
19930 asm-label-list , identifier
19932 Returns a TREE_LIST, indicating the labels in the order that they
19933 appeared. The TREE_VALUE of each node is a label. */
19936 cp_parser_asm_label_list (cp_parser* parser)
19938 tree labels = NULL_TREE;
19942 tree identifier, label, name;
19944 /* Look for the identifier. */
19945 identifier = cp_parser_identifier (parser);
19946 if (!error_operand_p (identifier))
19948 label = lookup_label (identifier);
19949 if (TREE_CODE (label) == LABEL_DECL)
19951 TREE_USED (label) = 1;
19952 check_goto (label);
19953 name = build_string (IDENTIFIER_LENGTH (identifier),
19954 IDENTIFIER_POINTER (identifier));
19955 labels = tree_cons (name, label, labels);
19958 /* If the next token is not a `,', then the list is
19960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19962 /* Consume the `,' token. */
19963 cp_lexer_consume_token (parser->lexer);
19966 return nreverse (labels);
19969 /* Parse an (optional) series of attributes.
19972 attributes attribute
19975 __attribute__ (( attribute-list [opt] ))
19977 The return value is as for cp_parser_attribute_list. */
19980 cp_parser_attributes_opt (cp_parser* parser)
19982 tree attributes = NULL_TREE;
19987 tree attribute_list;
19989 /* Peek at the next token. */
19990 token = cp_lexer_peek_token (parser->lexer);
19991 /* If it's not `__attribute__', then we're done. */
19992 if (token->keyword != RID_ATTRIBUTE)
19995 /* Consume the `__attribute__' keyword. */
19996 cp_lexer_consume_token (parser->lexer);
19997 /* Look for the two `(' tokens. */
19998 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19999 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20001 /* Peek at the next token. */
20002 token = cp_lexer_peek_token (parser->lexer);
20003 if (token->type != CPP_CLOSE_PAREN)
20004 /* Parse the attribute-list. */
20005 attribute_list = cp_parser_attribute_list (parser);
20007 /* If the next token is a `)', then there is no attribute
20009 attribute_list = NULL;
20011 /* Look for the two `)' tokens. */
20012 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20013 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20015 /* Add these new attributes to the list. */
20016 attributes = chainon (attributes, attribute_list);
20022 /* Parse an attribute-list.
20026 attribute-list , attribute
20030 identifier ( identifier )
20031 identifier ( identifier , expression-list )
20032 identifier ( expression-list )
20034 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20035 to an attribute. The TREE_PURPOSE of each node is the identifier
20036 indicating which attribute is in use. The TREE_VALUE represents
20037 the arguments, if any. */
20040 cp_parser_attribute_list (cp_parser* parser)
20042 tree attribute_list = NULL_TREE;
20043 bool save_translate_strings_p = parser->translate_strings_p;
20045 parser->translate_strings_p = false;
20052 /* Look for the identifier. We also allow keywords here; for
20053 example `__attribute__ ((const))' is legal. */
20054 token = cp_lexer_peek_token (parser->lexer);
20055 if (token->type == CPP_NAME
20056 || token->type == CPP_KEYWORD)
20058 tree arguments = NULL_TREE;
20060 /* Consume the token. */
20061 token = cp_lexer_consume_token (parser->lexer);
20063 /* Save away the identifier that indicates which attribute
20065 identifier = (token->type == CPP_KEYWORD)
20066 /* For keywords, use the canonical spelling, not the
20067 parsed identifier. */
20068 ? ridpointers[(int) token->keyword]
20071 attribute = build_tree_list (identifier, NULL_TREE);
20073 /* Peek at the next token. */
20074 token = cp_lexer_peek_token (parser->lexer);
20075 /* If it's an `(', then parse the attribute arguments. */
20076 if (token->type == CPP_OPEN_PAREN)
20079 int attr_flag = (attribute_takes_identifier_p (identifier)
20080 ? id_attr : normal_attr);
20081 vec = cp_parser_parenthesized_expression_list
20082 (parser, attr_flag, /*cast_p=*/false,
20083 /*allow_expansion_p=*/false,
20084 /*non_constant_p=*/NULL);
20086 arguments = error_mark_node;
20089 arguments = build_tree_list_vec (vec);
20090 release_tree_vector (vec);
20092 /* Save the arguments away. */
20093 TREE_VALUE (attribute) = arguments;
20096 if (arguments != error_mark_node)
20098 /* Add this attribute to the list. */
20099 TREE_CHAIN (attribute) = attribute_list;
20100 attribute_list = attribute;
20103 token = cp_lexer_peek_token (parser->lexer);
20105 /* Now, look for more attributes. If the next token isn't a
20106 `,', we're done. */
20107 if (token->type != CPP_COMMA)
20110 /* Consume the comma and keep going. */
20111 cp_lexer_consume_token (parser->lexer);
20113 parser->translate_strings_p = save_translate_strings_p;
20115 /* We built up the list in reverse order. */
20116 return nreverse (attribute_list);
20119 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20120 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20121 current value of the PEDANTIC flag, regardless of whether or not
20122 the `__extension__' keyword is present. The caller is responsible
20123 for restoring the value of the PEDANTIC flag. */
20126 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20128 /* Save the old value of the PEDANTIC flag. */
20129 *saved_pedantic = pedantic;
20131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20133 /* Consume the `__extension__' token. */
20134 cp_lexer_consume_token (parser->lexer);
20135 /* We're not being pedantic while the `__extension__' keyword is
20145 /* Parse a label declaration.
20148 __label__ label-declarator-seq ;
20150 label-declarator-seq:
20151 identifier , label-declarator-seq
20155 cp_parser_label_declaration (cp_parser* parser)
20157 /* Look for the `__label__' keyword. */
20158 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20164 /* Look for an identifier. */
20165 identifier = cp_parser_identifier (parser);
20166 /* If we failed, stop. */
20167 if (identifier == error_mark_node)
20169 /* Declare it as a label. */
20170 finish_label_decl (identifier);
20171 /* If the next token is a `;', stop. */
20172 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20174 /* Look for the `,' separating the label declarations. */
20175 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20178 /* Look for the final `;'. */
20179 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20182 /* Support Functions */
20184 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20185 NAME should have one of the representations used for an
20186 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20187 is returned. If PARSER->SCOPE is a dependent type, then a
20188 SCOPE_REF is returned.
20190 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20191 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20192 was formed. Abstractly, such entities should not be passed to this
20193 function, because they do not need to be looked up, but it is
20194 simpler to check for this special case here, rather than at the
20197 In cases not explicitly covered above, this function returns a
20198 DECL, OVERLOAD, or baselink representing the result of the lookup.
20199 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20202 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20203 (e.g., "struct") that was used. In that case bindings that do not
20204 refer to types are ignored.
20206 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20209 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20212 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20215 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20216 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20217 NULL_TREE otherwise. */
20220 cp_parser_lookup_name (cp_parser *parser, tree name,
20221 enum tag_types tag_type,
20224 bool check_dependency,
20225 tree *ambiguous_decls,
20226 location_t name_location)
20230 tree object_type = parser->context->object_type;
20232 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20233 flags |= LOOKUP_COMPLAIN;
20235 /* Assume that the lookup will be unambiguous. */
20236 if (ambiguous_decls)
20237 *ambiguous_decls = NULL_TREE;
20239 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20240 no longer valid. Note that if we are parsing tentatively, and
20241 the parse fails, OBJECT_TYPE will be automatically restored. */
20242 parser->context->object_type = NULL_TREE;
20244 if (name == error_mark_node)
20245 return error_mark_node;
20247 /* A template-id has already been resolved; there is no lookup to
20249 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20251 if (BASELINK_P (name))
20253 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20254 == TEMPLATE_ID_EXPR);
20258 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20259 it should already have been checked to make sure that the name
20260 used matches the type being destroyed. */
20261 if (TREE_CODE (name) == BIT_NOT_EXPR)
20265 /* Figure out to which type this destructor applies. */
20267 type = parser->scope;
20268 else if (object_type)
20269 type = object_type;
20271 type = current_class_type;
20272 /* If that's not a class type, there is no destructor. */
20273 if (!type || !CLASS_TYPE_P (type))
20274 return error_mark_node;
20275 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20276 lazily_declare_fn (sfk_destructor, type);
20277 if (!CLASSTYPE_DESTRUCTORS (type))
20278 return error_mark_node;
20279 /* If it was a class type, return the destructor. */
20280 return CLASSTYPE_DESTRUCTORS (type);
20283 /* By this point, the NAME should be an ordinary identifier. If
20284 the id-expression was a qualified name, the qualifying scope is
20285 stored in PARSER->SCOPE at this point. */
20286 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20288 /* Perform the lookup. */
20293 if (parser->scope == error_mark_node)
20294 return error_mark_node;
20296 /* If the SCOPE is dependent, the lookup must be deferred until
20297 the template is instantiated -- unless we are explicitly
20298 looking up names in uninstantiated templates. Even then, we
20299 cannot look up the name if the scope is not a class type; it
20300 might, for example, be a template type parameter. */
20301 dependent_p = (TYPE_P (parser->scope)
20302 && dependent_scope_p (parser->scope));
20303 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20305 /* Defer lookup. */
20306 decl = error_mark_node;
20309 tree pushed_scope = NULL_TREE;
20311 /* If PARSER->SCOPE is a dependent type, then it must be a
20312 class type, and we must not be checking dependencies;
20313 otherwise, we would have processed this lookup above. So
20314 that PARSER->SCOPE is not considered a dependent base by
20315 lookup_member, we must enter the scope here. */
20317 pushed_scope = push_scope (parser->scope);
20319 /* If the PARSER->SCOPE is a template specialization, it
20320 may be instantiated during name lookup. In that case,
20321 errors may be issued. Even if we rollback the current
20322 tentative parse, those errors are valid. */
20323 decl = lookup_qualified_name (parser->scope, name,
20324 tag_type != none_type,
20325 /*complain=*/true);
20327 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20328 lookup result and the nested-name-specifier nominates a class C:
20329 * if the name specified after the nested-name-specifier, when
20330 looked up in C, is the injected-class-name of C (Clause 9), or
20331 * if the name specified after the nested-name-specifier is the
20332 same as the identifier or the simple-template-id's template-
20333 name in the last component of the nested-name-specifier,
20334 the name is instead considered to name the constructor of
20335 class C. [ Note: for example, the constructor is not an
20336 acceptable lookup result in an elaborated-type-specifier so
20337 the constructor would not be used in place of the
20338 injected-class-name. --end note ] Such a constructor name
20339 shall be used only in the declarator-id of a declaration that
20340 names a constructor or in a using-declaration. */
20341 if (tag_type == none_type
20342 && DECL_SELF_REFERENCE_P (decl)
20343 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20344 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20345 tag_type != none_type,
20346 /*complain=*/true);
20348 /* If we have a single function from a using decl, pull it out. */
20349 if (TREE_CODE (decl) == OVERLOAD
20350 && !really_overloaded_fn (decl))
20351 decl = OVL_FUNCTION (decl);
20354 pop_scope (pushed_scope);
20357 /* If the scope is a dependent type and either we deferred lookup or
20358 we did lookup but didn't find the name, rememeber the name. */
20359 if (decl == error_mark_node && TYPE_P (parser->scope)
20360 && dependent_type_p (parser->scope))
20366 /* The resolution to Core Issue 180 says that `struct
20367 A::B' should be considered a type-name, even if `A'
20369 type = make_typename_type (parser->scope, name, tag_type,
20370 /*complain=*/tf_error);
20371 decl = TYPE_NAME (type);
20373 else if (is_template
20374 && (cp_parser_next_token_ends_template_argument_p (parser)
20375 || cp_lexer_next_token_is (parser->lexer,
20377 decl = make_unbound_class_template (parser->scope,
20379 /*complain=*/tf_error);
20381 decl = build_qualified_name (/*type=*/NULL_TREE,
20382 parser->scope, name,
20385 parser->qualifying_scope = parser->scope;
20386 parser->object_scope = NULL_TREE;
20388 else if (object_type)
20390 tree object_decl = NULL_TREE;
20391 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20392 OBJECT_TYPE is not a class. */
20393 if (CLASS_TYPE_P (object_type))
20394 /* If the OBJECT_TYPE is a template specialization, it may
20395 be instantiated during name lookup. In that case, errors
20396 may be issued. Even if we rollback the current tentative
20397 parse, those errors are valid. */
20398 object_decl = lookup_member (object_type,
20401 tag_type != none_type);
20402 /* Look it up in the enclosing context, too. */
20403 decl = lookup_name_real (name, tag_type != none_type,
20405 /*block_p=*/true, is_namespace, flags);
20406 parser->object_scope = object_type;
20407 parser->qualifying_scope = NULL_TREE;
20409 decl = object_decl;
20413 decl = lookup_name_real (name, tag_type != none_type,
20415 /*block_p=*/true, is_namespace, flags);
20416 parser->qualifying_scope = NULL_TREE;
20417 parser->object_scope = NULL_TREE;
20420 /* If the lookup failed, let our caller know. */
20421 if (!decl || decl == error_mark_node)
20422 return error_mark_node;
20424 /* Pull out the template from an injected-class-name (or multiple). */
20426 decl = maybe_get_template_decl_from_type_decl (decl);
20428 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20429 if (TREE_CODE (decl) == TREE_LIST)
20431 if (ambiguous_decls)
20432 *ambiguous_decls = decl;
20433 /* The error message we have to print is too complicated for
20434 cp_parser_error, so we incorporate its actions directly. */
20435 if (!cp_parser_simulate_error (parser))
20437 error_at (name_location, "reference to %qD is ambiguous",
20439 print_candidates (decl);
20441 return error_mark_node;
20444 gcc_assert (DECL_P (decl)
20445 || TREE_CODE (decl) == OVERLOAD
20446 || TREE_CODE (decl) == SCOPE_REF
20447 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20448 || BASELINK_P (decl));
20450 /* If we have resolved the name of a member declaration, check to
20451 see if the declaration is accessible. When the name resolves to
20452 set of overloaded functions, accessibility is checked when
20453 overload resolution is done.
20455 During an explicit instantiation, access is not checked at all,
20456 as per [temp.explicit]. */
20458 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20460 maybe_record_typedef_use (decl);
20465 /* Like cp_parser_lookup_name, but for use in the typical case where
20466 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20467 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20470 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20472 return cp_parser_lookup_name (parser, name,
20474 /*is_template=*/false,
20475 /*is_namespace=*/false,
20476 /*check_dependency=*/true,
20477 /*ambiguous_decls=*/NULL,
20481 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20482 the current context, return the TYPE_DECL. If TAG_NAME_P is
20483 true, the DECL indicates the class being defined in a class-head,
20484 or declared in an elaborated-type-specifier.
20486 Otherwise, return DECL. */
20489 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20491 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20492 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20495 template <typename T> struct B;
20498 template <typename T> struct A::B {};
20500 Similarly, in an elaborated-type-specifier:
20502 namespace N { struct X{}; }
20505 template <typename T> friend struct N::X;
20508 However, if the DECL refers to a class type, and we are in
20509 the scope of the class, then the name lookup automatically
20510 finds the TYPE_DECL created by build_self_reference rather
20511 than a TEMPLATE_DECL. For example, in:
20513 template <class T> struct S {
20517 there is no need to handle such case. */
20519 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20520 return DECL_TEMPLATE_RESULT (decl);
20525 /* If too many, or too few, template-parameter lists apply to the
20526 declarator, issue an error message. Returns TRUE if all went well,
20527 and FALSE otherwise. */
20530 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20531 cp_declarator *declarator,
20532 location_t declarator_location)
20534 unsigned num_templates;
20536 /* We haven't seen any classes that involve template parameters yet. */
20539 switch (declarator->kind)
20542 if (declarator->u.id.qualifying_scope)
20546 scope = declarator->u.id.qualifying_scope;
20548 while (scope && CLASS_TYPE_P (scope))
20550 /* You're supposed to have one `template <...>'
20551 for every template class, but you don't need one
20552 for a full specialization. For example:
20554 template <class T> struct S{};
20555 template <> struct S<int> { void f(); };
20556 void S<int>::f () {}
20558 is correct; there shouldn't be a `template <>' for
20559 the definition of `S<int>::f'. */
20560 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20561 /* If SCOPE does not have template information of any
20562 kind, then it is not a template, nor is it nested
20563 within a template. */
20565 if (explicit_class_specialization_p (scope))
20567 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20570 scope = TYPE_CONTEXT (scope);
20573 else if (TREE_CODE (declarator->u.id.unqualified_name)
20574 == TEMPLATE_ID_EXPR)
20575 /* If the DECLARATOR has the form `X<y>' then it uses one
20576 additional level of template parameters. */
20579 return cp_parser_check_template_parameters
20580 (parser, num_templates, declarator_location, declarator);
20586 case cdk_reference:
20588 return (cp_parser_check_declarator_template_parameters
20589 (parser, declarator->declarator, declarator_location));
20595 gcc_unreachable ();
20600 /* NUM_TEMPLATES were used in the current declaration. If that is
20601 invalid, return FALSE and issue an error messages. Otherwise,
20602 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20603 declarator and we can print more accurate diagnostics. */
20606 cp_parser_check_template_parameters (cp_parser* parser,
20607 unsigned num_templates,
20608 location_t location,
20609 cp_declarator *declarator)
20611 /* If there are the same number of template classes and parameter
20612 lists, that's OK. */
20613 if (parser->num_template_parameter_lists == num_templates)
20615 /* If there are more, but only one more, then we are referring to a
20616 member template. That's OK too. */
20617 if (parser->num_template_parameter_lists == num_templates + 1)
20619 /* If there are more template classes than parameter lists, we have
20622 template <class T> void S<T>::R<T>::f (); */
20623 if (parser->num_template_parameter_lists < num_templates)
20625 if (declarator && !current_function_decl)
20626 error_at (location, "specializing member %<%T::%E%> "
20627 "requires %<template<>%> syntax",
20628 declarator->u.id.qualifying_scope,
20629 declarator->u.id.unqualified_name);
20630 else if (declarator)
20631 error_at (location, "invalid declaration of %<%T::%E%>",
20632 declarator->u.id.qualifying_scope,
20633 declarator->u.id.unqualified_name);
20635 error_at (location, "too few template-parameter-lists");
20638 /* Otherwise, there are too many template parameter lists. We have
20641 template <class T> template <class U> void S::f(); */
20642 error_at (location, "too many template-parameter-lists");
20646 /* Parse an optional `::' token indicating that the following name is
20647 from the global namespace. If so, PARSER->SCOPE is set to the
20648 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20649 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20650 Returns the new value of PARSER->SCOPE, if the `::' token is
20651 present, and NULL_TREE otherwise. */
20654 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20658 /* Peek at the next token. */
20659 token = cp_lexer_peek_token (parser->lexer);
20660 /* If we're looking at a `::' token then we're starting from the
20661 global namespace, not our current location. */
20662 if (token->type == CPP_SCOPE)
20664 /* Consume the `::' token. */
20665 cp_lexer_consume_token (parser->lexer);
20666 /* Set the SCOPE so that we know where to start the lookup. */
20667 parser->scope = global_namespace;
20668 parser->qualifying_scope = global_namespace;
20669 parser->object_scope = NULL_TREE;
20671 return parser->scope;
20673 else if (!current_scope_valid_p)
20675 parser->scope = NULL_TREE;
20676 parser->qualifying_scope = NULL_TREE;
20677 parser->object_scope = NULL_TREE;
20683 /* Returns TRUE if the upcoming token sequence is the start of a
20684 constructor declarator. If FRIEND_P is true, the declarator is
20685 preceded by the `friend' specifier. */
20688 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20690 bool constructor_p;
20691 tree nested_name_specifier;
20692 cp_token *next_token;
20694 /* The common case is that this is not a constructor declarator, so
20695 try to avoid doing lots of work if at all possible. It's not
20696 valid declare a constructor at function scope. */
20697 if (parser->in_function_body)
20699 /* And only certain tokens can begin a constructor declarator. */
20700 next_token = cp_lexer_peek_token (parser->lexer);
20701 if (next_token->type != CPP_NAME
20702 && next_token->type != CPP_SCOPE
20703 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20704 && next_token->type != CPP_TEMPLATE_ID)
20707 /* Parse tentatively; we are going to roll back all of the tokens
20709 cp_parser_parse_tentatively (parser);
20710 /* Assume that we are looking at a constructor declarator. */
20711 constructor_p = true;
20713 /* Look for the optional `::' operator. */
20714 cp_parser_global_scope_opt (parser,
20715 /*current_scope_valid_p=*/false);
20716 /* Look for the nested-name-specifier. */
20717 nested_name_specifier
20718 = (cp_parser_nested_name_specifier_opt (parser,
20719 /*typename_keyword_p=*/false,
20720 /*check_dependency_p=*/false,
20722 /*is_declaration=*/false));
20723 /* Outside of a class-specifier, there must be a
20724 nested-name-specifier. */
20725 if (!nested_name_specifier &&
20726 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20728 constructor_p = false;
20729 else if (nested_name_specifier == error_mark_node)
20730 constructor_p = false;
20732 /* If we have a class scope, this is easy; DR 147 says that S::S always
20733 names the constructor, and no other qualified name could. */
20734 if (constructor_p && nested_name_specifier
20735 && CLASS_TYPE_P (nested_name_specifier))
20737 tree id = cp_parser_unqualified_id (parser,
20738 /*template_keyword_p=*/false,
20739 /*check_dependency_p=*/false,
20740 /*declarator_p=*/true,
20741 /*optional_p=*/false);
20742 if (is_overloaded_fn (id))
20743 id = DECL_NAME (get_first_fn (id));
20744 if (!constructor_name_p (id, nested_name_specifier))
20745 constructor_p = false;
20747 /* If we still think that this might be a constructor-declarator,
20748 look for a class-name. */
20749 else if (constructor_p)
20753 template <typename T> struct S {
20757 we must recognize that the nested `S' names a class. */
20759 type_decl = cp_parser_class_name (parser,
20760 /*typename_keyword_p=*/false,
20761 /*template_keyword_p=*/false,
20763 /*check_dependency_p=*/false,
20764 /*class_head_p=*/false,
20765 /*is_declaration=*/false);
20766 /* If there was no class-name, then this is not a constructor. */
20767 constructor_p = !cp_parser_error_occurred (parser);
20769 /* If we're still considering a constructor, we have to see a `(',
20770 to begin the parameter-declaration-clause, followed by either a
20771 `)', an `...', or a decl-specifier. We need to check for a
20772 type-specifier to avoid being fooled into thinking that:
20776 is a constructor. (It is actually a function named `f' that
20777 takes one parameter (of type `int') and returns a value of type
20780 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20781 constructor_p = false;
20784 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20785 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20786 /* A parameter declaration begins with a decl-specifier,
20787 which is either the "attribute" keyword, a storage class
20788 specifier, or (usually) a type-specifier. */
20789 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20792 tree pushed_scope = NULL_TREE;
20793 unsigned saved_num_template_parameter_lists;
20795 /* Names appearing in the type-specifier should be looked up
20796 in the scope of the class. */
20797 if (current_class_type)
20801 type = TREE_TYPE (type_decl);
20802 if (TREE_CODE (type) == TYPENAME_TYPE)
20804 type = resolve_typename_type (type,
20805 /*only_current_p=*/false);
20806 if (TREE_CODE (type) == TYPENAME_TYPE)
20808 cp_parser_abort_tentative_parse (parser);
20812 pushed_scope = push_scope (type);
20815 /* Inside the constructor parameter list, surrounding
20816 template-parameter-lists do not apply. */
20817 saved_num_template_parameter_lists
20818 = parser->num_template_parameter_lists;
20819 parser->num_template_parameter_lists = 0;
20821 /* Look for the type-specifier. */
20822 cp_parser_type_specifier (parser,
20823 CP_PARSER_FLAGS_NONE,
20824 /*decl_specs=*/NULL,
20825 /*is_declarator=*/true,
20826 /*declares_class_or_enum=*/NULL,
20827 /*is_cv_qualifier=*/NULL);
20829 parser->num_template_parameter_lists
20830 = saved_num_template_parameter_lists;
20832 /* Leave the scope of the class. */
20834 pop_scope (pushed_scope);
20836 constructor_p = !cp_parser_error_occurred (parser);
20840 /* We did not really want to consume any tokens. */
20841 cp_parser_abort_tentative_parse (parser);
20843 return constructor_p;
20846 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20847 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20848 they must be performed once we are in the scope of the function.
20850 Returns the function defined. */
20853 cp_parser_function_definition_from_specifiers_and_declarator
20854 (cp_parser* parser,
20855 cp_decl_specifier_seq *decl_specifiers,
20857 const cp_declarator *declarator)
20862 /* Begin the function-definition. */
20863 success_p = start_function (decl_specifiers, declarator, attributes);
20865 /* The things we're about to see are not directly qualified by any
20866 template headers we've seen thus far. */
20867 reset_specialization ();
20869 /* If there were names looked up in the decl-specifier-seq that we
20870 did not check, check them now. We must wait until we are in the
20871 scope of the function to perform the checks, since the function
20872 might be a friend. */
20873 perform_deferred_access_checks ();
20877 /* Skip the entire function. */
20878 cp_parser_skip_to_end_of_block_or_statement (parser);
20879 fn = error_mark_node;
20881 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20883 /* Seen already, skip it. An error message has already been output. */
20884 cp_parser_skip_to_end_of_block_or_statement (parser);
20885 fn = current_function_decl;
20886 current_function_decl = NULL_TREE;
20887 /* If this is a function from a class, pop the nested class. */
20888 if (current_class_name)
20889 pop_nested_class ();
20894 if (DECL_DECLARED_INLINE_P (current_function_decl))
20895 tv = TV_PARSE_INLINE;
20897 tv = TV_PARSE_FUNC;
20899 fn = cp_parser_function_definition_after_declarator (parser,
20900 /*inline_p=*/false);
20907 /* Parse the part of a function-definition that follows the
20908 declarator. INLINE_P is TRUE iff this function is an inline
20909 function defined within a class-specifier.
20911 Returns the function defined. */
20914 cp_parser_function_definition_after_declarator (cp_parser* parser,
20918 bool ctor_initializer_p = false;
20919 bool saved_in_unbraced_linkage_specification_p;
20920 bool saved_in_function_body;
20921 unsigned saved_num_template_parameter_lists;
20924 saved_in_function_body = parser->in_function_body;
20925 parser->in_function_body = true;
20926 /* If the next token is `return', then the code may be trying to
20927 make use of the "named return value" extension that G++ used to
20929 token = cp_lexer_peek_token (parser->lexer);
20930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20932 /* Consume the `return' keyword. */
20933 cp_lexer_consume_token (parser->lexer);
20934 /* Look for the identifier that indicates what value is to be
20936 cp_parser_identifier (parser);
20937 /* Issue an error message. */
20938 error_at (token->location,
20939 "named return values are no longer supported");
20940 /* Skip tokens until we reach the start of the function body. */
20943 cp_token *token = cp_lexer_peek_token (parser->lexer);
20944 if (token->type == CPP_OPEN_BRACE
20945 || token->type == CPP_EOF
20946 || token->type == CPP_PRAGMA_EOL)
20948 cp_lexer_consume_token (parser->lexer);
20951 /* The `extern' in `extern "C" void f () { ... }' does not apply to
20952 anything declared inside `f'. */
20953 saved_in_unbraced_linkage_specification_p
20954 = parser->in_unbraced_linkage_specification_p;
20955 parser->in_unbraced_linkage_specification_p = false;
20956 /* Inside the function, surrounding template-parameter-lists do not
20958 saved_num_template_parameter_lists
20959 = parser->num_template_parameter_lists;
20960 parser->num_template_parameter_lists = 0;
20962 start_lambda_scope (current_function_decl);
20964 /* If the next token is `try', `__transaction_atomic', or
20965 `__transaction_relaxed`, then we are looking at either function-try-block
20966 or function-transaction-block. Note that all of these include the
20968 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
20969 ctor_initializer_p = cp_parser_function_transaction (parser,
20970 RID_TRANSACTION_ATOMIC);
20971 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20972 RID_TRANSACTION_RELAXED))
20973 ctor_initializer_p = cp_parser_function_transaction (parser,
20974 RID_TRANSACTION_RELAXED);
20975 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20976 ctor_initializer_p = cp_parser_function_try_block (parser);
20979 = cp_parser_ctor_initializer_opt_and_function_body (parser);
20981 finish_lambda_scope ();
20983 /* Finish the function. */
20984 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20985 (inline_p ? 2 : 0));
20986 /* Generate code for it, if necessary. */
20987 expand_or_defer_fn (fn);
20988 /* Restore the saved values. */
20989 parser->in_unbraced_linkage_specification_p
20990 = saved_in_unbraced_linkage_specification_p;
20991 parser->num_template_parameter_lists
20992 = saved_num_template_parameter_lists;
20993 parser->in_function_body = saved_in_function_body;
20998 /* Parse a template-declaration, assuming that the `export' (and
20999 `extern') keywords, if present, has already been scanned. MEMBER_P
21000 is as for cp_parser_template_declaration. */
21003 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21005 tree decl = NULL_TREE;
21006 VEC (deferred_access_check,gc) *checks;
21007 tree parameter_list;
21008 bool friend_p = false;
21009 bool need_lang_pop;
21012 /* Look for the `template' keyword. */
21013 token = cp_lexer_peek_token (parser->lexer);
21014 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21018 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21020 if (at_class_scope_p () && current_function_decl)
21022 /* 14.5.2.2 [temp.mem]
21024 A local class shall not have member templates. */
21025 error_at (token->location,
21026 "invalid declaration of member template in local class");
21027 cp_parser_skip_to_end_of_block_or_statement (parser);
21032 A template ... shall not have C linkage. */
21033 if (current_lang_name == lang_name_c)
21035 error_at (token->location, "template with C linkage");
21036 /* Give it C++ linkage to avoid confusing other parts of the
21038 push_lang_context (lang_name_cplusplus);
21039 need_lang_pop = true;
21042 need_lang_pop = false;
21044 /* We cannot perform access checks on the template parameter
21045 declarations until we know what is being declared, just as we
21046 cannot check the decl-specifier list. */
21047 push_deferring_access_checks (dk_deferred);
21049 /* If the next token is `>', then we have an invalid
21050 specialization. Rather than complain about an invalid template
21051 parameter, issue an error message here. */
21052 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21054 cp_parser_error (parser, "invalid explicit specialization");
21055 begin_specialization ();
21056 parameter_list = NULL_TREE;
21060 /* Parse the template parameters. */
21061 parameter_list = cp_parser_template_parameter_list (parser);
21062 fixup_template_parms ();
21065 /* Get the deferred access checks from the parameter list. These
21066 will be checked once we know what is being declared, as for a
21067 member template the checks must be performed in the scope of the
21068 class containing the member. */
21069 checks = get_deferred_access_checks ();
21071 /* Look for the `>'. */
21072 cp_parser_skip_to_end_of_template_parameter_list (parser);
21073 /* We just processed one more parameter list. */
21074 ++parser->num_template_parameter_lists;
21075 /* If the next token is `template', there are more template
21077 if (cp_lexer_next_token_is_keyword (parser->lexer,
21079 cp_parser_template_declaration_after_export (parser, member_p);
21080 else if (cxx_dialect >= cxx0x
21081 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21082 decl = cp_parser_alias_declaration (parser);
21085 /* There are no access checks when parsing a template, as we do not
21086 know if a specialization will be a friend. */
21087 push_deferring_access_checks (dk_no_check);
21088 token = cp_lexer_peek_token (parser->lexer);
21089 decl = cp_parser_single_declaration (parser,
21092 /*explicit_specialization_p=*/false,
21094 pop_deferring_access_checks ();
21096 /* If this is a member template declaration, let the front
21098 if (member_p && !friend_p && decl)
21100 if (TREE_CODE (decl) == TYPE_DECL)
21101 cp_parser_check_access_in_redeclaration (decl, token->location);
21103 decl = finish_member_template_decl (decl);
21105 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21106 make_friend_class (current_class_type, TREE_TYPE (decl),
21107 /*complain=*/true);
21109 /* We are done with the current parameter list. */
21110 --parser->num_template_parameter_lists;
21112 pop_deferring_access_checks ();
21115 finish_template_decl (parameter_list);
21117 /* Check the template arguments for a literal operator template. */
21119 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21120 && UDLIT_OPER_P (DECL_NAME (decl)))
21123 if (parameter_list == NULL_TREE)
21127 int num_parms = TREE_VEC_LENGTH (parameter_list);
21128 if (num_parms != 1)
21132 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21133 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21134 if (TREE_TYPE (parm) != char_type_node
21135 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21140 error ("literal operator template %qD has invalid parameter list."
21141 " Expected non-type template argument pack <char...>",
21144 /* Register member declarations. */
21145 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21146 finish_member_declaration (decl);
21147 /* For the erroneous case of a template with C linkage, we pushed an
21148 implicit C++ linkage scope; exit that scope now. */
21150 pop_lang_context ();
21151 /* If DECL is a function template, we must return to parse it later.
21152 (Even though there is no definition, there might be default
21153 arguments that need handling.) */
21154 if (member_p && decl
21155 && (TREE_CODE (decl) == FUNCTION_DECL
21156 || DECL_FUNCTION_TEMPLATE_P (decl)))
21157 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21160 /* Perform the deferred access checks from a template-parameter-list.
21161 CHECKS is a TREE_LIST of access checks, as returned by
21162 get_deferred_access_checks. */
21165 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21167 ++processing_template_parmlist;
21168 perform_access_checks (checks);
21169 --processing_template_parmlist;
21172 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21173 `function-definition' sequence. MEMBER_P is true, this declaration
21174 appears in a class scope.
21176 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21177 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21180 cp_parser_single_declaration (cp_parser* parser,
21181 VEC (deferred_access_check,gc)* checks,
21183 bool explicit_specialization_p,
21186 int declares_class_or_enum;
21187 tree decl = NULL_TREE;
21188 cp_decl_specifier_seq decl_specifiers;
21189 bool function_definition_p = false;
21190 cp_token *decl_spec_token_start;
21192 /* This function is only used when processing a template
21194 gcc_assert (innermost_scope_kind () == sk_template_parms
21195 || innermost_scope_kind () == sk_template_spec);
21197 /* Defer access checks until we know what is being declared. */
21198 push_deferring_access_checks (dk_deferred);
21200 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21202 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21203 cp_parser_decl_specifier_seq (parser,
21204 CP_PARSER_FLAGS_OPTIONAL,
21206 &declares_class_or_enum);
21208 *friend_p = cp_parser_friend_p (&decl_specifiers);
21210 /* There are no template typedefs. */
21211 if (decl_specifiers.specs[(int) ds_typedef])
21213 error_at (decl_spec_token_start->location,
21214 "template declaration of %<typedef%>");
21215 decl = error_mark_node;
21218 /* Gather up the access checks that occurred the
21219 decl-specifier-seq. */
21220 stop_deferring_access_checks ();
21222 /* Check for the declaration of a template class. */
21223 if (declares_class_or_enum)
21225 if (cp_parser_declares_only_class_p (parser))
21227 decl = shadow_tag (&decl_specifiers);
21232 friend template <typename T> struct A<T>::B;
21235 A<T>::B will be represented by a TYPENAME_TYPE, and
21236 therefore not recognized by shadow_tag. */
21237 if (friend_p && *friend_p
21239 && decl_specifiers.type
21240 && TYPE_P (decl_specifiers.type))
21241 decl = decl_specifiers.type;
21243 if (decl && decl != error_mark_node)
21244 decl = TYPE_NAME (decl);
21246 decl = error_mark_node;
21248 /* Perform access checks for template parameters. */
21249 cp_parser_perform_template_parameter_access_checks (checks);
21253 /* Complain about missing 'typename' or other invalid type names. */
21254 if (!decl_specifiers.any_type_specifiers_p
21255 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21257 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21258 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21259 the rest of this declaration. */
21260 decl = error_mark_node;
21264 /* If it's not a template class, try for a template function. If
21265 the next token is a `;', then this declaration does not declare
21266 anything. But, if there were errors in the decl-specifiers, then
21267 the error might well have come from an attempted class-specifier.
21268 In that case, there's no need to warn about a missing declarator. */
21270 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21271 || decl_specifiers.type != error_mark_node))
21273 decl = cp_parser_init_declarator (parser,
21276 /*function_definition_allowed_p=*/true,
21278 declares_class_or_enum,
21279 &function_definition_p,
21282 /* 7.1.1-1 [dcl.stc]
21284 A storage-class-specifier shall not be specified in an explicit
21285 specialization... */
21287 && explicit_specialization_p
21288 && decl_specifiers.storage_class != sc_none)
21290 error_at (decl_spec_token_start->location,
21291 "explicit template specialization cannot have a storage class");
21292 decl = error_mark_node;
21296 /* Look for a trailing `;' after the declaration. */
21297 if (!function_definition_p
21298 && (decl == error_mark_node
21299 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21300 cp_parser_skip_to_end_of_block_or_statement (parser);
21303 pop_deferring_access_checks ();
21305 /* Clear any current qualification; whatever comes next is the start
21306 of something new. */
21307 parser->scope = NULL_TREE;
21308 parser->qualifying_scope = NULL_TREE;
21309 parser->object_scope = NULL_TREE;
21314 /* Parse a cast-expression that is not the operand of a unary "&". */
21317 cp_parser_simple_cast_expression (cp_parser *parser)
21319 return cp_parser_cast_expression (parser, /*address_p=*/false,
21320 /*cast_p=*/false, NULL);
21323 /* Parse a functional cast to TYPE. Returns an expression
21324 representing the cast. */
21327 cp_parser_functional_cast (cp_parser* parser, tree type)
21330 tree expression_list;
21334 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21336 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21337 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21338 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21339 if (TREE_CODE (type) == TYPE_DECL)
21340 type = TREE_TYPE (type);
21341 return finish_compound_literal (type, expression_list,
21342 tf_warning_or_error);
21346 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21348 /*allow_expansion_p=*/true,
21349 /*non_constant_p=*/NULL);
21351 expression_list = error_mark_node;
21354 expression_list = build_tree_list_vec (vec);
21355 release_tree_vector (vec);
21358 cast = build_functional_cast (type, expression_list,
21359 tf_warning_or_error);
21360 /* [expr.const]/1: In an integral constant expression "only type
21361 conversions to integral or enumeration type can be used". */
21362 if (TREE_CODE (type) == TYPE_DECL)
21363 type = TREE_TYPE (type);
21364 if (cast != error_mark_node
21365 && !cast_valid_in_integral_constant_expression_p (type)
21366 && cp_parser_non_integral_constant_expression (parser,
21368 return error_mark_node;
21372 /* Save the tokens that make up the body of a member function defined
21373 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21374 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21375 specifiers applied to the declaration. Returns the FUNCTION_DECL
21376 for the member function. */
21379 cp_parser_save_member_function_body (cp_parser* parser,
21380 cp_decl_specifier_seq *decl_specifiers,
21381 cp_declarator *declarator,
21388 /* Create the FUNCTION_DECL. */
21389 fn = grokmethod (decl_specifiers, declarator, attributes);
21390 /* If something went badly wrong, bail out now. */
21391 if (fn == error_mark_node)
21393 /* If there's a function-body, skip it. */
21394 if (cp_parser_token_starts_function_definition_p
21395 (cp_lexer_peek_token (parser->lexer)))
21396 cp_parser_skip_to_end_of_block_or_statement (parser);
21397 return error_mark_node;
21400 /* Remember it, if there default args to post process. */
21401 cp_parser_save_default_args (parser, fn);
21403 /* Save away the tokens that make up the body of the
21405 first = parser->lexer->next_token;
21406 /* We can have braced-init-list mem-initializers before the fn body. */
21407 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21409 cp_lexer_consume_token (parser->lexer);
21410 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21411 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21413 /* cache_group will stop after an un-nested { } pair, too. */
21414 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21417 /* variadic mem-inits have ... after the ')'. */
21418 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21419 cp_lexer_consume_token (parser->lexer);
21422 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21423 /* Handle function try blocks. */
21424 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21425 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21426 last = parser->lexer->next_token;
21428 /* Save away the inline definition; we will process it when the
21429 class is complete. */
21430 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21431 DECL_PENDING_INLINE_P (fn) = 1;
21433 /* We need to know that this was defined in the class, so that
21434 friend templates are handled correctly. */
21435 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21437 /* Add FN to the queue of functions to be parsed later. */
21438 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21443 /* Save the tokens that make up the in-class initializer for a non-static
21444 data member. Returns a DEFAULT_ARG. */
21447 cp_parser_save_nsdmi (cp_parser* parser)
21449 /* Save away the tokens that make up the body of the
21451 cp_token *first = parser->lexer->next_token;
21455 /* Save tokens until the next comma or semicolon. */
21456 cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21458 last = parser->lexer->next_token;
21460 node = make_node (DEFAULT_ARG);
21461 DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21462 DEFARG_INSTANTIATIONS (node) = NULL;
21468 /* Parse a template-argument-list, as well as the trailing ">" (but
21469 not the opening "<"). See cp_parser_template_argument_list for the
21473 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21477 tree saved_qualifying_scope;
21478 tree saved_object_scope;
21479 bool saved_greater_than_is_operator_p;
21480 int saved_unevaluated_operand;
21481 int saved_inhibit_evaluation_warnings;
21485 When parsing a template-id, the first non-nested `>' is taken as
21486 the end of the template-argument-list rather than a greater-than
21488 saved_greater_than_is_operator_p
21489 = parser->greater_than_is_operator_p;
21490 parser->greater_than_is_operator_p = false;
21491 /* Parsing the argument list may modify SCOPE, so we save it
21493 saved_scope = parser->scope;
21494 saved_qualifying_scope = parser->qualifying_scope;
21495 saved_object_scope = parser->object_scope;
21496 /* We need to evaluate the template arguments, even though this
21497 template-id may be nested within a "sizeof". */
21498 saved_unevaluated_operand = cp_unevaluated_operand;
21499 cp_unevaluated_operand = 0;
21500 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21501 c_inhibit_evaluation_warnings = 0;
21502 /* Parse the template-argument-list itself. */
21503 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21504 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21505 arguments = NULL_TREE;
21507 arguments = cp_parser_template_argument_list (parser);
21508 /* Look for the `>' that ends the template-argument-list. If we find
21509 a '>>' instead, it's probably just a typo. */
21510 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21512 if (cxx_dialect != cxx98)
21514 /* In C++0x, a `>>' in a template argument list or cast
21515 expression is considered to be two separate `>'
21516 tokens. So, change the current token to a `>', but don't
21517 consume it: it will be consumed later when the outer
21518 template argument list (or cast expression) is parsed.
21519 Note that this replacement of `>' for `>>' is necessary
21520 even if we are parsing tentatively: in the tentative
21521 case, after calling
21522 cp_parser_enclosed_template_argument_list we will always
21523 throw away all of the template arguments and the first
21524 closing `>', either because the template argument list
21525 was erroneous or because we are replacing those tokens
21526 with a CPP_TEMPLATE_ID token. The second `>' (which will
21527 not have been thrown away) is needed either to close an
21528 outer template argument list or to complete a new-style
21530 cp_token *token = cp_lexer_peek_token (parser->lexer);
21531 token->type = CPP_GREATER;
21533 else if (!saved_greater_than_is_operator_p)
21535 /* If we're in a nested template argument list, the '>>' has
21536 to be a typo for '> >'. We emit the error message, but we
21537 continue parsing and we push a '>' as next token, so that
21538 the argument list will be parsed correctly. Note that the
21539 global source location is still on the token before the
21540 '>>', so we need to say explicitly where we want it. */
21541 cp_token *token = cp_lexer_peek_token (parser->lexer);
21542 error_at (token->location, "%<>>%> should be %<> >%> "
21543 "within a nested template argument list");
21545 token->type = CPP_GREATER;
21549 /* If this is not a nested template argument list, the '>>'
21550 is a typo for '>'. Emit an error message and continue.
21551 Same deal about the token location, but here we can get it
21552 right by consuming the '>>' before issuing the diagnostic. */
21553 cp_token *token = cp_lexer_consume_token (parser->lexer);
21554 error_at (token->location,
21555 "spurious %<>>%>, use %<>%> to terminate "
21556 "a template argument list");
21560 cp_parser_skip_to_end_of_template_parameter_list (parser);
21561 /* The `>' token might be a greater-than operator again now. */
21562 parser->greater_than_is_operator_p
21563 = saved_greater_than_is_operator_p;
21564 /* Restore the SAVED_SCOPE. */
21565 parser->scope = saved_scope;
21566 parser->qualifying_scope = saved_qualifying_scope;
21567 parser->object_scope = saved_object_scope;
21568 cp_unevaluated_operand = saved_unevaluated_operand;
21569 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21574 /* MEMBER_FUNCTION is a member function, or a friend. If default
21575 arguments, or the body of the function have not yet been parsed,
21579 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21581 timevar_push (TV_PARSE_INMETH);
21582 /* If this member is a template, get the underlying
21584 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21585 member_function = DECL_TEMPLATE_RESULT (member_function);
21587 /* There should not be any class definitions in progress at this
21588 point; the bodies of members are only parsed outside of all class
21590 gcc_assert (parser->num_classes_being_defined == 0);
21591 /* While we're parsing the member functions we might encounter more
21592 classes. We want to handle them right away, but we don't want
21593 them getting mixed up with functions that are currently in the
21595 push_unparsed_function_queues (parser);
21597 /* Make sure that any template parameters are in scope. */
21598 maybe_begin_member_template_processing (member_function);
21600 /* If the body of the function has not yet been parsed, parse it
21602 if (DECL_PENDING_INLINE_P (member_function))
21604 tree function_scope;
21605 cp_token_cache *tokens;
21607 /* The function is no longer pending; we are processing it. */
21608 tokens = DECL_PENDING_INLINE_INFO (member_function);
21609 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21610 DECL_PENDING_INLINE_P (member_function) = 0;
21612 /* If this is a local class, enter the scope of the containing
21614 function_scope = current_function_decl;
21615 if (function_scope)
21616 push_function_context ();
21618 /* Push the body of the function onto the lexer stack. */
21619 cp_parser_push_lexer_for_tokens (parser, tokens);
21621 /* Let the front end know that we going to be defining this
21623 start_preparsed_function (member_function, NULL_TREE,
21624 SF_PRE_PARSED | SF_INCLASS_INLINE);
21626 /* Don't do access checking if it is a templated function. */
21627 if (processing_template_decl)
21628 push_deferring_access_checks (dk_no_check);
21630 /* Now, parse the body of the function. */
21631 cp_parser_function_definition_after_declarator (parser,
21632 /*inline_p=*/true);
21634 if (processing_template_decl)
21635 pop_deferring_access_checks ();
21637 /* Leave the scope of the containing function. */
21638 if (function_scope)
21639 pop_function_context ();
21640 cp_parser_pop_lexer (parser);
21643 /* Remove any template parameters from the symbol table. */
21644 maybe_end_member_template_processing ();
21646 /* Restore the queue. */
21647 pop_unparsed_function_queues (parser);
21648 timevar_pop (TV_PARSE_INMETH);
21651 /* If DECL contains any default args, remember it on the unparsed
21652 functions queue. */
21655 cp_parser_save_default_args (cp_parser* parser, tree decl)
21659 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21661 probe = TREE_CHAIN (probe))
21662 if (TREE_PURPOSE (probe))
21664 cp_default_arg_entry *entry
21665 = VEC_safe_push (cp_default_arg_entry, gc,
21666 unparsed_funs_with_default_args, NULL);
21667 entry->class_type = current_class_type;
21668 entry->decl = decl;
21673 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21674 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21675 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21676 from the parameter-type-list. */
21679 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21680 tree default_arg, tree parmtype)
21682 cp_token_cache *tokens;
21686 /* Push the saved tokens for the default argument onto the parser's
21688 tokens = DEFARG_TOKENS (default_arg);
21689 cp_parser_push_lexer_for_tokens (parser, tokens);
21691 start_lambda_scope (decl);
21693 /* Parse the default argument. */
21694 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21695 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21696 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21698 finish_lambda_scope ();
21700 if (!processing_template_decl)
21702 /* In a non-template class, check conversions now. In a template,
21703 we'll wait and instantiate these as needed. */
21704 if (TREE_CODE (decl) == PARM_DECL)
21705 parsed_arg = check_default_argument (parmtype, parsed_arg);
21708 int flags = LOOKUP_IMPLICIT;
21709 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21710 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21711 flags = LOOKUP_NORMAL;
21712 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21716 /* If the token stream has not been completely used up, then
21717 there was extra junk after the end of the default
21719 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21721 if (TREE_CODE (decl) == PARM_DECL)
21722 cp_parser_error (parser, "expected %<,%>");
21724 cp_parser_error (parser, "expected %<;%>");
21727 /* Revert to the main lexer. */
21728 cp_parser_pop_lexer (parser);
21733 /* FIELD is a non-static data member with an initializer which we saved for
21734 later; parse it now. */
21737 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21741 push_unparsed_function_queues (parser);
21742 def = cp_parser_late_parse_one_default_arg (parser, field,
21743 DECL_INITIAL (field),
21745 pop_unparsed_function_queues (parser);
21747 DECL_INITIAL (field) = def;
21750 /* FN is a FUNCTION_DECL which may contains a parameter with an
21751 unparsed DEFAULT_ARG. Parse the default args now. This function
21752 assumes that the current scope is the scope in which the default
21753 argument should be processed. */
21756 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21758 bool saved_local_variables_forbidden_p;
21759 tree parm, parmdecl;
21761 /* While we're parsing the default args, we might (due to the
21762 statement expression extension) encounter more classes. We want
21763 to handle them right away, but we don't want them getting mixed
21764 up with default args that are currently in the queue. */
21765 push_unparsed_function_queues (parser);
21767 /* Local variable names (and the `this' keyword) may not appear
21768 in a default argument. */
21769 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21770 parser->local_variables_forbidden_p = true;
21772 push_defarg_context (fn);
21774 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21775 parmdecl = DECL_ARGUMENTS (fn);
21776 parm && parm != void_list_node;
21777 parm = TREE_CHAIN (parm),
21778 parmdecl = DECL_CHAIN (parmdecl))
21780 tree default_arg = TREE_PURPOSE (parm);
21782 VEC(tree,gc) *insts;
21789 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21790 /* This can happen for a friend declaration for a function
21791 already declared with default arguments. */
21795 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21797 TREE_VALUE (parm));
21798 if (parsed_arg == error_mark_node)
21803 TREE_PURPOSE (parm) = parsed_arg;
21805 /* Update any instantiations we've already created. */
21806 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21807 VEC_iterate (tree, insts, ix, copy); ix++)
21808 TREE_PURPOSE (copy) = parsed_arg;
21811 pop_defarg_context ();
21813 /* Make sure no default arg is missing. */
21814 check_default_args (fn);
21816 /* Restore the state of local_variables_forbidden_p. */
21817 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21819 /* Restore the queue. */
21820 pop_unparsed_function_queues (parser);
21823 /* Parse the operand of `sizeof' (or a similar operator). Returns
21824 either a TYPE or an expression, depending on the form of the
21825 input. The KEYWORD indicates which kind of expression we have
21829 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21831 tree expr = NULL_TREE;
21832 const char *saved_message;
21834 bool saved_integral_constant_expression_p;
21835 bool saved_non_integral_constant_expression_p;
21836 bool pack_expansion_p = false;
21838 /* Types cannot be defined in a `sizeof' expression. Save away the
21840 saved_message = parser->type_definition_forbidden_message;
21841 /* And create the new one. */
21842 tmp = concat ("types may not be defined in %<",
21843 IDENTIFIER_POINTER (ridpointers[keyword]),
21844 "%> expressions", NULL);
21845 parser->type_definition_forbidden_message = tmp;
21847 /* The restrictions on constant-expressions do not apply inside
21848 sizeof expressions. */
21849 saved_integral_constant_expression_p
21850 = parser->integral_constant_expression_p;
21851 saved_non_integral_constant_expression_p
21852 = parser->non_integral_constant_expression_p;
21853 parser->integral_constant_expression_p = false;
21855 /* If it's a `...', then we are computing the length of a parameter
21857 if (keyword == RID_SIZEOF
21858 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21860 /* Consume the `...'. */
21861 cp_lexer_consume_token (parser->lexer);
21862 maybe_warn_variadic_templates ();
21864 /* Note that this is an expansion. */
21865 pack_expansion_p = true;
21868 /* Do not actually evaluate the expression. */
21869 ++cp_unevaluated_operand;
21870 ++c_inhibit_evaluation_warnings;
21871 /* If it's a `(', then we might be looking at the type-id
21873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21876 bool saved_in_type_id_in_expr_p;
21878 /* We can't be sure yet whether we're looking at a type-id or an
21880 cp_parser_parse_tentatively (parser);
21881 /* Consume the `('. */
21882 cp_lexer_consume_token (parser->lexer);
21883 /* Parse the type-id. */
21884 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21885 parser->in_type_id_in_expr_p = true;
21886 type = cp_parser_type_id (parser);
21887 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21888 /* Now, look for the trailing `)'. */
21889 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21890 /* If all went well, then we're done. */
21891 if (cp_parser_parse_definitely (parser))
21893 cp_decl_specifier_seq decl_specs;
21895 /* Build a trivial decl-specifier-seq. */
21896 clear_decl_specs (&decl_specs);
21897 decl_specs.type = type;
21899 /* Call grokdeclarator to figure out what type this is. */
21900 expr = grokdeclarator (NULL,
21904 /*attrlist=*/NULL);
21908 /* If the type-id production did not work out, then we must be
21909 looking at the unary-expression production. */
21911 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21912 /*cast_p=*/false, NULL);
21914 if (pack_expansion_p)
21915 /* Build a pack expansion. */
21916 expr = make_pack_expansion (expr);
21918 /* Go back to evaluating expressions. */
21919 --cp_unevaluated_operand;
21920 --c_inhibit_evaluation_warnings;
21922 /* Free the message we created. */
21924 /* And restore the old one. */
21925 parser->type_definition_forbidden_message = saved_message;
21926 parser->integral_constant_expression_p
21927 = saved_integral_constant_expression_p;
21928 parser->non_integral_constant_expression_p
21929 = saved_non_integral_constant_expression_p;
21934 /* If the current declaration has no declarator, return true. */
21937 cp_parser_declares_only_class_p (cp_parser *parser)
21939 /* If the next token is a `;' or a `,' then there is no
21941 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21942 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21945 /* Update the DECL_SPECS to reflect the storage class indicated by
21949 cp_parser_set_storage_class (cp_parser *parser,
21950 cp_decl_specifier_seq *decl_specs,
21952 location_t location)
21954 cp_storage_class storage_class;
21956 if (parser->in_unbraced_linkage_specification_p)
21958 error_at (location, "invalid use of %qD in linkage specification",
21959 ridpointers[keyword]);
21962 else if (decl_specs->storage_class != sc_none)
21964 decl_specs->conflicting_specifiers_p = true;
21968 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21969 && decl_specs->specs[(int) ds_thread])
21971 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21972 decl_specs->specs[(int) ds_thread] = 0;
21978 storage_class = sc_auto;
21981 storage_class = sc_register;
21984 storage_class = sc_static;
21987 storage_class = sc_extern;
21990 storage_class = sc_mutable;
21993 gcc_unreachable ();
21995 decl_specs->storage_class = storage_class;
21997 /* A storage class specifier cannot be applied alongside a typedef
21998 specifier. If there is a typedef specifier present then set
21999 conflicting_specifiers_p which will trigger an error later
22000 on in grokdeclarator. */
22001 if (decl_specs->specs[(int)ds_typedef])
22002 decl_specs->conflicting_specifiers_p = true;
22005 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22006 is true, the type is a class or enum definition. */
22009 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22011 location_t location,
22012 bool type_definition_p)
22014 decl_specs->any_specifiers_p = true;
22016 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22017 (with, for example, in "typedef int wchar_t;") we remember that
22018 this is what happened. In system headers, we ignore these
22019 declarations so that G++ can work with system headers that are not
22021 if (decl_specs->specs[(int) ds_typedef]
22022 && !type_definition_p
22023 && (type_spec == boolean_type_node
22024 || type_spec == char16_type_node
22025 || type_spec == char32_type_node
22026 || type_spec == wchar_type_node)
22027 && (decl_specs->type
22028 || decl_specs->specs[(int) ds_long]
22029 || decl_specs->specs[(int) ds_short]
22030 || decl_specs->specs[(int) ds_unsigned]
22031 || decl_specs->specs[(int) ds_signed]))
22033 decl_specs->redefined_builtin_type = type_spec;
22034 if (!decl_specs->type)
22036 decl_specs->type = type_spec;
22037 decl_specs->type_definition_p = false;
22038 decl_specs->type_location = location;
22041 else if (decl_specs->type)
22042 decl_specs->multiple_types_p = true;
22045 decl_specs->type = type_spec;
22046 decl_specs->type_definition_p = type_definition_p;
22047 decl_specs->redefined_builtin_type = NULL_TREE;
22048 decl_specs->type_location = location;
22052 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22053 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22056 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22058 return decl_specifiers->specs[(int) ds_friend] != 0;
22061 /* Issue an error message indicating that TOKEN_DESC was expected.
22062 If KEYWORD is true, it indicated this function is called by
22063 cp_parser_require_keword and the required token can only be
22064 a indicated keyword. */
22067 cp_parser_required_error (cp_parser *parser,
22068 required_token token_desc,
22071 switch (token_desc)
22074 cp_parser_error (parser, "expected %<new%>");
22077 cp_parser_error (parser, "expected %<delete%>");
22080 cp_parser_error (parser, "expected %<return%>");
22083 cp_parser_error (parser, "expected %<while%>");
22086 cp_parser_error (parser, "expected %<extern%>");
22088 case RT_STATIC_ASSERT:
22089 cp_parser_error (parser, "expected %<static_assert%>");
22092 cp_parser_error (parser, "expected %<decltype%>");
22095 cp_parser_error (parser, "expected %<operator%>");
22098 cp_parser_error (parser, "expected %<class%>");
22101 cp_parser_error (parser, "expected %<template%>");
22104 cp_parser_error (parser, "expected %<namespace%>");
22107 cp_parser_error (parser, "expected %<using%>");
22110 cp_parser_error (parser, "expected %<asm%>");
22113 cp_parser_error (parser, "expected %<try%>");
22116 cp_parser_error (parser, "expected %<catch%>");
22119 cp_parser_error (parser, "expected %<throw%>");
22122 cp_parser_error (parser, "expected %<__label__%>");
22125 cp_parser_error (parser, "expected %<@try%>");
22127 case RT_AT_SYNCHRONIZED:
22128 cp_parser_error (parser, "expected %<@synchronized%>");
22131 cp_parser_error (parser, "expected %<@throw%>");
22133 case RT_TRANSACTION_ATOMIC:
22134 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22136 case RT_TRANSACTION_RELAXED:
22137 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22144 switch (token_desc)
22147 cp_parser_error (parser, "expected %<;%>");
22149 case RT_OPEN_PAREN:
22150 cp_parser_error (parser, "expected %<(%>");
22152 case RT_CLOSE_BRACE:
22153 cp_parser_error (parser, "expected %<}%>");
22155 case RT_OPEN_BRACE:
22156 cp_parser_error (parser, "expected %<{%>");
22158 case RT_CLOSE_SQUARE:
22159 cp_parser_error (parser, "expected %<]%>");
22161 case RT_OPEN_SQUARE:
22162 cp_parser_error (parser, "expected %<[%>");
22165 cp_parser_error (parser, "expected %<,%>");
22168 cp_parser_error (parser, "expected %<::%>");
22171 cp_parser_error (parser, "expected %<<%>");
22174 cp_parser_error (parser, "expected %<>%>");
22177 cp_parser_error (parser, "expected %<=%>");
22180 cp_parser_error (parser, "expected %<...%>");
22183 cp_parser_error (parser, "expected %<*%>");
22186 cp_parser_error (parser, "expected %<~%>");
22189 cp_parser_error (parser, "expected %<:%>");
22191 case RT_COLON_SCOPE:
22192 cp_parser_error (parser, "expected %<:%> or %<::%>");
22194 case RT_CLOSE_PAREN:
22195 cp_parser_error (parser, "expected %<)%>");
22197 case RT_COMMA_CLOSE_PAREN:
22198 cp_parser_error (parser, "expected %<,%> or %<)%>");
22200 case RT_PRAGMA_EOL:
22201 cp_parser_error (parser, "expected end of line");
22204 cp_parser_error (parser, "expected identifier");
22207 cp_parser_error (parser, "expected selection-statement");
22209 case RT_INTERATION:
22210 cp_parser_error (parser, "expected iteration-statement");
22213 cp_parser_error (parser, "expected jump-statement");
22216 cp_parser_error (parser, "expected class-key");
22218 case RT_CLASS_TYPENAME_TEMPLATE:
22219 cp_parser_error (parser,
22220 "expected %<class%>, %<typename%>, or %<template%>");
22223 gcc_unreachable ();
22227 gcc_unreachable ();
22232 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22233 issue an error message indicating that TOKEN_DESC was expected.
22235 Returns the token consumed, if the token had the appropriate type.
22236 Otherwise, returns NULL. */
22239 cp_parser_require (cp_parser* parser,
22240 enum cpp_ttype type,
22241 required_token token_desc)
22243 if (cp_lexer_next_token_is (parser->lexer, type))
22244 return cp_lexer_consume_token (parser->lexer);
22247 /* Output the MESSAGE -- unless we're parsing tentatively. */
22248 if (!cp_parser_simulate_error (parser))
22249 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22254 /* An error message is produced if the next token is not '>'.
22255 All further tokens are skipped until the desired token is
22256 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22259 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22261 /* Current level of '< ... >'. */
22262 unsigned level = 0;
22263 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22264 unsigned nesting_depth = 0;
22266 /* Are we ready, yet? If not, issue error message. */
22267 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22270 /* Skip tokens until the desired token is found. */
22273 /* Peek at the next token. */
22274 switch (cp_lexer_peek_token (parser->lexer)->type)
22277 if (!nesting_depth)
22282 if (cxx_dialect == cxx98)
22283 /* C++0x views the `>>' operator as two `>' tokens, but
22286 else if (!nesting_depth && level-- == 0)
22288 /* We've hit a `>>' where the first `>' closes the
22289 template argument list, and the second `>' is
22290 spurious. Just consume the `>>' and stop; we've
22291 already produced at least one error. */
22292 cp_lexer_consume_token (parser->lexer);
22295 /* Fall through for C++0x, so we handle the second `>' in
22299 if (!nesting_depth && level-- == 0)
22301 /* We've reached the token we want, consume it and stop. */
22302 cp_lexer_consume_token (parser->lexer);
22307 case CPP_OPEN_PAREN:
22308 case CPP_OPEN_SQUARE:
22312 case CPP_CLOSE_PAREN:
22313 case CPP_CLOSE_SQUARE:
22314 if (nesting_depth-- == 0)
22319 case CPP_PRAGMA_EOL:
22320 case CPP_SEMICOLON:
22321 case CPP_OPEN_BRACE:
22322 case CPP_CLOSE_BRACE:
22323 /* The '>' was probably forgotten, don't look further. */
22330 /* Consume this token. */
22331 cp_lexer_consume_token (parser->lexer);
22335 /* If the next token is the indicated keyword, consume it. Otherwise,
22336 issue an error message indicating that TOKEN_DESC was expected.
22338 Returns the token consumed, if the token had the appropriate type.
22339 Otherwise, returns NULL. */
22342 cp_parser_require_keyword (cp_parser* parser,
22344 required_token token_desc)
22346 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22348 if (token && token->keyword != keyword)
22350 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22357 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22358 function-definition. */
22361 cp_parser_token_starts_function_definition_p (cp_token* token)
22363 return (/* An ordinary function-body begins with an `{'. */
22364 token->type == CPP_OPEN_BRACE
22365 /* A ctor-initializer begins with a `:'. */
22366 || token->type == CPP_COLON
22367 /* A function-try-block begins with `try'. */
22368 || token->keyword == RID_TRY
22369 /* A function-transaction-block begins with `__transaction_atomic'
22370 or `__transaction_relaxed'. */
22371 || token->keyword == RID_TRANSACTION_ATOMIC
22372 || token->keyword == RID_TRANSACTION_RELAXED
22373 /* The named return value extension begins with `return'. */
22374 || token->keyword == RID_RETURN);
22377 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22381 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22385 token = cp_lexer_peek_token (parser->lexer);
22386 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22389 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22390 C++0x) ending a template-argument. */
22393 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22397 token = cp_lexer_peek_token (parser->lexer);
22398 return (token->type == CPP_COMMA
22399 || token->type == CPP_GREATER
22400 || token->type == CPP_ELLIPSIS
22401 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22404 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22405 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22408 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22413 token = cp_lexer_peek_nth_token (parser->lexer, n);
22414 if (token->type == CPP_LESS)
22416 /* Check for the sequence `<::' in the original code. It would be lexed as
22417 `[:', where `[' is a digraph, and there is no whitespace before
22419 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22422 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22423 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22429 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22430 or none_type otherwise. */
22432 static enum tag_types
22433 cp_parser_token_is_class_key (cp_token* token)
22435 switch (token->keyword)
22440 return record_type;
22449 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22452 cp_parser_check_class_key (enum tag_types class_key, tree type)
22454 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22455 permerror (input_location, "%qs tag used in naming %q#T",
22456 class_key == union_type ? "union"
22457 : class_key == record_type ? "struct" : "class",
22461 /* Issue an error message if DECL is redeclared with different
22462 access than its original declaration [class.access.spec/3].
22463 This applies to nested classes and nested class templates.
22467 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22469 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22472 if ((TREE_PRIVATE (decl)
22473 != (current_access_specifier == access_private_node))
22474 || (TREE_PROTECTED (decl)
22475 != (current_access_specifier == access_protected_node)))
22476 error_at (location, "%qD redeclared with different access", decl);
22479 /* Look for the `template' keyword, as a syntactic disambiguator.
22480 Return TRUE iff it is present, in which case it will be
22484 cp_parser_optional_template_keyword (cp_parser *parser)
22486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22488 /* The `template' keyword can only be used within templates;
22489 outside templates the parser can always figure out what is a
22490 template and what is not. */
22491 if (!processing_template_decl)
22493 cp_token *token = cp_lexer_peek_token (parser->lexer);
22494 error_at (token->location,
22495 "%<template%> (as a disambiguator) is only allowed "
22496 "within templates");
22497 /* If this part of the token stream is rescanned, the same
22498 error message would be generated. So, we purge the token
22499 from the stream. */
22500 cp_lexer_purge_token (parser->lexer);
22505 /* Consume the `template' keyword. */
22506 cp_lexer_consume_token (parser->lexer);
22514 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22515 set PARSER->SCOPE, and perform other related actions. */
22518 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22521 struct tree_check *check_value;
22522 deferred_access_check *chk;
22523 VEC (deferred_access_check,gc) *checks;
22525 /* Get the stored value. */
22526 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22527 /* Perform any access checks that were deferred. */
22528 checks = check_value->checks;
22531 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22532 perform_or_defer_access_check (chk->binfo,
22536 /* Set the scope from the stored value. */
22537 parser->scope = check_value->value;
22538 parser->qualifying_scope = check_value->qualifying_scope;
22539 parser->object_scope = NULL_TREE;
22542 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22543 encounter the end of a block before what we were looking for. */
22546 cp_parser_cache_group (cp_parser *parser,
22547 enum cpp_ttype end,
22552 cp_token *token = cp_lexer_peek_token (parser->lexer);
22554 /* Abort a parenthesized expression if we encounter a semicolon. */
22555 if ((end == CPP_CLOSE_PAREN || depth == 0)
22556 && token->type == CPP_SEMICOLON)
22558 /* If we've reached the end of the file, stop. */
22559 if (token->type == CPP_EOF
22560 || (end != CPP_PRAGMA_EOL
22561 && token->type == CPP_PRAGMA_EOL))
22563 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22564 /* We've hit the end of an enclosing block, so there's been some
22565 kind of syntax error. */
22568 /* If we're caching something finished by a comma (or semicolon),
22569 such as an NSDMI, don't consume the comma. */
22570 if (end == CPP_COMMA
22571 && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22574 /* Consume the token. */
22575 cp_lexer_consume_token (parser->lexer);
22576 /* See if it starts a new group. */
22577 if (token->type == CPP_OPEN_BRACE)
22579 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22580 /* In theory this should probably check end == '}', but
22581 cp_parser_save_member_function_body needs it to exit
22582 after either '}' or ')' when called with ')'. */
22586 else if (token->type == CPP_OPEN_PAREN)
22588 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22589 if (depth == 0 && end == CPP_CLOSE_PAREN)
22592 else if (token->type == CPP_PRAGMA)
22593 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22594 else if (token->type == end)
22599 /* Begin parsing tentatively. We always save tokens while parsing
22600 tentatively so that if the tentative parsing fails we can restore the
22604 cp_parser_parse_tentatively (cp_parser* parser)
22606 /* Enter a new parsing context. */
22607 parser->context = cp_parser_context_new (parser->context);
22608 /* Begin saving tokens. */
22609 cp_lexer_save_tokens (parser->lexer);
22610 /* In order to avoid repetitive access control error messages,
22611 access checks are queued up until we are no longer parsing
22613 push_deferring_access_checks (dk_deferred);
22616 /* Commit to the currently active tentative parse. */
22619 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22621 cp_parser_context *context;
22624 /* Mark all of the levels as committed. */
22625 lexer = parser->lexer;
22626 for (context = parser->context; context->next; context = context->next)
22628 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22630 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22631 while (!cp_lexer_saving_tokens (lexer))
22632 lexer = lexer->next;
22633 cp_lexer_commit_tokens (lexer);
22637 /* Abort the currently active tentative parse. All consumed tokens
22638 will be rolled back, and no diagnostics will be issued. */
22641 cp_parser_abort_tentative_parse (cp_parser* parser)
22643 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22644 || errorcount > 0);
22645 cp_parser_simulate_error (parser);
22646 /* Now, pretend that we want to see if the construct was
22647 successfully parsed. */
22648 cp_parser_parse_definitely (parser);
22651 /* Stop parsing tentatively. If a parse error has occurred, restore the
22652 token stream. Otherwise, commit to the tokens we have consumed.
22653 Returns true if no error occurred; false otherwise. */
22656 cp_parser_parse_definitely (cp_parser* parser)
22658 bool error_occurred;
22659 cp_parser_context *context;
22661 /* Remember whether or not an error occurred, since we are about to
22662 destroy that information. */
22663 error_occurred = cp_parser_error_occurred (parser);
22664 /* Remove the topmost context from the stack. */
22665 context = parser->context;
22666 parser->context = context->next;
22667 /* If no parse errors occurred, commit to the tentative parse. */
22668 if (!error_occurred)
22670 /* Commit to the tokens read tentatively, unless that was
22672 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22673 cp_lexer_commit_tokens (parser->lexer);
22675 pop_to_parent_deferring_access_checks ();
22677 /* Otherwise, if errors occurred, roll back our state so that things
22678 are just as they were before we began the tentative parse. */
22681 cp_lexer_rollback_tokens (parser->lexer);
22682 pop_deferring_access_checks ();
22684 /* Add the context to the front of the free list. */
22685 context->next = cp_parser_context_free_list;
22686 cp_parser_context_free_list = context;
22688 return !error_occurred;
22691 /* Returns true if we are parsing tentatively and are not committed to
22692 this tentative parse. */
22695 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22697 return (cp_parser_parsing_tentatively (parser)
22698 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22701 /* Returns nonzero iff an error has occurred during the most recent
22702 tentative parse. */
22705 cp_parser_error_occurred (cp_parser* parser)
22707 return (cp_parser_parsing_tentatively (parser)
22708 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22711 /* Returns nonzero if GNU extensions are allowed. */
22714 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22716 return parser->allow_gnu_extensions_p;
22719 /* Objective-C++ Productions */
22722 /* Parse an Objective-C expression, which feeds into a primary-expression
22726 objc-message-expression
22727 objc-string-literal
22728 objc-encode-expression
22729 objc-protocol-expression
22730 objc-selector-expression
22732 Returns a tree representation of the expression. */
22735 cp_parser_objc_expression (cp_parser* parser)
22737 /* Try to figure out what kind of declaration is present. */
22738 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22742 case CPP_OPEN_SQUARE:
22743 return cp_parser_objc_message_expression (parser);
22745 case CPP_OBJC_STRING:
22746 kwd = cp_lexer_consume_token (parser->lexer);
22747 return objc_build_string_object (kwd->u.value);
22750 switch (kwd->keyword)
22752 case RID_AT_ENCODE:
22753 return cp_parser_objc_encode_expression (parser);
22755 case RID_AT_PROTOCOL:
22756 return cp_parser_objc_protocol_expression (parser);
22758 case RID_AT_SELECTOR:
22759 return cp_parser_objc_selector_expression (parser);
22765 error_at (kwd->location,
22766 "misplaced %<@%D%> Objective-C++ construct",
22768 cp_parser_skip_to_end_of_block_or_statement (parser);
22771 return error_mark_node;
22774 /* Parse an Objective-C message expression.
22776 objc-message-expression:
22777 [ objc-message-receiver objc-message-args ]
22779 Returns a representation of an Objective-C message. */
22782 cp_parser_objc_message_expression (cp_parser* parser)
22784 tree receiver, messageargs;
22786 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
22787 receiver = cp_parser_objc_message_receiver (parser);
22788 messageargs = cp_parser_objc_message_args (parser);
22789 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22791 return objc_build_message_expr (receiver, messageargs);
22794 /* Parse an objc-message-receiver.
22796 objc-message-receiver:
22798 simple-type-specifier
22800 Returns a representation of the type or expression. */
22803 cp_parser_objc_message_receiver (cp_parser* parser)
22807 /* An Objective-C message receiver may be either (1) a type
22808 or (2) an expression. */
22809 cp_parser_parse_tentatively (parser);
22810 rcv = cp_parser_expression (parser, false, NULL);
22812 if (cp_parser_parse_definitely (parser))
22815 rcv = cp_parser_simple_type_specifier (parser,
22816 /*decl_specs=*/NULL,
22817 CP_PARSER_FLAGS_NONE);
22819 return objc_get_class_reference (rcv);
22822 /* Parse the arguments and selectors comprising an Objective-C message.
22827 objc-selector-args , objc-comma-args
22829 objc-selector-args:
22830 objc-selector [opt] : assignment-expression
22831 objc-selector-args objc-selector [opt] : assignment-expression
22834 assignment-expression
22835 objc-comma-args , assignment-expression
22837 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22838 selector arguments and TREE_VALUE containing a list of comma
22842 cp_parser_objc_message_args (cp_parser* parser)
22844 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22845 bool maybe_unary_selector_p = true;
22846 cp_token *token = cp_lexer_peek_token (parser->lexer);
22848 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22850 tree selector = NULL_TREE, arg;
22852 if (token->type != CPP_COLON)
22853 selector = cp_parser_objc_selector (parser);
22855 /* Detect if we have a unary selector. */
22856 if (maybe_unary_selector_p
22857 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22858 return build_tree_list (selector, NULL_TREE);
22860 maybe_unary_selector_p = false;
22861 cp_parser_require (parser, CPP_COLON, RT_COLON);
22862 arg = cp_parser_assignment_expression (parser, false, NULL);
22865 = chainon (sel_args,
22866 build_tree_list (selector, arg));
22868 token = cp_lexer_peek_token (parser->lexer);
22871 /* Handle non-selector arguments, if any. */
22872 while (token->type == CPP_COMMA)
22876 cp_lexer_consume_token (parser->lexer);
22877 arg = cp_parser_assignment_expression (parser, false, NULL);
22880 = chainon (addl_args,
22881 build_tree_list (NULL_TREE, arg));
22883 token = cp_lexer_peek_token (parser->lexer);
22886 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22888 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22889 return build_tree_list (error_mark_node, error_mark_node);
22892 return build_tree_list (sel_args, addl_args);
22895 /* Parse an Objective-C encode expression.
22897 objc-encode-expression:
22898 @encode objc-typename
22900 Returns an encoded representation of the type argument. */
22903 cp_parser_objc_encode_expression (cp_parser* parser)
22908 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
22909 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22910 token = cp_lexer_peek_token (parser->lexer);
22911 type = complete_type (cp_parser_type_id (parser));
22912 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22916 error_at (token->location,
22917 "%<@encode%> must specify a type as an argument");
22918 return error_mark_node;
22921 /* This happens if we find @encode(T) (where T is a template
22922 typename or something dependent on a template typename) when
22923 parsing a template. In that case, we can't compile it
22924 immediately, but we rather create an AT_ENCODE_EXPR which will
22925 need to be instantiated when the template is used.
22927 if (dependent_type_p (type))
22929 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22930 TREE_READONLY (value) = 1;
22934 return objc_build_encode_expr (type);
22937 /* Parse an Objective-C @defs expression. */
22940 cp_parser_objc_defs_expression (cp_parser *parser)
22944 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
22945 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22946 name = cp_parser_identifier (parser);
22947 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22949 return objc_get_class_ivars (name);
22952 /* Parse an Objective-C protocol expression.
22954 objc-protocol-expression:
22955 @protocol ( identifier )
22957 Returns a representation of the protocol expression. */
22960 cp_parser_objc_protocol_expression (cp_parser* parser)
22964 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22965 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22966 proto = cp_parser_identifier (parser);
22967 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22969 return objc_build_protocol_expr (proto);
22972 /* Parse an Objective-C selector expression.
22974 objc-selector-expression:
22975 @selector ( objc-method-signature )
22977 objc-method-signature:
22983 objc-selector-seq objc-selector :
22985 Returns a representation of the method selector. */
22988 cp_parser_objc_selector_expression (cp_parser* parser)
22990 tree sel_seq = NULL_TREE;
22991 bool maybe_unary_selector_p = true;
22993 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22995 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
22996 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22997 token = cp_lexer_peek_token (parser->lexer);
22999 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23000 || token->type == CPP_SCOPE)
23002 tree selector = NULL_TREE;
23004 if (token->type != CPP_COLON
23005 || token->type == CPP_SCOPE)
23006 selector = cp_parser_objc_selector (parser);
23008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23009 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23011 /* Detect if we have a unary selector. */
23012 if (maybe_unary_selector_p)
23014 sel_seq = selector;
23015 goto finish_selector;
23019 cp_parser_error (parser, "expected %<:%>");
23022 maybe_unary_selector_p = false;
23023 token = cp_lexer_consume_token (parser->lexer);
23025 if (token->type == CPP_SCOPE)
23028 = chainon (sel_seq,
23029 build_tree_list (selector, NULL_TREE));
23031 = chainon (sel_seq,
23032 build_tree_list (NULL_TREE, NULL_TREE));
23036 = chainon (sel_seq,
23037 build_tree_list (selector, NULL_TREE));
23039 token = cp_lexer_peek_token (parser->lexer);
23043 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23045 return objc_build_selector_expr (loc, sel_seq);
23048 /* Parse a list of identifiers.
23050 objc-identifier-list:
23052 objc-identifier-list , identifier
23054 Returns a TREE_LIST of identifier nodes. */
23057 cp_parser_objc_identifier_list (cp_parser* parser)
23063 identifier = cp_parser_identifier (parser);
23064 if (identifier == error_mark_node)
23065 return error_mark_node;
23067 list = build_tree_list (NULL_TREE, identifier);
23068 sep = cp_lexer_peek_token (parser->lexer);
23070 while (sep->type == CPP_COMMA)
23072 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23073 identifier = cp_parser_identifier (parser);
23074 if (identifier == error_mark_node)
23077 list = chainon (list, build_tree_list (NULL_TREE,
23079 sep = cp_lexer_peek_token (parser->lexer);
23085 /* Parse an Objective-C alias declaration.
23087 objc-alias-declaration:
23088 @compatibility_alias identifier identifier ;
23090 This function registers the alias mapping with the Objective-C front end.
23091 It returns nothing. */
23094 cp_parser_objc_alias_declaration (cp_parser* parser)
23098 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23099 alias = cp_parser_identifier (parser);
23100 orig = cp_parser_identifier (parser);
23101 objc_declare_alias (alias, orig);
23102 cp_parser_consume_semicolon_at_end_of_statement (parser);
23105 /* Parse an Objective-C class forward-declaration.
23107 objc-class-declaration:
23108 @class objc-identifier-list ;
23110 The function registers the forward declarations with the Objective-C
23111 front end. It returns nothing. */
23114 cp_parser_objc_class_declaration (cp_parser* parser)
23116 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23121 id = cp_parser_identifier (parser);
23122 if (id == error_mark_node)
23125 objc_declare_class (id);
23127 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23128 cp_lexer_consume_token (parser->lexer);
23132 cp_parser_consume_semicolon_at_end_of_statement (parser);
23135 /* Parse a list of Objective-C protocol references.
23137 objc-protocol-refs-opt:
23138 objc-protocol-refs [opt]
23140 objc-protocol-refs:
23141 < objc-identifier-list >
23143 Returns a TREE_LIST of identifiers, if any. */
23146 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23148 tree protorefs = NULL_TREE;
23150 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23152 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23153 protorefs = cp_parser_objc_identifier_list (parser);
23154 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23160 /* Parse a Objective-C visibility specification. */
23163 cp_parser_objc_visibility_spec (cp_parser* parser)
23165 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23167 switch (vis->keyword)
23169 case RID_AT_PRIVATE:
23170 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23172 case RID_AT_PROTECTED:
23173 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23175 case RID_AT_PUBLIC:
23176 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23178 case RID_AT_PACKAGE:
23179 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23185 /* Eat '@private'/'@protected'/'@public'. */
23186 cp_lexer_consume_token (parser->lexer);
23189 /* Parse an Objective-C method type. Return 'true' if it is a class
23190 (+) method, and 'false' if it is an instance (-) method. */
23193 cp_parser_objc_method_type (cp_parser* parser)
23195 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23201 /* Parse an Objective-C protocol qualifier. */
23204 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23206 tree quals = NULL_TREE, node;
23207 cp_token *token = cp_lexer_peek_token (parser->lexer);
23209 node = token->u.value;
23211 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23212 && (node == ridpointers [(int) RID_IN]
23213 || node == ridpointers [(int) RID_OUT]
23214 || node == ridpointers [(int) RID_INOUT]
23215 || node == ridpointers [(int) RID_BYCOPY]
23216 || node == ridpointers [(int) RID_BYREF]
23217 || node == ridpointers [(int) RID_ONEWAY]))
23219 quals = tree_cons (NULL_TREE, node, quals);
23220 cp_lexer_consume_token (parser->lexer);
23221 token = cp_lexer_peek_token (parser->lexer);
23222 node = token->u.value;
23228 /* Parse an Objective-C typename. */
23231 cp_parser_objc_typename (cp_parser* parser)
23233 tree type_name = NULL_TREE;
23235 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23237 tree proto_quals, cp_type = NULL_TREE;
23239 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23240 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23242 /* An ObjC type name may consist of just protocol qualifiers, in which
23243 case the type shall default to 'id'. */
23244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23246 cp_type = cp_parser_type_id (parser);
23248 /* If the type could not be parsed, an error has already
23249 been produced. For error recovery, behave as if it had
23250 not been specified, which will use the default type
23252 if (cp_type == error_mark_node)
23254 cp_type = NULL_TREE;
23255 /* We need to skip to the closing parenthesis as
23256 cp_parser_type_id() does not seem to do it for
23258 cp_parser_skip_to_closing_parenthesis (parser,
23259 /*recovering=*/true,
23260 /*or_comma=*/false,
23261 /*consume_paren=*/false);
23265 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23266 type_name = build_tree_list (proto_quals, cp_type);
23272 /* Check to see if TYPE refers to an Objective-C selector name. */
23275 cp_parser_objc_selector_p (enum cpp_ttype type)
23277 return (type == CPP_NAME || type == CPP_KEYWORD
23278 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23279 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23280 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23281 || type == CPP_XOR || type == CPP_XOR_EQ);
23284 /* Parse an Objective-C selector. */
23287 cp_parser_objc_selector (cp_parser* parser)
23289 cp_token *token = cp_lexer_consume_token (parser->lexer);
23291 if (!cp_parser_objc_selector_p (token->type))
23293 error_at (token->location, "invalid Objective-C++ selector name");
23294 return error_mark_node;
23297 /* C++ operator names are allowed to appear in ObjC selectors. */
23298 switch (token->type)
23300 case CPP_AND_AND: return get_identifier ("and");
23301 case CPP_AND_EQ: return get_identifier ("and_eq");
23302 case CPP_AND: return get_identifier ("bitand");
23303 case CPP_OR: return get_identifier ("bitor");
23304 case CPP_COMPL: return get_identifier ("compl");
23305 case CPP_NOT: return get_identifier ("not");
23306 case CPP_NOT_EQ: return get_identifier ("not_eq");
23307 case CPP_OR_OR: return get_identifier ("or");
23308 case CPP_OR_EQ: return get_identifier ("or_eq");
23309 case CPP_XOR: return get_identifier ("xor");
23310 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23311 default: return token->u.value;
23315 /* Parse an Objective-C params list. */
23318 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23320 tree params = NULL_TREE;
23321 bool maybe_unary_selector_p = true;
23322 cp_token *token = cp_lexer_peek_token (parser->lexer);
23324 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23326 tree selector = NULL_TREE, type_name, identifier;
23327 tree parm_attr = NULL_TREE;
23329 if (token->keyword == RID_ATTRIBUTE)
23332 if (token->type != CPP_COLON)
23333 selector = cp_parser_objc_selector (parser);
23335 /* Detect if we have a unary selector. */
23336 if (maybe_unary_selector_p
23337 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23339 params = selector; /* Might be followed by attributes. */
23343 maybe_unary_selector_p = false;
23344 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23346 /* Something went quite wrong. There should be a colon
23347 here, but there is not. Stop parsing parameters. */
23350 type_name = cp_parser_objc_typename (parser);
23351 /* New ObjC allows attributes on parameters too. */
23352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23353 parm_attr = cp_parser_attributes_opt (parser);
23354 identifier = cp_parser_identifier (parser);
23358 objc_build_keyword_decl (selector,
23363 token = cp_lexer_peek_token (parser->lexer);
23366 if (params == NULL_TREE)
23368 cp_parser_error (parser, "objective-c++ method declaration is expected");
23369 return error_mark_node;
23372 /* We allow tail attributes for the method. */
23373 if (token->keyword == RID_ATTRIBUTE)
23375 *attributes = cp_parser_attributes_opt (parser);
23376 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23377 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23379 cp_parser_error (parser,
23380 "method attributes must be specified at the end");
23381 return error_mark_node;
23384 if (params == NULL_TREE)
23386 cp_parser_error (parser, "objective-c++ method declaration is expected");
23387 return error_mark_node;
23392 /* Parse the non-keyword Objective-C params. */
23395 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23398 tree params = make_node (TREE_LIST);
23399 cp_token *token = cp_lexer_peek_token (parser->lexer);
23400 *ellipsisp = false; /* Initially, assume no ellipsis. */
23402 while (token->type == CPP_COMMA)
23404 cp_parameter_declarator *parmdecl;
23407 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23408 token = cp_lexer_peek_token (parser->lexer);
23410 if (token->type == CPP_ELLIPSIS)
23412 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23414 token = cp_lexer_peek_token (parser->lexer);
23418 /* TODO: parse attributes for tail parameters. */
23419 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23420 parm = grokdeclarator (parmdecl->declarator,
23421 &parmdecl->decl_specifiers,
23422 PARM, /*initialized=*/0,
23423 /*attrlist=*/NULL);
23425 chainon (params, build_tree_list (NULL_TREE, parm));
23426 token = cp_lexer_peek_token (parser->lexer);
23429 /* We allow tail attributes for the method. */
23430 if (token->keyword == RID_ATTRIBUTE)
23432 if (*attributes == NULL_TREE)
23434 *attributes = cp_parser_attributes_opt (parser);
23435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23436 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23440 /* We have an error, but parse the attributes, so that we can
23442 *attributes = cp_parser_attributes_opt (parser);
23444 cp_parser_error (parser,
23445 "method attributes must be specified at the end");
23446 return error_mark_node;
23452 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23455 cp_parser_objc_interstitial_code (cp_parser* parser)
23457 cp_token *token = cp_lexer_peek_token (parser->lexer);
23459 /* If the next token is `extern' and the following token is a string
23460 literal, then we have a linkage specification. */
23461 if (token->keyword == RID_EXTERN
23462 && cp_parser_is_pure_string_literal
23463 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23464 cp_parser_linkage_specification (parser);
23465 /* Handle #pragma, if any. */
23466 else if (token->type == CPP_PRAGMA)
23467 cp_parser_pragma (parser, pragma_external);
23468 /* Allow stray semicolons. */
23469 else if (token->type == CPP_SEMICOLON)
23470 cp_lexer_consume_token (parser->lexer);
23471 /* Mark methods as optional or required, when building protocols. */
23472 else if (token->keyword == RID_AT_OPTIONAL)
23474 cp_lexer_consume_token (parser->lexer);
23475 objc_set_method_opt (true);
23477 else if (token->keyword == RID_AT_REQUIRED)
23479 cp_lexer_consume_token (parser->lexer);
23480 objc_set_method_opt (false);
23482 else if (token->keyword == RID_NAMESPACE)
23483 cp_parser_namespace_definition (parser);
23484 /* Other stray characters must generate errors. */
23485 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23487 cp_lexer_consume_token (parser->lexer);
23488 error ("stray %qs between Objective-C++ methods",
23489 token->type == CPP_OPEN_BRACE ? "{" : "}");
23491 /* Finally, try to parse a block-declaration, or a function-definition. */
23493 cp_parser_block_declaration (parser, /*statement_p=*/false);
23496 /* Parse a method signature. */
23499 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23501 tree rettype, kwdparms, optparms;
23502 bool ellipsis = false;
23503 bool is_class_method;
23505 is_class_method = cp_parser_objc_method_type (parser);
23506 rettype = cp_parser_objc_typename (parser);
23507 *attributes = NULL_TREE;
23508 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23509 if (kwdparms == error_mark_node)
23510 return error_mark_node;
23511 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23512 if (optparms == error_mark_node)
23513 return error_mark_node;
23515 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23519 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23522 cp_lexer_save_tokens (parser->lexer);
23523 tattr = cp_parser_attributes_opt (parser);
23524 gcc_assert (tattr) ;
23526 /* If the attributes are followed by a method introducer, this is not allowed.
23527 Dump the attributes and flag the situation. */
23528 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23529 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23532 /* Otherwise, the attributes introduce some interstitial code, possibly so
23533 rewind to allow that check. */
23534 cp_lexer_rollback_tokens (parser->lexer);
23538 /* Parse an Objective-C method prototype list. */
23541 cp_parser_objc_method_prototype_list (cp_parser* parser)
23543 cp_token *token = cp_lexer_peek_token (parser->lexer);
23545 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23547 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23549 tree attributes, sig;
23550 bool is_class_method;
23551 if (token->type == CPP_PLUS)
23552 is_class_method = true;
23554 is_class_method = false;
23555 sig = cp_parser_objc_method_signature (parser, &attributes);
23556 if (sig == error_mark_node)
23558 cp_parser_skip_to_end_of_block_or_statement (parser);
23559 token = cp_lexer_peek_token (parser->lexer);
23562 objc_add_method_declaration (is_class_method, sig, attributes);
23563 cp_parser_consume_semicolon_at_end_of_statement (parser);
23565 else if (token->keyword == RID_AT_PROPERTY)
23566 cp_parser_objc_at_property_declaration (parser);
23567 else if (token->keyword == RID_ATTRIBUTE
23568 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23569 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23571 "prefix attributes are ignored for methods");
23573 /* Allow for interspersed non-ObjC++ code. */
23574 cp_parser_objc_interstitial_code (parser);
23576 token = cp_lexer_peek_token (parser->lexer);
23579 if (token->type != CPP_EOF)
23580 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23582 cp_parser_error (parser, "expected %<@end%>");
23584 objc_finish_interface ();
23587 /* Parse an Objective-C method definition list. */
23590 cp_parser_objc_method_definition_list (cp_parser* parser)
23592 cp_token *token = cp_lexer_peek_token (parser->lexer);
23594 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23598 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23601 tree sig, attribute;
23602 bool is_class_method;
23603 if (token->type == CPP_PLUS)
23604 is_class_method = true;
23606 is_class_method = false;
23607 push_deferring_access_checks (dk_deferred);
23608 sig = cp_parser_objc_method_signature (parser, &attribute);
23609 if (sig == error_mark_node)
23611 cp_parser_skip_to_end_of_block_or_statement (parser);
23612 token = cp_lexer_peek_token (parser->lexer);
23615 objc_start_method_definition (is_class_method, sig, attribute,
23618 /* For historical reasons, we accept an optional semicolon. */
23619 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23620 cp_lexer_consume_token (parser->lexer);
23622 ptk = cp_lexer_peek_token (parser->lexer);
23623 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23624 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23626 perform_deferred_access_checks ();
23627 stop_deferring_access_checks ();
23628 meth = cp_parser_function_definition_after_declarator (parser,
23630 pop_deferring_access_checks ();
23631 objc_finish_method_definition (meth);
23634 /* The following case will be removed once @synthesize is
23635 completely implemented. */
23636 else if (token->keyword == RID_AT_PROPERTY)
23637 cp_parser_objc_at_property_declaration (parser);
23638 else if (token->keyword == RID_AT_SYNTHESIZE)
23639 cp_parser_objc_at_synthesize_declaration (parser);
23640 else if (token->keyword == RID_AT_DYNAMIC)
23641 cp_parser_objc_at_dynamic_declaration (parser);
23642 else if (token->keyword == RID_ATTRIBUTE
23643 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23644 warning_at (token->location, OPT_Wattributes,
23645 "prefix attributes are ignored for methods");
23647 /* Allow for interspersed non-ObjC++ code. */
23648 cp_parser_objc_interstitial_code (parser);
23650 token = cp_lexer_peek_token (parser->lexer);
23653 if (token->type != CPP_EOF)
23654 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23656 cp_parser_error (parser, "expected %<@end%>");
23658 objc_finish_implementation ();
23661 /* Parse Objective-C ivars. */
23664 cp_parser_objc_class_ivars (cp_parser* parser)
23666 cp_token *token = cp_lexer_peek_token (parser->lexer);
23668 if (token->type != CPP_OPEN_BRACE)
23669 return; /* No ivars specified. */
23671 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
23672 token = cp_lexer_peek_token (parser->lexer);
23674 while (token->type != CPP_CLOSE_BRACE
23675 && token->keyword != RID_AT_END && token->type != CPP_EOF)
23677 cp_decl_specifier_seq declspecs;
23678 int decl_class_or_enum_p;
23679 tree prefix_attributes;
23681 cp_parser_objc_visibility_spec (parser);
23683 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23686 cp_parser_decl_specifier_seq (parser,
23687 CP_PARSER_FLAGS_OPTIONAL,
23689 &decl_class_or_enum_p);
23691 /* auto, register, static, extern, mutable. */
23692 if (declspecs.storage_class != sc_none)
23694 cp_parser_error (parser, "invalid type for instance variable");
23695 declspecs.storage_class = sc_none;
23699 if (declspecs.specs[(int) ds_thread])
23701 cp_parser_error (parser, "invalid type for instance variable");
23702 declspecs.specs[(int) ds_thread] = 0;
23706 if (declspecs.specs[(int) ds_typedef])
23708 cp_parser_error (parser, "invalid type for instance variable");
23709 declspecs.specs[(int) ds_typedef] = 0;
23712 prefix_attributes = declspecs.attributes;
23713 declspecs.attributes = NULL_TREE;
23715 /* Keep going until we hit the `;' at the end of the
23717 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23719 tree width = NULL_TREE, attributes, first_attribute, decl;
23720 cp_declarator *declarator = NULL;
23721 int ctor_dtor_or_conv_p;
23723 /* Check for a (possibly unnamed) bitfield declaration. */
23724 token = cp_lexer_peek_token (parser->lexer);
23725 if (token->type == CPP_COLON)
23728 if (token->type == CPP_NAME
23729 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23732 /* Get the name of the bitfield. */
23733 declarator = make_id_declarator (NULL_TREE,
23734 cp_parser_identifier (parser),
23738 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23739 /* Get the width of the bitfield. */
23741 = cp_parser_constant_expression (parser,
23742 /*allow_non_constant=*/false,
23747 /* Parse the declarator. */
23749 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23750 &ctor_dtor_or_conv_p,
23751 /*parenthesized_p=*/NULL,
23752 /*member_p=*/false);
23755 /* Look for attributes that apply to the ivar. */
23756 attributes = cp_parser_attributes_opt (parser);
23757 /* Remember which attributes are prefix attributes and
23759 first_attribute = attributes;
23760 /* Combine the attributes. */
23761 attributes = chainon (prefix_attributes, attributes);
23764 /* Create the bitfield declaration. */
23765 decl = grokbitfield (declarator, &declspecs,
23769 decl = grokfield (declarator, &declspecs,
23770 NULL_TREE, /*init_const_expr_p=*/false,
23771 NULL_TREE, attributes);
23773 /* Add the instance variable. */
23774 if (decl != error_mark_node && decl != NULL_TREE)
23775 objc_add_instance_variable (decl);
23777 /* Reset PREFIX_ATTRIBUTES. */
23778 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23779 attributes = TREE_CHAIN (attributes);
23781 TREE_CHAIN (attributes) = NULL_TREE;
23783 token = cp_lexer_peek_token (parser->lexer);
23785 if (token->type == CPP_COMMA)
23787 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23793 cp_parser_consume_semicolon_at_end_of_statement (parser);
23794 token = cp_lexer_peek_token (parser->lexer);
23797 if (token->keyword == RID_AT_END)
23798 cp_parser_error (parser, "expected %<}%>");
23800 /* Do not consume the RID_AT_END, so it will be read again as terminating
23801 the @interface of @implementation. */
23802 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23803 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
23805 /* For historical reasons, we accept an optional semicolon. */
23806 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23807 cp_lexer_consume_token (parser->lexer);
23810 /* Parse an Objective-C protocol declaration. */
23813 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23815 tree proto, protorefs;
23818 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23821 tok = cp_lexer_peek_token (parser->lexer);
23822 error_at (tok->location, "identifier expected after %<@protocol%>");
23823 cp_parser_consume_semicolon_at_end_of_statement (parser);
23827 /* See if we have a forward declaration or a definition. */
23828 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23830 /* Try a forward declaration first. */
23831 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23837 id = cp_parser_identifier (parser);
23838 if (id == error_mark_node)
23841 objc_declare_protocol (id, attributes);
23843 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23844 cp_lexer_consume_token (parser->lexer);
23848 cp_parser_consume_semicolon_at_end_of_statement (parser);
23851 /* Ok, we got a full-fledged definition (or at least should). */
23854 proto = cp_parser_identifier (parser);
23855 protorefs = cp_parser_objc_protocol_refs_opt (parser);
23856 objc_start_protocol (proto, protorefs, attributes);
23857 cp_parser_objc_method_prototype_list (parser);
23861 /* Parse an Objective-C superclass or category. */
23864 cp_parser_objc_superclass_or_category (cp_parser *parser,
23867 tree *categ, bool *is_class_extension)
23869 cp_token *next = cp_lexer_peek_token (parser->lexer);
23871 *super = *categ = NULL_TREE;
23872 *is_class_extension = false;
23873 if (next->type == CPP_COLON)
23875 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23876 *super = cp_parser_identifier (parser);
23878 else if (next->type == CPP_OPEN_PAREN)
23880 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23882 /* If there is no category name, and this is an @interface, we
23883 have a class extension. */
23884 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23886 *categ = NULL_TREE;
23887 *is_class_extension = true;
23890 *categ = cp_parser_identifier (parser);
23892 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23896 /* Parse an Objective-C class interface. */
23899 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23901 tree name, super, categ, protos;
23902 bool is_class_extension;
23904 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
23905 name = cp_parser_identifier (parser);
23906 if (name == error_mark_node)
23908 /* It's hard to recover because even if valid @interface stuff
23909 is to follow, we can't compile it (or validate it) if we
23910 don't even know which class it refers to. Let's assume this
23911 was a stray '@interface' token in the stream and skip it.
23915 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23916 &is_class_extension);
23917 protos = cp_parser_objc_protocol_refs_opt (parser);
23919 /* We have either a class or a category on our hands. */
23920 if (categ || is_class_extension)
23921 objc_start_category_interface (name, categ, protos, attributes);
23924 objc_start_class_interface (name, super, protos, attributes);
23925 /* Handle instance variable declarations, if any. */
23926 cp_parser_objc_class_ivars (parser);
23927 objc_continue_interface ();
23930 cp_parser_objc_method_prototype_list (parser);
23933 /* Parse an Objective-C class implementation. */
23936 cp_parser_objc_class_implementation (cp_parser* parser)
23938 tree name, super, categ;
23939 bool is_class_extension;
23941 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
23942 name = cp_parser_identifier (parser);
23943 if (name == error_mark_node)
23945 /* It's hard to recover because even if valid @implementation
23946 stuff is to follow, we can't compile it (or validate it) if
23947 we don't even know which class it refers to. Let's assume
23948 this was a stray '@implementation' token in the stream and
23953 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23954 &is_class_extension);
23956 /* We have either a class or a category on our hands. */
23958 objc_start_category_implementation (name, categ);
23961 objc_start_class_implementation (name, super);
23962 /* Handle instance variable declarations, if any. */
23963 cp_parser_objc_class_ivars (parser);
23964 objc_continue_implementation ();
23967 cp_parser_objc_method_definition_list (parser);
23970 /* Consume the @end token and finish off the implementation. */
23973 cp_parser_objc_end_implementation (cp_parser* parser)
23975 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23976 objc_finish_implementation ();
23979 /* Parse an Objective-C declaration. */
23982 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23984 /* Try to figure out what kind of declaration is present. */
23985 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23988 switch (kwd->keyword)
23993 error_at (kwd->location, "attributes may not be specified before"
23994 " the %<@%D%> Objective-C++ keyword",
23998 case RID_AT_IMPLEMENTATION:
23999 warning_at (kwd->location, OPT_Wattributes,
24000 "prefix attributes are ignored before %<@%D%>",
24007 switch (kwd->keyword)
24010 cp_parser_objc_alias_declaration (parser);
24013 cp_parser_objc_class_declaration (parser);
24015 case RID_AT_PROTOCOL:
24016 cp_parser_objc_protocol_declaration (parser, attributes);
24018 case RID_AT_INTERFACE:
24019 cp_parser_objc_class_interface (parser, attributes);
24021 case RID_AT_IMPLEMENTATION:
24022 cp_parser_objc_class_implementation (parser);
24025 cp_parser_objc_end_implementation (parser);
24028 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24030 cp_parser_skip_to_end_of_block_or_statement (parser);
24034 /* Parse an Objective-C try-catch-finally statement.
24036 objc-try-catch-finally-stmt:
24037 @try compound-statement objc-catch-clause-seq [opt]
24038 objc-finally-clause [opt]
24040 objc-catch-clause-seq:
24041 objc-catch-clause objc-catch-clause-seq [opt]
24044 @catch ( objc-exception-declaration ) compound-statement
24046 objc-finally-clause:
24047 @finally compound-statement
24049 objc-exception-declaration:
24050 parameter-declaration
24053 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24057 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24058 for C. Keep them in sync. */
24061 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24063 location_t location;
24066 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24067 location = cp_lexer_peek_token (parser->lexer)->location;
24068 objc_maybe_warn_exceptions (location);
24069 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24070 node, lest it get absorbed into the surrounding block. */
24071 stmt = push_stmt_list ();
24072 cp_parser_compound_statement (parser, NULL, false, false);
24073 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24075 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24077 cp_parameter_declarator *parm;
24078 tree parameter_declaration = error_mark_node;
24079 bool seen_open_paren = false;
24081 cp_lexer_consume_token (parser->lexer);
24082 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24083 seen_open_paren = true;
24084 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24086 /* We have "@catch (...)" (where the '...' are literally
24087 what is in the code). Skip the '...'.
24088 parameter_declaration is set to NULL_TREE, and
24089 objc_being_catch_clauses() knows that that means
24091 cp_lexer_consume_token (parser->lexer);
24092 parameter_declaration = NULL_TREE;
24096 /* We have "@catch (NSException *exception)" or something
24097 like that. Parse the parameter declaration. */
24098 parm = cp_parser_parameter_declaration (parser, false, NULL);
24100 parameter_declaration = error_mark_node;
24102 parameter_declaration = grokdeclarator (parm->declarator,
24103 &parm->decl_specifiers,
24104 PARM, /*initialized=*/0,
24105 /*attrlist=*/NULL);
24107 if (seen_open_paren)
24108 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24111 /* If there was no open parenthesis, we are recovering from
24112 an error, and we are trying to figure out what mistake
24113 the user has made. */
24115 /* If there is an immediate closing parenthesis, the user
24116 probably forgot the opening one (ie, they typed "@catch
24117 NSException *e)". Parse the closing parenthesis and keep
24119 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24120 cp_lexer_consume_token (parser->lexer);
24122 /* If these is no immediate closing parenthesis, the user
24123 probably doesn't know that parenthesis are required at
24124 all (ie, they typed "@catch NSException *e"). So, just
24125 forget about the closing parenthesis and keep going. */
24127 objc_begin_catch_clause (parameter_declaration);
24128 cp_parser_compound_statement (parser, NULL, false, false);
24129 objc_finish_catch_clause ();
24131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24133 cp_lexer_consume_token (parser->lexer);
24134 location = cp_lexer_peek_token (parser->lexer)->location;
24135 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24136 node, lest it get absorbed into the surrounding block. */
24137 stmt = push_stmt_list ();
24138 cp_parser_compound_statement (parser, NULL, false, false);
24139 objc_build_finally_clause (location, pop_stmt_list (stmt));
24142 return objc_finish_try_stmt ();
24145 /* Parse an Objective-C synchronized statement.
24147 objc-synchronized-stmt:
24148 @synchronized ( expression ) compound-statement
24150 Returns NULL_TREE. */
24153 cp_parser_objc_synchronized_statement (cp_parser *parser)
24155 location_t location;
24158 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24160 location = cp_lexer_peek_token (parser->lexer)->location;
24161 objc_maybe_warn_exceptions (location);
24162 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24163 lock = cp_parser_expression (parser, false, NULL);
24164 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24166 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24167 node, lest it get absorbed into the surrounding block. */
24168 stmt = push_stmt_list ();
24169 cp_parser_compound_statement (parser, NULL, false, false);
24171 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24174 /* Parse an Objective-C throw statement.
24177 @throw assignment-expression [opt] ;
24179 Returns a constructed '@throw' statement. */
24182 cp_parser_objc_throw_statement (cp_parser *parser)
24184 tree expr = NULL_TREE;
24185 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24187 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24190 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24192 cp_parser_consume_semicolon_at_end_of_statement (parser);
24194 return objc_build_throw_stmt (loc, expr);
24197 /* Parse an Objective-C statement. */
24200 cp_parser_objc_statement (cp_parser * parser)
24202 /* Try to figure out what kind of declaration is present. */
24203 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24205 switch (kwd->keyword)
24208 return cp_parser_objc_try_catch_finally_statement (parser);
24209 case RID_AT_SYNCHRONIZED:
24210 return cp_parser_objc_synchronized_statement (parser);
24212 return cp_parser_objc_throw_statement (parser);
24214 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24216 cp_parser_skip_to_end_of_block_or_statement (parser);
24219 return error_mark_node;
24222 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24223 look ahead to see if an objc keyword follows the attributes. This
24224 is to detect the use of prefix attributes on ObjC @interface and
24228 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24230 cp_lexer_save_tokens (parser->lexer);
24231 *attrib = cp_parser_attributes_opt (parser);
24232 gcc_assert (*attrib);
24233 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24235 cp_lexer_commit_tokens (parser->lexer);
24238 cp_lexer_rollback_tokens (parser->lexer);
24242 /* This routine is a minimal replacement for
24243 c_parser_struct_declaration () used when parsing the list of
24244 types/names or ObjC++ properties. For example, when parsing the
24247 @property (readonly) int a, b, c;
24249 this function is responsible for parsing "int a, int b, int c" and
24250 returning the declarations as CHAIN of DECLs.
24252 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24253 similar parsing. */
24255 cp_parser_objc_struct_declaration (cp_parser *parser)
24257 tree decls = NULL_TREE;
24258 cp_decl_specifier_seq declspecs;
24259 int decl_class_or_enum_p;
24260 tree prefix_attributes;
24262 cp_parser_decl_specifier_seq (parser,
24263 CP_PARSER_FLAGS_NONE,
24265 &decl_class_or_enum_p);
24267 if (declspecs.type == error_mark_node)
24268 return error_mark_node;
24270 /* auto, register, static, extern, mutable. */
24271 if (declspecs.storage_class != sc_none)
24273 cp_parser_error (parser, "invalid type for property");
24274 declspecs.storage_class = sc_none;
24278 if (declspecs.specs[(int) ds_thread])
24280 cp_parser_error (parser, "invalid type for property");
24281 declspecs.specs[(int) ds_thread] = 0;
24285 if (declspecs.specs[(int) ds_typedef])
24287 cp_parser_error (parser, "invalid type for property");
24288 declspecs.specs[(int) ds_typedef] = 0;
24291 prefix_attributes = declspecs.attributes;
24292 declspecs.attributes = NULL_TREE;
24294 /* Keep going until we hit the `;' at the end of the declaration. */
24295 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24297 tree attributes, first_attribute, decl;
24298 cp_declarator *declarator;
24301 /* Parse the declarator. */
24302 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24303 NULL, NULL, false);
24305 /* Look for attributes that apply to the ivar. */
24306 attributes = cp_parser_attributes_opt (parser);
24307 /* Remember which attributes are prefix attributes and
24309 first_attribute = attributes;
24310 /* Combine the attributes. */
24311 attributes = chainon (prefix_attributes, attributes);
24313 decl = grokfield (declarator, &declspecs,
24314 NULL_TREE, /*init_const_expr_p=*/false,
24315 NULL_TREE, attributes);
24317 if (decl == error_mark_node || decl == NULL_TREE)
24318 return error_mark_node;
24320 /* Reset PREFIX_ATTRIBUTES. */
24321 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24322 attributes = TREE_CHAIN (attributes);
24324 TREE_CHAIN (attributes) = NULL_TREE;
24326 DECL_CHAIN (decl) = decls;
24329 token = cp_lexer_peek_token (parser->lexer);
24330 if (token->type == CPP_COMMA)
24332 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24341 /* Parse an Objective-C @property declaration. The syntax is:
24343 objc-property-declaration:
24344 '@property' objc-property-attributes[opt] struct-declaration ;
24346 objc-property-attributes:
24347 '(' objc-property-attribute-list ')'
24349 objc-property-attribute-list:
24350 objc-property-attribute
24351 objc-property-attribute-list, objc-property-attribute
24353 objc-property-attribute
24354 'getter' = identifier
24355 'setter' = identifier
24364 @property NSString *name;
24365 @property (readonly) id object;
24366 @property (retain, nonatomic, getter=getTheName) id name;
24367 @property int a, b, c;
24369 PS: This function is identical to
24370 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24372 cp_parser_objc_at_property_declaration (cp_parser *parser)
24374 /* The following variables hold the attributes of the properties as
24375 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24376 seen. When we see an attribute, we set them to 'true' (if they
24377 are boolean properties) or to the identifier (if they have an
24378 argument, ie, for getter and setter). Note that here we only
24379 parse the list of attributes, check the syntax and accumulate the
24380 attributes that we find. objc_add_property_declaration() will
24381 then process the information. */
24382 bool property_assign = false;
24383 bool property_copy = false;
24384 tree property_getter_ident = NULL_TREE;
24385 bool property_nonatomic = false;
24386 bool property_readonly = false;
24387 bool property_readwrite = false;
24388 bool property_retain = false;
24389 tree property_setter_ident = NULL_TREE;
24391 /* 'properties' is the list of properties that we read. Usually a
24392 single one, but maybe more (eg, in "@property int a, b, c;" there
24397 loc = cp_lexer_peek_token (parser->lexer)->location;
24399 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24401 /* Parse the optional attribute list... */
24402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24405 cp_lexer_consume_token (parser->lexer);
24409 bool syntax_error = false;
24410 cp_token *token = cp_lexer_peek_token (parser->lexer);
24413 if (token->type != CPP_NAME)
24415 cp_parser_error (parser, "expected identifier");
24418 keyword = C_RID_CODE (token->u.value);
24419 cp_lexer_consume_token (parser->lexer);
24422 case RID_ASSIGN: property_assign = true; break;
24423 case RID_COPY: property_copy = true; break;
24424 case RID_NONATOMIC: property_nonatomic = true; break;
24425 case RID_READONLY: property_readonly = true; break;
24426 case RID_READWRITE: property_readwrite = true; break;
24427 case RID_RETAIN: property_retain = true; break;
24431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24433 if (keyword == RID_GETTER)
24434 cp_parser_error (parser,
24435 "missing %<=%> (after %<getter%> attribute)");
24437 cp_parser_error (parser,
24438 "missing %<=%> (after %<setter%> attribute)");
24439 syntax_error = true;
24442 cp_lexer_consume_token (parser->lexer); /* eat the = */
24443 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24445 cp_parser_error (parser, "expected identifier");
24446 syntax_error = true;
24449 if (keyword == RID_SETTER)
24451 if (property_setter_ident != NULL_TREE)
24453 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24454 cp_lexer_consume_token (parser->lexer);
24457 property_setter_ident = cp_parser_objc_selector (parser);
24458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24459 cp_parser_error (parser, "setter name must terminate with %<:%>");
24461 cp_lexer_consume_token (parser->lexer);
24465 if (property_getter_ident != NULL_TREE)
24467 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24468 cp_lexer_consume_token (parser->lexer);
24471 property_getter_ident = cp_parser_objc_selector (parser);
24475 cp_parser_error (parser, "unknown property attribute");
24476 syntax_error = true;
24483 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24484 cp_lexer_consume_token (parser->lexer);
24489 /* FIXME: "@property (setter, assign);" will generate a spurious
24490 "error: expected ‘)’ before ‘,’ token". This is because
24491 cp_parser_require, unlike the C counterpart, will produce an
24492 error even if we are in error recovery. */
24493 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24495 cp_parser_skip_to_closing_parenthesis (parser,
24496 /*recovering=*/true,
24497 /*or_comma=*/false,
24498 /*consume_paren=*/true);
24502 /* ... and the property declaration(s). */
24503 properties = cp_parser_objc_struct_declaration (parser);
24505 if (properties == error_mark_node)
24507 cp_parser_skip_to_end_of_statement (parser);
24508 /* If the next token is now a `;', consume it. */
24509 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24510 cp_lexer_consume_token (parser->lexer);
24514 if (properties == NULL_TREE)
24515 cp_parser_error (parser, "expected identifier");
24518 /* Comma-separated properties are chained together in
24519 reverse order; add them one by one. */
24520 properties = nreverse (properties);
24522 for (; properties; properties = TREE_CHAIN (properties))
24523 objc_add_property_declaration (loc, copy_node (properties),
24524 property_readonly, property_readwrite,
24525 property_assign, property_retain,
24526 property_copy, property_nonatomic,
24527 property_getter_ident, property_setter_ident);
24530 cp_parser_consume_semicolon_at_end_of_statement (parser);
24533 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24535 objc-synthesize-declaration:
24536 @synthesize objc-synthesize-identifier-list ;
24538 objc-synthesize-identifier-list:
24539 objc-synthesize-identifier
24540 objc-synthesize-identifier-list, objc-synthesize-identifier
24542 objc-synthesize-identifier
24544 identifier = identifier
24547 @synthesize MyProperty;
24548 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24550 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24551 for C. Keep them in sync.
24554 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24556 tree list = NULL_TREE;
24558 loc = cp_lexer_peek_token (parser->lexer)->location;
24560 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24563 tree property, ivar;
24564 property = cp_parser_identifier (parser);
24565 if (property == error_mark_node)
24567 cp_parser_consume_semicolon_at_end_of_statement (parser);
24570 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24572 cp_lexer_consume_token (parser->lexer);
24573 ivar = cp_parser_identifier (parser);
24574 if (ivar == error_mark_node)
24576 cp_parser_consume_semicolon_at_end_of_statement (parser);
24582 list = chainon (list, build_tree_list (ivar, property));
24583 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24584 cp_lexer_consume_token (parser->lexer);
24588 cp_parser_consume_semicolon_at_end_of_statement (parser);
24589 objc_add_synthesize_declaration (loc, list);
24592 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24594 objc-dynamic-declaration:
24595 @dynamic identifier-list ;
24598 @dynamic MyProperty;
24599 @dynamic MyProperty, AnotherProperty;
24601 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24602 for C. Keep them in sync.
24605 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24607 tree list = NULL_TREE;
24609 loc = cp_lexer_peek_token (parser->lexer)->location;
24611 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24615 property = cp_parser_identifier (parser);
24616 if (property == error_mark_node)
24618 cp_parser_consume_semicolon_at_end_of_statement (parser);
24621 list = chainon (list, build_tree_list (NULL, property));
24622 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24623 cp_lexer_consume_token (parser->lexer);
24627 cp_parser_consume_semicolon_at_end_of_statement (parser);
24628 objc_add_dynamic_declaration (loc, list);
24632 /* OpenMP 2.5 parsing routines. */
24634 /* Returns name of the next clause.
24635 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24636 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24637 returned and the token is consumed. */
24639 static pragma_omp_clause
24640 cp_parser_omp_clause_name (cp_parser *parser)
24642 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24644 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24645 result = PRAGMA_OMP_CLAUSE_IF;
24646 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24647 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24648 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24649 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24650 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24652 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24653 const char *p = IDENTIFIER_POINTER (id);
24658 if (!strcmp ("collapse", p))
24659 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24660 else if (!strcmp ("copyin", p))
24661 result = PRAGMA_OMP_CLAUSE_COPYIN;
24662 else if (!strcmp ("copyprivate", p))
24663 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24666 if (!strcmp ("final", p))
24667 result = PRAGMA_OMP_CLAUSE_FINAL;
24668 else if (!strcmp ("firstprivate", p))
24669 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24672 if (!strcmp ("lastprivate", p))
24673 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24676 if (!strcmp ("mergeable", p))
24677 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24680 if (!strcmp ("nowait", p))
24681 result = PRAGMA_OMP_CLAUSE_NOWAIT;
24682 else if (!strcmp ("num_threads", p))
24683 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24686 if (!strcmp ("ordered", p))
24687 result = PRAGMA_OMP_CLAUSE_ORDERED;
24690 if (!strcmp ("reduction", p))
24691 result = PRAGMA_OMP_CLAUSE_REDUCTION;
24694 if (!strcmp ("schedule", p))
24695 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24696 else if (!strcmp ("shared", p))
24697 result = PRAGMA_OMP_CLAUSE_SHARED;
24700 if (!strcmp ("untied", p))
24701 result = PRAGMA_OMP_CLAUSE_UNTIED;
24706 if (result != PRAGMA_OMP_CLAUSE_NONE)
24707 cp_lexer_consume_token (parser->lexer);
24712 /* Validate that a clause of the given type does not already exist. */
24715 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24716 const char *name, location_t location)
24720 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24721 if (OMP_CLAUSE_CODE (c) == code)
24723 error_at (location, "too many %qs clauses", name);
24731 variable-list , identifier
24733 In addition, we match a closing parenthesis. An opening parenthesis
24734 will have been consumed by the caller.
24736 If KIND is nonzero, create the appropriate node and install the decl
24737 in OMP_CLAUSE_DECL and add the node to the head of the list.
24739 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24740 return the list created. */
24743 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24751 token = cp_lexer_peek_token (parser->lexer);
24752 name = cp_parser_id_expression (parser, /*template_p=*/false,
24753 /*check_dependency_p=*/true,
24754 /*template_p=*/NULL,
24755 /*declarator_p=*/false,
24756 /*optional_p=*/false);
24757 if (name == error_mark_node)
24760 decl = cp_parser_lookup_name_simple (parser, name, token->location);
24761 if (decl == error_mark_node)
24762 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24764 else if (kind != 0)
24766 tree u = build_omp_clause (token->location, kind);
24767 OMP_CLAUSE_DECL (u) = decl;
24768 OMP_CLAUSE_CHAIN (u) = list;
24772 list = tree_cons (decl, NULL_TREE, list);
24775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24777 cp_lexer_consume_token (parser->lexer);
24780 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24784 /* Try to resync to an unnested comma. Copied from
24785 cp_parser_parenthesized_expression_list. */
24787 ending = cp_parser_skip_to_closing_parenthesis (parser,
24788 /*recovering=*/true,
24790 /*consume_paren=*/true);
24798 /* Similarly, but expect leading and trailing parenthesis. This is a very
24799 common case for omp clauses. */
24802 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24804 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24805 return cp_parser_omp_var_list_no_open (parser, kind, list);
24810 collapse ( constant-expression ) */
24813 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24819 loc = cp_lexer_peek_token (parser->lexer)->location;
24820 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24823 num = cp_parser_constant_expression (parser, false, NULL);
24825 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24827 /*or_comma=*/false,
24828 /*consume_paren=*/true);
24830 if (num == error_mark_node)
24832 num = fold_non_dependent_expr (num);
24833 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24834 || !host_integerp (num, 0)
24835 || (n = tree_low_cst (num, 0)) <= 0
24838 error_at (loc, "collapse argument needs positive constant integer expression");
24842 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24843 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24844 OMP_CLAUSE_CHAIN (c) = list;
24845 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24851 default ( shared | none ) */
24854 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24856 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24859 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24861 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24863 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24864 const char *p = IDENTIFIER_POINTER (id);
24869 if (strcmp ("none", p) != 0)
24871 kind = OMP_CLAUSE_DEFAULT_NONE;
24875 if (strcmp ("shared", p) != 0)
24877 kind = OMP_CLAUSE_DEFAULT_SHARED;
24884 cp_lexer_consume_token (parser->lexer);
24889 cp_parser_error (parser, "expected %<none%> or %<shared%>");
24892 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24893 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24894 /*or_comma=*/false,
24895 /*consume_paren=*/true);
24897 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24900 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24901 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24902 OMP_CLAUSE_CHAIN (c) = list;
24903 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24909 final ( expression ) */
24912 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24916 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24919 t = cp_parser_condition (parser);
24921 if (t == error_mark_node
24922 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24923 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24924 /*or_comma=*/false,
24925 /*consume_paren=*/true);
24927 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24929 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24930 OMP_CLAUSE_FINAL_EXPR (c) = t;
24931 OMP_CLAUSE_CHAIN (c) = list;
24937 if ( expression ) */
24940 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24944 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24947 t = cp_parser_condition (parser);
24949 if (t == error_mark_node
24950 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24951 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24952 /*or_comma=*/false,
24953 /*consume_paren=*/true);
24955 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24957 c = build_omp_clause (location, OMP_CLAUSE_IF);
24958 OMP_CLAUSE_IF_EXPR (c) = t;
24959 OMP_CLAUSE_CHAIN (c) = list;
24968 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24969 tree list, location_t location)
24973 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24976 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24977 OMP_CLAUSE_CHAIN (c) = list;
24985 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24986 tree list, location_t location)
24990 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
24992 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
24993 OMP_CLAUSE_CHAIN (c) = list;
24998 num_threads ( expression ) */
25001 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25002 location_t location)
25006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25009 t = cp_parser_expression (parser, false, NULL);
25011 if (t == error_mark_node
25012 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25014 /*or_comma=*/false,
25015 /*consume_paren=*/true);
25017 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25018 "num_threads", location);
25020 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25021 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25022 OMP_CLAUSE_CHAIN (c) = list;
25031 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25032 tree list, location_t location)
25036 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25037 "ordered", location);
25039 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25040 OMP_CLAUSE_CHAIN (c) = list;
25045 reduction ( reduction-operator : variable-list )
25047 reduction-operator:
25048 One of: + * - & ^ | && ||
25052 reduction-operator:
25053 One of: + * - & ^ | && || min max */
25056 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25058 enum tree_code code;
25061 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25064 switch (cp_lexer_peek_token (parser->lexer)->type)
25076 code = BIT_AND_EXPR;
25079 code = BIT_XOR_EXPR;
25082 code = BIT_IOR_EXPR;
25085 code = TRUTH_ANDIF_EXPR;
25088 code = TRUTH_ORIF_EXPR;
25092 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25093 const char *p = IDENTIFIER_POINTER (id);
25095 if (strcmp (p, "min") == 0)
25100 if (strcmp (p, "max") == 0)
25108 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25109 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25111 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25112 /*or_comma=*/false,
25113 /*consume_paren=*/true);
25116 cp_lexer_consume_token (parser->lexer);
25118 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25121 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25122 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25123 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25129 schedule ( schedule-kind )
25130 schedule ( schedule-kind , expression )
25133 static | dynamic | guided | runtime | auto */
25136 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25140 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25143 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25145 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25147 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25148 const char *p = IDENTIFIER_POINTER (id);
25153 if (strcmp ("dynamic", p) != 0)
25155 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25159 if (strcmp ("guided", p) != 0)
25161 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25165 if (strcmp ("runtime", p) != 0)
25167 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25174 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25175 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25176 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25177 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25180 cp_lexer_consume_token (parser->lexer);
25182 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25185 cp_lexer_consume_token (parser->lexer);
25187 token = cp_lexer_peek_token (parser->lexer);
25188 t = cp_parser_assignment_expression (parser, false, NULL);
25190 if (t == error_mark_node)
25192 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25193 error_at (token->location, "schedule %<runtime%> does not take "
25194 "a %<chunk_size%> parameter");
25195 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25196 error_at (token->location, "schedule %<auto%> does not take "
25197 "a %<chunk_size%> parameter");
25199 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25204 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25207 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25208 OMP_CLAUSE_CHAIN (c) = list;
25212 cp_parser_error (parser, "invalid schedule kind");
25214 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25215 /*or_comma=*/false,
25216 /*consume_paren=*/true);
25224 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25225 tree list, location_t location)
25229 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25231 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25232 OMP_CLAUSE_CHAIN (c) = list;
25236 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25237 is a bitmask in MASK. Return the list of clauses found; the result
25238 of clause default goes in *pdefault. */
25241 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25242 const char *where, cp_token *pragma_tok)
25244 tree clauses = NULL;
25246 cp_token *token = NULL;
25248 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25250 pragma_omp_clause c_kind;
25251 const char *c_name;
25252 tree prev = clauses;
25254 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25255 cp_lexer_consume_token (parser->lexer);
25257 token = cp_lexer_peek_token (parser->lexer);
25258 c_kind = cp_parser_omp_clause_name (parser);
25263 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25264 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25266 c_name = "collapse";
25268 case PRAGMA_OMP_CLAUSE_COPYIN:
25269 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25272 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25273 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25275 c_name = "copyprivate";
25277 case PRAGMA_OMP_CLAUSE_DEFAULT:
25278 clauses = cp_parser_omp_clause_default (parser, clauses,
25280 c_name = "default";
25282 case PRAGMA_OMP_CLAUSE_FINAL:
25283 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25286 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25287 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25289 c_name = "firstprivate";
25291 case PRAGMA_OMP_CLAUSE_IF:
25292 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25295 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25296 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25298 c_name = "lastprivate";
25300 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25301 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25303 c_name = "mergeable";
25305 case PRAGMA_OMP_CLAUSE_NOWAIT:
25306 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25309 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25310 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25312 c_name = "num_threads";
25314 case PRAGMA_OMP_CLAUSE_ORDERED:
25315 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25317 c_name = "ordered";
25319 case PRAGMA_OMP_CLAUSE_PRIVATE:
25320 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25322 c_name = "private";
25324 case PRAGMA_OMP_CLAUSE_REDUCTION:
25325 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25326 c_name = "reduction";
25328 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25329 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25331 c_name = "schedule";
25333 case PRAGMA_OMP_CLAUSE_SHARED:
25334 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25338 case PRAGMA_OMP_CLAUSE_UNTIED:
25339 clauses = cp_parser_omp_clause_untied (parser, clauses,
25344 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25348 if (((mask >> c_kind) & 1) == 0)
25350 /* Remove the invalid clause(s) from the list to avoid
25351 confusing the rest of the compiler. */
25353 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25357 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25358 return finish_omp_clauses (clauses);
25365 In practice, we're also interested in adding the statement to an
25366 outer node. So it is convenient if we work around the fact that
25367 cp_parser_statement calls add_stmt. */
25370 cp_parser_begin_omp_structured_block (cp_parser *parser)
25372 unsigned save = parser->in_statement;
25374 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25375 This preserves the "not within loop or switch" style error messages
25376 for nonsense cases like
25382 if (parser->in_statement)
25383 parser->in_statement = IN_OMP_BLOCK;
25389 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25391 parser->in_statement = save;
25395 cp_parser_omp_structured_block (cp_parser *parser)
25397 tree stmt = begin_omp_structured_block ();
25398 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25400 cp_parser_statement (parser, NULL_TREE, false, NULL);
25402 cp_parser_end_omp_structured_block (parser, save);
25403 return finish_omp_structured_block (stmt);
25407 # pragma omp atomic new-line
25411 x binop= expr | x++ | ++x | x-- | --x
25413 +, *, -, /, &, ^, |, <<, >>
25415 where x is an lvalue expression with scalar type.
25418 # pragma omp atomic new-line
25421 # pragma omp atomic read new-line
25424 # pragma omp atomic write new-line
25427 # pragma omp atomic update new-line
25430 # pragma omp atomic capture new-line
25433 # pragma omp atomic capture new-line
25441 expression-stmt | x = x binop expr
25443 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25445 { v = x; update-stmt; } | { update-stmt; v = x; }
25447 where x and v are lvalue expressions with scalar type. */
25450 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25452 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25453 tree rhs1 = NULL_TREE, orig_lhs;
25454 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25455 bool structured_block = false;
25457 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25459 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25460 const char *p = IDENTIFIER_POINTER (id);
25462 if (!strcmp (p, "read"))
25463 code = OMP_ATOMIC_READ;
25464 else if (!strcmp (p, "write"))
25466 else if (!strcmp (p, "update"))
25468 else if (!strcmp (p, "capture"))
25469 code = OMP_ATOMIC_CAPTURE_NEW;
25473 cp_lexer_consume_token (parser->lexer);
25475 cp_parser_require_pragma_eol (parser, pragma_tok);
25479 case OMP_ATOMIC_READ:
25480 case NOP_EXPR: /* atomic write */
25481 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25482 /*cast_p=*/false, NULL);
25483 if (v == error_mark_node)
25485 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25487 if (code == NOP_EXPR)
25488 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25490 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25491 /*cast_p=*/false, NULL);
25492 if (lhs == error_mark_node)
25494 if (code == NOP_EXPR)
25496 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25504 case OMP_ATOMIC_CAPTURE_NEW:
25505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25507 cp_lexer_consume_token (parser->lexer);
25508 structured_block = true;
25512 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25513 /*cast_p=*/false, NULL);
25514 if (v == error_mark_node)
25516 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25524 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25525 /*cast_p=*/false, NULL);
25527 switch (TREE_CODE (lhs))
25532 case POSTINCREMENT_EXPR:
25533 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25534 code = OMP_ATOMIC_CAPTURE_OLD;
25536 case PREINCREMENT_EXPR:
25537 lhs = TREE_OPERAND (lhs, 0);
25538 opcode = PLUS_EXPR;
25539 rhs = integer_one_node;
25542 case POSTDECREMENT_EXPR:
25543 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25544 code = OMP_ATOMIC_CAPTURE_OLD;
25546 case PREDECREMENT_EXPR:
25547 lhs = TREE_OPERAND (lhs, 0);
25548 opcode = MINUS_EXPR;
25549 rhs = integer_one_node;
25552 case COMPOUND_EXPR:
25553 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25554 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25555 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25556 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25557 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25558 (TREE_OPERAND (lhs, 1), 0), 0)))
25560 /* Undo effects of boolean_increment for post {in,de}crement. */
25561 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25564 if (TREE_CODE (lhs) == MODIFY_EXPR
25565 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25567 /* Undo effects of boolean_increment. */
25568 if (integer_onep (TREE_OPERAND (lhs, 1)))
25570 /* This is pre or post increment. */
25571 rhs = TREE_OPERAND (lhs, 1);
25572 lhs = TREE_OPERAND (lhs, 0);
25574 if (code == OMP_ATOMIC_CAPTURE_NEW
25575 && !structured_block
25576 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25577 code = OMP_ATOMIC_CAPTURE_OLD;
25583 switch (cp_lexer_peek_token (parser->lexer)->type)
25586 opcode = MULT_EXPR;
25589 opcode = TRUNC_DIV_EXPR;
25592 opcode = PLUS_EXPR;
25595 opcode = MINUS_EXPR;
25597 case CPP_LSHIFT_EQ:
25598 opcode = LSHIFT_EXPR;
25600 case CPP_RSHIFT_EQ:
25601 opcode = RSHIFT_EXPR;
25604 opcode = BIT_AND_EXPR;
25607 opcode = BIT_IOR_EXPR;
25610 opcode = BIT_XOR_EXPR;
25613 if (structured_block || code == OMP_ATOMIC)
25615 enum cp_parser_prec oprec;
25617 cp_lexer_consume_token (parser->lexer);
25618 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25619 /*cast_p=*/false, NULL);
25620 if (rhs1 == error_mark_node)
25622 token = cp_lexer_peek_token (parser->lexer);
25623 switch (token->type)
25625 case CPP_SEMICOLON:
25626 if (code == OMP_ATOMIC_CAPTURE_NEW)
25628 code = OMP_ATOMIC_CAPTURE_OLD;
25633 cp_lexer_consume_token (parser->lexer);
25636 cp_parser_error (parser,
25637 "invalid form of %<#pragma omp atomic%>");
25640 opcode = MULT_EXPR;
25643 opcode = TRUNC_DIV_EXPR;
25646 opcode = PLUS_EXPR;
25649 opcode = MINUS_EXPR;
25652 opcode = LSHIFT_EXPR;
25655 opcode = RSHIFT_EXPR;
25658 opcode = BIT_AND_EXPR;
25661 opcode = BIT_IOR_EXPR;
25664 opcode = BIT_XOR_EXPR;
25667 cp_parser_error (parser,
25668 "invalid operator for %<#pragma omp atomic%>");
25671 oprec = TOKEN_PRECEDENCE (token);
25672 gcc_assert (oprec != PREC_NOT_OPERATOR);
25673 if (commutative_tree_code (opcode))
25674 oprec = (enum cp_parser_prec) (oprec - 1);
25675 cp_lexer_consume_token (parser->lexer);
25676 rhs = cp_parser_binary_expression (parser, false, false,
25678 if (rhs == error_mark_node)
25684 cp_parser_error (parser,
25685 "invalid operator for %<#pragma omp atomic%>");
25688 cp_lexer_consume_token (parser->lexer);
25690 rhs = cp_parser_expression (parser, false, NULL);
25691 if (rhs == error_mark_node)
25696 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25698 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25700 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25701 /*cast_p=*/false, NULL);
25702 if (v == error_mark_node)
25704 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25706 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25707 /*cast_p=*/false, NULL);
25708 if (lhs1 == error_mark_node)
25711 if (structured_block)
25713 cp_parser_consume_semicolon_at_end_of_statement (parser);
25714 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25717 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25718 if (!structured_block)
25719 cp_parser_consume_semicolon_at_end_of_statement (parser);
25723 cp_parser_skip_to_end_of_block_or_statement (parser);
25724 if (structured_block)
25726 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25727 cp_lexer_consume_token (parser->lexer);
25728 else if (code == OMP_ATOMIC_CAPTURE_NEW)
25730 cp_parser_skip_to_end_of_block_or_statement (parser);
25731 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25732 cp_lexer_consume_token (parser->lexer);
25739 # pragma omp barrier new-line */
25742 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25744 cp_parser_require_pragma_eol (parser, pragma_tok);
25745 finish_omp_barrier ();
25749 # pragma omp critical [(name)] new-line
25750 structured-block */
25753 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25755 tree stmt, name = NULL;
25757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25759 cp_lexer_consume_token (parser->lexer);
25761 name = cp_parser_identifier (parser);
25763 if (name == error_mark_node
25764 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25765 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25766 /*or_comma=*/false,
25767 /*consume_paren=*/true);
25768 if (name == error_mark_node)
25771 cp_parser_require_pragma_eol (parser, pragma_tok);
25773 stmt = cp_parser_omp_structured_block (parser);
25774 return c_finish_omp_critical (input_location, stmt, name);
25778 # pragma omp flush flush-vars[opt] new-line
25781 ( variable-list ) */
25784 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25787 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25788 cp_parser_require_pragma_eol (parser, pragma_tok);
25790 finish_omp_flush ();
25793 /* Helper function, to parse omp for increment expression. */
25796 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25798 tree cond = cp_parser_binary_expression (parser, false, true,
25799 PREC_NOT_OPERATOR, NULL);
25800 if (cond == error_mark_node
25801 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25803 cp_parser_skip_to_end_of_statement (parser);
25804 return error_mark_node;
25807 switch (TREE_CODE (cond))
25815 return error_mark_node;
25818 /* If decl is an iterator, preserve LHS and RHS of the relational
25819 expr until finish_omp_for. */
25821 && (type_dependent_expression_p (decl)
25822 || CLASS_TYPE_P (TREE_TYPE (decl))))
25825 return build_x_binary_op (TREE_CODE (cond),
25826 TREE_OPERAND (cond, 0), ERROR_MARK,
25827 TREE_OPERAND (cond, 1), ERROR_MARK,
25828 /*overload=*/NULL, tf_warning_or_error);
25831 /* Helper function, to parse omp for increment expression. */
25834 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25836 cp_token *token = cp_lexer_peek_token (parser->lexer);
25842 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25844 op = (token->type == CPP_PLUS_PLUS
25845 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25846 cp_lexer_consume_token (parser->lexer);
25847 lhs = cp_parser_cast_expression (parser, false, false, NULL);
25849 return error_mark_node;
25850 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25853 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25855 return error_mark_node;
25857 token = cp_lexer_peek_token (parser->lexer);
25858 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25860 op = (token->type == CPP_PLUS_PLUS
25861 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25862 cp_lexer_consume_token (parser->lexer);
25863 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25866 op = cp_parser_assignment_operator_opt (parser);
25867 if (op == ERROR_MARK)
25868 return error_mark_node;
25870 if (op != NOP_EXPR)
25872 rhs = cp_parser_assignment_expression (parser, false, NULL);
25873 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25874 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25877 lhs = cp_parser_binary_expression (parser, false, false,
25878 PREC_ADDITIVE_EXPRESSION, NULL);
25879 token = cp_lexer_peek_token (parser->lexer);
25880 decl_first = lhs == decl;
25883 if (token->type != CPP_PLUS
25884 && token->type != CPP_MINUS)
25885 return error_mark_node;
25889 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25890 cp_lexer_consume_token (parser->lexer);
25891 rhs = cp_parser_binary_expression (parser, false, false,
25892 PREC_ADDITIVE_EXPRESSION, NULL);
25893 token = cp_lexer_peek_token (parser->lexer);
25894 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25896 if (lhs == NULL_TREE)
25898 if (op == PLUS_EXPR)
25901 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25904 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25905 NULL, tf_warning_or_error);
25908 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25912 if (rhs != decl || op == MINUS_EXPR)
25913 return error_mark_node;
25914 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25917 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25919 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25922 /* Parse the restricted form of the for statement allowed by OpenMP. */
25925 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25927 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25928 tree real_decl, initv, condv, incrv, declv;
25929 tree this_pre_body, cl;
25930 location_t loc_first;
25931 bool collapse_err = false;
25932 int i, collapse = 1, nbraces = 0;
25933 VEC(tree,gc) *for_block = make_tree_vector ();
25935 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25936 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25937 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25939 gcc_assert (collapse >= 1);
25941 declv = make_tree_vec (collapse);
25942 initv = make_tree_vec (collapse);
25943 condv = make_tree_vec (collapse);
25944 incrv = make_tree_vec (collapse);
25946 loc_first = cp_lexer_peek_token (parser->lexer)->location;
25948 for (i = 0; i < collapse; i++)
25950 int bracecount = 0;
25951 bool add_private_clause = false;
25954 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25956 cp_parser_error (parser, "for statement expected");
25959 loc = cp_lexer_consume_token (parser->lexer)->location;
25961 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25964 init = decl = real_decl = NULL;
25965 this_pre_body = push_stmt_list ();
25966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25968 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25972 integer-type var = lb
25973 random-access-iterator-type var = lb
25974 pointer-type var = lb
25976 cp_decl_specifier_seq type_specifiers;
25978 /* First, try to parse as an initialized declaration. See
25979 cp_parser_condition, from whence the bulk of this is copied. */
25981 cp_parser_parse_tentatively (parser);
25982 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25983 /*is_trailing_return=*/false,
25985 if (cp_parser_parse_definitely (parser))
25987 /* If parsing a type specifier seq succeeded, then this
25988 MUST be a initialized declaration. */
25989 tree asm_specification, attributes;
25990 cp_declarator *declarator;
25992 declarator = cp_parser_declarator (parser,
25993 CP_PARSER_DECLARATOR_NAMED,
25994 /*ctor_dtor_or_conv_p=*/NULL,
25995 /*parenthesized_p=*/NULL,
25996 /*member_p=*/false);
25997 attributes = cp_parser_attributes_opt (parser);
25998 asm_specification = cp_parser_asm_specification_opt (parser);
26000 if (declarator == cp_error_declarator)
26001 cp_parser_skip_to_end_of_statement (parser);
26005 tree pushed_scope, auto_node;
26007 decl = start_decl (declarator, &type_specifiers,
26008 SD_INITIALIZED, attributes,
26009 /*prefix_attributes=*/NULL_TREE,
26012 auto_node = type_uses_auto (TREE_TYPE (decl));
26013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26015 if (cp_lexer_next_token_is (parser->lexer,
26017 error ("parenthesized initialization is not allowed in "
26018 "OpenMP %<for%> loop");
26020 /* Trigger an error. */
26021 cp_parser_require (parser, CPP_EQ, RT_EQ);
26023 init = error_mark_node;
26024 cp_parser_skip_to_end_of_statement (parser);
26026 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26027 || type_dependent_expression_p (decl)
26030 bool is_direct_init, is_non_constant_init;
26032 init = cp_parser_initializer (parser,
26034 &is_non_constant_init);
26039 = do_auto_deduction (TREE_TYPE (decl), init,
26042 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26043 && !type_dependent_expression_p (decl))
26047 cp_finish_decl (decl, init, !is_non_constant_init,
26049 LOOKUP_ONLYCONVERTING);
26050 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26052 VEC_safe_push (tree, gc, for_block, this_pre_body);
26056 init = pop_stmt_list (this_pre_body);
26057 this_pre_body = NULL_TREE;
26062 cp_lexer_consume_token (parser->lexer);
26063 init = cp_parser_assignment_expression (parser, false, NULL);
26066 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26067 init = error_mark_node;
26069 cp_finish_decl (decl, NULL_TREE,
26070 /*init_const_expr_p=*/false,
26072 LOOKUP_ONLYCONVERTING);
26076 pop_scope (pushed_scope);
26082 /* If parsing a type specifier sequence failed, then
26083 this MUST be a simple expression. */
26084 cp_parser_parse_tentatively (parser);
26085 decl = cp_parser_primary_expression (parser, false, false,
26087 if (!cp_parser_error_occurred (parser)
26090 && CLASS_TYPE_P (TREE_TYPE (decl)))
26094 cp_parser_parse_definitely (parser);
26095 cp_parser_require (parser, CPP_EQ, RT_EQ);
26096 rhs = cp_parser_assignment_expression (parser, false, NULL);
26097 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26099 tf_warning_or_error));
26100 add_private_clause = true;
26105 cp_parser_abort_tentative_parse (parser);
26106 init = cp_parser_expression (parser, false, NULL);
26109 if (TREE_CODE (init) == MODIFY_EXPR
26110 || TREE_CODE (init) == MODOP_EXPR)
26111 real_decl = TREE_OPERAND (init, 0);
26116 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26119 this_pre_body = pop_stmt_list (this_pre_body);
26123 pre_body = push_stmt_list ();
26125 add_stmt (this_pre_body);
26126 pre_body = pop_stmt_list (pre_body);
26129 pre_body = this_pre_body;
26134 if (par_clauses != NULL && real_decl != NULL_TREE)
26137 for (c = par_clauses; *c ; )
26138 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26139 && OMP_CLAUSE_DECL (*c) == real_decl)
26141 error_at (loc, "iteration variable %qD"
26142 " should not be firstprivate", real_decl);
26143 *c = OMP_CLAUSE_CHAIN (*c);
26145 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26146 && OMP_CLAUSE_DECL (*c) == real_decl)
26148 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26149 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26150 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26151 OMP_CLAUSE_DECL (l) = real_decl;
26152 OMP_CLAUSE_CHAIN (l) = clauses;
26153 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26155 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26156 CP_OMP_CLAUSE_INFO (*c) = NULL;
26157 add_private_clause = false;
26161 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26162 && OMP_CLAUSE_DECL (*c) == real_decl)
26163 add_private_clause = false;
26164 c = &OMP_CLAUSE_CHAIN (*c);
26168 if (add_private_clause)
26171 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26173 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26174 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26175 && OMP_CLAUSE_DECL (c) == decl)
26177 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26178 && OMP_CLAUSE_DECL (c) == decl)
26179 error_at (loc, "iteration variable %qD "
26180 "should not be firstprivate",
26182 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26183 && OMP_CLAUSE_DECL (c) == decl)
26184 error_at (loc, "iteration variable %qD should not be reduction",
26189 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26190 OMP_CLAUSE_DECL (c) = decl;
26191 c = finish_omp_clauses (c);
26194 OMP_CLAUSE_CHAIN (c) = clauses;
26201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26202 cond = cp_parser_omp_for_cond (parser, decl);
26203 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26208 /* If decl is an iterator, preserve the operator on decl
26209 until finish_omp_for. */
26211 && ((type_dependent_expression_p (decl)
26212 && !POINTER_TYPE_P (TREE_TYPE (decl)))
26213 || CLASS_TYPE_P (TREE_TYPE (decl))))
26214 incr = cp_parser_omp_for_incr (parser, decl);
26216 incr = cp_parser_expression (parser, false, NULL);
26219 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26220 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26221 /*or_comma=*/false,
26222 /*consume_paren=*/true);
26224 TREE_VEC_ELT (declv, i) = decl;
26225 TREE_VEC_ELT (initv, i) = init;
26226 TREE_VEC_ELT (condv, i) = cond;
26227 TREE_VEC_ELT (incrv, i) = incr;
26229 if (i == collapse - 1)
26232 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26233 in between the collapsed for loops to be still considered perfectly
26234 nested. Hopefully the final version clarifies this.
26235 For now handle (multiple) {'s and empty statements. */
26236 cp_parser_parse_tentatively (parser);
26239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26241 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26243 cp_lexer_consume_token (parser->lexer);
26246 else if (bracecount
26247 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26248 cp_lexer_consume_token (parser->lexer);
26251 loc = cp_lexer_peek_token (parser->lexer)->location;
26252 error_at (loc, "not enough collapsed for loops");
26253 collapse_err = true;
26254 cp_parser_abort_tentative_parse (parser);
26263 cp_parser_parse_definitely (parser);
26264 nbraces += bracecount;
26268 /* Note that we saved the original contents of this flag when we entered
26269 the structured block, and so we don't need to re-save it here. */
26270 parser->in_statement = IN_OMP_FOR;
26272 /* Note that the grammar doesn't call for a structured block here,
26273 though the loop as a whole is a structured block. */
26274 body = push_stmt_list ();
26275 cp_parser_statement (parser, NULL_TREE, false, NULL);
26276 body = pop_stmt_list (body);
26278 if (declv == NULL_TREE)
26281 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26282 pre_body, clauses);
26286 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26288 cp_lexer_consume_token (parser->lexer);
26291 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26292 cp_lexer_consume_token (parser->lexer);
26297 error_at (cp_lexer_peek_token (parser->lexer)->location,
26298 "collapsed loops not perfectly nested");
26300 collapse_err = true;
26301 cp_parser_statement_seq_opt (parser, NULL);
26302 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26307 while (!VEC_empty (tree, for_block))
26308 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26309 release_tree_vector (for_block);
26315 #pragma omp for for-clause[optseq] new-line
26318 #define OMP_FOR_CLAUSE_MASK \
26319 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26320 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26321 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26322 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26323 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26324 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26325 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26326 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26329 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26331 tree clauses, sb, ret;
26334 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26335 "#pragma omp for", pragma_tok);
26337 sb = begin_omp_structured_block ();
26338 save = cp_parser_begin_omp_structured_block (parser);
26340 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26342 cp_parser_end_omp_structured_block (parser, save);
26343 add_stmt (finish_omp_structured_block (sb));
26349 # pragma omp master new-line
26350 structured-block */
26353 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26355 cp_parser_require_pragma_eol (parser, pragma_tok);
26356 return c_finish_omp_master (input_location,
26357 cp_parser_omp_structured_block (parser));
26361 # pragma omp ordered new-line
26362 structured-block */
26365 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26367 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26368 cp_parser_require_pragma_eol (parser, pragma_tok);
26369 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26375 { section-sequence }
26378 section-directive[opt] structured-block
26379 section-sequence section-directive structured-block */
26382 cp_parser_omp_sections_scope (cp_parser *parser)
26384 tree stmt, substmt;
26385 bool error_suppress = false;
26388 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26391 stmt = push_stmt_list ();
26393 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26397 substmt = begin_omp_structured_block ();
26398 save = cp_parser_begin_omp_structured_block (parser);
26402 cp_parser_statement (parser, NULL_TREE, false, NULL);
26404 tok = cp_lexer_peek_token (parser->lexer);
26405 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26407 if (tok->type == CPP_CLOSE_BRACE)
26409 if (tok->type == CPP_EOF)
26413 cp_parser_end_omp_structured_block (parser, save);
26414 substmt = finish_omp_structured_block (substmt);
26415 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26416 add_stmt (substmt);
26421 tok = cp_lexer_peek_token (parser->lexer);
26422 if (tok->type == CPP_CLOSE_BRACE)
26424 if (tok->type == CPP_EOF)
26427 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26429 cp_lexer_consume_token (parser->lexer);
26430 cp_parser_require_pragma_eol (parser, tok);
26431 error_suppress = false;
26433 else if (!error_suppress)
26435 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26436 error_suppress = true;
26439 substmt = cp_parser_omp_structured_block (parser);
26440 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26441 add_stmt (substmt);
26443 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26445 substmt = pop_stmt_list (stmt);
26447 stmt = make_node (OMP_SECTIONS);
26448 TREE_TYPE (stmt) = void_type_node;
26449 OMP_SECTIONS_BODY (stmt) = substmt;
26456 # pragma omp sections sections-clause[optseq] newline
26459 #define OMP_SECTIONS_CLAUSE_MASK \
26460 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26461 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26462 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26463 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26464 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26467 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26471 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26472 "#pragma omp sections", pragma_tok);
26474 ret = cp_parser_omp_sections_scope (parser);
26476 OMP_SECTIONS_CLAUSES (ret) = clauses;
26482 # pragma parallel parallel-clause new-line
26483 # pragma parallel for parallel-for-clause new-line
26484 # pragma parallel sections parallel-sections-clause new-line */
26486 #define OMP_PARALLEL_CLAUSE_MASK \
26487 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26488 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26489 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26490 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26491 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26492 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26493 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26494 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26497 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26499 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26500 const char *p_name = "#pragma omp parallel";
26501 tree stmt, clauses, par_clause, ws_clause, block;
26502 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26504 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26506 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26508 cp_lexer_consume_token (parser->lexer);
26509 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26510 p_name = "#pragma omp parallel for";
26511 mask |= OMP_FOR_CLAUSE_MASK;
26512 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26514 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26516 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26517 const char *p = IDENTIFIER_POINTER (id);
26518 if (strcmp (p, "sections") == 0)
26520 cp_lexer_consume_token (parser->lexer);
26521 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26522 p_name = "#pragma omp parallel sections";
26523 mask |= OMP_SECTIONS_CLAUSE_MASK;
26524 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26528 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26529 block = begin_omp_parallel ();
26530 save = cp_parser_begin_omp_structured_block (parser);
26534 case PRAGMA_OMP_PARALLEL:
26535 cp_parser_statement (parser, NULL_TREE, false, NULL);
26536 par_clause = clauses;
26539 case PRAGMA_OMP_PARALLEL_FOR:
26540 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26541 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26544 case PRAGMA_OMP_PARALLEL_SECTIONS:
26545 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26546 stmt = cp_parser_omp_sections_scope (parser);
26548 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26552 gcc_unreachable ();
26555 cp_parser_end_omp_structured_block (parser, save);
26556 stmt = finish_omp_parallel (par_clause, block);
26557 if (p_kind != PRAGMA_OMP_PARALLEL)
26558 OMP_PARALLEL_COMBINED (stmt) = 1;
26563 # pragma omp single single-clause[optseq] new-line
26564 structured-block */
26566 #define OMP_SINGLE_CLAUSE_MASK \
26567 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26568 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26569 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26570 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26573 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26575 tree stmt = make_node (OMP_SINGLE);
26576 TREE_TYPE (stmt) = void_type_node;
26578 OMP_SINGLE_CLAUSES (stmt)
26579 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26580 "#pragma omp single", pragma_tok);
26581 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26583 return add_stmt (stmt);
26587 # pragma omp task task-clause[optseq] new-line
26588 structured-block */
26590 #define OMP_TASK_CLAUSE_MASK \
26591 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26592 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26593 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26594 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26595 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26596 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26597 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26598 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26601 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26603 tree clauses, block;
26606 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26607 "#pragma omp task", pragma_tok);
26608 block = begin_omp_task ();
26609 save = cp_parser_begin_omp_structured_block (parser);
26610 cp_parser_statement (parser, NULL_TREE, false, NULL);
26611 cp_parser_end_omp_structured_block (parser, save);
26612 return finish_omp_task (clauses, block);
26616 # pragma omp taskwait new-line */
26619 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26621 cp_parser_require_pragma_eol (parser, pragma_tok);
26622 finish_omp_taskwait ();
26626 # pragma omp taskyield new-line */
26629 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26631 cp_parser_require_pragma_eol (parser, pragma_tok);
26632 finish_omp_taskyield ();
26636 # pragma omp threadprivate (variable-list) */
26639 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26643 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26644 cp_parser_require_pragma_eol (parser, pragma_tok);
26646 finish_omp_threadprivate (vars);
26649 /* Main entry point to OpenMP statement pragmas. */
26652 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26656 switch (pragma_tok->pragma_kind)
26658 case PRAGMA_OMP_ATOMIC:
26659 cp_parser_omp_atomic (parser, pragma_tok);
26661 case PRAGMA_OMP_CRITICAL:
26662 stmt = cp_parser_omp_critical (parser, pragma_tok);
26664 case PRAGMA_OMP_FOR:
26665 stmt = cp_parser_omp_for (parser, pragma_tok);
26667 case PRAGMA_OMP_MASTER:
26668 stmt = cp_parser_omp_master (parser, pragma_tok);
26670 case PRAGMA_OMP_ORDERED:
26671 stmt = cp_parser_omp_ordered (parser, pragma_tok);
26673 case PRAGMA_OMP_PARALLEL:
26674 stmt = cp_parser_omp_parallel (parser, pragma_tok);
26676 case PRAGMA_OMP_SECTIONS:
26677 stmt = cp_parser_omp_sections (parser, pragma_tok);
26679 case PRAGMA_OMP_SINGLE:
26680 stmt = cp_parser_omp_single (parser, pragma_tok);
26682 case PRAGMA_OMP_TASK:
26683 stmt = cp_parser_omp_task (parser, pragma_tok);
26686 gcc_unreachable ();
26690 SET_EXPR_LOCATION (stmt, pragma_tok->location);
26693 /* Transactional Memory parsing routines. */
26695 /* Parse a transaction attribute.
26701 ??? Simplify this when C++0x bracket attributes are
26702 implemented properly. */
26705 cp_parser_txn_attribute_opt (cp_parser *parser)
26708 tree attr_name, attr = NULL;
26710 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26711 return cp_parser_attributes_opt (parser);
26713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26715 cp_lexer_consume_token (parser->lexer);
26716 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26719 token = cp_lexer_peek_token (parser->lexer);
26720 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26722 token = cp_lexer_consume_token (parser->lexer);
26724 attr_name = (token->type == CPP_KEYWORD
26725 /* For keywords, use the canonical spelling,
26726 not the parsed identifier. */
26727 ? ridpointers[(int) token->keyword]
26729 attr = build_tree_list (attr_name, NULL_TREE);
26732 cp_parser_error (parser, "expected identifier");
26734 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26736 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26740 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26742 transaction-statement:
26743 __transaction_atomic txn-attribute[opt] txn-exception-spec[opt]
26745 __transaction_relaxed txn-exception-spec[opt] compound-statement
26747 ??? The exception specification is not yet implemented.
26751 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26753 unsigned char old_in = parser->in_transaction;
26754 unsigned char this_in = 1, new_in;
26758 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26759 || keyword == RID_TRANSACTION_RELAXED);
26760 token = cp_parser_require_keyword (parser, keyword,
26761 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26762 : RT_TRANSACTION_RELAXED));
26763 gcc_assert (token != NULL);
26765 if (keyword == RID_TRANSACTION_RELAXED)
26766 this_in |= TM_STMT_ATTR_RELAXED;
26769 attrs = cp_parser_txn_attribute_opt (parser);
26771 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26774 /* Keep track if we're in the lexical scope of an outer transaction. */
26775 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
26777 stmt = begin_transaction_stmt (token->location, NULL, this_in);
26779 parser->in_transaction = new_in;
26780 cp_parser_compound_statement (parser, NULL, false, false);
26781 parser->in_transaction = old_in;
26783 finish_transaction_stmt (stmt, NULL, this_in);
26788 /* Parse a __transaction_atomic or __transaction_relaxed expression.
26790 transaction-expression:
26791 __transaction_atomic txn-exception-spec[opt] ( expression )
26792 __transaction_relaxed txn-exception-spec[opt] ( expression )
26794 ??? The exception specification is not yet implemented.
26798 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
26800 unsigned char old_in = parser->in_transaction;
26801 unsigned char this_in = 1;
26805 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26806 || keyword == RID_TRANSACTION_RELAXED);
26809 error (keyword == RID_TRANSACTION_RELAXED
26810 ? G_("%<__transaction_relaxed%> without transactional memory "
26812 : G_("%<__transaction_atomic%> without transactional memory "
26813 "support enabled"));
26815 token = cp_parser_require_keyword (parser, keyword,
26816 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26817 : RT_TRANSACTION_RELAXED));
26818 gcc_assert (token != NULL);
26820 if (keyword == RID_TRANSACTION_RELAXED)
26821 this_in |= TM_STMT_ATTR_RELAXED;
26823 parser->in_transaction = this_in;
26824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26826 tree expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26827 ret = build_transaction_expr (token->location, expr, this_in);
26831 cp_parser_error (parser, "expected %<(%>");
26832 ret = error_mark_node;
26834 parser->in_transaction = old_in;
26836 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
26837 return error_mark_node;
26839 return (flag_tm ? ret : error_mark_node);
26842 /* Parse a function-transaction-block.
26844 function-transaction-block:
26845 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
26847 __transaction_atomic txn-attribute[opt] function-try-block
26848 __transaction_relaxed ctor-initializer[opt] function-body
26849 __transaction_relaxed function-try-block
26853 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
26855 unsigned char old_in = parser->in_transaction;
26856 unsigned char new_in = 1;
26857 tree compound_stmt, stmt, attrs;
26858 bool ctor_initializer_p;
26861 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26862 || keyword == RID_TRANSACTION_RELAXED);
26863 token = cp_parser_require_keyword (parser, keyword,
26864 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26865 : RT_TRANSACTION_RELAXED));
26866 gcc_assert (token != NULL);
26868 if (keyword == RID_TRANSACTION_RELAXED)
26869 new_in |= TM_STMT_ATTR_RELAXED;
26872 attrs = cp_parser_txn_attribute_opt (parser);
26874 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26877 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
26879 parser->in_transaction = new_in;
26881 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26882 ctor_initializer_p = cp_parser_function_try_block (parser);
26885 = cp_parser_ctor_initializer_opt_and_function_body (parser);
26887 parser->in_transaction = old_in;
26889 finish_transaction_stmt (stmt, compound_stmt, new_in);
26891 return ctor_initializer_p;
26894 /* Parse a __transaction_cancel statement.
26897 __transaction_cancel txn-attribute[opt] ;
26898 __transaction_cancel txn-attribute[opt] throw-expression ;
26900 ??? Cancel and throw is not yet implemented. */
26903 cp_parser_transaction_cancel (cp_parser *parser)
26906 bool is_outer = false;
26909 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
26910 RT_TRANSACTION_CANCEL);
26911 gcc_assert (token != NULL);
26913 attrs = cp_parser_txn_attribute_opt (parser);
26915 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
26917 /* ??? Parse cancel-and-throw here. */
26919 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26923 error_at (token->location, "%<__transaction_cancel%> without "
26924 "transactional memory support enabled");
26925 return error_mark_node;
26927 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
26929 error_at (token->location, "%<__transaction_cancel%> within a "
26930 "%<__transaction_relaxed%>");
26931 return error_mark_node;
26935 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
26936 && !is_tm_may_cancel_outer (current_function_decl))
26938 error_at (token->location, "outer %<__transaction_cancel%> not "
26939 "within outer %<__transaction_atomic%>");
26940 error_at (token->location,
26941 " or a %<transaction_may_cancel_outer%> function");
26942 return error_mark_node;
26945 else if (parser->in_transaction == 0)
26947 error_at (token->location, "%<__transaction_cancel%> not within "
26948 "%<__transaction_atomic%>");
26949 return error_mark_node;
26952 stmt = build_tm_abort_call (token->location, is_outer);
26961 static GTY (()) cp_parser *the_parser;
26964 /* Special handling for the first token or line in the file. The first
26965 thing in the file might be #pragma GCC pch_preprocess, which loads a
26966 PCH file, which is a GC collection point. So we need to handle this
26967 first pragma without benefit of an existing lexer structure.
26969 Always returns one token to the caller in *FIRST_TOKEN. This is
26970 either the true first token of the file, or the first token after
26971 the initial pragma. */
26974 cp_parser_initial_pragma (cp_token *first_token)
26978 cp_lexer_get_preprocessor_token (NULL, first_token);
26979 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
26982 cp_lexer_get_preprocessor_token (NULL, first_token);
26983 if (first_token->type == CPP_STRING)
26985 name = first_token->u.value;
26987 cp_lexer_get_preprocessor_token (NULL, first_token);
26988 if (first_token->type != CPP_PRAGMA_EOL)
26989 error_at (first_token->location,
26990 "junk at end of %<#pragma GCC pch_preprocess%>");
26993 error_at (first_token->location, "expected string literal");
26995 /* Skip to the end of the pragma. */
26996 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
26997 cp_lexer_get_preprocessor_token (NULL, first_token);
26999 /* Now actually load the PCH file. */
27001 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27003 /* Read one more token to return to our caller. We have to do this
27004 after reading the PCH file in, since its pointers have to be
27006 cp_lexer_get_preprocessor_token (NULL, first_token);
27009 /* Normal parsing of a pragma token. Here we can (and must) use the
27013 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27015 cp_token *pragma_tok;
27018 pragma_tok = cp_lexer_consume_token (parser->lexer);
27019 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27020 parser->lexer->in_pragma = true;
27022 id = pragma_tok->pragma_kind;
27025 case PRAGMA_GCC_PCH_PREPROCESS:
27026 error_at (pragma_tok->location,
27027 "%<#pragma GCC pch_preprocess%> must be first");
27030 case PRAGMA_OMP_BARRIER:
27033 case pragma_compound:
27034 cp_parser_omp_barrier (parser, pragma_tok);
27037 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27038 "used in compound statements");
27045 case PRAGMA_OMP_FLUSH:
27048 case pragma_compound:
27049 cp_parser_omp_flush (parser, pragma_tok);
27052 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27053 "used in compound statements");
27060 case PRAGMA_OMP_TASKWAIT:
27063 case pragma_compound:
27064 cp_parser_omp_taskwait (parser, pragma_tok);
27067 error_at (pragma_tok->location,
27068 "%<#pragma omp taskwait%> may only be "
27069 "used in compound statements");
27076 case PRAGMA_OMP_TASKYIELD:
27079 case pragma_compound:
27080 cp_parser_omp_taskyield (parser, pragma_tok);
27083 error_at (pragma_tok->location,
27084 "%<#pragma omp taskyield%> may only be "
27085 "used in compound statements");
27092 case PRAGMA_OMP_THREADPRIVATE:
27093 cp_parser_omp_threadprivate (parser, pragma_tok);
27096 case PRAGMA_OMP_ATOMIC:
27097 case PRAGMA_OMP_CRITICAL:
27098 case PRAGMA_OMP_FOR:
27099 case PRAGMA_OMP_MASTER:
27100 case PRAGMA_OMP_ORDERED:
27101 case PRAGMA_OMP_PARALLEL:
27102 case PRAGMA_OMP_SECTIONS:
27103 case PRAGMA_OMP_SINGLE:
27104 case PRAGMA_OMP_TASK:
27105 if (context == pragma_external)
27107 cp_parser_omp_construct (parser, pragma_tok);
27110 case PRAGMA_OMP_SECTION:
27111 error_at (pragma_tok->location,
27112 "%<#pragma omp section%> may only be used in "
27113 "%<#pragma omp sections%> construct");
27117 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27118 c_invoke_pragma_handler (id);
27122 cp_parser_error (parser, "expected declaration specifiers");
27126 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27130 /* The interface the pragma parsers have to the lexer. */
27133 pragma_lex (tree *value)
27136 enum cpp_ttype ret;
27138 tok = cp_lexer_peek_token (the_parser->lexer);
27141 *value = tok->u.value;
27143 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27145 else if (ret == CPP_STRING)
27146 *value = cp_parser_string_literal (the_parser, false, false);
27149 cp_lexer_consume_token (the_parser->lexer);
27150 if (ret == CPP_KEYWORD)
27158 /* External interface. */
27160 /* Parse one entire translation unit. */
27163 c_parse_file (void)
27165 static bool already_called = false;
27167 if (already_called)
27169 sorry ("inter-module optimizations not implemented for C++");
27172 already_called = true;
27174 the_parser = cp_parser_new ();
27175 push_deferring_access_checks (flag_access_control
27176 ? dk_no_deferred : dk_no_check);
27177 cp_parser_translation_unit (the_parser);
27181 #include "gt-cp-parser.h"