2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
30 #include "c-family/c-pragma.h"
33 #include "diagnostic-core.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
39 #include "tree-pretty-print.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 static cp_token eof_token =
50 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 /* The various kinds of non integral constant we encounter. */
54 typedef enum non_integral_constant {
56 /* floating-point literal */
60 /* %<__FUNCTION__%> */
62 /* %<__PRETTY_FUNCTION__%> */
70 /* %<typeid%> operator */
72 /* non-constant compound literals */
80 /* an array reference */
86 /* the address of a label */
100 /* calls to overloaded operators */
104 /* a comma operator */
106 /* a call to a constructor */
108 /* a transaction expression */
110 } non_integral_constant;
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
118 /* is not a class or namespace */
120 /* is not a class, namespace, or enumeration */
124 /* The various kinds of required token */
125 typedef enum required_token {
127 RT_SEMICOLON, /* ';' */
128 RT_OPEN_PAREN, /* '(' */
129 RT_CLOSE_BRACE, /* '}' */
130 RT_OPEN_BRACE, /* '{' */
131 RT_CLOSE_SQUARE, /* ']' */
132 RT_OPEN_SQUARE, /* '[' */
136 RT_GREATER, /* '>' */
138 RT_ELLIPSIS, /* '...' */
142 RT_COLON_SCOPE, /* ':' or '::' */
143 RT_CLOSE_PAREN, /* ')' */
144 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145 RT_PRAGMA_EOL, /* end of line */
146 RT_NAME, /* identifier */
148 /* The type is CPP_KEYWORD */
150 RT_DELETE, /* delete */
151 RT_RETURN, /* return */
152 RT_WHILE, /* while */
153 RT_EXTERN, /* extern */
154 RT_STATIC_ASSERT, /* static_assert */
155 RT_DECLTYPE, /* decltype */
156 RT_OPERATOR, /* operator */
157 RT_CLASS, /* class */
158 RT_TEMPLATE, /* template */
159 RT_NAMESPACE, /* namespace */
160 RT_USING, /* using */
163 RT_CATCH, /* catch */
164 RT_THROW, /* throw */
165 RT_LABEL, /* __label__ */
166 RT_AT_TRY, /* @try */
167 RT_AT_SYNCHRONIZED, /* @synchronized */
168 RT_AT_THROW, /* @throw */
170 RT_SELECT, /* selection-statement */
171 RT_INTERATION, /* iteration-statement */
172 RT_JUMP, /* jump-statement */
173 RT_CLASS_KEY, /* class-key */
174 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
175 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
176 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
177 RT_TRANSACTION_CANCEL /* __transaction_cancel */
182 static cp_lexer *cp_lexer_new_main
184 static cp_lexer *cp_lexer_new_from_tokens
185 (cp_token_cache *tokens);
186 static void cp_lexer_destroy
188 static int cp_lexer_saving_tokens
190 static cp_token *cp_lexer_token_at
191 (cp_lexer *, cp_token_position);
192 static void cp_lexer_get_preprocessor_token
193 (cp_lexer *, cp_token *);
194 static inline cp_token *cp_lexer_peek_token
196 static cp_token *cp_lexer_peek_nth_token
197 (cp_lexer *, size_t);
198 static inline bool cp_lexer_next_token_is
199 (cp_lexer *, enum cpp_ttype);
200 static bool cp_lexer_next_token_is_not
201 (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_keyword
203 (cp_lexer *, enum rid);
204 static cp_token *cp_lexer_consume_token
206 static void cp_lexer_purge_token
208 static void cp_lexer_purge_tokens_after
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_save_tokens
212 static void cp_lexer_commit_tokens
214 static void cp_lexer_rollback_tokens
216 static void cp_lexer_print_token
217 (FILE *, cp_token *);
218 static inline bool cp_lexer_debugging_p
220 static void cp_lexer_start_debugging
221 (cp_lexer *) ATTRIBUTE_UNUSED;
222 static void cp_lexer_stop_debugging
223 (cp_lexer *) ATTRIBUTE_UNUSED;
225 static cp_token_cache *cp_token_cache_new
226 (cp_token *, cp_token *);
228 static void cp_parser_initial_pragma
231 static tree cp_literal_operator_id
234 /* Manifest constants. */
235 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
236 #define CP_SAVED_TOKEN_STACK 5
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
243 /* Nonzero if we are parsing an unevaluated operand: an operand to
244 sizeof, typeof, or alignof. */
245 int cp_unevaluated_operand;
247 /* Dump up to NUM tokens in BUFFER to FILE starting with token
248 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
249 first token in BUFFER. If NUM is 0, dump all the tokens. If
250 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
251 highlighted by surrounding it in [[ ]]. */
254 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
255 cp_token *start_token, unsigned num,
256 cp_token *curr_token)
258 unsigned i, nprinted;
262 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
268 num = buffer->length ();
270 if (start_token == NULL)
271 start_token = buffer->address ();
273 if (start_token > buffer->address ())
275 cp_lexer_print_token (file, &(*buffer)[0]);
276 fprintf (file, " ... ");
281 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
283 if (token == start_token)
290 if (token == curr_token)
291 fprintf (file, "[[");
293 cp_lexer_print_token (file, token);
295 if (token == curr_token)
296 fprintf (file, "]]");
302 case CPP_CLOSE_BRACE:
312 if (i == num && i < buffer->length ())
314 fprintf (file, " ... ");
315 cp_lexer_print_token (file, &buffer->last ());
318 fprintf (file, "\n");
322 /* Dump all tokens in BUFFER to stderr. */
325 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
327 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
332 description for T. */
335 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
339 fprintf (file, "%s: ", desc);
340 print_node_brief (file, "", t, 0);
345 /* Dump parser context C to FILE. */
348 cp_debug_print_context (FILE *file, cp_parser_context *c)
350 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
351 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
352 print_node_brief (file, "", c->object_type, 0);
353 fprintf (file, "}\n");
357 /* Print the stack of parsing contexts to FILE starting with FIRST. */
360 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
363 cp_parser_context *c;
365 fprintf (file, "Parsing context stack:\n");
366 for (i = 0, c = first; c; c = c->next, i++)
368 fprintf (file, "\t#%u: ", i);
369 cp_debug_print_context (file, c);
374 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
377 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
380 fprintf (file, "%s: true\n", desc);
384 /* Print an unparsed function entry UF to FILE. */
387 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
390 cp_default_arg_entry *default_arg_fn;
393 fprintf (file, "\tFunctions with default args:\n");
395 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
398 fprintf (file, "\t\tClass type: ");
399 print_node_brief (file, "", default_arg_fn->class_type, 0);
400 fprintf (file, "\t\tDeclaration: ");
401 print_node_brief (file, "", default_arg_fn->decl, 0);
402 fprintf (file, "\n");
405 fprintf (file, "\n\tFunctions with definitions that require "
406 "post-processing\n\t\t");
407 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
409 print_node_brief (file, "", fn, 0);
412 fprintf (file, "\n");
414 fprintf (file, "\n\tNon-static data members with initializers that require "
415 "post-processing\n\t\t");
416 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
418 print_node_brief (file, "", fn, 0);
421 fprintf (file, "\n");
425 /* Print the stack of unparsed member functions S to FILE. */
428 cp_debug_print_unparsed_queues (FILE *file,
429 vec<cp_unparsed_functions_entry, va_gc> *s)
432 cp_unparsed_functions_entry *uf;
434 fprintf (file, "Unparsed functions\n");
435 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
437 fprintf (file, "#%u:\n", i);
438 cp_debug_print_unparsed_function (file, uf);
443 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
444 the given PARSER. If FILE is NULL, the output is printed on stderr. */
447 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
449 cp_token *next_token, *first_token, *start_token;
454 next_token = parser->lexer->next_token;
455 first_token = parser->lexer->buffer->address ();
456 start_token = (next_token > first_token + window_size / 2)
457 ? next_token - window_size / 2
459 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
464 /* Dump debugging information for the given PARSER. If FILE is NULL,
465 the output is printed on stderr. */
468 cp_debug_parser (FILE *file, cp_parser *parser)
470 const size_t window_size = 20;
472 expanded_location eloc;
477 fprintf (file, "Parser state\n\n");
478 fprintf (file, "Number of tokens: %u\n",
479 vec_safe_length (parser->lexer->buffer));
480 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
481 cp_debug_print_tree_if_set (file, "Object scope",
482 parser->object_scope);
483 cp_debug_print_tree_if_set (file, "Qualifying scope",
484 parser->qualifying_scope);
485 cp_debug_print_context_stack (file, parser->context);
486 cp_debug_print_flag (file, "Allow GNU extensions",
487 parser->allow_gnu_extensions_p);
488 cp_debug_print_flag (file, "'>' token is greater-than",
489 parser->greater_than_is_operator_p);
490 cp_debug_print_flag (file, "Default args allowed in current "
491 "parameter list", parser->default_arg_ok_p);
492 cp_debug_print_flag (file, "Parsing integral constant-expression",
493 parser->integral_constant_expression_p);
494 cp_debug_print_flag (file, "Allow non-constant expression in current "
495 "constant-expression",
496 parser->allow_non_integral_constant_expression_p);
497 cp_debug_print_flag (file, "Seen non-constant expression",
498 parser->non_integral_constant_expression_p);
499 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
501 parser->local_variables_forbidden_p);
502 cp_debug_print_flag (file, "In unbraced linkage specification",
503 parser->in_unbraced_linkage_specification_p);
504 cp_debug_print_flag (file, "Parsing a declarator",
505 parser->in_declarator_p);
506 cp_debug_print_flag (file, "In template argument list",
507 parser->in_template_argument_list_p);
508 cp_debug_print_flag (file, "Parsing an iteration statement",
509 parser->in_statement & IN_ITERATION_STMT);
510 cp_debug_print_flag (file, "Parsing a switch statement",
511 parser->in_statement & IN_SWITCH_STMT);
512 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
513 parser->in_statement & IN_OMP_BLOCK);
514 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
515 parser->in_statement & IN_OMP_FOR);
516 cp_debug_print_flag (file, "Parsing an if statement",
517 parser->in_statement & IN_IF_STMT);
518 cp_debug_print_flag (file, "Parsing a type-id in an expression "
519 "context", parser->in_type_id_in_expr_p);
520 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
521 parser->implicit_extern_c);
522 cp_debug_print_flag (file, "String expressions should be translated "
523 "to execution character set",
524 parser->translate_strings_p);
525 cp_debug_print_flag (file, "Parsing function body outside of a "
526 "local class", parser->in_function_body);
527 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
528 parser->colon_corrects_to_scope_p);
529 if (parser->type_definition_forbidden_message)
530 fprintf (file, "Error message for forbidden type definitions: %s\n",
531 parser->type_definition_forbidden_message);
532 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
533 fprintf (file, "Number of class definitions in progress: %u\n",
534 parser->num_classes_being_defined);
535 fprintf (file, "Number of template parameter lists for the current "
536 "declaration: %u\n", parser->num_template_parameter_lists);
537 cp_debug_parser_tokens (file, parser, window_size);
538 token = parser->lexer->next_token;
539 fprintf (file, "Next token to parse:\n");
540 fprintf (file, "\tToken: ");
541 cp_lexer_print_token (file, token);
542 eloc = expand_location (token->location);
543 fprintf (file, "\n\tFile: %s\n", eloc.file);
544 fprintf (file, "\tLine: %d\n", eloc.line);
545 fprintf (file, "\tColumn: %d\n", eloc.column);
549 /* Allocate memory for a new lexer object and return it. */
552 cp_lexer_alloc (void)
556 c_common_no_more_pch ();
558 /* Allocate the memory. */
559 lexer = ggc_alloc_cleared_cp_lexer ();
561 /* Initially we are not debugging. */
562 lexer->debugging_p = false;
564 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
566 /* Create the buffer. */
567 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
573 /* Create a new main C++ lexer, the lexer that gets tokens from the
577 cp_lexer_new_main (void)
582 /* It's possible that parsing the first pragma will load a PCH file,
583 which is a GC collection point. So we have to do that before
584 allocating any memory. */
585 cp_parser_initial_pragma (&token);
587 lexer = cp_lexer_alloc ();
589 /* Put the first token in the buffer. */
590 lexer->buffer->quick_push (token);
592 /* Get the remaining tokens from the preprocessor. */
593 while (token.type != CPP_EOF)
595 cp_lexer_get_preprocessor_token (lexer, &token);
596 vec_safe_push (lexer->buffer, token);
599 lexer->last_token = lexer->buffer->address ()
600 + lexer->buffer->length ()
602 lexer->next_token = lexer->buffer->length ()
603 ? lexer->buffer->address ()
606 /* Subsequent preprocessor diagnostics should use compiler
607 diagnostic functions to get the compiler source location. */
610 gcc_assert (!lexer->next_token->purged_p);
614 /* Create a new lexer whose token stream is primed with the tokens in
615 CACHE. When these tokens are exhausted, no new tokens will be read. */
618 cp_lexer_new_from_tokens (cp_token_cache *cache)
620 cp_token *first = cache->first;
621 cp_token *last = cache->last;
622 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
624 /* We do not own the buffer. */
625 lexer->buffer = NULL;
626 lexer->next_token = first == last ? &eof_token : first;
627 lexer->last_token = last;
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
631 /* Initially we are not debugging. */
632 lexer->debugging_p = false;
634 gcc_assert (!lexer->next_token->purged_p);
638 /* Frees all resources associated with LEXER. */
641 cp_lexer_destroy (cp_lexer *lexer)
643 vec_free (lexer->buffer);
644 lexer->saved_tokens.release ();
648 /* Returns nonzero if debugging information should be output. */
651 cp_lexer_debugging_p (cp_lexer *lexer)
653 return lexer->debugging_p;
657 static inline cp_token_position
658 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
660 gcc_assert (!previous_p || lexer->next_token != &eof_token);
662 return lexer->next_token - previous_p;
665 static inline cp_token *
666 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
672 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
674 lexer->next_token = cp_lexer_token_at (lexer, pos);
677 static inline cp_token_position
678 cp_lexer_previous_token_position (cp_lexer *lexer)
680 if (lexer->next_token == &eof_token)
681 return lexer->last_token - 1;
683 return cp_lexer_token_position (lexer, true);
686 static inline cp_token *
687 cp_lexer_previous_token (cp_lexer *lexer)
689 cp_token_position tp = cp_lexer_previous_token_position (lexer);
691 return cp_lexer_token_at (lexer, tp);
694 /* nonzero if we are presently saving tokens. */
697 cp_lexer_saving_tokens (const cp_lexer* lexer)
699 return lexer->saved_tokens.length () != 0;
702 /* Store the next token from the preprocessor in *TOKEN. Return true
703 if we reach EOF. If LEXER is NULL, assume we are handling an
704 initial #pragma pch_preprocess, and thus want the lexer to return
705 processed strings. */
708 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
710 static int is_extern_c = 0;
712 /* Get a new token from the preprocessor. */
714 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
715 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
716 token->keyword = RID_MAX;
717 token->pragma_kind = PRAGMA_NONE;
718 token->purged_p = false;
720 /* On some systems, some header files are surrounded by an
721 implicit extern "C" block. Set a flag in the token if it
722 comes from such a header. */
723 is_extern_c += pending_lang_change;
724 pending_lang_change = 0;
725 token->implicit_extern_c = is_extern_c > 0;
727 /* Check to see if this token is a keyword. */
728 if (token->type == CPP_NAME)
730 if (C_IS_RESERVED_WORD (token->u.value))
732 /* Mark this token as a keyword. */
733 token->type = CPP_KEYWORD;
734 /* Record which keyword. */
735 token->keyword = C_RID_CODE (token->u.value);
739 if (warn_cxx0x_compat
740 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
741 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
743 /* Warn about the C++0x keyword (but still treat it as
745 warning (OPT_Wc__0x_compat,
746 "identifier %qE is a keyword in C++11",
749 /* Clear out the C_RID_CODE so we don't warn about this
750 particular identifier-turned-keyword again. */
751 C_SET_RID_CODE (token->u.value, RID_MAX);
754 token->ambiguous_p = false;
755 token->keyword = RID_MAX;
758 else if (token->type == CPP_AT_NAME)
760 /* This only happens in Objective-C++; it must be a keyword. */
761 token->type = CPP_KEYWORD;
762 switch (C_RID_CODE (token->u.value))
764 /* Replace 'class' with '@class', 'private' with '@private',
765 etc. This prevents confusion with the C++ keyword
766 'class', and makes the tokens consistent with other
767 Objective-C 'AT' keywords. For example '@class' is
768 reported as RID_AT_CLASS which is consistent with
769 '@synchronized', which is reported as
772 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
773 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
774 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
775 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
776 case RID_THROW: token->keyword = RID_AT_THROW; break;
777 case RID_TRY: token->keyword = RID_AT_TRY; break;
778 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
779 default: token->keyword = C_RID_CODE (token->u.value);
782 else if (token->type == CPP_PRAGMA)
784 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
785 token->pragma_kind = ((enum pragma_kind)
786 TREE_INT_CST_LOW (token->u.value));
787 token->u.value = NULL_TREE;
791 /* Update the globals input_location and the input file stack from TOKEN. */
793 cp_lexer_set_source_position_from_token (cp_token *token)
795 if (token->type != CPP_EOF)
797 input_location = token->location;
801 /* Return a pointer to the next token in the token stream, but do not
804 static inline cp_token *
805 cp_lexer_peek_token (cp_lexer *lexer)
807 if (cp_lexer_debugging_p (lexer))
809 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
810 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
811 putc ('\n', cp_lexer_debug_stream);
813 return lexer->next_token;
816 /* Return true if the next token has the indicated TYPE. */
819 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
821 return cp_lexer_peek_token (lexer)->type == type;
824 /* Return true if the next token does not have the indicated TYPE. */
827 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
829 return !cp_lexer_next_token_is (lexer, type);
832 /* Return true if the next token is the indicated KEYWORD. */
835 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
837 return cp_lexer_peek_token (lexer)->keyword == keyword;
840 /* Return true if the next token is not the indicated KEYWORD. */
843 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
845 return cp_lexer_peek_token (lexer)->keyword != keyword;
848 /* Return true if the next token is a keyword for a decl-specifier. */
851 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
855 token = cp_lexer_peek_token (lexer);
856 switch (token->keyword)
858 /* auto specifier: storage-class-specifier in C++,
859 simple-type-specifier in C++0x. */
861 /* Storage classes. */
867 /* Elaborated type specifiers. */
873 /* Simple type specifiers. */
888 /* GNU extensions. */
891 /* C++0x extensions. */
893 case RID_UNDERLYING_TYPE:
901 /* Returns TRUE iff the token T begins a decltype type. */
904 token_is_decltype (cp_token *t)
906 return (t->keyword == RID_DECLTYPE
907 || t->type == CPP_DECLTYPE);
910 /* Returns TRUE iff the next token begins a decltype type. */
913 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
915 cp_token *t = cp_lexer_peek_token (lexer);
916 return token_is_decltype (t);
919 /* Return a pointer to the Nth token in the token stream. If N is 1,
920 then this is precisely equivalent to cp_lexer_peek_token (except
921 that it is not inline). One would like to disallow that case, but
922 there is one case (cp_parser_nth_token_starts_template_id) where
923 the caller passes a variable for N and it might be 1. */
926 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
930 /* N is 1-based, not zero-based. */
933 if (cp_lexer_debugging_p (lexer))
934 fprintf (cp_lexer_debug_stream,
935 "cp_lexer: peeking ahead %ld at token: ", (long)n);
938 token = lexer->next_token;
939 gcc_assert (!n || token != &eof_token);
943 if (token == lexer->last_token)
949 if (!token->purged_p)
953 if (cp_lexer_debugging_p (lexer))
955 cp_lexer_print_token (cp_lexer_debug_stream, token);
956 putc ('\n', cp_lexer_debug_stream);
962 /* Return the next token, and advance the lexer's next_token pointer
963 to point to the next non-purged token. */
966 cp_lexer_consume_token (cp_lexer* lexer)
968 cp_token *token = lexer->next_token;
970 gcc_assert (token != &eof_token);
971 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
976 if (lexer->next_token == lexer->last_token)
978 lexer->next_token = &eof_token;
983 while (lexer->next_token->purged_p);
985 cp_lexer_set_source_position_from_token (token);
987 /* Provide debugging output. */
988 if (cp_lexer_debugging_p (lexer))
990 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
991 cp_lexer_print_token (cp_lexer_debug_stream, token);
992 putc ('\n', cp_lexer_debug_stream);
998 /* Permanently remove the next token from the token stream, and
999 advance the next_token pointer to refer to the next non-purged
1003 cp_lexer_purge_token (cp_lexer *lexer)
1005 cp_token *tok = lexer->next_token;
1007 gcc_assert (tok != &eof_token);
1008 tok->purged_p = true;
1009 tok->location = UNKNOWN_LOCATION;
1010 tok->u.value = NULL_TREE;
1011 tok->keyword = RID_MAX;
1016 if (tok == lexer->last_token)
1022 while (tok->purged_p);
1023 lexer->next_token = tok;
1026 /* Permanently remove all tokens after TOK, up to, but not
1027 including, the token that will be returned next by
1028 cp_lexer_peek_token. */
1031 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1033 cp_token *peek = lexer->next_token;
1035 if (peek == &eof_token)
1036 peek = lexer->last_token;
1038 gcc_assert (tok < peek);
1040 for ( tok += 1; tok != peek; tok += 1)
1042 tok->purged_p = true;
1043 tok->location = UNKNOWN_LOCATION;
1044 tok->u.value = NULL_TREE;
1045 tok->keyword = RID_MAX;
1049 /* Begin saving tokens. All tokens consumed after this point will be
1053 cp_lexer_save_tokens (cp_lexer* lexer)
1055 /* Provide debugging output. */
1056 if (cp_lexer_debugging_p (lexer))
1057 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1059 lexer->saved_tokens.safe_push (lexer->next_token);
1062 /* Commit to the portion of the token stream most recently saved. */
1065 cp_lexer_commit_tokens (cp_lexer* lexer)
1067 /* Provide debugging output. */
1068 if (cp_lexer_debugging_p (lexer))
1069 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1071 lexer->saved_tokens.pop ();
1074 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1075 to the token stream. Stop saving tokens. */
1078 cp_lexer_rollback_tokens (cp_lexer* lexer)
1080 /* Provide debugging output. */
1081 if (cp_lexer_debugging_p (lexer))
1082 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1084 lexer->next_token = lexer->saved_tokens.pop ();
1087 /* Print a representation of the TOKEN on the STREAM. */
1090 cp_lexer_print_token (FILE * stream, cp_token *token)
1092 /* We don't use cpp_type2name here because the parser defines
1093 a few tokens of its own. */
1094 static const char *const token_names[] = {
1095 /* cpplib-defined token types */
1096 #define OP(e, s) #e,
1097 #define TK(e, s) #e,
1101 /* C++ parser token types - see "Manifest constants", above. */
1104 "NESTED_NAME_SPECIFIER",
1107 /* For some tokens, print the associated data. */
1108 switch (token->type)
1111 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1112 For example, `struct' is mapped to an INTEGER_CST. */
1113 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1115 /* else fall through */
1117 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1124 case CPP_UTF8STRING:
1125 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1129 print_generic_expr (stream, token->u.value, 0);
1133 /* If we have a name for the token, print it out. Otherwise, we
1134 simply give the numeric code. */
1135 if (token->type < ARRAY_SIZE(token_names))
1136 fputs (token_names[token->type], stream);
1138 fprintf (stream, "[%d]", token->type);
1143 /* Start emitting debugging information. */
1146 cp_lexer_start_debugging (cp_lexer* lexer)
1148 lexer->debugging_p = true;
1149 cp_lexer_debug_stream = stderr;
1152 /* Stop emitting debugging information. */
1155 cp_lexer_stop_debugging (cp_lexer* lexer)
1157 lexer->debugging_p = false;
1158 cp_lexer_debug_stream = NULL;
1161 /* Create a new cp_token_cache, representing a range of tokens. */
1163 static cp_token_cache *
1164 cp_token_cache_new (cp_token *first, cp_token *last)
1166 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1167 cache->first = first;
1173 /* Decl-specifiers. */
1175 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1178 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1180 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1185 /* Nothing other than the parser should be creating declarators;
1186 declarators are a semi-syntactic representation of C++ entities.
1187 Other parts of the front end that need to create entities (like
1188 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1190 static cp_declarator *make_call_declarator
1191 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1192 static cp_declarator *make_array_declarator
1193 (cp_declarator *, tree);
1194 static cp_declarator *make_pointer_declarator
1195 (cp_cv_quals, cp_declarator *, tree);
1196 static cp_declarator *make_reference_declarator
1197 (cp_cv_quals, cp_declarator *, bool, tree);
1198 static cp_parameter_declarator *make_parameter_declarator
1199 (cp_decl_specifier_seq *, cp_declarator *, tree);
1200 static cp_declarator *make_ptrmem_declarator
1201 (cp_cv_quals, tree, cp_declarator *, tree);
1203 /* An erroneous declarator. */
1204 static cp_declarator *cp_error_declarator;
1206 /* The obstack on which declarators and related data structures are
1208 static struct obstack declarator_obstack;
1210 /* Alloc BYTES from the declarator memory pool. */
1212 static inline void *
1213 alloc_declarator (size_t bytes)
1215 return obstack_alloc (&declarator_obstack, bytes);
1218 /* Allocate a declarator of the indicated KIND. Clear fields that are
1219 common to all declarators. */
1221 static cp_declarator *
1222 make_declarator (cp_declarator_kind kind)
1224 cp_declarator *declarator;
1226 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1227 declarator->kind = kind;
1228 declarator->attributes = NULL_TREE;
1229 declarator->std_attributes = NULL_TREE;
1230 declarator->declarator = NULL;
1231 declarator->parameter_pack_p = false;
1232 declarator->id_loc = UNKNOWN_LOCATION;
1237 /* Make a declarator for a generalized identifier. If
1238 QUALIFYING_SCOPE is non-NULL, the identifier is
1239 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1240 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1243 static cp_declarator *
1244 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1245 special_function_kind sfk)
1247 cp_declarator *declarator;
1249 /* It is valid to write:
1251 class C { void f(); };
1255 The standard is not clear about whether `typedef const C D' is
1256 legal; as of 2002-09-15 the committee is considering that
1257 question. EDG 3.0 allows that syntax. Therefore, we do as
1259 if (qualifying_scope && TYPE_P (qualifying_scope))
1260 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1262 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1263 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1264 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1266 declarator = make_declarator (cdk_id);
1267 declarator->u.id.qualifying_scope = qualifying_scope;
1268 declarator->u.id.unqualified_name = unqualified_name;
1269 declarator->u.id.sfk = sfk;
1274 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1275 of modifiers such as const or volatile to apply to the pointer
1276 type, represented as identifiers. ATTRIBUTES represent the attributes that
1277 appertain to the pointer or reference. */
1280 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1283 cp_declarator *declarator;
1285 declarator = make_declarator (cdk_pointer);
1286 declarator->declarator = target;
1287 declarator->u.pointer.qualifiers = cv_qualifiers;
1288 declarator->u.pointer.class_type = NULL_TREE;
1291 declarator->id_loc = target->id_loc;
1292 declarator->parameter_pack_p = target->parameter_pack_p;
1293 target->parameter_pack_p = false;
1296 declarator->parameter_pack_p = false;
1298 declarator->std_attributes = attributes;
1303 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1304 represent the attributes that appertain to the pointer or
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309 bool rvalue_ref, tree attributes)
1311 cp_declarator *declarator;
1313 declarator = make_declarator (cdk_reference);
1314 declarator->declarator = target;
1315 declarator->u.reference.qualifiers = cv_qualifiers;
1316 declarator->u.reference.rvalue_ref = rvalue_ref;
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;
1326 declarator->std_attributes = attributes;
1331 /* Like make_pointer_declarator -- but for a pointer to a non-static
1332 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1333 appertain to the pointer or reference. */
1336 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1337 cp_declarator *pointee,
1340 cp_declarator *declarator;
1342 declarator = make_declarator (cdk_ptrmem);
1343 declarator->declarator = pointee;
1344 declarator->u.pointer.qualifiers = cv_qualifiers;
1345 declarator->u.pointer.class_type = class_type;
1349 declarator->parameter_pack_p = pointee->parameter_pack_p;
1350 pointee->parameter_pack_p = false;
1353 declarator->parameter_pack_p = false;
1355 declarator->std_attributes = attributes;
1360 /* Make a declarator for the function given by TARGET, with the
1361 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1362 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1363 indicates what exceptions can be thrown. */
1366 make_call_declarator (cp_declarator *target,
1368 cp_cv_quals cv_qualifiers,
1369 cp_virt_specifiers virt_specifiers,
1370 cp_ref_qualifier ref_qualifier,
1371 tree exception_specification,
1372 tree late_return_type)
1374 cp_declarator *declarator;
1376 declarator = make_declarator (cdk_function);
1377 declarator->declarator = target;
1378 declarator->u.function.parameters = parms;
1379 declarator->u.function.qualifiers = cv_qualifiers;
1380 declarator->u.function.virt_specifiers = virt_specifiers;
1381 declarator->u.function.ref_qualifier = ref_qualifier;
1382 declarator->u.function.exception_specification = exception_specification;
1383 declarator->u.function.late_return_type = late_return_type;
1386 declarator->id_loc = target->id_loc;
1387 declarator->parameter_pack_p = target->parameter_pack_p;
1388 target->parameter_pack_p = false;
1391 declarator->parameter_pack_p = false;
1396 /* Make a declarator for an array of BOUNDS elements, each of which is
1397 defined by ELEMENT. */
1400 make_array_declarator (cp_declarator *element, tree bounds)
1402 cp_declarator *declarator;
1404 declarator = make_declarator (cdk_array);
1405 declarator->declarator = element;
1406 declarator->u.array.bounds = bounds;
1409 declarator->id_loc = element->id_loc;
1410 declarator->parameter_pack_p = element->parameter_pack_p;
1411 element->parameter_pack_p = false;
1414 declarator->parameter_pack_p = false;
1419 /* Determine whether the declarator we've seen so far can be a
1420 parameter pack, when followed by an ellipsis. */
1422 declarator_can_be_parameter_pack (cp_declarator *declarator)
1424 /* Search for a declarator name, or any other declarator that goes
1425 after the point where the ellipsis could appear in a parameter
1426 pack. If we find any of these, then this declarator can not be
1427 made into a parameter pack. */
1429 while (declarator && !found)
1431 switch ((int)declarator->kind)
1442 declarator = declarator->declarator;
1450 cp_parameter_declarator *no_parameters;
1452 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1453 DECLARATOR and DEFAULT_ARGUMENT. */
1455 cp_parameter_declarator *
1456 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1457 cp_declarator *declarator,
1458 tree default_argument)
1460 cp_parameter_declarator *parameter;
1462 parameter = ((cp_parameter_declarator *)
1463 alloc_declarator (sizeof (cp_parameter_declarator)));
1464 parameter->next = NULL;
1465 if (decl_specifiers)
1466 parameter->decl_specifiers = *decl_specifiers;
1468 clear_decl_specs (¶meter->decl_specifiers);
1469 parameter->declarator = declarator;
1470 parameter->default_argument = default_argument;
1471 parameter->ellipsis_p = false;
1476 /* Returns true iff DECLARATOR is a declaration for a function. */
1479 function_declarator_p (const cp_declarator *declarator)
1483 if (declarator->kind == cdk_function
1484 && declarator->declarator->kind == cdk_id)
1486 if (declarator->kind == cdk_id
1487 || declarator->kind == cdk_error)
1489 declarator = declarator->declarator;
1499 A cp_parser parses the token stream as specified by the C++
1500 grammar. Its job is purely parsing, not semantic analysis. For
1501 example, the parser breaks the token stream into declarators,
1502 expressions, statements, and other similar syntactic constructs.
1503 It does not check that the types of the expressions on either side
1504 of an assignment-statement are compatible, or that a function is
1505 not declared with a parameter of type `void'.
1507 The parser invokes routines elsewhere in the compiler to perform
1508 semantic analysis and to build up the abstract syntax tree for the
1511 The parser (and the template instantiation code, which is, in a
1512 way, a close relative of parsing) are the only parts of the
1513 compiler that should be calling push_scope and pop_scope, or
1514 related functions. The parser (and template instantiation code)
1515 keeps track of what scope is presently active; everything else
1516 should simply honor that. (The code that generates static
1517 initializers may also need to set the scope, in order to check
1518 access control correctly when emitting the initializers.)
1523 The parser is of the standard recursive-descent variety. Upcoming
1524 tokens in the token stream are examined in order to determine which
1525 production to use when parsing a non-terminal. Some C++ constructs
1526 require arbitrary look ahead to disambiguate. For example, it is
1527 impossible, in the general case, to tell whether a statement is an
1528 expression or declaration without scanning the entire statement.
1529 Therefore, the parser is capable of "parsing tentatively." When the
1530 parser is not sure what construct comes next, it enters this mode.
1531 Then, while we attempt to parse the construct, the parser queues up
1532 error messages, rather than issuing them immediately, and saves the
1533 tokens it consumes. If the construct is parsed successfully, the
1534 parser "commits", i.e., it issues any queued error messages and
1535 the tokens that were being preserved are permanently discarded.
1536 If, however, the construct is not parsed successfully, the parser
1537 rolls back its state completely so that it can resume parsing using
1538 a different alternative.
1543 The performance of the parser could probably be improved substantially.
1544 We could often eliminate the need to parse tentatively by looking ahead
1545 a little bit. In some places, this approach might not entirely eliminate
1546 the need to parse tentatively, but it might still speed up the average
1549 /* Flags that are passed to some parsing functions. These values can
1550 be bitwise-ored together. */
1555 CP_PARSER_FLAGS_NONE = 0x0,
1556 /* The construct is optional. If it is not present, then no error
1557 should be issued. */
1558 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1559 /* When parsing a type-specifier, treat user-defined type-names
1560 as non-type identifiers. */
1561 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1562 /* When parsing a type-specifier, do not try to parse a class-specifier
1563 or enum-specifier. */
1564 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1565 /* When parsing a decl-specifier-seq, only allow type-specifier or
1567 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1570 /* This type is used for parameters and variables which hold
1571 combinations of the above flags. */
1572 typedef int cp_parser_flags;
1574 /* The different kinds of declarators we want to parse. */
1576 typedef enum cp_parser_declarator_kind
1578 /* We want an abstract declarator. */
1579 CP_PARSER_DECLARATOR_ABSTRACT,
1580 /* We want a named declarator. */
1581 CP_PARSER_DECLARATOR_NAMED,
1582 /* We don't mind, but the name must be an unqualified-id. */
1583 CP_PARSER_DECLARATOR_EITHER
1584 } cp_parser_declarator_kind;
1586 /* The precedence values used to parse binary expressions. The minimum value
1587 of PREC must be 1, because zero is reserved to quickly discriminate
1588 binary operators from other tokens. */
1593 PREC_LOGICAL_OR_EXPRESSION,
1594 PREC_LOGICAL_AND_EXPRESSION,
1595 PREC_INCLUSIVE_OR_EXPRESSION,
1596 PREC_EXCLUSIVE_OR_EXPRESSION,
1597 PREC_AND_EXPRESSION,
1598 PREC_EQUALITY_EXPRESSION,
1599 PREC_RELATIONAL_EXPRESSION,
1600 PREC_SHIFT_EXPRESSION,
1601 PREC_ADDITIVE_EXPRESSION,
1602 PREC_MULTIPLICATIVE_EXPRESSION,
1604 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1607 /* A mapping from a token type to a corresponding tree node type, with a
1608 precedence value. */
1610 typedef struct cp_parser_binary_operations_map_node
1612 /* The token type. */
1613 enum cpp_ttype token_type;
1614 /* The corresponding tree code. */
1615 enum tree_code tree_type;
1616 /* The precedence of this operator. */
1617 enum cp_parser_prec prec;
1618 } cp_parser_binary_operations_map_node;
1620 typedef struct cp_parser_expression_stack_entry
1622 /* Left hand side of the binary operation we are currently
1625 /* Original tree code for left hand side, if it was a binary
1626 expression itself (used for -Wparentheses). */
1627 enum tree_code lhs_type;
1628 /* Tree code for the binary operation we are parsing. */
1629 enum tree_code tree_type;
1630 /* Precedence of the binary operation we are parsing. */
1631 enum cp_parser_prec prec;
1632 /* Location of the binary operation we are parsing. */
1634 } cp_parser_expression_stack_entry;
1636 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1637 entries because precedence levels on the stack are monotonically
1639 typedef struct cp_parser_expression_stack_entry
1640 cp_parser_expression_stack[NUM_PREC_VALUES];
1644 /* Constructors and destructors. */
1646 static cp_parser_context *cp_parser_context_new
1647 (cp_parser_context *);
1649 /* Class variables. */
1651 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1653 /* The operator-precedence table used by cp_parser_binary_expression.
1654 Transformed into an associative array (binops_by_token) by
1657 static const cp_parser_binary_operations_map_node binops[] = {
1658 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1659 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1661 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1662 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1663 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1665 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1666 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1668 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1669 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1671 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1672 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1673 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1674 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1676 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1677 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1679 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1681 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1683 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1685 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1687 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1690 /* The same as binops, but initialized by cp_parser_new so that
1691 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1693 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1695 /* Constructors and destructors. */
1697 /* Construct a new context. The context below this one on the stack
1698 is given by NEXT. */
1700 static cp_parser_context *
1701 cp_parser_context_new (cp_parser_context* next)
1703 cp_parser_context *context;
1705 /* Allocate the storage. */
1706 if (cp_parser_context_free_list != NULL)
1708 /* Pull the first entry from the free list. */
1709 context = cp_parser_context_free_list;
1710 cp_parser_context_free_list = context->next;
1711 memset (context, 0, sizeof (*context));
1714 context = ggc_alloc_cleared_cp_parser_context ();
1716 /* No errors have occurred yet in this context. */
1717 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1718 /* If this is not the bottommost context, copy information that we
1719 need from the previous context. */
1722 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1723 expression, then we are parsing one in this context, too. */
1724 context->object_type = next->object_type;
1725 /* Thread the stack. */
1726 context->next = next;
1732 /* Managing the unparsed function queues. */
1734 #define unparsed_funs_with_default_args \
1735 parser->unparsed_queues->last ().funs_with_default_args
1736 #define unparsed_funs_with_definitions \
1737 parser->unparsed_queues->last ().funs_with_definitions
1738 #define unparsed_nsdmis \
1739 parser->unparsed_queues->last ().nsdmis
1742 push_unparsed_function_queues (cp_parser *parser)
1744 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1745 vec_safe_push (parser->unparsed_queues, e);
1749 pop_unparsed_function_queues (cp_parser *parser)
1751 release_tree_vector (unparsed_funs_with_definitions);
1752 parser->unparsed_queues->pop ();
1757 /* Constructors and destructors. */
1759 static cp_parser *cp_parser_new
1762 /* Routines to parse various constructs.
1764 Those that return `tree' will return the error_mark_node (rather
1765 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1766 Sometimes, they will return an ordinary node if error-recovery was
1767 attempted, even though a parse error occurred. So, to check
1768 whether or not a parse error occurred, you should always use
1769 cp_parser_error_occurred. If the construct is optional (indicated
1770 either by an `_opt' in the name of the function that does the
1771 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1772 the construct is not present. */
1774 /* Lexical conventions [gram.lex] */
1776 static tree cp_parser_identifier
1778 static tree cp_parser_string_literal
1779 (cp_parser *, bool, bool);
1780 static tree cp_parser_userdef_char_literal
1782 static tree cp_parser_userdef_string_literal
1784 static tree cp_parser_userdef_numeric_literal
1787 /* Basic concepts [gram.basic] */
1789 static bool cp_parser_translation_unit
1792 /* Expressions [gram.expr] */
1794 static tree cp_parser_primary_expression
1795 (cp_parser *, bool, bool, bool, cp_id_kind *);
1796 static tree cp_parser_id_expression
1797 (cp_parser *, bool, bool, bool *, bool, bool);
1798 static tree cp_parser_unqualified_id
1799 (cp_parser *, bool, bool, bool, bool);
1800 static tree cp_parser_nested_name_specifier_opt
1801 (cp_parser *, bool, bool, bool, bool);
1802 static tree cp_parser_nested_name_specifier
1803 (cp_parser *, bool, bool, bool, bool);
1804 static tree cp_parser_qualifying_entity
1805 (cp_parser *, bool, bool, bool, bool, bool);
1806 static tree cp_parser_postfix_expression
1807 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1808 static tree cp_parser_postfix_open_square_expression
1809 (cp_parser *, tree, bool, bool);
1810 static tree cp_parser_postfix_dot_deref_expression
1811 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1812 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1813 (cp_parser *, int, bool, bool, bool *);
1814 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1815 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1816 static void cp_parser_pseudo_destructor_name
1817 (cp_parser *, tree *, tree *);
1818 static tree cp_parser_unary_expression
1819 (cp_parser *, bool, bool, cp_id_kind *);
1820 static enum tree_code cp_parser_unary_operator
1822 static tree cp_parser_new_expression
1824 static vec<tree, va_gc> *cp_parser_new_placement
1826 static tree cp_parser_new_type_id
1827 (cp_parser *, tree *);
1828 static cp_declarator *cp_parser_new_declarator_opt
1830 static cp_declarator *cp_parser_direct_new_declarator
1832 static vec<tree, va_gc> *cp_parser_new_initializer
1834 static tree cp_parser_delete_expression
1836 static tree cp_parser_cast_expression
1837 (cp_parser *, bool, bool, bool, cp_id_kind *);
1838 static tree cp_parser_binary_expression
1839 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1840 static tree cp_parser_question_colon_clause
1841 (cp_parser *, tree);
1842 static tree cp_parser_assignment_expression
1843 (cp_parser *, bool, cp_id_kind *);
1844 static enum tree_code cp_parser_assignment_operator_opt
1846 static tree cp_parser_expression
1847 (cp_parser *, bool, cp_id_kind *);
1848 static tree cp_parser_expression
1849 (cp_parser *, bool, bool, cp_id_kind *);
1850 static tree cp_parser_constant_expression
1851 (cp_parser *, bool, bool *);
1852 static tree cp_parser_builtin_offsetof
1854 static tree cp_parser_lambda_expression
1856 static void cp_parser_lambda_introducer
1857 (cp_parser *, tree);
1858 static bool cp_parser_lambda_declarator_opt
1859 (cp_parser *, tree);
1860 static void cp_parser_lambda_body
1861 (cp_parser *, tree);
1863 /* Statements [gram.stmt.stmt] */
1865 static void cp_parser_statement
1866 (cp_parser *, tree, bool, bool *);
1867 static void cp_parser_label_for_labeled_statement
1868 (cp_parser *, tree);
1869 static tree cp_parser_expression_statement
1870 (cp_parser *, tree);
1871 static tree cp_parser_compound_statement
1872 (cp_parser *, tree, bool, bool);
1873 static void cp_parser_statement_seq_opt
1874 (cp_parser *, tree);
1875 static tree cp_parser_selection_statement
1876 (cp_parser *, bool *);
1877 static tree cp_parser_condition
1879 static tree cp_parser_iteration_statement
1881 static bool cp_parser_for_init_statement
1882 (cp_parser *, tree *decl);
1883 static tree cp_parser_for
1885 static tree cp_parser_c_for
1886 (cp_parser *, tree, tree);
1887 static tree cp_parser_range_for
1888 (cp_parser *, tree, tree, tree);
1889 static void do_range_for_auto_deduction
1891 static tree cp_parser_perform_range_for_lookup
1892 (tree, tree *, tree *);
1893 static tree cp_parser_range_for_member_function
1895 static tree cp_parser_jump_statement
1897 static void cp_parser_declaration_statement
1900 static tree cp_parser_implicitly_scoped_statement
1901 (cp_parser *, bool *);
1902 static void cp_parser_already_scoped_statement
1905 /* Declarations [gram.dcl.dcl] */
1907 static void cp_parser_declaration_seq_opt
1909 static void cp_parser_declaration
1911 static void cp_parser_block_declaration
1912 (cp_parser *, bool);
1913 static void cp_parser_simple_declaration
1914 (cp_parser *, bool, tree *);
1915 static void cp_parser_decl_specifier_seq
1916 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1917 static tree cp_parser_storage_class_specifier_opt
1919 static tree cp_parser_function_specifier_opt
1920 (cp_parser *, cp_decl_specifier_seq *);
1921 static tree cp_parser_type_specifier
1922 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1924 static tree cp_parser_simple_type_specifier
1925 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1926 static tree cp_parser_type_name
1928 static tree cp_parser_nonclass_name
1929 (cp_parser* parser);
1930 static tree cp_parser_elaborated_type_specifier
1931 (cp_parser *, bool, bool);
1932 static tree cp_parser_enum_specifier
1934 static void cp_parser_enumerator_list
1935 (cp_parser *, tree);
1936 static void cp_parser_enumerator_definition
1937 (cp_parser *, tree);
1938 static tree cp_parser_namespace_name
1940 static void cp_parser_namespace_definition
1942 static void cp_parser_namespace_body
1944 static tree cp_parser_qualified_namespace_specifier
1946 static void cp_parser_namespace_alias_definition
1948 static bool cp_parser_using_declaration
1949 (cp_parser *, bool);
1950 static void cp_parser_using_directive
1952 static tree cp_parser_alias_declaration
1954 static void cp_parser_asm_definition
1956 static void cp_parser_linkage_specification
1958 static void cp_parser_static_assert
1959 (cp_parser *, bool);
1960 static tree cp_parser_decltype
1963 /* Declarators [gram.dcl.decl] */
1965 static tree cp_parser_init_declarator
1966 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
1967 static cp_declarator *cp_parser_declarator
1968 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1969 static cp_declarator *cp_parser_direct_declarator
1970 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1971 static enum tree_code cp_parser_ptr_operator
1972 (cp_parser *, tree *, cp_cv_quals *, tree *);
1973 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1975 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1977 static cp_ref_qualifier cp_parser_ref_qualifier_seq_opt
1979 static tree cp_parser_late_return_type_opt
1980 (cp_parser *, cp_cv_quals);
1981 static tree cp_parser_declarator_id
1982 (cp_parser *, bool);
1983 static tree cp_parser_type_id
1985 static tree cp_parser_template_type_arg
1987 static tree cp_parser_trailing_type_id (cp_parser *);
1988 static tree cp_parser_type_id_1
1989 (cp_parser *, bool, bool);
1990 static void cp_parser_type_specifier_seq
1991 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1992 static tree cp_parser_parameter_declaration_clause
1994 static tree cp_parser_parameter_declaration_list
1995 (cp_parser *, bool *);
1996 static cp_parameter_declarator *cp_parser_parameter_declaration
1997 (cp_parser *, bool, bool *);
1998 static tree cp_parser_default_argument
1999 (cp_parser *, bool);
2000 static void cp_parser_function_body
2001 (cp_parser *, bool);
2002 static tree cp_parser_initializer
2003 (cp_parser *, bool *, bool *);
2004 static tree cp_parser_initializer_clause
2005 (cp_parser *, bool *);
2006 static tree cp_parser_braced_list
2007 (cp_parser*, bool*);
2008 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2009 (cp_parser *, bool *);
2011 static bool cp_parser_ctor_initializer_opt_and_function_body
2012 (cp_parser *, bool);
2014 /* Classes [gram.class] */
2016 static tree cp_parser_class_name
2017 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2018 static tree cp_parser_class_specifier
2020 static tree cp_parser_class_head
2021 (cp_parser *, bool *);
2022 static enum tag_types cp_parser_class_key
2024 static void cp_parser_member_specification_opt
2026 static void cp_parser_member_declaration
2028 static tree cp_parser_pure_specifier
2030 static tree cp_parser_constant_initializer
2033 /* Derived classes [gram.class.derived] */
2035 static tree cp_parser_base_clause
2037 static tree cp_parser_base_specifier
2040 /* Special member functions [gram.special] */
2042 static tree cp_parser_conversion_function_id
2044 static tree cp_parser_conversion_type_id
2046 static cp_declarator *cp_parser_conversion_declarator_opt
2048 static bool cp_parser_ctor_initializer_opt
2050 static void cp_parser_mem_initializer_list
2052 static tree cp_parser_mem_initializer
2054 static tree cp_parser_mem_initializer_id
2057 /* Overloading [gram.over] */
2059 static tree cp_parser_operator_function_id
2061 static tree cp_parser_operator
2064 /* Templates [gram.temp] */
2066 static void cp_parser_template_declaration
2067 (cp_parser *, bool);
2068 static tree cp_parser_template_parameter_list
2070 static tree cp_parser_template_parameter
2071 (cp_parser *, bool *, bool *);
2072 static tree cp_parser_type_parameter
2073 (cp_parser *, bool *);
2074 static tree cp_parser_template_id
2075 (cp_parser *, bool, bool, enum tag_types, bool);
2076 static tree cp_parser_template_name
2077 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2078 static tree cp_parser_template_argument_list
2080 static tree cp_parser_template_argument
2082 static void cp_parser_explicit_instantiation
2084 static void cp_parser_explicit_specialization
2087 /* Exception handling [gram.exception] */
2089 static tree cp_parser_try_block
2091 static bool cp_parser_function_try_block
2093 static void cp_parser_handler_seq
2095 static void cp_parser_handler
2097 static tree cp_parser_exception_declaration
2099 static tree cp_parser_throw_expression
2101 static tree cp_parser_exception_specification_opt
2103 static tree cp_parser_type_id_list
2106 /* GNU Extensions */
2108 static tree cp_parser_asm_specification_opt
2110 static tree cp_parser_asm_operand_list
2112 static tree cp_parser_asm_clobber_list
2114 static tree cp_parser_asm_label_list
2116 static bool cp_next_tokens_can_be_attribute_p
2118 static bool cp_next_tokens_can_be_gnu_attribute_p
2120 static bool cp_next_tokens_can_be_std_attribute_p
2122 static bool cp_nth_tokens_can_be_std_attribute_p
2123 (cp_parser *, size_t);
2124 static bool cp_nth_tokens_can_be_gnu_attribute_p
2125 (cp_parser *, size_t);
2126 static bool cp_nth_tokens_can_be_attribute_p
2127 (cp_parser *, size_t);
2128 static tree cp_parser_attributes_opt
2130 static tree cp_parser_gnu_attributes_opt
2132 static tree cp_parser_gnu_attribute_list
2134 static tree cp_parser_std_attribute
2136 static tree cp_parser_std_attribute_spec
2138 static tree cp_parser_std_attribute_spec_seq
2140 static bool cp_parser_extension_opt
2141 (cp_parser *, int *);
2142 static void cp_parser_label_declaration
2145 /* Transactional Memory Extensions */
2147 static tree cp_parser_transaction
2148 (cp_parser *, enum rid);
2149 static tree cp_parser_transaction_expression
2150 (cp_parser *, enum rid);
2151 static bool cp_parser_function_transaction
2152 (cp_parser *, enum rid);
2153 static tree cp_parser_transaction_cancel
2156 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2157 static bool cp_parser_pragma
2158 (cp_parser *, enum pragma_context);
2160 /* Objective-C++ Productions */
2162 static tree cp_parser_objc_message_receiver
2164 static tree cp_parser_objc_message_args
2166 static tree cp_parser_objc_message_expression
2168 static tree cp_parser_objc_encode_expression
2170 static tree cp_parser_objc_defs_expression
2172 static tree cp_parser_objc_protocol_expression
2174 static tree cp_parser_objc_selector_expression
2176 static tree cp_parser_objc_expression
2178 static bool cp_parser_objc_selector_p
2180 static tree cp_parser_objc_selector
2182 static tree cp_parser_objc_protocol_refs_opt
2184 static void cp_parser_objc_declaration
2185 (cp_parser *, tree);
2186 static tree cp_parser_objc_statement
2188 static bool cp_parser_objc_valid_prefix_attributes
2189 (cp_parser *, tree *);
2190 static void cp_parser_objc_at_property_declaration
2192 static void cp_parser_objc_at_synthesize_declaration
2194 static void cp_parser_objc_at_dynamic_declaration
2196 static tree cp_parser_objc_struct_declaration
2199 /* Utility Routines */
2201 static tree cp_parser_lookup_name
2202 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2203 static tree cp_parser_lookup_name_simple
2204 (cp_parser *, tree, location_t);
2205 static tree cp_parser_maybe_treat_template_as_class
2207 static bool cp_parser_check_declarator_template_parameters
2208 (cp_parser *, cp_declarator *, location_t);
2209 static bool cp_parser_check_template_parameters
2210 (cp_parser *, unsigned, location_t, cp_declarator *);
2211 static tree cp_parser_simple_cast_expression
2213 static tree cp_parser_global_scope_opt
2214 (cp_parser *, bool);
2215 static bool cp_parser_constructor_declarator_p
2216 (cp_parser *, bool);
2217 static tree cp_parser_function_definition_from_specifiers_and_declarator
2218 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2219 static tree cp_parser_function_definition_after_declarator
2220 (cp_parser *, bool);
2221 static void cp_parser_template_declaration_after_export
2222 (cp_parser *, bool);
2223 static void cp_parser_perform_template_parameter_access_checks
2224 (vec<deferred_access_check, va_gc> *);
2225 static tree cp_parser_single_declaration
2226 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2227 static tree cp_parser_functional_cast
2228 (cp_parser *, tree);
2229 static tree cp_parser_save_member_function_body
2230 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2231 static tree cp_parser_save_nsdmi
2233 static tree cp_parser_enclosed_template_argument_list
2235 static void cp_parser_save_default_args
2236 (cp_parser *, tree);
2237 static void cp_parser_late_parsing_for_member
2238 (cp_parser *, tree);
2239 static tree cp_parser_late_parse_one_default_arg
2240 (cp_parser *, tree, tree, tree);
2241 static void cp_parser_late_parsing_nsdmi
2242 (cp_parser *, tree);
2243 static void cp_parser_late_parsing_default_args
2244 (cp_parser *, tree);
2245 static tree cp_parser_sizeof_operand
2246 (cp_parser *, enum rid);
2247 static tree cp_parser_trait_expr
2248 (cp_parser *, enum rid);
2249 static bool cp_parser_declares_only_class_p
2251 static void cp_parser_set_storage_class
2252 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2253 static void cp_parser_set_decl_spec_type
2254 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2255 static void set_and_check_decl_spec_loc
2256 (cp_decl_specifier_seq *decl_specs,
2257 cp_decl_spec ds, cp_token *);
2258 static bool cp_parser_friend_p
2259 (const cp_decl_specifier_seq *);
2260 static void cp_parser_required_error
2261 (cp_parser *, required_token, bool);
2262 static cp_token *cp_parser_require
2263 (cp_parser *, enum cpp_ttype, required_token);
2264 static cp_token *cp_parser_require_keyword
2265 (cp_parser *, enum rid, required_token);
2266 static bool cp_parser_token_starts_function_definition_p
2268 static bool cp_parser_next_token_starts_class_definition_p
2270 static bool cp_parser_next_token_ends_template_argument_p
2272 static bool cp_parser_nth_token_starts_template_argument_list_p
2273 (cp_parser *, size_t);
2274 static enum tag_types cp_parser_token_is_class_key
2276 static void cp_parser_check_class_key
2277 (enum tag_types, tree type);
2278 static void cp_parser_check_access_in_redeclaration
2279 (tree type, location_t location);
2280 static bool cp_parser_optional_template_keyword
2282 static void cp_parser_pre_parsed_nested_name_specifier
2284 static bool cp_parser_cache_group
2285 (cp_parser *, enum cpp_ttype, unsigned);
2286 static tree cp_parser_cache_defarg
2287 (cp_parser *parser, bool nsdmi);
2288 static void cp_parser_parse_tentatively
2290 static void cp_parser_commit_to_tentative_parse
2292 static void cp_parser_abort_tentative_parse
2294 static bool cp_parser_parse_definitely
2296 static inline bool cp_parser_parsing_tentatively
2298 static bool cp_parser_uncommitted_to_tentative_parse_p
2300 static void cp_parser_error
2301 (cp_parser *, const char *);
2302 static void cp_parser_name_lookup_error
2303 (cp_parser *, tree, tree, name_lookup_error, location_t);
2304 static bool cp_parser_simulate_error
2306 static bool cp_parser_check_type_definition
2308 static void cp_parser_check_for_definition_in_return_type
2309 (cp_declarator *, tree, location_t type_location);
2310 static void cp_parser_check_for_invalid_template_id
2311 (cp_parser *, tree, enum tag_types, location_t location);
2312 static bool cp_parser_non_integral_constant_expression
2313 (cp_parser *, non_integral_constant);
2314 static void cp_parser_diagnose_invalid_type_name
2315 (cp_parser *, tree, tree, location_t);
2316 static bool cp_parser_parse_and_diagnose_invalid_type_name
2318 static int cp_parser_skip_to_closing_parenthesis
2319 (cp_parser *, bool, bool, bool);
2320 static void cp_parser_skip_to_end_of_statement
2322 static void cp_parser_consume_semicolon_at_end_of_statement
2324 static void cp_parser_skip_to_end_of_block_or_statement
2326 static bool cp_parser_skip_to_closing_brace
2328 static void cp_parser_skip_to_end_of_template_parameter_list
2330 static void cp_parser_skip_to_pragma_eol
2331 (cp_parser*, cp_token *);
2332 static bool cp_parser_error_occurred
2334 static bool cp_parser_allow_gnu_extensions_p
2336 static bool cp_parser_is_pure_string_literal
2338 static bool cp_parser_is_string_literal
2340 static bool cp_parser_is_keyword
2341 (cp_token *, enum rid);
2342 static tree cp_parser_make_typename_type
2343 (cp_parser *, tree, tree, location_t location);
2344 static cp_declarator * cp_parser_make_indirect_declarator
2345 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2347 /* Returns nonzero if we are parsing tentatively. */
2350 cp_parser_parsing_tentatively (cp_parser* parser)
2352 return parser->context->next != NULL;
2355 /* Returns nonzero if TOKEN is a string literal. */
2358 cp_parser_is_pure_string_literal (cp_token* token)
2360 return (token->type == CPP_STRING ||
2361 token->type == CPP_STRING16 ||
2362 token->type == CPP_STRING32 ||
2363 token->type == CPP_WSTRING ||
2364 token->type == CPP_UTF8STRING);
2367 /* Returns nonzero if TOKEN is a string literal
2368 of a user-defined string literal. */
2371 cp_parser_is_string_literal (cp_token* token)
2373 return (cp_parser_is_pure_string_literal (token) ||
2374 token->type == CPP_STRING_USERDEF ||
2375 token->type == CPP_STRING16_USERDEF ||
2376 token->type == CPP_STRING32_USERDEF ||
2377 token->type == CPP_WSTRING_USERDEF ||
2378 token->type == CPP_UTF8STRING_USERDEF);
2381 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2384 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2386 return token->keyword == keyword;
2389 /* If not parsing tentatively, issue a diagnostic of the form
2390 FILE:LINE: MESSAGE before TOKEN
2391 where TOKEN is the next token in the input stream. MESSAGE
2392 (specified by the caller) is usually of the form "expected
2396 cp_parser_error (cp_parser* parser, const char* gmsgid)
2398 if (!cp_parser_simulate_error (parser))
2400 cp_token *token = cp_lexer_peek_token (parser->lexer);
2401 /* This diagnostic makes more sense if it is tagged to the line
2402 of the token we just peeked at. */
2403 cp_lexer_set_source_position_from_token (token);
2405 if (token->type == CPP_PRAGMA)
2407 error_at (token->location,
2408 "%<#pragma%> is not allowed here");
2409 cp_parser_skip_to_pragma_eol (parser, token);
2413 c_parse_error (gmsgid,
2414 /* Because c_parser_error does not understand
2415 CPP_KEYWORD, keywords are treated like
2417 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2418 token->u.value, token->flags);
2422 /* Issue an error about name-lookup failing. NAME is the
2423 IDENTIFIER_NODE DECL is the result of
2424 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2425 the thing that we hoped to find. */
2428 cp_parser_name_lookup_error (cp_parser* parser,
2431 name_lookup_error desired,
2432 location_t location)
2434 /* If name lookup completely failed, tell the user that NAME was not
2436 if (decl == error_mark_node)
2438 if (parser->scope && parser->scope != global_namespace)
2439 error_at (location, "%<%E::%E%> has not been declared",
2440 parser->scope, name);
2441 else if (parser->scope == global_namespace)
2442 error_at (location, "%<::%E%> has not been declared", name);
2443 else if (parser->object_scope
2444 && !CLASS_TYPE_P (parser->object_scope))
2445 error_at (location, "request for member %qE in non-class type %qT",
2446 name, parser->object_scope);
2447 else if (parser->object_scope)
2448 error_at (location, "%<%T::%E%> has not been declared",
2449 parser->object_scope, name);
2451 error_at (location, "%qE has not been declared", name);
2453 else if (parser->scope && parser->scope != global_namespace)
2458 error_at (location, "%<%E::%E%> is not a type",
2459 parser->scope, name);
2462 error_at (location, "%<%E::%E%> is not a class or namespace",
2463 parser->scope, name);
2467 "%<%E::%E%> is not a class, namespace, or enumeration",
2468 parser->scope, name);
2475 else if (parser->scope == global_namespace)
2480 error_at (location, "%<::%E%> is not a type", name);
2483 error_at (location, "%<::%E%> is not a class or namespace", name);
2487 "%<::%E%> is not a class, namespace, or enumeration",
2499 error_at (location, "%qE is not a type", name);
2502 error_at (location, "%qE is not a class or namespace", name);
2506 "%qE is not a class, namespace, or enumeration", name);
2514 /* If we are parsing tentatively, remember that an error has occurred
2515 during this tentative parse. Returns true if the error was
2516 simulated; false if a message should be issued by the caller. */
2519 cp_parser_simulate_error (cp_parser* parser)
2521 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2523 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2529 /* This function is called when a type is defined. If type
2530 definitions are forbidden at this point, an error message is
2534 cp_parser_check_type_definition (cp_parser* parser)
2536 /* If types are forbidden here, issue a message. */
2537 if (parser->type_definition_forbidden_message)
2539 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2540 in the message need to be interpreted. */
2541 error (parser->type_definition_forbidden_message);
2547 /* This function is called when the DECLARATOR is processed. The TYPE
2548 was a type defined in the decl-specifiers. If it is invalid to
2549 define a type in the decl-specifiers for DECLARATOR, an error is
2550 issued. TYPE_LOCATION is the location of TYPE and is used
2551 for error reporting. */
2554 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2555 tree type, location_t type_location)
2557 /* [dcl.fct] forbids type definitions in return types.
2558 Unfortunately, it's not easy to know whether or not we are
2559 processing a return type until after the fact. */
2561 && (declarator->kind == cdk_pointer
2562 || declarator->kind == cdk_reference
2563 || declarator->kind == cdk_ptrmem))
2564 declarator = declarator->declarator;
2566 && declarator->kind == cdk_function)
2568 error_at (type_location,
2569 "new types may not be defined in a return type");
2570 inform (type_location,
2571 "(perhaps a semicolon is missing after the definition of %qT)",
2576 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2577 "<" in any valid C++ program. If the next token is indeed "<",
2578 issue a message warning the user about what appears to be an
2579 invalid attempt to form a template-id. LOCATION is the location
2580 of the type-specifier (TYPE) */
2583 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2585 enum tag_types tag_type,
2586 location_t location)
2588 cp_token_position start = 0;
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2593 error_at (location, "%qT is not a template", type);
2594 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2596 if (tag_type != none_type)
2597 error_at (location, "%qE is not a class template", type);
2599 error_at (location, "%qE is not a template", type);
2602 error_at (location, "invalid template-id");
2603 /* Remember the location of the invalid "<". */
2604 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2605 start = cp_lexer_token_position (parser->lexer, true);
2606 /* Consume the "<". */
2607 cp_lexer_consume_token (parser->lexer);
2608 /* Parse the template arguments. */
2609 cp_parser_enclosed_template_argument_list (parser);
2610 /* Permanently remove the invalid template arguments so that
2611 this error message is not issued again. */
2613 cp_lexer_purge_tokens_after (parser->lexer, start);
2617 /* If parsing an integral constant-expression, issue an error message
2618 about the fact that THING appeared and return true. Otherwise,
2619 return false. In either case, set
2620 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2623 cp_parser_non_integral_constant_expression (cp_parser *parser,
2624 non_integral_constant thing)
2626 parser->non_integral_constant_expression_p = true;
2627 if (parser->integral_constant_expression_p)
2629 if (!parser->allow_non_integral_constant_expression_p)
2631 const char *msg = NULL;
2635 error ("floating-point literal "
2636 "cannot appear in a constant-expression");
2639 error ("a cast to a type other than an integral or "
2640 "enumeration type cannot appear in a "
2641 "constant-expression");
2644 error ("%<typeid%> operator "
2645 "cannot appear in a constant-expression");
2648 error ("non-constant compound literals "
2649 "cannot appear in a constant-expression");
2652 error ("a function call "
2653 "cannot appear in a constant-expression");
2656 error ("an increment "
2657 "cannot appear in a constant-expression");
2660 error ("an decrement "
2661 "cannot appear in a constant-expression");
2664 error ("an array reference "
2665 "cannot appear in a constant-expression");
2667 case NIC_ADDR_LABEL:
2668 error ("the address of a label "
2669 "cannot appear in a constant-expression");
2671 case NIC_OVERLOADED:
2672 error ("calls to overloaded operators "
2673 "cannot appear in a constant-expression");
2675 case NIC_ASSIGNMENT:
2676 error ("an assignment cannot appear in a constant-expression");
2679 error ("a comma operator "
2680 "cannot appear in a constant-expression");
2682 case NIC_CONSTRUCTOR:
2683 error ("a call to a constructor "
2684 "cannot appear in a constant-expression");
2686 case NIC_TRANSACTION:
2687 error ("a transaction expression "
2688 "cannot appear in a constant-expression");
2694 msg = "__FUNCTION__";
2696 case NIC_PRETTY_FUNC:
2697 msg = "__PRETTY_FUNCTION__";
2717 case NIC_PREINCREMENT:
2720 case NIC_PREDECREMENT:
2733 error ("%qs cannot appear in a constant-expression", msg);
2740 /* Emit a diagnostic for an invalid type name. SCOPE is the
2741 qualifying scope (or NULL, if none) for ID. This function commits
2742 to the current active tentative parse, if any. (Otherwise, the
2743 problematic construct might be encountered again later, resulting
2744 in duplicate error messages.) LOCATION is the location of ID. */
2747 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2748 tree scope, tree id,
2749 location_t location)
2751 tree decl, old_scope;
2752 cp_parser_commit_to_tentative_parse (parser);
2753 /* Try to lookup the identifier. */
2754 old_scope = parser->scope;
2755 parser->scope = scope;
2756 decl = cp_parser_lookup_name_simple (parser, id, location);
2757 parser->scope = old_scope;
2758 /* If the lookup found a template-name, it means that the user forgot
2759 to specify an argument list. Emit a useful error message. */
2760 if (TREE_CODE (decl) == TEMPLATE_DECL)
2762 "invalid use of template-name %qE without an argument list",
2764 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2765 error_at (location, "invalid use of destructor %qD as a type", id);
2766 else if (TREE_CODE (decl) == TYPE_DECL)
2767 /* Something like 'unsigned A a;' */
2768 error_at (location, "invalid combination of multiple type-specifiers");
2769 else if (!parser->scope)
2771 /* Issue an error message. */
2772 error_at (location, "%qE does not name a type", id);
2773 /* If we're in a template class, it's possible that the user was
2774 referring to a type from a base class. For example:
2776 template <typename T> struct A { typedef T X; };
2777 template <typename T> struct B : public A<T> { X x; };
2779 The user should have said "typename A<T>::X". */
2780 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2781 inform (location, "C++11 %<constexpr%> only available with "
2782 "-std=c++11 or -std=gnu++11");
2783 else if (processing_template_decl && current_class_type
2784 && TYPE_BINFO (current_class_type))
2788 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2792 tree base_type = BINFO_TYPE (b);
2793 if (CLASS_TYPE_P (base_type)
2794 && dependent_type_p (base_type))
2797 /* Go from a particular instantiation of the
2798 template (which will have an empty TYPE_FIELDs),
2799 to the main version. */
2800 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2801 for (field = TYPE_FIELDS (base_type);
2803 field = DECL_CHAIN (field))
2804 if (TREE_CODE (field) == TYPE_DECL
2805 && DECL_NAME (field) == id)
2808 "(perhaps %<typename %T::%E%> was intended)",
2809 BINFO_TYPE (b), id);
2818 /* Here we diagnose qualified-ids where the scope is actually correct,
2819 but the identifier does not resolve to a valid type name. */
2820 else if (parser->scope != error_mark_node)
2822 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2823 error_at (location, "%qE in namespace %qE does not name a type",
2825 else if (CLASS_TYPE_P (parser->scope)
2826 && constructor_name_p (id, parser->scope))
2829 error_at (location, "%<%T::%E%> names the constructor, not"
2830 " the type", parser->scope, id);
2831 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2832 error_at (location, "and %qT has no template constructors",
2835 else if (TYPE_P (parser->scope)
2836 && dependent_scope_p (parser->scope))
2837 error_at (location, "need %<typename%> before %<%T::%E%> because "
2838 "%qT is a dependent scope",
2839 parser->scope, id, parser->scope);
2840 else if (TYPE_P (parser->scope))
2841 error_at (location, "%qE in %q#T does not name a type",
2848 /* Check for a common situation where a type-name should be present,
2849 but is not, and issue a sensible error message. Returns true if an
2850 invalid type-name was detected.
2852 The situation handled by this function are variable declarations of the
2853 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2854 Usually, `ID' should name a type, but if we got here it means that it
2855 does not. We try to emit the best possible error message depending on
2856 how exactly the id-expression looks like. */
2859 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2862 cp_token *token = cp_lexer_peek_token (parser->lexer);
2864 /* Avoid duplicate error about ambiguous lookup. */
2865 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2867 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2868 if (next->type == CPP_NAME && next->ambiguous_p)
2872 cp_parser_parse_tentatively (parser);
2873 id = cp_parser_id_expression (parser,
2874 /*template_keyword_p=*/false,
2875 /*check_dependency_p=*/true,
2876 /*template_p=*/NULL,
2877 /*declarator_p=*/true,
2878 /*optional_p=*/false);
2879 /* If the next token is a (, this is a function with no explicit return
2880 type, i.e. constructor, destructor or conversion op. */
2881 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2882 || TREE_CODE (id) == TYPE_DECL)
2884 cp_parser_abort_tentative_parse (parser);
2887 if (!cp_parser_parse_definitely (parser))
2890 /* Emit a diagnostic for the invalid type. */
2891 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2892 id, token->location);
2894 /* If we aren't in the middle of a declarator (i.e. in a
2895 parameter-declaration-clause), skip to the end of the declaration;
2896 there's no point in trying to process it. */
2897 if (!parser->in_declarator_p)
2898 cp_parser_skip_to_end_of_block_or_statement (parser);
2902 /* Consume tokens up to, and including, the next non-nested closing `)'.
2903 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2904 are doing error recovery. Returns -1 if OR_COMMA is true and we
2905 found an unnested comma. */
2908 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2913 unsigned paren_depth = 0;
2914 unsigned brace_depth = 0;
2915 unsigned square_depth = 0;
2917 if (recovering && !or_comma
2918 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2923 cp_token * token = cp_lexer_peek_token (parser->lexer);
2925 switch (token->type)
2928 case CPP_PRAGMA_EOL:
2929 /* If we've run out of tokens, then there is no closing `)'. */
2932 /* This is good for lambda expression capture-lists. */
2933 case CPP_OPEN_SQUARE:
2936 case CPP_CLOSE_SQUARE:
2937 if (!square_depth--)
2942 /* This matches the processing in skip_to_end_of_statement. */
2947 case CPP_OPEN_BRACE:
2950 case CPP_CLOSE_BRACE:
2956 if (recovering && or_comma && !brace_depth && !paren_depth
2961 case CPP_OPEN_PAREN:
2966 case CPP_CLOSE_PAREN:
2967 if (!brace_depth && !paren_depth--)
2970 cp_lexer_consume_token (parser->lexer);
2979 /* Consume the token. */
2980 cp_lexer_consume_token (parser->lexer);
2984 /* Consume tokens until we reach the end of the current statement.
2985 Normally, that will be just before consuming a `;'. However, if a
2986 non-nested `}' comes first, then we stop before consuming that. */
2989 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2991 unsigned nesting_depth = 0;
2995 cp_token *token = cp_lexer_peek_token (parser->lexer);
2997 switch (token->type)
3000 case CPP_PRAGMA_EOL:
3001 /* If we've run out of tokens, stop. */
3005 /* If the next token is a `;', we have reached the end of the
3011 case CPP_CLOSE_BRACE:
3012 /* If this is a non-nested '}', stop before consuming it.
3013 That way, when confronted with something like:
3017 we stop before consuming the closing '}', even though we
3018 have not yet reached a `;'. */
3019 if (nesting_depth == 0)
3022 /* If it is the closing '}' for a block that we have
3023 scanned, stop -- but only after consuming the token.
3029 we will stop after the body of the erroneously declared
3030 function, but before consuming the following `typedef'
3032 if (--nesting_depth == 0)
3034 cp_lexer_consume_token (parser->lexer);
3038 case CPP_OPEN_BRACE:
3046 /* Consume the token. */
3047 cp_lexer_consume_token (parser->lexer);
3051 /* This function is called at the end of a statement or declaration.
3052 If the next token is a semicolon, it is consumed; otherwise, error
3053 recovery is attempted. */
3056 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3058 /* Look for the trailing `;'. */
3059 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3061 /* If there is additional (erroneous) input, skip to the end of
3063 cp_parser_skip_to_end_of_statement (parser);
3064 /* If the next token is now a `;', consume it. */
3065 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3066 cp_lexer_consume_token (parser->lexer);
3070 /* Skip tokens until we have consumed an entire block, or until we
3071 have consumed a non-nested `;'. */
3074 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3076 int nesting_depth = 0;
3078 while (nesting_depth >= 0)
3080 cp_token *token = cp_lexer_peek_token (parser->lexer);
3082 switch (token->type)
3085 case CPP_PRAGMA_EOL:
3086 /* If we've run out of tokens, stop. */
3090 /* Stop if this is an unnested ';'. */
3095 case CPP_CLOSE_BRACE:
3096 /* Stop if this is an unnested '}', or closes the outermost
3099 if (nesting_depth < 0)
3105 case CPP_OPEN_BRACE:
3114 /* Consume the token. */
3115 cp_lexer_consume_token (parser->lexer);
3119 /* Skip tokens until a non-nested closing curly brace is the next
3120 token, or there are no more tokens. Return true in the first case,
3124 cp_parser_skip_to_closing_brace (cp_parser *parser)
3126 unsigned nesting_depth = 0;
3130 cp_token *token = cp_lexer_peek_token (parser->lexer);
3132 switch (token->type)
3135 case CPP_PRAGMA_EOL:
3136 /* If we've run out of tokens, stop. */
3139 case CPP_CLOSE_BRACE:
3140 /* If the next token is a non-nested `}', then we have reached
3141 the end of the current block. */
3142 if (nesting_depth-- == 0)
3146 case CPP_OPEN_BRACE:
3147 /* If it the next token is a `{', then we are entering a new
3148 block. Consume the entire block. */
3156 /* Consume the token. */
3157 cp_lexer_consume_token (parser->lexer);
3161 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3162 parameter is the PRAGMA token, allowing us to purge the entire pragma
3166 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3170 parser->lexer->in_pragma = false;
3173 token = cp_lexer_consume_token (parser->lexer);
3174 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3176 /* Ensure that the pragma is not parsed again. */
3177 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3180 /* Require pragma end of line, resyncing with it as necessary. The
3181 arguments are as for cp_parser_skip_to_pragma_eol. */
3184 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3186 parser->lexer->in_pragma = false;
3187 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3188 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3191 /* This is a simple wrapper around make_typename_type. When the id is
3192 an unresolved identifier node, we can provide a superior diagnostic
3193 using cp_parser_diagnose_invalid_type_name. */
3196 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3197 tree id, location_t id_location)
3200 if (TREE_CODE (id) == IDENTIFIER_NODE)
3202 result = make_typename_type (scope, id, typename_type,
3203 /*complain=*/tf_none);
3204 if (result == error_mark_node)
3205 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3208 return make_typename_type (scope, id, typename_type, tf_error);
3211 /* This is a wrapper around the
3212 make_{pointer,ptrmem,reference}_declarator functions that decides
3213 which one to call based on the CODE and CLASS_TYPE arguments. The
3214 CODE argument should be one of the values returned by
3215 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3216 appertain to the pointer or reference. */
3218 static cp_declarator *
3219 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3220 cp_cv_quals cv_qualifiers,
3221 cp_declarator *target,
3224 if (code == ERROR_MARK)
3225 return cp_error_declarator;
3227 if (code == INDIRECT_REF)
3228 if (class_type == NULL_TREE)
3229 return make_pointer_declarator (cv_qualifiers, target, attributes);
3231 return make_ptrmem_declarator (cv_qualifiers, class_type,
3232 target, attributes);
3233 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3234 return make_reference_declarator (cv_qualifiers, target,
3236 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3237 return make_reference_declarator (cv_qualifiers, target,
3242 /* Create a new C++ parser. */
3245 cp_parser_new (void)
3251 /* cp_lexer_new_main is called before doing GC allocation because
3252 cp_lexer_new_main might load a PCH file. */
3253 lexer = cp_lexer_new_main ();
3255 /* Initialize the binops_by_token so that we can get the tree
3256 directly from the token. */
3257 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3258 binops_by_token[binops[i].token_type] = binops[i];
3260 parser = ggc_alloc_cleared_cp_parser ();
3261 parser->lexer = lexer;
3262 parser->context = cp_parser_context_new (NULL);
3264 /* For now, we always accept GNU extensions. */
3265 parser->allow_gnu_extensions_p = 1;
3267 /* The `>' token is a greater-than operator, not the end of a
3269 parser->greater_than_is_operator_p = true;
3271 parser->default_arg_ok_p = true;
3273 /* We are not parsing a constant-expression. */
3274 parser->integral_constant_expression_p = false;
3275 parser->allow_non_integral_constant_expression_p = false;
3276 parser->non_integral_constant_expression_p = false;
3278 /* Local variable names are not forbidden. */
3279 parser->local_variables_forbidden_p = false;
3281 /* We are not processing an `extern "C"' declaration. */
3282 parser->in_unbraced_linkage_specification_p = false;
3284 /* We are not processing a declarator. */
3285 parser->in_declarator_p = false;
3287 /* We are not processing a template-argument-list. */
3288 parser->in_template_argument_list_p = false;
3290 /* We are not in an iteration statement. */
3291 parser->in_statement = 0;
3293 /* We are not in a switch statement. */
3294 parser->in_switch_statement_p = false;
3296 /* We are not parsing a type-id inside an expression. */
3297 parser->in_type_id_in_expr_p = false;
3299 /* Declarations aren't implicitly extern "C". */
3300 parser->implicit_extern_c = false;
3302 /* String literals should be translated to the execution character set. */
3303 parser->translate_strings_p = true;
3305 /* We are not parsing a function body. */
3306 parser->in_function_body = false;
3308 /* We can correct until told otherwise. */
3309 parser->colon_corrects_to_scope_p = true;
3311 /* The unparsed function queue is empty. */
3312 push_unparsed_function_queues (parser);
3314 /* There are no classes being defined. */
3315 parser->num_classes_being_defined = 0;
3317 /* No template parameters apply. */
3318 parser->num_template_parameter_lists = 0;
3323 /* Create a cp_lexer structure which will emit the tokens in CACHE
3324 and push it onto the parser's lexer stack. This is used for delayed
3325 parsing of in-class method bodies and default arguments, and should
3326 not be confused with tentative parsing. */
3328 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3330 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3331 lexer->next = parser->lexer;
3332 parser->lexer = lexer;
3334 /* Move the current source position to that of the first token in the
3336 cp_lexer_set_source_position_from_token (lexer->next_token);
3339 /* Pop the top lexer off the parser stack. This is never used for the
3340 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3342 cp_parser_pop_lexer (cp_parser *parser)
3344 cp_lexer *lexer = parser->lexer;
3345 parser->lexer = lexer->next;
3346 cp_lexer_destroy (lexer);
3348 /* Put the current source position back where it was before this
3349 lexer was pushed. */
3350 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3353 /* Lexical conventions [gram.lex] */
3355 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3359 cp_parser_identifier (cp_parser* parser)
3363 /* Look for the identifier. */
3364 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3365 /* Return the value. */
3366 return token ? token->u.value : error_mark_node;
3369 /* Parse a sequence of adjacent string constants. Returns a
3370 TREE_STRING representing the combined, nul-terminated string
3371 constant. If TRANSLATE is true, translate the string to the
3372 execution character set. If WIDE_OK is true, a wide string is
3375 C++98 [lex.string] says that if a narrow string literal token is
3376 adjacent to a wide string literal token, the behavior is undefined.
3377 However, C99 6.4.5p4 says that this results in a wide string literal.
3378 We follow C99 here, for consistency with the C front end.
3380 This code is largely lifted from lex_string() in c-lex.c.
3382 FUTURE: ObjC++ will need to handle @-strings here. */
3384 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3388 struct obstack str_ob;
3389 cpp_string str, istr, *strs;
3391 enum cpp_ttype type, curr_type;
3392 int have_suffix_p = 0;
3394 tree suffix_id = NULL_TREE;
3395 bool curr_tok_is_userdef_p = false;
3397 tok = cp_lexer_peek_token (parser->lexer);
3398 if (!cp_parser_is_string_literal (tok))
3400 cp_parser_error (parser, "expected string-literal");
3401 return error_mark_node;
3404 if (cpp_userdef_string_p (tok->type))
3406 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3407 curr_type = cpp_userdef_string_remove_type (tok->type);
3408 curr_tok_is_userdef_p = true;
3412 string_tree = tok->u.value;
3413 curr_type = tok->type;
3417 /* Try to avoid the overhead of creating and destroying an obstack
3418 for the common case of just one string. */
3419 if (!cp_parser_is_string_literal
3420 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3422 cp_lexer_consume_token (parser->lexer);
3424 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3425 str.len = TREE_STRING_LENGTH (string_tree);
3428 if (curr_tok_is_userdef_p)
3430 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3432 curr_type = cpp_userdef_string_remove_type (tok->type);
3435 curr_type = tok->type;
3441 gcc_obstack_init (&str_ob);
3446 cp_lexer_consume_token (parser->lexer);
3448 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3449 str.len = TREE_STRING_LENGTH (string_tree);
3451 if (curr_tok_is_userdef_p)
3453 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3454 if (have_suffix_p == 0)
3456 suffix_id = curr_suffix_id;
3459 else if (have_suffix_p == 1
3460 && curr_suffix_id != suffix_id)
3462 error ("inconsistent user-defined literal suffixes"
3463 " %qD and %qD in string literal",
3464 suffix_id, curr_suffix_id);
3467 curr_type = cpp_userdef_string_remove_type (tok->type);
3470 curr_type = tok->type;
3472 if (type != curr_type)
3474 if (type == CPP_STRING)
3476 else if (curr_type != CPP_STRING)
3477 error_at (tok->location,
3478 "unsupported non-standard concatenation "
3479 "of string literals");
3482 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3484 tok = cp_lexer_peek_token (parser->lexer);
3485 if (cpp_userdef_string_p (tok->type))
3487 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3488 curr_type = cpp_userdef_string_remove_type (tok->type);
3489 curr_tok_is_userdef_p = true;
3493 string_tree = tok->u.value;
3494 curr_type = tok->type;
3495 curr_tok_is_userdef_p = false;
3498 while (cp_parser_is_string_literal (tok));
3500 strs = (cpp_string *) obstack_finish (&str_ob);
3503 if (type != CPP_STRING && !wide_ok)
3505 cp_parser_error (parser, "a wide string is invalid in this context");
3509 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3510 (parse_in, strs, count, &istr, type))
3512 value = build_string (istr.len, (const char *)istr.text);
3513 free (CONST_CAST (unsigned char *, istr.text));
3519 case CPP_UTF8STRING:
3520 TREE_TYPE (value) = char_array_type_node;
3523 TREE_TYPE (value) = char16_array_type_node;
3526 TREE_TYPE (value) = char32_array_type_node;
3529 TREE_TYPE (value) = wchar_array_type_node;
3533 value = fix_string_type (value);
3537 tree literal = build_userdef_literal (suffix_id, value,
3538 OT_NONE, NULL_TREE);
3539 tok->u.value = literal;
3540 return cp_parser_userdef_string_literal (tok);
3544 /* cpp_interpret_string has issued an error. */
3545 value = error_mark_node;
3548 obstack_free (&str_ob, 0);
3553 /* Look up a literal operator with the name and the exact arguments. */
3556 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3559 decl = lookup_name (name);
3560 if (!decl || !is_overloaded_fn (decl))
3561 return error_mark_node;
3563 for (fns = decl; fns; fns = OVL_NEXT (fns))
3567 tree fn = OVL_CURRENT (fns);
3568 tree argtypes = NULL_TREE;
3569 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3570 if (argtypes != NULL_TREE)
3572 for (ix = 0; ix < vec_safe_length (args) && argtypes != NULL_TREE;
3573 ++ix, argtypes = TREE_CHAIN (argtypes))
3575 tree targ = TREE_VALUE (argtypes);
3576 tree tparm = TREE_TYPE ((*args)[ix]);
3577 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3578 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3579 if ((ptr || arr || !same_type_p (targ, tparm))
3581 || !same_type_p (TREE_TYPE (targ),
3582 TREE_TYPE (tparm))))
3586 && ix == vec_safe_length (args)
3587 /* May be this should be sufficient_parms_p instead,
3588 depending on how exactly should user-defined literals
3589 work in presence of default arguments on the literal
3590 operator parameters. */
3591 && argtypes == void_list_node)
3596 return error_mark_node;
3599 /* Parse a user-defined char constant. Returns a call to a user-defined
3600 literal operator taking the character as an argument. */
3603 cp_parser_userdef_char_literal (cp_parser *parser)
3605 cp_token *token = cp_lexer_consume_token (parser->lexer);
3606 tree literal = token->u.value;
3607 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3608 tree value = USERDEF_LITERAL_VALUE (literal);
3609 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3612 /* Build up a call to the user-defined operator */
3613 /* Lookup the name we got back from the id-expression. */
3614 vec<tree, va_gc> *args = make_tree_vector ();
3615 vec_safe_push (args, value);
3616 decl = lookup_literal_operator (name, args);
3617 if (!decl || decl == error_mark_node)
3619 error ("unable to find character literal operator %qD with %qT argument",
3620 name, TREE_TYPE (value));
3621 release_tree_vector (args);
3622 return error_mark_node;
3624 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3625 release_tree_vector (args);
3626 if (result != error_mark_node)
3629 error ("unable to find character literal operator %qD with %qT argument",
3630 name, TREE_TYPE (value));
3631 return error_mark_node;
3634 /* A subroutine of cp_parser_userdef_numeric_literal to
3635 create a char... template parameter pack from a string node. */
3638 make_char_string_pack (tree value)
3641 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3642 const char *str = TREE_STRING_POINTER (value);
3643 int i, len = TREE_STRING_LENGTH (value) - 1;
3644 tree argvec = make_tree_vec (1);
3646 /* Fill in CHARVEC with all of the parameters. */
3647 charvec = make_tree_vec (len);
3648 for (i = 0; i < len; ++i)
3649 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3651 /* Build the argument packs. */
3652 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3653 TREE_TYPE (argpack) = char_type_node;
3655 TREE_VEC_ELT (argvec, 0) = argpack;
3660 /* Parse a user-defined numeric constant. returns a call to a user-defined
3661 literal operator. */
3664 cp_parser_userdef_numeric_literal (cp_parser *parser)
3666 cp_token *token = cp_lexer_consume_token (parser->lexer);
3667 tree literal = token->u.value;
3668 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3669 tree value = USERDEF_LITERAL_VALUE (literal);
3670 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3671 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3672 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3674 vec<tree, va_gc> *args;
3676 /* Look for a literal operator taking the exact type of numeric argument
3677 as the literal value. */
3678 args = make_tree_vector ();
3679 vec_safe_push (args, value);
3680 decl = lookup_literal_operator (name, args);
3681 if (decl && decl != error_mark_node)
3683 result = finish_call_expr (decl, &args, false, true, tf_none);
3684 if (result != error_mark_node)
3686 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3687 warning_at (token->location, OPT_Woverflow,
3688 "integer literal exceeds range of %qT type",
3689 long_long_unsigned_type_node);
3693 warning_at (token->location, OPT_Woverflow,
3694 "floating literal exceeds range of %qT type",
3695 long_double_type_node);
3696 else if (overflow < 0)
3697 warning_at (token->location, OPT_Woverflow,
3698 "floating literal truncated to zero");
3700 release_tree_vector (args);
3704 release_tree_vector (args);
3706 /* If the numeric argument didn't work, look for a raw literal
3707 operator taking a const char* argument consisting of the number
3708 in string format. */
3709 args = make_tree_vector ();
3710 vec_safe_push (args, num_string);
3711 decl = lookup_literal_operator (name, args);
3712 if (decl && decl != error_mark_node)
3714 result = finish_call_expr (decl, &args, false, true, tf_none);
3715 if (result != error_mark_node)
3717 release_tree_vector (args);
3721 release_tree_vector (args);
3723 /* If the raw literal didn't work, look for a non-type template
3724 function with parameter pack char.... Call the function with
3725 template parameter characters representing the number. */
3726 args = make_tree_vector ();
3727 decl = lookup_literal_operator (name, args);
3728 if (decl && decl != error_mark_node)
3730 tree tmpl_args = make_char_string_pack (num_string);
3731 decl = lookup_template_function (decl, tmpl_args);
3732 result = finish_call_expr (decl, &args, false, true, tf_none);
3733 if (result != error_mark_node)
3735 release_tree_vector (args);
3739 release_tree_vector (args);
3741 error ("unable to find numeric literal operator %qD", name);
3742 return error_mark_node;
3745 /* Parse a user-defined string constant. Returns a call to a user-defined
3746 literal operator taking a character pointer and the length of the string
3750 cp_parser_userdef_string_literal (cp_token *token)
3752 tree literal = token->u.value;
3753 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3754 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3755 tree value = USERDEF_LITERAL_VALUE (literal);
3756 int len = TREE_STRING_LENGTH (value)
3757 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3760 /* Build up a call to the user-defined operator */
3761 /* Lookup the name we got back from the id-expression. */
3762 vec<tree, va_gc> *args = make_tree_vector ();
3763 vec_safe_push (args, value);
3764 vec_safe_push (args, build_int_cst (size_type_node, len));
3765 decl = lookup_name (name);
3766 if (!decl || decl == error_mark_node)
3768 error ("unable to find string literal operator %qD", name);
3769 release_tree_vector (args);
3770 return error_mark_node;
3772 result = finish_call_expr (decl, &args, false, true, tf_none);
3773 release_tree_vector (args);
3774 if (result != error_mark_node)
3777 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3778 name, TREE_TYPE (value), size_type_node);
3779 return error_mark_node;
3783 /* Basic concepts [gram.basic] */
3785 /* Parse a translation-unit.
3788 declaration-seq [opt]
3790 Returns TRUE if all went well. */
3793 cp_parser_translation_unit (cp_parser* parser)
3795 /* The address of the first non-permanent object on the declarator
3797 static void *declarator_obstack_base;
3801 /* Create the declarator obstack, if necessary. */
3802 if (!cp_error_declarator)
3804 gcc_obstack_init (&declarator_obstack);
3805 /* Create the error declarator. */
3806 cp_error_declarator = make_declarator (cdk_error);
3807 /* Create the empty parameter list. */
3808 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3809 /* Remember where the base of the declarator obstack lies. */
3810 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3813 cp_parser_declaration_seq_opt (parser);
3815 /* If there are no tokens left then all went well. */
3816 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3818 /* Get rid of the token array; we don't need it any more. */
3819 cp_lexer_destroy (parser->lexer);
3820 parser->lexer = NULL;
3822 /* This file might have been a context that's implicitly extern
3823 "C". If so, pop the lang context. (Only relevant for PCH.) */
3824 if (parser->implicit_extern_c)
3826 pop_lang_context ();
3827 parser->implicit_extern_c = false;
3831 finish_translation_unit ();
3837 cp_parser_error (parser, "expected declaration");
3841 /* Make sure the declarator obstack was fully cleaned up. */
3842 gcc_assert (obstack_next_free (&declarator_obstack)
3843 == declarator_obstack_base);
3845 /* All went well. */
3849 /* Return the appropriate tsubst flags for parsing, possibly in N3276
3850 decltype context. */
3852 static inline tsubst_flags_t
3853 complain_flags (bool decltype_p)
3855 tsubst_flags_t complain = tf_warning_or_error;
3857 complain |= tf_decltype;
3861 /* Expressions [gram.expr] */
3863 /* Parse a primary-expression.
3874 ( compound-statement )
3875 __builtin_va_arg ( assignment-expression , type-id )
3876 __builtin_offsetof ( type-id , offsetof-expression )
3879 __has_nothrow_assign ( type-id )
3880 __has_nothrow_constructor ( type-id )
3881 __has_nothrow_copy ( type-id )
3882 __has_trivial_assign ( type-id )
3883 __has_trivial_constructor ( type-id )
3884 __has_trivial_copy ( type-id )
3885 __has_trivial_destructor ( type-id )
3886 __has_virtual_destructor ( type-id )
3887 __is_abstract ( type-id )
3888 __is_base_of ( type-id , type-id )
3889 __is_class ( type-id )
3890 __is_convertible_to ( type-id , type-id )
3891 __is_empty ( type-id )
3892 __is_enum ( type-id )
3893 __is_final ( type-id )
3894 __is_literal_type ( type-id )
3895 __is_pod ( type-id )
3896 __is_polymorphic ( type-id )
3897 __is_std_layout ( type-id )
3898 __is_trivial ( type-id )
3899 __is_union ( type-id )
3901 Objective-C++ Extension:
3909 ADDRESS_P is true iff this expression was immediately preceded by
3910 "&" and therefore might denote a pointer-to-member. CAST_P is true
3911 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3912 true iff this expression is a template argument.
3914 Returns a representation of the expression. Upon return, *IDK
3915 indicates what kind of id-expression (if any) was present. */
3918 cp_parser_primary_expression (cp_parser *parser,
3921 bool template_arg_p,
3925 cp_token *token = NULL;
3927 /* Assume the primary expression is not an id-expression. */
3928 *idk = CP_ID_KIND_NONE;
3930 /* Peek at the next token. */
3931 token = cp_lexer_peek_token (parser->lexer);
3932 switch (token->type)
3941 user-defined-literal */
3947 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3948 return cp_parser_userdef_numeric_literal (parser);
3949 token = cp_lexer_consume_token (parser->lexer);
3950 if (TREE_CODE (token->u.value) == FIXED_CST)
3952 error_at (token->location,
3953 "fixed-point types not supported in C++");
3954 return error_mark_node;
3956 /* Floating-point literals are only allowed in an integral
3957 constant expression if they are cast to an integral or
3958 enumeration type. */
3959 if (TREE_CODE (token->u.value) == REAL_CST
3960 && parser->integral_constant_expression_p
3963 /* CAST_P will be set even in invalid code like "int(2.7 +
3964 ...)". Therefore, we have to check that the next token
3965 is sure to end the cast. */
3968 cp_token *next_token;
3970 next_token = cp_lexer_peek_token (parser->lexer);
3971 if (/* The comma at the end of an
3972 enumerator-definition. */
3973 next_token->type != CPP_COMMA
3974 /* The curly brace at the end of an enum-specifier. */
3975 && next_token->type != CPP_CLOSE_BRACE
3976 /* The end of a statement. */
3977 && next_token->type != CPP_SEMICOLON
3978 /* The end of the cast-expression. */
3979 && next_token->type != CPP_CLOSE_PAREN
3980 /* The end of an array bound. */
3981 && next_token->type != CPP_CLOSE_SQUARE
3982 /* The closing ">" in a template-argument-list. */
3983 && (next_token->type != CPP_GREATER
3984 || parser->greater_than_is_operator_p)
3985 /* C++0x only: A ">>" treated like two ">" tokens,
3986 in a template-argument-list. */
3987 && (next_token->type != CPP_RSHIFT
3988 || (cxx_dialect == cxx98)
3989 || parser->greater_than_is_operator_p))
3993 /* If we are within a cast, then the constraint that the
3994 cast is to an integral or enumeration type will be
3995 checked at that point. If we are not within a cast, then
3996 this code is invalid. */
3998 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4000 return token->u.value;
4002 case CPP_CHAR_USERDEF:
4003 case CPP_CHAR16_USERDEF:
4004 case CPP_CHAR32_USERDEF:
4005 case CPP_WCHAR_USERDEF:
4006 return cp_parser_userdef_char_literal (parser);
4012 case CPP_UTF8STRING:
4013 case CPP_STRING_USERDEF:
4014 case CPP_STRING16_USERDEF:
4015 case CPP_STRING32_USERDEF:
4016 case CPP_WSTRING_USERDEF:
4017 case CPP_UTF8STRING_USERDEF:
4018 /* ??? Should wide strings be allowed when parser->translate_strings_p
4019 is false (i.e. in attributes)? If not, we can kill the third
4020 argument to cp_parser_string_literal. */
4021 return cp_parser_string_literal (parser,
4022 parser->translate_strings_p,
4025 case CPP_OPEN_PAREN:
4028 bool saved_greater_than_is_operator_p;
4030 /* Consume the `('. */
4031 cp_lexer_consume_token (parser->lexer);
4032 /* Within a parenthesized expression, a `>' token is always
4033 the greater-than operator. */
4034 saved_greater_than_is_operator_p
4035 = parser->greater_than_is_operator_p;
4036 parser->greater_than_is_operator_p = true;
4037 /* If we see `( { ' then we are looking at the beginning of
4038 a GNU statement-expression. */
4039 if (cp_parser_allow_gnu_extensions_p (parser)
4040 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4042 /* Statement-expressions are not allowed by the standard. */
4043 pedwarn (token->location, OPT_Wpedantic,
4044 "ISO C++ forbids braced-groups within expressions");
4046 /* And they're not allowed outside of a function-body; you
4047 cannot, for example, write:
4049 int i = ({ int j = 3; j + 1; });
4051 at class or namespace scope. */
4052 if (!parser->in_function_body
4053 || parser->in_template_argument_list_p)
4055 error_at (token->location,
4056 "statement-expressions are not allowed outside "
4057 "functions nor in template-argument lists");
4058 cp_parser_skip_to_end_of_block_or_statement (parser);
4059 expr = error_mark_node;
4063 /* Start the statement-expression. */
4064 expr = begin_stmt_expr ();
4065 /* Parse the compound-statement. */
4066 cp_parser_compound_statement (parser, expr, false, false);
4068 expr = finish_stmt_expr (expr, false);
4073 /* Parse the parenthesized expression. */
4074 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4075 /* Let the front end know that this expression was
4076 enclosed in parentheses. This matters in case, for
4077 example, the expression is of the form `A::B', since
4078 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4080 finish_parenthesized_expr (expr);
4081 /* DR 705: Wrapping an unqualified name in parentheses
4082 suppresses arg-dependent lookup. We want to pass back
4083 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4084 (c++/37862), but none of the others. */
4085 if (*idk != CP_ID_KIND_QUALIFIED)
4086 *idk = CP_ID_KIND_NONE;
4088 /* The `>' token might be the end of a template-id or
4089 template-parameter-list now. */
4090 parser->greater_than_is_operator_p
4091 = saved_greater_than_is_operator_p;
4092 /* Consume the `)'. */
4093 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4094 cp_parser_skip_to_end_of_statement (parser);
4099 case CPP_OPEN_SQUARE:
4100 if (c_dialect_objc ())
4101 /* We have an Objective-C++ message. */
4102 return cp_parser_objc_expression (parser);
4104 tree lam = cp_parser_lambda_expression (parser);
4105 /* Don't warn about a failed tentative parse. */
4106 if (cp_parser_error_occurred (parser))
4107 return error_mark_node;
4108 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4112 case CPP_OBJC_STRING:
4113 if (c_dialect_objc ())
4114 /* We have an Objective-C++ string literal. */
4115 return cp_parser_objc_expression (parser);
4116 cp_parser_error (parser, "expected primary-expression");
4117 return error_mark_node;
4120 switch (token->keyword)
4122 /* These two are the boolean literals. */
4124 cp_lexer_consume_token (parser->lexer);
4125 return boolean_true_node;
4127 cp_lexer_consume_token (parser->lexer);
4128 return boolean_false_node;
4130 /* The `__null' literal. */
4132 cp_lexer_consume_token (parser->lexer);
4135 /* The `nullptr' literal. */
4137 cp_lexer_consume_token (parser->lexer);
4138 return nullptr_node;
4140 /* Recognize the `this' keyword. */
4142 cp_lexer_consume_token (parser->lexer);
4143 if (parser->local_variables_forbidden_p)
4145 error_at (token->location,
4146 "%<this%> may not be used in this context");
4147 return error_mark_node;
4149 /* Pointers cannot appear in constant-expressions. */
4150 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4151 return error_mark_node;
4152 return finish_this_expr ();
4154 /* The `operator' keyword can be the beginning of an
4159 case RID_FUNCTION_NAME:
4160 case RID_PRETTY_FUNCTION_NAME:
4161 case RID_C99_FUNCTION_NAME:
4163 non_integral_constant name;
4165 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4166 __func__ are the names of variables -- but they are
4167 treated specially. Therefore, they are handled here,
4168 rather than relying on the generic id-expression logic
4169 below. Grammatically, these names are id-expressions.
4171 Consume the token. */
4172 token = cp_lexer_consume_token (parser->lexer);
4174 switch (token->keyword)
4176 case RID_FUNCTION_NAME:
4177 name = NIC_FUNC_NAME;
4179 case RID_PRETTY_FUNCTION_NAME:
4180 name = NIC_PRETTY_FUNC;
4182 case RID_C99_FUNCTION_NAME:
4183 name = NIC_C99_FUNC;
4189 if (cp_parser_non_integral_constant_expression (parser, name))
4190 return error_mark_node;
4192 /* Look up the name. */
4193 return finish_fname (token->u.value);
4200 source_location type_location;
4202 /* The `__builtin_va_arg' construct is used to handle
4203 `va_arg'. Consume the `__builtin_va_arg' token. */
4204 cp_lexer_consume_token (parser->lexer);
4205 /* Look for the opening `('. */
4206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4207 /* Now, parse the assignment-expression. */
4208 expression = cp_parser_assignment_expression (parser,
4209 /*cast_p=*/false, NULL);
4210 /* Look for the `,'. */
4211 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4212 type_location = cp_lexer_peek_token (parser->lexer)->location;
4213 /* Parse the type-id. */
4214 type = cp_parser_type_id (parser);
4215 /* Look for the closing `)'. */
4216 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4217 /* Using `va_arg' in a constant-expression is not
4219 if (cp_parser_non_integral_constant_expression (parser,
4221 return error_mark_node;
4222 return build_x_va_arg (type_location, expression, type);
4226 return cp_parser_builtin_offsetof (parser);
4228 case RID_HAS_NOTHROW_ASSIGN:
4229 case RID_HAS_NOTHROW_CONSTRUCTOR:
4230 case RID_HAS_NOTHROW_COPY:
4231 case RID_HAS_TRIVIAL_ASSIGN:
4232 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4233 case RID_HAS_TRIVIAL_COPY:
4234 case RID_HAS_TRIVIAL_DESTRUCTOR:
4235 case RID_HAS_VIRTUAL_DESTRUCTOR:
4236 case RID_IS_ABSTRACT:
4237 case RID_IS_BASE_OF:
4239 case RID_IS_CONVERTIBLE_TO:
4243 case RID_IS_LITERAL_TYPE:
4245 case RID_IS_POLYMORPHIC:
4246 case RID_IS_STD_LAYOUT:
4247 case RID_IS_TRIVIAL:
4249 return cp_parser_trait_expr (parser, token->keyword);
4251 /* Objective-C++ expressions. */
4253 case RID_AT_PROTOCOL:
4254 case RID_AT_SELECTOR:
4255 return cp_parser_objc_expression (parser);
4258 if (parser->in_function_body
4259 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4262 error_at (token->location,
4263 "a template declaration cannot appear at block scope");
4264 cp_parser_skip_to_end_of_block_or_statement (parser);
4265 return error_mark_node;
4268 cp_parser_error (parser, "expected primary-expression");
4269 return error_mark_node;
4272 /* An id-expression can start with either an identifier, a
4273 `::' as the beginning of a qualified-id, or the "operator"
4277 case CPP_TEMPLATE_ID:
4278 case CPP_NESTED_NAME_SPECIFIER:
4282 const char *error_msg;
4285 cp_token *id_expr_token;
4288 /* Parse the id-expression. */
4290 = cp_parser_id_expression (parser,
4291 /*template_keyword_p=*/false,
4292 /*check_dependency_p=*/true,
4294 /*declarator_p=*/false,
4295 /*optional_p=*/false);
4296 if (id_expression == error_mark_node)
4297 return error_mark_node;
4298 id_expr_token = token;
4299 token = cp_lexer_peek_token (parser->lexer);
4300 done = (token->type != CPP_OPEN_SQUARE
4301 && token->type != CPP_OPEN_PAREN
4302 && token->type != CPP_DOT
4303 && token->type != CPP_DEREF
4304 && token->type != CPP_PLUS_PLUS
4305 && token->type != CPP_MINUS_MINUS);
4306 /* If we have a template-id, then no further lookup is
4307 required. If the template-id was for a template-class, we
4308 will sometimes have a TYPE_DECL at this point. */
4309 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4310 || TREE_CODE (id_expression) == TYPE_DECL)
4311 decl = id_expression;
4312 /* Look up the name. */
4315 tree ambiguous_decls;
4317 /* If we already know that this lookup is ambiguous, then
4318 we've already issued an error message; there's no reason
4320 if (id_expr_token->type == CPP_NAME
4321 && id_expr_token->ambiguous_p)
4323 cp_parser_simulate_error (parser);
4324 return error_mark_node;
4327 decl = cp_parser_lookup_name (parser, id_expression,
4330 /*is_namespace=*/false,
4331 /*check_dependency=*/true,
4333 id_expr_token->location);
4334 /* If the lookup was ambiguous, an error will already have
4336 if (ambiguous_decls)
4337 return error_mark_node;
4339 /* In Objective-C++, we may have an Objective-C 2.0
4340 dot-syntax for classes here. */
4341 if (c_dialect_objc ()
4342 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4343 && TREE_CODE (decl) == TYPE_DECL
4344 && objc_is_class_name (decl))
4347 cp_lexer_consume_token (parser->lexer);
4348 component = cp_parser_identifier (parser);
4349 if (component == error_mark_node)
4350 return error_mark_node;
4352 return objc_build_class_component_ref (id_expression, component);
4355 /* In Objective-C++, an instance variable (ivar) may be preferred
4356 to whatever cp_parser_lookup_name() found. */
4357 decl = objc_lookup_ivar (decl, id_expression);
4359 /* If name lookup gives us a SCOPE_REF, then the
4360 qualifying scope was dependent. */
4361 if (TREE_CODE (decl) == SCOPE_REF)
4363 /* At this point, we do not know if DECL is a valid
4364 integral constant expression. We assume that it is
4365 in fact such an expression, so that code like:
4367 template <int N> struct A {
4371 is accepted. At template-instantiation time, we
4372 will check that B<N>::i is actually a constant. */
4375 /* Check to see if DECL is a local variable in a context
4376 where that is forbidden. */
4377 if (parser->local_variables_forbidden_p
4378 && local_variable_p (decl))
4380 /* It might be that we only found DECL because we are
4381 trying to be generous with pre-ISO scoping rules.
4382 For example, consider:
4386 for (int i = 0; i < 10; ++i) {}
4387 extern void f(int j = i);
4390 Here, name look up will originally find the out
4391 of scope `i'. We need to issue a warning message,
4392 but then use the global `i'. */
4393 decl = check_for_out_of_scope_variable (decl);
4394 if (local_variable_p (decl))
4396 error_at (id_expr_token->location,
4397 "local variable %qD may not appear in this context",
4399 return error_mark_node;
4404 decl = (finish_id_expression
4405 (id_expression, decl, parser->scope,
4407 parser->integral_constant_expression_p,
4408 parser->allow_non_integral_constant_expression_p,
4409 &parser->non_integral_constant_expression_p,
4410 template_p, done, address_p,
4413 id_expr_token->location));
4415 cp_parser_error (parser, error_msg);
4419 /* Anything else is an error. */
4421 cp_parser_error (parser, "expected primary-expression");
4422 return error_mark_node;
4427 cp_parser_primary_expression (cp_parser *parser,
4430 bool template_arg_p,
4433 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4434 /*decltype*/false, idk);
4437 /* Parse an id-expression.
4444 :: [opt] nested-name-specifier template [opt] unqualified-id
4446 :: operator-function-id
4449 Return a representation of the unqualified portion of the
4450 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4451 a `::' or nested-name-specifier.
4453 Often, if the id-expression was a qualified-id, the caller will
4454 want to make a SCOPE_REF to represent the qualified-id. This
4455 function does not do this in order to avoid wastefully creating
4456 SCOPE_REFs when they are not required.
4458 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4461 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4462 uninstantiated templates.
4464 If *TEMPLATE_P is non-NULL, it is set to true iff the
4465 `template' keyword is used to explicitly indicate that the entity
4466 named is a template.
4468 If DECLARATOR_P is true, the id-expression is appearing as part of
4469 a declarator, rather than as part of an expression. */
4472 cp_parser_id_expression (cp_parser *parser,
4473 bool template_keyword_p,
4474 bool check_dependency_p,
4479 bool global_scope_p;
4480 bool nested_name_specifier_p;
4482 /* Assume the `template' keyword was not used. */
4484 *template_p = template_keyword_p;
4486 /* Look for the optional `::' operator. */
4488 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4490 /* Look for the optional nested-name-specifier. */
4491 nested_name_specifier_p
4492 = (cp_parser_nested_name_specifier_opt (parser,
4493 /*typename_keyword_p=*/false,
4498 /* If there is a nested-name-specifier, then we are looking at
4499 the first qualified-id production. */
4500 if (nested_name_specifier_p)
4503 tree saved_object_scope;
4504 tree saved_qualifying_scope;
4505 tree unqualified_id;
4508 /* See if the next token is the `template' keyword. */
4510 template_p = &is_template;
4511 *template_p = cp_parser_optional_template_keyword (parser);
4512 /* Name lookup we do during the processing of the
4513 unqualified-id might obliterate SCOPE. */
4514 saved_scope = parser->scope;
4515 saved_object_scope = parser->object_scope;
4516 saved_qualifying_scope = parser->qualifying_scope;
4517 /* Process the final unqualified-id. */
4518 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4521 /*optional_p=*/false);
4522 /* Restore the SAVED_SCOPE for our caller. */
4523 parser->scope = saved_scope;
4524 parser->object_scope = saved_object_scope;
4525 parser->qualifying_scope = saved_qualifying_scope;
4527 return unqualified_id;
4529 /* Otherwise, if we are in global scope, then we are looking at one
4530 of the other qualified-id productions. */
4531 else if (global_scope_p)
4536 /* Peek at the next token. */
4537 token = cp_lexer_peek_token (parser->lexer);
4539 /* If it's an identifier, and the next token is not a "<", then
4540 we can avoid the template-id case. This is an optimization
4541 for this common case. */
4542 if (token->type == CPP_NAME
4543 && !cp_parser_nth_token_starts_template_argument_list_p
4545 return cp_parser_identifier (parser);
4547 cp_parser_parse_tentatively (parser);
4548 /* Try a template-id. */
4549 id = cp_parser_template_id (parser,
4550 /*template_keyword_p=*/false,
4551 /*check_dependency_p=*/true,
4554 /* If that worked, we're done. */
4555 if (cp_parser_parse_definitely (parser))
4558 /* Peek at the next token. (Changes in the token buffer may
4559 have invalidated the pointer obtained above.) */
4560 token = cp_lexer_peek_token (parser->lexer);
4562 switch (token->type)
4565 return cp_parser_identifier (parser);
4568 if (token->keyword == RID_OPERATOR)
4569 return cp_parser_operator_function_id (parser);
4573 cp_parser_error (parser, "expected id-expression");
4574 return error_mark_node;
4578 return cp_parser_unqualified_id (parser, template_keyword_p,
4579 /*check_dependency_p=*/true,
4584 /* Parse an unqualified-id.
4588 operator-function-id
4589 conversion-function-id
4593 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4594 keyword, in a construct like `A::template ...'.
4596 Returns a representation of unqualified-id. For the `identifier'
4597 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4598 production a BIT_NOT_EXPR is returned; the operand of the
4599 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4600 other productions, see the documentation accompanying the
4601 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4602 names are looked up in uninstantiated templates. If DECLARATOR_P
4603 is true, the unqualified-id is appearing as part of a declarator,
4604 rather than as part of an expression. */
4607 cp_parser_unqualified_id (cp_parser* parser,
4608 bool template_keyword_p,
4609 bool check_dependency_p,
4615 /* Peek at the next token. */
4616 token = cp_lexer_peek_token (parser->lexer);
4618 switch (token->type)
4624 /* We don't know yet whether or not this will be a
4626 cp_parser_parse_tentatively (parser);
4627 /* Try a template-id. */
4628 id = cp_parser_template_id (parser, template_keyword_p,
4632 /* If it worked, we're done. */
4633 if (cp_parser_parse_definitely (parser))
4635 /* Otherwise, it's an ordinary identifier. */
4636 return cp_parser_identifier (parser);
4639 case CPP_TEMPLATE_ID:
4640 return cp_parser_template_id (parser, template_keyword_p,
4648 tree qualifying_scope;
4653 /* Consume the `~' token. */
4654 cp_lexer_consume_token (parser->lexer);
4655 /* Parse the class-name. The standard, as written, seems to
4658 template <typename T> struct S { ~S (); };
4659 template <typename T> S<T>::~S() {}
4661 is invalid, since `~' must be followed by a class-name, but
4662 `S<T>' is dependent, and so not known to be a class.
4663 That's not right; we need to look in uninstantiated
4664 templates. A further complication arises from:
4666 template <typename T> void f(T t) {
4670 Here, it is not possible to look up `T' in the scope of `T'
4671 itself. We must look in both the current scope, and the
4672 scope of the containing complete expression.
4674 Yet another issue is:
4683 The standard does not seem to say that the `S' in `~S'
4684 should refer to the type `S' and not the data member
4687 /* DR 244 says that we look up the name after the "~" in the
4688 same scope as we looked up the qualifying name. That idea
4689 isn't fully worked out; it's more complicated than that. */
4690 scope = parser->scope;
4691 object_scope = parser->object_scope;
4692 qualifying_scope = parser->qualifying_scope;
4694 /* Check for invalid scopes. */
4695 if (scope == error_mark_node)
4697 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4698 cp_lexer_consume_token (parser->lexer);
4699 return error_mark_node;
4701 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4703 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4704 error_at (token->location,
4705 "scope %qT before %<~%> is not a class-name",
4707 cp_parser_simulate_error (parser);
4708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4709 cp_lexer_consume_token (parser->lexer);
4710 return error_mark_node;
4712 gcc_assert (!scope || TYPE_P (scope));
4714 /* If the name is of the form "X::~X" it's OK even if X is a
4716 token = cp_lexer_peek_token (parser->lexer);
4718 && token->type == CPP_NAME
4719 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4721 && (token->u.value == TYPE_IDENTIFIER (scope)
4722 || (CLASS_TYPE_P (scope)
4723 && constructor_name_p (token->u.value, scope))))
4725 cp_lexer_consume_token (parser->lexer);
4726 return build_nt (BIT_NOT_EXPR, scope);
4729 /* If there was an explicit qualification (S::~T), first look
4730 in the scope given by the qualification (i.e., S).
4732 Note: in the calls to cp_parser_class_name below we pass
4733 typename_type so that lookup finds the injected-class-name
4734 rather than the constructor. */
4736 type_decl = NULL_TREE;
4739 cp_parser_parse_tentatively (parser);
4740 type_decl = cp_parser_class_name (parser,
4741 /*typename_keyword_p=*/false,
4742 /*template_keyword_p=*/false,
4744 /*check_dependency=*/false,
4745 /*class_head_p=*/false,
4747 if (cp_parser_parse_definitely (parser))
4750 /* In "N::S::~S", look in "N" as well. */
4751 if (!done && scope && qualifying_scope)
4753 cp_parser_parse_tentatively (parser);
4754 parser->scope = qualifying_scope;
4755 parser->object_scope = NULL_TREE;
4756 parser->qualifying_scope = NULL_TREE;
4758 = cp_parser_class_name (parser,
4759 /*typename_keyword_p=*/false,
4760 /*template_keyword_p=*/false,
4762 /*check_dependency=*/false,
4763 /*class_head_p=*/false,
4765 if (cp_parser_parse_definitely (parser))
4768 /* In "p->S::~T", look in the scope given by "*p" as well. */
4769 else if (!done && object_scope)
4771 cp_parser_parse_tentatively (parser);
4772 parser->scope = object_scope;
4773 parser->object_scope = NULL_TREE;
4774 parser->qualifying_scope = NULL_TREE;
4776 = cp_parser_class_name (parser,
4777 /*typename_keyword_p=*/false,
4778 /*template_keyword_p=*/false,
4780 /*check_dependency=*/false,
4781 /*class_head_p=*/false,
4783 if (cp_parser_parse_definitely (parser))
4786 /* Look in the surrounding context. */
4789 parser->scope = NULL_TREE;
4790 parser->object_scope = NULL_TREE;
4791 parser->qualifying_scope = NULL_TREE;
4792 if (processing_template_decl)
4793 cp_parser_parse_tentatively (parser);
4795 = cp_parser_class_name (parser,
4796 /*typename_keyword_p=*/false,
4797 /*template_keyword_p=*/false,
4799 /*check_dependency=*/false,
4800 /*class_head_p=*/false,
4802 if (processing_template_decl
4803 && ! cp_parser_parse_definitely (parser))
4805 /* We couldn't find a type with this name, so just accept
4806 it and check for a match at instantiation time. */
4807 type_decl = cp_parser_identifier (parser);
4808 if (type_decl != error_mark_node)
4809 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4813 /* If an error occurred, assume that the name of the
4814 destructor is the same as the name of the qualifying
4815 class. That allows us to keep parsing after running
4816 into ill-formed destructor names. */
4817 if (type_decl == error_mark_node && scope)
4818 return build_nt (BIT_NOT_EXPR, scope);
4819 else if (type_decl == error_mark_node)
4820 return error_mark_node;
4822 /* Check that destructor name and scope match. */
4823 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4825 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4826 error_at (token->location,
4827 "declaration of %<~%T%> as member of %qT",
4829 cp_parser_simulate_error (parser);
4830 return error_mark_node;
4835 A typedef-name that names a class shall not be used as the
4836 identifier in the declarator for a destructor declaration. */
4838 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4839 && !DECL_SELF_REFERENCE_P (type_decl)
4840 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4841 error_at (token->location,
4842 "typedef-name %qD used as destructor declarator",
4845 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4849 if (token->keyword == RID_OPERATOR)
4853 /* This could be a template-id, so we try that first. */
4854 cp_parser_parse_tentatively (parser);
4855 /* Try a template-id. */
4856 id = cp_parser_template_id (parser, template_keyword_p,
4857 /*check_dependency_p=*/true,
4860 /* If that worked, we're done. */
4861 if (cp_parser_parse_definitely (parser))
4863 /* We still don't know whether we're looking at an
4864 operator-function-id or a conversion-function-id. */
4865 cp_parser_parse_tentatively (parser);
4866 /* Try an operator-function-id. */
4867 id = cp_parser_operator_function_id (parser);
4868 /* If that didn't work, try a conversion-function-id. */
4869 if (!cp_parser_parse_definitely (parser))
4870 id = cp_parser_conversion_function_id (parser);
4871 else if (UDLIT_OPER_P (id))
4874 const char *name = UDLIT_OP_SUFFIX (id);
4875 if (name[0] != '_' && !in_system_header)
4876 warning (0, "literal operator suffixes not preceded by %<_%>"
4877 " are reserved for future standardization");
4887 cp_parser_error (parser, "expected unqualified-id");
4888 return error_mark_node;
4892 /* Parse an (optional) nested-name-specifier.
4894 nested-name-specifier: [C++98]
4895 class-or-namespace-name :: nested-name-specifier [opt]
4896 class-or-namespace-name :: template nested-name-specifier [opt]
4898 nested-name-specifier: [C++0x]
4901 nested-name-specifier identifier ::
4902 nested-name-specifier template [opt] simple-template-id ::
4904 PARSER->SCOPE should be set appropriately before this function is
4905 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4906 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4909 Sets PARSER->SCOPE to the class (TYPE) or namespace
4910 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4911 it unchanged if there is no nested-name-specifier. Returns the new
4912 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4914 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4915 part of a declaration and/or decl-specifier. */
4918 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4919 bool typename_keyword_p,
4920 bool check_dependency_p,
4922 bool is_declaration)
4924 bool success = false;
4925 cp_token_position start = 0;
4928 /* Remember where the nested-name-specifier starts. */
4929 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4931 start = cp_lexer_token_position (parser->lexer, false);
4932 push_deferring_access_checks (dk_deferred);
4939 tree saved_qualifying_scope;
4940 bool template_keyword_p;
4942 /* Spot cases that cannot be the beginning of a
4943 nested-name-specifier. */
4944 token = cp_lexer_peek_token (parser->lexer);
4946 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4947 the already parsed nested-name-specifier. */
4948 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4950 /* Grab the nested-name-specifier and continue the loop. */
4951 cp_parser_pre_parsed_nested_name_specifier (parser);
4952 /* If we originally encountered this nested-name-specifier
4953 with IS_DECLARATION set to false, we will not have
4954 resolved TYPENAME_TYPEs, so we must do so here. */
4956 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4958 new_scope = resolve_typename_type (parser->scope,
4959 /*only_current_p=*/false);
4960 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4961 parser->scope = new_scope;
4967 /* Spot cases that cannot be the beginning of a
4968 nested-name-specifier. On the second and subsequent times
4969 through the loop, we look for the `template' keyword. */
4970 if (success && token->keyword == RID_TEMPLATE)
4972 /* A template-id can start a nested-name-specifier. */
4973 else if (token->type == CPP_TEMPLATE_ID)
4975 /* DR 743: decltype can be used in a nested-name-specifier. */
4976 else if (token_is_decltype (token))
4980 /* If the next token is not an identifier, then it is
4981 definitely not a type-name or namespace-name. */
4982 if (token->type != CPP_NAME)
4984 /* If the following token is neither a `<' (to begin a
4985 template-id), nor a `::', then we are not looking at a
4986 nested-name-specifier. */
4987 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4989 if (token->type == CPP_COLON
4990 && parser->colon_corrects_to_scope_p
4991 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4993 error_at (token->location,
4994 "found %<:%> in nested-name-specifier, expected %<::%>");
4995 token->type = CPP_SCOPE;
4998 if (token->type != CPP_SCOPE
4999 && !cp_parser_nth_token_starts_template_argument_list_p
5004 /* The nested-name-specifier is optional, so we parse
5006 cp_parser_parse_tentatively (parser);
5008 /* Look for the optional `template' keyword, if this isn't the
5009 first time through the loop. */
5011 template_keyword_p = cp_parser_optional_template_keyword (parser);
5013 template_keyword_p = false;
5015 /* Save the old scope since the name lookup we are about to do
5016 might destroy it. */
5017 old_scope = parser->scope;
5018 saved_qualifying_scope = parser->qualifying_scope;
5019 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5020 look up names in "X<T>::I" in order to determine that "Y" is
5021 a template. So, if we have a typename at this point, we make
5022 an effort to look through it. */
5024 && !typename_keyword_p
5026 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5027 parser->scope = resolve_typename_type (parser->scope,
5028 /*only_current_p=*/false);
5029 /* Parse the qualifying entity. */
5031 = cp_parser_qualifying_entity (parser,
5037 /* Look for the `::' token. */
5038 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5040 /* If we found what we wanted, we keep going; otherwise, we're
5042 if (!cp_parser_parse_definitely (parser))
5044 bool error_p = false;
5046 /* Restore the OLD_SCOPE since it was valid before the
5047 failed attempt at finding the last
5048 class-or-namespace-name. */
5049 parser->scope = old_scope;
5050 parser->qualifying_scope = saved_qualifying_scope;
5052 /* If the next token is a decltype, and the one after that is a
5053 `::', then the decltype has failed to resolve to a class or
5054 enumeration type. Give this error even when parsing
5055 tentatively since it can't possibly be valid--and we're going
5056 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5057 won't get another chance.*/
5058 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5059 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5062 token = cp_lexer_consume_token (parser->lexer);
5063 error_at (token->location, "decltype evaluates to %qT, "
5064 "which is not a class or enumeration type",
5066 parser->scope = error_mark_node;
5070 cp_lexer_consume_token (parser->lexer);
5073 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5075 /* If the next token is an identifier, and the one after
5076 that is a `::', then any valid interpretation would have
5077 found a class-or-namespace-name. */
5078 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5079 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5081 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5084 token = cp_lexer_consume_token (parser->lexer);
5087 if (!token->ambiguous_p)
5090 tree ambiguous_decls;
5092 decl = cp_parser_lookup_name (parser, token->u.value,
5094 /*is_template=*/false,
5095 /*is_namespace=*/false,
5096 /*check_dependency=*/true,
5099 if (TREE_CODE (decl) == TEMPLATE_DECL)
5100 error_at (token->location,
5101 "%qD used without template parameters",
5103 else if (ambiguous_decls)
5105 error_at (token->location,
5106 "reference to %qD is ambiguous",
5108 print_candidates (ambiguous_decls);
5109 decl = error_mark_node;
5113 if (cxx_dialect != cxx98)
5114 cp_parser_name_lookup_error
5115 (parser, token->u.value, decl, NLE_NOT_CXX98,
5118 cp_parser_name_lookup_error
5119 (parser, token->u.value, decl, NLE_CXX98,
5123 parser->scope = error_mark_node;
5125 /* Treat this as a successful nested-name-specifier
5130 If the name found is not a class-name (clause
5131 _class_) or namespace-name (_namespace.def_), the
5132 program is ill-formed. */
5135 cp_lexer_consume_token (parser->lexer);
5139 /* We've found one valid nested-name-specifier. */
5141 /* Name lookup always gives us a DECL. */
5142 if (TREE_CODE (new_scope) == TYPE_DECL)
5143 new_scope = TREE_TYPE (new_scope);
5144 /* Uses of "template" must be followed by actual templates. */
5145 if (template_keyword_p
5146 && !(CLASS_TYPE_P (new_scope)
5147 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5148 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5149 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5150 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5151 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5152 == TEMPLATE_ID_EXPR)))
5153 permerror (input_location, TYPE_P (new_scope)
5154 ? G_("%qT is not a template")
5155 : G_("%qD is not a template"),
5157 /* If it is a class scope, try to complete it; we are about to
5158 be looking up names inside the class. */
5159 if (TYPE_P (new_scope)
5160 /* Since checking types for dependency can be expensive,
5161 avoid doing it if the type is already complete. */
5162 && !COMPLETE_TYPE_P (new_scope)
5163 /* Do not try to complete dependent types. */
5164 && !dependent_type_p (new_scope))
5166 new_scope = complete_type (new_scope);
5167 /* If it is a typedef to current class, use the current
5168 class instead, as the typedef won't have any names inside
5170 if (!COMPLETE_TYPE_P (new_scope)
5171 && currently_open_class (new_scope))
5172 new_scope = TYPE_MAIN_VARIANT (new_scope);
5174 /* Make sure we look in the right scope the next time through
5176 parser->scope = new_scope;
5179 /* If parsing tentatively, replace the sequence of tokens that makes
5180 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5181 token. That way, should we re-parse the token stream, we will
5182 not have to repeat the effort required to do the parse, nor will
5183 we issue duplicate error messages. */
5184 if (success && start)
5188 token = cp_lexer_token_at (parser->lexer, start);
5189 /* Reset the contents of the START token. */
5190 token->type = CPP_NESTED_NAME_SPECIFIER;
5191 /* Retrieve any deferred checks. Do not pop this access checks yet
5192 so the memory will not be reclaimed during token replacing below. */
5193 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5194 token->u.tree_check_value->value = parser->scope;
5195 token->u.tree_check_value->checks = get_deferred_access_checks ();
5196 token->u.tree_check_value->qualifying_scope =
5197 parser->qualifying_scope;
5198 token->keyword = RID_MAX;
5200 /* Purge all subsequent tokens. */
5201 cp_lexer_purge_tokens_after (parser->lexer, start);
5205 pop_to_parent_deferring_access_checks ();
5207 return success ? parser->scope : NULL_TREE;
5210 /* Parse a nested-name-specifier. See
5211 cp_parser_nested_name_specifier_opt for details. This function
5212 behaves identically, except that it will an issue an error if no
5213 nested-name-specifier is present. */
5216 cp_parser_nested_name_specifier (cp_parser *parser,
5217 bool typename_keyword_p,
5218 bool check_dependency_p,
5220 bool is_declaration)
5224 /* Look for the nested-name-specifier. */
5225 scope = cp_parser_nested_name_specifier_opt (parser,
5230 /* If it was not present, issue an error message. */
5233 cp_parser_error (parser, "expected nested-name-specifier");
5234 parser->scope = NULL_TREE;
5240 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5241 this is either a class-name or a namespace-name (which corresponds
5242 to the class-or-namespace-name production in the grammar). For
5243 C++0x, it can also be a type-name that refers to an enumeration
5244 type or a simple-template-id.
5246 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5247 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5248 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5249 TYPE_P is TRUE iff the next name should be taken as a class-name,
5250 even the same name is declared to be another entity in the same
5253 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5254 specified by the class-or-namespace-name. If neither is found the
5255 ERROR_MARK_NODE is returned. */
5258 cp_parser_qualifying_entity (cp_parser *parser,
5259 bool typename_keyword_p,
5260 bool template_keyword_p,
5261 bool check_dependency_p,
5263 bool is_declaration)
5266 tree saved_qualifying_scope;
5267 tree saved_object_scope;
5270 bool successful_parse_p;
5272 /* DR 743: decltype can appear in a nested-name-specifier. */
5273 if (cp_lexer_next_token_is_decltype (parser->lexer))
5275 scope = cp_parser_decltype (parser);
5276 if (TREE_CODE (scope) != ENUMERAL_TYPE
5277 && !MAYBE_CLASS_TYPE_P (scope))
5279 cp_parser_simulate_error (parser);
5280 return error_mark_node;
5282 if (TYPE_NAME (scope))
5283 scope = TYPE_NAME (scope);
5287 /* Before we try to parse the class-name, we must save away the
5288 current PARSER->SCOPE since cp_parser_class_name will destroy
5290 saved_scope = parser->scope;
5291 saved_qualifying_scope = parser->qualifying_scope;
5292 saved_object_scope = parser->object_scope;
5293 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5294 there is no need to look for a namespace-name. */
5295 only_class_p = template_keyword_p
5296 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5298 cp_parser_parse_tentatively (parser);
5299 scope = cp_parser_class_name (parser,
5302 type_p ? class_type : none_type,
5304 /*class_head_p=*/false,
5306 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5307 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5309 && cxx_dialect != cxx98
5310 && !successful_parse_p)
5312 /* Restore the saved scope. */
5313 parser->scope = saved_scope;
5314 parser->qualifying_scope = saved_qualifying_scope;
5315 parser->object_scope = saved_object_scope;
5317 /* Parse tentatively. */
5318 cp_parser_parse_tentatively (parser);
5320 /* Parse a type-name */
5321 scope = cp_parser_type_name (parser);
5323 /* "If the name found does not designate a namespace or a class,
5324 enumeration, or dependent type, the program is ill-formed."
5326 We cover classes and dependent types above and namespaces below,
5327 so this code is only looking for enums. */
5328 if (!scope || TREE_CODE (scope) != TYPE_DECL
5329 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5330 cp_parser_simulate_error (parser);
5332 successful_parse_p = cp_parser_parse_definitely (parser);
5334 /* If that didn't work, try for a namespace-name. */
5335 if (!only_class_p && !successful_parse_p)
5337 /* Restore the saved scope. */
5338 parser->scope = saved_scope;
5339 parser->qualifying_scope = saved_qualifying_scope;
5340 parser->object_scope = saved_object_scope;
5341 /* If we are not looking at an identifier followed by the scope
5342 resolution operator, then this is not part of a
5343 nested-name-specifier. (Note that this function is only used
5344 to parse the components of a nested-name-specifier.) */
5345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5346 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5347 return error_mark_node;
5348 scope = cp_parser_namespace_name (parser);
5354 /* Parse a postfix-expression.
5358 postfix-expression [ expression ]
5359 postfix-expression ( expression-list [opt] )
5360 simple-type-specifier ( expression-list [opt] )
5361 typename :: [opt] nested-name-specifier identifier
5362 ( expression-list [opt] )
5363 typename :: [opt] nested-name-specifier template [opt] template-id
5364 ( expression-list [opt] )
5365 postfix-expression . template [opt] id-expression
5366 postfix-expression -> template [opt] id-expression
5367 postfix-expression . pseudo-destructor-name
5368 postfix-expression -> pseudo-destructor-name
5369 postfix-expression ++
5370 postfix-expression --
5371 dynamic_cast < type-id > ( expression )
5372 static_cast < type-id > ( expression )
5373 reinterpret_cast < type-id > ( expression )
5374 const_cast < type-id > ( expression )
5375 typeid ( expression )
5381 ( type-id ) { initializer-list , [opt] }
5383 This extension is a GNU version of the C99 compound-literal
5384 construct. (The C99 grammar uses `type-name' instead of `type-id',
5385 but they are essentially the same concept.)
5387 If ADDRESS_P is true, the postfix expression is the operand of the
5388 `&' operator. CAST_P is true if this expression is the target of a
5391 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5392 class member access expressions [expr.ref].
5394 Returns a representation of the expression. */
5397 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5398 bool member_access_only_p, bool decltype_p,
5399 cp_id_kind * pidk_return)
5403 cp_id_kind idk = CP_ID_KIND_NONE;
5404 tree postfix_expression = NULL_TREE;
5405 bool is_member_access = false;
5407 /* Peek at the next token. */
5408 token = cp_lexer_peek_token (parser->lexer);
5409 /* Some of the productions are determined by keywords. */
5410 keyword = token->keyword;
5420 const char *saved_message;
5422 /* All of these can be handled in the same way from the point
5423 of view of parsing. Begin by consuming the token
5424 identifying the cast. */
5425 cp_lexer_consume_token (parser->lexer);
5427 /* New types cannot be defined in the cast. */
5428 saved_message = parser->type_definition_forbidden_message;
5429 parser->type_definition_forbidden_message
5430 = G_("types may not be defined in casts");
5432 /* Look for the opening `<'. */
5433 cp_parser_require (parser, CPP_LESS, RT_LESS);
5434 /* Parse the type to which we are casting. */
5435 type = cp_parser_type_id (parser);
5436 /* Look for the closing `>'. */
5437 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5438 /* Restore the old message. */
5439 parser->type_definition_forbidden_message = saved_message;
5441 bool saved_greater_than_is_operator_p
5442 = parser->greater_than_is_operator_p;
5443 parser->greater_than_is_operator_p = true;
5445 /* And the expression which is being cast. */
5446 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5447 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5448 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5450 parser->greater_than_is_operator_p
5451 = saved_greater_than_is_operator_p;
5453 /* Only type conversions to integral or enumeration types
5454 can be used in constant-expressions. */
5455 if (!cast_valid_in_integral_constant_expression_p (type)
5456 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5457 return error_mark_node;
5463 = build_dynamic_cast (type, expression, tf_warning_or_error);
5467 = build_static_cast (type, expression, tf_warning_or_error);
5471 = build_reinterpret_cast (type, expression,
5472 tf_warning_or_error);
5476 = build_const_cast (type, expression, tf_warning_or_error);
5487 const char *saved_message;
5488 bool saved_in_type_id_in_expr_p;
5490 /* Consume the `typeid' token. */
5491 cp_lexer_consume_token (parser->lexer);
5492 /* Look for the `(' token. */
5493 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5494 /* Types cannot be defined in a `typeid' expression. */
5495 saved_message = parser->type_definition_forbidden_message;
5496 parser->type_definition_forbidden_message
5497 = G_("types may not be defined in a %<typeid%> expression");
5498 /* We can't be sure yet whether we're looking at a type-id or an
5500 cp_parser_parse_tentatively (parser);
5501 /* Try a type-id first. */
5502 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5503 parser->in_type_id_in_expr_p = true;
5504 type = cp_parser_type_id (parser);
5505 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5506 /* Look for the `)' token. Otherwise, we can't be sure that
5507 we're not looking at an expression: consider `typeid (int
5508 (3))', for example. */
5509 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5510 /* If all went well, simply lookup the type-id. */
5511 if (cp_parser_parse_definitely (parser))
5512 postfix_expression = get_typeid (type, tf_warning_or_error);
5513 /* Otherwise, fall back to the expression variant. */
5518 /* Look for an expression. */
5519 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5520 /* Compute its typeid. */
5521 postfix_expression = build_typeid (expression, tf_warning_or_error);
5522 /* Look for the `)' token. */
5523 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5525 /* Restore the saved message. */
5526 parser->type_definition_forbidden_message = saved_message;
5527 /* `typeid' may not appear in an integral constant expression. */
5528 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5529 return error_mark_node;
5536 /* The syntax permitted here is the same permitted for an
5537 elaborated-type-specifier. */
5538 type = cp_parser_elaborated_type_specifier (parser,
5539 /*is_friend=*/false,
5540 /*is_declaration=*/false);
5541 postfix_expression = cp_parser_functional_cast (parser, type);
5545 case RID_BUILTIN_SHUFFLE:
5547 vec<tree, va_gc> *vec;
5550 location_t loc = token->location;
5552 cp_lexer_consume_token (parser->lexer);
5553 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5554 /*cast_p=*/false, /*allow_expansion_p=*/true,
5555 /*non_constant_p=*/NULL);
5557 return error_mark_node;
5559 FOR_EACH_VEC_ELT (*vec, i, p)
5562 if (vec->length () == 2)
5563 return c_build_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1]);
5564 else if (vec->length () == 3)
5565 return c_build_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2]);
5568 error_at (loc, "wrong number of arguments to "
5569 "%<__builtin_shuffle%>");
5570 return error_mark_node;
5579 /* If the next thing is a simple-type-specifier, we may be
5580 looking at a functional cast. We could also be looking at
5581 an id-expression. So, we try the functional cast, and if
5582 that doesn't work we fall back to the primary-expression. */
5583 cp_parser_parse_tentatively (parser);
5584 /* Look for the simple-type-specifier. */
5585 type = cp_parser_simple_type_specifier (parser,
5586 /*decl_specs=*/NULL,
5587 CP_PARSER_FLAGS_NONE);
5588 /* Parse the cast itself. */
5589 if (!cp_parser_error_occurred (parser))
5591 = cp_parser_functional_cast (parser, type);
5592 /* If that worked, we're done. */
5593 if (cp_parser_parse_definitely (parser))
5596 /* If the functional-cast didn't work out, try a
5597 compound-literal. */
5598 if (cp_parser_allow_gnu_extensions_p (parser)
5599 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5601 vec<constructor_elt, va_gc> *initializer_list = NULL;
5602 bool saved_in_type_id_in_expr_p;
5604 cp_parser_parse_tentatively (parser);
5605 /* Consume the `('. */
5606 cp_lexer_consume_token (parser->lexer);
5607 /* Parse the type. */
5608 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5609 parser->in_type_id_in_expr_p = true;
5610 type = cp_parser_type_id (parser);
5611 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5612 /* Look for the `)'. */
5613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5614 /* Look for the `{'. */
5615 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5616 /* If things aren't going well, there's no need to
5618 if (!cp_parser_error_occurred (parser))
5620 bool non_constant_p;
5621 /* Parse the initializer-list. */
5623 = cp_parser_initializer_list (parser, &non_constant_p);
5624 /* Allow a trailing `,'. */
5625 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5626 cp_lexer_consume_token (parser->lexer);
5627 /* Look for the final `}'. */
5628 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5630 /* If that worked, we're definitely looking at a
5631 compound-literal expression. */
5632 if (cp_parser_parse_definitely (parser))
5634 /* Warn the user that a compound literal is not
5635 allowed in standard C++. */
5636 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5637 /* For simplicity, we disallow compound literals in
5638 constant-expressions. We could
5639 allow compound literals of integer type, whose
5640 initializer was a constant, in constant
5641 expressions. Permitting that usage, as a further
5642 extension, would not change the meaning of any
5643 currently accepted programs. (Of course, as
5644 compound literals are not part of ISO C++, the
5645 standard has nothing to say.) */
5646 if (cp_parser_non_integral_constant_expression (parser,
5649 postfix_expression = error_mark_node;
5652 /* Form the representation of the compound-literal. */
5654 = (finish_compound_literal
5655 (type, build_constructor (init_list_type_node,
5657 tf_warning_or_error));
5662 /* It must be a primary-expression. */
5664 = cp_parser_primary_expression (parser, address_p, cast_p,
5665 /*template_arg_p=*/false,
5672 /* Note that we don't need to worry about calling build_cplus_new on a
5673 class-valued CALL_EXPR in decltype when it isn't the end of the
5674 postfix-expression; unary_complex_lvalue will take care of that for
5677 /* Keep looping until the postfix-expression is complete. */
5680 if (idk == CP_ID_KIND_UNQUALIFIED
5681 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5682 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5683 /* It is not a Koenig lookup function call. */
5685 = unqualified_name_lookup_error (postfix_expression);
5687 /* Peek at the next token. */
5688 token = cp_lexer_peek_token (parser->lexer);
5690 switch (token->type)
5692 case CPP_OPEN_SQUARE:
5693 if (cp_next_tokens_can_be_std_attribute_p (parser))
5695 cp_parser_error (parser,
5696 "two consecutive %<[%> shall "
5697 "only introduce an attribute");
5698 return error_mark_node;
5701 = cp_parser_postfix_open_square_expression (parser,
5705 idk = CP_ID_KIND_NONE;
5706 is_member_access = false;
5709 case CPP_OPEN_PAREN:
5710 /* postfix-expression ( expression-list [opt] ) */
5713 bool is_builtin_constant_p;
5714 bool saved_integral_constant_expression_p = false;
5715 bool saved_non_integral_constant_expression_p = false;
5716 tsubst_flags_t complain = complain_flags (decltype_p);
5717 vec<tree, va_gc> *args;
5719 is_member_access = false;
5721 is_builtin_constant_p
5722 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5723 if (is_builtin_constant_p)
5725 /* The whole point of __builtin_constant_p is to allow
5726 non-constant expressions to appear as arguments. */
5727 saved_integral_constant_expression_p
5728 = parser->integral_constant_expression_p;
5729 saved_non_integral_constant_expression_p
5730 = parser->non_integral_constant_expression_p;
5731 parser->integral_constant_expression_p = false;
5733 args = (cp_parser_parenthesized_expression_list
5735 /*cast_p=*/false, /*allow_expansion_p=*/true,
5736 /*non_constant_p=*/NULL));
5737 if (is_builtin_constant_p)
5739 parser->integral_constant_expression_p
5740 = saved_integral_constant_expression_p;
5741 parser->non_integral_constant_expression_p
5742 = saved_non_integral_constant_expression_p;
5747 postfix_expression = error_mark_node;
5751 /* Function calls are not permitted in
5752 constant-expressions. */
5753 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5754 && cp_parser_non_integral_constant_expression (parser,
5757 postfix_expression = error_mark_node;
5758 release_tree_vector (args);
5763 if (idk == CP_ID_KIND_UNQUALIFIED
5764 || idk == CP_ID_KIND_TEMPLATE_ID)
5766 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5768 if (!args->is_empty ())
5771 if (!any_type_dependent_arguments_p (args))
5773 = perform_koenig_lookup (postfix_expression, args,
5774 /*include_std=*/false,
5779 = unqualified_fn_lookup_error (postfix_expression);
5781 /* We do not perform argument-dependent lookup if
5782 normal lookup finds a non-function, in accordance
5783 with the expected resolution of DR 218. */
5784 else if (!args->is_empty ()
5785 && is_overloaded_fn (postfix_expression))
5787 tree fn = get_first_fn (postfix_expression);
5788 fn = STRIP_TEMPLATE (fn);
5790 /* Do not do argument dependent lookup if regular
5791 lookup finds a member function or a block-scope
5792 function declaration. [basic.lookup.argdep]/3 */
5793 if (!DECL_FUNCTION_MEMBER_P (fn)
5794 && !DECL_LOCAL_FUNCTION_P (fn))
5797 if (!any_type_dependent_arguments_p (args))
5799 = perform_koenig_lookup (postfix_expression, args,
5800 /*include_std=*/false,
5806 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5808 tree instance = TREE_OPERAND (postfix_expression, 0);
5809 tree fn = TREE_OPERAND (postfix_expression, 1);
5811 if (processing_template_decl
5812 && (type_dependent_expression_p (instance)
5813 || (!BASELINK_P (fn)
5814 && TREE_CODE (fn) != FIELD_DECL)
5815 || type_dependent_expression_p (fn)
5816 || any_type_dependent_arguments_p (args)))
5819 = build_nt_call_vec (postfix_expression, args);
5820 release_tree_vector (args);
5824 if (BASELINK_P (fn))
5827 = (build_new_method_call
5828 (instance, fn, &args, NULL_TREE,
5829 (idk == CP_ID_KIND_QUALIFIED
5830 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5837 = finish_call_expr (postfix_expression, &args,
5838 /*disallow_virtual=*/false,
5842 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5843 || TREE_CODE (postfix_expression) == MEMBER_REF
5844 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5845 postfix_expression = (build_offset_ref_call_from_tree
5846 (postfix_expression, &args,
5848 else if (idk == CP_ID_KIND_QUALIFIED)
5849 /* A call to a static class member, or a namespace-scope
5852 = finish_call_expr (postfix_expression, &args,
5853 /*disallow_virtual=*/true,
5857 /* All other function calls. */
5859 = finish_call_expr (postfix_expression, &args,
5860 /*disallow_virtual=*/false,
5864 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5865 idk = CP_ID_KIND_NONE;
5867 release_tree_vector (args);
5873 /* postfix-expression . template [opt] id-expression
5874 postfix-expression . pseudo-destructor-name
5875 postfix-expression -> template [opt] id-expression
5876 postfix-expression -> pseudo-destructor-name */
5878 /* Consume the `.' or `->' operator. */
5879 cp_lexer_consume_token (parser->lexer);
5882 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5887 is_member_access = true;
5891 /* postfix-expression ++ */
5892 /* Consume the `++' token. */
5893 cp_lexer_consume_token (parser->lexer);
5894 /* Generate a representation for the complete expression. */
5896 = finish_increment_expr (postfix_expression,
5897 POSTINCREMENT_EXPR);
5898 /* Increments may not appear in constant-expressions. */
5899 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5900 postfix_expression = error_mark_node;
5901 idk = CP_ID_KIND_NONE;
5902 is_member_access = false;
5905 case CPP_MINUS_MINUS:
5906 /* postfix-expression -- */
5907 /* Consume the `--' token. */
5908 cp_lexer_consume_token (parser->lexer);
5909 /* Generate a representation for the complete expression. */
5911 = finish_increment_expr (postfix_expression,
5912 POSTDECREMENT_EXPR);
5913 /* Decrements may not appear in constant-expressions. */
5914 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5915 postfix_expression = error_mark_node;
5916 idk = CP_ID_KIND_NONE;
5917 is_member_access = false;
5921 if (pidk_return != NULL)
5922 * pidk_return = idk;
5923 if (member_access_only_p)
5924 return is_member_access? postfix_expression : error_mark_node;
5926 return postfix_expression;
5930 /* We should never get here. */
5932 return error_mark_node;
5935 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5936 by cp_parser_builtin_offsetof. We're looking for
5938 postfix-expression [ expression ]
5939 postfix-expression [ braced-init-list ] (C++11)
5941 FOR_OFFSETOF is set if we're being called in that context, which
5942 changes how we deal with integer constant expressions. */
5945 cp_parser_postfix_open_square_expression (cp_parser *parser,
5946 tree postfix_expression,
5951 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5953 /* Consume the `[' token. */
5954 cp_lexer_consume_token (parser->lexer);
5956 /* Parse the index expression. */
5957 /* ??? For offsetof, there is a question of what to allow here. If
5958 offsetof is not being used in an integral constant expression context,
5959 then we *could* get the right answer by computing the value at runtime.
5960 If we are in an integral constant expression context, then we might
5961 could accept any constant expression; hard to say without analysis.
5962 Rather than open the barn door too wide right away, allow only integer
5963 constant expressions here. */
5965 index = cp_parser_constant_expression (parser, false, NULL);
5968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5970 bool expr_nonconst_p;
5971 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5972 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5975 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5978 /* Look for the closing `]'. */
5979 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5981 /* Build the ARRAY_REF. */
5982 postfix_expression = grok_array_decl (loc, postfix_expression,
5985 /* When not doing offsetof, array references are not permitted in
5986 constant-expressions. */
5988 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5989 postfix_expression = error_mark_node;
5991 return postfix_expression;
5994 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5995 by cp_parser_builtin_offsetof. We're looking for
5997 postfix-expression . template [opt] id-expression
5998 postfix-expression . pseudo-destructor-name
5999 postfix-expression -> template [opt] id-expression
6000 postfix-expression -> pseudo-destructor-name
6002 FOR_OFFSETOF is set if we're being called in that context. That sorta
6003 limits what of the above we'll actually accept, but nevermind.
6004 TOKEN_TYPE is the "." or "->" token, which will already have been
6005 removed from the stream. */
6008 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6009 enum cpp_ttype token_type,
6010 tree postfix_expression,
6011 bool for_offsetof, cp_id_kind *idk,
6012 location_t location)
6016 bool pseudo_destructor_p;
6017 tree scope = NULL_TREE;
6019 /* If this is a `->' operator, dereference the pointer. */
6020 if (token_type == CPP_DEREF)
6021 postfix_expression = build_x_arrow (location, postfix_expression,
6022 tf_warning_or_error);
6023 /* Check to see whether or not the expression is type-dependent. */
6024 dependent_p = type_dependent_expression_p (postfix_expression);
6025 /* The identifier following the `->' or `.' is not qualified. */
6026 parser->scope = NULL_TREE;
6027 parser->qualifying_scope = NULL_TREE;
6028 parser->object_scope = NULL_TREE;
6029 *idk = CP_ID_KIND_NONE;
6031 /* Enter the scope corresponding to the type of the object
6032 given by the POSTFIX_EXPRESSION. */
6033 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6035 scope = TREE_TYPE (postfix_expression);
6036 /* According to the standard, no expression should ever have
6037 reference type. Unfortunately, we do not currently match
6038 the standard in this respect in that our internal representation
6039 of an expression may have reference type even when the standard
6040 says it does not. Therefore, we have to manually obtain the
6041 underlying type here. */
6042 scope = non_reference (scope);
6043 /* The type of the POSTFIX_EXPRESSION must be complete. */
6044 if (scope == unknown_type_node)
6046 error_at (location, "%qE does not have class type",
6047 postfix_expression);
6050 /* Unlike the object expression in other contexts, *this is not
6051 required to be of complete type for purposes of class member
6052 access (5.2.5) outside the member function body. */
6053 else if (scope != current_class_ref
6054 && !(processing_template_decl && scope == current_class_type))
6055 scope = complete_type_or_else (scope, NULL_TREE);
6056 /* Let the name lookup machinery know that we are processing a
6057 class member access expression. */
6058 parser->context->object_type = scope;
6059 /* If something went wrong, we want to be able to discern that case,
6060 as opposed to the case where there was no SCOPE due to the type
6061 of expression being dependent. */
6063 scope = error_mark_node;
6064 /* If the SCOPE was erroneous, make the various semantic analysis
6065 functions exit quickly -- and without issuing additional error
6067 if (scope == error_mark_node)
6068 postfix_expression = error_mark_node;
6071 /* Assume this expression is not a pseudo-destructor access. */
6072 pseudo_destructor_p = false;
6074 /* If the SCOPE is a scalar type, then, if this is a valid program,
6075 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6076 is type dependent, it can be pseudo-destructor-name or something else.
6077 Try to parse it as pseudo-destructor-name first. */
6078 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6083 cp_parser_parse_tentatively (parser);
6084 /* Parse the pseudo-destructor-name. */
6086 cp_parser_pseudo_destructor_name (parser, &s, &type);
6088 && (cp_parser_error_occurred (parser)
6089 || TREE_CODE (type) != TYPE_DECL
6090 || !SCALAR_TYPE_P (TREE_TYPE (type))))
6091 cp_parser_abort_tentative_parse (parser);
6092 else if (cp_parser_parse_definitely (parser))
6094 pseudo_destructor_p = true;
6096 = finish_pseudo_destructor_expr (postfix_expression,
6097 s, TREE_TYPE (type));
6101 if (!pseudo_destructor_p)
6103 /* If the SCOPE is not a scalar type, we are looking at an
6104 ordinary class member access expression, rather than a
6105 pseudo-destructor-name. */
6107 cp_token *token = cp_lexer_peek_token (parser->lexer);
6108 /* Parse the id-expression. */
6109 name = (cp_parser_id_expression
6111 cp_parser_optional_template_keyword (parser),
6112 /*check_dependency_p=*/true,
6114 /*declarator_p=*/false,
6115 /*optional_p=*/false));
6116 /* In general, build a SCOPE_REF if the member name is qualified.
6117 However, if the name was not dependent and has already been
6118 resolved; there is no need to build the SCOPE_REF. For example;
6120 struct X { void f(); };
6121 template <typename T> void f(T* t) { t->X::f(); }
6123 Even though "t" is dependent, "X::f" is not and has been resolved
6124 to a BASELINK; there is no need to include scope information. */
6126 /* But we do need to remember that there was an explicit scope for
6127 virtual function calls. */
6129 *idk = CP_ID_KIND_QUALIFIED;
6131 /* If the name is a template-id that names a type, we will get a
6132 TYPE_DECL here. That is invalid code. */
6133 if (TREE_CODE (name) == TYPE_DECL)
6135 error_at (token->location, "invalid use of %qD", name);
6136 postfix_expression = error_mark_node;
6140 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6142 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6144 error_at (token->location, "%<%D::%D%> is not a class member",
6145 parser->scope, name);
6146 postfix_expression = error_mark_node;
6149 name = build_qualified_name (/*type=*/NULL_TREE,
6153 parser->scope = NULL_TREE;
6154 parser->qualifying_scope = NULL_TREE;
6155 parser->object_scope = NULL_TREE;
6157 if (parser->scope && name && BASELINK_P (name))
6158 adjust_result_of_qualified_name_lookup
6159 (name, parser->scope, scope);
6161 = finish_class_member_access_expr (postfix_expression, name,
6163 tf_warning_or_error);
6167 /* We no longer need to look up names in the scope of the object on
6168 the left-hand side of the `.' or `->' operator. */
6169 parser->context->object_type = NULL_TREE;
6171 /* Outside of offsetof, these operators may not appear in
6172 constant-expressions. */
6174 && (cp_parser_non_integral_constant_expression
6175 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6176 postfix_expression = error_mark_node;
6178 return postfix_expression;
6181 /* Parse a parenthesized expression-list.
6184 assignment-expression
6185 expression-list, assignment-expression
6190 identifier, expression-list
6192 CAST_P is true if this expression is the target of a cast.
6194 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6197 Returns a vector of trees. Each element is a representation of an
6198 assignment-expression. NULL is returned if the ( and or ) are
6199 missing. An empty, but allocated, vector is returned on no
6200 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6201 if we are parsing an attribute list for an attribute that wants a
6202 plain identifier argument, normal_attr for an attribute that wants
6203 an expression, or non_attr if we aren't parsing an attribute list. If
6204 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6205 not all of the expressions in the list were constant. */
6207 static vec<tree, va_gc> *
6208 cp_parser_parenthesized_expression_list (cp_parser* parser,
6209 int is_attribute_list,
6211 bool allow_expansion_p,
6212 bool *non_constant_p)
6214 vec<tree, va_gc> *expression_list;
6215 bool fold_expr_p = is_attribute_list != non_attr;
6216 tree identifier = NULL_TREE;
6217 bool saved_greater_than_is_operator_p;
6219 /* Assume all the expressions will be constant. */
6221 *non_constant_p = false;
6223 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6226 expression_list = make_tree_vector ();
6228 /* Within a parenthesized expression, a `>' token is always
6229 the greater-than operator. */
6230 saved_greater_than_is_operator_p
6231 = parser->greater_than_is_operator_p;
6232 parser->greater_than_is_operator_p = true;
6234 /* Consume expressions until there are no more. */
6235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6240 /* At the beginning of attribute lists, check to see if the
6241 next token is an identifier. */
6242 if (is_attribute_list == id_attr
6243 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6247 /* Consume the identifier. */
6248 token = cp_lexer_consume_token (parser->lexer);
6249 /* Save the identifier. */
6250 identifier = token->u.value;
6254 bool expr_non_constant_p;
6256 /* Parse the next assignment-expression. */
6257 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6259 /* A braced-init-list. */
6260 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6261 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6262 if (non_constant_p && expr_non_constant_p)
6263 *non_constant_p = true;
6265 else if (non_constant_p)
6267 expr = (cp_parser_constant_expression
6268 (parser, /*allow_non_constant_p=*/true,
6269 &expr_non_constant_p));
6270 if (expr_non_constant_p)
6271 *non_constant_p = true;
6274 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6277 expr = fold_non_dependent_expr (expr);
6279 /* If we have an ellipsis, then this is an expression
6281 if (allow_expansion_p
6282 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6284 /* Consume the `...'. */
6285 cp_lexer_consume_token (parser->lexer);
6287 /* Build the argument pack. */
6288 expr = make_pack_expansion (expr);
6291 /* Add it to the list. We add error_mark_node
6292 expressions to the list, so that we can still tell if
6293 the correct form for a parenthesized expression-list
6294 is found. That gives better errors. */
6295 vec_safe_push (expression_list, expr);
6297 if (expr == error_mark_node)
6301 /* After the first item, attribute lists look the same as
6302 expression lists. */
6303 is_attribute_list = non_attr;
6306 /* If the next token isn't a `,', then we are done. */
6307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6310 /* Otherwise, consume the `,' and keep going. */
6311 cp_lexer_consume_token (parser->lexer);
6314 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6319 /* We try and resync to an unnested comma, as that will give the
6320 user better diagnostics. */
6321 ending = cp_parser_skip_to_closing_parenthesis (parser,
6322 /*recovering=*/true,
6324 /*consume_paren=*/true);
6329 parser->greater_than_is_operator_p
6330 = saved_greater_than_is_operator_p;
6335 parser->greater_than_is_operator_p
6336 = saved_greater_than_is_operator_p;
6339 vec_safe_insert (expression_list, 0, identifier);
6341 return expression_list;
6344 /* Parse a pseudo-destructor-name.
6346 pseudo-destructor-name:
6347 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6348 :: [opt] nested-name-specifier template template-id :: ~ type-name
6349 :: [opt] nested-name-specifier [opt] ~ type-name
6351 If either of the first two productions is used, sets *SCOPE to the
6352 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6353 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6354 or ERROR_MARK_NODE if the parse fails. */
6357 cp_parser_pseudo_destructor_name (cp_parser* parser,
6361 bool nested_name_specifier_p;
6363 /* Assume that things will not work out. */
6364 *type = error_mark_node;
6366 /* Look for the optional `::' operator. */
6367 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6368 /* Look for the optional nested-name-specifier. */
6369 nested_name_specifier_p
6370 = (cp_parser_nested_name_specifier_opt (parser,
6371 /*typename_keyword_p=*/false,
6372 /*check_dependency_p=*/true,
6374 /*is_declaration=*/false)
6376 /* Now, if we saw a nested-name-specifier, we might be doing the
6377 second production. */
6378 if (nested_name_specifier_p
6379 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6381 /* Consume the `template' keyword. */
6382 cp_lexer_consume_token (parser->lexer);
6383 /* Parse the template-id. */
6384 cp_parser_template_id (parser,
6385 /*template_keyword_p=*/true,
6386 /*check_dependency_p=*/false,
6388 /*is_declaration=*/true);
6389 /* Look for the `::' token. */
6390 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6392 /* If the next token is not a `~', then there might be some
6393 additional qualification. */
6394 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6396 /* At this point, we're looking for "type-name :: ~". The type-name
6397 must not be a class-name, since this is a pseudo-destructor. So,
6398 it must be either an enum-name, or a typedef-name -- both of which
6399 are just identifiers. So, we peek ahead to check that the "::"
6400 and "~" tokens are present; if they are not, then we can avoid
6401 calling type_name. */
6402 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6403 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6404 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6406 cp_parser_error (parser, "non-scalar type");
6410 /* Look for the type-name. */
6411 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6412 if (*scope == error_mark_node)
6415 /* Look for the `::' token. */
6416 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6421 /* Look for the `~'. */
6422 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6424 /* Once we see the ~, this has to be a pseudo-destructor. */
6425 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6426 cp_parser_commit_to_tentative_parse (parser);
6428 /* Look for the type-name again. We are not responsible for
6429 checking that it matches the first type-name. */
6430 *type = cp_parser_nonclass_name (parser);
6433 /* Parse a unary-expression.
6439 unary-operator cast-expression
6440 sizeof unary-expression
6442 alignof ( type-id ) [C++0x]
6449 __extension__ cast-expression
6450 __alignof__ unary-expression
6451 __alignof__ ( type-id )
6452 alignof unary-expression [C++0x]
6453 __real__ cast-expression
6454 __imag__ cast-expression
6457 ADDRESS_P is true iff the unary-expression is appearing as the
6458 operand of the `&' operator. CAST_P is true if this expression is
6459 the target of a cast.
6461 Returns a representation of the expression. */
6464 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6465 bool decltype_p, cp_id_kind * pidk)
6468 enum tree_code unary_operator;
6470 /* Peek at the next token. */
6471 token = cp_lexer_peek_token (parser->lexer);
6472 /* Some keywords give away the kind of expression. */
6473 if (token->type == CPP_KEYWORD)
6475 enum rid keyword = token->keyword;
6484 location_t first_loc;
6486 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6487 /* Consume the token. */
6488 cp_lexer_consume_token (parser->lexer);
6489 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6490 /* Parse the operand. */
6491 operand = cp_parser_sizeof_operand (parser, keyword);
6493 if (TYPE_P (operand))
6494 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6497 /* ISO C++ defines alignof only with types, not with
6498 expressions. So pedwarn if alignof is used with a non-
6499 type expression. However, __alignof__ is ok. */
6500 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6501 pedwarn (token->location, OPT_Wpedantic,
6502 "ISO C++ does not allow %<alignof%> "
6505 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6507 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6508 SIZEOF_EXPR with the original operand. */
6509 if (op == SIZEOF_EXPR && ret != error_mark_node)
6511 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6513 if (!processing_template_decl && TYPE_P (operand))
6515 ret = build_min (SIZEOF_EXPR, size_type_node,
6516 build1 (NOP_EXPR, operand,
6518 SIZEOF_EXPR_TYPE_P (ret) = 1;
6521 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6522 TREE_SIDE_EFFECTS (ret) = 0;
6523 TREE_READONLY (ret) = 1;
6525 SET_EXPR_LOCATION (ret, first_loc);
6531 return cp_parser_new_expression (parser);
6534 return cp_parser_delete_expression (parser);
6538 /* The saved value of the PEDANTIC flag. */
6542 /* Save away the PEDANTIC flag. */
6543 cp_parser_extension_opt (parser, &saved_pedantic);
6544 /* Parse the cast-expression. */
6545 expr = cp_parser_simple_cast_expression (parser);
6546 /* Restore the PEDANTIC flag. */
6547 pedantic = saved_pedantic;
6557 /* Consume the `__real__' or `__imag__' token. */
6558 cp_lexer_consume_token (parser->lexer);
6559 /* Parse the cast-expression. */
6560 expression = cp_parser_simple_cast_expression (parser);
6561 /* Create the complete representation. */
6562 return build_x_unary_op (token->location,
6563 (keyword == RID_REALPART
6564 ? REALPART_EXPR : IMAGPART_EXPR),
6566 tf_warning_or_error);
6570 case RID_TRANSACTION_ATOMIC:
6571 case RID_TRANSACTION_RELAXED:
6572 return cp_parser_transaction_expression (parser, keyword);
6577 const char *saved_message;
6578 bool saved_integral_constant_expression_p;
6579 bool saved_non_integral_constant_expression_p;
6580 bool saved_greater_than_is_operator_p;
6582 cp_lexer_consume_token (parser->lexer);
6583 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6585 saved_message = parser->type_definition_forbidden_message;
6586 parser->type_definition_forbidden_message
6587 = G_("types may not be defined in %<noexcept%> expressions");
6589 saved_integral_constant_expression_p
6590 = parser->integral_constant_expression_p;
6591 saved_non_integral_constant_expression_p
6592 = parser->non_integral_constant_expression_p;
6593 parser->integral_constant_expression_p = false;
6595 saved_greater_than_is_operator_p
6596 = parser->greater_than_is_operator_p;
6597 parser->greater_than_is_operator_p = true;
6599 ++cp_unevaluated_operand;
6600 ++c_inhibit_evaluation_warnings;
6601 expr = cp_parser_expression (parser, false, NULL);
6602 --c_inhibit_evaluation_warnings;
6603 --cp_unevaluated_operand;
6605 parser->greater_than_is_operator_p
6606 = saved_greater_than_is_operator_p;
6608 parser->integral_constant_expression_p
6609 = saved_integral_constant_expression_p;
6610 parser->non_integral_constant_expression_p
6611 = saved_non_integral_constant_expression_p;
6613 parser->type_definition_forbidden_message = saved_message;
6615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6616 return finish_noexcept_expr (expr, tf_warning_or_error);
6624 /* Look for the `:: new' and `:: delete', which also signal the
6625 beginning of a new-expression, or delete-expression,
6626 respectively. If the next token is `::', then it might be one of
6628 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6632 /* See if the token after the `::' is one of the keywords in
6633 which we're interested. */
6634 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6635 /* If it's `new', we have a new-expression. */
6636 if (keyword == RID_NEW)
6637 return cp_parser_new_expression (parser);
6638 /* Similarly, for `delete'. */
6639 else if (keyword == RID_DELETE)
6640 return cp_parser_delete_expression (parser);
6643 /* Look for a unary operator. */
6644 unary_operator = cp_parser_unary_operator (token);
6645 /* The `++' and `--' operators can be handled similarly, even though
6646 they are not technically unary-operators in the grammar. */
6647 if (unary_operator == ERROR_MARK)
6649 if (token->type == CPP_PLUS_PLUS)
6650 unary_operator = PREINCREMENT_EXPR;
6651 else if (token->type == CPP_MINUS_MINUS)
6652 unary_operator = PREDECREMENT_EXPR;
6653 /* Handle the GNU address-of-label extension. */
6654 else if (cp_parser_allow_gnu_extensions_p (parser)
6655 && token->type == CPP_AND_AND)
6659 location_t loc = token->location;
6661 /* Consume the '&&' token. */
6662 cp_lexer_consume_token (parser->lexer);
6663 /* Look for the identifier. */
6664 identifier = cp_parser_identifier (parser);
6665 /* Create an expression representing the address. */
6666 expression = finish_label_address_expr (identifier, loc);
6667 if (cp_parser_non_integral_constant_expression (parser,
6669 expression = error_mark_node;
6673 if (unary_operator != ERROR_MARK)
6675 tree cast_expression;
6676 tree expression = error_mark_node;
6677 non_integral_constant non_constant_p = NIC_NONE;
6678 location_t loc = token->location;
6679 tsubst_flags_t complain = complain_flags (decltype_p);
6681 /* Consume the operator token. */
6682 token = cp_lexer_consume_token (parser->lexer);
6683 /* Parse the cast-expression. */
6685 = cp_parser_cast_expression (parser,
6686 unary_operator == ADDR_EXPR,
6690 /* Now, build an appropriate representation. */
6691 switch (unary_operator)
6694 non_constant_p = NIC_STAR;
6695 expression = build_x_indirect_ref (loc, cast_expression,
6701 non_constant_p = NIC_ADDR;
6704 expression = build_x_unary_op (loc, unary_operator,
6709 case PREINCREMENT_EXPR:
6710 case PREDECREMENT_EXPR:
6711 non_constant_p = unary_operator == PREINCREMENT_EXPR
6712 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6714 case UNARY_PLUS_EXPR:
6716 case TRUTH_NOT_EXPR:
6717 expression = finish_unary_op_expr (loc, unary_operator,
6718 cast_expression, complain);
6725 if (non_constant_p != NIC_NONE
6726 && cp_parser_non_integral_constant_expression (parser,
6728 expression = error_mark_node;
6733 return cp_parser_postfix_expression (parser, address_p, cast_p,
6734 /*member_access_only_p=*/false,
6740 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6743 return cp_parser_unary_expression (parser, address_p, cast_p,
6744 /*decltype*/false, pidk);
6747 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6748 unary-operator, the corresponding tree code is returned. */
6750 static enum tree_code
6751 cp_parser_unary_operator (cp_token* token)
6753 switch (token->type)
6756 return INDIRECT_REF;
6762 return UNARY_PLUS_EXPR;
6768 return TRUTH_NOT_EXPR;
6771 return BIT_NOT_EXPR;
6778 /* Parse a new-expression.
6781 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6782 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6784 Returns a representation of the expression. */
6787 cp_parser_new_expression (cp_parser* parser)
6789 bool global_scope_p;
6790 vec<tree, va_gc> *placement;
6792 vec<tree, va_gc> *initializer;
6793 tree nelts = NULL_TREE;
6796 /* Look for the optional `::' operator. */
6798 = (cp_parser_global_scope_opt (parser,
6799 /*current_scope_valid_p=*/false)
6801 /* Look for the `new' operator. */
6802 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6803 /* There's no easy way to tell a new-placement from the
6804 `( type-id )' construct. */
6805 cp_parser_parse_tentatively (parser);
6806 /* Look for a new-placement. */
6807 placement = cp_parser_new_placement (parser);
6808 /* If that didn't work out, there's no new-placement. */
6809 if (!cp_parser_parse_definitely (parser))
6811 if (placement != NULL)
6812 release_tree_vector (placement);
6816 /* If the next token is a `(', then we have a parenthesized
6818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6821 const char *saved_message = parser->type_definition_forbidden_message;
6823 /* Consume the `('. */
6824 cp_lexer_consume_token (parser->lexer);
6826 /* Parse the type-id. */
6827 parser->type_definition_forbidden_message
6828 = G_("types may not be defined in a new-expression");
6829 type = cp_parser_type_id (parser);
6830 parser->type_definition_forbidden_message = saved_message;
6832 /* Look for the closing `)'. */
6833 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6834 token = cp_lexer_peek_token (parser->lexer);
6835 /* There should not be a direct-new-declarator in this production,
6836 but GCC used to allowed this, so we check and emit a sensible error
6837 message for this case. */
6838 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6840 error_at (token->location,
6841 "array bound forbidden after parenthesized type-id");
6842 inform (token->location,
6843 "try removing the parentheses around the type-id");
6844 cp_parser_direct_new_declarator (parser);
6847 /* Otherwise, there must be a new-type-id. */
6849 type = cp_parser_new_type_id (parser, &nelts);
6851 /* If the next token is a `(' or '{', then we have a new-initializer. */
6852 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6853 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6854 initializer = cp_parser_new_initializer (parser);
6858 /* A new-expression may not appear in an integral constant
6860 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6861 ret = error_mark_node;
6864 /* Create a representation of the new-expression. */
6865 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6866 tf_warning_or_error);
6869 if (placement != NULL)
6870 release_tree_vector (placement);
6871 if (initializer != NULL)
6872 release_tree_vector (initializer);
6877 /* Parse a new-placement.
6882 Returns the same representation as for an expression-list. */
6884 static vec<tree, va_gc> *
6885 cp_parser_new_placement (cp_parser* parser)
6887 vec<tree, va_gc> *expression_list;
6889 /* Parse the expression-list. */
6890 expression_list = (cp_parser_parenthesized_expression_list
6891 (parser, non_attr, /*cast_p=*/false,
6892 /*allow_expansion_p=*/true,
6893 /*non_constant_p=*/NULL));
6895 return expression_list;
6898 /* Parse a new-type-id.
6901 type-specifier-seq new-declarator [opt]
6903 Returns the TYPE allocated. If the new-type-id indicates an array
6904 type, *NELTS is set to the number of elements in the last array
6905 bound; the TYPE will not include the last array bound. */
6908 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6910 cp_decl_specifier_seq type_specifier_seq;
6911 cp_declarator *new_declarator;
6912 cp_declarator *declarator;
6913 cp_declarator *outer_declarator;
6914 const char *saved_message;
6916 /* The type-specifier sequence must not contain type definitions.
6917 (It cannot contain declarations of new types either, but if they
6918 are not definitions we will catch that because they are not
6920 saved_message = parser->type_definition_forbidden_message;
6921 parser->type_definition_forbidden_message
6922 = G_("types may not be defined in a new-type-id");
6923 /* Parse the type-specifier-seq. */
6924 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6925 /*is_trailing_return=*/false,
6926 &type_specifier_seq);
6927 /* Restore the old message. */
6928 parser->type_definition_forbidden_message = saved_message;
6930 if (type_specifier_seq.type == error_mark_node)
6931 return error_mark_node;
6933 /* Parse the new-declarator. */
6934 new_declarator = cp_parser_new_declarator_opt (parser);
6936 /* Determine the number of elements in the last array dimension, if
6939 /* Skip down to the last array dimension. */
6940 declarator = new_declarator;
6941 outer_declarator = NULL;
6942 while (declarator && (declarator->kind == cdk_pointer
6943 || declarator->kind == cdk_ptrmem))
6945 outer_declarator = declarator;
6946 declarator = declarator->declarator;
6949 && declarator->kind == cdk_array
6950 && declarator->declarator
6951 && declarator->declarator->kind == cdk_array)
6953 outer_declarator = declarator;
6954 declarator = declarator->declarator;
6957 if (declarator && declarator->kind == cdk_array)
6959 *nelts = declarator->u.array.bounds;
6960 if (*nelts == error_mark_node)
6961 *nelts = integer_one_node;
6963 if (outer_declarator)
6964 outer_declarator->declarator = declarator->declarator;
6966 new_declarator = NULL;
6969 return groktypename (&type_specifier_seq, new_declarator, false);
6972 /* Parse an (optional) new-declarator.
6975 ptr-operator new-declarator [opt]
6976 direct-new-declarator
6978 Returns the declarator. */
6980 static cp_declarator *
6981 cp_parser_new_declarator_opt (cp_parser* parser)
6983 enum tree_code code;
6984 tree type, std_attributes = NULL_TREE;
6985 cp_cv_quals cv_quals;
6987 /* We don't know if there's a ptr-operator next, or not. */
6988 cp_parser_parse_tentatively (parser);
6989 /* Look for a ptr-operator. */
6990 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
6991 /* If that worked, look for more new-declarators. */
6992 if (cp_parser_parse_definitely (parser))
6994 cp_declarator *declarator;
6996 /* Parse another optional declarator. */
6997 declarator = cp_parser_new_declarator_opt (parser);
6999 declarator = cp_parser_make_indirect_declarator
7000 (code, type, cv_quals, declarator, std_attributes);
7005 /* If the next token is a `[', there is a direct-new-declarator. */
7006 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7007 return cp_parser_direct_new_declarator (parser);
7012 /* Parse a direct-new-declarator.
7014 direct-new-declarator:
7016 direct-new-declarator [constant-expression]
7020 static cp_declarator *
7021 cp_parser_direct_new_declarator (cp_parser* parser)
7023 cp_declarator *declarator = NULL;
7030 /* Look for the opening `['. */
7031 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7033 token = cp_lexer_peek_token (parser->lexer);
7034 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7035 /* The standard requires that the expression have integral
7036 type. DR 74 adds enumeration types. We believe that the
7037 real intent is that these expressions be handled like the
7038 expression in a `switch' condition, which also allows
7039 classes with a single conversion to integral or
7040 enumeration type. */
7041 if (!processing_template_decl)
7044 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7049 error_at (token->location,
7050 "expression in new-declarator must have integral "
7051 "or enumeration type");
7052 expression = error_mark_node;
7056 /* Look for the closing `]'. */
7057 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7059 /* Add this bound to the declarator. */
7060 declarator = make_array_declarator (declarator, expression);
7062 /* If the next token is not a `[', then there are no more
7064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7071 /* Parse a new-initializer.
7074 ( expression-list [opt] )
7077 Returns a representation of the expression-list. */
7079 static vec<tree, va_gc> *
7080 cp_parser_new_initializer (cp_parser* parser)
7082 vec<tree, va_gc> *expression_list;
7084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7087 bool expr_non_constant_p;
7088 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7089 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7090 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7091 expression_list = make_tree_vector_single (t);
7094 expression_list = (cp_parser_parenthesized_expression_list
7095 (parser, non_attr, /*cast_p=*/false,
7096 /*allow_expansion_p=*/true,
7097 /*non_constant_p=*/NULL));
7099 return expression_list;
7102 /* Parse a delete-expression.
7105 :: [opt] delete cast-expression
7106 :: [opt] delete [ ] cast-expression
7108 Returns a representation of the expression. */
7111 cp_parser_delete_expression (cp_parser* parser)
7113 bool global_scope_p;
7117 /* Look for the optional `::' operator. */
7119 = (cp_parser_global_scope_opt (parser,
7120 /*current_scope_valid_p=*/false)
7122 /* Look for the `delete' keyword. */
7123 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7124 /* See if the array syntax is in use. */
7125 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7127 /* Consume the `[' token. */
7128 cp_lexer_consume_token (parser->lexer);
7129 /* Look for the `]' token. */
7130 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7131 /* Remember that this is the `[]' construct. */
7137 /* Parse the cast-expression. */
7138 expression = cp_parser_simple_cast_expression (parser);
7140 /* A delete-expression may not appear in an integral constant
7142 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7143 return error_mark_node;
7145 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7146 tf_warning_or_error);
7149 /* Returns true if TOKEN may start a cast-expression and false
7153 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7155 cp_token *token = cp_lexer_peek_token (parser->lexer);
7156 switch (token->type)
7162 case CPP_CLOSE_SQUARE:
7163 case CPP_CLOSE_PAREN:
7164 case CPP_CLOSE_BRACE:
7168 case CPP_DEREF_STAR:
7176 case CPP_GREATER_EQ:
7196 case CPP_OPEN_PAREN:
7197 /* In ((type ()) () the last () isn't a valid cast-expression,
7198 so the whole must be parsed as postfix-expression. */
7199 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7202 /* '[' may start a primary-expression in obj-c++. */
7203 case CPP_OPEN_SQUARE:
7204 return c_dialect_objc ();
7211 /* Parse a cast-expression.
7215 ( type-id ) cast-expression
7217 ADDRESS_P is true iff the unary-expression is appearing as the
7218 operand of the `&' operator. CAST_P is true if this expression is
7219 the target of a cast.
7221 Returns a representation of the expression. */
7224 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7225 bool decltype_p, cp_id_kind * pidk)
7227 /* If it's a `(', then we might be looking at a cast. */
7228 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7230 tree type = NULL_TREE;
7231 tree expr = NULL_TREE;
7232 bool compound_literal_p;
7233 const char *saved_message;
7235 /* There's no way to know yet whether or not this is a cast.
7236 For example, `(int (3))' is a unary-expression, while `(int)
7237 3' is a cast. So, we resort to parsing tentatively. */
7238 cp_parser_parse_tentatively (parser);
7239 /* Types may not be defined in a cast. */
7240 saved_message = parser->type_definition_forbidden_message;
7241 parser->type_definition_forbidden_message
7242 = G_("types may not be defined in casts");
7243 /* Consume the `('. */
7244 cp_lexer_consume_token (parser->lexer);
7245 /* A very tricky bit is that `(struct S) { 3 }' is a
7246 compound-literal (which we permit in C++ as an extension).
7247 But, that construct is not a cast-expression -- it is a
7248 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7249 is legal; if the compound-literal were a cast-expression,
7250 you'd need an extra set of parentheses.) But, if we parse
7251 the type-id, and it happens to be a class-specifier, then we
7252 will commit to the parse at that point, because we cannot
7253 undo the action that is done when creating a new class. So,
7254 then we cannot back up and do a postfix-expression.
7256 Therefore, we scan ahead to the closing `)', and check to see
7257 if the token after the `)' is a `{'. If so, we are not
7258 looking at a cast-expression.
7260 Save tokens so that we can put them back. */
7261 cp_lexer_save_tokens (parser->lexer);
7262 /* Skip tokens until the next token is a closing parenthesis.
7263 If we find the closing `)', and the next token is a `{', then
7264 we are looking at a compound-literal. */
7266 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7267 /*consume_paren=*/true)
7268 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7269 /* Roll back the tokens we skipped. */
7270 cp_lexer_rollback_tokens (parser->lexer);
7271 /* If we were looking at a compound-literal, simulate an error
7272 so that the call to cp_parser_parse_definitely below will
7274 if (compound_literal_p)
7275 cp_parser_simulate_error (parser);
7278 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7279 parser->in_type_id_in_expr_p = true;
7280 /* Look for the type-id. */
7281 type = cp_parser_type_id (parser);
7282 /* Look for the closing `)'. */
7283 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7284 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7287 /* Restore the saved message. */
7288 parser->type_definition_forbidden_message = saved_message;
7290 /* At this point this can only be either a cast or a
7291 parenthesized ctor such as `(T ())' that looks like a cast to
7292 function returning T. */
7293 if (!cp_parser_error_occurred (parser)
7294 && cp_parser_tokens_start_cast_expression (parser))
7296 cp_parser_parse_definitely (parser);
7297 expr = cp_parser_cast_expression (parser,
7298 /*address_p=*/false,
7300 /*decltype_p=*/false,
7303 /* Warn about old-style casts, if so requested. */
7304 if (warn_old_style_cast
7305 && !in_system_header
7306 && !VOID_TYPE_P (type)
7307 && current_lang_name != lang_name_c)
7308 warning (OPT_Wold_style_cast, "use of old-style cast");
7310 /* Only type conversions to integral or enumeration types
7311 can be used in constant-expressions. */
7312 if (!cast_valid_in_integral_constant_expression_p (type)
7313 && cp_parser_non_integral_constant_expression (parser,
7315 return error_mark_node;
7317 /* Perform the cast. */
7318 expr = build_c_cast (input_location, type, expr);
7322 cp_parser_abort_tentative_parse (parser);
7325 /* If we get here, then it's not a cast, so it must be a
7326 unary-expression. */
7327 return cp_parser_unary_expression (parser, address_p, cast_p,
7331 /* Parse a binary expression of the general form:
7335 pm-expression .* cast-expression
7336 pm-expression ->* cast-expression
7338 multiplicative-expression:
7340 multiplicative-expression * pm-expression
7341 multiplicative-expression / pm-expression
7342 multiplicative-expression % pm-expression
7344 additive-expression:
7345 multiplicative-expression
7346 additive-expression + multiplicative-expression
7347 additive-expression - multiplicative-expression
7351 shift-expression << additive-expression
7352 shift-expression >> additive-expression
7354 relational-expression:
7356 relational-expression < shift-expression
7357 relational-expression > shift-expression
7358 relational-expression <= shift-expression
7359 relational-expression >= shift-expression
7363 relational-expression:
7364 relational-expression <? shift-expression
7365 relational-expression >? shift-expression
7367 equality-expression:
7368 relational-expression
7369 equality-expression == relational-expression
7370 equality-expression != relational-expression
7374 and-expression & equality-expression
7376 exclusive-or-expression:
7378 exclusive-or-expression ^ and-expression
7380 inclusive-or-expression:
7381 exclusive-or-expression
7382 inclusive-or-expression | exclusive-or-expression
7384 logical-and-expression:
7385 inclusive-or-expression
7386 logical-and-expression && inclusive-or-expression
7388 logical-or-expression:
7389 logical-and-expression
7390 logical-or-expression || logical-and-expression
7392 All these are implemented with a single function like:
7395 simple-cast-expression
7396 binary-expression <token> binary-expression
7398 CAST_P is true if this expression is the target of a cast.
7400 The binops_by_token map is used to get the tree codes for each <token> type.
7401 binary-expressions are associated according to a precedence table. */
7403 #define TOKEN_PRECEDENCE(token) \
7404 (((token->type == CPP_GREATER \
7405 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7406 && !parser->greater_than_is_operator_p) \
7407 ? PREC_NOT_OPERATOR \
7408 : binops_by_token[token->type].prec)
7411 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7412 bool no_toplevel_fold_p,
7414 enum cp_parser_prec prec,
7417 cp_parser_expression_stack stack;
7418 cp_parser_expression_stack_entry *sp = &stack[0];
7419 cp_parser_expression_stack_entry current;
7422 enum tree_code rhs_type;
7423 enum cp_parser_prec new_prec, lookahead_prec;
7426 /* Parse the first expression. */
7427 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7428 cast_p, decltype_p, pidk);
7429 current.lhs_type = ERROR_MARK;
7430 current.prec = prec;
7432 if (cp_parser_error_occurred (parser))
7433 return error_mark_node;
7437 /* Get an operator token. */
7438 token = cp_lexer_peek_token (parser->lexer);
7440 if (warn_cxx0x_compat
7441 && token->type == CPP_RSHIFT
7442 && !parser->greater_than_is_operator_p)
7444 if (warning_at (token->location, OPT_Wc__0x_compat,
7445 "%<>>%> operator is treated"
7446 " as two right angle brackets in C++11"))
7447 inform (token->location,
7448 "suggest parentheses around %<>>%> expression");
7451 new_prec = TOKEN_PRECEDENCE (token);
7453 /* Popping an entry off the stack means we completed a subexpression:
7454 - either we found a token which is not an operator (`>' where it is not
7455 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7456 will happen repeatedly;
7457 - or, we found an operator which has lower priority. This is the case
7458 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7460 if (new_prec <= current.prec)
7469 current.tree_type = binops_by_token[token->type].tree_type;
7470 current.loc = token->location;
7472 /* We used the operator token. */
7473 cp_lexer_consume_token (parser->lexer);
7475 /* For "false && x" or "true || x", x will never be executed;
7476 disable warnings while evaluating it. */
7477 if (current.tree_type == TRUTH_ANDIF_EXPR)
7478 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7479 else if (current.tree_type == TRUTH_ORIF_EXPR)
7480 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7482 /* Extract another operand. It may be the RHS of this expression
7483 or the LHS of a new, higher priority expression. */
7484 rhs = cp_parser_simple_cast_expression (parser);
7485 rhs_type = ERROR_MARK;
7487 /* Get another operator token. Look up its precedence to avoid
7488 building a useless (immediately popped) stack entry for common
7489 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7490 token = cp_lexer_peek_token (parser->lexer);
7491 lookahead_prec = TOKEN_PRECEDENCE (token);
7492 if (lookahead_prec > new_prec)
7494 /* ... and prepare to parse the RHS of the new, higher priority
7495 expression. Since precedence levels on the stack are
7496 monotonically increasing, we do not have to care about
7501 current.lhs_type = rhs_type;
7502 current.prec = new_prec;
7503 new_prec = lookahead_prec;
7507 lookahead_prec = new_prec;
7508 /* If the stack is not empty, we have parsed into LHS the right side
7509 (`4' in the example above) of an expression we had suspended.
7510 We can use the information on the stack to recover the LHS (`3')
7511 from the stack together with the tree code (`MULT_EXPR'), and
7512 the precedence of the higher level subexpression
7513 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7514 which will be used to actually build the additive expression. */
7516 rhs_type = current.lhs_type;
7521 /* Undo the disabling of warnings done above. */
7522 if (current.tree_type == TRUTH_ANDIF_EXPR)
7523 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7524 else if (current.tree_type == TRUTH_ORIF_EXPR)
7525 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7528 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7529 ERROR_MARK for everything that is not a binary expression.
7530 This makes warn_about_parentheses miss some warnings that
7531 involve unary operators. For unary expressions we should
7532 pass the correct tree_code unless the unary expression was
7533 surrounded by parentheses.
7535 if (no_toplevel_fold_p
7536 && lookahead_prec <= current.prec
7538 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7539 current.lhs = build2 (current.tree_type, boolean_type_node,
7542 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7543 current.lhs, current.lhs_type,
7544 rhs, rhs_type, &overload,
7545 complain_flags (decltype_p));
7546 current.lhs_type = current.tree_type;
7547 if (EXPR_P (current.lhs))
7548 SET_EXPR_LOCATION (current.lhs, current.loc);
7550 /* If the binary operator required the use of an overloaded operator,
7551 then this expression cannot be an integral constant-expression.
7552 An overloaded operator can be used even if both operands are
7553 otherwise permissible in an integral constant-expression if at
7554 least one of the operands is of enumeration type. */
7557 && cp_parser_non_integral_constant_expression (parser,
7559 return error_mark_node;
7566 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7567 bool no_toplevel_fold_p,
7568 enum cp_parser_prec prec,
7571 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7572 /*decltype*/false, prec, pidk);
7575 /* Parse the `? expression : assignment-expression' part of a
7576 conditional-expression. The LOGICAL_OR_EXPR is the
7577 logical-or-expression that started the conditional-expression.
7578 Returns a representation of the entire conditional-expression.
7580 This routine is used by cp_parser_assignment_expression.
7582 ? expression : assignment-expression
7586 ? : assignment-expression */
7589 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7592 tree assignment_expr;
7593 struct cp_token *token;
7594 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7596 /* Consume the `?' token. */
7597 cp_lexer_consume_token (parser->lexer);
7598 token = cp_lexer_peek_token (parser->lexer);
7599 if (cp_parser_allow_gnu_extensions_p (parser)
7600 && token->type == CPP_COLON)
7602 pedwarn (token->location, OPT_Wpedantic,
7603 "ISO C++ does not allow ?: with omitted middle operand");
7604 /* Implicit true clause. */
7606 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7607 warn_for_omitted_condop (token->location, logical_or_expr);
7611 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7612 parser->colon_corrects_to_scope_p = false;
7613 /* Parse the expression. */
7614 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7615 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7616 c_inhibit_evaluation_warnings +=
7617 ((logical_or_expr == truthvalue_true_node)
7618 - (logical_or_expr == truthvalue_false_node));
7619 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7622 /* The next token should be a `:'. */
7623 cp_parser_require (parser, CPP_COLON, RT_COLON);
7624 /* Parse the assignment-expression. */
7625 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7626 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7628 /* Build the conditional-expression. */
7629 return build_x_conditional_expr (loc, logical_or_expr,
7632 tf_warning_or_error);
7635 /* Parse an assignment-expression.
7637 assignment-expression:
7638 conditional-expression
7639 logical-or-expression assignment-operator assignment_expression
7642 CAST_P is true if this expression is the target of a cast.
7643 DECLTYPE_P is true if this expression is the operand of decltype.
7645 Returns a representation for the expression. */
7648 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7649 bool decltype_p, cp_id_kind * pidk)
7653 /* If the next token is the `throw' keyword, then we're looking at
7654 a throw-expression. */
7655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7656 expr = cp_parser_throw_expression (parser);
7657 /* Otherwise, it must be that we are looking at a
7658 logical-or-expression. */
7661 /* Parse the binary expressions (logical-or-expression). */
7662 expr = cp_parser_binary_expression (parser, cast_p, false,
7664 PREC_NOT_OPERATOR, pidk);
7665 /* If the next token is a `?' then we're actually looking at a
7666 conditional-expression. */
7667 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7668 return cp_parser_question_colon_clause (parser, expr);
7671 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7673 /* If it's an assignment-operator, we're using the second
7675 enum tree_code assignment_operator
7676 = cp_parser_assignment_operator_opt (parser);
7677 if (assignment_operator != ERROR_MARK)
7679 bool non_constant_p;
7680 location_t saved_input_location;
7682 /* Parse the right-hand side of the assignment. */
7683 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7685 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7686 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7688 /* An assignment may not appear in a
7689 constant-expression. */
7690 if (cp_parser_non_integral_constant_expression (parser,
7692 return error_mark_node;
7693 /* Build the assignment expression. Its default
7694 location is the location of the '=' token. */
7695 saved_input_location = input_location;
7696 input_location = loc;
7697 expr = build_x_modify_expr (loc, expr,
7698 assignment_operator,
7700 complain_flags (decltype_p));
7701 input_location = saved_input_location;
7710 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7713 return cp_parser_assignment_expression (parser, cast_p,
7714 /*decltype*/false, pidk);
7717 /* Parse an (optional) assignment-operator.
7719 assignment-operator: one of
7720 = *= /= %= += -= >>= <<= &= ^= |=
7724 assignment-operator: one of
7727 If the next token is an assignment operator, the corresponding tree
7728 code is returned, and the token is consumed. For example, for
7729 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7730 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7731 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7732 operator, ERROR_MARK is returned. */
7734 static enum tree_code
7735 cp_parser_assignment_operator_opt (cp_parser* parser)
7740 /* Peek at the next token. */
7741 token = cp_lexer_peek_token (parser->lexer);
7743 switch (token->type)
7754 op = TRUNC_DIV_EXPR;
7758 op = TRUNC_MOD_EXPR;
7790 /* Nothing else is an assignment operator. */
7794 /* If it was an assignment operator, consume it. */
7795 if (op != ERROR_MARK)
7796 cp_lexer_consume_token (parser->lexer);
7801 /* Parse an expression.
7804 assignment-expression
7805 expression , assignment-expression
7807 CAST_P is true if this expression is the target of a cast.
7808 DECLTYPE_P is true if this expression is the immediate operand of decltype,
7809 except possibly parenthesized or on the RHS of a comma (N3276).
7811 Returns a representation of the expression. */
7814 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
7817 tree expression = NULL_TREE;
7818 location_t loc = UNKNOWN_LOCATION;
7822 tree assignment_expression;
7824 /* Parse the next assignment-expression. */
7825 assignment_expression
7826 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
7828 /* We don't create a temporary for a call that is the immediate operand
7829 of decltype or on the RHS of a comma. But when we see a comma, we
7830 need to create a temporary for a call on the LHS. */
7831 if (decltype_p && !processing_template_decl
7832 && TREE_CODE (assignment_expression) == CALL_EXPR
7833 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
7834 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7835 assignment_expression
7836 = build_cplus_new (TREE_TYPE (assignment_expression),
7837 assignment_expression, tf_warning_or_error);
7839 /* If this is the first assignment-expression, we can just
7842 expression = assignment_expression;
7844 expression = build_x_compound_expr (loc, expression,
7845 assignment_expression,
7846 complain_flags (decltype_p));
7847 /* If the next token is not a comma, then we are done with the
7849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7851 /* Consume the `,'. */
7852 loc = cp_lexer_peek_token (parser->lexer)->location;
7853 cp_lexer_consume_token (parser->lexer);
7854 /* A comma operator cannot appear in a constant-expression. */
7855 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7856 expression = error_mark_node;
7863 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7865 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
7868 /* Parse a constant-expression.
7870 constant-expression:
7871 conditional-expression
7873 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7874 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7875 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7876 is false, NON_CONSTANT_P should be NULL. */
7879 cp_parser_constant_expression (cp_parser* parser,
7880 bool allow_non_constant_p,
7881 bool *non_constant_p)
7883 bool saved_integral_constant_expression_p;
7884 bool saved_allow_non_integral_constant_expression_p;
7885 bool saved_non_integral_constant_expression_p;
7888 /* It might seem that we could simply parse the
7889 conditional-expression, and then check to see if it were
7890 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7891 one that the compiler can figure out is constant, possibly after
7892 doing some simplifications or optimizations. The standard has a
7893 precise definition of constant-expression, and we must honor
7894 that, even though it is somewhat more restrictive.
7900 is not a legal declaration, because `(2, 3)' is not a
7901 constant-expression. The `,' operator is forbidden in a
7902 constant-expression. However, GCC's constant-folding machinery
7903 will fold this operation to an INTEGER_CST for `3'. */
7905 /* Save the old settings. */
7906 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7907 saved_allow_non_integral_constant_expression_p
7908 = parser->allow_non_integral_constant_expression_p;
7909 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7910 /* We are now parsing a constant-expression. */
7911 parser->integral_constant_expression_p = true;
7912 parser->allow_non_integral_constant_expression_p
7913 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7914 parser->non_integral_constant_expression_p = false;
7915 /* Although the grammar says "conditional-expression", we parse an
7916 "assignment-expression", which also permits "throw-expression"
7917 and the use of assignment operators. In the case that
7918 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7919 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7920 actually essential that we look for an assignment-expression.
7921 For example, cp_parser_initializer_clauses uses this function to
7922 determine whether a particular assignment-expression is in fact
7924 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7925 /* Restore the old settings. */
7926 parser->integral_constant_expression_p
7927 = saved_integral_constant_expression_p;
7928 parser->allow_non_integral_constant_expression_p
7929 = saved_allow_non_integral_constant_expression_p;
7930 if (cxx_dialect >= cxx0x)
7932 /* Require an rvalue constant expression here; that's what our
7933 callers expect. Reference constant expressions are handled
7934 separately in e.g. cp_parser_template_argument. */
7935 bool is_const = potential_rvalue_constant_expression (expression);
7936 parser->non_integral_constant_expression_p = !is_const;
7937 if (!is_const && !allow_non_constant_p)
7938 require_potential_rvalue_constant_expression (expression);
7940 if (allow_non_constant_p)
7941 *non_constant_p = parser->non_integral_constant_expression_p;
7942 parser->non_integral_constant_expression_p
7943 = saved_non_integral_constant_expression_p;
7948 /* Parse __builtin_offsetof.
7950 offsetof-expression:
7951 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7953 offsetof-member-designator:
7955 | offsetof-member-designator "." id-expression
7956 | offsetof-member-designator "[" expression "]"
7957 | offsetof-member-designator "->" id-expression */
7960 cp_parser_builtin_offsetof (cp_parser *parser)
7962 int save_ice_p, save_non_ice_p;
7967 /* We're about to accept non-integral-constant things, but will
7968 definitely yield an integral constant expression. Save and
7969 restore these values around our local parsing. */
7970 save_ice_p = parser->integral_constant_expression_p;
7971 save_non_ice_p = parser->non_integral_constant_expression_p;
7973 /* Consume the "__builtin_offsetof" token. */
7974 cp_lexer_consume_token (parser->lexer);
7975 /* Consume the opening `('. */
7976 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7977 /* Parse the type-id. */
7978 type = cp_parser_type_id (parser);
7979 /* Look for the `,'. */
7980 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7981 token = cp_lexer_peek_token (parser->lexer);
7983 /* Build the (type *)null that begins the traditional offsetof macro. */
7984 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7985 tf_warning_or_error);
7987 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7988 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7989 true, &dummy, token->location);
7992 token = cp_lexer_peek_token (parser->lexer);
7993 switch (token->type)
7995 case CPP_OPEN_SQUARE:
7996 /* offsetof-member-designator "[" expression "]" */
7997 expr = cp_parser_postfix_open_square_expression (parser, expr,
8002 /* offsetof-member-designator "->" identifier */
8003 expr = grok_array_decl (token->location, expr,
8004 integer_zero_node, false);
8008 /* offsetof-member-designator "." identifier */
8009 cp_lexer_consume_token (parser->lexer);
8010 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8015 case CPP_CLOSE_PAREN:
8016 /* Consume the ")" token. */
8017 cp_lexer_consume_token (parser->lexer);
8021 /* Error. We know the following require will fail, but
8022 that gives the proper error message. */
8023 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8024 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8025 expr = error_mark_node;
8031 /* If we're processing a template, we can't finish the semantics yet.
8032 Otherwise we can fold the entire expression now. */
8033 if (processing_template_decl)
8034 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8036 expr = finish_offsetof (expr);
8039 parser->integral_constant_expression_p = save_ice_p;
8040 parser->non_integral_constant_expression_p = save_non_ice_p;
8045 /* Parse a trait expression.
8047 Returns a representation of the expression, the underlying type
8048 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8051 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8054 tree type1, type2 = NULL_TREE;
8055 bool binary = false;
8056 cp_decl_specifier_seq decl_specs;
8060 case RID_HAS_NOTHROW_ASSIGN:
8061 kind = CPTK_HAS_NOTHROW_ASSIGN;
8063 case RID_HAS_NOTHROW_CONSTRUCTOR:
8064 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8066 case RID_HAS_NOTHROW_COPY:
8067 kind = CPTK_HAS_NOTHROW_COPY;
8069 case RID_HAS_TRIVIAL_ASSIGN:
8070 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8072 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8073 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8075 case RID_HAS_TRIVIAL_COPY:
8076 kind = CPTK_HAS_TRIVIAL_COPY;
8078 case RID_HAS_TRIVIAL_DESTRUCTOR:
8079 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8081 case RID_HAS_VIRTUAL_DESTRUCTOR:
8082 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8084 case RID_IS_ABSTRACT:
8085 kind = CPTK_IS_ABSTRACT;
8087 case RID_IS_BASE_OF:
8088 kind = CPTK_IS_BASE_OF;
8092 kind = CPTK_IS_CLASS;
8094 case RID_IS_CONVERTIBLE_TO:
8095 kind = CPTK_IS_CONVERTIBLE_TO;
8099 kind = CPTK_IS_EMPTY;
8102 kind = CPTK_IS_ENUM;
8105 kind = CPTK_IS_FINAL;
8107 case RID_IS_LITERAL_TYPE:
8108 kind = CPTK_IS_LITERAL_TYPE;
8113 case RID_IS_POLYMORPHIC:
8114 kind = CPTK_IS_POLYMORPHIC;
8116 case RID_IS_STD_LAYOUT:
8117 kind = CPTK_IS_STD_LAYOUT;
8119 case RID_IS_TRIVIAL:
8120 kind = CPTK_IS_TRIVIAL;
8123 kind = CPTK_IS_UNION;
8125 case RID_UNDERLYING_TYPE:
8126 kind = CPTK_UNDERLYING_TYPE;
8131 case RID_DIRECT_BASES:
8132 kind = CPTK_DIRECT_BASES;
8138 /* Consume the token. */
8139 cp_lexer_consume_token (parser->lexer);
8141 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8143 type1 = cp_parser_type_id (parser);
8145 if (type1 == error_mark_node)
8146 return error_mark_node;
8148 /* Build a trivial decl-specifier-seq. */
8149 clear_decl_specs (&decl_specs);
8150 decl_specs.type = type1;
8152 /* Call grokdeclarator to figure out what type this is. */
8153 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8154 /*initialized=*/0, /*attrlist=*/NULL);
8158 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8160 type2 = cp_parser_type_id (parser);
8162 if (type2 == error_mark_node)
8163 return error_mark_node;
8165 /* Build a trivial decl-specifier-seq. */
8166 clear_decl_specs (&decl_specs);
8167 decl_specs.type = type2;
8169 /* Call grokdeclarator to figure out what type this is. */
8170 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8171 /*initialized=*/0, /*attrlist=*/NULL);
8174 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8176 /* Complete the trait expression, which may mean either processing
8177 the trait expr now or saving it for template instantiation. */
8180 case CPTK_UNDERLYING_TYPE:
8181 return finish_underlying_type (type1);
8183 return finish_bases (type1, false);
8184 case CPTK_DIRECT_BASES:
8185 return finish_bases (type1, true);
8187 return finish_trait_expr (kind, type1, type2);
8191 /* Lambdas that appear in variable initializer or default argument scope
8192 get that in their mangling, so we need to record it. We might as well
8193 use the count for function and namespace scopes as well. */
8194 static GTY(()) tree lambda_scope;
8195 static GTY(()) int lambda_count;
8196 typedef struct GTY(()) tree_int
8201 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8204 start_lambda_scope (tree decl)
8208 /* Once we're inside a function, we ignore other scopes and just push
8209 the function again so that popping works properly. */
8210 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8211 decl = current_function_decl;
8212 ti.t = lambda_scope;
8213 ti.i = lambda_count;
8214 vec_safe_push (lambda_scope_stack, ti);
8215 if (lambda_scope != decl)
8217 /* Don't reset the count if we're still in the same function. */
8218 lambda_scope = decl;
8224 record_lambda_scope (tree lambda)
8226 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8227 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8231 finish_lambda_scope (void)
8233 tree_int *p = &lambda_scope_stack->last ();
8234 if (lambda_scope != p->t)
8236 lambda_scope = p->t;
8237 lambda_count = p->i;
8239 lambda_scope_stack->pop ();
8242 /* Parse a lambda expression.
8245 lambda-introducer lambda-declarator [opt] compound-statement
8247 Returns a representation of the expression. */
8250 cp_parser_lambda_expression (cp_parser* parser)
8252 tree lambda_expr = build_lambda_expr ();
8256 LAMBDA_EXPR_LOCATION (lambda_expr)
8257 = cp_lexer_peek_token (parser->lexer)->location;
8259 if (cp_unevaluated_operand)
8260 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8261 "lambda-expression in unevaluated context");
8263 /* We may be in the middle of deferred access check. Disable
8265 push_deferring_access_checks (dk_no_deferred);
8267 cp_parser_lambda_introducer (parser, lambda_expr);
8269 type = begin_lambda_type (lambda_expr);
8270 if (type == error_mark_node)
8271 return error_mark_node;
8273 record_lambda_scope (lambda_expr);
8275 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8276 determine_visibility (TYPE_NAME (type));
8278 /* Now that we've started the type, add the capture fields for any
8279 explicit captures. */
8280 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8283 /* Inside the class, surrounding template-parameter-lists do not apply. */
8284 unsigned int saved_num_template_parameter_lists
8285 = parser->num_template_parameter_lists;
8286 unsigned char in_statement = parser->in_statement;
8287 bool in_switch_statement_p = parser->in_switch_statement_p;
8289 parser->num_template_parameter_lists = 0;
8290 parser->in_statement = 0;
8291 parser->in_switch_statement_p = false;
8293 /* By virtue of defining a local class, a lambda expression has access to
8294 the private variables of enclosing classes. */
8296 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8299 cp_parser_lambda_body (parser, lambda_expr);
8300 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8301 cp_parser_skip_to_end_of_block_or_statement (parser);
8303 /* The capture list was built up in reverse order; fix that now. */
8305 tree newlist = NULL_TREE;
8308 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8311 next = TREE_CHAIN (elt);
8312 TREE_CHAIN (elt) = newlist;
8315 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8319 maybe_add_lambda_conv_op (type);
8321 type = finish_struct (type, /*attributes=*/NULL_TREE);
8323 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8324 parser->in_statement = in_statement;
8325 parser->in_switch_statement_p = in_switch_statement_p;
8328 pop_deferring_access_checks ();
8330 /* This field is only used during parsing of the lambda. */
8331 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8333 /* This lambda shouldn't have any proxies left at this point. */
8334 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8335 /* And now that we're done, push proxies for an enclosing lambda. */
8336 insert_pending_capture_proxies ();
8339 return build_lambda_object (lambda_expr);
8341 return error_mark_node;
8344 /* Parse the beginning of a lambda expression.
8347 [ lambda-capture [opt] ]
8349 LAMBDA_EXPR is the current representation of the lambda expression. */
8352 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8354 /* Need commas after the first capture. */
8357 /* Eat the leading `['. */
8358 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8360 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8361 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8362 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8363 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8364 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8365 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8367 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8369 cp_lexer_consume_token (parser->lexer);
8373 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8375 cp_token* capture_token;
8377 tree capture_init_expr;
8378 cp_id_kind idk = CP_ID_KIND_NONE;
8379 bool explicit_init_p = false;
8381 enum capture_kind_type
8386 enum capture_kind_type capture_kind = BY_COPY;
8388 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8390 error ("expected end of capture-list");
8397 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8399 /* Possibly capture `this'. */
8400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8402 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8403 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8404 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8405 "with by-copy capture default");
8406 cp_lexer_consume_token (parser->lexer);
8407 add_capture (lambda_expr,
8408 /*id=*/this_identifier,
8409 /*initializer=*/finish_this_expr(),
8410 /*by_reference_p=*/false,
8415 /* Remember whether we want to capture as a reference or not. */
8416 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8418 capture_kind = BY_REFERENCE;
8419 cp_lexer_consume_token (parser->lexer);
8422 /* Get the identifier. */
8423 capture_token = cp_lexer_peek_token (parser->lexer);
8424 capture_id = cp_parser_identifier (parser);
8426 if (capture_id == error_mark_node)
8427 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8428 delimiters, but I modified this to stop on unnested ']' as well. It
8429 was already changed to stop on unnested '}', so the
8430 "closing_parenthesis" name is no more misleading with my change. */
8432 cp_parser_skip_to_closing_parenthesis (parser,
8433 /*recovering=*/true,
8435 /*consume_paren=*/true);
8439 /* Find the initializer for this capture. */
8440 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8442 /* An explicit expression exists. */
8443 cp_lexer_consume_token (parser->lexer);
8444 pedwarn (input_location, OPT_Wpedantic,
8445 "ISO C++ does not allow initializers "
8446 "in lambda expression capture lists");
8447 capture_init_expr = cp_parser_assignment_expression (parser,
8450 explicit_init_p = true;
8454 const char* error_msg;
8456 /* Turn the identifier into an id-expression. */
8458 = cp_parser_lookup_name
8462 /*is_template=*/false,
8463 /*is_namespace=*/false,
8464 /*check_dependency=*/true,
8465 /*ambiguous_decls=*/NULL,
8466 capture_token->location);
8468 if (capture_init_expr == error_mark_node)
8470 unqualified_name_lookup_error (capture_id);
8473 else if (DECL_P (capture_init_expr)
8474 && (TREE_CODE (capture_init_expr) != VAR_DECL
8475 && TREE_CODE (capture_init_expr) != PARM_DECL))
8477 error_at (capture_token->location,
8478 "capture of non-variable %qD ",
8480 inform (0, "%q+#D declared here", capture_init_expr);
8483 if (TREE_CODE (capture_init_expr) == VAR_DECL
8484 && decl_storage_duration (capture_init_expr) != dk_auto)
8486 pedwarn (capture_token->location, 0, "capture of variable "
8487 "%qD with non-automatic storage duration",
8489 inform (0, "%q+#D declared here", capture_init_expr);
8494 = finish_id_expression
8499 /*integral_constant_expression_p=*/false,
8500 /*allow_non_integral_constant_expression_p=*/false,
8501 /*non_integral_constant_expression_p=*/NULL,
8502 /*template_p=*/false,
8504 /*address_p=*/false,
8505 /*template_arg_p=*/false,
8507 capture_token->location);
8510 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8511 && !explicit_init_p)
8513 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8514 && capture_kind == BY_COPY)
8515 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8516 "of %qD redundant with by-copy capture default",
8518 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8519 && capture_kind == BY_REFERENCE)
8520 pedwarn (capture_token->location, 0, "explicit by-reference "
8521 "capture of %qD redundant with by-reference capture "
8522 "default", capture_id);
8525 add_capture (lambda_expr,
8528 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8532 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8535 /* Parse the (optional) middle of a lambda expression.
8538 ( parameter-declaration-clause [opt] )
8539 attribute-specifier [opt]
8541 exception-specification [opt]
8542 lambda-return-type-clause [opt]
8544 LAMBDA_EXPR is the current representation of the lambda expression. */
8547 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8549 /* 5.1.1.4 of the standard says:
8550 If a lambda-expression does not include a lambda-declarator, it is as if
8551 the lambda-declarator were ().
8552 This means an empty parameter list, no attributes, and no exception
8554 tree param_list = void_list_node;
8555 tree attributes = NULL_TREE;
8556 tree exception_spec = NULL_TREE;
8559 /* The lambda-declarator is optional, but must begin with an opening
8560 parenthesis if present. */
8561 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8563 cp_lexer_consume_token (parser->lexer);
8565 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8567 /* Parse parameters. */
8568 param_list = cp_parser_parameter_declaration_clause (parser);
8570 /* Default arguments shall not be specified in the
8571 parameter-declaration-clause of a lambda-declarator. */
8572 for (t = param_list; t; t = TREE_CHAIN (t))
8573 if (TREE_PURPOSE (t))
8574 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8575 "default argument specified for lambda parameter");
8577 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8579 attributes = cp_parser_attributes_opt (parser);
8581 /* Parse optional `mutable' keyword. */
8582 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8584 cp_lexer_consume_token (parser->lexer);
8585 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8588 /* Parse optional exception specification. */
8589 exception_spec = cp_parser_exception_specification_opt (parser);
8591 /* Parse optional trailing return type. */
8592 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8594 cp_lexer_consume_token (parser->lexer);
8595 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8598 /* The function parameters must be in scope all the way until after the
8599 trailing-return-type in case of decltype. */
8600 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8601 pop_binding (DECL_NAME (t), t);
8606 /* Create the function call operator.
8608 Messing with declarators like this is no uglier than building up the
8609 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8612 cp_decl_specifier_seq return_type_specs;
8613 cp_declarator* declarator;
8618 clear_decl_specs (&return_type_specs);
8619 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8620 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8622 /* Maybe we will deduce the return type later. */
8623 return_type_specs.type = make_auto ();
8625 p = obstack_alloc (&declarator_obstack, 0);
8627 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8630 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8631 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8632 declarator = make_call_declarator (declarator, param_list, quals,
8633 VIRT_SPEC_UNSPECIFIED,
8636 /*late_return_type=*/NULL_TREE);
8637 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8639 fco = grokmethod (&return_type_specs,
8642 if (fco != error_mark_node)
8644 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8645 DECL_ARTIFICIAL (fco) = 1;
8646 /* Give the object parameter a different name. */
8647 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8650 finish_member_declaration (fco);
8652 obstack_free (&declarator_obstack, p);
8654 return (fco != error_mark_node);
8658 /* Parse the body of a lambda expression, which is simply
8662 but which requires special handling.
8663 LAMBDA_EXPR is the current representation of the lambda expression. */
8666 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8668 bool nested = (current_function_decl != NULL_TREE);
8669 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8671 push_function_context ();
8673 /* Still increment function_depth so that we don't GC in the
8674 middle of an expression. */
8676 /* Clear this in case we're in the middle of a default argument. */
8677 parser->local_variables_forbidden_p = false;
8679 /* Finish the function call operator
8681 + late_parsing_for_member
8682 + function_definition_after_declarator
8683 + ctor_initializer_opt_and_function_body */
8685 tree fco = lambda_function (lambda_expr);
8691 /* Let the front end know that we are going to be defining this
8693 start_preparsed_function (fco,
8695 SF_PRE_PARSED | SF_INCLASS_INLINE);
8697 start_lambda_scope (fco);
8698 body = begin_function_body ();
8700 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8703 /* Push the proxies for any explicit captures. */
8704 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8705 cap = TREE_CHAIN (cap))
8706 build_capture_proxy (TREE_PURPOSE (cap));
8708 compound_stmt = begin_compound_stmt (0);
8710 /* 5.1.1.4 of the standard says:
8711 If a lambda-expression does not include a trailing-return-type, it
8712 is as if the trailing-return-type denotes the following type:
8713 * if the compound-statement is of the form
8714 { return attribute-specifier [opt] expression ; }
8715 the type of the returned expression after lvalue-to-rvalue
8716 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8717 (_conv.array_ 4.2), and function-to-pointer conversion
8719 * otherwise, void. */
8721 /* In a lambda that has neither a lambda-return-type-clause
8722 nor a deducible form, errors should be reported for return statements
8723 in the body. Since we used void as the placeholder return type, parsing
8724 the body as usual will give such desired behavior. */
8725 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8726 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8727 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8729 tree expr = NULL_TREE;
8730 cp_id_kind idk = CP_ID_KIND_NONE;
8732 /* Parse tentatively in case there's more after the initial return
8734 cp_parser_parse_tentatively (parser);
8736 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8738 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8740 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8741 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8743 if (cp_parser_parse_definitely (parser))
8745 if (!processing_template_decl)
8746 apply_deduced_return_type (fco, lambda_return_type (expr));
8748 /* Will get error here if type not deduced yet. */
8749 finish_return_stmt (expr);
8757 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8758 cp_parser_label_declaration (parser);
8759 cp_parser_statement_seq_opt (parser, NULL_TREE);
8760 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8763 finish_compound_stmt (compound_stmt);
8766 finish_function_body (body);
8767 finish_lambda_scope ();
8769 /* Finish the function and generate code for it if necessary. */
8770 expand_or_defer_fn (finish_function (/*inline*/2));
8773 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8775 pop_function_context();
8780 /* Statements [gram.stmt.stmt] */
8782 /* Parse a statement.
8786 expression-statement
8791 declaration-statement
8798 attribute-specifier-seq (opt) expression-statement
8799 attribute-specifier-seq (opt) compound-statement
8800 attribute-specifier-seq (opt) selection-statement
8801 attribute-specifier-seq (opt) iteration-statement
8802 attribute-specifier-seq (opt) jump-statement
8803 declaration-statement
8804 attribute-specifier-seq (opt) try-block
8811 IN_COMPOUND is true when the statement is nested inside a
8812 cp_parser_compound_statement; this matters for certain pragmas.
8814 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8815 is a (possibly labeled) if statement which is not enclosed in braces
8816 and has an else clause. This is used to implement -Wparentheses. */
8819 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8820 bool in_compound, bool *if_p)
8822 tree statement, std_attrs = NULL_TREE;
8824 location_t statement_location, attrs_location;
8829 /* There is no statement yet. */
8830 statement = NULL_TREE;
8832 cp_lexer_save_tokens (parser->lexer);
8833 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8834 if (c_dialect_objc ())
8835 /* In obj-c++, seing '[[' might be the either the beginning of
8836 c++11 attributes, or a nested objc-message-expression. So
8837 let's parse the c++11 attributes tentatively. */
8838 cp_parser_parse_tentatively (parser);
8839 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8840 if (c_dialect_objc ())
8842 if (!cp_parser_parse_definitely (parser))
8843 std_attrs = NULL_TREE;
8846 /* Peek at the next token. */
8847 token = cp_lexer_peek_token (parser->lexer);
8848 /* Remember the location of the first token in the statement. */
8849 statement_location = token->location;
8850 /* If this is a keyword, then that will often determine what kind of
8851 statement we have. */
8852 if (token->type == CPP_KEYWORD)
8854 enum rid keyword = token->keyword;
8860 /* Looks like a labeled-statement with a case label.
8861 Parse the label, and then use tail recursion to parse
8863 cp_parser_label_for_labeled_statement (parser, std_attrs);
8868 statement = cp_parser_selection_statement (parser, if_p);
8874 statement = cp_parser_iteration_statement (parser);
8881 statement = cp_parser_jump_statement (parser);
8884 /* Objective-C++ exception-handling constructs. */
8887 case RID_AT_FINALLY:
8888 case RID_AT_SYNCHRONIZED:
8890 statement = cp_parser_objc_statement (parser);
8894 statement = cp_parser_try_block (parser);
8898 /* This must be a namespace alias definition. */
8899 cp_parser_declaration_statement (parser);
8902 case RID_TRANSACTION_ATOMIC:
8903 case RID_TRANSACTION_RELAXED:
8904 statement = cp_parser_transaction (parser, keyword);
8906 case RID_TRANSACTION_CANCEL:
8907 statement = cp_parser_transaction_cancel (parser);
8911 /* It might be a keyword like `int' that can start a
8912 declaration-statement. */
8916 else if (token->type == CPP_NAME)
8918 /* If the next token is a `:', then we are looking at a
8919 labeled-statement. */
8920 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8921 if (token->type == CPP_COLON)
8923 /* Looks like a labeled-statement with an ordinary label.
8924 Parse the label, and then use tail recursion to parse
8927 cp_parser_label_for_labeled_statement (parser, std_attrs);
8931 /* Anything that starts with a `{' must be a compound-statement. */
8932 else if (token->type == CPP_OPEN_BRACE)
8933 statement = cp_parser_compound_statement (parser, NULL, false, false);
8934 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8935 a statement all its own. */
8936 else if (token->type == CPP_PRAGMA)
8938 /* Only certain OpenMP pragmas are attached to statements, and thus
8939 are considered statements themselves. All others are not. In
8940 the context of a compound, accept the pragma as a "statement" and
8941 return so that we can check for a close brace. Otherwise we
8942 require a real statement and must go back and read one. */
8944 cp_parser_pragma (parser, pragma_compound);
8945 else if (!cp_parser_pragma (parser, pragma_stmt))
8949 else if (token->type == CPP_EOF)
8951 cp_parser_error (parser, "expected statement");
8955 /* Everything else must be a declaration-statement or an
8956 expression-statement. Try for the declaration-statement
8957 first, unless we are looking at a `;', in which case we know that
8958 we have an expression-statement. */
8961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8963 if (std_attrs != NULL_TREE)
8965 /* Attributes should be parsed as part of the the
8966 declaration, so let's un-parse them. */
8967 cp_lexer_rollback_tokens (parser->lexer);
8968 std_attrs = NULL_TREE;
8971 cp_parser_parse_tentatively (parser);
8972 /* Try to parse the declaration-statement. */
8973 cp_parser_declaration_statement (parser);
8974 /* If that worked, we're done. */
8975 if (cp_parser_parse_definitely (parser))
8978 /* Look for an expression-statement instead. */
8979 statement = cp_parser_expression_statement (parser, in_statement_expr);
8982 /* Set the line number for the statement. */
8983 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8984 SET_EXPR_LOCATION (statement, statement_location);
8986 /* Note that for now, we don't do anything with c++11 statements
8987 parsed at this level. */
8988 if (std_attrs != NULL_TREE)
8989 warning_at (attrs_location,
8991 "attributes at the beginning of statement are ignored");
8994 /* Parse the label for a labeled-statement, i.e.
8997 case constant-expression :
9001 case constant-expression ... constant-expression : statement
9003 When a label is parsed without errors, the label is added to the
9004 parse tree by the finish_* functions, so this function doesn't
9005 have to return the label. */
9008 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9011 tree label = NULL_TREE;
9012 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9014 /* The next token should be an identifier. */
9015 token = cp_lexer_peek_token (parser->lexer);
9016 if (token->type != CPP_NAME
9017 && token->type != CPP_KEYWORD)
9019 cp_parser_error (parser, "expected labeled-statement");
9023 parser->colon_corrects_to_scope_p = false;
9024 switch (token->keyword)
9031 /* Consume the `case' token. */
9032 cp_lexer_consume_token (parser->lexer);
9033 /* Parse the constant-expression. */
9034 expr = cp_parser_constant_expression (parser,
9035 /*allow_non_constant_p=*/false,
9038 ellipsis = cp_lexer_peek_token (parser->lexer);
9039 if (ellipsis->type == CPP_ELLIPSIS)
9041 /* Consume the `...' token. */
9042 cp_lexer_consume_token (parser->lexer);
9044 cp_parser_constant_expression (parser,
9045 /*allow_non_constant_p=*/false,
9047 /* We don't need to emit warnings here, as the common code
9048 will do this for us. */
9051 expr_hi = NULL_TREE;
9053 if (parser->in_switch_statement_p)
9054 finish_case_label (token->location, expr, expr_hi);
9056 error_at (token->location,
9057 "case label %qE not within a switch statement",
9063 /* Consume the `default' token. */
9064 cp_lexer_consume_token (parser->lexer);
9066 if (parser->in_switch_statement_p)
9067 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9069 error_at (token->location, "case label not within a switch statement");
9073 /* Anything else must be an ordinary label. */
9074 label = finish_label_stmt (cp_parser_identifier (parser));
9078 /* Require the `:' token. */
9079 cp_parser_require (parser, CPP_COLON, RT_COLON);
9081 /* An ordinary label may optionally be followed by attributes.
9082 However, this is only permitted if the attributes are then
9083 followed by a semicolon. This is because, for backward
9084 compatibility, when parsing
9085 lab: __attribute__ ((unused)) int i;
9086 we want the attribute to attach to "i", not "lab". */
9087 if (label != NULL_TREE
9088 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9091 cp_parser_parse_tentatively (parser);
9092 attrs = cp_parser_gnu_attributes_opt (parser);
9093 if (attrs == NULL_TREE
9094 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9095 cp_parser_abort_tentative_parse (parser);
9096 else if (!cp_parser_parse_definitely (parser))
9099 attributes = chainon (attributes, attrs);
9102 if (attributes != NULL_TREE)
9103 cplus_decl_attributes (&label, attributes, 0);
9105 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9108 /* Parse an expression-statement.
9110 expression-statement:
9113 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9114 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9115 indicates whether this expression-statement is part of an
9116 expression statement. */
9119 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9121 tree statement = NULL_TREE;
9122 cp_token *token = cp_lexer_peek_token (parser->lexer);
9124 /* If the next token is a ';', then there is no expression
9126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9127 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9129 /* Give a helpful message for "A<T>::type t;" and the like. */
9130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9131 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9133 if (TREE_CODE (statement) == SCOPE_REF)
9134 error_at (token->location, "need %<typename%> before %qE because "
9135 "%qT is a dependent scope",
9136 statement, TREE_OPERAND (statement, 0));
9137 else if (is_overloaded_fn (statement)
9138 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9141 tree fn = get_first_fn (statement);
9142 error_at (token->location,
9143 "%<%T::%D%> names the constructor, not the type",
9144 DECL_CONTEXT (fn), DECL_NAME (fn));
9148 /* Consume the final `;'. */
9149 cp_parser_consume_semicolon_at_end_of_statement (parser);
9151 if (in_statement_expr
9152 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9153 /* This is the final expression statement of a statement
9155 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9157 statement = finish_expr_stmt (statement);
9164 /* Parse a compound-statement.
9167 { statement-seq [opt] }
9172 { label-declaration-seq [opt] statement-seq [opt] }
9174 label-declaration-seq:
9176 label-declaration-seq label-declaration
9178 Returns a tree representing the statement. */
9181 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9182 bool in_try, bool function_body)
9186 /* Consume the `{'. */
9187 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9188 return error_mark_node;
9189 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9191 pedwarn (input_location, OPT_Wpedantic,
9192 "compound-statement in constexpr function");
9193 /* Begin the compound-statement. */
9194 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9195 /* If the next keyword is `__label__' we have a label declaration. */
9196 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9197 cp_parser_label_declaration (parser);
9198 /* Parse an (optional) statement-seq. */
9199 cp_parser_statement_seq_opt (parser, in_statement_expr);
9200 /* Finish the compound-statement. */
9201 finish_compound_stmt (compound_stmt);
9202 /* Consume the `}'. */
9203 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9205 return compound_stmt;
9208 /* Parse an (optional) statement-seq.
9212 statement-seq [opt] statement */
9215 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9217 /* Scan statements until there aren't any more. */
9220 cp_token *token = cp_lexer_peek_token (parser->lexer);
9222 /* If we are looking at a `}', then we have run out of
9223 statements; the same is true if we have reached the end
9224 of file, or have stumbled upon a stray '@end'. */
9225 if (token->type == CPP_CLOSE_BRACE
9226 || token->type == CPP_EOF
9227 || token->type == CPP_PRAGMA_EOL
9228 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9231 /* If we are in a compound statement and find 'else' then
9232 something went wrong. */
9233 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9235 if (parser->in_statement & IN_IF_STMT)
9239 token = cp_lexer_consume_token (parser->lexer);
9240 error_at (token->location, "%<else%> without a previous %<if%>");
9244 /* Parse the statement. */
9245 cp_parser_statement (parser, in_statement_expr, true, NULL);
9249 /* Parse a selection-statement.
9251 selection-statement:
9252 if ( condition ) statement
9253 if ( condition ) statement else statement
9254 switch ( condition ) statement
9256 Returns the new IF_STMT or SWITCH_STMT.
9258 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9259 is a (possibly labeled) if statement which is not enclosed in
9260 braces and has an else clause. This is used to implement
9264 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9272 /* Peek at the next token. */
9273 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9275 /* See what kind of keyword it is. */
9276 keyword = token->keyword;
9285 /* Look for the `('. */
9286 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9288 cp_parser_skip_to_end_of_statement (parser);
9289 return error_mark_node;
9292 /* Begin the selection-statement. */
9293 if (keyword == RID_IF)
9294 statement = begin_if_stmt ();
9296 statement = begin_switch_stmt ();
9298 /* Parse the condition. */
9299 condition = cp_parser_condition (parser);
9300 /* Look for the `)'. */
9301 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9302 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9303 /*consume_paren=*/true);
9305 if (keyword == RID_IF)
9308 unsigned char in_statement;
9310 /* Add the condition. */
9311 finish_if_stmt_cond (condition, statement);
9313 /* Parse the then-clause. */
9314 in_statement = parser->in_statement;
9315 parser->in_statement |= IN_IF_STMT;
9316 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9318 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9319 add_stmt (build_empty_stmt (loc));
9320 cp_lexer_consume_token (parser->lexer);
9321 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9322 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9323 "empty body in an %<if%> statement");
9327 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9328 parser->in_statement = in_statement;
9330 finish_then_clause (statement);
9332 /* If the next token is `else', parse the else-clause. */
9333 if (cp_lexer_next_token_is_keyword (parser->lexer,
9336 /* Consume the `else' keyword. */
9337 cp_lexer_consume_token (parser->lexer);
9338 begin_else_clause (statement);
9339 /* Parse the else-clause. */
9340 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9343 loc = cp_lexer_peek_token (parser->lexer)->location;
9345 OPT_Wempty_body, "suggest braces around "
9346 "empty body in an %<else%> statement");
9347 add_stmt (build_empty_stmt (loc));
9348 cp_lexer_consume_token (parser->lexer);
9351 cp_parser_implicitly_scoped_statement (parser, NULL);
9353 finish_else_clause (statement);
9355 /* If we are currently parsing a then-clause, then
9356 IF_P will not be NULL. We set it to true to
9357 indicate that this if statement has an else clause.
9358 This may trigger the Wparentheses warning below
9359 when we get back up to the parent if statement. */
9365 /* This if statement does not have an else clause. If
9366 NESTED_IF is true, then the then-clause is an if
9367 statement which does have an else clause. We warn
9368 about the potential ambiguity. */
9370 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9371 "suggest explicit braces to avoid ambiguous"
9375 /* Now we're all done with the if-statement. */
9376 finish_if_stmt (statement);
9380 bool in_switch_statement_p;
9381 unsigned char in_statement;
9383 /* Add the condition. */
9384 finish_switch_cond (condition, statement);
9386 /* Parse the body of the switch-statement. */
9387 in_switch_statement_p = parser->in_switch_statement_p;
9388 in_statement = parser->in_statement;
9389 parser->in_switch_statement_p = true;
9390 parser->in_statement |= IN_SWITCH_STMT;
9391 cp_parser_implicitly_scoped_statement (parser, NULL);
9392 parser->in_switch_statement_p = in_switch_statement_p;
9393 parser->in_statement = in_statement;
9395 /* Now we're all done with the switch-statement. */
9396 finish_switch_stmt (statement);
9404 cp_parser_error (parser, "expected selection-statement");
9405 return error_mark_node;
9409 /* Parse a condition.
9413 type-specifier-seq declarator = initializer-clause
9414 type-specifier-seq declarator braced-init-list
9419 type-specifier-seq declarator asm-specification [opt]
9420 attributes [opt] = assignment-expression
9422 Returns the expression that should be tested. */
9425 cp_parser_condition (cp_parser* parser)
9427 cp_decl_specifier_seq type_specifiers;
9428 const char *saved_message;
9429 int declares_class_or_enum;
9431 /* Try the declaration first. */
9432 cp_parser_parse_tentatively (parser);
9433 /* New types are not allowed in the type-specifier-seq for a
9435 saved_message = parser->type_definition_forbidden_message;
9436 parser->type_definition_forbidden_message
9437 = G_("types may not be defined in conditions");
9438 /* Parse the type-specifier-seq. */
9439 cp_parser_decl_specifier_seq (parser,
9440 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9442 &declares_class_or_enum);
9443 /* Restore the saved message. */
9444 parser->type_definition_forbidden_message = saved_message;
9445 /* If all is well, we might be looking at a declaration. */
9446 if (!cp_parser_error_occurred (parser))
9449 tree asm_specification;
9451 cp_declarator *declarator;
9452 tree initializer = NULL_TREE;
9454 /* Parse the declarator. */
9455 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9456 /*ctor_dtor_or_conv_p=*/NULL,
9457 /*parenthesized_p=*/NULL,
9458 /*member_p=*/false);
9459 /* Parse the attributes. */
9460 attributes = cp_parser_attributes_opt (parser);
9461 /* Parse the asm-specification. */
9462 asm_specification = cp_parser_asm_specification_opt (parser);
9463 /* If the next token is not an `=' or '{', then we might still be
9464 looking at an expression. For example:
9468 looks like a decl-specifier-seq and a declarator -- but then
9469 there is no `=', so this is an expression. */
9470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9471 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9472 cp_parser_simulate_error (parser);
9474 /* If we did see an `=' or '{', then we are looking at a declaration
9476 if (cp_parser_parse_definitely (parser))
9479 bool non_constant_p;
9480 bool flags = LOOKUP_ONLYCONVERTING;
9482 /* Create the declaration. */
9483 decl = start_decl (declarator, &type_specifiers,
9484 /*initialized_p=*/true,
9485 attributes, /*prefix_attributes=*/NULL_TREE,
9488 /* Parse the initializer. */
9489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9491 initializer = cp_parser_braced_list (parser, &non_constant_p);
9492 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9497 /* Consume the `='. */
9498 cp_parser_require (parser, CPP_EQ, RT_EQ);
9499 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9501 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9502 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9504 /* Process the initializer. */
9505 cp_finish_decl (decl,
9506 initializer, !non_constant_p,
9511 pop_scope (pushed_scope);
9513 return convert_from_reference (decl);
9516 /* If we didn't even get past the declarator successfully, we are
9517 definitely not looking at a declaration. */
9519 cp_parser_abort_tentative_parse (parser);
9521 /* Otherwise, we are looking at an expression. */
9522 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9525 /* Parses a for-statement or range-for-statement until the closing ')',
9529 cp_parser_for (cp_parser *parser)
9531 tree init, scope, decl;
9534 /* Begin the for-statement. */
9535 scope = begin_for_scope (&init);
9537 /* Parse the initialization. */
9538 is_range_for = cp_parser_for_init_statement (parser, &decl);
9541 return cp_parser_range_for (parser, scope, init, decl);
9543 return cp_parser_c_for (parser, scope, init);
9547 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9549 /* Normal for loop */
9550 tree condition = NULL_TREE;
9551 tree expression = NULL_TREE;
9554 stmt = begin_for_stmt (scope, init);
9555 /* The for-init-statement has already been parsed in
9556 cp_parser_for_init_statement, so no work is needed here. */
9557 finish_for_init_stmt (stmt);
9559 /* If there's a condition, process it. */
9560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9561 condition = cp_parser_condition (parser);
9562 finish_for_cond (condition, stmt);
9563 /* Look for the `;'. */
9564 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9566 /* If there's an expression, process it. */
9567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9568 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9569 finish_for_expr (expression, stmt);
9574 /* Tries to parse a range-based for-statement:
9577 decl-specifier-seq declarator : expression
9579 The decl-specifier-seq declarator and the `:' are already parsed by
9580 cp_parser_for_init_statement. If processing_template_decl it returns a
9581 newly created RANGE_FOR_STMT; if not, it is converted to a
9582 regular FOR_STMT. */
9585 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9587 tree stmt, range_expr;
9589 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9591 bool expr_non_constant_p;
9592 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9595 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9597 /* If in template, STMT is converted to a normal for-statement
9598 at instantiation. If not, it is done just ahead. */
9599 if (processing_template_decl)
9601 if (check_for_bare_parameter_packs (range_expr))
9602 range_expr = error_mark_node;
9603 stmt = begin_range_for_stmt (scope, init);
9604 finish_range_for_decl (stmt, range_decl, range_expr);
9605 if (!type_dependent_expression_p (range_expr)
9606 /* do_auto_deduction doesn't mess with template init-lists. */
9607 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9608 do_range_for_auto_deduction (range_decl, range_expr);
9612 stmt = begin_for_stmt (scope, init);
9613 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9618 /* Subroutine of cp_convert_range_for: given the initializer expression,
9619 builds up the range temporary. */
9622 build_range_temp (tree range_expr)
9624 tree range_type, range_temp;
9626 /* Find out the type deduced by the declaration
9627 `auto &&__range = range_expr'. */
9628 range_type = cp_build_reference_type (make_auto (), true);
9629 range_type = do_auto_deduction (range_type, range_expr,
9630 type_uses_auto (range_type));
9632 /* Create the __range variable. */
9633 range_temp = build_decl (input_location, VAR_DECL,
9634 get_identifier ("__for_range"), range_type);
9635 TREE_USED (range_temp) = 1;
9636 DECL_ARTIFICIAL (range_temp) = 1;
9641 /* Used by cp_parser_range_for in template context: we aren't going to
9642 do a full conversion yet, but we still need to resolve auto in the
9643 type of the for-range-declaration if present. This is basically
9644 a shortcut version of cp_convert_range_for. */
9647 do_range_for_auto_deduction (tree decl, tree range_expr)
9649 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9652 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9653 range_temp = convert_from_reference (build_range_temp (range_expr));
9654 iter_type = (cp_parser_perform_range_for_lookup
9655 (range_temp, &begin_dummy, &end_dummy));
9656 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9657 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9658 tf_warning_or_error);
9659 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9660 iter_decl, auto_node);
9664 /* Converts a range-based for-statement into a normal
9665 for-statement, as per the definition.
9667 for (RANGE_DECL : RANGE_EXPR)
9670 should be equivalent to:
9673 auto &&__range = RANGE_EXPR;
9674 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9678 RANGE_DECL = *__begin;
9683 If RANGE_EXPR is an array:
9684 BEGIN_EXPR = __range
9685 END_EXPR = __range + ARRAY_SIZE(__range)
9686 Else if RANGE_EXPR has a member 'begin' or 'end':
9687 BEGIN_EXPR = __range.begin()
9688 END_EXPR = __range.end()
9690 BEGIN_EXPR = begin(__range)
9691 END_EXPR = end(__range);
9693 If __range has a member 'begin' but not 'end', or vice versa, we must
9694 still use the second alternative (it will surely fail, however).
9695 When calling begin()/end() in the third alternative we must use
9696 argument dependent lookup, but always considering 'std' as an associated
9700 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9703 tree iter_type, begin_expr, end_expr;
9704 tree condition, expression;
9706 if (range_decl == error_mark_node || range_expr == error_mark_node)
9707 /* If an error happened previously do nothing or else a lot of
9708 unhelpful errors would be issued. */
9709 begin_expr = end_expr = iter_type = error_mark_node;
9712 tree range_temp = build_range_temp (range_expr);
9713 pushdecl (range_temp);
9714 cp_finish_decl (range_temp, range_expr,
9715 /*is_constant_init*/false, NULL_TREE,
9716 LOOKUP_ONLYCONVERTING);
9718 range_temp = convert_from_reference (range_temp);
9719 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9720 &begin_expr, &end_expr);
9723 /* The new for initialization statement. */
9724 begin = build_decl (input_location, VAR_DECL,
9725 get_identifier ("__for_begin"), iter_type);
9726 TREE_USED (begin) = 1;
9727 DECL_ARTIFICIAL (begin) = 1;
9729 cp_finish_decl (begin, begin_expr,
9730 /*is_constant_init*/false, NULL_TREE,
9731 LOOKUP_ONLYCONVERTING);
9733 end = build_decl (input_location, VAR_DECL,
9734 get_identifier ("__for_end"), iter_type);
9735 TREE_USED (end) = 1;
9736 DECL_ARTIFICIAL (end) = 1;
9738 cp_finish_decl (end, end_expr,
9739 /*is_constant_init*/false, NULL_TREE,
9740 LOOKUP_ONLYCONVERTING);
9742 finish_for_init_stmt (statement);
9744 /* The new for condition. */
9745 condition = build_x_binary_op (input_location, NE_EXPR,
9748 NULL, tf_warning_or_error);
9749 finish_for_cond (condition, statement);
9751 /* The new increment expression. */
9752 expression = finish_unary_op_expr (input_location,
9753 PREINCREMENT_EXPR, begin,
9754 tf_warning_or_error);
9755 finish_for_expr (expression, statement);
9757 /* The declaration is initialized with *__begin inside the loop body. */
9758 cp_finish_decl (range_decl,
9759 build_x_indirect_ref (input_location, begin, RO_NULL,
9760 tf_warning_or_error),
9761 /*is_constant_init*/false, NULL_TREE,
9762 LOOKUP_ONLYCONVERTING);
9767 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9768 We need to solve both at the same time because the method used
9769 depends on the existence of members begin or end.
9770 Returns the type deduced for the iterator expression. */
9773 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9775 if (error_operand_p (range))
9777 *begin = *end = error_mark_node;
9778 return error_mark_node;
9781 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9783 error ("range-based %<for%> expression of type %qT "
9784 "has incomplete type", TREE_TYPE (range));
9785 *begin = *end = error_mark_node;
9786 return error_mark_node;
9788 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9790 /* If RANGE is an array, we will use pointer arithmetic. */
9792 *end = build_binary_op (input_location, PLUS_EXPR,
9794 array_type_nelts_top (TREE_TYPE (range)),
9796 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9800 /* If it is not an array, we must do a bit of magic. */
9801 tree id_begin, id_end;
9802 tree member_begin, member_end;
9804 *begin = *end = error_mark_node;
9806 id_begin = get_identifier ("begin");
9807 id_end = get_identifier ("end");
9808 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9809 /*protect=*/2, /*want_type=*/false,
9810 tf_warning_or_error);
9811 member_end = lookup_member (TREE_TYPE (range), id_end,
9812 /*protect=*/2, /*want_type=*/false,
9813 tf_warning_or_error);
9815 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9817 /* Use the member functions. */
9818 if (member_begin != NULL_TREE)
9819 *begin = cp_parser_range_for_member_function (range, id_begin);
9821 error ("range-based %<for%> expression of type %qT has an "
9822 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9824 if (member_end != NULL_TREE)
9825 *end = cp_parser_range_for_member_function (range, id_end);
9827 error ("range-based %<for%> expression of type %qT has a "
9828 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9832 /* Use global functions with ADL. */
9833 vec<tree, va_gc> *vec;
9834 vec = make_tree_vector ();
9836 vec_safe_push (vec, range);
9838 member_begin = perform_koenig_lookup (id_begin, vec,
9839 /*include_std=*/true,
9840 tf_warning_or_error);
9841 *begin = finish_call_expr (member_begin, &vec, false, true,
9842 tf_warning_or_error);
9843 member_end = perform_koenig_lookup (id_end, vec,
9844 /*include_std=*/true,
9845 tf_warning_or_error);
9846 *end = finish_call_expr (member_end, &vec, false, true,
9847 tf_warning_or_error);
9849 release_tree_vector (vec);
9852 /* Last common checks. */
9853 if (*begin == error_mark_node || *end == error_mark_node)
9855 /* If one of the expressions is an error do no more checks. */
9856 *begin = *end = error_mark_node;
9857 return error_mark_node;
9861 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9862 /* The unqualified type of the __begin and __end temporaries should
9863 be the same, as required by the multiple auto declaration. */
9864 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9865 error ("inconsistent begin/end types in range-based %<for%> "
9866 "statement: %qT and %qT",
9867 TREE_TYPE (*begin), TREE_TYPE (*end));
9873 /* Helper function for cp_parser_perform_range_for_lookup.
9874 Builds a tree for RANGE.IDENTIFIER(). */
9877 cp_parser_range_for_member_function (tree range, tree identifier)
9880 vec<tree, va_gc> *vec;
9882 member = finish_class_member_access_expr (range, identifier,
9883 false, tf_warning_or_error);
9884 if (member == error_mark_node)
9885 return error_mark_node;
9887 vec = make_tree_vector ();
9888 res = finish_call_expr (member, &vec,
9889 /*disallow_virtual=*/false,
9891 tf_warning_or_error);
9892 release_tree_vector (vec);
9896 /* Parse an iteration-statement.
9898 iteration-statement:
9899 while ( condition ) statement
9900 do statement while ( expression ) ;
9901 for ( for-init-statement condition [opt] ; expression [opt] )
9904 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9907 cp_parser_iteration_statement (cp_parser* parser)
9912 unsigned char in_statement;
9914 /* Peek at the next token. */
9915 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9917 return error_mark_node;
9919 /* Remember whether or not we are already within an iteration
9921 in_statement = parser->in_statement;
9923 /* See what kind of keyword it is. */
9924 keyword = token->keyword;
9931 /* Begin the while-statement. */
9932 statement = begin_while_stmt ();
9933 /* Look for the `('. */
9934 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9935 /* Parse the condition. */
9936 condition = cp_parser_condition (parser);
9937 finish_while_stmt_cond (condition, statement);
9938 /* Look for the `)'. */
9939 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9940 /* Parse the dependent statement. */
9941 parser->in_statement = IN_ITERATION_STMT;
9942 cp_parser_already_scoped_statement (parser);
9943 parser->in_statement = in_statement;
9944 /* We're done with the while-statement. */
9945 finish_while_stmt (statement);
9953 /* Begin the do-statement. */
9954 statement = begin_do_stmt ();
9955 /* Parse the body of the do-statement. */
9956 parser->in_statement = IN_ITERATION_STMT;
9957 cp_parser_implicitly_scoped_statement (parser, NULL);
9958 parser->in_statement = in_statement;
9959 finish_do_body (statement);
9960 /* Look for the `while' keyword. */
9961 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9962 /* Look for the `('. */
9963 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9964 /* Parse the expression. */
9965 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9966 /* We're done with the do-statement. */
9967 finish_do_stmt (expression, statement);
9968 /* Look for the `)'. */
9969 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9970 /* Look for the `;'. */
9971 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9977 /* Look for the `('. */
9978 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9980 statement = cp_parser_for (parser);
9982 /* Look for the `)'. */
9983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9985 /* Parse the body of the for-statement. */
9986 parser->in_statement = IN_ITERATION_STMT;
9987 cp_parser_already_scoped_statement (parser);
9988 parser->in_statement = in_statement;
9990 /* We're done with the for-statement. */
9991 finish_for_stmt (statement);
9996 cp_parser_error (parser, "expected iteration-statement");
9997 statement = error_mark_node;
10004 /* Parse a for-init-statement or the declarator of a range-based-for.
10005 Returns true if a range-based-for declaration is seen.
10007 for-init-statement:
10008 expression-statement
10009 simple-declaration */
10012 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10014 /* If the next token is a `;', then we have an empty
10015 expression-statement. Grammatically, this is also a
10016 simple-declaration, but an invalid one, because it does not
10017 declare anything. Therefore, if we did not handle this case
10018 specially, we would issue an error message about an invalid
10020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10022 bool is_range_for = false;
10023 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10025 parser->colon_corrects_to_scope_p = false;
10027 /* We're going to speculatively look for a declaration, falling back
10028 to an expression, if necessary. */
10029 cp_parser_parse_tentatively (parser);
10030 /* Parse the declaration. */
10031 cp_parser_simple_declaration (parser,
10032 /*function_definition_allowed_p=*/false,
10034 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10035 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10037 /* It is a range-for, consume the ':' */
10038 cp_lexer_consume_token (parser->lexer);
10039 is_range_for = true;
10040 if (cxx_dialect < cxx0x)
10042 error_at (cp_lexer_peek_token (parser->lexer)->location,
10043 "range-based %<for%> loops are not allowed "
10045 *decl = error_mark_node;
10049 /* The ';' is not consumed yet because we told
10050 cp_parser_simple_declaration not to. */
10051 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10053 if (cp_parser_parse_definitely (parser))
10054 return is_range_for;
10055 /* If the tentative parse failed, then we shall need to look for an
10056 expression-statement. */
10058 /* If we are here, it is an expression-statement. */
10059 cp_parser_expression_statement (parser, NULL_TREE);
10063 /* Parse a jump-statement.
10068 return expression [opt] ;
10069 return braced-init-list ;
10075 goto * expression ;
10077 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10080 cp_parser_jump_statement (cp_parser* parser)
10082 tree statement = error_mark_node;
10085 unsigned char in_statement;
10087 /* Peek at the next token. */
10088 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10090 return error_mark_node;
10092 /* See what kind of keyword it is. */
10093 keyword = token->keyword;
10097 in_statement = parser->in_statement & ~IN_IF_STMT;
10098 switch (in_statement)
10101 error_at (token->location, "break statement not within loop or switch");
10104 gcc_assert ((in_statement & IN_SWITCH_STMT)
10105 || in_statement == IN_ITERATION_STMT);
10106 statement = finish_break_stmt ();
10109 error_at (token->location, "invalid exit from OpenMP structured block");
10112 error_at (token->location, "break statement used with OpenMP for loop");
10115 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10119 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10122 error_at (token->location, "continue statement not within a loop");
10124 case IN_ITERATION_STMT:
10126 statement = finish_continue_stmt ();
10129 error_at (token->location, "invalid exit from OpenMP structured block");
10132 gcc_unreachable ();
10134 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10140 bool expr_non_constant_p;
10142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10144 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10145 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10147 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10148 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10150 /* If the next token is a `;', then there is no
10153 /* Build the return-statement. */
10154 statement = finish_return_stmt (expr);
10155 /* Look for the final `;'. */
10156 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10161 /* Create the goto-statement. */
10162 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10164 /* Issue a warning about this use of a GNU extension. */
10165 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10166 /* Consume the '*' token. */
10167 cp_lexer_consume_token (parser->lexer);
10168 /* Parse the dependent expression. */
10169 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10172 finish_goto_stmt (cp_parser_identifier (parser));
10173 /* Look for the final `;'. */
10174 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10178 cp_parser_error (parser, "expected jump-statement");
10185 /* Parse a declaration-statement.
10187 declaration-statement:
10188 block-declaration */
10191 cp_parser_declaration_statement (cp_parser* parser)
10195 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10196 p = obstack_alloc (&declarator_obstack, 0);
10198 /* Parse the block-declaration. */
10199 cp_parser_block_declaration (parser, /*statement_p=*/true);
10201 /* Free any declarators allocated. */
10202 obstack_free (&declarator_obstack, p);
10204 /* Finish off the statement. */
10208 /* Some dependent statements (like `if (cond) statement'), are
10209 implicitly in their own scope. In other words, if the statement is
10210 a single statement (as opposed to a compound-statement), it is
10211 none-the-less treated as if it were enclosed in braces. Any
10212 declarations appearing in the dependent statement are out of scope
10213 after control passes that point. This function parses a statement,
10214 but ensures that is in its own scope, even if it is not a
10215 compound-statement.
10217 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10218 is a (possibly labeled) if statement which is not enclosed in
10219 braces and has an else clause. This is used to implement
10222 Returns the new statement. */
10225 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10232 /* Mark if () ; with a special NOP_EXPR. */
10233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10235 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10236 cp_lexer_consume_token (parser->lexer);
10237 statement = add_stmt (build_empty_stmt (loc));
10239 /* if a compound is opened, we simply parse the statement directly. */
10240 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10241 statement = cp_parser_compound_statement (parser, NULL, false, false);
10242 /* If the token is not a `{', then we must take special action. */
10245 /* Create a compound-statement. */
10246 statement = begin_compound_stmt (0);
10247 /* Parse the dependent-statement. */
10248 cp_parser_statement (parser, NULL_TREE, false, if_p);
10249 /* Finish the dummy compound-statement. */
10250 finish_compound_stmt (statement);
10253 /* Return the statement. */
10257 /* For some dependent statements (like `while (cond) statement'), we
10258 have already created a scope. Therefore, even if the dependent
10259 statement is a compound-statement, we do not want to create another
10263 cp_parser_already_scoped_statement (cp_parser* parser)
10265 /* If the token is a `{', then we must take special action. */
10266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10267 cp_parser_statement (parser, NULL_TREE, false, NULL);
10270 /* Avoid calling cp_parser_compound_statement, so that we
10271 don't create a new scope. Do everything else by hand. */
10272 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10273 /* If the next keyword is `__label__' we have a label declaration. */
10274 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10275 cp_parser_label_declaration (parser);
10276 /* Parse an (optional) statement-seq. */
10277 cp_parser_statement_seq_opt (parser, NULL_TREE);
10278 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10282 /* Declarations [gram.dcl.dcl] */
10284 /* Parse an optional declaration-sequence.
10288 declaration-seq declaration */
10291 cp_parser_declaration_seq_opt (cp_parser* parser)
10297 token = cp_lexer_peek_token (parser->lexer);
10299 if (token->type == CPP_CLOSE_BRACE
10300 || token->type == CPP_EOF
10301 || token->type == CPP_PRAGMA_EOL)
10304 if (token->type == CPP_SEMICOLON)
10306 /* A declaration consisting of a single semicolon is
10307 invalid. Allow it unless we're being pedantic. */
10308 cp_lexer_consume_token (parser->lexer);
10309 if (!in_system_header)
10310 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10314 /* If we're entering or exiting a region that's implicitly
10315 extern "C", modify the lang context appropriately. */
10316 if (!parser->implicit_extern_c && token->implicit_extern_c)
10318 push_lang_context (lang_name_c);
10319 parser->implicit_extern_c = true;
10321 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10323 pop_lang_context ();
10324 parser->implicit_extern_c = false;
10327 if (token->type == CPP_PRAGMA)
10329 /* A top-level declaration can consist solely of a #pragma.
10330 A nested declaration cannot, so this is done here and not
10331 in cp_parser_declaration. (A #pragma at block scope is
10332 handled in cp_parser_statement.) */
10333 cp_parser_pragma (parser, pragma_external);
10337 /* Parse the declaration itself. */
10338 cp_parser_declaration (parser);
10342 /* Parse a declaration.
10346 function-definition
10347 template-declaration
10348 explicit-instantiation
10349 explicit-specialization
10350 linkage-specification
10351 namespace-definition
10356 __extension__ declaration */
10359 cp_parser_declaration (cp_parser* parser)
10363 int saved_pedantic;
10365 tree attributes = NULL_TREE;
10367 /* Check for the `__extension__' keyword. */
10368 if (cp_parser_extension_opt (parser, &saved_pedantic))
10370 /* Parse the qualified declaration. */
10371 cp_parser_declaration (parser);
10372 /* Restore the PEDANTIC flag. */
10373 pedantic = saved_pedantic;
10378 /* Try to figure out what kind of declaration is present. */
10379 token1 = *cp_lexer_peek_token (parser->lexer);
10381 if (token1.type != CPP_EOF)
10382 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10385 token2.type = CPP_EOF;
10386 token2.keyword = RID_MAX;
10389 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10390 p = obstack_alloc (&declarator_obstack, 0);
10392 /* If the next token is `extern' and the following token is a string
10393 literal, then we have a linkage specification. */
10394 if (token1.keyword == RID_EXTERN
10395 && cp_parser_is_pure_string_literal (&token2))
10396 cp_parser_linkage_specification (parser);
10397 /* If the next token is `template', then we have either a template
10398 declaration, an explicit instantiation, or an explicit
10400 else if (token1.keyword == RID_TEMPLATE)
10402 /* `template <>' indicates a template specialization. */
10403 if (token2.type == CPP_LESS
10404 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10405 cp_parser_explicit_specialization (parser);
10406 /* `template <' indicates a template declaration. */
10407 else if (token2.type == CPP_LESS)
10408 cp_parser_template_declaration (parser, /*member_p=*/false);
10409 /* Anything else must be an explicit instantiation. */
10411 cp_parser_explicit_instantiation (parser);
10413 /* If the next token is `export', then we have a template
10415 else if (token1.keyword == RID_EXPORT)
10416 cp_parser_template_declaration (parser, /*member_p=*/false);
10417 /* If the next token is `extern', 'static' or 'inline' and the one
10418 after that is `template', we have a GNU extended explicit
10419 instantiation directive. */
10420 else if (cp_parser_allow_gnu_extensions_p (parser)
10421 && (token1.keyword == RID_EXTERN
10422 || token1.keyword == RID_STATIC
10423 || token1.keyword == RID_INLINE)
10424 && token2.keyword == RID_TEMPLATE)
10425 cp_parser_explicit_instantiation (parser);
10426 /* If the next token is `namespace', check for a named or unnamed
10427 namespace definition. */
10428 else if (token1.keyword == RID_NAMESPACE
10429 && (/* A named namespace definition. */
10430 (token2.type == CPP_NAME
10431 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10433 /* An unnamed namespace definition. */
10434 || token2.type == CPP_OPEN_BRACE
10435 || token2.keyword == RID_ATTRIBUTE))
10436 cp_parser_namespace_definition (parser);
10437 /* An inline (associated) namespace definition. */
10438 else if (token1.keyword == RID_INLINE
10439 && token2.keyword == RID_NAMESPACE)
10440 cp_parser_namespace_definition (parser);
10441 /* Objective-C++ declaration/definition. */
10442 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10443 cp_parser_objc_declaration (parser, NULL_TREE);
10444 else if (c_dialect_objc ()
10445 && token1.keyword == RID_ATTRIBUTE
10446 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10447 cp_parser_objc_declaration (parser, attributes);
10448 /* We must have either a block declaration or a function
10451 /* Try to parse a block-declaration, or a function-definition. */
10452 cp_parser_block_declaration (parser, /*statement_p=*/false);
10454 /* Free any declarators allocated. */
10455 obstack_free (&declarator_obstack, p);
10458 /* Parse a block-declaration.
10463 namespace-alias-definition
10470 __extension__ block-declaration
10475 static_assert-declaration
10477 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10478 part of a declaration-statement. */
10481 cp_parser_block_declaration (cp_parser *parser,
10485 int saved_pedantic;
10487 /* Check for the `__extension__' keyword. */
10488 if (cp_parser_extension_opt (parser, &saved_pedantic))
10490 /* Parse the qualified declaration. */
10491 cp_parser_block_declaration (parser, statement_p);
10492 /* Restore the PEDANTIC flag. */
10493 pedantic = saved_pedantic;
10498 /* Peek at the next token to figure out which kind of declaration is
10500 token1 = cp_lexer_peek_token (parser->lexer);
10502 /* If the next keyword is `asm', we have an asm-definition. */
10503 if (token1->keyword == RID_ASM)
10506 cp_parser_commit_to_tentative_parse (parser);
10507 cp_parser_asm_definition (parser);
10509 /* If the next keyword is `namespace', we have a
10510 namespace-alias-definition. */
10511 else if (token1->keyword == RID_NAMESPACE)
10512 cp_parser_namespace_alias_definition (parser);
10513 /* If the next keyword is `using', we have a
10514 using-declaration, a using-directive, or an alias-declaration. */
10515 else if (token1->keyword == RID_USING)
10520 cp_parser_commit_to_tentative_parse (parser);
10521 /* If the token after `using' is `namespace', then we have a
10522 using-directive. */
10523 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10524 if (token2->keyword == RID_NAMESPACE)
10525 cp_parser_using_directive (parser);
10526 /* If the second token after 'using' is '=', then we have an
10527 alias-declaration. */
10528 else if (cxx_dialect >= cxx0x
10529 && token2->type == CPP_NAME
10530 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10531 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10532 cp_parser_alias_declaration (parser);
10533 /* Otherwise, it's a using-declaration. */
10535 cp_parser_using_declaration (parser,
10536 /*access_declaration_p=*/false);
10538 /* If the next keyword is `__label__' we have a misplaced label
10540 else if (token1->keyword == RID_LABEL)
10542 cp_lexer_consume_token (parser->lexer);
10543 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10544 cp_parser_skip_to_end_of_statement (parser);
10545 /* If the next token is now a `;', consume it. */
10546 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10547 cp_lexer_consume_token (parser->lexer);
10549 /* If the next token is `static_assert' we have a static assertion. */
10550 else if (token1->keyword == RID_STATIC_ASSERT)
10551 cp_parser_static_assert (parser, /*member_p=*/false);
10552 /* Anything else must be a simple-declaration. */
10554 cp_parser_simple_declaration (parser, !statement_p,
10555 /*maybe_range_for_decl*/NULL);
10558 /* Parse a simple-declaration.
10560 simple-declaration:
10561 decl-specifier-seq [opt] init-declarator-list [opt] ;
10563 init-declarator-list:
10565 init-declarator-list , init-declarator
10567 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10568 function-definition as a simple-declaration.
10570 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10571 parsed declaration if it is an uninitialized single declarator not followed
10572 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10573 if present, will not be consumed. */
10576 cp_parser_simple_declaration (cp_parser* parser,
10577 bool function_definition_allowed_p,
10578 tree *maybe_range_for_decl)
10580 cp_decl_specifier_seq decl_specifiers;
10581 int declares_class_or_enum;
10582 bool saw_declarator;
10584 if (maybe_range_for_decl)
10585 *maybe_range_for_decl = NULL_TREE;
10587 /* Defer access checks until we know what is being declared; the
10588 checks for names appearing in the decl-specifier-seq should be
10589 done as if we were in the scope of the thing being declared. */
10590 push_deferring_access_checks (dk_deferred);
10592 /* Parse the decl-specifier-seq. We have to keep track of whether
10593 or not the decl-specifier-seq declares a named class or
10594 enumeration type, since that is the only case in which the
10595 init-declarator-list is allowed to be empty.
10599 In a simple-declaration, the optional init-declarator-list can be
10600 omitted only when declaring a class or enumeration, that is when
10601 the decl-specifier-seq contains either a class-specifier, an
10602 elaborated-type-specifier, or an enum-specifier. */
10603 cp_parser_decl_specifier_seq (parser,
10604 CP_PARSER_FLAGS_OPTIONAL,
10606 &declares_class_or_enum);
10607 /* We no longer need to defer access checks. */
10608 stop_deferring_access_checks ();
10610 /* In a block scope, a valid declaration must always have a
10611 decl-specifier-seq. By not trying to parse declarators, we can
10612 resolve the declaration/expression ambiguity more quickly. */
10613 if (!function_definition_allowed_p
10614 && !decl_specifiers.any_specifiers_p)
10616 cp_parser_error (parser, "expected declaration");
10620 /* If the next two tokens are both identifiers, the code is
10621 erroneous. The usual cause of this situation is code like:
10625 where "T" should name a type -- but does not. */
10626 if (!decl_specifiers.any_type_specifiers_p
10627 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10629 /* If parsing tentatively, we should commit; we really are
10630 looking at a declaration. */
10631 cp_parser_commit_to_tentative_parse (parser);
10636 /* If we have seen at least one decl-specifier, and the next token
10637 is not a parenthesis, then we must be looking at a declaration.
10638 (After "int (" we might be looking at a functional cast.) */
10639 if (decl_specifiers.any_specifiers_p
10640 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10641 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10642 && !cp_parser_error_occurred (parser))
10643 cp_parser_commit_to_tentative_parse (parser);
10645 /* Keep going until we hit the `;' at the end of the simple
10647 saw_declarator = false;
10648 while (cp_lexer_next_token_is_not (parser->lexer,
10652 bool function_definition_p;
10655 if (saw_declarator)
10657 /* If we are processing next declarator, coma is expected */
10658 token = cp_lexer_peek_token (parser->lexer);
10659 gcc_assert (token->type == CPP_COMMA);
10660 cp_lexer_consume_token (parser->lexer);
10661 if (maybe_range_for_decl)
10662 *maybe_range_for_decl = error_mark_node;
10665 saw_declarator = true;
10667 /* Parse the init-declarator. */
10668 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10670 function_definition_allowed_p,
10671 /*member_p=*/false,
10672 declares_class_or_enum,
10673 &function_definition_p,
10674 maybe_range_for_decl);
10675 /* If an error occurred while parsing tentatively, exit quickly.
10676 (That usually happens when in the body of a function; each
10677 statement is treated as a declaration-statement until proven
10679 if (cp_parser_error_occurred (parser))
10681 /* Handle function definitions specially. */
10682 if (function_definition_p)
10684 /* If the next token is a `,', then we are probably
10685 processing something like:
10689 which is erroneous. */
10690 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10692 cp_token *token = cp_lexer_peek_token (parser->lexer);
10693 error_at (token->location,
10695 " declarations and function-definitions is forbidden");
10697 /* Otherwise, we're done with the list of declarators. */
10700 pop_deferring_access_checks ();
10704 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10705 *maybe_range_for_decl = decl;
10706 /* The next token should be either a `,' or a `;'. */
10707 token = cp_lexer_peek_token (parser->lexer);
10708 /* If it's a `,', there are more declarators to come. */
10709 if (token->type == CPP_COMMA)
10710 /* will be consumed next time around */;
10711 /* If it's a `;', we are done. */
10712 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10714 /* Anything else is an error. */
10717 /* If we have already issued an error message we don't need
10718 to issue another one. */
10719 if (decl != error_mark_node
10720 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10721 cp_parser_error (parser, "expected %<,%> or %<;%>");
10722 /* Skip tokens until we reach the end of the statement. */
10723 cp_parser_skip_to_end_of_statement (parser);
10724 /* If the next token is now a `;', consume it. */
10725 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10726 cp_lexer_consume_token (parser->lexer);
10729 /* After the first time around, a function-definition is not
10730 allowed -- even if it was OK at first. For example:
10735 function_definition_allowed_p = false;
10738 /* Issue an error message if no declarators are present, and the
10739 decl-specifier-seq does not itself declare a class or
10741 if (!saw_declarator)
10743 if (cp_parser_declares_only_class_p (parser))
10744 shadow_tag (&decl_specifiers);
10745 /* Perform any deferred access checks. */
10746 perform_deferred_access_checks (tf_warning_or_error);
10749 /* Consume the `;'. */
10750 if (!maybe_range_for_decl)
10751 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10754 pop_deferring_access_checks ();
10757 /* Parse a decl-specifier-seq.
10759 decl-specifier-seq:
10760 decl-specifier-seq [opt] decl-specifier
10761 decl-specifier attribute-specifier-seq [opt] (C++11)
10764 storage-class-specifier
10775 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10777 The parser flags FLAGS is used to control type-specifier parsing.
10779 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10782 1: one of the decl-specifiers is an elaborated-type-specifier
10783 (i.e., a type declaration)
10784 2: one of the decl-specifiers is an enum-specifier or a
10785 class-specifier (i.e., a type definition)
10790 cp_parser_decl_specifier_seq (cp_parser* parser,
10791 cp_parser_flags flags,
10792 cp_decl_specifier_seq *decl_specs,
10793 int* declares_class_or_enum)
10795 bool constructor_possible_p = !parser->in_declarator_p;
10796 bool found_decl_spec = false;
10797 cp_token *start_token = NULL;
10800 /* Clear DECL_SPECS. */
10801 clear_decl_specs (decl_specs);
10803 /* Assume no class or enumeration type is declared. */
10804 *declares_class_or_enum = 0;
10806 /* Keep reading specifiers until there are no more to read. */
10809 bool constructor_p;
10813 /* Peek at the next token. */
10814 token = cp_lexer_peek_token (parser->lexer);
10816 /* Save the first token of the decl spec list for error
10819 start_token = token;
10820 /* Handle attributes. */
10821 if (cp_next_tokens_can_be_attribute_p (parser))
10823 /* Parse the attributes. */
10824 tree attrs = cp_parser_attributes_opt (parser);
10826 /* In a sequence of declaration specifiers, c++11 attributes
10827 appertain to the type that precede them. In that case
10830 The attribute-specifier-seq affects the type only for
10831 the declaration it appears in, not other declarations
10832 involving the same type.
10834 But for now let's force the user to position the
10835 attribute either at the beginning of the declaration or
10836 after the declarator-id, which would clearly mean that it
10837 applies to the declarator. */
10838 if (cxx11_attribute_p (attrs))
10840 if (!found_decl_spec)
10841 /* The c++11 attribute is at the beginning of the
10842 declaration. It appertains to the entity being
10846 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10848 /* This is an attribute following a
10849 class-specifier. */
10850 if (decl_specs->type_definition_p)
10851 warn_misplaced_attr_for_class_type (token->location,
10857 decl_specs->std_attributes
10858 = chainon (decl_specs->std_attributes,
10860 if (decl_specs->locations[ds_std_attribute] == 0)
10861 decl_specs->locations[ds_std_attribute] = token->location;
10867 decl_specs->attributes
10868 = chainon (decl_specs->attributes,
10870 if (decl_specs->locations[ds_attribute] == 0)
10871 decl_specs->locations[ds_attribute] = token->location;
10874 /* Assume we will find a decl-specifier keyword. */
10875 found_decl_spec = true;
10876 /* If the next token is an appropriate keyword, we can simply
10877 add it to the list. */
10878 switch (token->keyword)
10884 if (!at_class_scope_p ())
10886 error_at (token->location, "%<friend%> used outside of class");
10887 cp_lexer_purge_token (parser->lexer);
10892 /* Consume the token. */
10893 cp_lexer_consume_token (parser->lexer);
10897 case RID_CONSTEXPR:
10899 cp_lexer_consume_token (parser->lexer);
10902 /* function-specifier:
10909 cp_parser_function_specifier_opt (parser, decl_specs);
10916 /* Consume the token. */
10917 cp_lexer_consume_token (parser->lexer);
10918 /* A constructor declarator cannot appear in a typedef. */
10919 constructor_possible_p = false;
10920 /* The "typedef" keyword can only occur in a declaration; we
10921 may as well commit at this point. */
10922 cp_parser_commit_to_tentative_parse (parser);
10924 if (decl_specs->storage_class != sc_none)
10925 decl_specs->conflicting_specifiers_p = true;
10928 /* storage-class-specifier:
10938 if (cxx_dialect == cxx98)
10940 /* Consume the token. */
10941 cp_lexer_consume_token (parser->lexer);
10943 /* Complain about `auto' as a storage specifier, if
10944 we're complaining about C++0x compatibility. */
10945 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10946 " changes meaning in C++11; please remove it");
10948 /* Set the storage class anyway. */
10949 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10953 /* C++0x auto type-specifier. */
10954 found_decl_spec = false;
10961 /* Consume the token. */
10962 cp_lexer_consume_token (parser->lexer);
10963 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10967 /* Consume the token. */
10969 cp_lexer_consume_token (parser->lexer);
10973 /* We did not yet find a decl-specifier yet. */
10974 found_decl_spec = false;
10978 if (found_decl_spec
10979 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10980 && token->keyword != RID_CONSTEXPR)
10981 error ("decl-specifier invalid in condition");
10984 set_and_check_decl_spec_loc (decl_specs, ds, token);
10986 /* Constructors are a special case. The `S' in `S()' is not a
10987 decl-specifier; it is the beginning of the declarator. */
10989 = (!found_decl_spec
10990 && constructor_possible_p
10991 && (cp_parser_constructor_declarator_p
10992 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10994 /* If we don't have a DECL_SPEC yet, then we must be looking at
10995 a type-specifier. */
10996 if (!found_decl_spec && !constructor_p)
10998 int decl_spec_declares_class_or_enum;
10999 bool is_cv_qualifier;
11003 = cp_parser_type_specifier (parser, flags,
11005 /*is_declaration=*/true,
11006 &decl_spec_declares_class_or_enum,
11008 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11010 /* If this type-specifier referenced a user-defined type
11011 (a typedef, class-name, etc.), then we can't allow any
11012 more such type-specifiers henceforth.
11016 The longest sequence of decl-specifiers that could
11017 possibly be a type name is taken as the
11018 decl-specifier-seq of a declaration. The sequence shall
11019 be self-consistent as described below.
11023 As a general rule, at most one type-specifier is allowed
11024 in the complete decl-specifier-seq of a declaration. The
11025 only exceptions are the following:
11027 -- const or volatile can be combined with any other
11030 -- signed or unsigned can be combined with char, long,
11038 void g (const int Pc);
11040 Here, Pc is *not* part of the decl-specifier seq; it's
11041 the declarator. Therefore, once we see a type-specifier
11042 (other than a cv-qualifier), we forbid any additional
11043 user-defined types. We *do* still allow things like `int
11044 int' to be considered a decl-specifier-seq, and issue the
11045 error message later. */
11046 if (type_spec && !is_cv_qualifier)
11047 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11048 /* A constructor declarator cannot follow a type-specifier. */
11051 constructor_possible_p = false;
11052 found_decl_spec = true;
11053 if (!is_cv_qualifier)
11054 decl_specs->any_type_specifiers_p = true;
11058 /* If we still do not have a DECL_SPEC, then there are no more
11059 decl-specifiers. */
11060 if (!found_decl_spec)
11063 decl_specs->any_specifiers_p = true;
11064 /* After we see one decl-specifier, further decl-specifiers are
11065 always optional. */
11066 flags |= CP_PARSER_FLAGS_OPTIONAL;
11069 /* Don't allow a friend specifier with a class definition. */
11070 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11071 && (*declares_class_or_enum & 2))
11072 error_at (decl_specs->locations[ds_friend],
11073 "class definition may not be declared a friend");
11076 /* Parse an (optional) storage-class-specifier.
11078 storage-class-specifier:
11087 storage-class-specifier:
11090 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11093 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11095 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11098 if (cxx_dialect != cxx98)
11100 /* Fall through for C++98. */
11107 /* Consume the token. */
11108 return cp_lexer_consume_token (parser->lexer)->u.value;
11115 /* Parse an (optional) function-specifier.
11117 function-specifier:
11122 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11123 Updates DECL_SPECS, if it is non-NULL. */
11126 cp_parser_function_specifier_opt (cp_parser* parser,
11127 cp_decl_specifier_seq *decl_specs)
11129 cp_token *token = cp_lexer_peek_token (parser->lexer);
11130 switch (token->keyword)
11133 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11137 /* 14.5.2.3 [temp.mem]
11139 A member function template shall not be virtual. */
11140 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11141 error_at (token->location, "templates may not be %<virtual%>");
11143 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11147 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11154 /* Consume the token. */
11155 return cp_lexer_consume_token (parser->lexer)->u.value;
11158 /* Parse a linkage-specification.
11160 linkage-specification:
11161 extern string-literal { declaration-seq [opt] }
11162 extern string-literal declaration */
11165 cp_parser_linkage_specification (cp_parser* parser)
11169 /* Look for the `extern' keyword. */
11170 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11172 /* Look for the string-literal. */
11173 linkage = cp_parser_string_literal (parser, false, false);
11175 /* Transform the literal into an identifier. If the literal is a
11176 wide-character string, or contains embedded NULs, then we can't
11177 handle it as the user wants. */
11178 if (strlen (TREE_STRING_POINTER (linkage))
11179 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11181 cp_parser_error (parser, "invalid linkage-specification");
11182 /* Assume C++ linkage. */
11183 linkage = lang_name_cplusplus;
11186 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11188 /* We're now using the new linkage. */
11189 push_lang_context (linkage);
11191 /* If the next token is a `{', then we're using the first
11193 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11195 /* Consume the `{' token. */
11196 cp_lexer_consume_token (parser->lexer);
11197 /* Parse the declarations. */
11198 cp_parser_declaration_seq_opt (parser);
11199 /* Look for the closing `}'. */
11200 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11202 /* Otherwise, there's just one declaration. */
11205 bool saved_in_unbraced_linkage_specification_p;
11207 saved_in_unbraced_linkage_specification_p
11208 = parser->in_unbraced_linkage_specification_p;
11209 parser->in_unbraced_linkage_specification_p = true;
11210 cp_parser_declaration (parser);
11211 parser->in_unbraced_linkage_specification_p
11212 = saved_in_unbraced_linkage_specification_p;
11215 /* We're done with the linkage-specification. */
11216 pop_lang_context ();
11219 /* Parse a static_assert-declaration.
11221 static_assert-declaration:
11222 static_assert ( constant-expression , string-literal ) ;
11224 If MEMBER_P, this static_assert is a class member. */
11227 cp_parser_static_assert(cp_parser *parser, bool member_p)
11232 location_t saved_loc;
11235 /* Peek at the `static_assert' token so we can keep track of exactly
11236 where the static assertion started. */
11237 token = cp_lexer_peek_token (parser->lexer);
11238 saved_loc = token->location;
11240 /* Look for the `static_assert' keyword. */
11241 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11245 /* We know we are in a static assertion; commit to any tentative
11247 if (cp_parser_parsing_tentatively (parser))
11248 cp_parser_commit_to_tentative_parse (parser);
11250 /* Parse the `(' starting the static assertion condition. */
11251 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11253 /* Parse the constant-expression. Allow a non-constant expression
11254 here in order to give better diagnostics in finish_static_assert. */
11256 cp_parser_constant_expression (parser,
11257 /*allow_non_constant_p=*/true,
11258 /*non_constant_p=*/&dummy);
11260 /* Parse the separating `,'. */
11261 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11263 /* Parse the string-literal message. */
11264 message = cp_parser_string_literal (parser,
11265 /*translate=*/false,
11268 /* A `)' completes the static assertion. */
11269 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11270 cp_parser_skip_to_closing_parenthesis (parser,
11271 /*recovering=*/true,
11272 /*or_comma=*/false,
11273 /*consume_paren=*/true);
11275 /* A semicolon terminates the declaration. */
11276 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11278 /* Complete the static assertion, which may mean either processing
11279 the static assert now or saving it for template instantiation. */
11280 finish_static_assert (condition, message, saved_loc, member_p);
11283 /* Parse a `decltype' type. Returns the type.
11285 simple-type-specifier:
11286 decltype ( expression ) */
11289 cp_parser_decltype (cp_parser *parser)
11292 bool id_expression_or_member_access_p = false;
11293 const char *saved_message;
11294 bool saved_integral_constant_expression_p;
11295 bool saved_non_integral_constant_expression_p;
11296 cp_token *id_expr_start_token;
11297 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11299 if (start_token->type == CPP_DECLTYPE)
11301 /* Already parsed. */
11302 cp_lexer_consume_token (parser->lexer);
11303 return start_token->u.value;
11306 /* Look for the `decltype' token. */
11307 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11308 return error_mark_node;
11310 /* Types cannot be defined in a `decltype' expression. Save away the
11312 saved_message = parser->type_definition_forbidden_message;
11314 /* And create the new one. */
11315 parser->type_definition_forbidden_message
11316 = G_("types may not be defined in %<decltype%> expressions");
11318 /* The restrictions on constant-expressions do not apply inside
11319 decltype expressions. */
11320 saved_integral_constant_expression_p
11321 = parser->integral_constant_expression_p;
11322 saved_non_integral_constant_expression_p
11323 = parser->non_integral_constant_expression_p;
11324 parser->integral_constant_expression_p = false;
11326 /* Do not actually evaluate the expression. */
11327 ++cp_unevaluated_operand;
11329 /* Do not warn about problems with the expression. */
11330 ++c_inhibit_evaluation_warnings;
11332 /* Parse the opening `('. */
11333 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11334 return error_mark_node;
11336 /* First, try parsing an id-expression. */
11337 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11338 cp_parser_parse_tentatively (parser);
11339 expr = cp_parser_id_expression (parser,
11340 /*template_keyword_p=*/false,
11341 /*check_dependency_p=*/true,
11342 /*template_p=*/NULL,
11343 /*declarator_p=*/false,
11344 /*optional_p=*/false);
11346 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11348 bool non_integral_constant_expression_p = false;
11349 tree id_expression = expr;
11351 const char *error_msg;
11353 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11354 /* Lookup the name we got back from the id-expression. */
11355 expr = cp_parser_lookup_name (parser, expr,
11357 /*is_template=*/false,
11358 /*is_namespace=*/false,
11359 /*check_dependency=*/true,
11360 /*ambiguous_decls=*/NULL,
11361 id_expr_start_token->location);
11364 && expr != error_mark_node
11365 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11366 && TREE_CODE (expr) != TYPE_DECL
11367 && (TREE_CODE (expr) != BIT_NOT_EXPR
11368 || !TYPE_P (TREE_OPERAND (expr, 0)))
11369 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11371 /* Complete lookup of the id-expression. */
11372 expr = (finish_id_expression
11373 (id_expression, expr, parser->scope, &idk,
11374 /*integral_constant_expression_p=*/false,
11375 /*allow_non_integral_constant_expression_p=*/true,
11376 &non_integral_constant_expression_p,
11377 /*template_p=*/false,
11379 /*address_p=*/false,
11380 /*template_arg_p=*/false,
11382 id_expr_start_token->location));
11384 if (expr == error_mark_node)
11385 /* We found an id-expression, but it was something that we
11386 should not have found. This is an error, not something
11387 we can recover from, so note that we found an
11388 id-expression and we'll recover as gracefully as
11390 id_expression_or_member_access_p = true;
11394 && expr != error_mark_node
11395 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11396 /* We have an id-expression. */
11397 id_expression_or_member_access_p = true;
11400 if (!id_expression_or_member_access_p)
11402 /* Abort the id-expression parse. */
11403 cp_parser_abort_tentative_parse (parser);
11405 /* Parsing tentatively, again. */
11406 cp_parser_parse_tentatively (parser);
11408 /* Parse a class member access. */
11409 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11410 /*cast_p=*/false, /*decltype*/true,
11411 /*member_access_only_p=*/true, NULL);
11414 && expr != error_mark_node
11415 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11416 /* We have an id-expression. */
11417 id_expression_or_member_access_p = true;
11420 if (id_expression_or_member_access_p)
11421 /* We have parsed the complete id-expression or member access. */
11422 cp_parser_parse_definitely (parser);
11425 bool saved_greater_than_is_operator_p;
11427 /* Abort our attempt to parse an id-expression or member access
11429 cp_parser_abort_tentative_parse (parser);
11431 /* Within a parenthesized expression, a `>' token is always
11432 the greater-than operator. */
11433 saved_greater_than_is_operator_p
11434 = parser->greater_than_is_operator_p;
11435 parser->greater_than_is_operator_p = true;
11437 /* Parse a full expression. */
11438 expr = cp_parser_expression (parser, /*cast_p=*/false,
11439 /*decltype*/true, NULL);
11441 /* The `>' token might be the end of a template-id or
11442 template-parameter-list now. */
11443 parser->greater_than_is_operator_p
11444 = saved_greater_than_is_operator_p;
11447 /* Go back to evaluating expressions. */
11448 --cp_unevaluated_operand;
11449 --c_inhibit_evaluation_warnings;
11451 /* Restore the old message and the integral constant expression
11453 parser->type_definition_forbidden_message = saved_message;
11454 parser->integral_constant_expression_p
11455 = saved_integral_constant_expression_p;
11456 parser->non_integral_constant_expression_p
11457 = saved_non_integral_constant_expression_p;
11459 /* Parse to the closing `)'. */
11460 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11462 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11463 /*consume_paren=*/true);
11464 return error_mark_node;
11467 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11468 tf_warning_or_error);
11470 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11472 start_token->type = CPP_DECLTYPE;
11473 start_token->u.value = expr;
11474 start_token->keyword = RID_MAX;
11475 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11480 /* Special member functions [gram.special] */
11482 /* Parse a conversion-function-id.
11484 conversion-function-id:
11485 operator conversion-type-id
11487 Returns an IDENTIFIER_NODE representing the operator. */
11490 cp_parser_conversion_function_id (cp_parser* parser)
11494 tree saved_qualifying_scope;
11495 tree saved_object_scope;
11496 tree pushed_scope = NULL_TREE;
11498 /* Look for the `operator' token. */
11499 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11500 return error_mark_node;
11501 /* When we parse the conversion-type-id, the current scope will be
11502 reset. However, we need that information in able to look up the
11503 conversion function later, so we save it here. */
11504 saved_scope = parser->scope;
11505 saved_qualifying_scope = parser->qualifying_scope;
11506 saved_object_scope = parser->object_scope;
11507 /* We must enter the scope of the class so that the names of
11508 entities declared within the class are available in the
11509 conversion-type-id. For example, consider:
11516 S::operator I() { ... }
11518 In order to see that `I' is a type-name in the definition, we
11519 must be in the scope of `S'. */
11521 pushed_scope = push_scope (saved_scope);
11522 /* Parse the conversion-type-id. */
11523 type = cp_parser_conversion_type_id (parser);
11524 /* Leave the scope of the class, if any. */
11526 pop_scope (pushed_scope);
11527 /* Restore the saved scope. */
11528 parser->scope = saved_scope;
11529 parser->qualifying_scope = saved_qualifying_scope;
11530 parser->object_scope = saved_object_scope;
11531 /* If the TYPE is invalid, indicate failure. */
11532 if (type == error_mark_node)
11533 return error_mark_node;
11534 return mangle_conv_op_name_for_type (type);
11537 /* Parse a conversion-type-id:
11539 conversion-type-id:
11540 type-specifier-seq conversion-declarator [opt]
11542 Returns the TYPE specified. */
11545 cp_parser_conversion_type_id (cp_parser* parser)
11548 cp_decl_specifier_seq type_specifiers;
11549 cp_declarator *declarator;
11550 tree type_specified;
11552 /* Parse the attributes. */
11553 attributes = cp_parser_attributes_opt (parser);
11554 /* Parse the type-specifiers. */
11555 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11556 /*is_trailing_return=*/false,
11558 /* If that didn't work, stop. */
11559 if (type_specifiers.type == error_mark_node)
11560 return error_mark_node;
11561 /* Parse the conversion-declarator. */
11562 declarator = cp_parser_conversion_declarator_opt (parser);
11564 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11565 /*initialized=*/0, &attributes);
11567 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11569 /* Don't give this error when parsing tentatively. This happens to
11570 work because we always parse this definitively once. */
11571 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11572 && type_uses_auto (type_specified))
11574 if (cxx_dialect < cxx1y)
11576 error ("invalid use of %<auto%> in conversion operator");
11577 return error_mark_node;
11579 else if (template_parm_scope_p ())
11580 warning (0, "use of %<auto%> in member template "
11581 "conversion operator can never be deduced");
11584 return type_specified;
11587 /* Parse an (optional) conversion-declarator.
11589 conversion-declarator:
11590 ptr-operator conversion-declarator [opt]
11594 static cp_declarator *
11595 cp_parser_conversion_declarator_opt (cp_parser* parser)
11597 enum tree_code code;
11598 tree class_type, std_attributes = NULL_TREE;
11599 cp_cv_quals cv_quals;
11601 /* We don't know if there's a ptr-operator next, or not. */
11602 cp_parser_parse_tentatively (parser);
11603 /* Try the ptr-operator. */
11604 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11606 /* If it worked, look for more conversion-declarators. */
11607 if (cp_parser_parse_definitely (parser))
11609 cp_declarator *declarator;
11611 /* Parse another optional declarator. */
11612 declarator = cp_parser_conversion_declarator_opt (parser);
11614 declarator = cp_parser_make_indirect_declarator
11615 (code, class_type, cv_quals, declarator, std_attributes);
11623 /* Parse an (optional) ctor-initializer.
11626 : mem-initializer-list
11628 Returns TRUE iff the ctor-initializer was actually present. */
11631 cp_parser_ctor_initializer_opt (cp_parser* parser)
11633 /* If the next token is not a `:', then there is no
11634 ctor-initializer. */
11635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11637 /* Do default initialization of any bases and members. */
11638 if (DECL_CONSTRUCTOR_P (current_function_decl))
11639 finish_mem_initializers (NULL_TREE);
11644 /* Consume the `:' token. */
11645 cp_lexer_consume_token (parser->lexer);
11646 /* And the mem-initializer-list. */
11647 cp_parser_mem_initializer_list (parser);
11652 /* Parse a mem-initializer-list.
11654 mem-initializer-list:
11655 mem-initializer ... [opt]
11656 mem-initializer ... [opt] , mem-initializer-list */
11659 cp_parser_mem_initializer_list (cp_parser* parser)
11661 tree mem_initializer_list = NULL_TREE;
11662 tree target_ctor = error_mark_node;
11663 cp_token *token = cp_lexer_peek_token (parser->lexer);
11665 /* Let the semantic analysis code know that we are starting the
11666 mem-initializer-list. */
11667 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11668 error_at (token->location,
11669 "only constructors take member initializers");
11671 /* Loop through the list. */
11674 tree mem_initializer;
11676 token = cp_lexer_peek_token (parser->lexer);
11677 /* Parse the mem-initializer. */
11678 mem_initializer = cp_parser_mem_initializer (parser);
11679 /* If the next token is a `...', we're expanding member initializers. */
11680 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11682 /* Consume the `...'. */
11683 cp_lexer_consume_token (parser->lexer);
11685 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11686 can be expanded but members cannot. */
11687 if (mem_initializer != error_mark_node
11688 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11690 error_at (token->location,
11691 "cannot expand initializer for member %<%D%>",
11692 TREE_PURPOSE (mem_initializer));
11693 mem_initializer = error_mark_node;
11696 /* Construct the pack expansion type. */
11697 if (mem_initializer != error_mark_node)
11698 mem_initializer = make_pack_expansion (mem_initializer);
11700 if (target_ctor != error_mark_node
11701 && mem_initializer != error_mark_node)
11703 error ("mem-initializer for %qD follows constructor delegation",
11704 TREE_PURPOSE (mem_initializer));
11705 mem_initializer = error_mark_node;
11707 /* Look for a target constructor. */
11708 if (mem_initializer != error_mark_node
11709 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
11710 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11712 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11713 if (mem_initializer_list)
11715 error ("constructor delegation follows mem-initializer for %qD",
11716 TREE_PURPOSE (mem_initializer_list));
11717 mem_initializer = error_mark_node;
11719 target_ctor = mem_initializer;
11721 /* Add it to the list, unless it was erroneous. */
11722 if (mem_initializer != error_mark_node)
11724 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11725 mem_initializer_list = mem_initializer;
11727 /* If the next token is not a `,', we're done. */
11728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11730 /* Consume the `,' token. */
11731 cp_lexer_consume_token (parser->lexer);
11734 /* Perform semantic analysis. */
11735 if (DECL_CONSTRUCTOR_P (current_function_decl))
11736 finish_mem_initializers (mem_initializer_list);
11739 /* Parse a mem-initializer.
11742 mem-initializer-id ( expression-list [opt] )
11743 mem-initializer-id braced-init-list
11748 ( expression-list [opt] )
11750 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11751 class) or FIELD_DECL (for a non-static data member) to initialize;
11752 the TREE_VALUE is the expression-list. An empty initialization
11753 list is represented by void_list_node. */
11756 cp_parser_mem_initializer (cp_parser* parser)
11758 tree mem_initializer_id;
11759 tree expression_list;
11761 cp_token *token = cp_lexer_peek_token (parser->lexer);
11763 /* Find out what is being initialized. */
11764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11766 permerror (token->location,
11767 "anachronistic old-style base class initializer");
11768 mem_initializer_id = NULL_TREE;
11772 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11773 if (mem_initializer_id == error_mark_node)
11774 return mem_initializer_id;
11776 member = expand_member_init (mem_initializer_id);
11777 if (member && !DECL_P (member))
11778 in_base_initializer = 1;
11780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11782 bool expr_non_constant_p;
11783 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11784 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11785 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11786 expression_list = build_tree_list (NULL_TREE, expression_list);
11790 vec<tree, va_gc> *vec;
11791 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11793 /*allow_expansion_p=*/true,
11794 /*non_constant_p=*/NULL);
11796 return error_mark_node;
11797 expression_list = build_tree_list_vec (vec);
11798 release_tree_vector (vec);
11801 if (expression_list == error_mark_node)
11802 return error_mark_node;
11803 if (!expression_list)
11804 expression_list = void_type_node;
11806 in_base_initializer = 0;
11808 return member ? build_tree_list (member, expression_list) : error_mark_node;
11811 /* Parse a mem-initializer-id.
11813 mem-initializer-id:
11814 :: [opt] nested-name-specifier [opt] class-name
11817 Returns a TYPE indicating the class to be initializer for the first
11818 production. Returns an IDENTIFIER_NODE indicating the data member
11819 to be initialized for the second production. */
11822 cp_parser_mem_initializer_id (cp_parser* parser)
11824 bool global_scope_p;
11825 bool nested_name_specifier_p;
11826 bool template_p = false;
11829 cp_token *token = cp_lexer_peek_token (parser->lexer);
11831 /* `typename' is not allowed in this context ([temp.res]). */
11832 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11834 error_at (token->location,
11835 "keyword %<typename%> not allowed in this context (a qualified "
11836 "member initializer is implicitly a type)");
11837 cp_lexer_consume_token (parser->lexer);
11839 /* Look for the optional `::' operator. */
11841 = (cp_parser_global_scope_opt (parser,
11842 /*current_scope_valid_p=*/false)
11844 /* Look for the optional nested-name-specifier. The simplest way to
11849 The keyword `typename' is not permitted in a base-specifier or
11850 mem-initializer; in these contexts a qualified name that
11851 depends on a template-parameter is implicitly assumed to be a
11854 is to assume that we have seen the `typename' keyword at this
11856 nested_name_specifier_p
11857 = (cp_parser_nested_name_specifier_opt (parser,
11858 /*typename_keyword_p=*/true,
11859 /*check_dependency_p=*/true,
11861 /*is_declaration=*/true)
11863 if (nested_name_specifier_p)
11864 template_p = cp_parser_optional_template_keyword (parser);
11865 /* If there is a `::' operator or a nested-name-specifier, then we
11866 are definitely looking for a class-name. */
11867 if (global_scope_p || nested_name_specifier_p)
11868 return cp_parser_class_name (parser,
11869 /*typename_keyword_p=*/true,
11870 /*template_keyword_p=*/template_p,
11872 /*check_dependency_p=*/true,
11873 /*class_head_p=*/false,
11874 /*is_declaration=*/true);
11875 /* Otherwise, we could also be looking for an ordinary identifier. */
11876 cp_parser_parse_tentatively (parser);
11877 /* Try a class-name. */
11878 id = cp_parser_class_name (parser,
11879 /*typename_keyword_p=*/true,
11880 /*template_keyword_p=*/false,
11882 /*check_dependency_p=*/true,
11883 /*class_head_p=*/false,
11884 /*is_declaration=*/true);
11885 /* If we found one, we're done. */
11886 if (cp_parser_parse_definitely (parser))
11888 /* Otherwise, look for an ordinary identifier. */
11889 return cp_parser_identifier (parser);
11892 /* Overloading [gram.over] */
11894 /* Parse an operator-function-id.
11896 operator-function-id:
11899 Returns an IDENTIFIER_NODE for the operator which is a
11900 human-readable spelling of the identifier, e.g., `operator +'. */
11903 cp_parser_operator_function_id (cp_parser* parser)
11905 /* Look for the `operator' keyword. */
11906 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11907 return error_mark_node;
11908 /* And then the name of the operator itself. */
11909 return cp_parser_operator (parser);
11912 /* Return an identifier node for a user-defined literal operator.
11913 The suffix identifier is chained to the operator name identifier. */
11916 cp_literal_operator_id (const char* name)
11919 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11920 + strlen (name) + 10);
11921 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11922 identifier = get_identifier (buffer);
11923 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11928 /* Parse an operator.
11931 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11932 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11933 || ++ -- , ->* -> () []
11940 Returns an IDENTIFIER_NODE for the operator which is a
11941 human-readable spelling of the identifier, e.g., `operator +'. */
11944 cp_parser_operator (cp_parser* parser)
11946 tree id = NULL_TREE;
11949 /* Peek at the next token. */
11950 token = cp_lexer_peek_token (parser->lexer);
11951 /* Figure out which operator we have. */
11952 switch (token->type)
11958 /* The keyword should be either `new' or `delete'. */
11959 if (token->keyword == RID_NEW)
11961 else if (token->keyword == RID_DELETE)
11966 /* Consume the `new' or `delete' token. */
11967 cp_lexer_consume_token (parser->lexer);
11969 /* Peek at the next token. */
11970 token = cp_lexer_peek_token (parser->lexer);
11971 /* If it's a `[' token then this is the array variant of the
11973 if (token->type == CPP_OPEN_SQUARE)
11975 /* Consume the `[' token. */
11976 cp_lexer_consume_token (parser->lexer);
11977 /* Look for the `]' token. */
11978 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11979 id = ansi_opname (op == NEW_EXPR
11980 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11982 /* Otherwise, we have the non-array variant. */
11984 id = ansi_opname (op);
11990 id = ansi_opname (PLUS_EXPR);
11994 id = ansi_opname (MINUS_EXPR);
11998 id = ansi_opname (MULT_EXPR);
12002 id = ansi_opname (TRUNC_DIV_EXPR);
12006 id = ansi_opname (TRUNC_MOD_EXPR);
12010 id = ansi_opname (BIT_XOR_EXPR);
12014 id = ansi_opname (BIT_AND_EXPR);
12018 id = ansi_opname (BIT_IOR_EXPR);
12022 id = ansi_opname (BIT_NOT_EXPR);
12026 id = ansi_opname (TRUTH_NOT_EXPR);
12030 id = ansi_assopname (NOP_EXPR);
12034 id = ansi_opname (LT_EXPR);
12038 id = ansi_opname (GT_EXPR);
12042 id = ansi_assopname (PLUS_EXPR);
12046 id = ansi_assopname (MINUS_EXPR);
12050 id = ansi_assopname (MULT_EXPR);
12054 id = ansi_assopname (TRUNC_DIV_EXPR);
12058 id = ansi_assopname (TRUNC_MOD_EXPR);
12062 id = ansi_assopname (BIT_XOR_EXPR);
12066 id = ansi_assopname (BIT_AND_EXPR);
12070 id = ansi_assopname (BIT_IOR_EXPR);
12074 id = ansi_opname (LSHIFT_EXPR);
12078 id = ansi_opname (RSHIFT_EXPR);
12081 case CPP_LSHIFT_EQ:
12082 id = ansi_assopname (LSHIFT_EXPR);
12085 case CPP_RSHIFT_EQ:
12086 id = ansi_assopname (RSHIFT_EXPR);
12090 id = ansi_opname (EQ_EXPR);
12094 id = ansi_opname (NE_EXPR);
12098 id = ansi_opname (LE_EXPR);
12101 case CPP_GREATER_EQ:
12102 id = ansi_opname (GE_EXPR);
12106 id = ansi_opname (TRUTH_ANDIF_EXPR);
12110 id = ansi_opname (TRUTH_ORIF_EXPR);
12113 case CPP_PLUS_PLUS:
12114 id = ansi_opname (POSTINCREMENT_EXPR);
12117 case CPP_MINUS_MINUS:
12118 id = ansi_opname (PREDECREMENT_EXPR);
12122 id = ansi_opname (COMPOUND_EXPR);
12125 case CPP_DEREF_STAR:
12126 id = ansi_opname (MEMBER_REF);
12130 id = ansi_opname (COMPONENT_REF);
12133 case CPP_OPEN_PAREN:
12134 /* Consume the `('. */
12135 cp_lexer_consume_token (parser->lexer);
12136 /* Look for the matching `)'. */
12137 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12138 return ansi_opname (CALL_EXPR);
12140 case CPP_OPEN_SQUARE:
12141 /* Consume the `['. */
12142 cp_lexer_consume_token (parser->lexer);
12143 /* Look for the matching `]'. */
12144 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12145 return ansi_opname (ARRAY_REF);
12148 if (cxx_dialect == cxx98)
12149 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12150 if (TREE_STRING_LENGTH (token->u.value) > 2)
12152 error ("expected empty string after %<operator%> keyword");
12153 return error_mark_node;
12155 /* Consume the string. */
12156 cp_lexer_consume_token (parser->lexer);
12157 /* Look for the suffix identifier. */
12158 token = cp_lexer_peek_token (parser->lexer);
12159 if (token->type == CPP_NAME)
12161 id = cp_parser_identifier (parser);
12162 if (id != error_mark_node)
12164 const char *name = IDENTIFIER_POINTER (id);
12165 return cp_literal_operator_id (name);
12170 error ("expected suffix identifier");
12171 return error_mark_node;
12174 case CPP_STRING_USERDEF:
12175 error ("missing space between %<\"\"%> and suffix identifier");
12176 return error_mark_node;
12179 /* Anything else is an error. */
12183 /* If we have selected an identifier, we need to consume the
12186 cp_lexer_consume_token (parser->lexer);
12187 /* Otherwise, no valid operator name was present. */
12190 cp_parser_error (parser, "expected operator");
12191 id = error_mark_node;
12197 /* Parse a template-declaration.
12199 template-declaration:
12200 export [opt] template < template-parameter-list > declaration
12202 If MEMBER_P is TRUE, this template-declaration occurs within a
12205 The grammar rule given by the standard isn't correct. What
12206 is really meant is:
12208 template-declaration:
12209 export [opt] template-parameter-list-seq
12210 decl-specifier-seq [opt] init-declarator [opt] ;
12211 export [opt] template-parameter-list-seq
12212 function-definition
12214 template-parameter-list-seq:
12215 template-parameter-list-seq [opt]
12216 template < template-parameter-list > */
12219 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12221 /* Check for `export'. */
12222 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12224 /* Consume the `export' token. */
12225 cp_lexer_consume_token (parser->lexer);
12226 /* Warn that we do not support `export'. */
12227 warning (0, "keyword %<export%> not implemented, and will be ignored");
12230 cp_parser_template_declaration_after_export (parser, member_p);
12233 /* Parse a template-parameter-list.
12235 template-parameter-list:
12237 template-parameter-list , template-parameter
12239 Returns a TREE_LIST. Each node represents a template parameter.
12240 The nodes are connected via their TREE_CHAINs. */
12243 cp_parser_template_parameter_list (cp_parser* parser)
12245 tree parameter_list = NULL_TREE;
12247 begin_template_parm_list ();
12249 /* The loop below parses the template parms. We first need to know
12250 the total number of template parms to be able to compute proper
12251 canonical types of each dependent type. So after the loop, when
12252 we know the total number of template parms,
12253 end_template_parm_list computes the proper canonical types and
12254 fixes up the dependent types accordingly. */
12259 bool is_parameter_pack;
12260 location_t parm_loc;
12262 /* Parse the template-parameter. */
12263 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12264 parameter = cp_parser_template_parameter (parser,
12266 &is_parameter_pack);
12267 /* Add it to the list. */
12268 if (parameter != error_mark_node)
12269 parameter_list = process_template_parm (parameter_list,
12273 is_parameter_pack);
12276 tree err_parm = build_tree_list (parameter, parameter);
12277 parameter_list = chainon (parameter_list, err_parm);
12280 /* If the next token is not a `,', we're done. */
12281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12283 /* Otherwise, consume the `,' token. */
12284 cp_lexer_consume_token (parser->lexer);
12287 return end_template_parm_list (parameter_list);
12290 /* Parse a template-parameter.
12292 template-parameter:
12294 parameter-declaration
12296 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12297 the parameter. The TREE_PURPOSE is the default value, if any.
12298 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12299 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12300 set to true iff this parameter is a parameter pack. */
12303 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12304 bool *is_parameter_pack)
12307 cp_parameter_declarator *parameter_declarator;
12308 cp_declarator *id_declarator;
12311 /* Assume it is a type parameter or a template parameter. */
12312 *is_non_type = false;
12313 /* Assume it not a parameter pack. */
12314 *is_parameter_pack = false;
12315 /* Peek at the next token. */
12316 token = cp_lexer_peek_token (parser->lexer);
12317 /* If it is `class' or `template', we have a type-parameter. */
12318 if (token->keyword == RID_TEMPLATE)
12319 return cp_parser_type_parameter (parser, is_parameter_pack);
12320 /* If it is `class' or `typename' we do not know yet whether it is a
12321 type parameter or a non-type parameter. Consider:
12323 template <typename T, typename T::X X> ...
12327 template <class C, class D*> ...
12329 Here, the first parameter is a type parameter, and the second is
12330 a non-type parameter. We can tell by looking at the token after
12331 the identifier -- if it is a `,', `=', or `>' then we have a type
12333 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12335 /* Peek at the token after `class' or `typename'. */
12336 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12337 /* If it's an ellipsis, we have a template type parameter
12339 if (token->type == CPP_ELLIPSIS)
12340 return cp_parser_type_parameter (parser, is_parameter_pack);
12341 /* If it's an identifier, skip it. */
12342 if (token->type == CPP_NAME)
12343 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12344 /* Now, see if the token looks like the end of a template
12346 if (token->type == CPP_COMMA
12347 || token->type == CPP_EQ
12348 || token->type == CPP_GREATER)
12349 return cp_parser_type_parameter (parser, is_parameter_pack);
12352 /* Otherwise, it is a non-type parameter.
12356 When parsing a default template-argument for a non-type
12357 template-parameter, the first non-nested `>' is taken as the end
12358 of the template parameter-list rather than a greater-than
12360 *is_non_type = true;
12361 parameter_declarator
12362 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12363 /*parenthesized_p=*/NULL);
12365 /* If the parameter declaration is marked as a parameter pack, set
12366 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12367 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12369 if (parameter_declarator
12370 && parameter_declarator->declarator
12371 && parameter_declarator->declarator->parameter_pack_p)
12373 *is_parameter_pack = true;
12374 parameter_declarator->declarator->parameter_pack_p = false;
12377 if (parameter_declarator
12378 && parameter_declarator->default_argument)
12380 /* Can happen in some cases of erroneous input (c++/34892). */
12381 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12382 /* Consume the `...' for better error recovery. */
12383 cp_lexer_consume_token (parser->lexer);
12385 /* If the next token is an ellipsis, and we don't already have it
12386 marked as a parameter pack, then we have a parameter pack (that
12387 has no declarator). */
12388 else if (!*is_parameter_pack
12389 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12390 && (declarator_can_be_parameter_pack
12391 (parameter_declarator->declarator)))
12393 /* Consume the `...'. */
12394 cp_lexer_consume_token (parser->lexer);
12395 maybe_warn_variadic_templates ();
12397 *is_parameter_pack = true;
12399 /* We might end up with a pack expansion as the type of the non-type
12400 template parameter, in which case this is a non-type template
12402 else if (parameter_declarator
12403 && parameter_declarator->decl_specifiers.type
12404 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12406 *is_parameter_pack = true;
12407 parameter_declarator->decl_specifiers.type =
12408 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12411 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12413 /* Parameter packs cannot have default arguments. However, a
12414 user may try to do so, so we'll parse them and give an
12415 appropriate diagnostic here. */
12417 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12419 /* Find the name of the parameter pack. */
12420 id_declarator = parameter_declarator->declarator;
12421 while (id_declarator && id_declarator->kind != cdk_id)
12422 id_declarator = id_declarator->declarator;
12424 if (id_declarator && id_declarator->kind == cdk_id)
12425 error_at (start_token->location,
12426 "template parameter pack %qD cannot have a default argument",
12427 id_declarator->u.id.unqualified_name);
12429 error_at (start_token->location,
12430 "template parameter pack cannot have a default argument");
12432 /* Parse the default argument, but throw away the result. */
12433 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12436 parm = grokdeclarator (parameter_declarator->declarator,
12437 ¶meter_declarator->decl_specifiers,
12438 TPARM, /*initialized=*/0,
12439 /*attrlist=*/NULL);
12440 if (parm == error_mark_node)
12441 return error_mark_node;
12443 return build_tree_list (parameter_declarator->default_argument, parm);
12446 /* Parse a type-parameter.
12449 class identifier [opt]
12450 class identifier [opt] = type-id
12451 typename identifier [opt]
12452 typename identifier [opt] = type-id
12453 template < template-parameter-list > class identifier [opt]
12454 template < template-parameter-list > class identifier [opt]
12457 GNU Extension (variadic templates):
12460 class ... identifier [opt]
12461 typename ... identifier [opt]
12463 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12464 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12465 the declaration of the parameter.
12467 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12470 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12475 /* Look for a keyword to tell us what kind of parameter this is. */
12476 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12478 return error_mark_node;
12480 switch (token->keyword)
12486 tree default_argument;
12488 /* If the next token is an ellipsis, we have a template
12490 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12492 /* Consume the `...' token. */
12493 cp_lexer_consume_token (parser->lexer);
12494 maybe_warn_variadic_templates ();
12496 *is_parameter_pack = true;
12499 /* If the next token is an identifier, then it names the
12501 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12502 identifier = cp_parser_identifier (parser);
12504 identifier = NULL_TREE;
12506 /* Create the parameter. */
12507 parameter = finish_template_type_parm (class_type_node, identifier);
12509 /* If the next token is an `=', we have a default argument. */
12510 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12512 /* Consume the `=' token. */
12513 cp_lexer_consume_token (parser->lexer);
12514 /* Parse the default-argument. */
12515 push_deferring_access_checks (dk_no_deferred);
12516 default_argument = cp_parser_type_id (parser);
12518 /* Template parameter packs cannot have default
12520 if (*is_parameter_pack)
12523 error_at (token->location,
12524 "template parameter pack %qD cannot have a "
12525 "default argument", identifier);
12527 error_at (token->location,
12528 "template parameter packs cannot have "
12529 "default arguments");
12530 default_argument = NULL_TREE;
12532 pop_deferring_access_checks ();
12535 default_argument = NULL_TREE;
12537 /* Create the combined representation of the parameter and the
12538 default argument. */
12539 parameter = build_tree_list (default_argument, parameter);
12546 tree default_argument;
12548 /* Look for the `<'. */
12549 cp_parser_require (parser, CPP_LESS, RT_LESS);
12550 /* Parse the template-parameter-list. */
12551 cp_parser_template_parameter_list (parser);
12552 /* Look for the `>'. */
12553 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12554 /* Look for the `class' keyword. */
12555 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12556 /* If the next token is an ellipsis, we have a template
12558 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12560 /* Consume the `...' token. */
12561 cp_lexer_consume_token (parser->lexer);
12562 maybe_warn_variadic_templates ();
12564 *is_parameter_pack = true;
12566 /* If the next token is an `=', then there is a
12567 default-argument. If the next token is a `>', we are at
12568 the end of the parameter-list. If the next token is a `,',
12569 then we are at the end of this parameter. */
12570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12571 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12572 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12574 identifier = cp_parser_identifier (parser);
12575 /* Treat invalid names as if the parameter were nameless. */
12576 if (identifier == error_mark_node)
12577 identifier = NULL_TREE;
12580 identifier = NULL_TREE;
12582 /* Create the template parameter. */
12583 parameter = finish_template_template_parm (class_type_node,
12586 /* If the next token is an `=', then there is a
12587 default-argument. */
12588 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12592 /* Consume the `='. */
12593 cp_lexer_consume_token (parser->lexer);
12594 /* Parse the id-expression. */
12595 push_deferring_access_checks (dk_no_deferred);
12596 /* save token before parsing the id-expression, for error
12598 token = cp_lexer_peek_token (parser->lexer);
12600 = cp_parser_id_expression (parser,
12601 /*template_keyword_p=*/false,
12602 /*check_dependency_p=*/true,
12603 /*template_p=*/&is_template,
12604 /*declarator_p=*/false,
12605 /*optional_p=*/false);
12606 if (TREE_CODE (default_argument) == TYPE_DECL)
12607 /* If the id-expression was a template-id that refers to
12608 a template-class, we already have the declaration here,
12609 so no further lookup is needed. */
12612 /* Look up the name. */
12614 = cp_parser_lookup_name (parser, default_argument,
12616 /*is_template=*/is_template,
12617 /*is_namespace=*/false,
12618 /*check_dependency=*/true,
12619 /*ambiguous_decls=*/NULL,
12621 /* See if the default argument is valid. */
12623 = check_template_template_default_arg (default_argument);
12625 /* Template parameter packs cannot have default
12627 if (*is_parameter_pack)
12630 error_at (token->location,
12631 "template parameter pack %qD cannot "
12632 "have a default argument",
12635 error_at (token->location, "template parameter packs cannot "
12636 "have default arguments");
12637 default_argument = NULL_TREE;
12639 pop_deferring_access_checks ();
12642 default_argument = NULL_TREE;
12644 /* Create the combined representation of the parameter and the
12645 default argument. */
12646 parameter = build_tree_list (default_argument, parameter);
12651 gcc_unreachable ();
12658 /* Parse a template-id.
12661 template-name < template-argument-list [opt] >
12663 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12664 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12665 returned. Otherwise, if the template-name names a function, or set
12666 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12667 names a class, returns a TYPE_DECL for the specialization.
12669 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12670 uninstantiated templates. */
12673 cp_parser_template_id (cp_parser *parser,
12674 bool template_keyword_p,
12675 bool check_dependency_p,
12676 enum tag_types tag_type,
12677 bool is_declaration)
12683 cp_token_position start_of_id = 0;
12684 deferred_access_check *chk;
12685 vec<deferred_access_check, va_gc> *access_check;
12686 cp_token *next_token = NULL, *next_token_2 = NULL;
12687 bool is_identifier;
12689 /* If the next token corresponds to a template-id, there is no need
12691 next_token = cp_lexer_peek_token (parser->lexer);
12692 if (next_token->type == CPP_TEMPLATE_ID)
12694 struct tree_check *check_value;
12696 /* Get the stored value. */
12697 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12698 /* Perform any access checks that were deferred. */
12699 access_check = check_value->checks;
12702 FOR_EACH_VEC_ELT (*access_check, i, chk)
12703 perform_or_defer_access_check (chk->binfo,
12706 tf_warning_or_error);
12708 /* Return the stored value. */
12709 return check_value->value;
12712 /* Avoid performing name lookup if there is no possibility of
12713 finding a template-id. */
12714 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12715 || (next_token->type == CPP_NAME
12716 && !cp_parser_nth_token_starts_template_argument_list_p
12719 cp_parser_error (parser, "expected template-id");
12720 return error_mark_node;
12723 /* Remember where the template-id starts. */
12724 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12725 start_of_id = cp_lexer_token_position (parser->lexer, false);
12727 push_deferring_access_checks (dk_deferred);
12729 /* Parse the template-name. */
12730 is_identifier = false;
12731 templ = cp_parser_template_name (parser, template_keyword_p,
12732 check_dependency_p,
12736 if (templ == error_mark_node || is_identifier)
12738 pop_deferring_access_checks ();
12742 /* If we find the sequence `[:' after a template-name, it's probably
12743 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12744 parse correctly the argument list. */
12745 next_token = cp_lexer_peek_token (parser->lexer);
12746 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12747 if (next_token->type == CPP_OPEN_SQUARE
12748 && next_token->flags & DIGRAPH
12749 && next_token_2->type == CPP_COLON
12750 && !(next_token_2->flags & PREV_WHITE))
12752 cp_parser_parse_tentatively (parser);
12753 /* Change `:' into `::'. */
12754 next_token_2->type = CPP_SCOPE;
12755 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12757 cp_lexer_consume_token (parser->lexer);
12759 /* Parse the arguments. */
12760 arguments = cp_parser_enclosed_template_argument_list (parser);
12761 if (!cp_parser_parse_definitely (parser))
12763 /* If we couldn't parse an argument list, then we revert our changes
12764 and return simply an error. Maybe this is not a template-id
12766 next_token_2->type = CPP_COLON;
12767 cp_parser_error (parser, "expected %<<%>");
12768 pop_deferring_access_checks ();
12769 return error_mark_node;
12771 /* Otherwise, emit an error about the invalid digraph, but continue
12772 parsing because we got our argument list. */
12773 if (permerror (next_token->location,
12774 "%<<::%> cannot begin a template-argument list"))
12776 static bool hint = false;
12777 inform (next_token->location,
12778 "%<<:%> is an alternate spelling for %<[%>."
12779 " Insert whitespace between %<<%> and %<::%>");
12780 if (!hint && !flag_permissive)
12782 inform (next_token->location, "(if you use %<-fpermissive%> "
12783 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12784 "accept your code)");
12791 /* Look for the `<' that starts the template-argument-list. */
12792 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12794 pop_deferring_access_checks ();
12795 return error_mark_node;
12797 /* Parse the arguments. */
12798 arguments = cp_parser_enclosed_template_argument_list (parser);
12801 /* Build a representation of the specialization. */
12802 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12803 template_id = build_min_nt_loc (next_token->location,
12806 else if (DECL_TYPE_TEMPLATE_P (templ)
12807 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12809 bool entering_scope;
12810 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12811 template (rather than some instantiation thereof) only if
12812 is not nested within some other construct. For example, in
12813 "template <typename T> void f(T) { A<T>::", A<T> is just an
12814 instantiation of A. */
12815 entering_scope = (template_parm_scope_p ()
12816 && cp_lexer_next_token_is (parser->lexer,
12819 = finish_template_type (templ, arguments, entering_scope);
12823 /* If it's not a class-template or a template-template, it should be
12824 a function-template. */
12825 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12826 || TREE_CODE (templ) == OVERLOAD
12827 || BASELINK_P (templ)));
12829 template_id = lookup_template_function (templ, arguments);
12832 /* If parsing tentatively, replace the sequence of tokens that makes
12833 up the template-id with a CPP_TEMPLATE_ID token. That way,
12834 should we re-parse the token stream, we will not have to repeat
12835 the effort required to do the parse, nor will we issue duplicate
12836 error messages about problems during instantiation of the
12840 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12842 /* Reset the contents of the START_OF_ID token. */
12843 token->type = CPP_TEMPLATE_ID;
12844 /* Retrieve any deferred checks. Do not pop this access checks yet
12845 so the memory will not be reclaimed during token replacing below. */
12846 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12847 token->u.tree_check_value->value = template_id;
12848 token->u.tree_check_value->checks = get_deferred_access_checks ();
12849 token->keyword = RID_MAX;
12851 /* Purge all subsequent tokens. */
12852 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12854 /* ??? Can we actually assume that, if template_id ==
12855 error_mark_node, we will have issued a diagnostic to the
12856 user, as opposed to simply marking the tentative parse as
12858 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12859 error_at (token->location, "parse error in template argument list");
12862 pop_deferring_access_checks ();
12863 return template_id;
12866 /* Parse a template-name.
12871 The standard should actually say:
12875 operator-function-id
12877 A defect report has been filed about this issue.
12879 A conversion-function-id cannot be a template name because they cannot
12880 be part of a template-id. In fact, looking at this code:
12882 a.operator K<int>()
12884 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12885 It is impossible to call a templated conversion-function-id with an
12886 explicit argument list, since the only allowed template parameter is
12887 the type to which it is converting.
12889 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12890 `template' keyword, in a construction like:
12894 In that case `f' is taken to be a template-name, even though there
12895 is no way of knowing for sure.
12897 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12898 name refers to a set of overloaded functions, at least one of which
12899 is a template, or an IDENTIFIER_NODE with the name of the template,
12900 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12901 names are looked up inside uninstantiated templates. */
12904 cp_parser_template_name (cp_parser* parser,
12905 bool template_keyword_p,
12906 bool check_dependency_p,
12907 bool is_declaration,
12908 enum tag_types tag_type,
12909 bool *is_identifier)
12914 cp_token *token = cp_lexer_peek_token (parser->lexer);
12916 /* If the next token is `operator', then we have either an
12917 operator-function-id or a conversion-function-id. */
12918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12920 /* We don't know whether we're looking at an
12921 operator-function-id or a conversion-function-id. */
12922 cp_parser_parse_tentatively (parser);
12923 /* Try an operator-function-id. */
12924 identifier = cp_parser_operator_function_id (parser);
12925 /* If that didn't work, try a conversion-function-id. */
12926 if (!cp_parser_parse_definitely (parser))
12928 cp_parser_error (parser, "expected template-name");
12929 return error_mark_node;
12932 /* Look for the identifier. */
12934 identifier = cp_parser_identifier (parser);
12936 /* If we didn't find an identifier, we don't have a template-id. */
12937 if (identifier == error_mark_node)
12938 return error_mark_node;
12940 /* If the name immediately followed the `template' keyword, then it
12941 is a template-name. However, if the next token is not `<', then
12942 we do not treat it as a template-name, since it is not being used
12943 as part of a template-id. This enables us to handle constructs
12946 template <typename T> struct S { S(); };
12947 template <typename T> S<T>::S();
12949 correctly. We would treat `S' as a template -- if it were `S<T>'
12950 -- but we do not if there is no `<'. */
12952 if (processing_template_decl
12953 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12955 /* In a declaration, in a dependent context, we pretend that the
12956 "template" keyword was present in order to improve error
12957 recovery. For example, given:
12959 template <typename T> void f(T::X<int>);
12961 we want to treat "X<int>" as a template-id. */
12963 && !template_keyword_p
12964 && parser->scope && TYPE_P (parser->scope)
12965 && check_dependency_p
12966 && dependent_scope_p (parser->scope)
12967 /* Do not do this for dtors (or ctors), since they never
12968 need the template keyword before their name. */
12969 && !constructor_name_p (identifier, parser->scope))
12971 cp_token_position start = 0;
12973 /* Explain what went wrong. */
12974 error_at (token->location, "non-template %qD used as template",
12976 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12977 parser->scope, identifier);
12978 /* If parsing tentatively, find the location of the "<" token. */
12979 if (cp_parser_simulate_error (parser))
12980 start = cp_lexer_token_position (parser->lexer, true);
12981 /* Parse the template arguments so that we can issue error
12982 messages about them. */
12983 cp_lexer_consume_token (parser->lexer);
12984 cp_parser_enclosed_template_argument_list (parser);
12985 /* Skip tokens until we find a good place from which to
12986 continue parsing. */
12987 cp_parser_skip_to_closing_parenthesis (parser,
12988 /*recovering=*/true,
12990 /*consume_paren=*/false);
12991 /* If parsing tentatively, permanently remove the
12992 template argument list. That will prevent duplicate
12993 error messages from being issued about the missing
12994 "template" keyword. */
12996 cp_lexer_purge_tokens_after (parser->lexer, start);
12998 *is_identifier = true;
13002 /* If the "template" keyword is present, then there is generally
13003 no point in doing name-lookup, so we just return IDENTIFIER.
13004 But, if the qualifying scope is non-dependent then we can
13005 (and must) do name-lookup normally. */
13006 if (template_keyword_p
13008 || (TYPE_P (parser->scope)
13009 && dependent_type_p (parser->scope))))
13013 /* Look up the name. */
13014 decl = cp_parser_lookup_name (parser, identifier,
13016 /*is_template=*/true,
13017 /*is_namespace=*/false,
13018 check_dependency_p,
13019 /*ambiguous_decls=*/NULL,
13022 /* If DECL is a template, then the name was a template-name. */
13023 if (TREE_CODE (decl) == TEMPLATE_DECL)
13027 tree fn = NULL_TREE;
13029 /* The standard does not explicitly indicate whether a name that
13030 names a set of overloaded declarations, some of which are
13031 templates, is a template-name. However, such a name should
13032 be a template-name; otherwise, there is no way to form a
13033 template-id for the overloaded templates. */
13034 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13035 if (TREE_CODE (fns) == OVERLOAD)
13036 for (fn = fns; fn; fn = OVL_NEXT (fn))
13037 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13042 /* The name does not name a template. */
13043 cp_parser_error (parser, "expected template-name");
13044 return error_mark_node;
13048 /* If DECL is dependent, and refers to a function, then just return
13049 its name; we will look it up again during template instantiation. */
13050 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13052 tree scope = ovl_scope (decl);
13053 if (TYPE_P (scope) && dependent_type_p (scope))
13060 /* Parse a template-argument-list.
13062 template-argument-list:
13063 template-argument ... [opt]
13064 template-argument-list , template-argument ... [opt]
13066 Returns a TREE_VEC containing the arguments. */
13069 cp_parser_template_argument_list (cp_parser* parser)
13071 tree fixed_args[10];
13072 unsigned n_args = 0;
13073 unsigned alloced = 10;
13074 tree *arg_ary = fixed_args;
13076 bool saved_in_template_argument_list_p;
13078 bool saved_non_ice_p;
13080 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13081 parser->in_template_argument_list_p = true;
13082 /* Even if the template-id appears in an integral
13083 constant-expression, the contents of the argument list do
13085 saved_ice_p = parser->integral_constant_expression_p;
13086 parser->integral_constant_expression_p = false;
13087 saved_non_ice_p = parser->non_integral_constant_expression_p;
13088 parser->non_integral_constant_expression_p = false;
13090 /* Parse the arguments. */
13096 /* Consume the comma. */
13097 cp_lexer_consume_token (parser->lexer);
13099 /* Parse the template-argument. */
13100 argument = cp_parser_template_argument (parser);
13102 /* If the next token is an ellipsis, we're expanding a template
13104 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13106 if (argument == error_mark_node)
13108 cp_token *token = cp_lexer_peek_token (parser->lexer);
13109 error_at (token->location,
13110 "expected parameter pack before %<...%>");
13112 /* Consume the `...' token. */
13113 cp_lexer_consume_token (parser->lexer);
13115 /* Make the argument into a TYPE_PACK_EXPANSION or
13116 EXPR_PACK_EXPANSION. */
13117 argument = make_pack_expansion (argument);
13120 if (n_args == alloced)
13124 if (arg_ary == fixed_args)
13126 arg_ary = XNEWVEC (tree, alloced);
13127 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13130 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13132 arg_ary[n_args++] = argument;
13134 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13136 vec = make_tree_vec (n_args);
13139 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13141 if (arg_ary != fixed_args)
13143 parser->non_integral_constant_expression_p = saved_non_ice_p;
13144 parser->integral_constant_expression_p = saved_ice_p;
13145 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13146 #ifdef ENABLE_CHECKING
13147 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13152 /* Parse a template-argument.
13155 assignment-expression
13159 The representation is that of an assignment-expression, type-id, or
13160 id-expression -- except that the qualified id-expression is
13161 evaluated, so that the value returned is either a DECL or an
13164 Although the standard says "assignment-expression", it forbids
13165 throw-expressions or assignments in the template argument.
13166 Therefore, we use "conditional-expression" instead. */
13169 cp_parser_template_argument (cp_parser* parser)
13174 bool maybe_type_id = false;
13175 cp_token *token = NULL, *argument_start_token = NULL;
13176 location_t loc = 0;
13179 /* There's really no way to know what we're looking at, so we just
13180 try each alternative in order.
13184 In a template-argument, an ambiguity between a type-id and an
13185 expression is resolved to a type-id, regardless of the form of
13186 the corresponding template-parameter.
13188 Therefore, we try a type-id first. */
13189 cp_parser_parse_tentatively (parser);
13190 argument = cp_parser_template_type_arg (parser);
13191 /* If there was no error parsing the type-id but the next token is a
13192 '>>', our behavior depends on which dialect of C++ we're
13193 parsing. In C++98, we probably found a typo for '> >'. But there
13194 are type-id which are also valid expressions. For instance:
13196 struct X { int operator >> (int); };
13197 template <int V> struct Foo {};
13200 Here 'X()' is a valid type-id of a function type, but the user just
13201 wanted to write the expression "X() >> 5". Thus, we remember that we
13202 found a valid type-id, but we still try to parse the argument as an
13203 expression to see what happens.
13205 In C++0x, the '>>' will be considered two separate '>'
13207 if (!cp_parser_error_occurred (parser)
13208 && cxx_dialect == cxx98
13209 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13211 maybe_type_id = true;
13212 cp_parser_abort_tentative_parse (parser);
13216 /* If the next token isn't a `,' or a `>', then this argument wasn't
13217 really finished. This means that the argument is not a valid
13219 if (!cp_parser_next_token_ends_template_argument_p (parser))
13220 cp_parser_error (parser, "expected template-argument");
13221 /* If that worked, we're done. */
13222 if (cp_parser_parse_definitely (parser))
13225 /* We're still not sure what the argument will be. */
13226 cp_parser_parse_tentatively (parser);
13227 /* Try a template. */
13228 argument_start_token = cp_lexer_peek_token (parser->lexer);
13229 argument = cp_parser_id_expression (parser,
13230 /*template_keyword_p=*/false,
13231 /*check_dependency_p=*/true,
13233 /*declarator_p=*/false,
13234 /*optional_p=*/false);
13235 /* If the next token isn't a `,' or a `>', then this argument wasn't
13236 really finished. */
13237 if (!cp_parser_next_token_ends_template_argument_p (parser))
13238 cp_parser_error (parser, "expected template-argument");
13239 if (!cp_parser_error_occurred (parser))
13241 /* Figure out what is being referred to. If the id-expression
13242 was for a class template specialization, then we will have a
13243 TYPE_DECL at this point. There is no need to do name lookup
13244 at this point in that case. */
13245 if (TREE_CODE (argument) != TYPE_DECL)
13246 argument = cp_parser_lookup_name (parser, argument,
13248 /*is_template=*/template_p,
13249 /*is_namespace=*/false,
13250 /*check_dependency=*/true,
13251 /*ambiguous_decls=*/NULL,
13252 argument_start_token->location);
13253 if (TREE_CODE (argument) != TEMPLATE_DECL
13254 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13255 cp_parser_error (parser, "expected template-name");
13257 if (cp_parser_parse_definitely (parser))
13259 /* It must be a non-type argument. There permitted cases are given
13260 in [temp.arg.nontype]:
13262 -- an integral constant-expression of integral or enumeration
13265 -- the name of a non-type template-parameter; or
13267 -- the name of an object or function with external linkage...
13269 -- the address of an object or function with external linkage...
13271 -- a pointer to member... */
13272 /* Look for a non-type template parameter. */
13273 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13275 cp_parser_parse_tentatively (parser);
13276 argument = cp_parser_primary_expression (parser,
13277 /*address_p=*/false,
13279 /*template_arg_p=*/true,
13281 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13282 || !cp_parser_next_token_ends_template_argument_p (parser))
13283 cp_parser_simulate_error (parser);
13284 if (cp_parser_parse_definitely (parser))
13288 /* If the next token is "&", the argument must be the address of an
13289 object or function with external linkage. */
13290 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13293 loc = cp_lexer_peek_token (parser->lexer)->location;
13294 cp_lexer_consume_token (parser->lexer);
13296 /* See if we might have an id-expression. */
13297 token = cp_lexer_peek_token (parser->lexer);
13298 if (token->type == CPP_NAME
13299 || token->keyword == RID_OPERATOR
13300 || token->type == CPP_SCOPE
13301 || token->type == CPP_TEMPLATE_ID
13302 || token->type == CPP_NESTED_NAME_SPECIFIER)
13304 cp_parser_parse_tentatively (parser);
13305 argument = cp_parser_primary_expression (parser,
13308 /*template_arg_p=*/true,
13310 if (cp_parser_error_occurred (parser)
13311 || !cp_parser_next_token_ends_template_argument_p (parser))
13312 cp_parser_abort_tentative_parse (parser);
13317 if (TREE_CODE (argument) == INDIRECT_REF)
13319 gcc_assert (REFERENCE_REF_P (argument));
13320 argument = TREE_OPERAND (argument, 0);
13323 /* If we're in a template, we represent a qualified-id referring
13324 to a static data member as a SCOPE_REF even if the scope isn't
13325 dependent so that we can check access control later. */
13327 if (TREE_CODE (probe) == SCOPE_REF)
13328 probe = TREE_OPERAND (probe, 1);
13329 if (TREE_CODE (probe) == VAR_DECL)
13331 /* A variable without external linkage might still be a
13332 valid constant-expression, so no error is issued here
13333 if the external-linkage check fails. */
13334 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13335 cp_parser_simulate_error (parser);
13337 else if (is_overloaded_fn (argument))
13338 /* All overloaded functions are allowed; if the external
13339 linkage test does not pass, an error will be issued
13343 && (TREE_CODE (argument) == OFFSET_REF
13344 || TREE_CODE (argument) == SCOPE_REF))
13345 /* A pointer-to-member. */
13347 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13350 cp_parser_simulate_error (parser);
13352 if (cp_parser_parse_definitely (parser))
13355 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13356 tf_warning_or_error);
13361 /* If the argument started with "&", there are no other valid
13362 alternatives at this point. */
13365 cp_parser_error (parser, "invalid non-type template argument");
13366 return error_mark_node;
13369 /* If the argument wasn't successfully parsed as a type-id followed
13370 by '>>', the argument can only be a constant expression now.
13371 Otherwise, we try parsing the constant-expression tentatively,
13372 because the argument could really be a type-id. */
13374 cp_parser_parse_tentatively (parser);
13375 argument = cp_parser_constant_expression (parser,
13376 /*allow_non_constant_p=*/false,
13377 /*non_constant_p=*/NULL);
13378 argument = fold_non_dependent_expr (argument);
13379 if (!maybe_type_id)
13381 if (!cp_parser_next_token_ends_template_argument_p (parser))
13382 cp_parser_error (parser, "expected template-argument");
13383 if (cp_parser_parse_definitely (parser))
13385 /* We did our best to parse the argument as a non type-id, but that
13386 was the only alternative that matched (albeit with a '>' after
13387 it). We can assume it's just a typo from the user, and a
13388 diagnostic will then be issued. */
13389 return cp_parser_template_type_arg (parser);
13392 /* Parse an explicit-instantiation.
13394 explicit-instantiation:
13395 template declaration
13397 Although the standard says `declaration', what it really means is:
13399 explicit-instantiation:
13400 template decl-specifier-seq [opt] declarator [opt] ;
13402 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13403 supposed to be allowed. A defect report has been filed about this
13408 explicit-instantiation:
13409 storage-class-specifier template
13410 decl-specifier-seq [opt] declarator [opt] ;
13411 function-specifier template
13412 decl-specifier-seq [opt] declarator [opt] ; */
13415 cp_parser_explicit_instantiation (cp_parser* parser)
13417 int declares_class_or_enum;
13418 cp_decl_specifier_seq decl_specifiers;
13419 tree extension_specifier = NULL_TREE;
13421 timevar_push (TV_TEMPLATE_INST);
13423 /* Look for an (optional) storage-class-specifier or
13424 function-specifier. */
13425 if (cp_parser_allow_gnu_extensions_p (parser))
13427 extension_specifier
13428 = cp_parser_storage_class_specifier_opt (parser);
13429 if (!extension_specifier)
13430 extension_specifier
13431 = cp_parser_function_specifier_opt (parser,
13432 /*decl_specs=*/NULL);
13435 /* Look for the `template' keyword. */
13436 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13437 /* Let the front end know that we are processing an explicit
13439 begin_explicit_instantiation ();
13440 /* [temp.explicit] says that we are supposed to ignore access
13441 control while processing explicit instantiation directives. */
13442 push_deferring_access_checks (dk_no_check);
13443 /* Parse a decl-specifier-seq. */
13444 cp_parser_decl_specifier_seq (parser,
13445 CP_PARSER_FLAGS_OPTIONAL,
13447 &declares_class_or_enum);
13448 /* If there was exactly one decl-specifier, and it declared a class,
13449 and there's no declarator, then we have an explicit type
13451 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13455 type = check_tag_decl (&decl_specifiers,
13456 /*explicit_type_instantiation_p=*/true);
13457 /* Turn access control back on for names used during
13458 template instantiation. */
13459 pop_deferring_access_checks ();
13461 do_type_instantiation (type, extension_specifier,
13462 /*complain=*/tf_error);
13466 cp_declarator *declarator;
13469 /* Parse the declarator. */
13471 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13472 /*ctor_dtor_or_conv_p=*/NULL,
13473 /*parenthesized_p=*/NULL,
13474 /*member_p=*/false);
13475 if (declares_class_or_enum & 2)
13476 cp_parser_check_for_definition_in_return_type (declarator,
13477 decl_specifiers.type,
13478 decl_specifiers.locations[ds_type_spec]);
13479 if (declarator != cp_error_declarator)
13481 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13482 permerror (decl_specifiers.locations[ds_inline],
13483 "explicit instantiation shall not use"
13484 " %<inline%> specifier");
13485 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13486 permerror (decl_specifiers.locations[ds_constexpr],
13487 "explicit instantiation shall not use"
13488 " %<constexpr%> specifier");
13490 decl = grokdeclarator (declarator, &decl_specifiers,
13491 NORMAL, 0, &decl_specifiers.attributes);
13492 /* Turn access control back on for names used during
13493 template instantiation. */
13494 pop_deferring_access_checks ();
13495 /* Do the explicit instantiation. */
13496 do_decl_instantiation (decl, extension_specifier);
13500 pop_deferring_access_checks ();
13501 /* Skip the body of the explicit instantiation. */
13502 cp_parser_skip_to_end_of_statement (parser);
13505 /* We're done with the instantiation. */
13506 end_explicit_instantiation ();
13508 cp_parser_consume_semicolon_at_end_of_statement (parser);
13510 timevar_pop (TV_TEMPLATE_INST);
13513 /* Parse an explicit-specialization.
13515 explicit-specialization:
13516 template < > declaration
13518 Although the standard says `declaration', what it really means is:
13520 explicit-specialization:
13521 template <> decl-specifier [opt] init-declarator [opt] ;
13522 template <> function-definition
13523 template <> explicit-specialization
13524 template <> template-declaration */
13527 cp_parser_explicit_specialization (cp_parser* parser)
13529 bool need_lang_pop;
13530 cp_token *token = cp_lexer_peek_token (parser->lexer);
13532 /* Look for the `template' keyword. */
13533 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13534 /* Look for the `<'. */
13535 cp_parser_require (parser, CPP_LESS, RT_LESS);
13536 /* Look for the `>'. */
13537 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13538 /* We have processed another parameter list. */
13539 ++parser->num_template_parameter_lists;
13542 A template ... explicit specialization ... shall not have C
13544 if (current_lang_name == lang_name_c)
13546 error_at (token->location, "template specialization with C linkage");
13547 /* Give it C++ linkage to avoid confusing other parts of the
13549 push_lang_context (lang_name_cplusplus);
13550 need_lang_pop = true;
13553 need_lang_pop = false;
13554 /* Let the front end know that we are beginning a specialization. */
13555 if (!begin_specialization ())
13557 end_specialization ();
13561 /* If the next keyword is `template', we need to figure out whether
13562 or not we're looking a template-declaration. */
13563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13565 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13566 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13567 cp_parser_template_declaration_after_export (parser,
13568 /*member_p=*/false);
13570 cp_parser_explicit_specialization (parser);
13573 /* Parse the dependent declaration. */
13574 cp_parser_single_declaration (parser,
13576 /*member_p=*/false,
13577 /*explicit_specialization_p=*/true,
13578 /*friend_p=*/NULL);
13579 /* We're done with the specialization. */
13580 end_specialization ();
13581 /* For the erroneous case of a template with C linkage, we pushed an
13582 implicit C++ linkage scope; exit that scope now. */
13584 pop_lang_context ();
13585 /* We're done with this parameter list. */
13586 --parser->num_template_parameter_lists;
13589 /* Parse a type-specifier.
13592 simple-type-specifier
13595 elaborated-type-specifier
13603 Returns a representation of the type-specifier. For a
13604 class-specifier, enum-specifier, or elaborated-type-specifier, a
13605 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13607 The parser flags FLAGS is used to control type-specifier parsing.
13609 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13610 in a decl-specifier-seq.
13612 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13613 class-specifier, enum-specifier, or elaborated-type-specifier, then
13614 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13615 if a type is declared; 2 if it is defined. Otherwise, it is set to
13618 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13619 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13620 is set to FALSE. */
13623 cp_parser_type_specifier (cp_parser* parser,
13624 cp_parser_flags flags,
13625 cp_decl_specifier_seq *decl_specs,
13626 bool is_declaration,
13627 int* declares_class_or_enum,
13628 bool* is_cv_qualifier)
13630 tree type_spec = NULL_TREE;
13633 cp_decl_spec ds = ds_last;
13635 /* Assume this type-specifier does not declare a new type. */
13636 if (declares_class_or_enum)
13637 *declares_class_or_enum = 0;
13638 /* And that it does not specify a cv-qualifier. */
13639 if (is_cv_qualifier)
13640 *is_cv_qualifier = false;
13641 /* Peek at the next token. */
13642 token = cp_lexer_peek_token (parser->lexer);
13644 /* If we're looking at a keyword, we can use that to guide the
13645 production we choose. */
13646 keyword = token->keyword;
13650 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13651 goto elaborated_type_specifier;
13653 /* Look for the enum-specifier. */
13654 type_spec = cp_parser_enum_specifier (parser);
13655 /* If that worked, we're done. */
13658 if (declares_class_or_enum)
13659 *declares_class_or_enum = 2;
13661 cp_parser_set_decl_spec_type (decl_specs,
13664 /*type_definition_p=*/true);
13668 goto elaborated_type_specifier;
13670 /* Any of these indicate either a class-specifier, or an
13671 elaborated-type-specifier. */
13675 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13676 goto elaborated_type_specifier;
13678 /* Parse tentatively so that we can back up if we don't find a
13679 class-specifier. */
13680 cp_parser_parse_tentatively (parser);
13681 /* Look for the class-specifier. */
13682 type_spec = cp_parser_class_specifier (parser);
13683 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13684 /* If that worked, we're done. */
13685 if (cp_parser_parse_definitely (parser))
13687 if (declares_class_or_enum)
13688 *declares_class_or_enum = 2;
13690 cp_parser_set_decl_spec_type (decl_specs,
13693 /*type_definition_p=*/true);
13697 /* Fall through. */
13698 elaborated_type_specifier:
13699 /* We're declaring (not defining) a class or enum. */
13700 if (declares_class_or_enum)
13701 *declares_class_or_enum = 1;
13703 /* Fall through. */
13705 /* Look for an elaborated-type-specifier. */
13707 = (cp_parser_elaborated_type_specifier
13709 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13712 cp_parser_set_decl_spec_type (decl_specs,
13715 /*type_definition_p=*/false);
13720 if (is_cv_qualifier)
13721 *is_cv_qualifier = true;
13726 if (is_cv_qualifier)
13727 *is_cv_qualifier = true;
13732 if (is_cv_qualifier)
13733 *is_cv_qualifier = true;
13737 /* The `__complex__' keyword is a GNU extension. */
13745 /* Handle simple keywords. */
13750 set_and_check_decl_spec_loc (decl_specs, ds, token);
13751 decl_specs->any_specifiers_p = true;
13753 return cp_lexer_consume_token (parser->lexer)->u.value;
13756 /* If we do not already have a type-specifier, assume we are looking
13757 at a simple-type-specifier. */
13758 type_spec = cp_parser_simple_type_specifier (parser,
13762 /* If we didn't find a type-specifier, and a type-specifier was not
13763 optional in this context, issue an error message. */
13764 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13766 cp_parser_error (parser, "expected type specifier");
13767 return error_mark_node;
13773 /* Parse a simple-type-specifier.
13775 simple-type-specifier:
13776 :: [opt] nested-name-specifier [opt] type-name
13777 :: [opt] nested-name-specifier template template-id
13792 simple-type-specifier:
13794 decltype ( expression )
13797 __underlying_type ( type-id )
13801 simple-type-specifier:
13803 __typeof__ unary-expression
13804 __typeof__ ( type-id )
13806 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13807 appropriately updated. */
13810 cp_parser_simple_type_specifier (cp_parser* parser,
13811 cp_decl_specifier_seq *decl_specs,
13812 cp_parser_flags flags)
13814 tree type = NULL_TREE;
13817 /* Peek at the next token. */
13818 token = cp_lexer_peek_token (parser->lexer);
13820 /* If we're looking at a keyword, things are easy. */
13821 switch (token->keyword)
13825 decl_specs->explicit_char_p = true;
13826 type = char_type_node;
13829 type = char16_type_node;
13832 type = char32_type_node;
13835 type = wchar_type_node;
13838 type = boolean_type_node;
13841 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13842 type = short_integer_type_node;
13846 decl_specs->explicit_int_p = true;
13847 type = integer_type_node;
13850 if (!int128_integer_type_node)
13853 decl_specs->explicit_int128_p = true;
13854 type = int128_integer_type_node;
13858 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13859 type = long_integer_type_node;
13862 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13863 type = integer_type_node;
13866 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13867 type = unsigned_type_node;
13870 type = float_type_node;
13873 type = double_type_node;
13876 type = void_type_node;
13880 maybe_warn_cpp0x (CPP0X_AUTO);
13881 type = make_auto ();
13885 /* Since DR 743, decltype can either be a simple-type-specifier by
13886 itself or begin a nested-name-specifier. Parsing it will replace
13887 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13888 handling below decide what to do. */
13889 cp_parser_decltype (parser);
13890 cp_lexer_set_token_position (parser->lexer, token);
13894 /* Consume the `typeof' token. */
13895 cp_lexer_consume_token (parser->lexer);
13896 /* Parse the operand to `typeof'. */
13897 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13898 /* If it is not already a TYPE, take its type. */
13899 if (!TYPE_P (type))
13900 type = finish_typeof (type);
13903 cp_parser_set_decl_spec_type (decl_specs, type,
13905 /*type_definition_p=*/false);
13909 case RID_UNDERLYING_TYPE:
13910 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13912 cp_parser_set_decl_spec_type (decl_specs, type,
13914 /*type_definition_p=*/false);
13919 case RID_DIRECT_BASES:
13920 type = cp_parser_trait_expr (parser, token->keyword);
13922 cp_parser_set_decl_spec_type (decl_specs, type,
13924 /*type_definition_p=*/false);
13930 /* If token is an already-parsed decltype not followed by ::,
13931 it's a simple-type-specifier. */
13932 if (token->type == CPP_DECLTYPE
13933 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13935 type = token->u.value;
13937 cp_parser_set_decl_spec_type (decl_specs, type,
13939 /*type_definition_p=*/false);
13940 cp_lexer_consume_token (parser->lexer);
13944 /* If the type-specifier was for a built-in type, we're done. */
13947 /* Record the type. */
13949 && (token->keyword != RID_SIGNED
13950 && token->keyword != RID_UNSIGNED
13951 && token->keyword != RID_SHORT
13952 && token->keyword != RID_LONG))
13953 cp_parser_set_decl_spec_type (decl_specs,
13956 /*type_definition_p=*/false);
13958 decl_specs->any_specifiers_p = true;
13960 /* Consume the token. */
13961 cp_lexer_consume_token (parser->lexer);
13963 /* There is no valid C++ program where a non-template type is
13964 followed by a "<". That usually indicates that the user thought
13965 that the type was a template. */
13966 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13969 return TYPE_NAME (type);
13972 /* The type-specifier must be a user-defined type. */
13973 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13978 /* Don't gobble tokens or issue error messages if this is an
13979 optional type-specifier. */
13980 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13981 cp_parser_parse_tentatively (parser);
13983 /* Look for the optional `::' operator. */
13985 = (cp_parser_global_scope_opt (parser,
13986 /*current_scope_valid_p=*/false)
13988 /* Look for the nested-name specifier. */
13990 = (cp_parser_nested_name_specifier_opt (parser,
13991 /*typename_keyword_p=*/false,
13992 /*check_dependency_p=*/true,
13994 /*is_declaration=*/false)
13996 token = cp_lexer_peek_token (parser->lexer);
13997 /* If we have seen a nested-name-specifier, and the next token
13998 is `template', then we are using the template-id production. */
14000 && cp_parser_optional_template_keyword (parser))
14002 /* Look for the template-id. */
14003 type = cp_parser_template_id (parser,
14004 /*template_keyword_p=*/true,
14005 /*check_dependency_p=*/true,
14007 /*is_declaration=*/false);
14008 /* If the template-id did not name a type, we are out of
14010 if (TREE_CODE (type) != TYPE_DECL)
14012 cp_parser_error (parser, "expected template-id for type");
14016 /* Otherwise, look for a type-name. */
14018 type = cp_parser_type_name (parser);
14019 /* Keep track of all name-lookups performed in class scopes. */
14023 && TREE_CODE (type) == TYPE_DECL
14024 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
14025 maybe_note_name_used_in_class (DECL_NAME (type), type);
14026 /* If it didn't work out, we don't have a TYPE. */
14027 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14028 && !cp_parser_parse_definitely (parser))
14030 if (type && decl_specs)
14031 cp_parser_set_decl_spec_type (decl_specs, type,
14033 /*type_definition_p=*/false);
14036 /* If we didn't get a type-name, issue an error message. */
14037 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14039 cp_parser_error (parser, "expected type-name");
14040 return error_mark_node;
14043 if (type && type != error_mark_node)
14045 /* See if TYPE is an Objective-C type, and if so, parse and
14046 accept any protocol references following it. Do this before
14047 the cp_parser_check_for_invalid_template_id() call, because
14048 Objective-C types can be followed by '<...>' which would
14049 enclose protocol names rather than template arguments, and so
14050 everything is fine. */
14051 if (c_dialect_objc () && !parser->scope
14052 && (objc_is_id (type) || objc_is_class_name (type)))
14054 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14055 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14057 /* Clobber the "unqualified" type previously entered into
14058 DECL_SPECS with the new, improved protocol-qualified version. */
14060 decl_specs->type = qual_type;
14065 /* There is no valid C++ program where a non-template type is
14066 followed by a "<". That usually indicates that the user
14067 thought that the type was a template. */
14068 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14076 /* Parse a type-name.
14082 simple-template-id [in c++0x]
14090 Returns a TYPE_DECL for the type. */
14093 cp_parser_type_name (cp_parser* parser)
14097 /* We can't know yet whether it is a class-name or not. */
14098 cp_parser_parse_tentatively (parser);
14099 /* Try a class-name. */
14100 type_decl = cp_parser_class_name (parser,
14101 /*typename_keyword_p=*/false,
14102 /*template_keyword_p=*/false,
14104 /*check_dependency_p=*/true,
14105 /*class_head_p=*/false,
14106 /*is_declaration=*/false);
14107 /* If it's not a class-name, keep looking. */
14108 if (!cp_parser_parse_definitely (parser))
14110 if (cxx_dialect < cxx0x)
14111 /* It must be a typedef-name or an enum-name. */
14112 return cp_parser_nonclass_name (parser);
14114 cp_parser_parse_tentatively (parser);
14115 /* It is either a simple-template-id representing an
14116 instantiation of an alias template... */
14117 type_decl = cp_parser_template_id (parser,
14118 /*template_keyword_p=*/false,
14119 /*check_dependency_p=*/false,
14121 /*is_declaration=*/false);
14122 /* Note that this must be an instantiation of an alias template
14123 because [temp.names]/6 says:
14125 A template-id that names an alias template specialization
14128 Whereas [temp.names]/7 says:
14130 A simple-template-id that names a class template
14131 specialization is a class-name. */
14132 if (type_decl != NULL_TREE
14133 && TREE_CODE (type_decl) == TYPE_DECL
14134 && TYPE_DECL_ALIAS_P (type_decl))
14135 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14137 cp_parser_simulate_error (parser);
14139 if (!cp_parser_parse_definitely (parser))
14140 /* ... Or a typedef-name or an enum-name. */
14141 return cp_parser_nonclass_name (parser);
14147 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14155 Returns a TYPE_DECL for the type. */
14158 cp_parser_nonclass_name (cp_parser* parser)
14163 cp_token *token = cp_lexer_peek_token (parser->lexer);
14164 identifier = cp_parser_identifier (parser);
14165 if (identifier == error_mark_node)
14166 return error_mark_node;
14168 /* Look up the type-name. */
14169 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14171 if (TREE_CODE (type_decl) == USING_DECL)
14173 if (!DECL_DEPENDENT_P (type_decl))
14174 type_decl = strip_using_decl (type_decl);
14175 else if (USING_DECL_TYPENAME_P (type_decl))
14177 /* We have found a type introduced by a using
14178 declaration at class scope that refers to a dependent
14181 using typename :: [opt] nested-name-specifier unqualified-id ;
14183 type_decl = make_typename_type (TREE_TYPE (type_decl),
14184 DECL_NAME (type_decl),
14185 typename_type, tf_error);
14186 if (type_decl != error_mark_node)
14187 type_decl = TYPE_NAME (type_decl);
14191 if (TREE_CODE (type_decl) != TYPE_DECL
14192 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14194 /* See if this is an Objective-C type. */
14195 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14196 tree type = objc_get_protocol_qualified_type (identifier, protos);
14198 type_decl = TYPE_NAME (type);
14201 /* Issue an error if we did not find a type-name. */
14202 if (TREE_CODE (type_decl) != TYPE_DECL
14203 /* In Objective-C, we have the complication that class names are
14204 normally type names and start declarations (eg, the
14205 "NSObject" in "NSObject *object;"), but can be used in an
14206 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14207 is an expression. So, a classname followed by a dot is not a
14208 valid type-name. */
14209 || (objc_is_class_name (TREE_TYPE (type_decl))
14210 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14212 if (!cp_parser_simulate_error (parser))
14213 cp_parser_name_lookup_error (parser, identifier, type_decl,
14214 NLE_TYPE, token->location);
14215 return error_mark_node;
14217 /* Remember that the name was used in the definition of the
14218 current class so that we can check later to see if the
14219 meaning would have been different after the class was
14220 entirely defined. */
14221 else if (type_decl != error_mark_node
14223 maybe_note_name_used_in_class (identifier, type_decl);
14228 /* Parse an elaborated-type-specifier. Note that the grammar given
14229 here incorporates the resolution to DR68.
14231 elaborated-type-specifier:
14232 class-key :: [opt] nested-name-specifier [opt] identifier
14233 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14234 enum-key :: [opt] nested-name-specifier [opt] identifier
14235 typename :: [opt] nested-name-specifier identifier
14236 typename :: [opt] nested-name-specifier template [opt]
14241 elaborated-type-specifier:
14242 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14243 class-key attributes :: [opt] nested-name-specifier [opt]
14244 template [opt] template-id
14245 enum attributes :: [opt] nested-name-specifier [opt] identifier
14247 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14248 declared `friend'. If IS_DECLARATION is TRUE, then this
14249 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14250 something is being declared.
14252 Returns the TYPE specified. */
14255 cp_parser_elaborated_type_specifier (cp_parser* parser,
14257 bool is_declaration)
14259 enum tag_types tag_type;
14261 tree type = NULL_TREE;
14262 tree attributes = NULL_TREE;
14264 cp_token *token = NULL;
14266 /* See if we're looking at the `enum' keyword. */
14267 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14269 /* Consume the `enum' token. */
14270 cp_lexer_consume_token (parser->lexer);
14271 /* Remember that it's an enumeration type. */
14272 tag_type = enum_type;
14273 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14274 enums) is used here. */
14275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14276 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14278 pedwarn (input_location, 0, "elaborated-type-specifier "
14279 "for a scoped enum must not use the %<%D%> keyword",
14280 cp_lexer_peek_token (parser->lexer)->u.value);
14281 /* Consume the `struct' or `class' and parse it anyway. */
14282 cp_lexer_consume_token (parser->lexer);
14284 /* Parse the attributes. */
14285 attributes = cp_parser_attributes_opt (parser);
14287 /* Or, it might be `typename'. */
14288 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14291 /* Consume the `typename' token. */
14292 cp_lexer_consume_token (parser->lexer);
14293 /* Remember that it's a `typename' type. */
14294 tag_type = typename_type;
14296 /* Otherwise it must be a class-key. */
14299 tag_type = cp_parser_class_key (parser);
14300 if (tag_type == none_type)
14301 return error_mark_node;
14302 /* Parse the attributes. */
14303 attributes = cp_parser_attributes_opt (parser);
14306 /* Look for the `::' operator. */
14307 globalscope = cp_parser_global_scope_opt (parser,
14308 /*current_scope_valid_p=*/false);
14309 /* Look for the nested-name-specifier. */
14310 if (tag_type == typename_type && !globalscope)
14312 if (!cp_parser_nested_name_specifier (parser,
14313 /*typename_keyword_p=*/true,
14314 /*check_dependency_p=*/true,
14317 return error_mark_node;
14320 /* Even though `typename' is not present, the proposed resolution
14321 to Core Issue 180 says that in `class A<T>::B', `B' should be
14322 considered a type-name, even if `A<T>' is dependent. */
14323 cp_parser_nested_name_specifier_opt (parser,
14324 /*typename_keyword_p=*/true,
14325 /*check_dependency_p=*/true,
14328 /* For everything but enumeration types, consider a template-id.
14329 For an enumeration type, consider only a plain identifier. */
14330 if (tag_type != enum_type)
14332 bool template_p = false;
14335 /* Allow the `template' keyword. */
14336 template_p = cp_parser_optional_template_keyword (parser);
14337 /* If we didn't see `template', we don't know if there's a
14338 template-id or not. */
14340 cp_parser_parse_tentatively (parser);
14341 /* Parse the template-id. */
14342 token = cp_lexer_peek_token (parser->lexer);
14343 decl = cp_parser_template_id (parser, template_p,
14344 /*check_dependency_p=*/true,
14347 /* If we didn't find a template-id, look for an ordinary
14349 if (!template_p && !cp_parser_parse_definitely (parser))
14351 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14352 in effect, then we must assume that, upon instantiation, the
14353 template will correspond to a class. */
14354 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14355 && tag_type == typename_type)
14356 type = make_typename_type (parser->scope, decl,
14358 /*complain=*/tf_error);
14359 /* If the `typename' keyword is in effect and DECL is not a type
14360 decl, then type is non existent. */
14361 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14363 else if (TREE_CODE (decl) == TYPE_DECL)
14364 type = check_elaborated_type_specifier (tag_type, decl,
14365 /*allow_template_p=*/true);
14366 else if (decl == error_mark_node)
14367 type = error_mark_node;
14372 token = cp_lexer_peek_token (parser->lexer);
14373 identifier = cp_parser_identifier (parser);
14375 if (identifier == error_mark_node)
14377 parser->scope = NULL_TREE;
14378 return error_mark_node;
14381 /* For a `typename', we needn't call xref_tag. */
14382 if (tag_type == typename_type
14383 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14384 return cp_parser_make_typename_type (parser, parser->scope,
14387 /* Look up a qualified name in the usual way. */
14391 tree ambiguous_decls;
14393 decl = cp_parser_lookup_name (parser, identifier,
14395 /*is_template=*/false,
14396 /*is_namespace=*/false,
14397 /*check_dependency=*/true,
14401 /* If the lookup was ambiguous, an error will already have been
14403 if (ambiguous_decls)
14404 return error_mark_node;
14406 /* If we are parsing friend declaration, DECL may be a
14407 TEMPLATE_DECL tree node here. However, we need to check
14408 whether this TEMPLATE_DECL results in valid code. Consider
14409 the following example:
14412 template <class T> class C {};
14415 template <class T> friend class N::C; // #1, valid code
14417 template <class T> class Y {
14418 friend class N::C; // #2, invalid code
14421 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14422 name lookup of `N::C'. We see that friend declaration must
14423 be template for the code to be valid. Note that
14424 processing_template_decl does not work here since it is
14425 always 1 for the above two cases. */
14427 decl = (cp_parser_maybe_treat_template_as_class
14428 (decl, /*tag_name_p=*/is_friend
14429 && parser->num_template_parameter_lists));
14431 if (TREE_CODE (decl) != TYPE_DECL)
14433 cp_parser_diagnose_invalid_type_name (parser,
14437 return error_mark_node;
14440 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14442 bool allow_template = (parser->num_template_parameter_lists
14443 || DECL_SELF_REFERENCE_P (decl));
14444 type = check_elaborated_type_specifier (tag_type, decl,
14447 if (type == error_mark_node)
14448 return error_mark_node;
14451 /* Forward declarations of nested types, such as
14456 are invalid unless all components preceding the final '::'
14457 are complete. If all enclosing types are complete, these
14458 declarations become merely pointless.
14460 Invalid forward declarations of nested types are errors
14461 caught elsewhere in parsing. Those that are pointless arrive
14464 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14465 && !is_friend && !processing_explicit_instantiation)
14466 warning (0, "declaration %qD does not declare anything", decl);
14468 type = TREE_TYPE (decl);
14472 /* An elaborated-type-specifier sometimes introduces a new type and
14473 sometimes names an existing type. Normally, the rule is that it
14474 introduces a new type only if there is not an existing type of
14475 the same name already in scope. For example, given:
14478 void f() { struct S s; }
14480 the `struct S' in the body of `f' is the same `struct S' as in
14481 the global scope; the existing definition is used. However, if
14482 there were no global declaration, this would introduce a new
14483 local class named `S'.
14485 An exception to this rule applies to the following code:
14487 namespace N { struct S; }
14489 Here, the elaborated-type-specifier names a new type
14490 unconditionally; even if there is already an `S' in the
14491 containing scope this declaration names a new type.
14492 This exception only applies if the elaborated-type-specifier
14493 forms the complete declaration:
14497 A declaration consisting solely of `class-key identifier ;' is
14498 either a redeclaration of the name in the current scope or a
14499 forward declaration of the identifier as a class name. It
14500 introduces the name into the current scope.
14502 We are in this situation precisely when the next token is a `;'.
14504 An exception to the exception is that a `friend' declaration does
14505 *not* name a new type; i.e., given:
14507 struct S { friend struct T; };
14509 `T' is not a new type in the scope of `S'.
14511 Also, `new struct S' or `sizeof (struct S)' never results in the
14512 definition of a new type; a new type can only be declared in a
14513 declaration context. */
14519 /* Friends have special name lookup rules. */
14520 ts = ts_within_enclosing_non_class;
14521 else if (is_declaration
14522 && cp_lexer_next_token_is (parser->lexer,
14524 /* This is a `class-key identifier ;' */
14530 (parser->num_template_parameter_lists
14531 && (cp_parser_next_token_starts_class_definition_p (parser)
14532 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14533 /* An unqualified name was used to reference this type, so
14534 there were no qualifying templates. */
14535 if (!cp_parser_check_template_parameters (parser,
14536 /*num_templates=*/0,
14538 /*declarator=*/NULL))
14539 return error_mark_node;
14540 type = xref_tag (tag_type, identifier, ts, template_p);
14544 if (type == error_mark_node)
14545 return error_mark_node;
14547 /* Allow attributes on forward declarations of classes. */
14550 if (TREE_CODE (type) == TYPENAME_TYPE)
14551 warning (OPT_Wattributes,
14552 "attributes ignored on uninstantiated type");
14553 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14554 && ! processing_explicit_instantiation)
14555 warning (OPT_Wattributes,
14556 "attributes ignored on template instantiation");
14557 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14558 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14560 warning (OPT_Wattributes,
14561 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14564 if (tag_type != enum_type)
14566 /* Indicate whether this class was declared as a `class' or as a
14568 if (TREE_CODE (type) == RECORD_TYPE)
14569 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14570 cp_parser_check_class_key (tag_type, type);
14573 /* A "<" cannot follow an elaborated type specifier. If that
14574 happens, the user was probably trying to form a template-id. */
14575 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14581 /* Parse an enum-specifier.
14584 enum-head { enumerator-list [opt] }
14585 enum-head { enumerator-list , } [C++0x]
14588 enum-key identifier [opt] enum-base [opt]
14589 enum-key nested-name-specifier identifier enum-base [opt]
14594 enum struct [C++0x]
14597 : type-specifier-seq
14599 opaque-enum-specifier:
14600 enum-key identifier enum-base [opt] ;
14603 enum-key attributes[opt] identifier [opt] enum-base [opt]
14604 { enumerator-list [opt] }attributes[opt]
14605 enum-key attributes[opt] identifier [opt] enum-base [opt]
14606 { enumerator-list, }attributes[opt] [C++0x]
14608 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14609 if the token stream isn't an enum-specifier after all. */
14612 cp_parser_enum_specifier (cp_parser* parser)
14615 tree type = NULL_TREE;
14617 tree nested_name_specifier = NULL_TREE;
14619 bool scoped_enum_p = false;
14620 bool has_underlying_type = false;
14621 bool nested_being_defined = false;
14622 bool new_value_list = false;
14623 bool is_new_type = false;
14624 bool is_anonymous = false;
14625 tree underlying_type = NULL_TREE;
14626 cp_token *type_start_token = NULL;
14627 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14629 parser->colon_corrects_to_scope_p = false;
14631 /* Parse tentatively so that we can back up if we don't find a
14633 cp_parser_parse_tentatively (parser);
14635 /* Caller guarantees that the current token is 'enum', an identifier
14636 possibly follows, and the token after that is an opening brace.
14637 If we don't have an identifier, fabricate an anonymous name for
14638 the enumeration being defined. */
14639 cp_lexer_consume_token (parser->lexer);
14641 /* Parse the "class" or "struct", which indicates a scoped
14642 enumeration type in C++0x. */
14643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14644 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14646 if (cxx_dialect < cxx0x)
14647 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14649 /* Consume the `struct' or `class' token. */
14650 cp_lexer_consume_token (parser->lexer);
14652 scoped_enum_p = true;
14655 attributes = cp_parser_attributes_opt (parser);
14657 /* Clear the qualification. */
14658 parser->scope = NULL_TREE;
14659 parser->qualifying_scope = NULL_TREE;
14660 parser->object_scope = NULL_TREE;
14662 /* Figure out in what scope the declaration is being placed. */
14663 prev_scope = current_scope ();
14665 type_start_token = cp_lexer_peek_token (parser->lexer);
14667 push_deferring_access_checks (dk_no_check);
14668 nested_name_specifier
14669 = cp_parser_nested_name_specifier_opt (parser,
14670 /*typename_keyword_p=*/true,
14671 /*check_dependency_p=*/false,
14673 /*is_declaration=*/false);
14675 if (nested_name_specifier)
14679 identifier = cp_parser_identifier (parser);
14680 name = cp_parser_lookup_name (parser, identifier,
14682 /*is_template=*/false,
14683 /*is_namespace=*/false,
14684 /*check_dependency=*/true,
14685 /*ambiguous_decls=*/NULL,
14687 if (name && name != error_mark_node)
14689 type = TREE_TYPE (name);
14690 if (TREE_CODE (type) == TYPENAME_TYPE)
14692 /* Are template enums allowed in ISO? */
14693 if (template_parm_scope_p ())
14694 pedwarn (type_start_token->location, OPT_Wpedantic,
14695 "%qD is an enumeration template", name);
14696 /* ignore a typename reference, for it will be solved by name
14702 error_at (type_start_token->location,
14703 "%qD is not an enumerator-name", identifier);
14707 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14708 identifier = cp_parser_identifier (parser);
14711 identifier = make_anon_name ();
14712 is_anonymous = true;
14715 pop_deferring_access_checks ();
14717 /* Check for the `:' that denotes a specified underlying type in C++0x.
14718 Note that a ':' could also indicate a bitfield width, however. */
14719 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14721 cp_decl_specifier_seq type_specifiers;
14723 /* Consume the `:'. */
14724 cp_lexer_consume_token (parser->lexer);
14726 /* Parse the type-specifier-seq. */
14727 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14728 /*is_trailing_return=*/false,
14731 /* At this point this is surely not elaborated type specifier. */
14732 if (!cp_parser_parse_definitely (parser))
14735 if (cxx_dialect < cxx0x)
14736 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14738 has_underlying_type = true;
14740 /* If that didn't work, stop. */
14741 if (type_specifiers.type != error_mark_node)
14743 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14744 /*initialized=*/0, NULL);
14745 if (underlying_type == error_mark_node)
14746 underlying_type = NULL_TREE;
14750 /* Look for the `{' but don't consume it yet. */
14751 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14753 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14755 cp_parser_error (parser, "expected %<{%>");
14756 if (has_underlying_type)
14762 /* An opaque-enum-specifier must have a ';' here. */
14763 if ((scoped_enum_p || underlying_type)
14764 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14766 cp_parser_error (parser, "expected %<;%> or %<{%>");
14767 if (has_underlying_type)
14775 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14778 if (nested_name_specifier)
14780 if (CLASS_TYPE_P (nested_name_specifier))
14782 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14783 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14784 push_scope (nested_name_specifier);
14786 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14788 push_nested_namespace (nested_name_specifier);
14792 /* Issue an error message if type-definitions are forbidden here. */
14793 if (!cp_parser_check_type_definition (parser))
14794 type = error_mark_node;
14796 /* Create the new type. We do this before consuming the opening
14797 brace so the enum will be recorded as being on the line of its
14798 tag (or the 'enum' keyword, if there is no tag). */
14799 type = start_enum (identifier, type, underlying_type,
14800 scoped_enum_p, &is_new_type);
14802 /* If the next token is not '{' it is an opaque-enum-specifier or an
14803 elaborated-type-specifier. */
14804 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14806 timevar_push (TV_PARSE_ENUM);
14807 if (nested_name_specifier)
14809 /* The following catches invalid code such as:
14810 enum class S<int>::E { A, B, C }; */
14811 if (!processing_specialization
14812 && CLASS_TYPE_P (nested_name_specifier)
14813 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14814 error_at (type_start_token->location, "cannot add an enumerator "
14815 "list to a template instantiation");
14817 /* If that scope does not contain the scope in which the
14818 class was originally declared, the program is invalid. */
14819 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14821 if (at_namespace_scope_p ())
14822 error_at (type_start_token->location,
14823 "declaration of %qD in namespace %qD which does not "
14825 type, prev_scope, nested_name_specifier);
14827 error_at (type_start_token->location,
14828 "declaration of %qD in %qD which does not enclose %qD",
14829 type, prev_scope, nested_name_specifier);
14830 type = error_mark_node;
14835 begin_scope (sk_scoped_enum, type);
14837 /* Consume the opening brace. */
14838 cp_lexer_consume_token (parser->lexer);
14840 if (type == error_mark_node)
14841 ; /* Nothing to add */
14842 else if (OPAQUE_ENUM_P (type)
14843 || (cxx_dialect > cxx98 && processing_specialization))
14845 new_value_list = true;
14846 SET_OPAQUE_ENUM_P (type, false);
14847 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14851 error_at (type_start_token->location, "multiple definition of %q#T", type);
14852 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14853 "previous definition here");
14854 type = error_mark_node;
14857 if (type == error_mark_node)
14858 cp_parser_skip_to_end_of_block_or_statement (parser);
14859 /* If the next token is not '}', then there are some enumerators. */
14860 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14861 cp_parser_enumerator_list (parser, type);
14863 /* Consume the final '}'. */
14864 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14868 timevar_pop (TV_PARSE_ENUM);
14872 /* If a ';' follows, then it is an opaque-enum-specifier
14873 and additional restrictions apply. */
14874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14877 error_at (type_start_token->location,
14878 "opaque-enum-specifier without name");
14879 else if (nested_name_specifier)
14880 error_at (type_start_token->location,
14881 "opaque-enum-specifier must use a simple identifier");
14885 /* Look for trailing attributes to apply to this enumeration, and
14886 apply them if appropriate. */
14887 if (cp_parser_allow_gnu_extensions_p (parser))
14889 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14890 trailing_attr = chainon (trailing_attr, attributes);
14891 cplus_decl_attributes (&type,
14893 (int) ATTR_FLAG_TYPE_IN_PLACE);
14896 /* Finish up the enumeration. */
14897 if (type != error_mark_node)
14899 if (new_value_list)
14900 finish_enum_value_list (type);
14902 finish_enum (type);
14905 if (nested_name_specifier)
14907 if (CLASS_TYPE_P (nested_name_specifier))
14909 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14910 pop_scope (nested_name_specifier);
14912 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14914 pop_nested_namespace (nested_name_specifier);
14918 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14922 /* Parse an enumerator-list. The enumerators all have the indicated
14926 enumerator-definition
14927 enumerator-list , enumerator-definition */
14930 cp_parser_enumerator_list (cp_parser* parser, tree type)
14934 /* Parse an enumerator-definition. */
14935 cp_parser_enumerator_definition (parser, type);
14937 /* If the next token is not a ',', we've reached the end of
14939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14941 /* Otherwise, consume the `,' and keep going. */
14942 cp_lexer_consume_token (parser->lexer);
14943 /* If the next token is a `}', there is a trailing comma. */
14944 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14946 if (cxx_dialect < cxx0x && !in_system_header)
14947 pedwarn (input_location, OPT_Wpedantic,
14948 "comma at end of enumerator list");
14954 /* Parse an enumerator-definition. The enumerator has the indicated
14957 enumerator-definition:
14959 enumerator = constant-expression
14965 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14971 /* Save the input location because we are interested in the location
14972 of the identifier and not the location of the explicit value. */
14973 loc = cp_lexer_peek_token (parser->lexer)->location;
14975 /* Look for the identifier. */
14976 identifier = cp_parser_identifier (parser);
14977 if (identifier == error_mark_node)
14980 /* If the next token is an '=', then there is an explicit value. */
14981 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14983 /* Consume the `=' token. */
14984 cp_lexer_consume_token (parser->lexer);
14985 /* Parse the value. */
14986 value = cp_parser_constant_expression (parser,
14987 /*allow_non_constant_p=*/false,
14993 /* If we are processing a template, make sure the initializer of the
14994 enumerator doesn't contain any bare template parameter pack. */
14995 if (check_for_bare_parameter_packs (value))
14996 value = error_mark_node;
14998 /* integral_constant_value will pull out this expression, so make sure
14999 it's folded as appropriate. */
15000 value = fold_non_dependent_expr (value);
15002 /* Create the enumerator. */
15003 build_enumerator (identifier, value, type, loc);
15006 /* Parse a namespace-name.
15009 original-namespace-name
15012 Returns the NAMESPACE_DECL for the namespace. */
15015 cp_parser_namespace_name (cp_parser* parser)
15018 tree namespace_decl;
15020 cp_token *token = cp_lexer_peek_token (parser->lexer);
15022 /* Get the name of the namespace. */
15023 identifier = cp_parser_identifier (parser);
15024 if (identifier == error_mark_node)
15025 return error_mark_node;
15027 /* Look up the identifier in the currently active scope. Look only
15028 for namespaces, due to:
15030 [basic.lookup.udir]
15032 When looking up a namespace-name in a using-directive or alias
15033 definition, only namespace names are considered.
15037 [basic.lookup.qual]
15039 During the lookup of a name preceding the :: scope resolution
15040 operator, object, function, and enumerator names are ignored.
15042 (Note that cp_parser_qualifying_entity only calls this
15043 function if the token after the name is the scope resolution
15045 namespace_decl = cp_parser_lookup_name (parser, identifier,
15047 /*is_template=*/false,
15048 /*is_namespace=*/true,
15049 /*check_dependency=*/true,
15050 /*ambiguous_decls=*/NULL,
15052 /* If it's not a namespace, issue an error. */
15053 if (namespace_decl == error_mark_node
15054 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15056 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15057 error_at (token->location, "%qD is not a namespace-name", identifier);
15058 cp_parser_error (parser, "expected namespace-name");
15059 namespace_decl = error_mark_node;
15062 return namespace_decl;
15065 /* Parse a namespace-definition.
15067 namespace-definition:
15068 named-namespace-definition
15069 unnamed-namespace-definition
15071 named-namespace-definition:
15072 original-namespace-definition
15073 extension-namespace-definition
15075 original-namespace-definition:
15076 namespace identifier { namespace-body }
15078 extension-namespace-definition:
15079 namespace original-namespace-name { namespace-body }
15081 unnamed-namespace-definition:
15082 namespace { namespace-body } */
15085 cp_parser_namespace_definition (cp_parser* parser)
15087 tree identifier, attribs;
15088 bool has_visibility;
15091 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15093 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15095 cp_lexer_consume_token (parser->lexer);
15100 /* Look for the `namespace' keyword. */
15101 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15103 /* Get the name of the namespace. We do not attempt to distinguish
15104 between an original-namespace-definition and an
15105 extension-namespace-definition at this point. The semantic
15106 analysis routines are responsible for that. */
15107 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15108 identifier = cp_parser_identifier (parser);
15110 identifier = NULL_TREE;
15112 /* Parse any specified attributes. */
15113 attribs = cp_parser_attributes_opt (parser);
15115 /* Look for the `{' to start the namespace. */
15116 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15117 /* Start the namespace. */
15118 push_namespace (identifier);
15120 /* "inline namespace" is equivalent to a stub namespace definition
15121 followed by a strong using directive. */
15124 tree name_space = current_namespace;
15125 /* Set up namespace association. */
15126 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15127 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15128 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15129 /* Import the contents of the inline namespace. */
15131 do_using_directive (name_space);
15132 push_namespace (identifier);
15135 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15137 /* Parse the body of the namespace. */
15138 cp_parser_namespace_body (parser);
15140 if (has_visibility)
15141 pop_visibility (1);
15143 /* Finish the namespace. */
15145 /* Look for the final `}'. */
15146 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15149 /* Parse a namespace-body.
15152 declaration-seq [opt] */
15155 cp_parser_namespace_body (cp_parser* parser)
15157 cp_parser_declaration_seq_opt (parser);
15160 /* Parse a namespace-alias-definition.
15162 namespace-alias-definition:
15163 namespace identifier = qualified-namespace-specifier ; */
15166 cp_parser_namespace_alias_definition (cp_parser* parser)
15169 tree namespace_specifier;
15171 cp_token *token = cp_lexer_peek_token (parser->lexer);
15173 /* Look for the `namespace' keyword. */
15174 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15175 /* Look for the identifier. */
15176 identifier = cp_parser_identifier (parser);
15177 if (identifier == error_mark_node)
15179 /* Look for the `=' token. */
15180 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15181 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15183 error_at (token->location, "%<namespace%> definition is not allowed here");
15184 /* Skip the definition. */
15185 cp_lexer_consume_token (parser->lexer);
15186 if (cp_parser_skip_to_closing_brace (parser))
15187 cp_lexer_consume_token (parser->lexer);
15190 cp_parser_require (parser, CPP_EQ, RT_EQ);
15191 /* Look for the qualified-namespace-specifier. */
15192 namespace_specifier
15193 = cp_parser_qualified_namespace_specifier (parser);
15194 /* Look for the `;' token. */
15195 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15197 /* Register the alias in the symbol table. */
15198 do_namespace_alias (identifier, namespace_specifier);
15201 /* Parse a qualified-namespace-specifier.
15203 qualified-namespace-specifier:
15204 :: [opt] nested-name-specifier [opt] namespace-name
15206 Returns a NAMESPACE_DECL corresponding to the specified
15210 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15212 /* Look for the optional `::'. */
15213 cp_parser_global_scope_opt (parser,
15214 /*current_scope_valid_p=*/false);
15216 /* Look for the optional nested-name-specifier. */
15217 cp_parser_nested_name_specifier_opt (parser,
15218 /*typename_keyword_p=*/false,
15219 /*check_dependency_p=*/true,
15221 /*is_declaration=*/true);
15223 return cp_parser_namespace_name (parser);
15226 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15227 access declaration.
15230 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15231 using :: unqualified-id ;
15233 access-declaration:
15239 cp_parser_using_declaration (cp_parser* parser,
15240 bool access_declaration_p)
15243 bool typename_p = false;
15244 bool global_scope_p;
15248 int oldcount = errorcount;
15249 cp_token *diag_token = NULL;
15251 if (access_declaration_p)
15253 diag_token = cp_lexer_peek_token (parser->lexer);
15254 cp_parser_parse_tentatively (parser);
15258 /* Look for the `using' keyword. */
15259 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15261 /* Peek at the next token. */
15262 token = cp_lexer_peek_token (parser->lexer);
15263 /* See if it's `typename'. */
15264 if (token->keyword == RID_TYPENAME)
15266 /* Remember that we've seen it. */
15268 /* Consume the `typename' token. */
15269 cp_lexer_consume_token (parser->lexer);
15273 /* Look for the optional global scope qualification. */
15275 = (cp_parser_global_scope_opt (parser,
15276 /*current_scope_valid_p=*/false)
15279 /* If we saw `typename', or didn't see `::', then there must be a
15280 nested-name-specifier present. */
15281 if (typename_p || !global_scope_p)
15282 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15283 /*check_dependency_p=*/true,
15285 /*is_declaration=*/true);
15286 /* Otherwise, we could be in either of the two productions. In that
15287 case, treat the nested-name-specifier as optional. */
15289 qscope = cp_parser_nested_name_specifier_opt (parser,
15290 /*typename_keyword_p=*/false,
15291 /*check_dependency_p=*/true,
15293 /*is_declaration=*/true);
15295 qscope = global_namespace;
15297 if (access_declaration_p && cp_parser_error_occurred (parser))
15298 /* Something has already gone wrong; there's no need to parse
15299 further. Since an error has occurred, the return value of
15300 cp_parser_parse_definitely will be false, as required. */
15301 return cp_parser_parse_definitely (parser);
15303 token = cp_lexer_peek_token (parser->lexer);
15304 /* Parse the unqualified-id. */
15305 identifier = cp_parser_unqualified_id (parser,
15306 /*template_keyword_p=*/false,
15307 /*check_dependency_p=*/true,
15308 /*declarator_p=*/true,
15309 /*optional_p=*/false);
15311 if (access_declaration_p)
15313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15314 cp_parser_simulate_error (parser);
15315 if (!cp_parser_parse_definitely (parser))
15319 /* The function we call to handle a using-declaration is different
15320 depending on what scope we are in. */
15321 if (qscope == error_mark_node || identifier == error_mark_node)
15323 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15324 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15325 /* [namespace.udecl]
15327 A using declaration shall not name a template-id. */
15328 error_at (token->location,
15329 "a template-id may not appear in a using-declaration");
15332 if (at_class_scope_p ())
15334 /* Create the USING_DECL. */
15335 decl = do_class_using_decl (parser->scope, identifier);
15337 if (decl && typename_p)
15338 USING_DECL_TYPENAME_P (decl) = 1;
15340 if (check_for_bare_parameter_packs (decl))
15343 /* Add it to the list of members in this class. */
15344 finish_member_declaration (decl);
15348 decl = cp_parser_lookup_name_simple (parser,
15351 if (decl == error_mark_node)
15352 cp_parser_name_lookup_error (parser, identifier,
15355 else if (check_for_bare_parameter_packs (decl))
15357 else if (!at_namespace_scope_p ())
15358 do_local_using_decl (decl, qscope, identifier);
15360 do_toplevel_using_decl (decl, qscope, identifier);
15364 /* Look for the final `;'. */
15365 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15367 if (access_declaration_p && errorcount == oldcount)
15368 warning_at (diag_token->location, OPT_Wdeprecated,
15369 "access declarations are deprecated "
15370 "in favour of using-declarations; "
15371 "suggestion: add the %<using%> keyword");
15376 /* Parse an alias-declaration.
15379 using identifier attribute-specifier-seq [opt] = type-id */
15382 cp_parser_alias_declaration (cp_parser* parser)
15384 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15385 location_t id_location;
15386 cp_declarator *declarator;
15387 cp_decl_specifier_seq decl_specs;
15389 const char *saved_message = NULL;
15391 /* Look for the `using' keyword. */
15392 cp_token *using_token
15393 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15394 if (using_token == NULL)
15395 return error_mark_node;
15397 id_location = cp_lexer_peek_token (parser->lexer)->location;
15398 id = cp_parser_identifier (parser);
15399 if (id == error_mark_node)
15400 return error_mark_node;
15402 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15403 attributes = cp_parser_attributes_opt (parser);
15404 if (attributes == error_mark_node)
15405 return error_mark_node;
15407 cp_parser_require (parser, CPP_EQ, RT_EQ);
15409 if (cp_parser_error_occurred (parser))
15410 return error_mark_node;
15412 cp_parser_commit_to_tentative_parse (parser);
15414 /* Now we are going to parse the type-id of the declaration. */
15419 "A type-specifier-seq shall not define a class or enumeration
15420 unless it appears in the type-id of an alias-declaration (7.1.3) that
15421 is not the declaration of a template-declaration."
15423 In other words, if we currently are in an alias template, the
15424 type-id should not define a type.
15426 So let's set parser->type_definition_forbidden_message in that
15427 case; cp_parser_check_type_definition (called by
15428 cp_parser_class_specifier) will then emit an error if a type is
15429 defined in the type-id. */
15430 if (parser->num_template_parameter_lists)
15432 saved_message = parser->type_definition_forbidden_message;
15433 parser->type_definition_forbidden_message =
15434 G_("types may not be defined in alias template declarations");
15437 type = cp_parser_type_id (parser);
15439 /* Restore the error message if need be. */
15440 if (parser->num_template_parameter_lists)
15441 parser->type_definition_forbidden_message = saved_message;
15443 if (type == error_mark_node)
15445 cp_parser_skip_to_end_of_block_or_statement (parser);
15446 return error_mark_node;
15449 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15451 if (cp_parser_error_occurred (parser))
15453 cp_parser_skip_to_end_of_block_or_statement (parser);
15454 return error_mark_node;
15457 /* A typedef-name can also be introduced by an alias-declaration. The
15458 identifier following the using keyword becomes a typedef-name. It has
15459 the same semantics as if it were introduced by the typedef
15460 specifier. In particular, it does not define a new type and it shall
15461 not appear in the type-id. */
15463 clear_decl_specs (&decl_specs);
15464 decl_specs.type = type;
15465 if (attributes != NULL_TREE)
15467 decl_specs.attributes = attributes;
15468 set_and_check_decl_spec_loc (&decl_specs,
15472 set_and_check_decl_spec_loc (&decl_specs,
15475 set_and_check_decl_spec_loc (&decl_specs,
15479 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15480 declarator->id_loc = id_location;
15482 member_p = at_class_scope_p ();
15484 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15485 NULL_TREE, attributes);
15487 decl = start_decl (declarator, &decl_specs, 0,
15488 attributes, NULL_TREE, &pushed_scope);
15489 if (decl == error_mark_node)
15492 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15495 pop_scope (pushed_scope);
15497 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15498 added into the symbol table; otherwise, return the TYPE_DECL. */
15499 if (DECL_LANG_SPECIFIC (decl)
15500 && DECL_TEMPLATE_INFO (decl)
15501 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15503 decl = DECL_TI_TEMPLATE (decl);
15505 check_member_template (decl);
15511 /* Parse a using-directive.
15514 using namespace :: [opt] nested-name-specifier [opt]
15515 namespace-name ; */
15518 cp_parser_using_directive (cp_parser* parser)
15520 tree namespace_decl;
15523 /* Look for the `using' keyword. */
15524 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15525 /* And the `namespace' keyword. */
15526 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15527 /* Look for the optional `::' operator. */
15528 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15529 /* And the optional nested-name-specifier. */
15530 cp_parser_nested_name_specifier_opt (parser,
15531 /*typename_keyword_p=*/false,
15532 /*check_dependency_p=*/true,
15534 /*is_declaration=*/true);
15535 /* Get the namespace being used. */
15536 namespace_decl = cp_parser_namespace_name (parser);
15537 /* And any specified attributes. */
15538 attribs = cp_parser_attributes_opt (parser);
15539 /* Update the symbol table. */
15540 parse_using_directive (namespace_decl, attribs);
15541 /* Look for the final `;'. */
15542 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15545 /* Parse an asm-definition.
15548 asm ( string-literal ) ;
15553 asm volatile [opt] ( string-literal ) ;
15554 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15555 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15556 : asm-operand-list [opt] ) ;
15557 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15558 : asm-operand-list [opt]
15559 : asm-clobber-list [opt] ) ;
15560 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15561 : asm-clobber-list [opt]
15562 : asm-goto-list ) ; */
15565 cp_parser_asm_definition (cp_parser* parser)
15568 tree outputs = NULL_TREE;
15569 tree inputs = NULL_TREE;
15570 tree clobbers = NULL_TREE;
15571 tree labels = NULL_TREE;
15573 bool volatile_p = false;
15574 bool extended_p = false;
15575 bool invalid_inputs_p = false;
15576 bool invalid_outputs_p = false;
15577 bool goto_p = false;
15578 required_token missing = RT_NONE;
15580 /* Look for the `asm' keyword. */
15581 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15582 /* See if the next token is `volatile'. */
15583 if (cp_parser_allow_gnu_extensions_p (parser)
15584 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15586 /* Remember that we saw the `volatile' keyword. */
15588 /* Consume the token. */
15589 cp_lexer_consume_token (parser->lexer);
15591 if (cp_parser_allow_gnu_extensions_p (parser)
15592 && parser->in_function_body
15593 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15595 /* Remember that we saw the `goto' keyword. */
15597 /* Consume the token. */
15598 cp_lexer_consume_token (parser->lexer);
15600 /* Look for the opening `('. */
15601 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15603 /* Look for the string. */
15604 string = cp_parser_string_literal (parser, false, false);
15605 if (string == error_mark_node)
15607 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15608 /*consume_paren=*/true);
15612 /* If we're allowing GNU extensions, check for the extended assembly
15613 syntax. Unfortunately, the `:' tokens need not be separated by
15614 a space in C, and so, for compatibility, we tolerate that here
15615 too. Doing that means that we have to treat the `::' operator as
15617 if (cp_parser_allow_gnu_extensions_p (parser)
15618 && parser->in_function_body
15619 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15620 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15622 bool inputs_p = false;
15623 bool clobbers_p = false;
15624 bool labels_p = false;
15626 /* The extended syntax was used. */
15629 /* Look for outputs. */
15630 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15632 /* Consume the `:'. */
15633 cp_lexer_consume_token (parser->lexer);
15634 /* Parse the output-operands. */
15635 if (cp_lexer_next_token_is_not (parser->lexer,
15637 && cp_lexer_next_token_is_not (parser->lexer,
15639 && cp_lexer_next_token_is_not (parser->lexer,
15642 outputs = cp_parser_asm_operand_list (parser);
15644 if (outputs == error_mark_node)
15645 invalid_outputs_p = true;
15647 /* If the next token is `::', there are no outputs, and the
15648 next token is the beginning of the inputs. */
15649 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15650 /* The inputs are coming next. */
15653 /* Look for inputs. */
15655 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15657 /* Consume the `:' or `::'. */
15658 cp_lexer_consume_token (parser->lexer);
15659 /* Parse the output-operands. */
15660 if (cp_lexer_next_token_is_not (parser->lexer,
15662 && cp_lexer_next_token_is_not (parser->lexer,
15664 && cp_lexer_next_token_is_not (parser->lexer,
15666 inputs = cp_parser_asm_operand_list (parser);
15668 if (inputs == error_mark_node)
15669 invalid_inputs_p = true;
15671 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15672 /* The clobbers are coming next. */
15675 /* Look for clobbers. */
15677 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15680 /* Consume the `:' or `::'. */
15681 cp_lexer_consume_token (parser->lexer);
15682 /* Parse the clobbers. */
15683 if (cp_lexer_next_token_is_not (parser->lexer,
15685 && cp_lexer_next_token_is_not (parser->lexer,
15687 clobbers = cp_parser_asm_clobber_list (parser);
15690 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15691 /* The labels are coming next. */
15694 /* Look for labels. */
15696 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15699 /* Consume the `:' or `::'. */
15700 cp_lexer_consume_token (parser->lexer);
15701 /* Parse the labels. */
15702 labels = cp_parser_asm_label_list (parser);
15705 if (goto_p && !labels_p)
15706 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15709 missing = RT_COLON_SCOPE;
15711 /* Look for the closing `)'. */
15712 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15713 missing ? missing : RT_CLOSE_PAREN))
15714 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15715 /*consume_paren=*/true);
15716 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15718 if (!invalid_inputs_p && !invalid_outputs_p)
15720 /* Create the ASM_EXPR. */
15721 if (parser->in_function_body)
15723 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15724 inputs, clobbers, labels);
15725 /* If the extended syntax was not used, mark the ASM_EXPR. */
15728 tree temp = asm_stmt;
15729 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15730 temp = TREE_OPERAND (temp, 0);
15732 ASM_INPUT_P (temp) = 1;
15736 add_asm_node (string);
15740 /* Declarators [gram.dcl.decl] */
15742 /* Parse an init-declarator.
15745 declarator initializer [opt]
15750 declarator asm-specification [opt] attributes [opt] initializer [opt]
15752 function-definition:
15753 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15755 decl-specifier-seq [opt] declarator function-try-block
15759 function-definition:
15760 __extension__ function-definition
15764 function-definition:
15765 decl-specifier-seq [opt] declarator function-transaction-block
15767 The DECL_SPECIFIERS apply to this declarator. Returns a
15768 representation of the entity declared. If MEMBER_P is TRUE, then
15769 this declarator appears in a class scope. The new DECL created by
15770 this declarator is returned.
15772 The CHECKS are access checks that should be performed once we know
15773 what entity is being declared (and, therefore, what classes have
15776 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15777 for a function-definition here as well. If the declarator is a
15778 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15779 be TRUE upon return. By that point, the function-definition will
15780 have been completely parsed.
15782 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15785 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15786 parsed declaration if it is an uninitialized single declarator not followed
15787 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15788 if present, will not be consumed. If returned, this declarator will be
15789 created with SD_INITIALIZED but will not call cp_finish_decl. */
15792 cp_parser_init_declarator (cp_parser* parser,
15793 cp_decl_specifier_seq *decl_specifiers,
15794 vec<deferred_access_check, va_gc> *checks,
15795 bool function_definition_allowed_p,
15797 int declares_class_or_enum,
15798 bool* function_definition_p,
15799 tree* maybe_range_for_decl)
15801 cp_token *token = NULL, *asm_spec_start_token = NULL,
15802 *attributes_start_token = NULL;
15803 cp_declarator *declarator;
15804 tree prefix_attributes;
15805 tree attributes = NULL;
15806 tree asm_specification;
15808 tree decl = NULL_TREE;
15810 int is_initialized;
15811 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15812 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15814 enum cpp_ttype initialization_kind;
15815 bool is_direct_init = false;
15816 bool is_non_constant_init;
15817 int ctor_dtor_or_conv_p;
15819 tree pushed_scope = NULL_TREE;
15820 bool range_for_decl_p = false;
15822 /* Gather the attributes that were provided with the
15823 decl-specifiers. */
15824 prefix_attributes = decl_specifiers->attributes;
15826 /* Assume that this is not the declarator for a function
15828 if (function_definition_p)
15829 *function_definition_p = false;
15831 /* Defer access checks while parsing the declarator; we cannot know
15832 what names are accessible until we know what is being
15834 resume_deferring_access_checks ();
15836 /* Parse the declarator. */
15837 token = cp_lexer_peek_token (parser->lexer);
15839 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15840 &ctor_dtor_or_conv_p,
15841 /*parenthesized_p=*/NULL,
15843 /* Gather up the deferred checks. */
15844 stop_deferring_access_checks ();
15846 /* If the DECLARATOR was erroneous, there's no need to go
15848 if (declarator == cp_error_declarator)
15849 return error_mark_node;
15851 /* Check that the number of template-parameter-lists is OK. */
15852 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15854 return error_mark_node;
15856 if (declares_class_or_enum & 2)
15857 cp_parser_check_for_definition_in_return_type (declarator,
15858 decl_specifiers->type,
15859 decl_specifiers->locations[ds_type_spec]);
15861 /* Figure out what scope the entity declared by the DECLARATOR is
15862 located in. `grokdeclarator' sometimes changes the scope, so
15863 we compute it now. */
15864 scope = get_scope_of_declarator (declarator);
15866 /* Perform any lookups in the declared type which were thought to be
15867 dependent, but are not in the scope of the declarator. */
15868 decl_specifiers->type
15869 = maybe_update_decl_type (decl_specifiers->type, scope);
15871 /* If we're allowing GNU extensions, look for an
15872 asm-specification. */
15873 if (cp_parser_allow_gnu_extensions_p (parser))
15875 /* Look for an asm-specification. */
15876 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15877 asm_specification = cp_parser_asm_specification_opt (parser);
15880 asm_specification = NULL_TREE;
15882 /* Look for attributes. */
15883 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15884 attributes = cp_parser_attributes_opt (parser);
15886 /* Peek at the next token. */
15887 token = cp_lexer_peek_token (parser->lexer);
15888 /* Check to see if the token indicates the start of a
15889 function-definition. */
15890 if (function_declarator_p (declarator)
15891 && cp_parser_token_starts_function_definition_p (token))
15893 if (!function_definition_allowed_p)
15895 /* If a function-definition should not appear here, issue an
15897 cp_parser_error (parser,
15898 "a function-definition is not allowed here");
15899 return error_mark_node;
15903 location_t func_brace_location
15904 = cp_lexer_peek_token (parser->lexer)->location;
15906 /* Neither attributes nor an asm-specification are allowed
15907 on a function-definition. */
15908 if (asm_specification)
15909 error_at (asm_spec_start_token->location,
15910 "an asm-specification is not allowed "
15911 "on a function-definition");
15913 error_at (attributes_start_token->location,
15914 "attributes are not allowed on a function-definition");
15915 /* This is a function-definition. */
15916 *function_definition_p = true;
15918 /* Parse the function definition. */
15920 decl = cp_parser_save_member_function_body (parser,
15923 prefix_attributes);
15926 = (cp_parser_function_definition_from_specifiers_and_declarator
15927 (parser, decl_specifiers, prefix_attributes, declarator));
15929 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15931 /* This is where the prologue starts... */
15932 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15933 = func_brace_location;
15942 Only in function declarations for constructors, destructors, and
15943 type conversions can the decl-specifier-seq be omitted.
15945 We explicitly postpone this check past the point where we handle
15946 function-definitions because we tolerate function-definitions
15947 that are missing their return types in some modes. */
15948 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15950 cp_parser_error (parser,
15951 "expected constructor, destructor, or type conversion");
15952 return error_mark_node;
15955 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15956 if (token->type == CPP_EQ
15957 || token->type == CPP_OPEN_PAREN
15958 || token->type == CPP_OPEN_BRACE)
15960 is_initialized = SD_INITIALIZED;
15961 initialization_kind = token->type;
15962 if (maybe_range_for_decl)
15963 *maybe_range_for_decl = error_mark_node;
15965 if (token->type == CPP_EQ
15966 && function_declarator_p (declarator))
15968 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15969 if (t2->keyword == RID_DEFAULT)
15970 is_initialized = SD_DEFAULTED;
15971 else if (t2->keyword == RID_DELETE)
15972 is_initialized = SD_DELETED;
15977 /* If the init-declarator isn't initialized and isn't followed by a
15978 `,' or `;', it's not a valid init-declarator. */
15979 if (token->type != CPP_COMMA
15980 && token->type != CPP_SEMICOLON)
15982 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15983 range_for_decl_p = true;
15986 cp_parser_error (parser, "expected initializer");
15987 return error_mark_node;
15990 is_initialized = SD_UNINITIALIZED;
15991 initialization_kind = CPP_EOF;
15994 /* Because start_decl has side-effects, we should only call it if we
15995 know we're going ahead. By this point, we know that we cannot
15996 possibly be looking at any other construct. */
15997 cp_parser_commit_to_tentative_parse (parser);
15999 /* If the decl specifiers were bad, issue an error now that we're
16000 sure this was intended to be a declarator. Then continue
16001 declaring the variable(s), as int, to try to cut down on further
16003 if (decl_specifiers->any_specifiers_p
16004 && decl_specifiers->type == error_mark_node)
16006 cp_parser_error (parser, "invalid type in declaration");
16007 decl_specifiers->type = integer_type_node;
16010 /* Check to see whether or not this declaration is a friend. */
16011 friend_p = cp_parser_friend_p (decl_specifiers);
16013 /* Enter the newly declared entry in the symbol table. If we're
16014 processing a declaration in a class-specifier, we wait until
16015 after processing the initializer. */
16018 if (parser->in_unbraced_linkage_specification_p)
16019 decl_specifiers->storage_class = sc_extern;
16020 decl = start_decl (declarator, decl_specifiers,
16021 range_for_decl_p? SD_INITIALIZED : is_initialized,
16022 attributes, prefix_attributes,
16024 /* Adjust location of decl if declarator->id_loc is more appropriate:
16025 set, and decl wasn't merged with another decl, in which case its
16026 location would be different from input_location, and more accurate. */
16028 && declarator->id_loc != UNKNOWN_LOCATION
16029 && DECL_SOURCE_LOCATION (decl) == input_location)
16030 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16033 /* Enter the SCOPE. That way unqualified names appearing in the
16034 initializer will be looked up in SCOPE. */
16035 pushed_scope = push_scope (scope);
16037 /* Perform deferred access control checks, now that we know in which
16038 SCOPE the declared entity resides. */
16039 if (!member_p && decl)
16041 tree saved_current_function_decl = NULL_TREE;
16043 /* If the entity being declared is a function, pretend that we
16044 are in its scope. If it is a `friend', it may have access to
16045 things that would not otherwise be accessible. */
16046 if (TREE_CODE (decl) == FUNCTION_DECL)
16048 saved_current_function_decl = current_function_decl;
16049 current_function_decl = decl;
16052 /* Perform access checks for template parameters. */
16053 cp_parser_perform_template_parameter_access_checks (checks);
16055 /* Perform the access control checks for the declarator and the
16056 decl-specifiers. */
16057 perform_deferred_access_checks (tf_warning_or_error);
16059 /* Restore the saved value. */
16060 if (TREE_CODE (decl) == FUNCTION_DECL)
16061 current_function_decl = saved_current_function_decl;
16064 /* Parse the initializer. */
16065 initializer = NULL_TREE;
16066 is_direct_init = false;
16067 is_non_constant_init = true;
16068 if (is_initialized)
16070 if (function_declarator_p (declarator))
16072 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16073 if (initialization_kind == CPP_EQ)
16074 initializer = cp_parser_pure_specifier (parser);
16077 /* If the declaration was erroneous, we don't really
16078 know what the user intended, so just silently
16079 consume the initializer. */
16080 if (decl != error_mark_node)
16081 error_at (initializer_start_token->location,
16082 "initializer provided for function");
16083 cp_parser_skip_to_closing_parenthesis (parser,
16084 /*recovering=*/true,
16085 /*or_comma=*/false,
16086 /*consume_paren=*/true);
16091 /* We want to record the extra mangling scope for in-class
16092 initializers of class members and initializers of static data
16093 member templates. The former involves deferring
16094 parsing of the initializer until end of class as with default
16095 arguments. So right here we only handle the latter. */
16096 if (!member_p && processing_template_decl)
16097 start_lambda_scope (decl);
16098 initializer = cp_parser_initializer (parser,
16100 &is_non_constant_init);
16101 if (!member_p && processing_template_decl)
16102 finish_lambda_scope ();
16103 if (initializer == error_mark_node)
16104 cp_parser_skip_to_end_of_statement (parser);
16108 /* The old parser allows attributes to appear after a parenthesized
16109 initializer. Mark Mitchell proposed removing this functionality
16110 on the GCC mailing lists on 2002-08-13. This parser accepts the
16111 attributes -- but ignores them. */
16112 if (cp_parser_allow_gnu_extensions_p (parser)
16113 && initialization_kind == CPP_OPEN_PAREN)
16114 if (cp_parser_attributes_opt (parser))
16115 warning (OPT_Wattributes,
16116 "attributes after parenthesized initializer ignored");
16118 /* For an in-class declaration, use `grokfield' to create the
16124 pop_scope (pushed_scope);
16125 pushed_scope = NULL_TREE;
16127 decl = grokfield (declarator, decl_specifiers,
16128 initializer, !is_non_constant_init,
16129 /*asmspec=*/NULL_TREE,
16130 prefix_attributes);
16131 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16132 cp_parser_save_default_args (parser, decl);
16135 /* Finish processing the declaration. But, skip member
16137 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16139 cp_finish_decl (decl,
16140 initializer, !is_non_constant_init,
16142 /* If the initializer is in parentheses, then this is
16143 a direct-initialization, which means that an
16144 `explicit' constructor is OK. Otherwise, an
16145 `explicit' constructor cannot be used. */
16146 ((is_direct_init || !is_initialized)
16147 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16149 else if ((cxx_dialect != cxx98) && friend_p
16150 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16151 /* Core issue #226 (C++0x only): A default template-argument
16152 shall not be specified in a friend class template
16154 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16155 /*is_partial=*/false, /*is_friend_decl=*/1);
16157 if (!friend_p && pushed_scope)
16158 pop_scope (pushed_scope);
16163 /* Parse a declarator.
16167 ptr-operator declarator
16169 abstract-declarator:
16170 ptr-operator abstract-declarator [opt]
16171 direct-abstract-declarator
16176 attributes [opt] direct-declarator
16177 attributes [opt] ptr-operator declarator
16179 abstract-declarator:
16180 attributes [opt] ptr-operator abstract-declarator [opt]
16181 attributes [opt] direct-abstract-declarator
16183 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16184 detect constructor, destructor or conversion operators. It is set
16185 to -1 if the declarator is a name, and +1 if it is a
16186 function. Otherwise it is set to zero. Usually you just want to
16187 test for >0, but internally the negative value is used.
16189 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16190 a decl-specifier-seq unless it declares a constructor, destructor,
16191 or conversion. It might seem that we could check this condition in
16192 semantic analysis, rather than parsing, but that makes it difficult
16193 to handle something like `f()'. We want to notice that there are
16194 no decl-specifiers, and therefore realize that this is an
16195 expression, not a declaration.)
16197 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16198 the declarator is a direct-declarator of the form "(...)".
16200 MEMBER_P is true iff this declarator is a member-declarator. */
16202 static cp_declarator *
16203 cp_parser_declarator (cp_parser* parser,
16204 cp_parser_declarator_kind dcl_kind,
16205 int* ctor_dtor_or_conv_p,
16206 bool* parenthesized_p,
16209 cp_declarator *declarator;
16210 enum tree_code code;
16211 cp_cv_quals cv_quals;
16213 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16215 /* Assume this is not a constructor, destructor, or type-conversion
16217 if (ctor_dtor_or_conv_p)
16218 *ctor_dtor_or_conv_p = 0;
16220 if (cp_parser_allow_gnu_extensions_p (parser))
16221 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16223 /* Check for the ptr-operator production. */
16224 cp_parser_parse_tentatively (parser);
16225 /* Parse the ptr-operator. */
16226 code = cp_parser_ptr_operator (parser,
16231 /* If that worked, then we have a ptr-operator. */
16232 if (cp_parser_parse_definitely (parser))
16234 /* If a ptr-operator was found, then this declarator was not
16236 if (parenthesized_p)
16237 *parenthesized_p = true;
16238 /* The dependent declarator is optional if we are parsing an
16239 abstract-declarator. */
16240 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16241 cp_parser_parse_tentatively (parser);
16243 /* Parse the dependent declarator. */
16244 declarator = cp_parser_declarator (parser, dcl_kind,
16245 /*ctor_dtor_or_conv_p=*/NULL,
16246 /*parenthesized_p=*/NULL,
16247 /*member_p=*/false);
16249 /* If we are parsing an abstract-declarator, we must handle the
16250 case where the dependent declarator is absent. */
16251 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16252 && !cp_parser_parse_definitely (parser))
16255 declarator = cp_parser_make_indirect_declarator
16256 (code, class_type, cv_quals, declarator, std_attributes);
16258 /* Everything else is a direct-declarator. */
16261 if (parenthesized_p)
16262 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16264 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16265 ctor_dtor_or_conv_p,
16269 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16270 declarator->attributes = gnu_attributes;
16274 /* Parse a direct-declarator or direct-abstract-declarator.
16278 direct-declarator ( parameter-declaration-clause )
16279 cv-qualifier-seq [opt]
16280 ref-qualifier [opt]
16281 exception-specification [opt]
16282 direct-declarator [ constant-expression [opt] ]
16285 direct-abstract-declarator:
16286 direct-abstract-declarator [opt]
16287 ( parameter-declaration-clause )
16288 cv-qualifier-seq [opt]
16289 ref-qualifier [opt]
16290 exception-specification [opt]
16291 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16292 ( abstract-declarator )
16294 Returns a representation of the declarator. DCL_KIND is
16295 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16296 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16297 we are parsing a direct-declarator. It is
16298 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16299 of ambiguity we prefer an abstract declarator, as per
16300 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16301 cp_parser_declarator. */
16303 static cp_declarator *
16304 cp_parser_direct_declarator (cp_parser* parser,
16305 cp_parser_declarator_kind dcl_kind,
16306 int* ctor_dtor_or_conv_p,
16310 cp_declarator *declarator = NULL;
16311 tree scope = NULL_TREE;
16312 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16313 bool saved_in_declarator_p = parser->in_declarator_p;
16315 tree pushed_scope = NULL_TREE;
16319 /* Peek at the next token. */
16320 token = cp_lexer_peek_token (parser->lexer);
16321 if (token->type == CPP_OPEN_PAREN)
16323 /* This is either a parameter-declaration-clause, or a
16324 parenthesized declarator. When we know we are parsing a
16325 named declarator, it must be a parenthesized declarator
16326 if FIRST is true. For instance, `(int)' is a
16327 parameter-declaration-clause, with an omitted
16328 direct-abstract-declarator. But `((*))', is a
16329 parenthesized abstract declarator. Finally, when T is a
16330 template parameter `(T)' is a
16331 parameter-declaration-clause, and not a parenthesized
16334 We first try and parse a parameter-declaration-clause,
16335 and then try a nested declarator (if FIRST is true).
16337 It is not an error for it not to be a
16338 parameter-declaration-clause, even when FIRST is
16344 The first is the declaration of a function while the
16345 second is the definition of a variable, including its
16348 Having seen only the parenthesis, we cannot know which of
16349 these two alternatives should be selected. Even more
16350 complex are examples like:
16355 The former is a function-declaration; the latter is a
16356 variable initialization.
16358 Thus again, we try a parameter-declaration-clause, and if
16359 that fails, we back out and return. */
16361 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16364 unsigned saved_num_template_parameter_lists;
16365 bool is_declarator = false;
16368 /* In a member-declarator, the only valid interpretation
16369 of a parenthesis is the start of a
16370 parameter-declaration-clause. (It is invalid to
16371 initialize a static data member with a parenthesized
16372 initializer; only the "=" form of initialization is
16375 cp_parser_parse_tentatively (parser);
16377 /* Consume the `('. */
16378 cp_lexer_consume_token (parser->lexer);
16381 /* If this is going to be an abstract declarator, we're
16382 in a declarator and we can't have default args. */
16383 parser->default_arg_ok_p = false;
16384 parser->in_declarator_p = true;
16387 /* Inside the function parameter list, surrounding
16388 template-parameter-lists do not apply. */
16389 saved_num_template_parameter_lists
16390 = parser->num_template_parameter_lists;
16391 parser->num_template_parameter_lists = 0;
16393 begin_scope (sk_function_parms, NULL_TREE);
16395 /* Parse the parameter-declaration-clause. */
16396 params = cp_parser_parameter_declaration_clause (parser);
16398 parser->num_template_parameter_lists
16399 = saved_num_template_parameter_lists;
16401 /* Consume the `)'. */
16402 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16404 /* If all went well, parse the cv-qualifier-seq,
16405 ref-qualifier and the exception-specification. */
16406 if (member_p || cp_parser_parse_definitely (parser))
16408 cp_cv_quals cv_quals;
16409 cp_virt_specifiers virt_specifiers;
16410 cp_ref_qualifier ref_qual;
16411 tree exception_specification;
16414 bool memfn = (member_p || (pushed_scope
16415 && CLASS_TYPE_P (pushed_scope)));
16417 is_declarator = true;
16419 if (ctor_dtor_or_conv_p)
16420 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16423 /* Parse the cv-qualifier-seq. */
16424 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16425 /* Parse the ref-qualifier. */
16426 ref_qual = cp_parser_ref_qualifier_seq_opt (parser);
16427 /* And the exception-specification. */
16428 exception_specification
16429 = cp_parser_exception_specification_opt (parser);
16431 attrs = cp_parser_std_attribute_spec_seq (parser);
16433 late_return = (cp_parser_late_return_type_opt
16434 (parser, memfn ? cv_quals : -1));
16436 /* Parse the virt-specifier-seq. */
16437 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16439 /* Create the function-declarator. */
16440 declarator = make_call_declarator (declarator,
16445 exception_specification,
16447 declarator->std_attributes = attrs;
16448 /* Any subsequent parameter lists are to do with
16449 return type, so are not those of the declared
16451 parser->default_arg_ok_p = false;
16454 /* Remove the function parms from scope. */
16455 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16456 pop_binding (DECL_NAME (t), t);
16460 /* Repeat the main loop. */
16464 /* If this is the first, we can try a parenthesized
16468 bool saved_in_type_id_in_expr_p;
16470 parser->default_arg_ok_p = saved_default_arg_ok_p;
16471 parser->in_declarator_p = saved_in_declarator_p;
16473 /* Consume the `('. */
16474 cp_lexer_consume_token (parser->lexer);
16475 /* Parse the nested declarator. */
16476 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16477 parser->in_type_id_in_expr_p = true;
16479 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16480 /*parenthesized_p=*/NULL,
16482 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16484 /* Expect a `)'. */
16485 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16486 declarator = cp_error_declarator;
16487 if (declarator == cp_error_declarator)
16490 goto handle_declarator;
16492 /* Otherwise, we must be done. */
16496 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16497 && token->type == CPP_OPEN_SQUARE
16498 && !cp_next_tokens_can_be_attribute_p (parser))
16500 /* Parse an array-declarator. */
16501 tree bounds, attrs;
16503 if (ctor_dtor_or_conv_p)
16504 *ctor_dtor_or_conv_p = 0;
16507 parser->default_arg_ok_p = false;
16508 parser->in_declarator_p = true;
16509 /* Consume the `['. */
16510 cp_lexer_consume_token (parser->lexer);
16511 /* Peek at the next token. */
16512 token = cp_lexer_peek_token (parser->lexer);
16513 /* If the next token is `]', then there is no
16514 constant-expression. */
16515 if (token->type != CPP_CLOSE_SQUARE)
16517 bool non_constant_p;
16520 = cp_parser_constant_expression (parser,
16521 /*allow_non_constant=*/true,
16523 if (!non_constant_p)
16525 else if (error_operand_p (bounds))
16526 /* Already gave an error. */;
16527 else if (!parser->in_function_body
16528 || current_binding_level->kind == sk_function_parms)
16530 /* Normally, the array bound must be an integral constant
16531 expression. However, as an extension, we allow VLAs
16532 in function scopes as long as they aren't part of a
16533 parameter declaration. */
16534 cp_parser_error (parser,
16535 "array bound is not an integer constant");
16536 bounds = error_mark_node;
16538 else if (processing_template_decl)
16540 /* Remember this wasn't a constant-expression. */
16541 bounds = build_nop (TREE_TYPE (bounds), bounds);
16542 TREE_SIDE_EFFECTS (bounds) = 1;
16546 bounds = NULL_TREE;
16547 /* Look for the closing `]'. */
16548 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16550 declarator = cp_error_declarator;
16554 attrs = cp_parser_std_attribute_spec_seq (parser);
16555 declarator = make_array_declarator (declarator, bounds);
16556 declarator->std_attributes = attrs;
16558 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16561 tree qualifying_scope;
16562 tree unqualified_name;
16564 special_function_kind sfk;
16566 bool pack_expansion_p = false;
16567 cp_token *declarator_id_start_token;
16569 /* Parse a declarator-id */
16570 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16573 cp_parser_parse_tentatively (parser);
16575 /* If we see an ellipsis, we should be looking at a
16577 if (token->type == CPP_ELLIPSIS)
16579 /* Consume the `...' */
16580 cp_lexer_consume_token (parser->lexer);
16582 pack_expansion_p = true;
16586 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16588 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16589 qualifying_scope = parser->scope;
16594 if (!unqualified_name && pack_expansion_p)
16596 /* Check whether an error occurred. */
16597 okay = !cp_parser_error_occurred (parser);
16599 /* We already consumed the ellipsis to mark a
16600 parameter pack, but we have no way to report it,
16601 so abort the tentative parse. We will be exiting
16602 immediately anyway. */
16603 cp_parser_abort_tentative_parse (parser);
16606 okay = cp_parser_parse_definitely (parser);
16609 unqualified_name = error_mark_node;
16610 else if (unqualified_name
16611 && (qualifying_scope
16612 || (TREE_CODE (unqualified_name)
16613 != IDENTIFIER_NODE)))
16615 cp_parser_error (parser, "expected unqualified-id");
16616 unqualified_name = error_mark_node;
16620 if (!unqualified_name)
16622 if (unqualified_name == error_mark_node)
16624 declarator = cp_error_declarator;
16625 pack_expansion_p = false;
16626 declarator->parameter_pack_p = false;
16630 attrs = cp_parser_std_attribute_spec_seq (parser);
16632 if (qualifying_scope && at_namespace_scope_p ()
16633 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16635 /* In the declaration of a member of a template class
16636 outside of the class itself, the SCOPE will sometimes
16637 be a TYPENAME_TYPE. For example, given:
16639 template <typename T>
16640 int S<T>::R::i = 3;
16642 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16643 this context, we must resolve S<T>::R to an ordinary
16644 type, rather than a typename type.
16646 The reason we normally avoid resolving TYPENAME_TYPEs
16647 is that a specialization of `S' might render
16648 `S<T>::R' not a type. However, if `S' is
16649 specialized, then this `i' will not be used, so there
16650 is no harm in resolving the types here. */
16653 /* Resolve the TYPENAME_TYPE. */
16654 type = resolve_typename_type (qualifying_scope,
16655 /*only_current_p=*/false);
16656 /* If that failed, the declarator is invalid. */
16657 if (TREE_CODE (type) == TYPENAME_TYPE)
16659 if (typedef_variant_p (type))
16660 error_at (declarator_id_start_token->location,
16661 "cannot define member of dependent typedef "
16664 error_at (declarator_id_start_token->location,
16665 "%<%T::%E%> is not a type",
16666 TYPE_CONTEXT (qualifying_scope),
16667 TYPE_IDENTIFIER (qualifying_scope));
16669 qualifying_scope = type;
16674 if (unqualified_name)
16678 if (qualifying_scope
16679 && CLASS_TYPE_P (qualifying_scope))
16680 class_type = qualifying_scope;
16682 class_type = current_class_type;
16684 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16686 tree name_type = TREE_TYPE (unqualified_name);
16687 if (class_type && same_type_p (name_type, class_type))
16689 if (qualifying_scope
16690 && CLASSTYPE_USE_TEMPLATE (name_type))
16692 error_at (declarator_id_start_token->location,
16693 "invalid use of constructor as a template");
16694 inform (declarator_id_start_token->location,
16695 "use %<%T::%D%> instead of %<%T::%D%> to "
16696 "name the constructor in a qualified name",
16698 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16699 class_type, name_type);
16700 declarator = cp_error_declarator;
16704 unqualified_name = constructor_name (class_type);
16708 /* We do not attempt to print the declarator
16709 here because we do not have enough
16710 information about its original syntactic
16712 cp_parser_error (parser, "invalid declarator");
16713 declarator = cp_error_declarator;
16720 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16721 sfk = sfk_destructor;
16722 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16723 sfk = sfk_conversion;
16724 else if (/* There's no way to declare a constructor
16725 for an anonymous type, even if the type
16726 got a name for linkage purposes. */
16727 !TYPE_WAS_ANONYMOUS (class_type)
16728 && constructor_name_p (unqualified_name,
16731 unqualified_name = constructor_name (class_type);
16732 sfk = sfk_constructor;
16734 else if (is_overloaded_fn (unqualified_name)
16735 && DECL_CONSTRUCTOR_P (get_first_fn
16736 (unqualified_name)))
16737 sfk = sfk_constructor;
16739 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16740 *ctor_dtor_or_conv_p = -1;
16743 declarator = make_id_declarator (qualifying_scope,
16746 declarator->std_attributes = attrs;
16747 declarator->id_loc = token->location;
16748 declarator->parameter_pack_p = pack_expansion_p;
16750 if (pack_expansion_p)
16751 maybe_warn_variadic_templates ();
16754 handle_declarator:;
16755 scope = get_scope_of_declarator (declarator);
16757 /* Any names that appear after the declarator-id for a
16758 member are looked up in the containing scope. */
16759 pushed_scope = push_scope (scope);
16760 parser->in_declarator_p = true;
16761 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16762 || (declarator && declarator->kind == cdk_id))
16763 /* Default args are only allowed on function
16765 parser->default_arg_ok_p = saved_default_arg_ok_p;
16767 parser->default_arg_ok_p = false;
16776 /* For an abstract declarator, we might wind up with nothing at this
16777 point. That's an error; the declarator is not optional. */
16779 cp_parser_error (parser, "expected declarator");
16781 /* If we entered a scope, we must exit it now. */
16783 pop_scope (pushed_scope);
16785 parser->default_arg_ok_p = saved_default_arg_ok_p;
16786 parser->in_declarator_p = saved_in_declarator_p;
16791 /* Parse a ptr-operator.
16794 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16795 * cv-qualifier-seq [opt]
16797 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16798 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16803 & cv-qualifier-seq [opt]
16805 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16806 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16807 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16808 filled in with the TYPE containing the member. *CV_QUALS is
16809 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16810 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16811 Note that the tree codes returned by this function have nothing
16812 to do with the types of trees that will be eventually be created
16813 to represent the pointer or reference type being parsed. They are
16814 just constants with suggestive names. */
16815 static enum tree_code
16816 cp_parser_ptr_operator (cp_parser* parser,
16818 cp_cv_quals *cv_quals,
16821 enum tree_code code = ERROR_MARK;
16823 tree attrs = NULL_TREE;
16825 /* Assume that it's not a pointer-to-member. */
16827 /* And that there are no cv-qualifiers. */
16828 *cv_quals = TYPE_UNQUALIFIED;
16830 /* Peek at the next token. */
16831 token = cp_lexer_peek_token (parser->lexer);
16833 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16834 if (token->type == CPP_MULT)
16835 code = INDIRECT_REF;
16836 else if (token->type == CPP_AND)
16838 else if ((cxx_dialect != cxx98) &&
16839 token->type == CPP_AND_AND) /* C++0x only */
16840 code = NON_LVALUE_EXPR;
16842 if (code != ERROR_MARK)
16844 /* Consume the `*', `&' or `&&'. */
16845 cp_lexer_consume_token (parser->lexer);
16847 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16848 `&', if we are allowing GNU extensions. (The only qualifier
16849 that can legally appear after `&' is `restrict', but that is
16850 enforced during semantic analysis. */
16851 if (code == INDIRECT_REF
16852 || cp_parser_allow_gnu_extensions_p (parser))
16853 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16855 attrs = cp_parser_std_attribute_spec_seq (parser);
16856 if (attributes != NULL)
16857 *attributes = attrs;
16861 /* Try the pointer-to-member case. */
16862 cp_parser_parse_tentatively (parser);
16863 /* Look for the optional `::' operator. */
16864 cp_parser_global_scope_opt (parser,
16865 /*current_scope_valid_p=*/false);
16866 /* Look for the nested-name specifier. */
16867 token = cp_lexer_peek_token (parser->lexer);
16868 cp_parser_nested_name_specifier (parser,
16869 /*typename_keyword_p=*/false,
16870 /*check_dependency_p=*/true,
16872 /*is_declaration=*/false);
16873 /* If we found it, and the next token is a `*', then we are
16874 indeed looking at a pointer-to-member operator. */
16875 if (!cp_parser_error_occurred (parser)
16876 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16878 /* Indicate that the `*' operator was used. */
16879 code = INDIRECT_REF;
16881 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16882 error_at (token->location, "%qD is a namespace", parser->scope);
16883 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16884 error_at (token->location, "cannot form pointer to member of "
16885 "non-class %q#T", parser->scope);
16888 /* The type of which the member is a member is given by the
16890 *type = parser->scope;
16891 /* The next name will not be qualified. */
16892 parser->scope = NULL_TREE;
16893 parser->qualifying_scope = NULL_TREE;
16894 parser->object_scope = NULL_TREE;
16895 /* Look for optional c++11 attributes. */
16896 attrs = cp_parser_std_attribute_spec_seq (parser);
16897 if (attributes != NULL)
16898 *attributes = attrs;
16899 /* Look for the optional cv-qualifier-seq. */
16900 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16903 /* If that didn't work we don't have a ptr-operator. */
16904 if (!cp_parser_parse_definitely (parser))
16905 cp_parser_error (parser, "expected ptr-operator");
16911 /* Parse an (optional) cv-qualifier-seq.
16914 cv-qualifier cv-qualifier-seq [opt]
16925 Returns a bitmask representing the cv-qualifiers. */
16928 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16930 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16935 cp_cv_quals cv_qualifier;
16937 /* Peek at the next token. */
16938 token = cp_lexer_peek_token (parser->lexer);
16939 /* See if it's a cv-qualifier. */
16940 switch (token->keyword)
16943 cv_qualifier = TYPE_QUAL_CONST;
16947 cv_qualifier = TYPE_QUAL_VOLATILE;
16951 cv_qualifier = TYPE_QUAL_RESTRICT;
16955 cv_qualifier = TYPE_UNQUALIFIED;
16962 if (cv_quals & cv_qualifier)
16964 error_at (token->location, "duplicate cv-qualifier");
16965 cp_lexer_purge_token (parser->lexer);
16969 cp_lexer_consume_token (parser->lexer);
16970 cv_quals |= cv_qualifier;
16977 /* Parse an (optional) ref-qualifier
16983 Returns cp_ref_qualifier representing ref-qualifier. */
16985 static cp_ref_qualifier
16986 cp_parser_ref_qualifier_seq_opt (cp_parser* parser)
16988 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
16989 cp_token *token = cp_lexer_peek_token (parser->lexer);
16991 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
16992 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
16995 switch (token->type)
16998 ref_qual = REF_QUAL_LVALUE;
17001 ref_qual = REF_QUAL_RVALUE;
17007 cp_lexer_consume_token (parser->lexer);
17013 /* Parse an (optional) virt-specifier-seq.
17015 virt-specifier-seq:
17016 virt-specifier virt-specifier-seq [opt]
17022 Returns a bitmask representing the virt-specifiers. */
17024 static cp_virt_specifiers
17025 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17027 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17032 cp_virt_specifiers virt_specifier;
17034 /* Peek at the next token. */
17035 token = cp_lexer_peek_token (parser->lexer);
17036 /* See if it's a virt-specifier-qualifier. */
17037 if (token->type != CPP_NAME)
17039 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17041 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17042 virt_specifier = VIRT_SPEC_OVERRIDE;
17044 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17046 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17047 virt_specifier = VIRT_SPEC_FINAL;
17049 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17051 virt_specifier = VIRT_SPEC_FINAL;
17056 if (virt_specifiers & virt_specifier)
17058 error_at (token->location, "duplicate virt-specifier");
17059 cp_lexer_purge_token (parser->lexer);
17063 cp_lexer_consume_token (parser->lexer);
17064 virt_specifiers |= virt_specifier;
17067 return virt_specifiers;
17070 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17071 is in scope even though it isn't real. */
17074 inject_this_parameter (tree ctype, cp_cv_quals quals)
17078 if (current_class_ptr)
17080 /* We don't clear this between NSDMIs. Is it already what we want? */
17081 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17082 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17083 && cp_type_quals (type) == quals)
17087 this_parm = build_this_parm (ctype, quals);
17088 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17089 current_class_ptr = NULL_TREE;
17091 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17092 current_class_ptr = this_parm;
17095 /* Parse a late-specified return type, if any. This is not a separate
17096 non-terminal, but part of a function declarator, which looks like
17098 -> trailing-type-specifier-seq abstract-declarator(opt)
17100 Returns the type indicated by the type-id.
17102 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17106 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
17111 /* Peek at the next token. */
17112 token = cp_lexer_peek_token (parser->lexer);
17113 /* A late-specified return type is indicated by an initial '->'. */
17114 if (token->type != CPP_DEREF)
17117 /* Consume the ->. */
17118 cp_lexer_consume_token (parser->lexer);
17120 tree save_ccp = current_class_ptr;
17121 tree save_ccr = current_class_ref;
17124 /* DR 1207: 'this' is in scope in the trailing return type. */
17125 inject_this_parameter (current_class_type, quals);
17128 type = cp_parser_trailing_type_id (parser);
17132 current_class_ptr = save_ccp;
17133 current_class_ref = save_ccr;
17139 /* Parse a declarator-id.
17143 :: [opt] nested-name-specifier [opt] type-name
17145 In the `id-expression' case, the value returned is as for
17146 cp_parser_id_expression if the id-expression was an unqualified-id.
17147 If the id-expression was a qualified-id, then a SCOPE_REF is
17148 returned. The first operand is the scope (either a NAMESPACE_DECL
17149 or TREE_TYPE), but the second is still just a representation of an
17153 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17156 /* The expression must be an id-expression. Assume that qualified
17157 names are the names of types so that:
17160 int S<T>::R::i = 3;
17162 will work; we must treat `S<T>::R' as the name of a type.
17163 Similarly, assume that qualified names are templates, where
17167 int S<T>::R<T>::i = 3;
17170 id = cp_parser_id_expression (parser,
17171 /*template_keyword_p=*/false,
17172 /*check_dependency_p=*/false,
17173 /*template_p=*/NULL,
17174 /*declarator_p=*/true,
17176 if (id && BASELINK_P (id))
17177 id = BASELINK_FUNCTIONS (id);
17181 /* Parse a type-id.
17184 type-specifier-seq abstract-declarator [opt]
17186 Returns the TYPE specified. */
17189 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17190 bool is_trailing_return)
17192 cp_decl_specifier_seq type_specifier_seq;
17193 cp_declarator *abstract_declarator;
17195 /* Parse the type-specifier-seq. */
17196 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17197 is_trailing_return,
17198 &type_specifier_seq);
17199 if (type_specifier_seq.type == error_mark_node)
17200 return error_mark_node;
17202 /* There might or might not be an abstract declarator. */
17203 cp_parser_parse_tentatively (parser);
17204 /* Look for the declarator. */
17205 abstract_declarator
17206 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17207 /*parenthesized_p=*/NULL,
17208 /*member_p=*/false);
17209 /* Check to see if there really was a declarator. */
17210 if (!cp_parser_parse_definitely (parser))
17211 abstract_declarator = NULL;
17213 if (type_specifier_seq.type
17214 && type_uses_auto (type_specifier_seq.type))
17216 /* A type-id with type 'auto' is only ok if the abstract declarator
17217 is a function declarator with a late-specified return type. */
17218 if (abstract_declarator
17219 && abstract_declarator->kind == cdk_function
17220 && abstract_declarator->u.function.late_return_type)
17224 error ("invalid use of %<auto%>");
17225 return error_mark_node;
17229 return groktypename (&type_specifier_seq, abstract_declarator,
17233 static tree cp_parser_type_id (cp_parser *parser)
17235 return cp_parser_type_id_1 (parser, false, false);
17238 static tree cp_parser_template_type_arg (cp_parser *parser)
17241 const char *saved_message = parser->type_definition_forbidden_message;
17242 parser->type_definition_forbidden_message
17243 = G_("types may not be defined in template arguments");
17244 r = cp_parser_type_id_1 (parser, true, false);
17245 parser->type_definition_forbidden_message = saved_message;
17249 static tree cp_parser_trailing_type_id (cp_parser *parser)
17251 return cp_parser_type_id_1 (parser, false, true);
17254 /* Parse a type-specifier-seq.
17256 type-specifier-seq:
17257 type-specifier type-specifier-seq [opt]
17261 type-specifier-seq:
17262 attributes type-specifier-seq [opt]
17264 If IS_DECLARATION is true, we are at the start of a "condition" or
17265 exception-declaration, so we might be followed by a declarator-id.
17267 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17268 i.e. we've just seen "->".
17270 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17273 cp_parser_type_specifier_seq (cp_parser* parser,
17274 bool is_declaration,
17275 bool is_trailing_return,
17276 cp_decl_specifier_seq *type_specifier_seq)
17278 bool seen_type_specifier = false;
17279 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17280 cp_token *start_token = NULL;
17282 /* Clear the TYPE_SPECIFIER_SEQ. */
17283 clear_decl_specs (type_specifier_seq);
17285 /* In the context of a trailing return type, enum E { } is an
17286 elaborated-type-specifier followed by a function-body, not an
17288 if (is_trailing_return)
17289 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17291 /* Parse the type-specifiers and attributes. */
17294 tree type_specifier;
17295 bool is_cv_qualifier;
17297 /* Check for attributes first. */
17298 if (cp_next_tokens_can_be_attribute_p (parser))
17300 type_specifier_seq->attributes =
17301 chainon (type_specifier_seq->attributes,
17302 cp_parser_attributes_opt (parser));
17306 /* record the token of the beginning of the type specifier seq,
17307 for error reporting purposes*/
17309 start_token = cp_lexer_peek_token (parser->lexer);
17311 /* Look for the type-specifier. */
17312 type_specifier = cp_parser_type_specifier (parser,
17314 type_specifier_seq,
17315 /*is_declaration=*/false,
17318 if (!type_specifier)
17320 /* If the first type-specifier could not be found, this is not a
17321 type-specifier-seq at all. */
17322 if (!seen_type_specifier)
17324 cp_parser_error (parser, "expected type-specifier");
17325 type_specifier_seq->type = error_mark_node;
17328 /* If subsequent type-specifiers could not be found, the
17329 type-specifier-seq is complete. */
17333 seen_type_specifier = true;
17334 /* The standard says that a condition can be:
17336 type-specifier-seq declarator = assignment-expression
17343 we should treat the "S" as a declarator, not as a
17344 type-specifier. The standard doesn't say that explicitly for
17345 type-specifier-seq, but it does say that for
17346 decl-specifier-seq in an ordinary declaration. Perhaps it
17347 would be clearer just to allow a decl-specifier-seq here, and
17348 then add a semantic restriction that if any decl-specifiers
17349 that are not type-specifiers appear, the program is invalid. */
17350 if (is_declaration && !is_cv_qualifier)
17351 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17355 /* Parse a parameter-declaration-clause.
17357 parameter-declaration-clause:
17358 parameter-declaration-list [opt] ... [opt]
17359 parameter-declaration-list , ...
17361 Returns a representation for the parameter declarations. A return
17362 value of NULL indicates a parameter-declaration-clause consisting
17363 only of an ellipsis. */
17366 cp_parser_parameter_declaration_clause (cp_parser* parser)
17373 /* Peek at the next token. */
17374 token = cp_lexer_peek_token (parser->lexer);
17375 /* Check for trivial parameter-declaration-clauses. */
17376 if (token->type == CPP_ELLIPSIS)
17378 /* Consume the `...' token. */
17379 cp_lexer_consume_token (parser->lexer);
17382 else if (token->type == CPP_CLOSE_PAREN)
17383 /* There are no parameters. */
17385 #ifndef NO_IMPLICIT_EXTERN_C
17386 if (in_system_header && current_class_type == NULL
17387 && current_lang_name == lang_name_c)
17391 return void_list_node;
17393 /* Check for `(void)', too, which is a special case. */
17394 else if (token->keyword == RID_VOID
17395 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17396 == CPP_CLOSE_PAREN))
17398 /* Consume the `void' token. */
17399 cp_lexer_consume_token (parser->lexer);
17400 /* There are no parameters. */
17401 return void_list_node;
17404 /* Parse the parameter-declaration-list. */
17405 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17406 /* If a parse error occurred while parsing the
17407 parameter-declaration-list, then the entire
17408 parameter-declaration-clause is erroneous. */
17412 /* Peek at the next token. */
17413 token = cp_lexer_peek_token (parser->lexer);
17414 /* If it's a `,', the clause should terminate with an ellipsis. */
17415 if (token->type == CPP_COMMA)
17417 /* Consume the `,'. */
17418 cp_lexer_consume_token (parser->lexer);
17419 /* Expect an ellipsis. */
17421 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17423 /* It might also be `...' if the optional trailing `,' was
17425 else if (token->type == CPP_ELLIPSIS)
17427 /* Consume the `...' token. */
17428 cp_lexer_consume_token (parser->lexer);
17429 /* And remember that we saw it. */
17433 ellipsis_p = false;
17435 /* Finish the parameter list. */
17437 parameters = chainon (parameters, void_list_node);
17442 /* Parse a parameter-declaration-list.
17444 parameter-declaration-list:
17445 parameter-declaration
17446 parameter-declaration-list , parameter-declaration
17448 Returns a representation of the parameter-declaration-list, as for
17449 cp_parser_parameter_declaration_clause. However, the
17450 `void_list_node' is never appended to the list. Upon return,
17451 *IS_ERROR will be true iff an error occurred. */
17454 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17456 tree parameters = NULL_TREE;
17457 tree *tail = ¶meters;
17458 bool saved_in_unbraced_linkage_specification_p;
17461 /* Assume all will go well. */
17463 /* The special considerations that apply to a function within an
17464 unbraced linkage specifications do not apply to the parameters
17465 to the function. */
17466 saved_in_unbraced_linkage_specification_p
17467 = parser->in_unbraced_linkage_specification_p;
17468 parser->in_unbraced_linkage_specification_p = false;
17470 /* Look for more parameters. */
17473 cp_parameter_declarator *parameter;
17474 tree decl = error_mark_node;
17475 bool parenthesized_p = false;
17476 /* Parse the parameter. */
17478 = cp_parser_parameter_declaration (parser,
17479 /*template_parm_p=*/false,
17482 /* We don't know yet if the enclosing context is deprecated, so wait
17483 and warn in grokparms if appropriate. */
17484 deprecated_state = DEPRECATED_SUPPRESS;
17487 decl = grokdeclarator (parameter->declarator,
17488 ¶meter->decl_specifiers,
17490 parameter->default_argument != NULL_TREE,
17491 ¶meter->decl_specifiers.attributes);
17493 deprecated_state = DEPRECATED_NORMAL;
17495 /* If a parse error occurred parsing the parameter declaration,
17496 then the entire parameter-declaration-list is erroneous. */
17497 if (decl == error_mark_node)
17500 parameters = error_mark_node;
17504 if (parameter->decl_specifiers.attributes)
17505 cplus_decl_attributes (&decl,
17506 parameter->decl_specifiers.attributes,
17508 if (DECL_NAME (decl))
17509 decl = pushdecl (decl);
17511 if (decl != error_mark_node)
17513 retrofit_lang_decl (decl);
17514 DECL_PARM_INDEX (decl) = ++index;
17515 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17518 /* Add the new parameter to the list. */
17519 *tail = build_tree_list (parameter->default_argument, decl);
17520 tail = &TREE_CHAIN (*tail);
17522 /* Peek at the next token. */
17523 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17524 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17525 /* These are for Objective-C++ */
17526 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17527 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17528 /* The parameter-declaration-list is complete. */
17530 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17534 /* Peek at the next token. */
17535 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17536 /* If it's an ellipsis, then the list is complete. */
17537 if (token->type == CPP_ELLIPSIS)
17539 /* Otherwise, there must be more parameters. Consume the
17541 cp_lexer_consume_token (parser->lexer);
17542 /* When parsing something like:
17544 int i(float f, double d)
17546 we can tell after seeing the declaration for "f" that we
17547 are not looking at an initialization of a variable "i",
17548 but rather at the declaration of a function "i".
17550 Due to the fact that the parsing of template arguments
17551 (as specified to a template-id) requires backtracking we
17552 cannot use this technique when inside a template argument
17554 if (!parser->in_template_argument_list_p
17555 && !parser->in_type_id_in_expr_p
17556 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17557 /* However, a parameter-declaration of the form
17558 "foat(f)" (which is a valid declaration of a
17559 parameter "f") can also be interpreted as an
17560 expression (the conversion of "f" to "float"). */
17561 && !parenthesized_p)
17562 cp_parser_commit_to_tentative_parse (parser);
17566 cp_parser_error (parser, "expected %<,%> or %<...%>");
17567 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17568 cp_parser_skip_to_closing_parenthesis (parser,
17569 /*recovering=*/true,
17570 /*or_comma=*/false,
17571 /*consume_paren=*/false);
17576 parser->in_unbraced_linkage_specification_p
17577 = saved_in_unbraced_linkage_specification_p;
17582 /* Parse a parameter declaration.
17584 parameter-declaration:
17585 decl-specifier-seq ... [opt] declarator
17586 decl-specifier-seq declarator = assignment-expression
17587 decl-specifier-seq ... [opt] abstract-declarator [opt]
17588 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17590 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17591 declares a template parameter. (In that case, a non-nested `>'
17592 token encountered during the parsing of the assignment-expression
17593 is not interpreted as a greater-than operator.)
17595 Returns a representation of the parameter, or NULL if an error
17596 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17597 true iff the declarator is of the form "(p)". */
17599 static cp_parameter_declarator *
17600 cp_parser_parameter_declaration (cp_parser *parser,
17601 bool template_parm_p,
17602 bool *parenthesized_p)
17604 int declares_class_or_enum;
17605 cp_decl_specifier_seq decl_specifiers;
17606 cp_declarator *declarator;
17607 tree default_argument;
17608 cp_token *token = NULL, *declarator_token_start = NULL;
17609 const char *saved_message;
17611 /* In a template parameter, `>' is not an operator.
17615 When parsing a default template-argument for a non-type
17616 template-parameter, the first non-nested `>' is taken as the end
17617 of the template parameter-list rather than a greater-than
17620 /* Type definitions may not appear in parameter types. */
17621 saved_message = parser->type_definition_forbidden_message;
17622 parser->type_definition_forbidden_message
17623 = G_("types may not be defined in parameter types");
17625 /* Parse the declaration-specifiers. */
17626 cp_parser_decl_specifier_seq (parser,
17627 CP_PARSER_FLAGS_NONE,
17629 &declares_class_or_enum);
17631 /* Complain about missing 'typename' or other invalid type names. */
17632 if (!decl_specifiers.any_type_specifiers_p)
17633 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17635 /* If an error occurred, there's no reason to attempt to parse the
17636 rest of the declaration. */
17637 if (cp_parser_error_occurred (parser))
17639 parser->type_definition_forbidden_message = saved_message;
17643 /* Peek at the next token. */
17644 token = cp_lexer_peek_token (parser->lexer);
17646 /* If the next token is a `)', `,', `=', `>', or `...', then there
17647 is no declarator. However, when variadic templates are enabled,
17648 there may be a declarator following `...'. */
17649 if (token->type == CPP_CLOSE_PAREN
17650 || token->type == CPP_COMMA
17651 || token->type == CPP_EQ
17652 || token->type == CPP_GREATER)
17655 if (parenthesized_p)
17656 *parenthesized_p = false;
17658 /* Otherwise, there should be a declarator. */
17661 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17662 parser->default_arg_ok_p = false;
17664 /* After seeing a decl-specifier-seq, if the next token is not a
17665 "(", there is no possibility that the code is a valid
17666 expression. Therefore, if parsing tentatively, we commit at
17668 if (!parser->in_template_argument_list_p
17669 /* In an expression context, having seen:
17673 we cannot be sure whether we are looking at a
17674 function-type (taking a "char" as a parameter) or a cast
17675 of some object of type "char" to "int". */
17676 && !parser->in_type_id_in_expr_p
17677 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17678 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17679 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17680 cp_parser_commit_to_tentative_parse (parser);
17681 /* Parse the declarator. */
17682 declarator_token_start = token;
17683 declarator = cp_parser_declarator (parser,
17684 CP_PARSER_DECLARATOR_EITHER,
17685 /*ctor_dtor_or_conv_p=*/NULL,
17687 /*member_p=*/false);
17688 parser->default_arg_ok_p = saved_default_arg_ok_p;
17689 /* After the declarator, allow more attributes. */
17690 decl_specifiers.attributes
17691 = chainon (decl_specifiers.attributes,
17692 cp_parser_attributes_opt (parser));
17695 /* If the next token is an ellipsis, and we have not seen a
17696 declarator name, and the type of the declarator contains parameter
17697 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17698 a parameter pack expansion expression. Otherwise, leave the
17699 ellipsis for a C-style variadic function. */
17700 token = cp_lexer_peek_token (parser->lexer);
17701 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17703 tree type = decl_specifiers.type;
17705 if (type && DECL_P (type))
17706 type = TREE_TYPE (type);
17709 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17710 && declarator_can_be_parameter_pack (declarator)
17711 && (!declarator || !declarator->parameter_pack_p)
17712 && uses_parameter_packs (type))
17714 /* Consume the `...'. */
17715 cp_lexer_consume_token (parser->lexer);
17716 maybe_warn_variadic_templates ();
17718 /* Build a pack expansion type */
17720 declarator->parameter_pack_p = true;
17722 decl_specifiers.type = make_pack_expansion (type);
17726 /* The restriction on defining new types applies only to the type
17727 of the parameter, not to the default argument. */
17728 parser->type_definition_forbidden_message = saved_message;
17730 /* If the next token is `=', then process a default argument. */
17731 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17733 token = cp_lexer_peek_token (parser->lexer);
17734 /* If we are defining a class, then the tokens that make up the
17735 default argument must be saved and processed later. */
17736 if (!template_parm_p && at_class_scope_p ()
17737 && TYPE_BEING_DEFINED (current_class_type)
17738 && !LAMBDA_TYPE_P (current_class_type))
17739 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17740 /* Outside of a class definition, we can just parse the
17741 assignment-expression. */
17744 = cp_parser_default_argument (parser, template_parm_p);
17746 if (!parser->default_arg_ok_p)
17748 if (flag_permissive)
17749 warning (0, "deprecated use of default argument for parameter of non-function");
17752 error_at (token->location,
17753 "default arguments are only "
17754 "permitted for function parameters");
17755 default_argument = NULL_TREE;
17758 else if ((declarator && declarator->parameter_pack_p)
17759 || (decl_specifiers.type
17760 && PACK_EXPANSION_P (decl_specifiers.type)))
17762 /* Find the name of the parameter pack. */
17763 cp_declarator *id_declarator = declarator;
17764 while (id_declarator && id_declarator->kind != cdk_id)
17765 id_declarator = id_declarator->declarator;
17767 if (id_declarator && id_declarator->kind == cdk_id)
17768 error_at (declarator_token_start->location,
17770 ? G_("template parameter pack %qD "
17771 "cannot have a default argument")
17772 : G_("parameter pack %qD cannot have "
17773 "a default argument"),
17774 id_declarator->u.id.unqualified_name);
17776 error_at (declarator_token_start->location,
17778 ? G_("template parameter pack cannot have "
17779 "a default argument")
17780 : G_("parameter pack cannot have a "
17781 "default argument"));
17783 default_argument = NULL_TREE;
17787 default_argument = NULL_TREE;
17789 return make_parameter_declarator (&decl_specifiers,
17794 /* Parse a default argument and return it.
17796 TEMPLATE_PARM_P is true if this is a default argument for a
17797 non-type template parameter. */
17799 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17801 tree default_argument = NULL_TREE;
17802 bool saved_greater_than_is_operator_p;
17803 bool saved_local_variables_forbidden_p;
17804 bool non_constant_p, is_direct_init;
17806 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17808 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17809 parser->greater_than_is_operator_p = !template_parm_p;
17810 /* Local variable names (and the `this' keyword) may not
17811 appear in a default argument. */
17812 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17813 parser->local_variables_forbidden_p = true;
17814 /* Parse the assignment-expression. */
17815 if (template_parm_p)
17816 push_deferring_access_checks (dk_no_deferred);
17818 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17819 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17820 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17821 if (template_parm_p)
17822 pop_deferring_access_checks ();
17823 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17824 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17826 return default_argument;
17829 /* Parse a function-body.
17832 compound_statement */
17835 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17837 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17840 /* Parse a ctor-initializer-opt followed by a function-body. Return
17841 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17842 is true we are parsing a function-try-block. */
17845 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17846 bool in_function_try_block)
17849 bool ctor_initializer_p;
17850 const bool check_body_p =
17851 DECL_CONSTRUCTOR_P (current_function_decl)
17852 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17855 /* Begin the function body. */
17856 body = begin_function_body ();
17857 /* Parse the optional ctor-initializer. */
17858 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17860 /* If we're parsing a constexpr constructor definition, we need
17861 to check that the constructor body is indeed empty. However,
17862 before we get to cp_parser_function_body lot of junk has been
17863 generated, so we can't just check that we have an empty block.
17864 Rather we take a snapshot of the outermost block, and check whether
17865 cp_parser_function_body changed its state. */
17868 list = cur_stmt_list;
17869 if (STATEMENT_LIST_TAIL (list))
17870 last = STATEMENT_LIST_TAIL (list)->stmt;
17872 /* Parse the function-body. */
17873 cp_parser_function_body (parser, in_function_try_block);
17875 check_constexpr_ctor_body (last, list);
17876 /* Finish the function body. */
17877 finish_function_body (body);
17879 return ctor_initializer_p;
17882 /* Parse an initializer.
17885 = initializer-clause
17886 ( expression-list )
17888 Returns an expression representing the initializer. If no
17889 initializer is present, NULL_TREE is returned.
17891 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17892 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17893 set to TRUE if there is no initializer present. If there is an
17894 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17895 is set to true; otherwise it is set to false. */
17898 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17899 bool* non_constant_p)
17904 /* Peek at the next token. */
17905 token = cp_lexer_peek_token (parser->lexer);
17907 /* Let our caller know whether or not this initializer was
17909 *is_direct_init = (token->type != CPP_EQ);
17910 /* Assume that the initializer is constant. */
17911 *non_constant_p = false;
17913 if (token->type == CPP_EQ)
17915 /* Consume the `='. */
17916 cp_lexer_consume_token (parser->lexer);
17917 /* Parse the initializer-clause. */
17918 init = cp_parser_initializer_clause (parser, non_constant_p);
17920 else if (token->type == CPP_OPEN_PAREN)
17922 vec<tree, va_gc> *vec;
17923 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17925 /*allow_expansion_p=*/true,
17928 return error_mark_node;
17929 init = build_tree_list_vec (vec);
17930 release_tree_vector (vec);
17932 else if (token->type == CPP_OPEN_BRACE)
17934 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17935 init = cp_parser_braced_list (parser, non_constant_p);
17936 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17940 /* Anything else is an error. */
17941 cp_parser_error (parser, "expected initializer");
17942 init = error_mark_node;
17948 /* Parse an initializer-clause.
17950 initializer-clause:
17951 assignment-expression
17954 Returns an expression representing the initializer.
17956 If the `assignment-expression' production is used the value
17957 returned is simply a representation for the expression.
17959 Otherwise, calls cp_parser_braced_list. */
17962 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17966 /* Assume the expression is constant. */
17967 *non_constant_p = false;
17969 /* If it is not a `{', then we are looking at an
17970 assignment-expression. */
17971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17974 = cp_parser_constant_expression (parser,
17975 /*allow_non_constant_p=*/true,
17979 initializer = cp_parser_braced_list (parser, non_constant_p);
17981 return initializer;
17984 /* Parse a brace-enclosed initializer list.
17987 { initializer-list , [opt] }
17990 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17991 the elements of the initializer-list (or NULL, if the last
17992 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17993 NULL_TREE. There is no way to detect whether or not the optional
17994 trailing `,' was provided. NON_CONSTANT_P is as for
17995 cp_parser_initializer. */
17998 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18002 /* Consume the `{' token. */
18003 cp_lexer_consume_token (parser->lexer);
18004 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18005 initializer = make_node (CONSTRUCTOR);
18006 /* If it's not a `}', then there is a non-trivial initializer. */
18007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18009 /* Parse the initializer list. */
18010 CONSTRUCTOR_ELTS (initializer)
18011 = cp_parser_initializer_list (parser, non_constant_p);
18012 /* A trailing `,' token is allowed. */
18013 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18014 cp_lexer_consume_token (parser->lexer);
18017 *non_constant_p = false;
18018 /* Now, there should be a trailing `}'. */
18019 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18020 TREE_TYPE (initializer) = init_list_type_node;
18021 return initializer;
18024 /* Parse an initializer-list.
18027 initializer-clause ... [opt]
18028 initializer-list , initializer-clause ... [opt]
18033 designation initializer-clause ...[opt]
18034 initializer-list , designation initializer-clause ...[opt]
18039 [ constant-expression ] =
18041 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18042 for the initializer. If the INDEX of the elt is non-NULL, it is the
18043 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18044 as for cp_parser_initializer. */
18046 static vec<constructor_elt, va_gc> *
18047 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18049 vec<constructor_elt, va_gc> *v = NULL;
18051 /* Assume all of the expressions are constant. */
18052 *non_constant_p = false;
18054 /* Parse the rest of the list. */
18060 bool clause_non_constant_p;
18062 /* If the next token is an identifier and the following one is a
18063 colon, we are looking at the GNU designated-initializer
18065 if (cp_parser_allow_gnu_extensions_p (parser)
18066 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18067 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18069 /* Warn the user that they are using an extension. */
18070 pedwarn (input_location, OPT_Wpedantic,
18071 "ISO C++ does not allow designated initializers");
18072 /* Consume the identifier. */
18073 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18074 /* Consume the `:'. */
18075 cp_lexer_consume_token (parser->lexer);
18077 /* Also handle the C99 syntax, '. id ='. */
18078 else if (cp_parser_allow_gnu_extensions_p (parser)
18079 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18080 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18081 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18083 /* Warn the user that they are using an extension. */
18084 pedwarn (input_location, OPT_Wpedantic,
18085 "ISO C++ does not allow C99 designated initializers");
18086 /* Consume the `.'. */
18087 cp_lexer_consume_token (parser->lexer);
18088 /* Consume the identifier. */
18089 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18090 /* Consume the `='. */
18091 cp_lexer_consume_token (parser->lexer);
18093 /* Also handle C99 array designators, '[ const ] ='. */
18094 else if (cp_parser_allow_gnu_extensions_p (parser)
18095 && !c_dialect_objc ()
18096 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18098 /* In C++11, [ could start a lambda-introducer. */
18099 bool non_const = false;
18101 cp_parser_parse_tentatively (parser);
18102 cp_lexer_consume_token (parser->lexer);
18103 designator = cp_parser_constant_expression (parser, true, &non_const);
18104 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18105 cp_parser_require (parser, CPP_EQ, RT_EQ);
18106 if (!cp_parser_parse_definitely (parser))
18107 designator = NULL_TREE;
18108 else if (non_const)
18109 require_potential_rvalue_constant_expression (designator);
18112 designator = NULL_TREE;
18114 /* Parse the initializer. */
18115 initializer = cp_parser_initializer_clause (parser,
18116 &clause_non_constant_p);
18117 /* If any clause is non-constant, so is the entire initializer. */
18118 if (clause_non_constant_p)
18119 *non_constant_p = true;
18121 /* If we have an ellipsis, this is an initializer pack
18123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18125 /* Consume the `...'. */
18126 cp_lexer_consume_token (parser->lexer);
18128 /* Turn the initializer into an initializer expansion. */
18129 initializer = make_pack_expansion (initializer);
18132 /* Add it to the vector. */
18133 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18135 /* If the next token is not a comma, we have reached the end of
18137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18140 /* Peek at the next token. */
18141 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18142 /* If the next token is a `}', then we're still done. An
18143 initializer-clause can have a trailing `,' after the
18144 initializer-list and before the closing `}'. */
18145 if (token->type == CPP_CLOSE_BRACE)
18148 /* Consume the `,' token. */
18149 cp_lexer_consume_token (parser->lexer);
18155 /* Classes [gram.class] */
18157 /* Parse a class-name.
18163 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18164 to indicate that names looked up in dependent types should be
18165 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18166 keyword has been used to indicate that the name that appears next
18167 is a template. TAG_TYPE indicates the explicit tag given before
18168 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18169 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18170 is the class being defined in a class-head.
18172 Returns the TYPE_DECL representing the class. */
18175 cp_parser_class_name (cp_parser *parser,
18176 bool typename_keyword_p,
18177 bool template_keyword_p,
18178 enum tag_types tag_type,
18179 bool check_dependency_p,
18181 bool is_declaration)
18187 tree identifier = NULL_TREE;
18189 /* All class-names start with an identifier. */
18190 token = cp_lexer_peek_token (parser->lexer);
18191 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18193 cp_parser_error (parser, "expected class-name");
18194 return error_mark_node;
18197 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18198 to a template-id, so we save it here. */
18199 scope = parser->scope;
18200 if (scope == error_mark_node)
18201 return error_mark_node;
18203 /* Any name names a type if we're following the `typename' keyword
18204 in a qualified name where the enclosing scope is type-dependent. */
18205 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18206 && dependent_type_p (scope));
18207 /* Handle the common case (an identifier, but not a template-id)
18209 if (token->type == CPP_NAME
18210 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18212 cp_token *identifier_token;
18215 /* Look for the identifier. */
18216 identifier_token = cp_lexer_peek_token (parser->lexer);
18217 ambiguous_p = identifier_token->ambiguous_p;
18218 identifier = cp_parser_identifier (parser);
18219 /* If the next token isn't an identifier, we are certainly not
18220 looking at a class-name. */
18221 if (identifier == error_mark_node)
18222 decl = error_mark_node;
18223 /* If we know this is a type-name, there's no need to look it
18225 else if (typename_p)
18229 tree ambiguous_decls;
18230 /* If we already know that this lookup is ambiguous, then
18231 we've already issued an error message; there's no reason
18235 cp_parser_simulate_error (parser);
18236 return error_mark_node;
18238 /* If the next token is a `::', then the name must be a type
18241 [basic.lookup.qual]
18243 During the lookup for a name preceding the :: scope
18244 resolution operator, object, function, and enumerator
18245 names are ignored. */
18246 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18247 tag_type = typename_type;
18248 /* Look up the name. */
18249 decl = cp_parser_lookup_name (parser, identifier,
18251 /*is_template=*/false,
18252 /*is_namespace=*/false,
18253 check_dependency_p,
18255 identifier_token->location);
18256 if (ambiguous_decls)
18258 if (cp_parser_parsing_tentatively (parser))
18259 cp_parser_simulate_error (parser);
18260 return error_mark_node;
18266 /* Try a template-id. */
18267 decl = cp_parser_template_id (parser, template_keyword_p,
18268 check_dependency_p,
18271 if (decl == error_mark_node)
18272 return error_mark_node;
18275 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18277 /* If this is a typename, create a TYPENAME_TYPE. */
18278 if (typename_p && decl != error_mark_node)
18280 decl = make_typename_type (scope, decl, typename_type,
18281 /*complain=*/tf_error);
18282 if (decl != error_mark_node)
18283 decl = TYPE_NAME (decl);
18286 decl = strip_using_decl (decl);
18288 /* Check to see that it is really the name of a class. */
18289 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18290 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
18291 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18292 /* Situations like this:
18294 template <typename T> struct A {
18295 typename T::template X<int>::I i;
18298 are problematic. Is `T::template X<int>' a class-name? The
18299 standard does not seem to be definitive, but there is no other
18300 valid interpretation of the following `::'. Therefore, those
18301 names are considered class-names. */
18303 decl = make_typename_type (scope, decl, tag_type, tf_error);
18304 if (decl != error_mark_node)
18305 decl = TYPE_NAME (decl);
18307 else if (TREE_CODE (decl) != TYPE_DECL
18308 || TREE_TYPE (decl) == error_mark_node
18309 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18310 /* In Objective-C 2.0, a classname followed by '.' starts a
18311 dot-syntax expression, and it's not a type-name. */
18312 || (c_dialect_objc ()
18313 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18314 && objc_is_class_name (decl)))
18315 decl = error_mark_node;
18317 if (decl == error_mark_node)
18318 cp_parser_error (parser, "expected class-name");
18319 else if (identifier && !parser->scope)
18320 maybe_note_name_used_in_class (identifier, decl);
18325 /* Parse a class-specifier.
18328 class-head { member-specification [opt] }
18330 Returns the TREE_TYPE representing the class. */
18333 cp_parser_class_specifier_1 (cp_parser* parser)
18336 tree attributes = NULL_TREE;
18337 bool nested_name_specifier_p;
18338 unsigned saved_num_template_parameter_lists;
18339 bool saved_in_function_body;
18340 unsigned char in_statement;
18341 bool in_switch_statement_p;
18342 bool saved_in_unbraced_linkage_specification_p;
18343 tree old_scope = NULL_TREE;
18344 tree scope = NULL_TREE;
18345 cp_token *closing_brace;
18347 push_deferring_access_checks (dk_no_deferred);
18349 /* Parse the class-head. */
18350 type = cp_parser_class_head (parser,
18351 &nested_name_specifier_p);
18352 /* If the class-head was a semantic disaster, skip the entire body
18356 cp_parser_skip_to_end_of_block_or_statement (parser);
18357 pop_deferring_access_checks ();
18358 return error_mark_node;
18361 /* Look for the `{'. */
18362 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18364 pop_deferring_access_checks ();
18365 return error_mark_node;
18368 /* Issue an error message if type-definitions are forbidden here. */
18369 cp_parser_check_type_definition (parser);
18370 /* Remember that we are defining one more class. */
18371 ++parser->num_classes_being_defined;
18372 /* Inside the class, surrounding template-parameter-lists do not
18374 saved_num_template_parameter_lists
18375 = parser->num_template_parameter_lists;
18376 parser->num_template_parameter_lists = 0;
18377 /* We are not in a function body. */
18378 saved_in_function_body = parser->in_function_body;
18379 parser->in_function_body = false;
18380 /* Or in a loop. */
18381 in_statement = parser->in_statement;
18382 parser->in_statement = 0;
18383 /* Or in a switch. */
18384 in_switch_statement_p = parser->in_switch_statement_p;
18385 parser->in_switch_statement_p = false;
18386 /* We are not immediately inside an extern "lang" block. */
18387 saved_in_unbraced_linkage_specification_p
18388 = parser->in_unbraced_linkage_specification_p;
18389 parser->in_unbraced_linkage_specification_p = false;
18391 /* Start the class. */
18392 if (nested_name_specifier_p)
18394 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18395 old_scope = push_inner_scope (scope);
18397 type = begin_class_definition (type);
18399 if (type == error_mark_node)
18400 /* If the type is erroneous, skip the entire body of the class. */
18401 cp_parser_skip_to_closing_brace (parser);
18403 /* Parse the member-specification. */
18404 cp_parser_member_specification_opt (parser);
18406 /* Look for the trailing `}'. */
18407 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18408 /* Look for trailing attributes to apply to this class. */
18409 if (cp_parser_allow_gnu_extensions_p (parser))
18410 attributes = cp_parser_gnu_attributes_opt (parser);
18411 if (type != error_mark_node)
18412 type = finish_struct (type, attributes);
18413 if (nested_name_specifier_p)
18414 pop_inner_scope (old_scope, scope);
18416 /* We've finished a type definition. Check for the common syntax
18417 error of forgetting a semicolon after the definition. We need to
18418 be careful, as we can't just check for not-a-semicolon and be done
18419 with it; the user might have typed:
18421 class X { } c = ...;
18422 class X { } *p = ...;
18424 and so forth. Instead, enumerate all the possible tokens that
18425 might follow this production; if we don't see one of them, then
18426 complain and silently insert the semicolon. */
18428 cp_token *token = cp_lexer_peek_token (parser->lexer);
18429 bool want_semicolon = true;
18431 if (cp_next_tokens_can_be_std_attribute_p (parser))
18432 /* Don't try to parse c++11 attributes here. As per the
18433 grammar, that should be a task for
18434 cp_parser_decl_specifier_seq. */
18435 want_semicolon = false;
18437 switch (token->type)
18440 case CPP_SEMICOLON:
18443 case CPP_OPEN_PAREN:
18444 case CPP_CLOSE_PAREN:
18446 want_semicolon = false;
18449 /* While it's legal for type qualifiers and storage class
18450 specifiers to follow type definitions in the grammar, only
18451 compiler testsuites contain code like that. Assume that if
18452 we see such code, then what we're really seeing is a case
18456 const <type> var = ...;
18461 static <type> func (...) ...
18463 i.e. the qualifier or specifier applies to the next
18464 declaration. To do so, however, we need to look ahead one
18465 more token to see if *that* token is a type specifier.
18467 This code could be improved to handle:
18470 static const <type> var = ...; */
18472 if (keyword_is_decl_specifier (token->keyword))
18474 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18476 /* Handling user-defined types here would be nice, but very
18479 = (lookahead->type == CPP_KEYWORD
18480 && keyword_begins_type_specifier (lookahead->keyword));
18487 /* If we don't have a type, then something is very wrong and we
18488 shouldn't try to do anything clever. Likewise for not seeing the
18490 if (closing_brace && TYPE_P (type) && want_semicolon)
18492 cp_token_position prev
18493 = cp_lexer_previous_token_position (parser->lexer);
18494 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18495 location_t loc = prev_token->location;
18497 if (CLASSTYPE_DECLARED_CLASS (type))
18498 error_at (loc, "expected %<;%> after class definition");
18499 else if (TREE_CODE (type) == RECORD_TYPE)
18500 error_at (loc, "expected %<;%> after struct definition");
18501 else if (TREE_CODE (type) == UNION_TYPE)
18502 error_at (loc, "expected %<;%> after union definition");
18504 gcc_unreachable ();
18506 /* Unget one token and smash it to look as though we encountered
18507 a semicolon in the input stream. */
18508 cp_lexer_set_token_position (parser->lexer, prev);
18509 token = cp_lexer_peek_token (parser->lexer);
18510 token->type = CPP_SEMICOLON;
18511 token->keyword = RID_MAX;
18515 /* If this class is not itself within the scope of another class,
18516 then we need to parse the bodies of all of the queued function
18517 definitions. Note that the queued functions defined in a class
18518 are not always processed immediately following the
18519 class-specifier for that class. Consider:
18522 struct B { void f() { sizeof (A); } };
18525 If `f' were processed before the processing of `A' were
18526 completed, there would be no way to compute the size of `A'.
18527 Note that the nesting we are interested in here is lexical --
18528 not the semantic nesting given by TYPE_CONTEXT. In particular,
18531 struct A { struct B; };
18532 struct A::B { void f() { } };
18534 there is no need to delay the parsing of `A::B::f'. */
18535 if (--parser->num_classes_being_defined == 0)
18538 tree class_type = NULL_TREE;
18539 tree pushed_scope = NULL_TREE;
18541 cp_default_arg_entry *e;
18542 tree save_ccp, save_ccr;
18544 /* In a first pass, parse default arguments to the functions.
18545 Then, in a second pass, parse the bodies of the functions.
18546 This two-phased approach handles cases like:
18554 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18557 /* If there are default arguments that have not yet been processed,
18558 take care of them now. */
18559 if (class_type != e->class_type)
18562 pop_scope (pushed_scope);
18563 class_type = e->class_type;
18564 pushed_scope = push_scope (class_type);
18566 /* Make sure that any template parameters are in scope. */
18567 maybe_begin_member_template_processing (decl);
18568 /* Parse the default argument expressions. */
18569 cp_parser_late_parsing_default_args (parser, decl);
18570 /* Remove any template parameters from the symbol table. */
18571 maybe_end_member_template_processing ();
18573 vec_safe_truncate (unparsed_funs_with_default_args, 0);
18574 /* Now parse any NSDMIs. */
18575 save_ccp = current_class_ptr;
18576 save_ccr = current_class_ref;
18577 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18579 if (class_type != DECL_CONTEXT (decl))
18582 pop_scope (pushed_scope);
18583 class_type = DECL_CONTEXT (decl);
18584 pushed_scope = push_scope (class_type);
18586 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18587 cp_parser_late_parsing_nsdmi (parser, decl);
18589 vec_safe_truncate (unparsed_nsdmis, 0);
18590 current_class_ptr = save_ccp;
18591 current_class_ref = save_ccr;
18593 pop_scope (pushed_scope);
18594 /* Now parse the body of the functions. */
18595 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
18596 cp_parser_late_parsing_for_member (parser, decl);
18597 vec_safe_truncate (unparsed_funs_with_definitions, 0);
18600 /* Put back any saved access checks. */
18601 pop_deferring_access_checks ();
18603 /* Restore saved state. */
18604 parser->in_switch_statement_p = in_switch_statement_p;
18605 parser->in_statement = in_statement;
18606 parser->in_function_body = saved_in_function_body;
18607 parser->num_template_parameter_lists
18608 = saved_num_template_parameter_lists;
18609 parser->in_unbraced_linkage_specification_p
18610 = saved_in_unbraced_linkage_specification_p;
18616 cp_parser_class_specifier (cp_parser* parser)
18619 timevar_push (TV_PARSE_STRUCT);
18620 ret = cp_parser_class_specifier_1 (parser);
18621 timevar_pop (TV_PARSE_STRUCT);
18625 /* Parse a class-head.
18628 class-key identifier [opt] base-clause [opt]
18629 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18630 class-key nested-name-specifier [opt] template-id
18633 class-virt-specifier:
18637 class-key attributes identifier [opt] base-clause [opt]
18638 class-key attributes nested-name-specifier identifier base-clause [opt]
18639 class-key attributes nested-name-specifier [opt] template-id
18642 Upon return BASES is initialized to the list of base classes (or
18643 NULL, if there are none) in the same form returned by
18644 cp_parser_base_clause.
18646 Returns the TYPE of the indicated class. Sets
18647 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18648 involving a nested-name-specifier was used, and FALSE otherwise.
18650 Returns error_mark_node if this is not a class-head.
18652 Returns NULL_TREE if the class-head is syntactically valid, but
18653 semantically invalid in a way that means we should skip the entire
18654 body of the class. */
18657 cp_parser_class_head (cp_parser* parser,
18658 bool* nested_name_specifier_p)
18660 tree nested_name_specifier;
18661 enum tag_types class_key;
18662 tree id = NULL_TREE;
18663 tree type = NULL_TREE;
18666 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18667 bool template_id_p = false;
18668 bool qualified_p = false;
18669 bool invalid_nested_name_p = false;
18670 bool invalid_explicit_specialization_p = false;
18671 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18672 tree pushed_scope = NULL_TREE;
18673 unsigned num_templates;
18674 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18675 /* Assume no nested-name-specifier will be present. */
18676 *nested_name_specifier_p = false;
18677 /* Assume no template parameter lists will be used in defining the
18680 parser->colon_corrects_to_scope_p = false;
18682 /* Look for the class-key. */
18683 class_key = cp_parser_class_key (parser);
18684 if (class_key == none_type)
18685 return error_mark_node;
18687 /* Parse the attributes. */
18688 attributes = cp_parser_attributes_opt (parser);
18690 /* If the next token is `::', that is invalid -- but sometimes
18691 people do try to write:
18695 Handle this gracefully by accepting the extra qualifier, and then
18696 issuing an error about it later if this really is a
18697 class-head. If it turns out just to be an elaborated type
18698 specifier, remain silent. */
18699 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18700 qualified_p = true;
18702 push_deferring_access_checks (dk_no_check);
18704 /* Determine the name of the class. Begin by looking for an
18705 optional nested-name-specifier. */
18706 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18707 nested_name_specifier
18708 = cp_parser_nested_name_specifier_opt (parser,
18709 /*typename_keyword_p=*/false,
18710 /*check_dependency_p=*/false,
18712 /*is_declaration=*/false);
18713 /* If there was a nested-name-specifier, then there *must* be an
18715 if (nested_name_specifier)
18717 type_start_token = cp_lexer_peek_token (parser->lexer);
18718 /* Although the grammar says `identifier', it really means
18719 `class-name' or `template-name'. You are only allowed to
18720 define a class that has already been declared with this
18723 The proposed resolution for Core Issue 180 says that wherever
18724 you see `class T::X' you should treat `X' as a type-name.
18726 It is OK to define an inaccessible class; for example:
18728 class A { class B; };
18731 We do not know if we will see a class-name, or a
18732 template-name. We look for a class-name first, in case the
18733 class-name is a template-id; if we looked for the
18734 template-name first we would stop after the template-name. */
18735 cp_parser_parse_tentatively (parser);
18736 type = cp_parser_class_name (parser,
18737 /*typename_keyword_p=*/false,
18738 /*template_keyword_p=*/false,
18740 /*check_dependency_p=*/false,
18741 /*class_head_p=*/true,
18742 /*is_declaration=*/false);
18743 /* If that didn't work, ignore the nested-name-specifier. */
18744 if (!cp_parser_parse_definitely (parser))
18746 invalid_nested_name_p = true;
18747 type_start_token = cp_lexer_peek_token (parser->lexer);
18748 id = cp_parser_identifier (parser);
18749 if (id == error_mark_node)
18752 /* If we could not find a corresponding TYPE, treat this
18753 declaration like an unqualified declaration. */
18754 if (type == error_mark_node)
18755 nested_name_specifier = NULL_TREE;
18756 /* Otherwise, count the number of templates used in TYPE and its
18757 containing scopes. */
18762 for (scope = TREE_TYPE (type);
18763 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18764 scope = (TYPE_P (scope)
18765 ? TYPE_CONTEXT (scope)
18766 : DECL_CONTEXT (scope)))
18768 && CLASS_TYPE_P (scope)
18769 && CLASSTYPE_TEMPLATE_INFO (scope)
18770 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18771 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
18772 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
18776 /* Otherwise, the identifier is optional. */
18779 /* We don't know whether what comes next is a template-id,
18780 an identifier, or nothing at all. */
18781 cp_parser_parse_tentatively (parser);
18782 /* Check for a template-id. */
18783 type_start_token = cp_lexer_peek_token (parser->lexer);
18784 id = cp_parser_template_id (parser,
18785 /*template_keyword_p=*/false,
18786 /*check_dependency_p=*/true,
18788 /*is_declaration=*/true);
18789 /* If that didn't work, it could still be an identifier. */
18790 if (!cp_parser_parse_definitely (parser))
18792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18794 type_start_token = cp_lexer_peek_token (parser->lexer);
18795 id = cp_parser_identifier (parser);
18802 template_id_p = true;
18807 pop_deferring_access_checks ();
18811 cp_parser_check_for_invalid_template_id (parser, id,
18813 type_start_token->location);
18815 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18817 /* If it's not a `:' or a `{' then we can't really be looking at a
18818 class-head, since a class-head only appears as part of a
18819 class-specifier. We have to detect this situation before calling
18820 xref_tag, since that has irreversible side-effects. */
18821 if (!cp_parser_next_token_starts_class_definition_p (parser))
18823 cp_parser_error (parser, "expected %<{%> or %<:%>");
18824 type = error_mark_node;
18828 /* At this point, we're going ahead with the class-specifier, even
18829 if some other problem occurs. */
18830 cp_parser_commit_to_tentative_parse (parser);
18831 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18833 cp_parser_error (parser,
18834 "cannot specify %<override%> for a class");
18835 type = error_mark_node;
18838 /* Issue the error about the overly-qualified name now. */
18841 cp_parser_error (parser,
18842 "global qualification of class name is invalid");
18843 type = error_mark_node;
18846 else if (invalid_nested_name_p)
18848 cp_parser_error (parser,
18849 "qualified name does not name a class");
18850 type = error_mark_node;
18853 else if (nested_name_specifier)
18857 /* Reject typedef-names in class heads. */
18858 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18860 error_at (type_start_token->location,
18861 "invalid class name in declaration of %qD",
18867 /* Figure out in what scope the declaration is being placed. */
18868 scope = current_scope ();
18869 /* If that scope does not contain the scope in which the
18870 class was originally declared, the program is invalid. */
18871 if (scope && !is_ancestor (scope, nested_name_specifier))
18873 if (at_namespace_scope_p ())
18874 error_at (type_start_token->location,
18875 "declaration of %qD in namespace %qD which does not "
18877 type, scope, nested_name_specifier);
18879 error_at (type_start_token->location,
18880 "declaration of %qD in %qD which does not enclose %qD",
18881 type, scope, nested_name_specifier);
18887 A declarator-id shall not be qualified except for the
18888 definition of a ... nested class outside of its class
18889 ... [or] the definition or explicit instantiation of a
18890 class member of a namespace outside of its namespace. */
18891 if (scope == nested_name_specifier)
18893 permerror (nested_name_specifier_token_start->location,
18894 "extra qualification not allowed");
18895 nested_name_specifier = NULL_TREE;
18899 /* An explicit-specialization must be preceded by "template <>". If
18900 it is not, try to recover gracefully. */
18901 if (at_namespace_scope_p ()
18902 && parser->num_template_parameter_lists == 0
18905 error_at (type_start_token->location,
18906 "an explicit specialization must be preceded by %<template <>%>");
18907 invalid_explicit_specialization_p = true;
18908 /* Take the same action that would have been taken by
18909 cp_parser_explicit_specialization. */
18910 ++parser->num_template_parameter_lists;
18911 begin_specialization ();
18913 /* There must be no "return" statements between this point and the
18914 end of this function; set "type "to the correct return value and
18915 use "goto done;" to return. */
18916 /* Make sure that the right number of template parameters were
18918 if (!cp_parser_check_template_parameters (parser, num_templates,
18919 type_start_token->location,
18920 /*declarator=*/NULL))
18922 /* If something went wrong, there is no point in even trying to
18923 process the class-definition. */
18928 /* Look up the type. */
18931 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18932 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18933 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18935 error_at (type_start_token->location,
18936 "function template %qD redeclared as a class template", id);
18937 type = error_mark_node;
18941 type = TREE_TYPE (id);
18942 type = maybe_process_partial_specialization (type);
18944 if (nested_name_specifier)
18945 pushed_scope = push_scope (nested_name_specifier);
18947 else if (nested_name_specifier)
18953 template <typename T> struct S { struct T };
18954 template <typename T> struct S<T>::T { };
18956 we will get a TYPENAME_TYPE when processing the definition of
18957 `S::T'. We need to resolve it to the actual type before we
18958 try to define it. */
18959 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18961 class_type = resolve_typename_type (TREE_TYPE (type),
18962 /*only_current_p=*/false);
18963 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18964 type = TYPE_NAME (class_type);
18967 cp_parser_error (parser, "could not resolve typename type");
18968 type = error_mark_node;
18972 if (maybe_process_partial_specialization (TREE_TYPE (type))
18973 == error_mark_node)
18979 class_type = current_class_type;
18980 /* Enter the scope indicated by the nested-name-specifier. */
18981 pushed_scope = push_scope (nested_name_specifier);
18982 /* Get the canonical version of this type. */
18983 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18984 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18985 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18987 type = push_template_decl (type);
18988 if (type == error_mark_node)
18995 type = TREE_TYPE (type);
18996 *nested_name_specifier_p = true;
18998 else /* The name is not a nested name. */
19000 /* If the class was unnamed, create a dummy name. */
19002 id = make_anon_name ();
19003 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19004 parser->num_template_parameter_lists);
19007 /* Indicate whether this class was declared as a `class' or as a
19009 if (TREE_CODE (type) == RECORD_TYPE)
19010 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19011 cp_parser_check_class_key (class_key, type);
19013 /* If this type was already complete, and we see another definition,
19014 that's an error. */
19015 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19017 error_at (type_start_token->location, "redefinition of %q#T",
19019 error_at (type_start_token->location, "previous definition of %q+#T",
19024 else if (type == error_mark_node)
19029 /* Apply attributes now, before any use of the class as a template
19030 argument in its base list. */
19031 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19032 fixup_attribute_variants (type);
19035 /* We will have entered the scope containing the class; the names of
19036 base classes should be looked up in that context. For example:
19038 struct A { struct B {}; struct C; };
19039 struct A::C : B {};
19043 /* Get the list of base-classes, if there is one. */
19044 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19045 bases = cp_parser_base_clause (parser);
19049 /* If we're really defining a class, process the base classes.
19050 If they're invalid, fail. */
19051 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19052 && !xref_basetypes (type, bases))
19056 /* Leave the scope given by the nested-name-specifier. We will
19057 enter the class scope itself while processing the members. */
19059 pop_scope (pushed_scope);
19061 if (invalid_explicit_specialization_p)
19063 end_specialization ();
19064 --parser->num_template_parameter_lists;
19068 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19069 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19070 CLASSTYPE_FINAL (type) = 1;
19072 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19076 /* Parse a class-key.
19083 Returns the kind of class-key specified, or none_type to indicate
19086 static enum tag_types
19087 cp_parser_class_key (cp_parser* parser)
19090 enum tag_types tag_type;
19092 /* Look for the class-key. */
19093 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19097 /* Check to see if the TOKEN is a class-key. */
19098 tag_type = cp_parser_token_is_class_key (token);
19100 cp_parser_error (parser, "expected class-key");
19104 /* Parse an (optional) member-specification.
19106 member-specification:
19107 member-declaration member-specification [opt]
19108 access-specifier : member-specification [opt] */
19111 cp_parser_member_specification_opt (cp_parser* parser)
19118 /* Peek at the next token. */
19119 token = cp_lexer_peek_token (parser->lexer);
19120 /* If it's a `}', or EOF then we've seen all the members. */
19121 if (token->type == CPP_CLOSE_BRACE
19122 || token->type == CPP_EOF
19123 || token->type == CPP_PRAGMA_EOL)
19126 /* See if this token is a keyword. */
19127 keyword = token->keyword;
19131 case RID_PROTECTED:
19133 /* Consume the access-specifier. */
19134 cp_lexer_consume_token (parser->lexer);
19135 /* Remember which access-specifier is active. */
19136 current_access_specifier = token->u.value;
19137 /* Look for the `:'. */
19138 cp_parser_require (parser, CPP_COLON, RT_COLON);
19142 /* Accept #pragmas at class scope. */
19143 if (token->type == CPP_PRAGMA)
19145 cp_parser_pragma (parser, pragma_external);
19149 /* Otherwise, the next construction must be a
19150 member-declaration. */
19151 cp_parser_member_declaration (parser);
19156 /* Parse a member-declaration.
19158 member-declaration:
19159 decl-specifier-seq [opt] member-declarator-list [opt] ;
19160 function-definition ; [opt]
19161 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19163 template-declaration
19166 member-declarator-list:
19168 member-declarator-list , member-declarator
19171 declarator pure-specifier [opt]
19172 declarator constant-initializer [opt]
19173 identifier [opt] : constant-expression
19177 member-declaration:
19178 __extension__ member-declaration
19181 declarator attributes [opt] pure-specifier [opt]
19182 declarator attributes [opt] constant-initializer [opt]
19183 identifier [opt] attributes [opt] : constant-expression
19187 member-declaration:
19188 static_assert-declaration */
19191 cp_parser_member_declaration (cp_parser* parser)
19193 cp_decl_specifier_seq decl_specifiers;
19194 tree prefix_attributes;
19196 int declares_class_or_enum;
19198 cp_token *token = NULL;
19199 cp_token *decl_spec_token_start = NULL;
19200 cp_token *initializer_token_start = NULL;
19201 int saved_pedantic;
19202 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19204 /* Check for the `__extension__' keyword. */
19205 if (cp_parser_extension_opt (parser, &saved_pedantic))
19208 cp_parser_member_declaration (parser);
19209 /* Restore the old value of the PEDANTIC flag. */
19210 pedantic = saved_pedantic;
19215 /* Check for a template-declaration. */
19216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19218 /* An explicit specialization here is an error condition, and we
19219 expect the specialization handler to detect and report this. */
19220 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19221 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19222 cp_parser_explicit_specialization (parser);
19224 cp_parser_template_declaration (parser, /*member_p=*/true);
19229 /* Check for a using-declaration. */
19230 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19232 if (cxx_dialect < cxx0x)
19234 /* Parse the using-declaration. */
19235 cp_parser_using_declaration (parser,
19236 /*access_declaration_p=*/false);
19242 bool alias_decl_expected;
19243 cp_parser_parse_tentatively (parser);
19244 decl = cp_parser_alias_declaration (parser);
19245 /* Note that if we actually see the '=' token after the
19246 identifier, cp_parser_alias_declaration commits the
19247 tentative parse. In that case, we really expects an
19248 alias-declaration. Otherwise, we expect a using
19250 alias_decl_expected =
19251 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19252 cp_parser_parse_definitely (parser);
19254 if (alias_decl_expected)
19255 finish_member_declaration (decl);
19257 cp_parser_using_declaration (parser,
19258 /*access_declaration_p=*/false);
19263 /* Check for @defs. */
19264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19267 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19268 ivar = ivar_chains;
19272 ivar = TREE_CHAIN (member);
19273 TREE_CHAIN (member) = NULL_TREE;
19274 finish_member_declaration (member);
19279 /* If the next token is `static_assert' we have a static assertion. */
19280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19282 cp_parser_static_assert (parser, /*member_p=*/true);
19286 parser->colon_corrects_to_scope_p = false;
19288 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19291 /* Parse the decl-specifier-seq. */
19292 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19293 cp_parser_decl_specifier_seq (parser,
19294 CP_PARSER_FLAGS_OPTIONAL,
19296 &declares_class_or_enum);
19297 /* Check for an invalid type-name. */
19298 if (!decl_specifiers.any_type_specifiers_p
19299 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19301 /* If there is no declarator, then the decl-specifier-seq should
19303 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19305 /* If there was no decl-specifier-seq, and the next token is a
19306 `;', then we have something like:
19312 Each member-declaration shall declare at least one member
19313 name of the class. */
19314 if (!decl_specifiers.any_specifiers_p)
19316 cp_token *token = cp_lexer_peek_token (parser->lexer);
19317 if (!in_system_header_at (token->location))
19318 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19324 /* See if this declaration is a friend. */
19325 friend_p = cp_parser_friend_p (&decl_specifiers);
19326 /* If there were decl-specifiers, check to see if there was
19327 a class-declaration. */
19328 type = check_tag_decl (&decl_specifiers,
19329 /*explicit_type_instantiation_p=*/false);
19330 /* Nested classes have already been added to the class, but
19331 a `friend' needs to be explicitly registered. */
19334 /* If the `friend' keyword was present, the friend must
19335 be introduced with a class-key. */
19336 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19337 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19338 "in C++03 a class-key must be used "
19339 "when declaring a friend");
19342 template <typename T> struct A {
19343 friend struct A<T>::B;
19346 A<T>::B will be represented by a TYPENAME_TYPE, and
19347 therefore not recognized by check_tag_decl. */
19350 type = decl_specifiers.type;
19351 if (type && TREE_CODE (type) == TYPE_DECL)
19352 type = TREE_TYPE (type);
19354 if (!type || !TYPE_P (type))
19355 error_at (decl_spec_token_start->location,
19356 "friend declaration does not name a class or "
19359 make_friend_class (current_class_type, type,
19360 /*complain=*/true);
19362 /* If there is no TYPE, an error message will already have
19364 else if (!type || type == error_mark_node)
19366 /* An anonymous aggregate has to be handled specially; such
19367 a declaration really declares a data member (with a
19368 particular type), as opposed to a nested class. */
19369 else if (ANON_AGGR_TYPE_P (type))
19372 if (decl_specifiers.storage_class != sc_none)
19373 error_at (decl_spec_token_start->location,
19374 "a storage class on an anonymous aggregate "
19375 "in class scope is not allowed");
19377 /* Remove constructors and such from TYPE, now that we
19378 know it is an anonymous aggregate. */
19379 fixup_anonymous_aggr (type);
19380 /* And make the corresponding data member. */
19381 decl = build_decl (decl_spec_token_start->location,
19382 FIELD_DECL, NULL_TREE, type);
19383 /* Add it to the class. */
19384 finish_member_declaration (decl);
19387 cp_parser_check_access_in_redeclaration
19389 decl_spec_token_start->location);
19394 bool assume_semicolon = false;
19396 /* Clear attributes from the decl_specifiers but keep them
19397 around as prefix attributes that apply them to the entity
19399 prefix_attributes = decl_specifiers.attributes;
19400 decl_specifiers.attributes = NULL_TREE;
19402 /* See if these declarations will be friends. */
19403 friend_p = cp_parser_friend_p (&decl_specifiers);
19405 /* Keep going until we hit the `;' at the end of the
19407 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19409 tree attributes = NULL_TREE;
19410 tree first_attribute;
19412 /* Peek at the next token. */
19413 token = cp_lexer_peek_token (parser->lexer);
19415 /* Check for a bitfield declaration. */
19416 if (token->type == CPP_COLON
19417 || (token->type == CPP_NAME
19418 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19424 /* Get the name of the bitfield. Note that we cannot just
19425 check TOKEN here because it may have been invalidated by
19426 the call to cp_lexer_peek_nth_token above. */
19427 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19428 identifier = cp_parser_identifier (parser);
19430 identifier = NULL_TREE;
19432 /* Consume the `:' token. */
19433 cp_lexer_consume_token (parser->lexer);
19434 /* Get the width of the bitfield. */
19436 = cp_parser_constant_expression (parser,
19437 /*allow_non_constant=*/false,
19440 /* Look for attributes that apply to the bitfield. */
19441 attributes = cp_parser_attributes_opt (parser);
19442 /* Remember which attributes are prefix attributes and
19444 first_attribute = attributes;
19445 /* Combine the attributes. */
19446 attributes = chainon (prefix_attributes, attributes);
19448 /* Create the bitfield declaration. */
19449 decl = grokbitfield (identifier
19450 ? make_id_declarator (NULL_TREE,
19460 cp_declarator *declarator;
19462 tree asm_specification;
19463 int ctor_dtor_or_conv_p;
19465 /* Parse the declarator. */
19467 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19468 &ctor_dtor_or_conv_p,
19469 /*parenthesized_p=*/NULL,
19470 /*member_p=*/true);
19472 /* If something went wrong parsing the declarator, make sure
19473 that we at least consume some tokens. */
19474 if (declarator == cp_error_declarator)
19476 /* Skip to the end of the statement. */
19477 cp_parser_skip_to_end_of_statement (parser);
19478 /* If the next token is not a semicolon, that is
19479 probably because we just skipped over the body of
19480 a function. So, we consume a semicolon if
19481 present, but do not issue an error message if it
19483 if (cp_lexer_next_token_is (parser->lexer,
19485 cp_lexer_consume_token (parser->lexer);
19489 if (declares_class_or_enum & 2)
19490 cp_parser_check_for_definition_in_return_type
19491 (declarator, decl_specifiers.type,
19492 decl_specifiers.locations[ds_type_spec]);
19494 /* Look for an asm-specification. */
19495 asm_specification = cp_parser_asm_specification_opt (parser);
19496 /* Look for attributes that apply to the declaration. */
19497 attributes = cp_parser_attributes_opt (parser);
19498 /* Remember which attributes are prefix attributes and
19500 first_attribute = attributes;
19501 /* Combine the attributes. */
19502 attributes = chainon (prefix_attributes, attributes);
19504 /* If it's an `=', then we have a constant-initializer or a
19505 pure-specifier. It is not correct to parse the
19506 initializer before registering the member declaration
19507 since the member declaration should be in scope while
19508 its initializer is processed. However, the rest of the
19509 front end does not yet provide an interface that allows
19510 us to handle this correctly. */
19511 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19515 A pure-specifier shall be used only in the declaration of
19516 a virtual function.
19518 A member-declarator can contain a constant-initializer
19519 only if it declares a static member of integral or
19522 Therefore, if the DECLARATOR is for a function, we look
19523 for a pure-specifier; otherwise, we look for a
19524 constant-initializer. When we call `grokfield', it will
19525 perform more stringent semantics checks. */
19526 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19527 if (function_declarator_p (declarator)
19528 || (decl_specifiers.type
19529 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19530 && declarator->kind == cdk_id
19531 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19532 == FUNCTION_TYPE)))
19533 initializer = cp_parser_pure_specifier (parser);
19534 else if (decl_specifiers.storage_class != sc_static)
19535 initializer = cp_parser_save_nsdmi (parser);
19536 else if (cxx_dialect >= cxx0x)
19539 /* Don't require a constant rvalue in C++11, since we
19540 might want a reference constant. We'll enforce
19541 constancy later. */
19542 cp_lexer_consume_token (parser->lexer);
19543 /* Parse the initializer. */
19544 initializer = cp_parser_initializer_clause (parser,
19548 /* Parse the initializer. */
19549 initializer = cp_parser_constant_initializer (parser);
19551 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19552 && !function_declarator_p (declarator))
19555 if (decl_specifiers.storage_class != sc_static)
19556 initializer = cp_parser_save_nsdmi (parser);
19558 initializer = cp_parser_initializer (parser, &x, &x);
19560 /* Otherwise, there is no initializer. */
19562 initializer = NULL_TREE;
19564 /* See if we are probably looking at a function
19565 definition. We are certainly not looking at a
19566 member-declarator. Calling `grokfield' has
19567 side-effects, so we must not do it unless we are sure
19568 that we are looking at a member-declarator. */
19569 if (cp_parser_token_starts_function_definition_p
19570 (cp_lexer_peek_token (parser->lexer)))
19572 /* The grammar does not allow a pure-specifier to be
19573 used when a member function is defined. (It is
19574 possible that this fact is an oversight in the
19575 standard, since a pure function may be defined
19576 outside of the class-specifier. */
19577 if (initializer && initializer_token_start)
19578 error_at (initializer_token_start->location,
19579 "pure-specifier on function-definition");
19580 decl = cp_parser_save_member_function_body (parser,
19584 /* If the member was not a friend, declare it here. */
19586 finish_member_declaration (decl);
19587 /* Peek at the next token. */
19588 token = cp_lexer_peek_token (parser->lexer);
19589 /* If the next token is a semicolon, consume it. */
19590 if (token->type == CPP_SEMICOLON)
19591 cp_lexer_consume_token (parser->lexer);
19595 if (declarator->kind == cdk_function)
19596 declarator->id_loc = token->location;
19597 /* Create the declaration. */
19598 decl = grokfield (declarator, &decl_specifiers,
19599 initializer, /*init_const_expr_p=*/true,
19604 /* Reset PREFIX_ATTRIBUTES. */
19605 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19606 attributes = TREE_CHAIN (attributes);
19608 TREE_CHAIN (attributes) = NULL_TREE;
19610 /* If there is any qualification still in effect, clear it
19611 now; we will be starting fresh with the next declarator. */
19612 parser->scope = NULL_TREE;
19613 parser->qualifying_scope = NULL_TREE;
19614 parser->object_scope = NULL_TREE;
19615 /* If it's a `,', then there are more declarators. */
19616 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19618 cp_lexer_consume_token (parser->lexer);
19619 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19621 cp_token *token = cp_lexer_previous_token (parser->lexer);
19622 error_at (token->location,
19623 "stray %<,%> at end of member declaration");
19626 /* If the next token isn't a `;', then we have a parse error. */
19627 else if (cp_lexer_next_token_is_not (parser->lexer,
19630 /* The next token might be a ways away from where the
19631 actual semicolon is missing. Find the previous token
19632 and use that for our error position. */
19633 cp_token *token = cp_lexer_previous_token (parser->lexer);
19634 error_at (token->location,
19635 "expected %<;%> at end of member declaration");
19637 /* Assume that the user meant to provide a semicolon. If
19638 we were to cp_parser_skip_to_end_of_statement, we might
19639 skip to a semicolon inside a member function definition
19640 and issue nonsensical error messages. */
19641 assume_semicolon = true;
19646 /* Add DECL to the list of members. */
19648 finish_member_declaration (decl);
19650 if (TREE_CODE (decl) == FUNCTION_DECL)
19651 cp_parser_save_default_args (parser, decl);
19652 else if (TREE_CODE (decl) == FIELD_DECL
19653 && !DECL_C_BIT_FIELD (decl)
19654 && DECL_INITIAL (decl))
19655 /* Add DECL to the queue of NSDMI to be parsed later. */
19656 vec_safe_push (unparsed_nsdmis, decl);
19659 if (assume_semicolon)
19664 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19666 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19669 /* Parse a pure-specifier.
19674 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19675 Otherwise, ERROR_MARK_NODE is returned. */
19678 cp_parser_pure_specifier (cp_parser* parser)
19682 /* Look for the `=' token. */
19683 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19684 return error_mark_node;
19685 /* Look for the `0' token. */
19686 token = cp_lexer_peek_token (parser->lexer);
19688 if (token->type == CPP_EOF
19689 || token->type == CPP_PRAGMA_EOL)
19690 return error_mark_node;
19692 cp_lexer_consume_token (parser->lexer);
19694 /* Accept = default or = delete in c++0x mode. */
19695 if (token->keyword == RID_DEFAULT
19696 || token->keyword == RID_DELETE)
19698 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19699 return token->u.value;
19702 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19703 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19705 cp_parser_error (parser,
19706 "invalid pure specifier (only %<= 0%> is allowed)");
19707 cp_parser_skip_to_end_of_statement (parser);
19708 return error_mark_node;
19710 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19712 error_at (token->location, "templates may not be %<virtual%>");
19713 return error_mark_node;
19716 return integer_zero_node;
19719 /* Parse a constant-initializer.
19721 constant-initializer:
19722 = constant-expression
19724 Returns a representation of the constant-expression. */
19727 cp_parser_constant_initializer (cp_parser* parser)
19729 /* Look for the `=' token. */
19730 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19731 return error_mark_node;
19733 /* It is invalid to write:
19735 struct S { static const int i = { 7 }; };
19738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19740 cp_parser_error (parser,
19741 "a brace-enclosed initializer is not allowed here");
19742 /* Consume the opening brace. */
19743 cp_lexer_consume_token (parser->lexer);
19744 /* Skip the initializer. */
19745 cp_parser_skip_to_closing_brace (parser);
19746 /* Look for the trailing `}'. */
19747 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19749 return error_mark_node;
19752 return cp_parser_constant_expression (parser,
19753 /*allow_non_constant=*/false,
19757 /* Derived classes [gram.class.derived] */
19759 /* Parse a base-clause.
19762 : base-specifier-list
19764 base-specifier-list:
19765 base-specifier ... [opt]
19766 base-specifier-list , base-specifier ... [opt]
19768 Returns a TREE_LIST representing the base-classes, in the order in
19769 which they were declared. The representation of each node is as
19770 described by cp_parser_base_specifier.
19772 In the case that no bases are specified, this function will return
19773 NULL_TREE, not ERROR_MARK_NODE. */
19776 cp_parser_base_clause (cp_parser* parser)
19778 tree bases = NULL_TREE;
19780 /* Look for the `:' that begins the list. */
19781 cp_parser_require (parser, CPP_COLON, RT_COLON);
19783 /* Scan the base-specifier-list. */
19788 bool pack_expansion_p = false;
19790 /* Look for the base-specifier. */
19791 base = cp_parser_base_specifier (parser);
19792 /* Look for the (optional) ellipsis. */
19793 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19795 /* Consume the `...'. */
19796 cp_lexer_consume_token (parser->lexer);
19798 pack_expansion_p = true;
19801 /* Add BASE to the front of the list. */
19802 if (base && base != error_mark_node)
19804 if (pack_expansion_p)
19805 /* Make this a pack expansion type. */
19806 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19808 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19810 TREE_CHAIN (base) = bases;
19814 /* Peek at the next token. */
19815 token = cp_lexer_peek_token (parser->lexer);
19816 /* If it's not a comma, then the list is complete. */
19817 if (token->type != CPP_COMMA)
19819 /* Consume the `,'. */
19820 cp_lexer_consume_token (parser->lexer);
19823 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19824 base class had a qualified name. However, the next name that
19825 appears is certainly not qualified. */
19826 parser->scope = NULL_TREE;
19827 parser->qualifying_scope = NULL_TREE;
19828 parser->object_scope = NULL_TREE;
19830 return nreverse (bases);
19833 /* Parse a base-specifier.
19836 :: [opt] nested-name-specifier [opt] class-name
19837 virtual access-specifier [opt] :: [opt] nested-name-specifier
19839 access-specifier virtual [opt] :: [opt] nested-name-specifier
19842 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19843 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19844 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19845 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19848 cp_parser_base_specifier (cp_parser* parser)
19852 bool virtual_p = false;
19853 bool duplicate_virtual_error_issued_p = false;
19854 bool duplicate_access_error_issued_p = false;
19855 bool class_scope_p, template_p;
19856 tree access = access_default_node;
19859 /* Process the optional `virtual' and `access-specifier'. */
19862 /* Peek at the next token. */
19863 token = cp_lexer_peek_token (parser->lexer);
19864 /* Process `virtual'. */
19865 switch (token->keyword)
19868 /* If `virtual' appears more than once, issue an error. */
19869 if (virtual_p && !duplicate_virtual_error_issued_p)
19871 cp_parser_error (parser,
19872 "%<virtual%> specified more than once in base-specified");
19873 duplicate_virtual_error_issued_p = true;
19878 /* Consume the `virtual' token. */
19879 cp_lexer_consume_token (parser->lexer);
19884 case RID_PROTECTED:
19886 /* If more than one access specifier appears, issue an
19888 if (access != access_default_node
19889 && !duplicate_access_error_issued_p)
19891 cp_parser_error (parser,
19892 "more than one access specifier in base-specified");
19893 duplicate_access_error_issued_p = true;
19896 access = ridpointers[(int) token->keyword];
19898 /* Consume the access-specifier. */
19899 cp_lexer_consume_token (parser->lexer);
19908 /* It is not uncommon to see programs mechanically, erroneously, use
19909 the 'typename' keyword to denote (dependent) qualified types
19910 as base classes. */
19911 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19913 token = cp_lexer_peek_token (parser->lexer);
19914 if (!processing_template_decl)
19915 error_at (token->location,
19916 "keyword %<typename%> not allowed outside of templates");
19918 error_at (token->location,
19919 "keyword %<typename%> not allowed in this context "
19920 "(the base class is implicitly a type)");
19921 cp_lexer_consume_token (parser->lexer);
19924 /* Look for the optional `::' operator. */
19925 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19926 /* Look for the nested-name-specifier. The simplest way to
19931 The keyword `typename' is not permitted in a base-specifier or
19932 mem-initializer; in these contexts a qualified name that
19933 depends on a template-parameter is implicitly assumed to be a
19936 is to pretend that we have seen the `typename' keyword at this
19938 cp_parser_nested_name_specifier_opt (parser,
19939 /*typename_keyword_p=*/true,
19940 /*check_dependency_p=*/true,
19942 /*is_declaration=*/true);
19943 /* If the base class is given by a qualified name, assume that names
19944 we see are type names or templates, as appropriate. */
19945 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19946 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19949 && cp_lexer_next_token_is_decltype (parser->lexer))
19950 /* DR 950 allows decltype as a base-specifier. */
19951 type = cp_parser_decltype (parser);
19954 /* Otherwise, look for the class-name. */
19955 type = cp_parser_class_name (parser,
19959 /*check_dependency_p=*/true,
19960 /*class_head_p=*/false,
19961 /*is_declaration=*/true);
19962 type = TREE_TYPE (type);
19965 if (type == error_mark_node)
19966 return error_mark_node;
19968 return finish_base_specifier (type, access, virtual_p);
19971 /* Exception handling [gram.exception] */
19973 /* Parse an (optional) noexcept-specification.
19975 noexcept-specification:
19976 noexcept ( constant-expression ) [opt]
19978 If no noexcept-specification is present, returns NULL_TREE.
19979 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19980 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19981 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19982 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19983 in which case a boolean condition is returned instead. */
19986 cp_parser_noexcept_specification_opt (cp_parser* parser,
19987 bool require_constexpr,
19988 bool* consumed_expr,
19992 const char *saved_message;
19994 /* Peek at the next token. */
19995 token = cp_lexer_peek_token (parser->lexer);
19997 /* Is it a noexcept-specification? */
19998 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20001 cp_lexer_consume_token (parser->lexer);
20003 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20005 cp_lexer_consume_token (parser->lexer);
20007 if (require_constexpr)
20009 /* Types may not be defined in an exception-specification. */
20010 saved_message = parser->type_definition_forbidden_message;
20011 parser->type_definition_forbidden_message
20012 = G_("types may not be defined in an exception-specification");
20014 expr = cp_parser_constant_expression (parser, false, NULL);
20016 /* Restore the saved message. */
20017 parser->type_definition_forbidden_message = saved_message;
20021 expr = cp_parser_expression (parser, false, NULL);
20022 *consumed_expr = true;
20025 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20029 expr = boolean_true_node;
20030 if (!require_constexpr)
20031 *consumed_expr = false;
20034 /* We cannot build a noexcept-spec right away because this will check
20035 that expr is a constexpr. */
20037 return build_noexcept_spec (expr, tf_warning_or_error);
20045 /* Parse an (optional) exception-specification.
20047 exception-specification:
20048 throw ( type-id-list [opt] )
20050 Returns a TREE_LIST representing the exception-specification. The
20051 TREE_VALUE of each node is a type. */
20054 cp_parser_exception_specification_opt (cp_parser* parser)
20058 const char *saved_message;
20060 /* Peek at the next token. */
20061 token = cp_lexer_peek_token (parser->lexer);
20063 /* Is it a noexcept-specification? */
20064 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20066 if (type_id_list != NULL_TREE)
20067 return type_id_list;
20069 /* If it's not `throw', then there's no exception-specification. */
20070 if (!cp_parser_is_keyword (token, RID_THROW))
20074 /* Enable this once a lot of code has transitioned to noexcept? */
20075 if (cxx_dialect >= cxx0x && !in_system_header)
20076 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20077 "deprecated in C++0x; use %<noexcept%> instead");
20080 /* Consume the `throw'. */
20081 cp_lexer_consume_token (parser->lexer);
20083 /* Look for the `('. */
20084 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20086 /* Peek at the next token. */
20087 token = cp_lexer_peek_token (parser->lexer);
20088 /* If it's not a `)', then there is a type-id-list. */
20089 if (token->type != CPP_CLOSE_PAREN)
20091 /* Types may not be defined in an exception-specification. */
20092 saved_message = parser->type_definition_forbidden_message;
20093 parser->type_definition_forbidden_message
20094 = G_("types may not be defined in an exception-specification");
20095 /* Parse the type-id-list. */
20096 type_id_list = cp_parser_type_id_list (parser);
20097 /* Restore the saved message. */
20098 parser->type_definition_forbidden_message = saved_message;
20101 type_id_list = empty_except_spec;
20103 /* Look for the `)'. */
20104 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20106 return type_id_list;
20109 /* Parse an (optional) type-id-list.
20113 type-id-list , type-id ... [opt]
20115 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20116 in the order that the types were presented. */
20119 cp_parser_type_id_list (cp_parser* parser)
20121 tree types = NULL_TREE;
20128 /* Get the next type-id. */
20129 type = cp_parser_type_id (parser);
20130 /* Parse the optional ellipsis. */
20131 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20133 /* Consume the `...'. */
20134 cp_lexer_consume_token (parser->lexer);
20136 /* Turn the type into a pack expansion expression. */
20137 type = make_pack_expansion (type);
20139 /* Add it to the list. */
20140 types = add_exception_specifier (types, type, /*complain=*/1);
20141 /* Peek at the next token. */
20142 token = cp_lexer_peek_token (parser->lexer);
20143 /* If it is not a `,', we are done. */
20144 if (token->type != CPP_COMMA)
20146 /* Consume the `,'. */
20147 cp_lexer_consume_token (parser->lexer);
20150 return nreverse (types);
20153 /* Parse a try-block.
20156 try compound-statement handler-seq */
20159 cp_parser_try_block (cp_parser* parser)
20163 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20164 try_block = begin_try_block ();
20165 cp_parser_compound_statement (parser, NULL, true, false);
20166 finish_try_block (try_block);
20167 cp_parser_handler_seq (parser);
20168 finish_handler_sequence (try_block);
20173 /* Parse a function-try-block.
20175 function-try-block:
20176 try ctor-initializer [opt] function-body handler-seq */
20179 cp_parser_function_try_block (cp_parser* parser)
20181 tree compound_stmt;
20183 bool ctor_initializer_p;
20185 /* Look for the `try' keyword. */
20186 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20188 /* Let the rest of the front end know where we are. */
20189 try_block = begin_function_try_block (&compound_stmt);
20190 /* Parse the function-body. */
20191 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20192 (parser, /*in_function_try_block=*/true);
20193 /* We're done with the `try' part. */
20194 finish_function_try_block (try_block);
20195 /* Parse the handlers. */
20196 cp_parser_handler_seq (parser);
20197 /* We're done with the handlers. */
20198 finish_function_handler_sequence (try_block, compound_stmt);
20200 return ctor_initializer_p;
20203 /* Parse a handler-seq.
20206 handler handler-seq [opt] */
20209 cp_parser_handler_seq (cp_parser* parser)
20215 /* Parse the handler. */
20216 cp_parser_handler (parser);
20217 /* Peek at the next token. */
20218 token = cp_lexer_peek_token (parser->lexer);
20219 /* If it's not `catch' then there are no more handlers. */
20220 if (!cp_parser_is_keyword (token, RID_CATCH))
20225 /* Parse a handler.
20228 catch ( exception-declaration ) compound-statement */
20231 cp_parser_handler (cp_parser* parser)
20236 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20237 handler = begin_handler ();
20238 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20239 declaration = cp_parser_exception_declaration (parser);
20240 finish_handler_parms (declaration, handler);
20241 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20242 cp_parser_compound_statement (parser, NULL, false, false);
20243 finish_handler (handler);
20246 /* Parse an exception-declaration.
20248 exception-declaration:
20249 type-specifier-seq declarator
20250 type-specifier-seq abstract-declarator
20254 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20255 ellipsis variant is used. */
20258 cp_parser_exception_declaration (cp_parser* parser)
20260 cp_decl_specifier_seq type_specifiers;
20261 cp_declarator *declarator;
20262 const char *saved_message;
20264 /* If it's an ellipsis, it's easy to handle. */
20265 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20267 /* Consume the `...' token. */
20268 cp_lexer_consume_token (parser->lexer);
20272 /* Types may not be defined in exception-declarations. */
20273 saved_message = parser->type_definition_forbidden_message;
20274 parser->type_definition_forbidden_message
20275 = G_("types may not be defined in exception-declarations");
20277 /* Parse the type-specifier-seq. */
20278 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20279 /*is_trailing_return=*/false,
20281 /* If it's a `)', then there is no declarator. */
20282 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20285 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20286 /*ctor_dtor_or_conv_p=*/NULL,
20287 /*parenthesized_p=*/NULL,
20288 /*member_p=*/false);
20290 /* Restore the saved message. */
20291 parser->type_definition_forbidden_message = saved_message;
20293 if (!type_specifiers.any_specifiers_p)
20294 return error_mark_node;
20296 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20299 /* Parse a throw-expression.
20302 throw assignment-expression [opt]
20304 Returns a THROW_EXPR representing the throw-expression. */
20307 cp_parser_throw_expression (cp_parser* parser)
20312 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20313 token = cp_lexer_peek_token (parser->lexer);
20314 /* Figure out whether or not there is an assignment-expression
20315 following the "throw" keyword. */
20316 if (token->type == CPP_COMMA
20317 || token->type == CPP_SEMICOLON
20318 || token->type == CPP_CLOSE_PAREN
20319 || token->type == CPP_CLOSE_SQUARE
20320 || token->type == CPP_CLOSE_BRACE
20321 || token->type == CPP_COLON)
20322 expression = NULL_TREE;
20324 expression = cp_parser_assignment_expression (parser,
20325 /*cast_p=*/false, NULL);
20327 return build_throw (expression);
20330 /* GNU Extensions */
20332 /* Parse an (optional) asm-specification.
20335 asm ( string-literal )
20337 If the asm-specification is present, returns a STRING_CST
20338 corresponding to the string-literal. Otherwise, returns
20342 cp_parser_asm_specification_opt (cp_parser* parser)
20345 tree asm_specification;
20347 /* Peek at the next token. */
20348 token = cp_lexer_peek_token (parser->lexer);
20349 /* If the next token isn't the `asm' keyword, then there's no
20350 asm-specification. */
20351 if (!cp_parser_is_keyword (token, RID_ASM))
20354 /* Consume the `asm' token. */
20355 cp_lexer_consume_token (parser->lexer);
20356 /* Look for the `('. */
20357 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20359 /* Look for the string-literal. */
20360 asm_specification = cp_parser_string_literal (parser, false, false);
20362 /* Look for the `)'. */
20363 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20365 return asm_specification;
20368 /* Parse an asm-operand-list.
20372 asm-operand-list , asm-operand
20375 string-literal ( expression )
20376 [ string-literal ] string-literal ( expression )
20378 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20379 each node is the expression. The TREE_PURPOSE is itself a
20380 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20381 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20382 is a STRING_CST for the string literal before the parenthesis. Returns
20383 ERROR_MARK_NODE if any of the operands are invalid. */
20386 cp_parser_asm_operand_list (cp_parser* parser)
20388 tree asm_operands = NULL_TREE;
20389 bool invalid_operands = false;
20393 tree string_literal;
20397 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20399 /* Consume the `[' token. */
20400 cp_lexer_consume_token (parser->lexer);
20401 /* Read the operand name. */
20402 name = cp_parser_identifier (parser);
20403 if (name != error_mark_node)
20404 name = build_string (IDENTIFIER_LENGTH (name),
20405 IDENTIFIER_POINTER (name));
20406 /* Look for the closing `]'. */
20407 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20411 /* Look for the string-literal. */
20412 string_literal = cp_parser_string_literal (parser, false, false);
20414 /* Look for the `('. */
20415 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20416 /* Parse the expression. */
20417 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20418 /* Look for the `)'. */
20419 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20421 if (name == error_mark_node
20422 || string_literal == error_mark_node
20423 || expression == error_mark_node)
20424 invalid_operands = true;
20426 /* Add this operand to the list. */
20427 asm_operands = tree_cons (build_tree_list (name, string_literal),
20430 /* If the next token is not a `,', there are no more
20432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20434 /* Consume the `,'. */
20435 cp_lexer_consume_token (parser->lexer);
20438 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20441 /* Parse an asm-clobber-list.
20445 asm-clobber-list , string-literal
20447 Returns a TREE_LIST, indicating the clobbers in the order that they
20448 appeared. The TREE_VALUE of each node is a STRING_CST. */
20451 cp_parser_asm_clobber_list (cp_parser* parser)
20453 tree clobbers = NULL_TREE;
20457 tree string_literal;
20459 /* Look for the string literal. */
20460 string_literal = cp_parser_string_literal (parser, false, false);
20461 /* Add it to the list. */
20462 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20463 /* If the next token is not a `,', then the list is
20465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20467 /* Consume the `,' token. */
20468 cp_lexer_consume_token (parser->lexer);
20474 /* Parse an asm-label-list.
20478 asm-label-list , identifier
20480 Returns a TREE_LIST, indicating the labels in the order that they
20481 appeared. The TREE_VALUE of each node is a label. */
20484 cp_parser_asm_label_list (cp_parser* parser)
20486 tree labels = NULL_TREE;
20490 tree identifier, label, name;
20492 /* Look for the identifier. */
20493 identifier = cp_parser_identifier (parser);
20494 if (!error_operand_p (identifier))
20496 label = lookup_label (identifier);
20497 if (TREE_CODE (label) == LABEL_DECL)
20499 TREE_USED (label) = 1;
20500 check_goto (label);
20501 name = build_string (IDENTIFIER_LENGTH (identifier),
20502 IDENTIFIER_POINTER (identifier));
20503 labels = tree_cons (name, label, labels);
20506 /* If the next token is not a `,', then the list is
20508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20510 /* Consume the `,' token. */
20511 cp_lexer_consume_token (parser->lexer);
20514 return nreverse (labels);
20517 /* Return TRUE iff the next tokens in the stream are possibly the
20518 beginning of a GNU extension attribute. */
20521 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20523 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20526 /* Return TRUE iff the next tokens in the stream are possibly the
20527 beginning of a standard C++-11 attribute specifier. */
20530 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20532 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20535 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20536 beginning of a standard C++-11 attribute specifier. */
20539 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20541 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20543 return (cxx_dialect >= cxx0x
20544 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20545 || (token->type == CPP_OPEN_SQUARE
20546 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20547 && token->type == CPP_OPEN_SQUARE)));
20550 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20551 beginning of a GNU extension attribute. */
20554 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20556 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20558 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20561 /* Return true iff the next tokens can be the beginning of either a
20562 GNU attribute list, or a standard C++11 attribute sequence. */
20565 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20567 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20568 || cp_next_tokens_can_be_std_attribute_p (parser));
20571 /* Return true iff the next Nth tokens can be the beginning of either
20572 a GNU attribute list, or a standard C++11 attribute sequence. */
20575 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20577 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20578 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20581 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20582 of GNU attributes, or return NULL. */
20585 cp_parser_attributes_opt (cp_parser *parser)
20587 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20588 return cp_parser_gnu_attributes_opt (parser);
20589 return cp_parser_std_attribute_spec_seq (parser);
20592 /* Parse an (optional) series of attributes.
20595 attributes attribute
20598 __attribute__ (( attribute-list [opt] ))
20600 The return value is as for cp_parser_gnu_attribute_list. */
20603 cp_parser_gnu_attributes_opt (cp_parser* parser)
20605 tree attributes = NULL_TREE;
20610 tree attribute_list;
20613 /* Peek at the next token. */
20614 token = cp_lexer_peek_token (parser->lexer);
20615 /* If it's not `__attribute__', then we're done. */
20616 if (token->keyword != RID_ATTRIBUTE)
20619 /* Consume the `__attribute__' keyword. */
20620 cp_lexer_consume_token (parser->lexer);
20621 /* Look for the two `(' tokens. */
20622 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20623 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20625 /* Peek at the next token. */
20626 token = cp_lexer_peek_token (parser->lexer);
20627 if (token->type != CPP_CLOSE_PAREN)
20628 /* Parse the attribute-list. */
20629 attribute_list = cp_parser_gnu_attribute_list (parser);
20631 /* If the next token is a `)', then there is no attribute
20633 attribute_list = NULL;
20635 /* Look for the two `)' tokens. */
20636 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20638 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20641 cp_parser_skip_to_end_of_statement (parser);
20643 /* Add these new attributes to the list. */
20644 attributes = chainon (attributes, attribute_list);
20650 /* Parse a GNU attribute-list.
20654 attribute-list , attribute
20658 identifier ( identifier )
20659 identifier ( identifier , expression-list )
20660 identifier ( expression-list )
20662 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20663 to an attribute. The TREE_PURPOSE of each node is the identifier
20664 indicating which attribute is in use. The TREE_VALUE represents
20665 the arguments, if any. */
20668 cp_parser_gnu_attribute_list (cp_parser* parser)
20670 tree attribute_list = NULL_TREE;
20671 bool save_translate_strings_p = parser->translate_strings_p;
20673 parser->translate_strings_p = false;
20680 /* Look for the identifier. We also allow keywords here; for
20681 example `__attribute__ ((const))' is legal. */
20682 token = cp_lexer_peek_token (parser->lexer);
20683 if (token->type == CPP_NAME
20684 || token->type == CPP_KEYWORD)
20686 tree arguments = NULL_TREE;
20688 /* Consume the token. */
20689 token = cp_lexer_consume_token (parser->lexer);
20691 /* Save away the identifier that indicates which attribute
20693 identifier = (token->type == CPP_KEYWORD)
20694 /* For keywords, use the canonical spelling, not the
20695 parsed identifier. */
20696 ? ridpointers[(int) token->keyword]
20699 attribute = build_tree_list (identifier, NULL_TREE);
20701 /* Peek at the next token. */
20702 token = cp_lexer_peek_token (parser->lexer);
20703 /* If it's an `(', then parse the attribute arguments. */
20704 if (token->type == CPP_OPEN_PAREN)
20706 vec<tree, va_gc> *vec;
20707 int attr_flag = (attribute_takes_identifier_p (identifier)
20708 ? id_attr : normal_attr);
20709 vec = cp_parser_parenthesized_expression_list
20710 (parser, attr_flag, /*cast_p=*/false,
20711 /*allow_expansion_p=*/false,
20712 /*non_constant_p=*/NULL);
20714 arguments = error_mark_node;
20717 arguments = build_tree_list_vec (vec);
20718 release_tree_vector (vec);
20720 /* Save the arguments away. */
20721 TREE_VALUE (attribute) = arguments;
20724 if (arguments != error_mark_node)
20726 /* Add this attribute to the list. */
20727 TREE_CHAIN (attribute) = attribute_list;
20728 attribute_list = attribute;
20731 token = cp_lexer_peek_token (parser->lexer);
20733 /* Now, look for more attributes. If the next token isn't a
20734 `,', we're done. */
20735 if (token->type != CPP_COMMA)
20738 /* Consume the comma and keep going. */
20739 cp_lexer_consume_token (parser->lexer);
20741 parser->translate_strings_p = save_translate_strings_p;
20743 /* We built up the list in reverse order. */
20744 return nreverse (attribute_list);
20747 /* Parse a standard C++11 attribute.
20749 The returned representation is a TREE_LIST which TREE_PURPOSE is
20750 the scoped name of the attribute, and the TREE_VALUE is its
20753 Note that the scoped name of the attribute is itself a TREE_LIST
20754 which TREE_PURPOSE is the namespace of the attribute, and
20755 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20756 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20757 and which TREE_PURPOSE is directly the attribute name.
20759 Clients of the attribute code should use get_attribute_namespace
20760 and get_attribute_name to get the actual namespace and name of
20761 attributes, regardless of their being GNU or C++11 attributes.
20764 attribute-token attribute-argument-clause [opt]
20768 attribute-scoped-token
20770 attribute-scoped-token:
20771 attribute-namespace :: identifier
20773 attribute-namespace:
20776 attribute-argument-clause:
20777 ( balanced-token-seq )
20779 balanced-token-seq:
20780 balanced-token [opt]
20781 balanced-token-seq balanced-token
20784 ( balanced-token-seq )
20785 [ balanced-token-seq ]
20786 { balanced-token-seq }. */
20789 cp_parser_std_attribute (cp_parser *parser)
20791 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20794 /* First, parse name of the the attribute, a.k.a
20795 attribute-token. */
20797 token = cp_lexer_peek_token (parser->lexer);
20798 if (token->type == CPP_NAME)
20799 attr_id = token->u.value;
20800 else if (token->type == CPP_KEYWORD)
20801 attr_id = ridpointers[(int) token->keyword];
20802 else if (token->flags & NAMED_OP)
20803 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20805 if (attr_id == NULL_TREE)
20808 cp_lexer_consume_token (parser->lexer);
20810 token = cp_lexer_peek_token (parser->lexer);
20811 if (token->type == CPP_SCOPE)
20813 /* We are seeing a scoped attribute token. */
20815 cp_lexer_consume_token (parser->lexer);
20818 token = cp_lexer_consume_token (parser->lexer);
20819 if (token->type == CPP_NAME)
20820 attr_id = token->u.value;
20821 else if (token->type == CPP_KEYWORD)
20822 attr_id = ridpointers[(int) token->keyword];
20825 error_at (token->location,
20826 "expected an identifier for the attribute name");
20827 return error_mark_node;
20829 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20831 token = cp_lexer_peek_token (parser->lexer);
20835 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20837 /* C++11 noreturn attribute is equivalent to GNU's. */
20838 if (is_attribute_p ("noreturn", attr_id))
20839 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
20842 /* Now parse the optional argument clause of the attribute. */
20844 if (token->type != CPP_OPEN_PAREN)
20848 vec<tree, va_gc> *vec;
20849 int attr_flag = normal_attr;
20851 if (attr_ns == get_identifier ("gnu")
20852 && attribute_takes_identifier_p (attr_id))
20853 /* A GNU attribute that takes an identifier in parameter. */
20854 attr_flag = id_attr;
20856 vec = cp_parser_parenthesized_expression_list
20857 (parser, attr_flag, /*cast_p=*/false,
20858 /*allow_expansion_p=*/true,
20859 /*non_constant_p=*/NULL);
20861 arguments = error_mark_node;
20864 arguments = build_tree_list_vec (vec);
20865 release_tree_vector (vec);
20868 if (arguments == error_mark_node)
20869 attribute = error_mark_node;
20871 TREE_VALUE (attribute) = arguments;
20877 /* Parse a list of standard C++-11 attributes.
20881 attribute-list , attribute[opt]
20883 attribute-list , attribute ...
20887 cp_parser_std_attribute_list (cp_parser *parser)
20889 tree attributes = NULL_TREE, attribute = NULL_TREE;
20890 cp_token *token = NULL;
20894 attribute = cp_parser_std_attribute (parser);
20895 if (attribute == error_mark_node)
20897 if (attribute != NULL_TREE)
20899 TREE_CHAIN (attribute) = attributes;
20900 attributes = attribute;
20902 token = cp_lexer_peek_token (parser->lexer);
20903 if (token->type != CPP_COMMA)
20905 cp_lexer_consume_token (parser->lexer);
20907 attributes = nreverse (attributes);
20911 /* Parse a standard C++-11 attribute specifier.
20913 attribute-specifier:
20914 [ [ attribute-list ] ]
20915 alignment-specifier
20917 alignment-specifier:
20918 alignas ( type-id ... [opt] )
20919 alignas ( alignment-expression ... [opt] ). */
20922 cp_parser_std_attribute_spec (cp_parser *parser)
20924 tree attributes = NULL_TREE;
20925 cp_token *token = cp_lexer_peek_token (parser->lexer);
20927 if (token->type == CPP_OPEN_SQUARE
20928 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
20930 cp_lexer_consume_token (parser->lexer);
20931 cp_lexer_consume_token (parser->lexer);
20933 attributes = cp_parser_std_attribute_list (parser);
20935 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
20936 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20937 cp_parser_skip_to_end_of_statement (parser);
20939 /* Warn about parsing c++11 attribute in non-c++1 mode, only
20940 when we are sure that we have actually parsed them. */
20941 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20947 /* Look for an alignment-specifier. */
20949 token = cp_lexer_peek_token (parser->lexer);
20951 if (token->type != CPP_KEYWORD
20952 || token->keyword != RID_ALIGNAS)
20955 cp_lexer_consume_token (parser->lexer);
20956 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20958 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
20960 cp_parser_error (parser, "expected %<(%>");
20961 return error_mark_node;
20964 cp_parser_parse_tentatively (parser);
20965 alignas_expr = cp_parser_type_id (parser);
20967 if (!cp_parser_parse_definitely (parser))
20969 gcc_assert (alignas_expr == error_mark_node
20970 || alignas_expr == NULL_TREE);
20973 cp_parser_assignment_expression (parser, /*cast_p=*/false,
20974 /**cp_id_kind=*/NULL);
20975 if (alignas_expr == NULL_TREE
20976 || alignas_expr == error_mark_node)
20977 return alignas_expr;
20980 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
20982 cp_parser_error (parser, "expected %<)%>");
20983 return error_mark_node;
20986 alignas_expr = cxx_alignas_expr (alignas_expr);
20988 /* Build the C++-11 representation of an 'aligned'
20991 build_tree_list (build_tree_list (get_identifier ("gnu"),
20992 get_identifier ("aligned")),
20993 build_tree_list (NULL_TREE, alignas_expr));
20999 /* Parse a standard C++-11 attribute-specifier-seq.
21001 attribute-specifier-seq:
21002 attribute-specifier-seq [opt] attribute-specifier
21006 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21008 tree attr_specs = NULL;
21012 tree attr_spec = cp_parser_std_attribute_spec (parser);
21013 if (attr_spec == NULL_TREE)
21015 if (attr_spec == error_mark_node)
21016 return error_mark_node;
21018 TREE_CHAIN (attr_spec) = attr_specs;
21019 attr_specs = attr_spec;
21022 attr_specs = nreverse (attr_specs);
21026 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21027 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21028 current value of the PEDANTIC flag, regardless of whether or not
21029 the `__extension__' keyword is present. The caller is responsible
21030 for restoring the value of the PEDANTIC flag. */
21033 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21035 /* Save the old value of the PEDANTIC flag. */
21036 *saved_pedantic = pedantic;
21038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21040 /* Consume the `__extension__' token. */
21041 cp_lexer_consume_token (parser->lexer);
21042 /* We're not being pedantic while the `__extension__' keyword is
21052 /* Parse a label declaration.
21055 __label__ label-declarator-seq ;
21057 label-declarator-seq:
21058 identifier , label-declarator-seq
21062 cp_parser_label_declaration (cp_parser* parser)
21064 /* Look for the `__label__' keyword. */
21065 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21071 /* Look for an identifier. */
21072 identifier = cp_parser_identifier (parser);
21073 /* If we failed, stop. */
21074 if (identifier == error_mark_node)
21076 /* Declare it as a label. */
21077 finish_label_decl (identifier);
21078 /* If the next token is a `;', stop. */
21079 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21081 /* Look for the `,' separating the label declarations. */
21082 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21085 /* Look for the final `;'. */
21086 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21089 /* Support Functions */
21091 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21092 NAME should have one of the representations used for an
21093 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21094 is returned. If PARSER->SCOPE is a dependent type, then a
21095 SCOPE_REF is returned.
21097 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21098 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21099 was formed. Abstractly, such entities should not be passed to this
21100 function, because they do not need to be looked up, but it is
21101 simpler to check for this special case here, rather than at the
21104 In cases not explicitly covered above, this function returns a
21105 DECL, OVERLOAD, or baselink representing the result of the lookup.
21106 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21109 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21110 (e.g., "struct") that was used. In that case bindings that do not
21111 refer to types are ignored.
21113 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21116 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21119 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21122 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21123 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21124 NULL_TREE otherwise. */
21127 cp_parser_lookup_name (cp_parser *parser, tree name,
21128 enum tag_types tag_type,
21131 bool check_dependency,
21132 tree *ambiguous_decls,
21133 location_t name_location)
21136 tree object_type = parser->context->object_type;
21138 /* Assume that the lookup will be unambiguous. */
21139 if (ambiguous_decls)
21140 *ambiguous_decls = NULL_TREE;
21142 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21143 no longer valid. Note that if we are parsing tentatively, and
21144 the parse fails, OBJECT_TYPE will be automatically restored. */
21145 parser->context->object_type = NULL_TREE;
21147 if (name == error_mark_node)
21148 return error_mark_node;
21150 /* A template-id has already been resolved; there is no lookup to
21152 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21154 if (BASELINK_P (name))
21156 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21157 == TEMPLATE_ID_EXPR);
21161 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21162 it should already have been checked to make sure that the name
21163 used matches the type being destroyed. */
21164 if (TREE_CODE (name) == BIT_NOT_EXPR)
21168 /* Figure out to which type this destructor applies. */
21170 type = parser->scope;
21171 else if (object_type)
21172 type = object_type;
21174 type = current_class_type;
21175 /* If that's not a class type, there is no destructor. */
21176 if (!type || !CLASS_TYPE_P (type))
21177 return error_mark_node;
21178 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21179 lazily_declare_fn (sfk_destructor, type);
21180 if (!CLASSTYPE_DESTRUCTORS (type))
21181 return error_mark_node;
21182 /* If it was a class type, return the destructor. */
21183 return CLASSTYPE_DESTRUCTORS (type);
21186 /* By this point, the NAME should be an ordinary identifier. If
21187 the id-expression was a qualified name, the qualifying scope is
21188 stored in PARSER->SCOPE at this point. */
21189 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21191 /* Perform the lookup. */
21196 if (parser->scope == error_mark_node)
21197 return error_mark_node;
21199 /* If the SCOPE is dependent, the lookup must be deferred until
21200 the template is instantiated -- unless we are explicitly
21201 looking up names in uninstantiated templates. Even then, we
21202 cannot look up the name if the scope is not a class type; it
21203 might, for example, be a template type parameter. */
21204 dependent_p = (TYPE_P (parser->scope)
21205 && dependent_scope_p (parser->scope));
21206 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21208 /* Defer lookup. */
21209 decl = error_mark_node;
21212 tree pushed_scope = NULL_TREE;
21214 /* If PARSER->SCOPE is a dependent type, then it must be a
21215 class type, and we must not be checking dependencies;
21216 otherwise, we would have processed this lookup above. So
21217 that PARSER->SCOPE is not considered a dependent base by
21218 lookup_member, we must enter the scope here. */
21220 pushed_scope = push_scope (parser->scope);
21222 /* If the PARSER->SCOPE is a template specialization, it
21223 may be instantiated during name lookup. In that case,
21224 errors may be issued. Even if we rollback the current
21225 tentative parse, those errors are valid. */
21226 decl = lookup_qualified_name (parser->scope, name,
21227 tag_type != none_type,
21228 /*complain=*/true);
21230 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21231 lookup result and the nested-name-specifier nominates a class C:
21232 * if the name specified after the nested-name-specifier, when
21233 looked up in C, is the injected-class-name of C (Clause 9), or
21234 * if the name specified after the nested-name-specifier is the
21235 same as the identifier or the simple-template-id's template-
21236 name in the last component of the nested-name-specifier,
21237 the name is instead considered to name the constructor of
21238 class C. [ Note: for example, the constructor is not an
21239 acceptable lookup result in an elaborated-type-specifier so
21240 the constructor would not be used in place of the
21241 injected-class-name. --end note ] Such a constructor name
21242 shall be used only in the declarator-id of a declaration that
21243 names a constructor or in a using-declaration. */
21244 if (tag_type == none_type
21245 && DECL_SELF_REFERENCE_P (decl)
21246 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21247 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21248 tag_type != none_type,
21249 /*complain=*/true);
21251 /* If we have a single function from a using decl, pull it out. */
21252 if (TREE_CODE (decl) == OVERLOAD
21253 && !really_overloaded_fn (decl))
21254 decl = OVL_FUNCTION (decl);
21257 pop_scope (pushed_scope);
21260 /* If the scope is a dependent type and either we deferred lookup or
21261 we did lookup but didn't find the name, rememeber the name. */
21262 if (decl == error_mark_node && TYPE_P (parser->scope)
21263 && dependent_type_p (parser->scope))
21269 /* The resolution to Core Issue 180 says that `struct
21270 A::B' should be considered a type-name, even if `A'
21272 type = make_typename_type (parser->scope, name, tag_type,
21273 /*complain=*/tf_error);
21274 decl = TYPE_NAME (type);
21276 else if (is_template
21277 && (cp_parser_next_token_ends_template_argument_p (parser)
21278 || cp_lexer_next_token_is (parser->lexer,
21280 decl = make_unbound_class_template (parser->scope,
21282 /*complain=*/tf_error);
21284 decl = build_qualified_name (/*type=*/NULL_TREE,
21285 parser->scope, name,
21288 parser->qualifying_scope = parser->scope;
21289 parser->object_scope = NULL_TREE;
21291 else if (object_type)
21293 tree object_decl = NULL_TREE;
21294 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21295 OBJECT_TYPE is not a class. */
21296 if (CLASS_TYPE_P (object_type))
21297 /* If the OBJECT_TYPE is a template specialization, it may
21298 be instantiated during name lookup. In that case, errors
21299 may be issued. Even if we rollback the current tentative
21300 parse, those errors are valid. */
21301 object_decl = lookup_member (object_type,
21304 tag_type != none_type,
21305 tf_warning_or_error);
21306 /* Look it up in the enclosing context, too. */
21307 decl = lookup_name_real (name, tag_type != none_type,
21309 /*block_p=*/true, is_namespace, 0);
21310 parser->object_scope = object_type;
21311 parser->qualifying_scope = NULL_TREE;
21313 decl = object_decl;
21317 decl = lookup_name_real (name, tag_type != none_type,
21319 /*block_p=*/true, is_namespace, 0);
21320 parser->qualifying_scope = NULL_TREE;
21321 parser->object_scope = NULL_TREE;
21324 /* If the lookup failed, let our caller know. */
21325 if (!decl || decl == error_mark_node)
21326 return error_mark_node;
21328 /* Pull out the template from an injected-class-name (or multiple). */
21330 decl = maybe_get_template_decl_from_type_decl (decl);
21332 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21333 if (TREE_CODE (decl) == TREE_LIST)
21335 if (ambiguous_decls)
21336 *ambiguous_decls = decl;
21337 /* The error message we have to print is too complicated for
21338 cp_parser_error, so we incorporate its actions directly. */
21339 if (!cp_parser_simulate_error (parser))
21341 error_at (name_location, "reference to %qD is ambiguous",
21343 print_candidates (decl);
21345 return error_mark_node;
21348 gcc_assert (DECL_P (decl)
21349 || TREE_CODE (decl) == OVERLOAD
21350 || TREE_CODE (decl) == SCOPE_REF
21351 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21352 || BASELINK_P (decl));
21354 /* If we have resolved the name of a member declaration, check to
21355 see if the declaration is accessible. When the name resolves to
21356 set of overloaded functions, accessibility is checked when
21357 overload resolution is done.
21359 During an explicit instantiation, access is not checked at all,
21360 as per [temp.explicit]. */
21362 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21364 maybe_record_typedef_use (decl);
21369 /* Like cp_parser_lookup_name, but for use in the typical case where
21370 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21371 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21374 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21376 return cp_parser_lookup_name (parser, name,
21378 /*is_template=*/false,
21379 /*is_namespace=*/false,
21380 /*check_dependency=*/true,
21381 /*ambiguous_decls=*/NULL,
21385 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21386 the current context, return the TYPE_DECL. If TAG_NAME_P is
21387 true, the DECL indicates the class being defined in a class-head,
21388 or declared in an elaborated-type-specifier.
21390 Otherwise, return DECL. */
21393 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21395 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21396 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21399 template <typename T> struct B;
21402 template <typename T> struct A::B {};
21404 Similarly, in an elaborated-type-specifier:
21406 namespace N { struct X{}; }
21409 template <typename T> friend struct N::X;
21412 However, if the DECL refers to a class type, and we are in
21413 the scope of the class, then the name lookup automatically
21414 finds the TYPE_DECL created by build_self_reference rather
21415 than a TEMPLATE_DECL. For example, in:
21417 template <class T> struct S {
21421 there is no need to handle such case. */
21423 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21424 return DECL_TEMPLATE_RESULT (decl);
21429 /* If too many, or too few, template-parameter lists apply to the
21430 declarator, issue an error message. Returns TRUE if all went well,
21431 and FALSE otherwise. */
21434 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21435 cp_declarator *declarator,
21436 location_t declarator_location)
21438 switch (declarator->kind)
21442 unsigned num_templates = 0;
21443 tree scope = declarator->u.id.qualifying_scope;
21446 num_templates = num_template_headers_for_class (scope);
21447 else if (TREE_CODE (declarator->u.id.unqualified_name)
21448 == TEMPLATE_ID_EXPR)
21449 /* If the DECLARATOR has the form `X<y>' then it uses one
21450 additional level of template parameters. */
21453 return cp_parser_check_template_parameters
21454 (parser, num_templates, declarator_location, declarator);
21460 case cdk_reference:
21462 return (cp_parser_check_declarator_template_parameters
21463 (parser, declarator->declarator, declarator_location));
21469 gcc_unreachable ();
21474 /* NUM_TEMPLATES were used in the current declaration. If that is
21475 invalid, return FALSE and issue an error messages. Otherwise,
21476 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21477 declarator and we can print more accurate diagnostics. */
21480 cp_parser_check_template_parameters (cp_parser* parser,
21481 unsigned num_templates,
21482 location_t location,
21483 cp_declarator *declarator)
21485 /* If there are the same number of template classes and parameter
21486 lists, that's OK. */
21487 if (parser->num_template_parameter_lists == num_templates)
21489 /* If there are more, but only one more, then we are referring to a
21490 member template. That's OK too. */
21491 if (parser->num_template_parameter_lists == num_templates + 1)
21493 /* If there are more template classes than parameter lists, we have
21496 template <class T> void S<T>::R<T>::f (); */
21497 if (parser->num_template_parameter_lists < num_templates)
21499 if (declarator && !current_function_decl)
21500 error_at (location, "specializing member %<%T::%E%> "
21501 "requires %<template<>%> syntax",
21502 declarator->u.id.qualifying_scope,
21503 declarator->u.id.unqualified_name);
21504 else if (declarator)
21505 error_at (location, "invalid declaration of %<%T::%E%>",
21506 declarator->u.id.qualifying_scope,
21507 declarator->u.id.unqualified_name);
21509 error_at (location, "too few template-parameter-lists");
21512 /* Otherwise, there are too many template parameter lists. We have
21515 template <class T> template <class U> void S::f(); */
21516 error_at (location, "too many template-parameter-lists");
21520 /* Parse an optional `::' token indicating that the following name is
21521 from the global namespace. If so, PARSER->SCOPE is set to the
21522 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21523 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21524 Returns the new value of PARSER->SCOPE, if the `::' token is
21525 present, and NULL_TREE otherwise. */
21528 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21532 /* Peek at the next token. */
21533 token = cp_lexer_peek_token (parser->lexer);
21534 /* If we're looking at a `::' token then we're starting from the
21535 global namespace, not our current location. */
21536 if (token->type == CPP_SCOPE)
21538 /* Consume the `::' token. */
21539 cp_lexer_consume_token (parser->lexer);
21540 /* Set the SCOPE so that we know where to start the lookup. */
21541 parser->scope = global_namespace;
21542 parser->qualifying_scope = global_namespace;
21543 parser->object_scope = NULL_TREE;
21545 return parser->scope;
21547 else if (!current_scope_valid_p)
21549 parser->scope = NULL_TREE;
21550 parser->qualifying_scope = NULL_TREE;
21551 parser->object_scope = NULL_TREE;
21557 /* Returns TRUE if the upcoming token sequence is the start of a
21558 constructor declarator. If FRIEND_P is true, the declarator is
21559 preceded by the `friend' specifier. */
21562 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21564 bool constructor_p;
21565 tree nested_name_specifier;
21566 cp_token *next_token;
21568 /* The common case is that this is not a constructor declarator, so
21569 try to avoid doing lots of work if at all possible. It's not
21570 valid declare a constructor at function scope. */
21571 if (parser->in_function_body)
21573 /* And only certain tokens can begin a constructor declarator. */
21574 next_token = cp_lexer_peek_token (parser->lexer);
21575 if (next_token->type != CPP_NAME
21576 && next_token->type != CPP_SCOPE
21577 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21578 && next_token->type != CPP_TEMPLATE_ID)
21581 /* Parse tentatively; we are going to roll back all of the tokens
21583 cp_parser_parse_tentatively (parser);
21584 /* Assume that we are looking at a constructor declarator. */
21585 constructor_p = true;
21587 /* Look for the optional `::' operator. */
21588 cp_parser_global_scope_opt (parser,
21589 /*current_scope_valid_p=*/false);
21590 /* Look for the nested-name-specifier. */
21591 nested_name_specifier
21592 = (cp_parser_nested_name_specifier_opt (parser,
21593 /*typename_keyword_p=*/false,
21594 /*check_dependency_p=*/false,
21596 /*is_declaration=*/false));
21597 /* Outside of a class-specifier, there must be a
21598 nested-name-specifier. */
21599 if (!nested_name_specifier &&
21600 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21602 constructor_p = false;
21603 else if (nested_name_specifier == error_mark_node)
21604 constructor_p = false;
21606 /* If we have a class scope, this is easy; DR 147 says that S::S always
21607 names the constructor, and no other qualified name could. */
21608 if (constructor_p && nested_name_specifier
21609 && CLASS_TYPE_P (nested_name_specifier))
21611 tree id = cp_parser_unqualified_id (parser,
21612 /*template_keyword_p=*/false,
21613 /*check_dependency_p=*/false,
21614 /*declarator_p=*/true,
21615 /*optional_p=*/false);
21616 if (is_overloaded_fn (id))
21617 id = DECL_NAME (get_first_fn (id));
21618 if (!constructor_name_p (id, nested_name_specifier))
21619 constructor_p = false;
21621 /* If we still think that this might be a constructor-declarator,
21622 look for a class-name. */
21623 else if (constructor_p)
21627 template <typename T> struct S {
21631 we must recognize that the nested `S' names a class. */
21633 type_decl = cp_parser_class_name (parser,
21634 /*typename_keyword_p=*/false,
21635 /*template_keyword_p=*/false,
21637 /*check_dependency_p=*/false,
21638 /*class_head_p=*/false,
21639 /*is_declaration=*/false);
21640 /* If there was no class-name, then this is not a constructor. */
21641 constructor_p = !cp_parser_error_occurred (parser);
21643 /* If we're still considering a constructor, we have to see a `(',
21644 to begin the parameter-declaration-clause, followed by either a
21645 `)', an `...', or a decl-specifier. We need to check for a
21646 type-specifier to avoid being fooled into thinking that:
21650 is a constructor. (It is actually a function named `f' that
21651 takes one parameter (of type `int') and returns a value of type
21654 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21655 constructor_p = false;
21658 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21659 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21660 /* A parameter declaration begins with a decl-specifier,
21661 which is either the "attribute" keyword, a storage class
21662 specifier, or (usually) a type-specifier. */
21663 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21666 tree pushed_scope = NULL_TREE;
21667 unsigned saved_num_template_parameter_lists;
21669 /* Names appearing in the type-specifier should be looked up
21670 in the scope of the class. */
21671 if (current_class_type)
21675 type = TREE_TYPE (type_decl);
21676 if (TREE_CODE (type) == TYPENAME_TYPE)
21678 type = resolve_typename_type (type,
21679 /*only_current_p=*/false);
21680 if (TREE_CODE (type) == TYPENAME_TYPE)
21682 cp_parser_abort_tentative_parse (parser);
21686 pushed_scope = push_scope (type);
21689 /* Inside the constructor parameter list, surrounding
21690 template-parameter-lists do not apply. */
21691 saved_num_template_parameter_lists
21692 = parser->num_template_parameter_lists;
21693 parser->num_template_parameter_lists = 0;
21695 /* Look for the type-specifier. */
21696 cp_parser_type_specifier (parser,
21697 CP_PARSER_FLAGS_NONE,
21698 /*decl_specs=*/NULL,
21699 /*is_declarator=*/true,
21700 /*declares_class_or_enum=*/NULL,
21701 /*is_cv_qualifier=*/NULL);
21703 parser->num_template_parameter_lists
21704 = saved_num_template_parameter_lists;
21706 /* Leave the scope of the class. */
21708 pop_scope (pushed_scope);
21710 constructor_p = !cp_parser_error_occurred (parser);
21714 /* We did not really want to consume any tokens. */
21715 cp_parser_abort_tentative_parse (parser);
21717 return constructor_p;
21720 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21721 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21722 they must be performed once we are in the scope of the function.
21724 Returns the function defined. */
21727 cp_parser_function_definition_from_specifiers_and_declarator
21728 (cp_parser* parser,
21729 cp_decl_specifier_seq *decl_specifiers,
21731 const cp_declarator *declarator)
21736 /* Begin the function-definition. */
21737 success_p = start_function (decl_specifiers, declarator, attributes);
21739 /* The things we're about to see are not directly qualified by any
21740 template headers we've seen thus far. */
21741 reset_specialization ();
21743 /* If there were names looked up in the decl-specifier-seq that we
21744 did not check, check them now. We must wait until we are in the
21745 scope of the function to perform the checks, since the function
21746 might be a friend. */
21747 perform_deferred_access_checks (tf_warning_or_error);
21751 /* Skip the entire function. */
21752 cp_parser_skip_to_end_of_block_or_statement (parser);
21753 fn = error_mark_node;
21755 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21757 /* Seen already, skip it. An error message has already been output. */
21758 cp_parser_skip_to_end_of_block_or_statement (parser);
21759 fn = current_function_decl;
21760 current_function_decl = NULL_TREE;
21761 /* If this is a function from a class, pop the nested class. */
21762 if (current_class_name)
21763 pop_nested_class ();
21768 if (DECL_DECLARED_INLINE_P (current_function_decl))
21769 tv = TV_PARSE_INLINE;
21771 tv = TV_PARSE_FUNC;
21773 fn = cp_parser_function_definition_after_declarator (parser,
21774 /*inline_p=*/false);
21781 /* Parse the part of a function-definition that follows the
21782 declarator. INLINE_P is TRUE iff this function is an inline
21783 function defined within a class-specifier.
21785 Returns the function defined. */
21788 cp_parser_function_definition_after_declarator (cp_parser* parser,
21792 bool ctor_initializer_p = false;
21793 bool saved_in_unbraced_linkage_specification_p;
21794 bool saved_in_function_body;
21795 unsigned saved_num_template_parameter_lists;
21798 saved_in_function_body = parser->in_function_body;
21799 parser->in_function_body = true;
21800 /* If the next token is `return', then the code may be trying to
21801 make use of the "named return value" extension that G++ used to
21803 token = cp_lexer_peek_token (parser->lexer);
21804 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21806 /* Consume the `return' keyword. */
21807 cp_lexer_consume_token (parser->lexer);
21808 /* Look for the identifier that indicates what value is to be
21810 cp_parser_identifier (parser);
21811 /* Issue an error message. */
21812 error_at (token->location,
21813 "named return values are no longer supported");
21814 /* Skip tokens until we reach the start of the function body. */
21817 cp_token *token = cp_lexer_peek_token (parser->lexer);
21818 if (token->type == CPP_OPEN_BRACE
21819 || token->type == CPP_EOF
21820 || token->type == CPP_PRAGMA_EOL)
21822 cp_lexer_consume_token (parser->lexer);
21825 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21826 anything declared inside `f'. */
21827 saved_in_unbraced_linkage_specification_p
21828 = parser->in_unbraced_linkage_specification_p;
21829 parser->in_unbraced_linkage_specification_p = false;
21830 /* Inside the function, surrounding template-parameter-lists do not
21832 saved_num_template_parameter_lists
21833 = parser->num_template_parameter_lists;
21834 parser->num_template_parameter_lists = 0;
21836 start_lambda_scope (current_function_decl);
21838 /* If the next token is `try', `__transaction_atomic', or
21839 `__transaction_relaxed`, then we are looking at either function-try-block
21840 or function-transaction-block. Note that all of these include the
21842 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21843 ctor_initializer_p = cp_parser_function_transaction (parser,
21844 RID_TRANSACTION_ATOMIC);
21845 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21846 RID_TRANSACTION_RELAXED))
21847 ctor_initializer_p = cp_parser_function_transaction (parser,
21848 RID_TRANSACTION_RELAXED);
21849 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21850 ctor_initializer_p = cp_parser_function_try_block (parser);
21852 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21853 (parser, /*in_function_try_block=*/false);
21855 finish_lambda_scope ();
21857 /* Finish the function. */
21858 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21859 (inline_p ? 2 : 0));
21860 /* Generate code for it, if necessary. */
21861 expand_or_defer_fn (fn);
21862 /* Restore the saved values. */
21863 parser->in_unbraced_linkage_specification_p
21864 = saved_in_unbraced_linkage_specification_p;
21865 parser->num_template_parameter_lists
21866 = saved_num_template_parameter_lists;
21867 parser->in_function_body = saved_in_function_body;
21872 /* Parse a template-declaration, assuming that the `export' (and
21873 `extern') keywords, if present, has already been scanned. MEMBER_P
21874 is as for cp_parser_template_declaration. */
21877 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21879 tree decl = NULL_TREE;
21880 vec<deferred_access_check, va_gc> *checks;
21881 tree parameter_list;
21882 bool friend_p = false;
21883 bool need_lang_pop;
21886 /* Look for the `template' keyword. */
21887 token = cp_lexer_peek_token (parser->lexer);
21888 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21892 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21894 if (at_class_scope_p () && current_function_decl)
21896 /* 14.5.2.2 [temp.mem]
21898 A local class shall not have member templates. */
21899 error_at (token->location,
21900 "invalid declaration of member template in local class");
21901 cp_parser_skip_to_end_of_block_or_statement (parser);
21906 A template ... shall not have C linkage. */
21907 if (current_lang_name == lang_name_c)
21909 error_at (token->location, "template with C linkage");
21910 /* Give it C++ linkage to avoid confusing other parts of the
21912 push_lang_context (lang_name_cplusplus);
21913 need_lang_pop = true;
21916 need_lang_pop = false;
21918 /* We cannot perform access checks on the template parameter
21919 declarations until we know what is being declared, just as we
21920 cannot check the decl-specifier list. */
21921 push_deferring_access_checks (dk_deferred);
21923 /* If the next token is `>', then we have an invalid
21924 specialization. Rather than complain about an invalid template
21925 parameter, issue an error message here. */
21926 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21928 cp_parser_error (parser, "invalid explicit specialization");
21929 begin_specialization ();
21930 parameter_list = NULL_TREE;
21934 /* Parse the template parameters. */
21935 parameter_list = cp_parser_template_parameter_list (parser);
21938 /* Get the deferred access checks from the parameter list. These
21939 will be checked once we know what is being declared, as for a
21940 member template the checks must be performed in the scope of the
21941 class containing the member. */
21942 checks = get_deferred_access_checks ();
21944 /* Look for the `>'. */
21945 cp_parser_skip_to_end_of_template_parameter_list (parser);
21946 /* We just processed one more parameter list. */
21947 ++parser->num_template_parameter_lists;
21948 /* If the next token is `template', there are more template
21950 if (cp_lexer_next_token_is_keyword (parser->lexer,
21952 cp_parser_template_declaration_after_export (parser, member_p);
21953 else if (cxx_dialect >= cxx0x
21954 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21955 decl = cp_parser_alias_declaration (parser);
21958 /* There are no access checks when parsing a template, as we do not
21959 know if a specialization will be a friend. */
21960 push_deferring_access_checks (dk_no_check);
21961 token = cp_lexer_peek_token (parser->lexer);
21962 decl = cp_parser_single_declaration (parser,
21965 /*explicit_specialization_p=*/false,
21967 pop_deferring_access_checks ();
21969 /* If this is a member template declaration, let the front
21971 if (member_p && !friend_p && decl)
21973 if (TREE_CODE (decl) == TYPE_DECL)
21974 cp_parser_check_access_in_redeclaration (decl, token->location);
21976 decl = finish_member_template_decl (decl);
21978 else if (friend_p && decl
21979 && (TREE_CODE (decl) == TYPE_DECL
21980 || DECL_TYPE_TEMPLATE_P (decl)))
21981 make_friend_class (current_class_type, TREE_TYPE (decl),
21982 /*complain=*/true);
21984 /* We are done with the current parameter list. */
21985 --parser->num_template_parameter_lists;
21987 pop_deferring_access_checks ();
21990 finish_template_decl (parameter_list);
21992 /* Check the template arguments for a literal operator template. */
21994 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21995 && UDLIT_OPER_P (DECL_NAME (decl)))
21998 if (parameter_list == NULL_TREE)
22002 int num_parms = TREE_VEC_LENGTH (parameter_list);
22003 if (num_parms != 1)
22007 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22008 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22009 if (TREE_TYPE (parm) != char_type_node
22010 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22015 error ("literal operator template %qD has invalid parameter list."
22016 " Expected non-type template argument pack <char...>",
22019 /* Register member declarations. */
22020 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22021 finish_member_declaration (decl);
22022 /* For the erroneous case of a template with C linkage, we pushed an
22023 implicit C++ linkage scope; exit that scope now. */
22025 pop_lang_context ();
22026 /* If DECL is a function template, we must return to parse it later.
22027 (Even though there is no definition, there might be default
22028 arguments that need handling.) */
22029 if (member_p && decl
22030 && (TREE_CODE (decl) == FUNCTION_DECL
22031 || DECL_FUNCTION_TEMPLATE_P (decl)))
22032 vec_safe_push (unparsed_funs_with_definitions, decl);
22035 /* Perform the deferred access checks from a template-parameter-list.
22036 CHECKS is a TREE_LIST of access checks, as returned by
22037 get_deferred_access_checks. */
22040 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22042 ++processing_template_parmlist;
22043 perform_access_checks (checks, tf_warning_or_error);
22044 --processing_template_parmlist;
22047 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22048 `function-definition' sequence that follows a template header.
22049 If MEMBER_P is true, this declaration appears in a class scope.
22051 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22052 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22055 cp_parser_single_declaration (cp_parser* parser,
22056 vec<deferred_access_check, va_gc> *checks,
22058 bool explicit_specialization_p,
22061 int declares_class_or_enum;
22062 tree decl = NULL_TREE;
22063 cp_decl_specifier_seq decl_specifiers;
22064 bool function_definition_p = false;
22065 cp_token *decl_spec_token_start;
22067 /* This function is only used when processing a template
22069 gcc_assert (innermost_scope_kind () == sk_template_parms
22070 || innermost_scope_kind () == sk_template_spec);
22072 /* Defer access checks until we know what is being declared. */
22073 push_deferring_access_checks (dk_deferred);
22075 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22077 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22078 cp_parser_decl_specifier_seq (parser,
22079 CP_PARSER_FLAGS_OPTIONAL,
22081 &declares_class_or_enum);
22083 *friend_p = cp_parser_friend_p (&decl_specifiers);
22085 /* There are no template typedefs. */
22086 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22088 error_at (decl_spec_token_start->location,
22089 "template declaration of %<typedef%>");
22090 decl = error_mark_node;
22093 /* Gather up the access checks that occurred the
22094 decl-specifier-seq. */
22095 stop_deferring_access_checks ();
22097 /* Check for the declaration of a template class. */
22098 if (declares_class_or_enum)
22100 if (cp_parser_declares_only_class_p (parser))
22102 decl = shadow_tag (&decl_specifiers);
22107 friend template <typename T> struct A<T>::B;
22110 A<T>::B will be represented by a TYPENAME_TYPE, and
22111 therefore not recognized by shadow_tag. */
22112 if (friend_p && *friend_p
22114 && decl_specifiers.type
22115 && TYPE_P (decl_specifiers.type))
22116 decl = decl_specifiers.type;
22118 if (decl && decl != error_mark_node)
22119 decl = TYPE_NAME (decl);
22121 decl = error_mark_node;
22123 /* Perform access checks for template parameters. */
22124 cp_parser_perform_template_parameter_access_checks (checks);
22128 /* Complain about missing 'typename' or other invalid type names. */
22129 if (!decl_specifiers.any_type_specifiers_p
22130 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22132 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22133 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22134 the rest of this declaration. */
22135 decl = error_mark_node;
22139 /* If it's not a template class, try for a template function. If
22140 the next token is a `;', then this declaration does not declare
22141 anything. But, if there were errors in the decl-specifiers, then
22142 the error might well have come from an attempted class-specifier.
22143 In that case, there's no need to warn about a missing declarator. */
22145 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22146 || decl_specifiers.type != error_mark_node))
22148 decl = cp_parser_init_declarator (parser,
22151 /*function_definition_allowed_p=*/true,
22153 declares_class_or_enum,
22154 &function_definition_p,
22157 /* 7.1.1-1 [dcl.stc]
22159 A storage-class-specifier shall not be specified in an explicit
22160 specialization... */
22162 && explicit_specialization_p
22163 && decl_specifiers.storage_class != sc_none)
22165 error_at (decl_spec_token_start->location,
22166 "explicit template specialization cannot have a storage class");
22167 decl = error_mark_node;
22170 if (decl && TREE_CODE (decl) == VAR_DECL)
22171 check_template_variable (decl);
22174 /* Look for a trailing `;' after the declaration. */
22175 if (!function_definition_p
22176 && (decl == error_mark_node
22177 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22178 cp_parser_skip_to_end_of_block_or_statement (parser);
22181 pop_deferring_access_checks ();
22183 /* Clear any current qualification; whatever comes next is the start
22184 of something new. */
22185 parser->scope = NULL_TREE;
22186 parser->qualifying_scope = NULL_TREE;
22187 parser->object_scope = NULL_TREE;
22192 /* Parse a cast-expression that is not the operand of a unary "&". */
22195 cp_parser_simple_cast_expression (cp_parser *parser)
22197 return cp_parser_cast_expression (parser, /*address_p=*/false,
22198 /*cast_p=*/false, /*decltype*/false, NULL);
22201 /* Parse a functional cast to TYPE. Returns an expression
22202 representing the cast. */
22205 cp_parser_functional_cast (cp_parser* parser, tree type)
22207 vec<tree, va_gc> *vec;
22208 tree expression_list;
22212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22214 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22215 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22216 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22217 if (TREE_CODE (type) == TYPE_DECL)
22218 type = TREE_TYPE (type);
22219 return finish_compound_literal (type, expression_list,
22220 tf_warning_or_error);
22224 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22226 /*allow_expansion_p=*/true,
22227 /*non_constant_p=*/NULL);
22229 expression_list = error_mark_node;
22232 expression_list = build_tree_list_vec (vec);
22233 release_tree_vector (vec);
22236 cast = build_functional_cast (type, expression_list,
22237 tf_warning_or_error);
22238 /* [expr.const]/1: In an integral constant expression "only type
22239 conversions to integral or enumeration type can be used". */
22240 if (TREE_CODE (type) == TYPE_DECL)
22241 type = TREE_TYPE (type);
22242 if (cast != error_mark_node
22243 && !cast_valid_in_integral_constant_expression_p (type)
22244 && cp_parser_non_integral_constant_expression (parser,
22246 return error_mark_node;
22250 /* Save the tokens that make up the body of a member function defined
22251 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22252 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22253 specifiers applied to the declaration. Returns the FUNCTION_DECL
22254 for the member function. */
22257 cp_parser_save_member_function_body (cp_parser* parser,
22258 cp_decl_specifier_seq *decl_specifiers,
22259 cp_declarator *declarator,
22266 /* Create the FUNCTION_DECL. */
22267 fn = grokmethod (decl_specifiers, declarator, attributes);
22268 /* If something went badly wrong, bail out now. */
22269 if (fn == error_mark_node)
22271 /* If there's a function-body, skip it. */
22272 if (cp_parser_token_starts_function_definition_p
22273 (cp_lexer_peek_token (parser->lexer)))
22274 cp_parser_skip_to_end_of_block_or_statement (parser);
22275 return error_mark_node;
22278 /* Remember it, if there default args to post process. */
22279 cp_parser_save_default_args (parser, fn);
22281 /* Save away the tokens that make up the body of the
22283 first = parser->lexer->next_token;
22284 /* We can have braced-init-list mem-initializers before the fn body. */
22285 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22287 cp_lexer_consume_token (parser->lexer);
22288 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22289 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22291 /* cache_group will stop after an un-nested { } pair, too. */
22292 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22295 /* variadic mem-inits have ... after the ')'. */
22296 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22297 cp_lexer_consume_token (parser->lexer);
22300 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22301 /* Handle function try blocks. */
22302 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22303 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22304 last = parser->lexer->next_token;
22306 /* Save away the inline definition; we will process it when the
22307 class is complete. */
22308 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22309 DECL_PENDING_INLINE_P (fn) = 1;
22311 /* We need to know that this was defined in the class, so that
22312 friend templates are handled correctly. */
22313 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22315 /* Add FN to the queue of functions to be parsed later. */
22316 vec_safe_push (unparsed_funs_with_definitions, fn);
22321 /* Save the tokens that make up the in-class initializer for a non-static
22322 data member. Returns a DEFAULT_ARG. */
22325 cp_parser_save_nsdmi (cp_parser* parser)
22327 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22330 /* Parse a template-argument-list, as well as the trailing ">" (but
22331 not the opening "<"). See cp_parser_template_argument_list for the
22335 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22339 tree saved_qualifying_scope;
22340 tree saved_object_scope;
22341 bool saved_greater_than_is_operator_p;
22342 int saved_unevaluated_operand;
22343 int saved_inhibit_evaluation_warnings;
22347 When parsing a template-id, the first non-nested `>' is taken as
22348 the end of the template-argument-list rather than a greater-than
22350 saved_greater_than_is_operator_p
22351 = parser->greater_than_is_operator_p;
22352 parser->greater_than_is_operator_p = false;
22353 /* Parsing the argument list may modify SCOPE, so we save it
22355 saved_scope = parser->scope;
22356 saved_qualifying_scope = parser->qualifying_scope;
22357 saved_object_scope = parser->object_scope;
22358 /* We need to evaluate the template arguments, even though this
22359 template-id may be nested within a "sizeof". */
22360 saved_unevaluated_operand = cp_unevaluated_operand;
22361 cp_unevaluated_operand = 0;
22362 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22363 c_inhibit_evaluation_warnings = 0;
22364 /* Parse the template-argument-list itself. */
22365 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22366 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22367 arguments = NULL_TREE;
22369 arguments = cp_parser_template_argument_list (parser);
22370 /* Look for the `>' that ends the template-argument-list. If we find
22371 a '>>' instead, it's probably just a typo. */
22372 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22374 if (cxx_dialect != cxx98)
22376 /* In C++0x, a `>>' in a template argument list or cast
22377 expression is considered to be two separate `>'
22378 tokens. So, change the current token to a `>', but don't
22379 consume it: it will be consumed later when the outer
22380 template argument list (or cast expression) is parsed.
22381 Note that this replacement of `>' for `>>' is necessary
22382 even if we are parsing tentatively: in the tentative
22383 case, after calling
22384 cp_parser_enclosed_template_argument_list we will always
22385 throw away all of the template arguments and the first
22386 closing `>', either because the template argument list
22387 was erroneous or because we are replacing those tokens
22388 with a CPP_TEMPLATE_ID token. The second `>' (which will
22389 not have been thrown away) is needed either to close an
22390 outer template argument list or to complete a new-style
22392 cp_token *token = cp_lexer_peek_token (parser->lexer);
22393 token->type = CPP_GREATER;
22395 else if (!saved_greater_than_is_operator_p)
22397 /* If we're in a nested template argument list, the '>>' has
22398 to be a typo for '> >'. We emit the error message, but we
22399 continue parsing and we push a '>' as next token, so that
22400 the argument list will be parsed correctly. Note that the
22401 global source location is still on the token before the
22402 '>>', so we need to say explicitly where we want it. */
22403 cp_token *token = cp_lexer_peek_token (parser->lexer);
22404 error_at (token->location, "%<>>%> should be %<> >%> "
22405 "within a nested template argument list");
22407 token->type = CPP_GREATER;
22411 /* If this is not a nested template argument list, the '>>'
22412 is a typo for '>'. Emit an error message and continue.
22413 Same deal about the token location, but here we can get it
22414 right by consuming the '>>' before issuing the diagnostic. */
22415 cp_token *token = cp_lexer_consume_token (parser->lexer);
22416 error_at (token->location,
22417 "spurious %<>>%>, use %<>%> to terminate "
22418 "a template argument list");
22422 cp_parser_skip_to_end_of_template_parameter_list (parser);
22423 /* The `>' token might be a greater-than operator again now. */
22424 parser->greater_than_is_operator_p
22425 = saved_greater_than_is_operator_p;
22426 /* Restore the SAVED_SCOPE. */
22427 parser->scope = saved_scope;
22428 parser->qualifying_scope = saved_qualifying_scope;
22429 parser->object_scope = saved_object_scope;
22430 cp_unevaluated_operand = saved_unevaluated_operand;
22431 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22436 /* MEMBER_FUNCTION is a member function, or a friend. If default
22437 arguments, or the body of the function have not yet been parsed,
22441 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22443 timevar_push (TV_PARSE_INMETH);
22444 /* If this member is a template, get the underlying
22446 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22447 member_function = DECL_TEMPLATE_RESULT (member_function);
22449 /* There should not be any class definitions in progress at this
22450 point; the bodies of members are only parsed outside of all class
22452 gcc_assert (parser->num_classes_being_defined == 0);
22453 /* While we're parsing the member functions we might encounter more
22454 classes. We want to handle them right away, but we don't want
22455 them getting mixed up with functions that are currently in the
22457 push_unparsed_function_queues (parser);
22459 /* Make sure that any template parameters are in scope. */
22460 maybe_begin_member_template_processing (member_function);
22462 /* If the body of the function has not yet been parsed, parse it
22464 if (DECL_PENDING_INLINE_P (member_function))
22466 tree function_scope;
22467 cp_token_cache *tokens;
22469 /* The function is no longer pending; we are processing it. */
22470 tokens = DECL_PENDING_INLINE_INFO (member_function);
22471 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22472 DECL_PENDING_INLINE_P (member_function) = 0;
22474 /* If this is a local class, enter the scope of the containing
22476 function_scope = current_function_decl;
22477 if (function_scope)
22478 push_function_context ();
22480 /* Push the body of the function onto the lexer stack. */
22481 cp_parser_push_lexer_for_tokens (parser, tokens);
22483 /* Let the front end know that we going to be defining this
22485 start_preparsed_function (member_function, NULL_TREE,
22486 SF_PRE_PARSED | SF_INCLASS_INLINE);
22488 /* Don't do access checking if it is a templated function. */
22489 if (processing_template_decl)
22490 push_deferring_access_checks (dk_no_check);
22492 /* Now, parse the body of the function. */
22493 cp_parser_function_definition_after_declarator (parser,
22494 /*inline_p=*/true);
22496 if (processing_template_decl)
22497 pop_deferring_access_checks ();
22499 /* Leave the scope of the containing function. */
22500 if (function_scope)
22501 pop_function_context ();
22502 cp_parser_pop_lexer (parser);
22505 /* Remove any template parameters from the symbol table. */
22506 maybe_end_member_template_processing ();
22508 /* Restore the queue. */
22509 pop_unparsed_function_queues (parser);
22510 timevar_pop (TV_PARSE_INMETH);
22513 /* If DECL contains any default args, remember it on the unparsed
22514 functions queue. */
22517 cp_parser_save_default_args (cp_parser* parser, tree decl)
22521 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22523 probe = TREE_CHAIN (probe))
22524 if (TREE_PURPOSE (probe))
22526 cp_default_arg_entry entry = {current_class_type, decl};
22527 vec_safe_push (unparsed_funs_with_default_args, entry);
22532 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22533 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22534 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22535 from the parameter-type-list. */
22538 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22539 tree default_arg, tree parmtype)
22541 cp_token_cache *tokens;
22545 if (default_arg == error_mark_node)
22546 return error_mark_node;
22548 /* Push the saved tokens for the default argument onto the parser's
22550 tokens = DEFARG_TOKENS (default_arg);
22551 cp_parser_push_lexer_for_tokens (parser, tokens);
22553 start_lambda_scope (decl);
22555 /* Parse the default argument. */
22556 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22557 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22558 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22560 finish_lambda_scope ();
22562 if (parsed_arg == error_mark_node)
22563 cp_parser_skip_to_end_of_statement (parser);
22565 if (!processing_template_decl)
22567 /* In a non-template class, check conversions now. In a template,
22568 we'll wait and instantiate these as needed. */
22569 if (TREE_CODE (decl) == PARM_DECL)
22570 parsed_arg = check_default_argument (parmtype, parsed_arg,
22571 tf_warning_or_error);
22574 int flags = LOOKUP_IMPLICIT;
22575 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22576 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22577 flags = LOOKUP_NORMAL;
22578 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22582 /* If the token stream has not been completely used up, then
22583 there was extra junk after the end of the default
22585 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22587 if (TREE_CODE (decl) == PARM_DECL)
22588 cp_parser_error (parser, "expected %<,%>");
22590 cp_parser_error (parser, "expected %<;%>");
22593 /* Revert to the main lexer. */
22594 cp_parser_pop_lexer (parser);
22599 /* FIELD is a non-static data member with an initializer which we saved for
22600 later; parse it now. */
22603 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22607 push_unparsed_function_queues (parser);
22608 def = cp_parser_late_parse_one_default_arg (parser, field,
22609 DECL_INITIAL (field),
22611 pop_unparsed_function_queues (parser);
22613 DECL_INITIAL (field) = def;
22616 /* FN is a FUNCTION_DECL which may contains a parameter with an
22617 unparsed DEFAULT_ARG. Parse the default args now. This function
22618 assumes that the current scope is the scope in which the default
22619 argument should be processed. */
22622 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22624 bool saved_local_variables_forbidden_p;
22625 tree parm, parmdecl;
22627 /* While we're parsing the default args, we might (due to the
22628 statement expression extension) encounter more classes. We want
22629 to handle them right away, but we don't want them getting mixed
22630 up with default args that are currently in the queue. */
22631 push_unparsed_function_queues (parser);
22633 /* Local variable names (and the `this' keyword) may not appear
22634 in a default argument. */
22635 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22636 parser->local_variables_forbidden_p = true;
22638 push_defarg_context (fn);
22640 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22641 parmdecl = DECL_ARGUMENTS (fn);
22642 parm && parm != void_list_node;
22643 parm = TREE_CHAIN (parm),
22644 parmdecl = DECL_CHAIN (parmdecl))
22646 tree default_arg = TREE_PURPOSE (parm);
22648 vec<tree, va_gc> *insts;
22655 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22656 /* This can happen for a friend declaration for a function
22657 already declared with default arguments. */
22661 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22663 TREE_VALUE (parm));
22664 if (parsed_arg == error_mark_node)
22669 TREE_PURPOSE (parm) = parsed_arg;
22671 /* Update any instantiations we've already created. */
22672 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22673 vec_safe_iterate (insts, ix, ©); ix++)
22674 TREE_PURPOSE (copy) = parsed_arg;
22677 pop_defarg_context ();
22679 /* Make sure no default arg is missing. */
22680 check_default_args (fn);
22682 /* Restore the state of local_variables_forbidden_p. */
22683 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22685 /* Restore the queue. */
22686 pop_unparsed_function_queues (parser);
22689 /* Parse the operand of `sizeof' (or a similar operator). Returns
22690 either a TYPE or an expression, depending on the form of the
22691 input. The KEYWORD indicates which kind of expression we have
22695 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22697 tree expr = NULL_TREE;
22698 const char *saved_message;
22700 bool saved_integral_constant_expression_p;
22701 bool saved_non_integral_constant_expression_p;
22702 bool pack_expansion_p = false;
22704 /* Types cannot be defined in a `sizeof' expression. Save away the
22706 saved_message = parser->type_definition_forbidden_message;
22707 /* And create the new one. */
22708 tmp = concat ("types may not be defined in %<",
22709 IDENTIFIER_POINTER (ridpointers[keyword]),
22710 "%> expressions", NULL);
22711 parser->type_definition_forbidden_message = tmp;
22713 /* The restrictions on constant-expressions do not apply inside
22714 sizeof expressions. */
22715 saved_integral_constant_expression_p
22716 = parser->integral_constant_expression_p;
22717 saved_non_integral_constant_expression_p
22718 = parser->non_integral_constant_expression_p;
22719 parser->integral_constant_expression_p = false;
22721 /* If it's a `...', then we are computing the length of a parameter
22723 if (keyword == RID_SIZEOF
22724 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22726 /* Consume the `...'. */
22727 cp_lexer_consume_token (parser->lexer);
22728 maybe_warn_variadic_templates ();
22730 /* Note that this is an expansion. */
22731 pack_expansion_p = true;
22734 /* Do not actually evaluate the expression. */
22735 ++cp_unevaluated_operand;
22736 ++c_inhibit_evaluation_warnings;
22737 /* If it's a `(', then we might be looking at the type-id
22739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22742 bool saved_in_type_id_in_expr_p;
22744 /* We can't be sure yet whether we're looking at a type-id or an
22746 cp_parser_parse_tentatively (parser);
22747 /* Consume the `('. */
22748 cp_lexer_consume_token (parser->lexer);
22749 /* Parse the type-id. */
22750 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22751 parser->in_type_id_in_expr_p = true;
22752 type = cp_parser_type_id (parser);
22753 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22754 /* Now, look for the trailing `)'. */
22755 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22756 /* If all went well, then we're done. */
22757 if (cp_parser_parse_definitely (parser))
22759 cp_decl_specifier_seq decl_specs;
22761 /* Build a trivial decl-specifier-seq. */
22762 clear_decl_specs (&decl_specs);
22763 decl_specs.type = type;
22765 /* Call grokdeclarator to figure out what type this is. */
22766 expr = grokdeclarator (NULL,
22770 /*attrlist=*/NULL);
22773 else if (pack_expansion_p)
22774 permerror (cp_lexer_peek_token (parser->lexer)->location,
22775 "%<sizeof...%> argument must be surrounded by parentheses");
22777 /* If the type-id production did not work out, then we must be
22778 looking at the unary-expression production. */
22780 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22781 /*cast_p=*/false, NULL);
22783 if (pack_expansion_p)
22784 /* Build a pack expansion. */
22785 expr = make_pack_expansion (expr);
22787 /* Go back to evaluating expressions. */
22788 --cp_unevaluated_operand;
22789 --c_inhibit_evaluation_warnings;
22791 /* Free the message we created. */
22793 /* And restore the old one. */
22794 parser->type_definition_forbidden_message = saved_message;
22795 parser->integral_constant_expression_p
22796 = saved_integral_constant_expression_p;
22797 parser->non_integral_constant_expression_p
22798 = saved_non_integral_constant_expression_p;
22803 /* If the current declaration has no declarator, return true. */
22806 cp_parser_declares_only_class_p (cp_parser *parser)
22808 /* If the next token is a `;' or a `,' then there is no
22810 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22811 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22814 /* Update the DECL_SPECS to reflect the storage class indicated by
22818 cp_parser_set_storage_class (cp_parser *parser,
22819 cp_decl_specifier_seq *decl_specs,
22823 cp_storage_class storage_class;
22825 if (parser->in_unbraced_linkage_specification_p)
22827 error_at (token->location, "invalid use of %qD in linkage specification",
22828 ridpointers[keyword]);
22831 else if (decl_specs->storage_class != sc_none)
22833 decl_specs->conflicting_specifiers_p = true;
22837 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22838 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22839 && decl_specs->gnu_thread_keyword_p)
22841 pedwarn (decl_specs->locations[ds_thread], 0,
22842 "%<__thread%> before %qD", ridpointers[keyword]);
22848 storage_class = sc_auto;
22851 storage_class = sc_register;
22854 storage_class = sc_static;
22857 storage_class = sc_extern;
22860 storage_class = sc_mutable;
22863 gcc_unreachable ();
22865 decl_specs->storage_class = storage_class;
22866 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22868 /* A storage class specifier cannot be applied alongside a typedef
22869 specifier. If there is a typedef specifier present then set
22870 conflicting_specifiers_p which will trigger an error later
22871 on in grokdeclarator. */
22872 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22873 decl_specs->conflicting_specifiers_p = true;
22876 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22877 is true, the type is a class or enum definition. */
22880 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22883 bool type_definition_p)
22885 decl_specs->any_specifiers_p = true;
22887 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22888 (with, for example, in "typedef int wchar_t;") we remember that
22889 this is what happened. In system headers, we ignore these
22890 declarations so that G++ can work with system headers that are not
22892 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22893 && !type_definition_p
22894 && (type_spec == boolean_type_node
22895 || type_spec == char16_type_node
22896 || type_spec == char32_type_node
22897 || type_spec == wchar_type_node)
22898 && (decl_specs->type
22899 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22900 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22901 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22902 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22904 decl_specs->redefined_builtin_type = type_spec;
22905 set_and_check_decl_spec_loc (decl_specs,
22906 ds_redefined_builtin_type_spec,
22908 if (!decl_specs->type)
22910 decl_specs->type = type_spec;
22911 decl_specs->type_definition_p = false;
22912 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
22915 else if (decl_specs->type)
22916 decl_specs->multiple_types_p = true;
22919 decl_specs->type = type_spec;
22920 decl_specs->type_definition_p = type_definition_p;
22921 decl_specs->redefined_builtin_type = NULL_TREE;
22922 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
22926 /* True iff TOKEN is the GNU keyword __thread. */
22929 token_is__thread (cp_token *token)
22931 gcc_assert (token->keyword == RID_THREAD);
22932 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
22935 /* Set the location for a declarator specifier and check if it is
22938 DECL_SPECS is the sequence of declarator specifiers onto which to
22941 DS is the single declarator specifier to set which location is to
22942 be set onto the existing sequence of declarators.
22944 LOCATION is the location for the declarator specifier to
22948 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22949 cp_decl_spec ds, cp_token *token)
22951 gcc_assert (ds < ds_last);
22953 if (decl_specs == NULL)
22956 source_location location = token->location;
22958 if (decl_specs->locations[ds] == 0)
22960 decl_specs->locations[ds] = location;
22961 if (ds == ds_thread)
22962 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
22968 if (decl_specs->locations[ds_long_long] != 0)
22969 error_at (location,
22970 "%<long long long%> is too long for GCC");
22973 decl_specs->locations[ds_long_long] = location;
22974 pedwarn_cxx98 (location,
22976 "ISO C++ 1998 does not support %<long long%>");
22979 else if (ds == ds_thread)
22981 bool gnu = token_is__thread (token);
22982 if (gnu != decl_specs->gnu_thread_keyword_p)
22983 error_at (location,
22984 "both %<__thread%> and %<thread_local%> specified");
22986 error_at (location, "duplicate %qD", token->u.value);
22990 static const char *const decl_spec_names[] = {
23007 error_at (location,
23008 "duplicate %qs", decl_spec_names[ds]);
23013 /* Return true iff the declarator specifier DS is present in the
23014 sequence of declarator specifiers DECL_SPECS. */
23017 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23020 gcc_assert (ds < ds_last);
23022 if (decl_specs == NULL)
23025 return decl_specs->locations[ds] != 0;
23028 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23029 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23032 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23034 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23037 /* Issue an error message indicating that TOKEN_DESC was expected.
23038 If KEYWORD is true, it indicated this function is called by
23039 cp_parser_require_keword and the required token can only be
23040 a indicated keyword. */
23043 cp_parser_required_error (cp_parser *parser,
23044 required_token token_desc,
23047 switch (token_desc)
23050 cp_parser_error (parser, "expected %<new%>");
23053 cp_parser_error (parser, "expected %<delete%>");
23056 cp_parser_error (parser, "expected %<return%>");
23059 cp_parser_error (parser, "expected %<while%>");
23062 cp_parser_error (parser, "expected %<extern%>");
23064 case RT_STATIC_ASSERT:
23065 cp_parser_error (parser, "expected %<static_assert%>");
23068 cp_parser_error (parser, "expected %<decltype%>");
23071 cp_parser_error (parser, "expected %<operator%>");
23074 cp_parser_error (parser, "expected %<class%>");
23077 cp_parser_error (parser, "expected %<template%>");
23080 cp_parser_error (parser, "expected %<namespace%>");
23083 cp_parser_error (parser, "expected %<using%>");
23086 cp_parser_error (parser, "expected %<asm%>");
23089 cp_parser_error (parser, "expected %<try%>");
23092 cp_parser_error (parser, "expected %<catch%>");
23095 cp_parser_error (parser, "expected %<throw%>");
23098 cp_parser_error (parser, "expected %<__label__%>");
23101 cp_parser_error (parser, "expected %<@try%>");
23103 case RT_AT_SYNCHRONIZED:
23104 cp_parser_error (parser, "expected %<@synchronized%>");
23107 cp_parser_error (parser, "expected %<@throw%>");
23109 case RT_TRANSACTION_ATOMIC:
23110 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23112 case RT_TRANSACTION_RELAXED:
23113 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23120 switch (token_desc)
23123 cp_parser_error (parser, "expected %<;%>");
23125 case RT_OPEN_PAREN:
23126 cp_parser_error (parser, "expected %<(%>");
23128 case RT_CLOSE_BRACE:
23129 cp_parser_error (parser, "expected %<}%>");
23131 case RT_OPEN_BRACE:
23132 cp_parser_error (parser, "expected %<{%>");
23134 case RT_CLOSE_SQUARE:
23135 cp_parser_error (parser, "expected %<]%>");
23137 case RT_OPEN_SQUARE:
23138 cp_parser_error (parser, "expected %<[%>");
23141 cp_parser_error (parser, "expected %<,%>");
23144 cp_parser_error (parser, "expected %<::%>");
23147 cp_parser_error (parser, "expected %<<%>");
23150 cp_parser_error (parser, "expected %<>%>");
23153 cp_parser_error (parser, "expected %<=%>");
23156 cp_parser_error (parser, "expected %<...%>");
23159 cp_parser_error (parser, "expected %<*%>");
23162 cp_parser_error (parser, "expected %<~%>");
23165 cp_parser_error (parser, "expected %<:%>");
23167 case RT_COLON_SCOPE:
23168 cp_parser_error (parser, "expected %<:%> or %<::%>");
23170 case RT_CLOSE_PAREN:
23171 cp_parser_error (parser, "expected %<)%>");
23173 case RT_COMMA_CLOSE_PAREN:
23174 cp_parser_error (parser, "expected %<,%> or %<)%>");
23176 case RT_PRAGMA_EOL:
23177 cp_parser_error (parser, "expected end of line");
23180 cp_parser_error (parser, "expected identifier");
23183 cp_parser_error (parser, "expected selection-statement");
23185 case RT_INTERATION:
23186 cp_parser_error (parser, "expected iteration-statement");
23189 cp_parser_error (parser, "expected jump-statement");
23192 cp_parser_error (parser, "expected class-key");
23194 case RT_CLASS_TYPENAME_TEMPLATE:
23195 cp_parser_error (parser,
23196 "expected %<class%>, %<typename%>, or %<template%>");
23199 gcc_unreachable ();
23203 gcc_unreachable ();
23208 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23209 issue an error message indicating that TOKEN_DESC was expected.
23211 Returns the token consumed, if the token had the appropriate type.
23212 Otherwise, returns NULL. */
23215 cp_parser_require (cp_parser* parser,
23216 enum cpp_ttype type,
23217 required_token token_desc)
23219 if (cp_lexer_next_token_is (parser->lexer, type))
23220 return cp_lexer_consume_token (parser->lexer);
23223 /* Output the MESSAGE -- unless we're parsing tentatively. */
23224 if (!cp_parser_simulate_error (parser))
23225 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23230 /* An error message is produced if the next token is not '>'.
23231 All further tokens are skipped until the desired token is
23232 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23235 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23237 /* Current level of '< ... >'. */
23238 unsigned level = 0;
23239 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23240 unsigned nesting_depth = 0;
23242 /* Are we ready, yet? If not, issue error message. */
23243 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23246 /* Skip tokens until the desired token is found. */
23249 /* Peek at the next token. */
23250 switch (cp_lexer_peek_token (parser->lexer)->type)
23253 if (!nesting_depth)
23258 if (cxx_dialect == cxx98)
23259 /* C++0x views the `>>' operator as two `>' tokens, but
23262 else if (!nesting_depth && level-- == 0)
23264 /* We've hit a `>>' where the first `>' closes the
23265 template argument list, and the second `>' is
23266 spurious. Just consume the `>>' and stop; we've
23267 already produced at least one error. */
23268 cp_lexer_consume_token (parser->lexer);
23271 /* Fall through for C++0x, so we handle the second `>' in
23275 if (!nesting_depth && level-- == 0)
23277 /* We've reached the token we want, consume it and stop. */
23278 cp_lexer_consume_token (parser->lexer);
23283 case CPP_OPEN_PAREN:
23284 case CPP_OPEN_SQUARE:
23288 case CPP_CLOSE_PAREN:
23289 case CPP_CLOSE_SQUARE:
23290 if (nesting_depth-- == 0)
23295 case CPP_PRAGMA_EOL:
23296 case CPP_SEMICOLON:
23297 case CPP_OPEN_BRACE:
23298 case CPP_CLOSE_BRACE:
23299 /* The '>' was probably forgotten, don't look further. */
23306 /* Consume this token. */
23307 cp_lexer_consume_token (parser->lexer);
23311 /* If the next token is the indicated keyword, consume it. Otherwise,
23312 issue an error message indicating that TOKEN_DESC was expected.
23314 Returns the token consumed, if the token had the appropriate type.
23315 Otherwise, returns NULL. */
23318 cp_parser_require_keyword (cp_parser* parser,
23320 required_token token_desc)
23322 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23324 if (token && token->keyword != keyword)
23326 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23333 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23334 function-definition. */
23337 cp_parser_token_starts_function_definition_p (cp_token* token)
23339 return (/* An ordinary function-body begins with an `{'. */
23340 token->type == CPP_OPEN_BRACE
23341 /* A ctor-initializer begins with a `:'. */
23342 || token->type == CPP_COLON
23343 /* A function-try-block begins with `try'. */
23344 || token->keyword == RID_TRY
23345 /* A function-transaction-block begins with `__transaction_atomic'
23346 or `__transaction_relaxed'. */
23347 || token->keyword == RID_TRANSACTION_ATOMIC
23348 || token->keyword == RID_TRANSACTION_RELAXED
23349 /* The named return value extension begins with `return'. */
23350 || token->keyword == RID_RETURN);
23353 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23357 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23361 token = cp_lexer_peek_token (parser->lexer);
23362 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23365 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23366 C++0x) ending a template-argument. */
23369 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23373 token = cp_lexer_peek_token (parser->lexer);
23374 return (token->type == CPP_COMMA
23375 || token->type == CPP_GREATER
23376 || token->type == CPP_ELLIPSIS
23377 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23380 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23381 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23384 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23389 token = cp_lexer_peek_nth_token (parser->lexer, n);
23390 if (token->type == CPP_LESS)
23392 /* Check for the sequence `<::' in the original code. It would be lexed as
23393 `[:', where `[' is a digraph, and there is no whitespace before
23395 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23398 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23399 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23405 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23406 or none_type otherwise. */
23408 static enum tag_types
23409 cp_parser_token_is_class_key (cp_token* token)
23411 switch (token->keyword)
23416 return record_type;
23425 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23428 cp_parser_check_class_key (enum tag_types class_key, tree type)
23430 if (type == error_mark_node)
23432 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23434 permerror (input_location, "%qs tag used in naming %q#T",
23435 class_key == union_type ? "union"
23436 : class_key == record_type ? "struct" : "class",
23438 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23439 "%q#T was previously declared here", type);
23443 /* Issue an error message if DECL is redeclared with different
23444 access than its original declaration [class.access.spec/3].
23445 This applies to nested classes and nested class templates.
23449 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23451 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23454 if ((TREE_PRIVATE (decl)
23455 != (current_access_specifier == access_private_node))
23456 || (TREE_PROTECTED (decl)
23457 != (current_access_specifier == access_protected_node)))
23458 error_at (location, "%qD redeclared with different access", decl);
23461 /* Look for the `template' keyword, as a syntactic disambiguator.
23462 Return TRUE iff it is present, in which case it will be
23466 cp_parser_optional_template_keyword (cp_parser *parser)
23468 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23470 /* In C++98 the `template' keyword can only be used within templates;
23471 outside templates the parser can always figure out what is a
23472 template and what is not. In C++11, per the resolution of DR 468,
23473 `template' is allowed in cases where it is not strictly necessary. */
23474 if (!processing_template_decl
23475 && pedantic && cxx_dialect == cxx98)
23477 cp_token *token = cp_lexer_peek_token (parser->lexer);
23478 pedwarn (token->location, OPT_Wpedantic,
23479 "in C++98 %<template%> (as a disambiguator) is only "
23480 "allowed within templates");
23481 /* If this part of the token stream is rescanned, the same
23482 error message would be generated. So, we purge the token
23483 from the stream. */
23484 cp_lexer_purge_token (parser->lexer);
23489 /* Consume the `template' keyword. */
23490 cp_lexer_consume_token (parser->lexer);
23497 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23498 set PARSER->SCOPE, and perform other related actions. */
23501 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23504 struct tree_check *check_value;
23505 deferred_access_check *chk;
23506 vec<deferred_access_check, va_gc> *checks;
23508 /* Get the stored value. */
23509 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23510 /* Perform any access checks that were deferred. */
23511 checks = check_value->checks;
23514 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23515 perform_or_defer_access_check (chk->binfo,
23517 chk->diag_decl, tf_warning_or_error);
23519 /* Set the scope from the stored value. */
23520 parser->scope = check_value->value;
23521 parser->qualifying_scope = check_value->qualifying_scope;
23522 parser->object_scope = NULL_TREE;
23525 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23526 encounter the end of a block before what we were looking for. */
23529 cp_parser_cache_group (cp_parser *parser,
23530 enum cpp_ttype end,
23535 cp_token *token = cp_lexer_peek_token (parser->lexer);
23537 /* Abort a parenthesized expression if we encounter a semicolon. */
23538 if ((end == CPP_CLOSE_PAREN || depth == 0)
23539 && token->type == CPP_SEMICOLON)
23541 /* If we've reached the end of the file, stop. */
23542 if (token->type == CPP_EOF
23543 || (end != CPP_PRAGMA_EOL
23544 && token->type == CPP_PRAGMA_EOL))
23546 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23547 /* We've hit the end of an enclosing block, so there's been some
23548 kind of syntax error. */
23551 /* Consume the token. */
23552 cp_lexer_consume_token (parser->lexer);
23553 /* See if it starts a new group. */
23554 if (token->type == CPP_OPEN_BRACE)
23556 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23557 /* In theory this should probably check end == '}', but
23558 cp_parser_save_member_function_body needs it to exit
23559 after either '}' or ')' when called with ')'. */
23563 else if (token->type == CPP_OPEN_PAREN)
23565 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23566 if (depth == 0 && end == CPP_CLOSE_PAREN)
23569 else if (token->type == CPP_PRAGMA)
23570 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23571 else if (token->type == end)
23576 /* Like above, for caching a default argument or NSDMI. Both of these are
23577 terminated by a non-nested comma, but it can be unclear whether or not a
23578 comma is nested in a template argument list unless we do more parsing.
23579 In order to handle this ambiguity, when we encounter a ',' after a '<'
23580 we try to parse what follows as a parameter-declaration-list (in the
23581 case of a default argument) or a member-declarator (in the case of an
23582 NSDMI). If that succeeds, then we stop caching. */
23585 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23587 unsigned depth = 0;
23588 int maybe_template_id = 0;
23589 cp_token *first_token;
23591 tree default_argument;
23593 /* Add tokens until we have processed the entire default
23594 argument. We add the range [first_token, token). */
23595 first_token = cp_lexer_peek_token (parser->lexer);
23596 if (first_token->type == CPP_OPEN_BRACE)
23598 /* For list-initialization, this is straightforward. */
23599 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23600 token = cp_lexer_peek_token (parser->lexer);
23606 /* Peek at the next token. */
23607 token = cp_lexer_peek_token (parser->lexer);
23608 /* What we do depends on what token we have. */
23609 switch (token->type)
23611 /* In valid code, a default argument must be
23612 immediately followed by a `,' `)', or `...'. */
23614 if (depth == 0 && maybe_template_id)
23616 /* If we've seen a '<', we might be in a
23617 template-argument-list. Until Core issue 325 is
23618 resolved, we don't know how this situation ought
23619 to be handled, so try to DTRT. We check whether
23620 what comes after the comma is a valid parameter
23621 declaration list. If it is, then the comma ends
23622 the default argument; otherwise the default
23623 argument continues. */
23624 bool error = false;
23627 /* Set ITALP so cp_parser_parameter_declaration_list
23628 doesn't decide to commit to this parse. */
23629 bool saved_italp = parser->in_template_argument_list_p;
23630 parser->in_template_argument_list_p = true;
23632 cp_parser_parse_tentatively (parser);
23633 cp_lexer_consume_token (parser->lexer);
23637 int ctor_dtor_or_conv_p;
23638 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23639 &ctor_dtor_or_conv_p,
23640 /*parenthesized_p=*/NULL,
23641 /*member_p=*/true);
23645 begin_scope (sk_function_parms, NULL_TREE);
23646 cp_parser_parameter_declaration_list (parser, &error);
23647 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23648 pop_binding (DECL_NAME (t), t);
23651 if (!cp_parser_error_occurred (parser) && !error)
23653 cp_parser_abort_tentative_parse (parser);
23655 parser->in_template_argument_list_p = saved_italp;
23658 case CPP_CLOSE_PAREN:
23660 /* If we run into a non-nested `;', `}', or `]',
23661 then the code is invalid -- but the default
23662 argument is certainly over. */
23663 case CPP_SEMICOLON:
23664 case CPP_CLOSE_BRACE:
23665 case CPP_CLOSE_SQUARE:
23668 /* Update DEPTH, if necessary. */
23669 else if (token->type == CPP_CLOSE_PAREN
23670 || token->type == CPP_CLOSE_BRACE
23671 || token->type == CPP_CLOSE_SQUARE)
23675 case CPP_OPEN_PAREN:
23676 case CPP_OPEN_SQUARE:
23677 case CPP_OPEN_BRACE:
23683 /* This might be the comparison operator, or it might
23684 start a template argument list. */
23685 ++maybe_template_id;
23689 if (cxx_dialect == cxx98)
23691 /* Fall through for C++0x, which treats the `>>'
23692 operator like two `>' tokens in certain
23698 /* This might be an operator, or it might close a
23699 template argument list. But if a previous '<'
23700 started a template argument list, this will have
23701 closed it, so we can't be in one anymore. */
23702 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23703 if (maybe_template_id < 0)
23704 maybe_template_id = 0;
23708 /* If we run out of tokens, issue an error message. */
23710 case CPP_PRAGMA_EOL:
23711 error_at (token->location, "file ends in default argument");
23717 /* In these cases, we should look for template-ids.
23718 For example, if the default argument is
23719 `X<int, double>()', we need to do name lookup to
23720 figure out whether or not `X' is a template; if
23721 so, the `,' does not end the default argument.
23723 That is not yet done. */
23730 /* If we've reached the end, stop. */
23734 /* Add the token to the token block. */
23735 token = cp_lexer_consume_token (parser->lexer);
23738 /* Create a DEFAULT_ARG to represent the unparsed default
23740 default_argument = make_node (DEFAULT_ARG);
23741 DEFARG_TOKENS (default_argument)
23742 = cp_token_cache_new (first_token, token);
23743 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23745 return default_argument;
23748 /* Begin parsing tentatively. We always save tokens while parsing
23749 tentatively so that if the tentative parsing fails we can restore the
23753 cp_parser_parse_tentatively (cp_parser* parser)
23755 /* Enter a new parsing context. */
23756 parser->context = cp_parser_context_new (parser->context);
23757 /* Begin saving tokens. */
23758 cp_lexer_save_tokens (parser->lexer);
23759 /* In order to avoid repetitive access control error messages,
23760 access checks are queued up until we are no longer parsing
23762 push_deferring_access_checks (dk_deferred);
23765 /* Commit to the currently active tentative parse. */
23768 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23770 cp_parser_context *context;
23773 /* Mark all of the levels as committed. */
23774 lexer = parser->lexer;
23775 for (context = parser->context; context->next; context = context->next)
23777 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23779 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23780 while (!cp_lexer_saving_tokens (lexer))
23781 lexer = lexer->next;
23782 cp_lexer_commit_tokens (lexer);
23786 /* Abort the currently active tentative parse. All consumed tokens
23787 will be rolled back, and no diagnostics will be issued. */
23790 cp_parser_abort_tentative_parse (cp_parser* parser)
23792 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23793 || errorcount > 0);
23794 cp_parser_simulate_error (parser);
23795 /* Now, pretend that we want to see if the construct was
23796 successfully parsed. */
23797 cp_parser_parse_definitely (parser);
23800 /* Stop parsing tentatively. If a parse error has occurred, restore the
23801 token stream. Otherwise, commit to the tokens we have consumed.
23802 Returns true if no error occurred; false otherwise. */
23805 cp_parser_parse_definitely (cp_parser* parser)
23807 bool error_occurred;
23808 cp_parser_context *context;
23810 /* Remember whether or not an error occurred, since we are about to
23811 destroy that information. */
23812 error_occurred = cp_parser_error_occurred (parser);
23813 /* Remove the topmost context from the stack. */
23814 context = parser->context;
23815 parser->context = context->next;
23816 /* If no parse errors occurred, commit to the tentative parse. */
23817 if (!error_occurred)
23819 /* Commit to the tokens read tentatively, unless that was
23821 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23822 cp_lexer_commit_tokens (parser->lexer);
23824 pop_to_parent_deferring_access_checks ();
23826 /* Otherwise, if errors occurred, roll back our state so that things
23827 are just as they were before we began the tentative parse. */
23830 cp_lexer_rollback_tokens (parser->lexer);
23831 pop_deferring_access_checks ();
23833 /* Add the context to the front of the free list. */
23834 context->next = cp_parser_context_free_list;
23835 cp_parser_context_free_list = context;
23837 return !error_occurred;
23840 /* Returns true if we are parsing tentatively and are not committed to
23841 this tentative parse. */
23844 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23846 return (cp_parser_parsing_tentatively (parser)
23847 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23850 /* Returns nonzero iff an error has occurred during the most recent
23851 tentative parse. */
23854 cp_parser_error_occurred (cp_parser* parser)
23856 return (cp_parser_parsing_tentatively (parser)
23857 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23860 /* Returns nonzero if GNU extensions are allowed. */
23863 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23865 return parser->allow_gnu_extensions_p;
23868 /* Objective-C++ Productions */
23871 /* Parse an Objective-C expression, which feeds into a primary-expression
23875 objc-message-expression
23876 objc-string-literal
23877 objc-encode-expression
23878 objc-protocol-expression
23879 objc-selector-expression
23881 Returns a tree representation of the expression. */
23884 cp_parser_objc_expression (cp_parser* parser)
23886 /* Try to figure out what kind of declaration is present. */
23887 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23891 case CPP_OPEN_SQUARE:
23892 return cp_parser_objc_message_expression (parser);
23894 case CPP_OBJC_STRING:
23895 kwd = cp_lexer_consume_token (parser->lexer);
23896 return objc_build_string_object (kwd->u.value);
23899 switch (kwd->keyword)
23901 case RID_AT_ENCODE:
23902 return cp_parser_objc_encode_expression (parser);
23904 case RID_AT_PROTOCOL:
23905 return cp_parser_objc_protocol_expression (parser);
23907 case RID_AT_SELECTOR:
23908 return cp_parser_objc_selector_expression (parser);
23914 error_at (kwd->location,
23915 "misplaced %<@%D%> Objective-C++ construct",
23917 cp_parser_skip_to_end_of_block_or_statement (parser);
23920 return error_mark_node;
23923 /* Parse an Objective-C message expression.
23925 objc-message-expression:
23926 [ objc-message-receiver objc-message-args ]
23928 Returns a representation of an Objective-C message. */
23931 cp_parser_objc_message_expression (cp_parser* parser)
23933 tree receiver, messageargs;
23935 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23936 receiver = cp_parser_objc_message_receiver (parser);
23937 messageargs = cp_parser_objc_message_args (parser);
23938 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23940 return objc_build_message_expr (receiver, messageargs);
23943 /* Parse an objc-message-receiver.
23945 objc-message-receiver:
23947 simple-type-specifier
23949 Returns a representation of the type or expression. */
23952 cp_parser_objc_message_receiver (cp_parser* parser)
23956 /* An Objective-C message receiver may be either (1) a type
23957 or (2) an expression. */
23958 cp_parser_parse_tentatively (parser);
23959 rcv = cp_parser_expression (parser, false, NULL);
23961 if (cp_parser_parse_definitely (parser))
23964 rcv = cp_parser_simple_type_specifier (parser,
23965 /*decl_specs=*/NULL,
23966 CP_PARSER_FLAGS_NONE);
23968 return objc_get_class_reference (rcv);
23971 /* Parse the arguments and selectors comprising an Objective-C message.
23976 objc-selector-args , objc-comma-args
23978 objc-selector-args:
23979 objc-selector [opt] : assignment-expression
23980 objc-selector-args objc-selector [opt] : assignment-expression
23983 assignment-expression
23984 objc-comma-args , assignment-expression
23986 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23987 selector arguments and TREE_VALUE containing a list of comma
23991 cp_parser_objc_message_args (cp_parser* parser)
23993 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23994 bool maybe_unary_selector_p = true;
23995 cp_token *token = cp_lexer_peek_token (parser->lexer);
23997 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23999 tree selector = NULL_TREE, arg;
24001 if (token->type != CPP_COLON)
24002 selector = cp_parser_objc_selector (parser);
24004 /* Detect if we have a unary selector. */
24005 if (maybe_unary_selector_p
24006 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24007 return build_tree_list (selector, NULL_TREE);
24009 maybe_unary_selector_p = false;
24010 cp_parser_require (parser, CPP_COLON, RT_COLON);
24011 arg = cp_parser_assignment_expression (parser, false, NULL);
24014 = chainon (sel_args,
24015 build_tree_list (selector, arg));
24017 token = cp_lexer_peek_token (parser->lexer);
24020 /* Handle non-selector arguments, if any. */
24021 while (token->type == CPP_COMMA)
24025 cp_lexer_consume_token (parser->lexer);
24026 arg = cp_parser_assignment_expression (parser, false, NULL);
24029 = chainon (addl_args,
24030 build_tree_list (NULL_TREE, arg));
24032 token = cp_lexer_peek_token (parser->lexer);
24035 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24037 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24038 return build_tree_list (error_mark_node, error_mark_node);
24041 return build_tree_list (sel_args, addl_args);
24044 /* Parse an Objective-C encode expression.
24046 objc-encode-expression:
24047 @encode objc-typename
24049 Returns an encoded representation of the type argument. */
24052 cp_parser_objc_encode_expression (cp_parser* parser)
24057 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24058 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24059 token = cp_lexer_peek_token (parser->lexer);
24060 type = complete_type (cp_parser_type_id (parser));
24061 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24065 error_at (token->location,
24066 "%<@encode%> must specify a type as an argument");
24067 return error_mark_node;
24070 /* This happens if we find @encode(T) (where T is a template
24071 typename or something dependent on a template typename) when
24072 parsing a template. In that case, we can't compile it
24073 immediately, but we rather create an AT_ENCODE_EXPR which will
24074 need to be instantiated when the template is used.
24076 if (dependent_type_p (type))
24078 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24079 TREE_READONLY (value) = 1;
24083 return objc_build_encode_expr (type);
24086 /* Parse an Objective-C @defs expression. */
24089 cp_parser_objc_defs_expression (cp_parser *parser)
24093 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24094 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24095 name = cp_parser_identifier (parser);
24096 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24098 return objc_get_class_ivars (name);
24101 /* Parse an Objective-C protocol expression.
24103 objc-protocol-expression:
24104 @protocol ( identifier )
24106 Returns a representation of the protocol expression. */
24109 cp_parser_objc_protocol_expression (cp_parser* parser)
24113 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24114 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24115 proto = cp_parser_identifier (parser);
24116 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24118 return objc_build_protocol_expr (proto);
24121 /* Parse an Objective-C selector expression.
24123 objc-selector-expression:
24124 @selector ( objc-method-signature )
24126 objc-method-signature:
24132 objc-selector-seq objc-selector :
24134 Returns a representation of the method selector. */
24137 cp_parser_objc_selector_expression (cp_parser* parser)
24139 tree sel_seq = NULL_TREE;
24140 bool maybe_unary_selector_p = true;
24142 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24144 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
24145 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24146 token = cp_lexer_peek_token (parser->lexer);
24148 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24149 || token->type == CPP_SCOPE)
24151 tree selector = NULL_TREE;
24153 if (token->type != CPP_COLON
24154 || token->type == CPP_SCOPE)
24155 selector = cp_parser_objc_selector (parser);
24157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24158 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24160 /* Detect if we have a unary selector. */
24161 if (maybe_unary_selector_p)
24163 sel_seq = selector;
24164 goto finish_selector;
24168 cp_parser_error (parser, "expected %<:%>");
24171 maybe_unary_selector_p = false;
24172 token = cp_lexer_consume_token (parser->lexer);
24174 if (token->type == CPP_SCOPE)
24177 = chainon (sel_seq,
24178 build_tree_list (selector, NULL_TREE));
24180 = chainon (sel_seq,
24181 build_tree_list (NULL_TREE, NULL_TREE));
24185 = chainon (sel_seq,
24186 build_tree_list (selector, NULL_TREE));
24188 token = cp_lexer_peek_token (parser->lexer);
24192 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24194 return objc_build_selector_expr (loc, sel_seq);
24197 /* Parse a list of identifiers.
24199 objc-identifier-list:
24201 objc-identifier-list , identifier
24203 Returns a TREE_LIST of identifier nodes. */
24206 cp_parser_objc_identifier_list (cp_parser* parser)
24212 identifier = cp_parser_identifier (parser);
24213 if (identifier == error_mark_node)
24214 return error_mark_node;
24216 list = build_tree_list (NULL_TREE, identifier);
24217 sep = cp_lexer_peek_token (parser->lexer);
24219 while (sep->type == CPP_COMMA)
24221 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24222 identifier = cp_parser_identifier (parser);
24223 if (identifier == error_mark_node)
24226 list = chainon (list, build_tree_list (NULL_TREE,
24228 sep = cp_lexer_peek_token (parser->lexer);
24234 /* Parse an Objective-C alias declaration.
24236 objc-alias-declaration:
24237 @compatibility_alias identifier identifier ;
24239 This function registers the alias mapping with the Objective-C front end.
24240 It returns nothing. */
24243 cp_parser_objc_alias_declaration (cp_parser* parser)
24247 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24248 alias = cp_parser_identifier (parser);
24249 orig = cp_parser_identifier (parser);
24250 objc_declare_alias (alias, orig);
24251 cp_parser_consume_semicolon_at_end_of_statement (parser);
24254 /* Parse an Objective-C class forward-declaration.
24256 objc-class-declaration:
24257 @class objc-identifier-list ;
24259 The function registers the forward declarations with the Objective-C
24260 front end. It returns nothing. */
24263 cp_parser_objc_class_declaration (cp_parser* parser)
24265 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24270 id = cp_parser_identifier (parser);
24271 if (id == error_mark_node)
24274 objc_declare_class (id);
24276 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24277 cp_lexer_consume_token (parser->lexer);
24281 cp_parser_consume_semicolon_at_end_of_statement (parser);
24284 /* Parse a list of Objective-C protocol references.
24286 objc-protocol-refs-opt:
24287 objc-protocol-refs [opt]
24289 objc-protocol-refs:
24290 < objc-identifier-list >
24292 Returns a TREE_LIST of identifiers, if any. */
24295 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24297 tree protorefs = NULL_TREE;
24299 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24301 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24302 protorefs = cp_parser_objc_identifier_list (parser);
24303 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24309 /* Parse a Objective-C visibility specification. */
24312 cp_parser_objc_visibility_spec (cp_parser* parser)
24314 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24316 switch (vis->keyword)
24318 case RID_AT_PRIVATE:
24319 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24321 case RID_AT_PROTECTED:
24322 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24324 case RID_AT_PUBLIC:
24325 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24327 case RID_AT_PACKAGE:
24328 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24334 /* Eat '@private'/'@protected'/'@public'. */
24335 cp_lexer_consume_token (parser->lexer);
24338 /* Parse an Objective-C method type. Return 'true' if it is a class
24339 (+) method, and 'false' if it is an instance (-) method. */
24342 cp_parser_objc_method_type (cp_parser* parser)
24344 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24350 /* Parse an Objective-C protocol qualifier. */
24353 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24355 tree quals = NULL_TREE, node;
24356 cp_token *token = cp_lexer_peek_token (parser->lexer);
24358 node = token->u.value;
24360 while (node && TREE_CODE (node) == IDENTIFIER_NODE
24361 && (node == ridpointers [(int) RID_IN]
24362 || node == ridpointers [(int) RID_OUT]
24363 || node == ridpointers [(int) RID_INOUT]
24364 || node == ridpointers [(int) RID_BYCOPY]
24365 || node == ridpointers [(int) RID_BYREF]
24366 || node == ridpointers [(int) RID_ONEWAY]))
24368 quals = tree_cons (NULL_TREE, node, quals);
24369 cp_lexer_consume_token (parser->lexer);
24370 token = cp_lexer_peek_token (parser->lexer);
24371 node = token->u.value;
24377 /* Parse an Objective-C typename. */
24380 cp_parser_objc_typename (cp_parser* parser)
24382 tree type_name = NULL_TREE;
24384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24386 tree proto_quals, cp_type = NULL_TREE;
24388 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24389 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24391 /* An ObjC type name may consist of just protocol qualifiers, in which
24392 case the type shall default to 'id'. */
24393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24395 cp_type = cp_parser_type_id (parser);
24397 /* If the type could not be parsed, an error has already
24398 been produced. For error recovery, behave as if it had
24399 not been specified, which will use the default type
24401 if (cp_type == error_mark_node)
24403 cp_type = NULL_TREE;
24404 /* We need to skip to the closing parenthesis as
24405 cp_parser_type_id() does not seem to do it for
24407 cp_parser_skip_to_closing_parenthesis (parser,
24408 /*recovering=*/true,
24409 /*or_comma=*/false,
24410 /*consume_paren=*/false);
24414 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24415 type_name = build_tree_list (proto_quals, cp_type);
24421 /* Check to see if TYPE refers to an Objective-C selector name. */
24424 cp_parser_objc_selector_p (enum cpp_ttype type)
24426 return (type == CPP_NAME || type == CPP_KEYWORD
24427 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24428 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24429 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24430 || type == CPP_XOR || type == CPP_XOR_EQ);
24433 /* Parse an Objective-C selector. */
24436 cp_parser_objc_selector (cp_parser* parser)
24438 cp_token *token = cp_lexer_consume_token (parser->lexer);
24440 if (!cp_parser_objc_selector_p (token->type))
24442 error_at (token->location, "invalid Objective-C++ selector name");
24443 return error_mark_node;
24446 /* C++ operator names are allowed to appear in ObjC selectors. */
24447 switch (token->type)
24449 case CPP_AND_AND: return get_identifier ("and");
24450 case CPP_AND_EQ: return get_identifier ("and_eq");
24451 case CPP_AND: return get_identifier ("bitand");
24452 case CPP_OR: return get_identifier ("bitor");
24453 case CPP_COMPL: return get_identifier ("compl");
24454 case CPP_NOT: return get_identifier ("not");
24455 case CPP_NOT_EQ: return get_identifier ("not_eq");
24456 case CPP_OR_OR: return get_identifier ("or");
24457 case CPP_OR_EQ: return get_identifier ("or_eq");
24458 case CPP_XOR: return get_identifier ("xor");
24459 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24460 default: return token->u.value;
24464 /* Parse an Objective-C params list. */
24467 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24469 tree params = NULL_TREE;
24470 bool maybe_unary_selector_p = true;
24471 cp_token *token = cp_lexer_peek_token (parser->lexer);
24473 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24475 tree selector = NULL_TREE, type_name, identifier;
24476 tree parm_attr = NULL_TREE;
24478 if (token->keyword == RID_ATTRIBUTE)
24481 if (token->type != CPP_COLON)
24482 selector = cp_parser_objc_selector (parser);
24484 /* Detect if we have a unary selector. */
24485 if (maybe_unary_selector_p
24486 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24488 params = selector; /* Might be followed by attributes. */
24492 maybe_unary_selector_p = false;
24493 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24495 /* Something went quite wrong. There should be a colon
24496 here, but there is not. Stop parsing parameters. */
24499 type_name = cp_parser_objc_typename (parser);
24500 /* New ObjC allows attributes on parameters too. */
24501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24502 parm_attr = cp_parser_attributes_opt (parser);
24503 identifier = cp_parser_identifier (parser);
24507 objc_build_keyword_decl (selector,
24512 token = cp_lexer_peek_token (parser->lexer);
24515 if (params == NULL_TREE)
24517 cp_parser_error (parser, "objective-c++ method declaration is expected");
24518 return error_mark_node;
24521 /* We allow tail attributes for the method. */
24522 if (token->keyword == RID_ATTRIBUTE)
24524 *attributes = cp_parser_attributes_opt (parser);
24525 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24526 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24528 cp_parser_error (parser,
24529 "method attributes must be specified at the end");
24530 return error_mark_node;
24533 if (params == NULL_TREE)
24535 cp_parser_error (parser, "objective-c++ method declaration is expected");
24536 return error_mark_node;
24541 /* Parse the non-keyword Objective-C params. */
24544 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24547 tree params = make_node (TREE_LIST);
24548 cp_token *token = cp_lexer_peek_token (parser->lexer);
24549 *ellipsisp = false; /* Initially, assume no ellipsis. */
24551 while (token->type == CPP_COMMA)
24553 cp_parameter_declarator *parmdecl;
24556 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24557 token = cp_lexer_peek_token (parser->lexer);
24559 if (token->type == CPP_ELLIPSIS)
24561 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24563 token = cp_lexer_peek_token (parser->lexer);
24567 /* TODO: parse attributes for tail parameters. */
24568 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24569 parm = grokdeclarator (parmdecl->declarator,
24570 &parmdecl->decl_specifiers,
24571 PARM, /*initialized=*/0,
24572 /*attrlist=*/NULL);
24574 chainon (params, build_tree_list (NULL_TREE, parm));
24575 token = cp_lexer_peek_token (parser->lexer);
24578 /* We allow tail attributes for the method. */
24579 if (token->keyword == RID_ATTRIBUTE)
24581 if (*attributes == NULL_TREE)
24583 *attributes = cp_parser_attributes_opt (parser);
24584 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24585 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24589 /* We have an error, but parse the attributes, so that we can
24591 *attributes = cp_parser_attributes_opt (parser);
24593 cp_parser_error (parser,
24594 "method attributes must be specified at the end");
24595 return error_mark_node;
24601 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24604 cp_parser_objc_interstitial_code (cp_parser* parser)
24606 cp_token *token = cp_lexer_peek_token (parser->lexer);
24608 /* If the next token is `extern' and the following token is a string
24609 literal, then we have a linkage specification. */
24610 if (token->keyword == RID_EXTERN
24611 && cp_parser_is_pure_string_literal
24612 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24613 cp_parser_linkage_specification (parser);
24614 /* Handle #pragma, if any. */
24615 else if (token->type == CPP_PRAGMA)
24616 cp_parser_pragma (parser, pragma_external);
24617 /* Allow stray semicolons. */
24618 else if (token->type == CPP_SEMICOLON)
24619 cp_lexer_consume_token (parser->lexer);
24620 /* Mark methods as optional or required, when building protocols. */
24621 else if (token->keyword == RID_AT_OPTIONAL)
24623 cp_lexer_consume_token (parser->lexer);
24624 objc_set_method_opt (true);
24626 else if (token->keyword == RID_AT_REQUIRED)
24628 cp_lexer_consume_token (parser->lexer);
24629 objc_set_method_opt (false);
24631 else if (token->keyword == RID_NAMESPACE)
24632 cp_parser_namespace_definition (parser);
24633 /* Other stray characters must generate errors. */
24634 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24636 cp_lexer_consume_token (parser->lexer);
24637 error ("stray %qs between Objective-C++ methods",
24638 token->type == CPP_OPEN_BRACE ? "{" : "}");
24640 /* Finally, try to parse a block-declaration, or a function-definition. */
24642 cp_parser_block_declaration (parser, /*statement_p=*/false);
24645 /* Parse a method signature. */
24648 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24650 tree rettype, kwdparms, optparms;
24651 bool ellipsis = false;
24652 bool is_class_method;
24654 is_class_method = cp_parser_objc_method_type (parser);
24655 rettype = cp_parser_objc_typename (parser);
24656 *attributes = NULL_TREE;
24657 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24658 if (kwdparms == error_mark_node)
24659 return error_mark_node;
24660 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24661 if (optparms == error_mark_node)
24662 return error_mark_node;
24664 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24668 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24671 cp_lexer_save_tokens (parser->lexer);
24672 tattr = cp_parser_attributes_opt (parser);
24673 gcc_assert (tattr) ;
24675 /* If the attributes are followed by a method introducer, this is not allowed.
24676 Dump the attributes and flag the situation. */
24677 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24678 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24681 /* Otherwise, the attributes introduce some interstitial code, possibly so
24682 rewind to allow that check. */
24683 cp_lexer_rollback_tokens (parser->lexer);
24687 /* Parse an Objective-C method prototype list. */
24690 cp_parser_objc_method_prototype_list (cp_parser* parser)
24692 cp_token *token = cp_lexer_peek_token (parser->lexer);
24694 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24696 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24698 tree attributes, sig;
24699 bool is_class_method;
24700 if (token->type == CPP_PLUS)
24701 is_class_method = true;
24703 is_class_method = false;
24704 sig = cp_parser_objc_method_signature (parser, &attributes);
24705 if (sig == error_mark_node)
24707 cp_parser_skip_to_end_of_block_or_statement (parser);
24708 token = cp_lexer_peek_token (parser->lexer);
24711 objc_add_method_declaration (is_class_method, sig, attributes);
24712 cp_parser_consume_semicolon_at_end_of_statement (parser);
24714 else if (token->keyword == RID_AT_PROPERTY)
24715 cp_parser_objc_at_property_declaration (parser);
24716 else if (token->keyword == RID_ATTRIBUTE
24717 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24718 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24720 "prefix attributes are ignored for methods");
24722 /* Allow for interspersed non-ObjC++ code. */
24723 cp_parser_objc_interstitial_code (parser);
24725 token = cp_lexer_peek_token (parser->lexer);
24728 if (token->type != CPP_EOF)
24729 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24731 cp_parser_error (parser, "expected %<@end%>");
24733 objc_finish_interface ();
24736 /* Parse an Objective-C method definition list. */
24739 cp_parser_objc_method_definition_list (cp_parser* parser)
24741 cp_token *token = cp_lexer_peek_token (parser->lexer);
24743 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24747 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24750 tree sig, attribute;
24751 bool is_class_method;
24752 if (token->type == CPP_PLUS)
24753 is_class_method = true;
24755 is_class_method = false;
24756 push_deferring_access_checks (dk_deferred);
24757 sig = cp_parser_objc_method_signature (parser, &attribute);
24758 if (sig == error_mark_node)
24760 cp_parser_skip_to_end_of_block_or_statement (parser);
24761 token = cp_lexer_peek_token (parser->lexer);
24764 objc_start_method_definition (is_class_method, sig, attribute,
24767 /* For historical reasons, we accept an optional semicolon. */
24768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24769 cp_lexer_consume_token (parser->lexer);
24771 ptk = cp_lexer_peek_token (parser->lexer);
24772 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24773 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24775 perform_deferred_access_checks (tf_warning_or_error);
24776 stop_deferring_access_checks ();
24777 meth = cp_parser_function_definition_after_declarator (parser,
24779 pop_deferring_access_checks ();
24780 objc_finish_method_definition (meth);
24783 /* The following case will be removed once @synthesize is
24784 completely implemented. */
24785 else if (token->keyword == RID_AT_PROPERTY)
24786 cp_parser_objc_at_property_declaration (parser);
24787 else if (token->keyword == RID_AT_SYNTHESIZE)
24788 cp_parser_objc_at_synthesize_declaration (parser);
24789 else if (token->keyword == RID_AT_DYNAMIC)
24790 cp_parser_objc_at_dynamic_declaration (parser);
24791 else if (token->keyword == RID_ATTRIBUTE
24792 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24793 warning_at (token->location, OPT_Wattributes,
24794 "prefix attributes are ignored for methods");
24796 /* Allow for interspersed non-ObjC++ code. */
24797 cp_parser_objc_interstitial_code (parser);
24799 token = cp_lexer_peek_token (parser->lexer);
24802 if (token->type != CPP_EOF)
24803 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24805 cp_parser_error (parser, "expected %<@end%>");
24807 objc_finish_implementation ();
24810 /* Parse Objective-C ivars. */
24813 cp_parser_objc_class_ivars (cp_parser* parser)
24815 cp_token *token = cp_lexer_peek_token (parser->lexer);
24817 if (token->type != CPP_OPEN_BRACE)
24818 return; /* No ivars specified. */
24820 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24821 token = cp_lexer_peek_token (parser->lexer);
24823 while (token->type != CPP_CLOSE_BRACE
24824 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24826 cp_decl_specifier_seq declspecs;
24827 int decl_class_or_enum_p;
24828 tree prefix_attributes;
24830 cp_parser_objc_visibility_spec (parser);
24832 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24835 cp_parser_decl_specifier_seq (parser,
24836 CP_PARSER_FLAGS_OPTIONAL,
24838 &decl_class_or_enum_p);
24840 /* auto, register, static, extern, mutable. */
24841 if (declspecs.storage_class != sc_none)
24843 cp_parser_error (parser, "invalid type for instance variable");
24844 declspecs.storage_class = sc_none;
24847 /* thread_local. */
24848 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24850 cp_parser_error (parser, "invalid type for instance variable");
24851 declspecs.locations[ds_thread] = 0;
24855 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24857 cp_parser_error (parser, "invalid type for instance variable");
24858 declspecs.locations[ds_typedef] = 0;
24861 prefix_attributes = declspecs.attributes;
24862 declspecs.attributes = NULL_TREE;
24864 /* Keep going until we hit the `;' at the end of the
24866 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24868 tree width = NULL_TREE, attributes, first_attribute, decl;
24869 cp_declarator *declarator = NULL;
24870 int ctor_dtor_or_conv_p;
24872 /* Check for a (possibly unnamed) bitfield declaration. */
24873 token = cp_lexer_peek_token (parser->lexer);
24874 if (token->type == CPP_COLON)
24877 if (token->type == CPP_NAME
24878 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24881 /* Get the name of the bitfield. */
24882 declarator = make_id_declarator (NULL_TREE,
24883 cp_parser_identifier (parser),
24887 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24888 /* Get the width of the bitfield. */
24890 = cp_parser_constant_expression (parser,
24891 /*allow_non_constant=*/false,
24896 /* Parse the declarator. */
24898 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24899 &ctor_dtor_or_conv_p,
24900 /*parenthesized_p=*/NULL,
24901 /*member_p=*/false);
24904 /* Look for attributes that apply to the ivar. */
24905 attributes = cp_parser_attributes_opt (parser);
24906 /* Remember which attributes are prefix attributes and
24908 first_attribute = attributes;
24909 /* Combine the attributes. */
24910 attributes = chainon (prefix_attributes, attributes);
24913 /* Create the bitfield declaration. */
24914 decl = grokbitfield (declarator, &declspecs,
24918 decl = grokfield (declarator, &declspecs,
24919 NULL_TREE, /*init_const_expr_p=*/false,
24920 NULL_TREE, attributes);
24922 /* Add the instance variable. */
24923 if (decl != error_mark_node && decl != NULL_TREE)
24924 objc_add_instance_variable (decl);
24926 /* Reset PREFIX_ATTRIBUTES. */
24927 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24928 attributes = TREE_CHAIN (attributes);
24930 TREE_CHAIN (attributes) = NULL_TREE;
24932 token = cp_lexer_peek_token (parser->lexer);
24934 if (token->type == CPP_COMMA)
24936 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24942 cp_parser_consume_semicolon_at_end_of_statement (parser);
24943 token = cp_lexer_peek_token (parser->lexer);
24946 if (token->keyword == RID_AT_END)
24947 cp_parser_error (parser, "expected %<}%>");
24949 /* Do not consume the RID_AT_END, so it will be read again as terminating
24950 the @interface of @implementation. */
24951 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24952 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24954 /* For historical reasons, we accept an optional semicolon. */
24955 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24956 cp_lexer_consume_token (parser->lexer);
24959 /* Parse an Objective-C protocol declaration. */
24962 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24964 tree proto, protorefs;
24967 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24970 tok = cp_lexer_peek_token (parser->lexer);
24971 error_at (tok->location, "identifier expected after %<@protocol%>");
24972 cp_parser_consume_semicolon_at_end_of_statement (parser);
24976 /* See if we have a forward declaration or a definition. */
24977 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24979 /* Try a forward declaration first. */
24980 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24986 id = cp_parser_identifier (parser);
24987 if (id == error_mark_node)
24990 objc_declare_protocol (id, attributes);
24992 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24993 cp_lexer_consume_token (parser->lexer);
24997 cp_parser_consume_semicolon_at_end_of_statement (parser);
25000 /* Ok, we got a full-fledged definition (or at least should). */
25003 proto = cp_parser_identifier (parser);
25004 protorefs = cp_parser_objc_protocol_refs_opt (parser);
25005 objc_start_protocol (proto, protorefs, attributes);
25006 cp_parser_objc_method_prototype_list (parser);
25010 /* Parse an Objective-C superclass or category. */
25013 cp_parser_objc_superclass_or_category (cp_parser *parser,
25016 tree *categ, bool *is_class_extension)
25018 cp_token *next = cp_lexer_peek_token (parser->lexer);
25020 *super = *categ = NULL_TREE;
25021 *is_class_extension = false;
25022 if (next->type == CPP_COLON)
25024 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25025 *super = cp_parser_identifier (parser);
25027 else if (next->type == CPP_OPEN_PAREN)
25029 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25031 /* If there is no category name, and this is an @interface, we
25032 have a class extension. */
25033 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25035 *categ = NULL_TREE;
25036 *is_class_extension = true;
25039 *categ = cp_parser_identifier (parser);
25041 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25045 /* Parse an Objective-C class interface. */
25048 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25050 tree name, super, categ, protos;
25051 bool is_class_extension;
25053 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25054 name = cp_parser_identifier (parser);
25055 if (name == error_mark_node)
25057 /* It's hard to recover because even if valid @interface stuff
25058 is to follow, we can't compile it (or validate it) if we
25059 don't even know which class it refers to. Let's assume this
25060 was a stray '@interface' token in the stream and skip it.
25064 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25065 &is_class_extension);
25066 protos = cp_parser_objc_protocol_refs_opt (parser);
25068 /* We have either a class or a category on our hands. */
25069 if (categ || is_class_extension)
25070 objc_start_category_interface (name, categ, protos, attributes);
25073 objc_start_class_interface (name, super, protos, attributes);
25074 /* Handle instance variable declarations, if any. */
25075 cp_parser_objc_class_ivars (parser);
25076 objc_continue_interface ();
25079 cp_parser_objc_method_prototype_list (parser);
25082 /* Parse an Objective-C class implementation. */
25085 cp_parser_objc_class_implementation (cp_parser* parser)
25087 tree name, super, categ;
25088 bool is_class_extension;
25090 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25091 name = cp_parser_identifier (parser);
25092 if (name == error_mark_node)
25094 /* It's hard to recover because even if valid @implementation
25095 stuff is to follow, we can't compile it (or validate it) if
25096 we don't even know which class it refers to. Let's assume
25097 this was a stray '@implementation' token in the stream and
25102 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25103 &is_class_extension);
25105 /* We have either a class or a category on our hands. */
25107 objc_start_category_implementation (name, categ);
25110 objc_start_class_implementation (name, super);
25111 /* Handle instance variable declarations, if any. */
25112 cp_parser_objc_class_ivars (parser);
25113 objc_continue_implementation ();
25116 cp_parser_objc_method_definition_list (parser);
25119 /* Consume the @end token and finish off the implementation. */
25122 cp_parser_objc_end_implementation (cp_parser* parser)
25124 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25125 objc_finish_implementation ();
25128 /* Parse an Objective-C declaration. */
25131 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25133 /* Try to figure out what kind of declaration is present. */
25134 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25137 switch (kwd->keyword)
25142 error_at (kwd->location, "attributes may not be specified before"
25143 " the %<@%D%> Objective-C++ keyword",
25147 case RID_AT_IMPLEMENTATION:
25148 warning_at (kwd->location, OPT_Wattributes,
25149 "prefix attributes are ignored before %<@%D%>",
25156 switch (kwd->keyword)
25159 cp_parser_objc_alias_declaration (parser);
25162 cp_parser_objc_class_declaration (parser);
25164 case RID_AT_PROTOCOL:
25165 cp_parser_objc_protocol_declaration (parser, attributes);
25167 case RID_AT_INTERFACE:
25168 cp_parser_objc_class_interface (parser, attributes);
25170 case RID_AT_IMPLEMENTATION:
25171 cp_parser_objc_class_implementation (parser);
25174 cp_parser_objc_end_implementation (parser);
25177 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25179 cp_parser_skip_to_end_of_block_or_statement (parser);
25183 /* Parse an Objective-C try-catch-finally statement.
25185 objc-try-catch-finally-stmt:
25186 @try compound-statement objc-catch-clause-seq [opt]
25187 objc-finally-clause [opt]
25189 objc-catch-clause-seq:
25190 objc-catch-clause objc-catch-clause-seq [opt]
25193 @catch ( objc-exception-declaration ) compound-statement
25195 objc-finally-clause:
25196 @finally compound-statement
25198 objc-exception-declaration:
25199 parameter-declaration
25202 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25206 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25207 for C. Keep them in sync. */
25210 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25212 location_t location;
25215 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25216 location = cp_lexer_peek_token (parser->lexer)->location;
25217 objc_maybe_warn_exceptions (location);
25218 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25219 node, lest it get absorbed into the surrounding block. */
25220 stmt = push_stmt_list ();
25221 cp_parser_compound_statement (parser, NULL, false, false);
25222 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25224 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25226 cp_parameter_declarator *parm;
25227 tree parameter_declaration = error_mark_node;
25228 bool seen_open_paren = false;
25230 cp_lexer_consume_token (parser->lexer);
25231 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25232 seen_open_paren = true;
25233 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25235 /* We have "@catch (...)" (where the '...' are literally
25236 what is in the code). Skip the '...'.
25237 parameter_declaration is set to NULL_TREE, and
25238 objc_being_catch_clauses() knows that that means
25240 cp_lexer_consume_token (parser->lexer);
25241 parameter_declaration = NULL_TREE;
25245 /* We have "@catch (NSException *exception)" or something
25246 like that. Parse the parameter declaration. */
25247 parm = cp_parser_parameter_declaration (parser, false, NULL);
25249 parameter_declaration = error_mark_node;
25251 parameter_declaration = grokdeclarator (parm->declarator,
25252 &parm->decl_specifiers,
25253 PARM, /*initialized=*/0,
25254 /*attrlist=*/NULL);
25256 if (seen_open_paren)
25257 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25260 /* If there was no open parenthesis, we are recovering from
25261 an error, and we are trying to figure out what mistake
25262 the user has made. */
25264 /* If there is an immediate closing parenthesis, the user
25265 probably forgot the opening one (ie, they typed "@catch
25266 NSException *e)". Parse the closing parenthesis and keep
25268 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25269 cp_lexer_consume_token (parser->lexer);
25271 /* If these is no immediate closing parenthesis, the user
25272 probably doesn't know that parenthesis are required at
25273 all (ie, they typed "@catch NSException *e"). So, just
25274 forget about the closing parenthesis and keep going. */
25276 objc_begin_catch_clause (parameter_declaration);
25277 cp_parser_compound_statement (parser, NULL, false, false);
25278 objc_finish_catch_clause ();
25280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25282 cp_lexer_consume_token (parser->lexer);
25283 location = cp_lexer_peek_token (parser->lexer)->location;
25284 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25285 node, lest it get absorbed into the surrounding block. */
25286 stmt = push_stmt_list ();
25287 cp_parser_compound_statement (parser, NULL, false, false);
25288 objc_build_finally_clause (location, pop_stmt_list (stmt));
25291 return objc_finish_try_stmt ();
25294 /* Parse an Objective-C synchronized statement.
25296 objc-synchronized-stmt:
25297 @synchronized ( expression ) compound-statement
25299 Returns NULL_TREE. */
25302 cp_parser_objc_synchronized_statement (cp_parser *parser)
25304 location_t location;
25307 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25309 location = cp_lexer_peek_token (parser->lexer)->location;
25310 objc_maybe_warn_exceptions (location);
25311 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25312 lock = cp_parser_expression (parser, false, NULL);
25313 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25315 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25316 node, lest it get absorbed into the surrounding block. */
25317 stmt = push_stmt_list ();
25318 cp_parser_compound_statement (parser, NULL, false, false);
25320 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25323 /* Parse an Objective-C throw statement.
25326 @throw assignment-expression [opt] ;
25328 Returns a constructed '@throw' statement. */
25331 cp_parser_objc_throw_statement (cp_parser *parser)
25333 tree expr = NULL_TREE;
25334 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25336 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25339 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25341 cp_parser_consume_semicolon_at_end_of_statement (parser);
25343 return objc_build_throw_stmt (loc, expr);
25346 /* Parse an Objective-C statement. */
25349 cp_parser_objc_statement (cp_parser * parser)
25351 /* Try to figure out what kind of declaration is present. */
25352 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25354 switch (kwd->keyword)
25357 return cp_parser_objc_try_catch_finally_statement (parser);
25358 case RID_AT_SYNCHRONIZED:
25359 return cp_parser_objc_synchronized_statement (parser);
25361 return cp_parser_objc_throw_statement (parser);
25363 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25365 cp_parser_skip_to_end_of_block_or_statement (parser);
25368 return error_mark_node;
25371 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25372 look ahead to see if an objc keyword follows the attributes. This
25373 is to detect the use of prefix attributes on ObjC @interface and
25377 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25379 cp_lexer_save_tokens (parser->lexer);
25380 *attrib = cp_parser_attributes_opt (parser);
25381 gcc_assert (*attrib);
25382 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25384 cp_lexer_commit_tokens (parser->lexer);
25387 cp_lexer_rollback_tokens (parser->lexer);
25391 /* This routine is a minimal replacement for
25392 c_parser_struct_declaration () used when parsing the list of
25393 types/names or ObjC++ properties. For example, when parsing the
25396 @property (readonly) int a, b, c;
25398 this function is responsible for parsing "int a, int b, int c" and
25399 returning the declarations as CHAIN of DECLs.
25401 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25402 similar parsing. */
25404 cp_parser_objc_struct_declaration (cp_parser *parser)
25406 tree decls = NULL_TREE;
25407 cp_decl_specifier_seq declspecs;
25408 int decl_class_or_enum_p;
25409 tree prefix_attributes;
25411 cp_parser_decl_specifier_seq (parser,
25412 CP_PARSER_FLAGS_NONE,
25414 &decl_class_or_enum_p);
25416 if (declspecs.type == error_mark_node)
25417 return error_mark_node;
25419 /* auto, register, static, extern, mutable. */
25420 if (declspecs.storage_class != sc_none)
25422 cp_parser_error (parser, "invalid type for property");
25423 declspecs.storage_class = sc_none;
25426 /* thread_local. */
25427 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25429 cp_parser_error (parser, "invalid type for property");
25430 declspecs.locations[ds_thread] = 0;
25434 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25436 cp_parser_error (parser, "invalid type for property");
25437 declspecs.locations[ds_typedef] = 0;
25440 prefix_attributes = declspecs.attributes;
25441 declspecs.attributes = NULL_TREE;
25443 /* Keep going until we hit the `;' at the end of the declaration. */
25444 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25446 tree attributes, first_attribute, decl;
25447 cp_declarator *declarator;
25450 /* Parse the declarator. */
25451 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25452 NULL, NULL, false);
25454 /* Look for attributes that apply to the ivar. */
25455 attributes = cp_parser_attributes_opt (parser);
25456 /* Remember which attributes are prefix attributes and
25458 first_attribute = attributes;
25459 /* Combine the attributes. */
25460 attributes = chainon (prefix_attributes, attributes);
25462 decl = grokfield (declarator, &declspecs,
25463 NULL_TREE, /*init_const_expr_p=*/false,
25464 NULL_TREE, attributes);
25466 if (decl == error_mark_node || decl == NULL_TREE)
25467 return error_mark_node;
25469 /* Reset PREFIX_ATTRIBUTES. */
25470 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25471 attributes = TREE_CHAIN (attributes);
25473 TREE_CHAIN (attributes) = NULL_TREE;
25475 DECL_CHAIN (decl) = decls;
25478 token = cp_lexer_peek_token (parser->lexer);
25479 if (token->type == CPP_COMMA)
25481 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25490 /* Parse an Objective-C @property declaration. The syntax is:
25492 objc-property-declaration:
25493 '@property' objc-property-attributes[opt] struct-declaration ;
25495 objc-property-attributes:
25496 '(' objc-property-attribute-list ')'
25498 objc-property-attribute-list:
25499 objc-property-attribute
25500 objc-property-attribute-list, objc-property-attribute
25502 objc-property-attribute
25503 'getter' = identifier
25504 'setter' = identifier
25513 @property NSString *name;
25514 @property (readonly) id object;
25515 @property (retain, nonatomic, getter=getTheName) id name;
25516 @property int a, b, c;
25518 PS: This function is identical to
25519 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25521 cp_parser_objc_at_property_declaration (cp_parser *parser)
25523 /* The following variables hold the attributes of the properties as
25524 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25525 seen. When we see an attribute, we set them to 'true' (if they
25526 are boolean properties) or to the identifier (if they have an
25527 argument, ie, for getter and setter). Note that here we only
25528 parse the list of attributes, check the syntax and accumulate the
25529 attributes that we find. objc_add_property_declaration() will
25530 then process the information. */
25531 bool property_assign = false;
25532 bool property_copy = false;
25533 tree property_getter_ident = NULL_TREE;
25534 bool property_nonatomic = false;
25535 bool property_readonly = false;
25536 bool property_readwrite = false;
25537 bool property_retain = false;
25538 tree property_setter_ident = NULL_TREE;
25540 /* 'properties' is the list of properties that we read. Usually a
25541 single one, but maybe more (eg, in "@property int a, b, c;" there
25546 loc = cp_lexer_peek_token (parser->lexer)->location;
25548 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25550 /* Parse the optional attribute list... */
25551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25554 cp_lexer_consume_token (parser->lexer);
25558 bool syntax_error = false;
25559 cp_token *token = cp_lexer_peek_token (parser->lexer);
25562 if (token->type != CPP_NAME)
25564 cp_parser_error (parser, "expected identifier");
25567 keyword = C_RID_CODE (token->u.value);
25568 cp_lexer_consume_token (parser->lexer);
25571 case RID_ASSIGN: property_assign = true; break;
25572 case RID_COPY: property_copy = true; break;
25573 case RID_NONATOMIC: property_nonatomic = true; break;
25574 case RID_READONLY: property_readonly = true; break;
25575 case RID_READWRITE: property_readwrite = true; break;
25576 case RID_RETAIN: property_retain = true; break;
25580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25582 if (keyword == RID_GETTER)
25583 cp_parser_error (parser,
25584 "missing %<=%> (after %<getter%> attribute)");
25586 cp_parser_error (parser,
25587 "missing %<=%> (after %<setter%> attribute)");
25588 syntax_error = true;
25591 cp_lexer_consume_token (parser->lexer); /* eat the = */
25592 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25594 cp_parser_error (parser, "expected identifier");
25595 syntax_error = true;
25598 if (keyword == RID_SETTER)
25600 if (property_setter_ident != NULL_TREE)
25602 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25603 cp_lexer_consume_token (parser->lexer);
25606 property_setter_ident = cp_parser_objc_selector (parser);
25607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25608 cp_parser_error (parser, "setter name must terminate with %<:%>");
25610 cp_lexer_consume_token (parser->lexer);
25614 if (property_getter_ident != NULL_TREE)
25616 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25617 cp_lexer_consume_token (parser->lexer);
25620 property_getter_ident = cp_parser_objc_selector (parser);
25624 cp_parser_error (parser, "unknown property attribute");
25625 syntax_error = true;
25632 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25633 cp_lexer_consume_token (parser->lexer);
25638 /* FIXME: "@property (setter, assign);" will generate a spurious
25639 "error: expected ‘)’ before ‘,’ token". This is because
25640 cp_parser_require, unlike the C counterpart, will produce an
25641 error even if we are in error recovery. */
25642 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25644 cp_parser_skip_to_closing_parenthesis (parser,
25645 /*recovering=*/true,
25646 /*or_comma=*/false,
25647 /*consume_paren=*/true);
25651 /* ... and the property declaration(s). */
25652 properties = cp_parser_objc_struct_declaration (parser);
25654 if (properties == error_mark_node)
25656 cp_parser_skip_to_end_of_statement (parser);
25657 /* If the next token is now a `;', consume it. */
25658 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25659 cp_lexer_consume_token (parser->lexer);
25663 if (properties == NULL_TREE)
25664 cp_parser_error (parser, "expected identifier");
25667 /* Comma-separated properties are chained together in
25668 reverse order; add them one by one. */
25669 properties = nreverse (properties);
25671 for (; properties; properties = TREE_CHAIN (properties))
25672 objc_add_property_declaration (loc, copy_node (properties),
25673 property_readonly, property_readwrite,
25674 property_assign, property_retain,
25675 property_copy, property_nonatomic,
25676 property_getter_ident, property_setter_ident);
25679 cp_parser_consume_semicolon_at_end_of_statement (parser);
25682 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25684 objc-synthesize-declaration:
25685 @synthesize objc-synthesize-identifier-list ;
25687 objc-synthesize-identifier-list:
25688 objc-synthesize-identifier
25689 objc-synthesize-identifier-list, objc-synthesize-identifier
25691 objc-synthesize-identifier
25693 identifier = identifier
25696 @synthesize MyProperty;
25697 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25699 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25700 for C. Keep them in sync.
25703 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25705 tree list = NULL_TREE;
25707 loc = cp_lexer_peek_token (parser->lexer)->location;
25709 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25712 tree property, ivar;
25713 property = cp_parser_identifier (parser);
25714 if (property == error_mark_node)
25716 cp_parser_consume_semicolon_at_end_of_statement (parser);
25719 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25721 cp_lexer_consume_token (parser->lexer);
25722 ivar = cp_parser_identifier (parser);
25723 if (ivar == error_mark_node)
25725 cp_parser_consume_semicolon_at_end_of_statement (parser);
25731 list = chainon (list, build_tree_list (ivar, property));
25732 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25733 cp_lexer_consume_token (parser->lexer);
25737 cp_parser_consume_semicolon_at_end_of_statement (parser);
25738 objc_add_synthesize_declaration (loc, list);
25741 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25743 objc-dynamic-declaration:
25744 @dynamic identifier-list ;
25747 @dynamic MyProperty;
25748 @dynamic MyProperty, AnotherProperty;
25750 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25751 for C. Keep them in sync.
25754 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25756 tree list = NULL_TREE;
25758 loc = cp_lexer_peek_token (parser->lexer)->location;
25760 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25764 property = cp_parser_identifier (parser);
25765 if (property == error_mark_node)
25767 cp_parser_consume_semicolon_at_end_of_statement (parser);
25770 list = chainon (list, build_tree_list (NULL, property));
25771 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25772 cp_lexer_consume_token (parser->lexer);
25776 cp_parser_consume_semicolon_at_end_of_statement (parser);
25777 objc_add_dynamic_declaration (loc, list);
25781 /* OpenMP 2.5 parsing routines. */
25783 /* Returns name of the next clause.
25784 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25785 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25786 returned and the token is consumed. */
25788 static pragma_omp_clause
25789 cp_parser_omp_clause_name (cp_parser *parser)
25791 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25794 result = PRAGMA_OMP_CLAUSE_IF;
25795 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25796 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25797 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25798 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25799 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25801 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25802 const char *p = IDENTIFIER_POINTER (id);
25807 if (!strcmp ("collapse", p))
25808 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25809 else if (!strcmp ("copyin", p))
25810 result = PRAGMA_OMP_CLAUSE_COPYIN;
25811 else if (!strcmp ("copyprivate", p))
25812 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25815 if (!strcmp ("final", p))
25816 result = PRAGMA_OMP_CLAUSE_FINAL;
25817 else if (!strcmp ("firstprivate", p))
25818 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25821 if (!strcmp ("lastprivate", p))
25822 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25825 if (!strcmp ("mergeable", p))
25826 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25829 if (!strcmp ("nowait", p))
25830 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25831 else if (!strcmp ("num_threads", p))
25832 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25835 if (!strcmp ("ordered", p))
25836 result = PRAGMA_OMP_CLAUSE_ORDERED;
25839 if (!strcmp ("reduction", p))
25840 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25843 if (!strcmp ("schedule", p))
25844 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25845 else if (!strcmp ("shared", p))
25846 result = PRAGMA_OMP_CLAUSE_SHARED;
25849 if (!strcmp ("untied", p))
25850 result = PRAGMA_OMP_CLAUSE_UNTIED;
25855 if (result != PRAGMA_OMP_CLAUSE_NONE)
25856 cp_lexer_consume_token (parser->lexer);
25861 /* Validate that a clause of the given type does not already exist. */
25864 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25865 const char *name, location_t location)
25869 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25870 if (OMP_CLAUSE_CODE (c) == code)
25872 error_at (location, "too many %qs clauses", name);
25880 variable-list , identifier
25882 In addition, we match a closing parenthesis. An opening parenthesis
25883 will have been consumed by the caller.
25885 If KIND is nonzero, create the appropriate node and install the decl
25886 in OMP_CLAUSE_DECL and add the node to the head of the list.
25888 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25889 return the list created. */
25892 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25900 token = cp_lexer_peek_token (parser->lexer);
25901 name = cp_parser_id_expression (parser, /*template_p=*/false,
25902 /*check_dependency_p=*/true,
25903 /*template_p=*/NULL,
25904 /*declarator_p=*/false,
25905 /*optional_p=*/false);
25906 if (name == error_mark_node)
25909 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25910 if (decl == error_mark_node)
25911 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25913 else if (kind != 0)
25915 tree u = build_omp_clause (token->location, kind);
25916 OMP_CLAUSE_DECL (u) = decl;
25917 OMP_CLAUSE_CHAIN (u) = list;
25921 list = tree_cons (decl, NULL_TREE, list);
25924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25926 cp_lexer_consume_token (parser->lexer);
25929 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25933 /* Try to resync to an unnested comma. Copied from
25934 cp_parser_parenthesized_expression_list. */
25936 ending = cp_parser_skip_to_closing_parenthesis (parser,
25937 /*recovering=*/true,
25939 /*consume_paren=*/true);
25947 /* Similarly, but expect leading and trailing parenthesis. This is a very
25948 common case for omp clauses. */
25951 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25953 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25954 return cp_parser_omp_var_list_no_open (parser, kind, list);
25959 collapse ( constant-expression ) */
25962 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25968 loc = cp_lexer_peek_token (parser->lexer)->location;
25969 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25972 num = cp_parser_constant_expression (parser, false, NULL);
25974 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25975 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25976 /*or_comma=*/false,
25977 /*consume_paren=*/true);
25979 if (num == error_mark_node)
25981 num = fold_non_dependent_expr (num);
25982 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25983 || !host_integerp (num, 0)
25984 || (n = tree_low_cst (num, 0)) <= 0
25987 error_at (loc, "collapse argument needs positive constant integer expression");
25991 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25992 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25993 OMP_CLAUSE_CHAIN (c) = list;
25994 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
26000 default ( shared | none ) */
26003 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26005 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26008 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26010 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26012 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26013 const char *p = IDENTIFIER_POINTER (id);
26018 if (strcmp ("none", p) != 0)
26020 kind = OMP_CLAUSE_DEFAULT_NONE;
26024 if (strcmp ("shared", p) != 0)
26026 kind = OMP_CLAUSE_DEFAULT_SHARED;
26033 cp_lexer_consume_token (parser->lexer);
26038 cp_parser_error (parser, "expected %<none%> or %<shared%>");
26041 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26042 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26043 /*or_comma=*/false,
26044 /*consume_paren=*/true);
26046 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26049 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26050 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26051 OMP_CLAUSE_CHAIN (c) = list;
26052 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26058 final ( expression ) */
26061 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26065 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26068 t = cp_parser_condition (parser);
26070 if (t == error_mark_node
26071 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26072 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26073 /*or_comma=*/false,
26074 /*consume_paren=*/true);
26076 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26078 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26079 OMP_CLAUSE_FINAL_EXPR (c) = t;
26080 OMP_CLAUSE_CHAIN (c) = list;
26086 if ( expression ) */
26089 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26093 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26096 t = cp_parser_condition (parser);
26098 if (t == error_mark_node
26099 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26100 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26101 /*or_comma=*/false,
26102 /*consume_paren=*/true);
26104 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26106 c = build_omp_clause (location, OMP_CLAUSE_IF);
26107 OMP_CLAUSE_IF_EXPR (c) = t;
26108 OMP_CLAUSE_CHAIN (c) = list;
26117 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26118 tree list, location_t location)
26122 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26125 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
26126 OMP_CLAUSE_CHAIN (c) = list;
26134 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
26135 tree list, location_t location)
26139 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
26141 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
26142 OMP_CLAUSE_CHAIN (c) = list;
26147 num_threads ( expression ) */
26150 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
26151 location_t location)
26155 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26158 t = cp_parser_expression (parser, false, NULL);
26160 if (t == error_mark_node
26161 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26162 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26163 /*or_comma=*/false,
26164 /*consume_paren=*/true);
26166 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
26167 "num_threads", location);
26169 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
26170 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
26171 OMP_CLAUSE_CHAIN (c) = list;
26180 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
26181 tree list, location_t location)
26185 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
26186 "ordered", location);
26188 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
26189 OMP_CLAUSE_CHAIN (c) = list;
26194 reduction ( reduction-operator : variable-list )
26196 reduction-operator:
26197 One of: + * - & ^ | && ||
26201 reduction-operator:
26202 One of: + * - & ^ | && || min max */
26205 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26207 enum tree_code code;
26210 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26213 switch (cp_lexer_peek_token (parser->lexer)->type)
26225 code = BIT_AND_EXPR;
26228 code = BIT_XOR_EXPR;
26231 code = BIT_IOR_EXPR;
26234 code = TRUTH_ANDIF_EXPR;
26237 code = TRUTH_ORIF_EXPR;
26241 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26242 const char *p = IDENTIFIER_POINTER (id);
26244 if (strcmp (p, "min") == 0)
26249 if (strcmp (p, "max") == 0)
26257 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26258 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26260 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26261 /*or_comma=*/false,
26262 /*consume_paren=*/true);
26265 cp_lexer_consume_token (parser->lexer);
26267 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26270 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26271 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26272 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26278 schedule ( schedule-kind )
26279 schedule ( schedule-kind , expression )
26282 static | dynamic | guided | runtime | auto */
26285 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26292 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26294 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26296 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26297 const char *p = IDENTIFIER_POINTER (id);
26302 if (strcmp ("dynamic", p) != 0)
26304 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26308 if (strcmp ("guided", p) != 0)
26310 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26314 if (strcmp ("runtime", p) != 0)
26316 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26323 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26324 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26325 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26326 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26329 cp_lexer_consume_token (parser->lexer);
26331 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26334 cp_lexer_consume_token (parser->lexer);
26336 token = cp_lexer_peek_token (parser->lexer);
26337 t = cp_parser_assignment_expression (parser, false, NULL);
26339 if (t == error_mark_node)
26341 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26342 error_at (token->location, "schedule %<runtime%> does not take "
26343 "a %<chunk_size%> parameter");
26344 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26345 error_at (token->location, "schedule %<auto%> does not take "
26346 "a %<chunk_size%> parameter");
26348 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26350 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26353 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26356 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26357 OMP_CLAUSE_CHAIN (c) = list;
26361 cp_parser_error (parser, "invalid schedule kind");
26363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26364 /*or_comma=*/false,
26365 /*consume_paren=*/true);
26373 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26374 tree list, location_t location)
26378 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26380 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26381 OMP_CLAUSE_CHAIN (c) = list;
26385 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26386 is a bitmask in MASK. Return the list of clauses found; the result
26387 of clause default goes in *pdefault. */
26390 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26391 const char *where, cp_token *pragma_tok)
26393 tree clauses = NULL;
26395 cp_token *token = NULL;
26397 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26399 pragma_omp_clause c_kind;
26400 const char *c_name;
26401 tree prev = clauses;
26403 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26404 cp_lexer_consume_token (parser->lexer);
26406 token = cp_lexer_peek_token (parser->lexer);
26407 c_kind = cp_parser_omp_clause_name (parser);
26412 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26413 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26415 c_name = "collapse";
26417 case PRAGMA_OMP_CLAUSE_COPYIN:
26418 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26421 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26422 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26424 c_name = "copyprivate";
26426 case PRAGMA_OMP_CLAUSE_DEFAULT:
26427 clauses = cp_parser_omp_clause_default (parser, clauses,
26429 c_name = "default";
26431 case PRAGMA_OMP_CLAUSE_FINAL:
26432 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26435 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26436 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26438 c_name = "firstprivate";
26440 case PRAGMA_OMP_CLAUSE_IF:
26441 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26444 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26445 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26447 c_name = "lastprivate";
26449 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26450 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26452 c_name = "mergeable";
26454 case PRAGMA_OMP_CLAUSE_NOWAIT:
26455 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26458 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26459 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26461 c_name = "num_threads";
26463 case PRAGMA_OMP_CLAUSE_ORDERED:
26464 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26466 c_name = "ordered";
26468 case PRAGMA_OMP_CLAUSE_PRIVATE:
26469 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26471 c_name = "private";
26473 case PRAGMA_OMP_CLAUSE_REDUCTION:
26474 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26475 c_name = "reduction";
26477 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26478 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26480 c_name = "schedule";
26482 case PRAGMA_OMP_CLAUSE_SHARED:
26483 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26487 case PRAGMA_OMP_CLAUSE_UNTIED:
26488 clauses = cp_parser_omp_clause_untied (parser, clauses,
26493 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26497 if (((mask >> c_kind) & 1) == 0)
26499 /* Remove the invalid clause(s) from the list to avoid
26500 confusing the rest of the compiler. */
26502 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26506 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26507 return finish_omp_clauses (clauses);
26514 In practice, we're also interested in adding the statement to an
26515 outer node. So it is convenient if we work around the fact that
26516 cp_parser_statement calls add_stmt. */
26519 cp_parser_begin_omp_structured_block (cp_parser *parser)
26521 unsigned save = parser->in_statement;
26523 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26524 This preserves the "not within loop or switch" style error messages
26525 for nonsense cases like
26531 if (parser->in_statement)
26532 parser->in_statement = IN_OMP_BLOCK;
26538 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26540 parser->in_statement = save;
26544 cp_parser_omp_structured_block (cp_parser *parser)
26546 tree stmt = begin_omp_structured_block ();
26547 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26549 cp_parser_statement (parser, NULL_TREE, false, NULL);
26551 cp_parser_end_omp_structured_block (parser, save);
26552 return finish_omp_structured_block (stmt);
26556 # pragma omp atomic new-line
26560 x binop= expr | x++ | ++x | x-- | --x
26562 +, *, -, /, &, ^, |, <<, >>
26564 where x is an lvalue expression with scalar type.
26567 # pragma omp atomic new-line
26570 # pragma omp atomic read new-line
26573 # pragma omp atomic write new-line
26576 # pragma omp atomic update new-line
26579 # pragma omp atomic capture new-line
26582 # pragma omp atomic capture new-line
26590 expression-stmt | x = x binop expr
26592 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26594 { v = x; update-stmt; } | { update-stmt; v = x; }
26596 where x and v are lvalue expressions with scalar type. */
26599 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26601 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26602 tree rhs1 = NULL_TREE, orig_lhs;
26603 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26604 bool structured_block = false;
26606 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26608 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26609 const char *p = IDENTIFIER_POINTER (id);
26611 if (!strcmp (p, "read"))
26612 code = OMP_ATOMIC_READ;
26613 else if (!strcmp (p, "write"))
26615 else if (!strcmp (p, "update"))
26617 else if (!strcmp (p, "capture"))
26618 code = OMP_ATOMIC_CAPTURE_NEW;
26622 cp_lexer_consume_token (parser->lexer);
26624 cp_parser_require_pragma_eol (parser, pragma_tok);
26628 case OMP_ATOMIC_READ:
26629 case NOP_EXPR: /* atomic write */
26630 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26631 /*cast_p=*/false, NULL);
26632 if (v == error_mark_node)
26634 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26636 if (code == NOP_EXPR)
26637 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26639 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26640 /*cast_p=*/false, NULL);
26641 if (lhs == error_mark_node)
26643 if (code == NOP_EXPR)
26645 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26653 case OMP_ATOMIC_CAPTURE_NEW:
26654 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26656 cp_lexer_consume_token (parser->lexer);
26657 structured_block = true;
26661 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26662 /*cast_p=*/false, NULL);
26663 if (v == error_mark_node)
26665 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26673 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26674 /*cast_p=*/false, NULL);
26676 switch (TREE_CODE (lhs))
26681 case POSTINCREMENT_EXPR:
26682 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26683 code = OMP_ATOMIC_CAPTURE_OLD;
26685 case PREINCREMENT_EXPR:
26686 lhs = TREE_OPERAND (lhs, 0);
26687 opcode = PLUS_EXPR;
26688 rhs = integer_one_node;
26691 case POSTDECREMENT_EXPR:
26692 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26693 code = OMP_ATOMIC_CAPTURE_OLD;
26695 case PREDECREMENT_EXPR:
26696 lhs = TREE_OPERAND (lhs, 0);
26697 opcode = MINUS_EXPR;
26698 rhs = integer_one_node;
26701 case COMPOUND_EXPR:
26702 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26703 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26704 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26705 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26706 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26707 (TREE_OPERAND (lhs, 1), 0), 0)))
26709 /* Undo effects of boolean_increment for post {in,de}crement. */
26710 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26713 if (TREE_CODE (lhs) == MODIFY_EXPR
26714 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26716 /* Undo effects of boolean_increment. */
26717 if (integer_onep (TREE_OPERAND (lhs, 1)))
26719 /* This is pre or post increment. */
26720 rhs = TREE_OPERAND (lhs, 1);
26721 lhs = TREE_OPERAND (lhs, 0);
26723 if (code == OMP_ATOMIC_CAPTURE_NEW
26724 && !structured_block
26725 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26726 code = OMP_ATOMIC_CAPTURE_OLD;
26732 switch (cp_lexer_peek_token (parser->lexer)->type)
26735 opcode = MULT_EXPR;
26738 opcode = TRUNC_DIV_EXPR;
26741 opcode = PLUS_EXPR;
26744 opcode = MINUS_EXPR;
26746 case CPP_LSHIFT_EQ:
26747 opcode = LSHIFT_EXPR;
26749 case CPP_RSHIFT_EQ:
26750 opcode = RSHIFT_EXPR;
26753 opcode = BIT_AND_EXPR;
26756 opcode = BIT_IOR_EXPR;
26759 opcode = BIT_XOR_EXPR;
26762 if (structured_block || code == OMP_ATOMIC)
26764 enum cp_parser_prec oprec;
26766 cp_lexer_consume_token (parser->lexer);
26767 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26768 /*cast_p=*/false, NULL);
26769 if (rhs1 == error_mark_node)
26771 token = cp_lexer_peek_token (parser->lexer);
26772 switch (token->type)
26774 case CPP_SEMICOLON:
26775 if (code == OMP_ATOMIC_CAPTURE_NEW)
26777 code = OMP_ATOMIC_CAPTURE_OLD;
26782 cp_lexer_consume_token (parser->lexer);
26785 cp_parser_error (parser,
26786 "invalid form of %<#pragma omp atomic%>");
26789 opcode = MULT_EXPR;
26792 opcode = TRUNC_DIV_EXPR;
26795 opcode = PLUS_EXPR;
26798 opcode = MINUS_EXPR;
26801 opcode = LSHIFT_EXPR;
26804 opcode = RSHIFT_EXPR;
26807 opcode = BIT_AND_EXPR;
26810 opcode = BIT_IOR_EXPR;
26813 opcode = BIT_XOR_EXPR;
26816 cp_parser_error (parser,
26817 "invalid operator for %<#pragma omp atomic%>");
26820 oprec = TOKEN_PRECEDENCE (token);
26821 gcc_assert (oprec != PREC_NOT_OPERATOR);
26822 if (commutative_tree_code (opcode))
26823 oprec = (enum cp_parser_prec) (oprec - 1);
26824 cp_lexer_consume_token (parser->lexer);
26825 rhs = cp_parser_binary_expression (parser, false, false,
26827 if (rhs == error_mark_node)
26833 cp_parser_error (parser,
26834 "invalid operator for %<#pragma omp atomic%>");
26837 cp_lexer_consume_token (parser->lexer);
26839 rhs = cp_parser_expression (parser, false, NULL);
26840 if (rhs == error_mark_node)
26845 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26847 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26849 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26850 /*cast_p=*/false, NULL);
26851 if (v == error_mark_node)
26853 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26855 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26856 /*cast_p=*/false, NULL);
26857 if (lhs1 == error_mark_node)
26860 if (structured_block)
26862 cp_parser_consume_semicolon_at_end_of_statement (parser);
26863 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26866 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26867 if (!structured_block)
26868 cp_parser_consume_semicolon_at_end_of_statement (parser);
26872 cp_parser_skip_to_end_of_block_or_statement (parser);
26873 if (structured_block)
26875 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26876 cp_lexer_consume_token (parser->lexer);
26877 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26879 cp_parser_skip_to_end_of_block_or_statement (parser);
26880 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26881 cp_lexer_consume_token (parser->lexer);
26888 # pragma omp barrier new-line */
26891 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26893 cp_parser_require_pragma_eol (parser, pragma_tok);
26894 finish_omp_barrier ();
26898 # pragma omp critical [(name)] new-line
26899 structured-block */
26902 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26904 tree stmt, name = NULL;
26906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26908 cp_lexer_consume_token (parser->lexer);
26910 name = cp_parser_identifier (parser);
26912 if (name == error_mark_node
26913 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26914 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26915 /*or_comma=*/false,
26916 /*consume_paren=*/true);
26917 if (name == error_mark_node)
26920 cp_parser_require_pragma_eol (parser, pragma_tok);
26922 stmt = cp_parser_omp_structured_block (parser);
26923 return c_finish_omp_critical (input_location, stmt, name);
26927 # pragma omp flush flush-vars[opt] new-line
26930 ( variable-list ) */
26933 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26935 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26936 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26937 cp_parser_require_pragma_eol (parser, pragma_tok);
26939 finish_omp_flush ();
26942 /* Helper function, to parse omp for increment expression. */
26945 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26947 tree cond = cp_parser_binary_expression (parser, false, true,
26948 PREC_NOT_OPERATOR, NULL);
26949 if (cond == error_mark_node
26950 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26952 cp_parser_skip_to_end_of_statement (parser);
26953 return error_mark_node;
26956 switch (TREE_CODE (cond))
26964 return error_mark_node;
26967 /* If decl is an iterator, preserve LHS and RHS of the relational
26968 expr until finish_omp_for. */
26970 && (type_dependent_expression_p (decl)
26971 || CLASS_TYPE_P (TREE_TYPE (decl))))
26974 return build_x_binary_op (input_location, TREE_CODE (cond),
26975 TREE_OPERAND (cond, 0), ERROR_MARK,
26976 TREE_OPERAND (cond, 1), ERROR_MARK,
26977 /*overload=*/NULL, tf_warning_or_error);
26980 /* Helper function, to parse omp for increment expression. */
26983 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26985 cp_token *token = cp_lexer_peek_token (parser->lexer);
26991 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26993 op = (token->type == CPP_PLUS_PLUS
26994 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26995 cp_lexer_consume_token (parser->lexer);
26996 lhs = cp_parser_simple_cast_expression (parser);
26998 return error_mark_node;
26999 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27002 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
27004 return error_mark_node;
27006 token = cp_lexer_peek_token (parser->lexer);
27007 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
27009 op = (token->type == CPP_PLUS_PLUS
27010 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
27011 cp_lexer_consume_token (parser->lexer);
27012 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27015 op = cp_parser_assignment_operator_opt (parser);
27016 if (op == ERROR_MARK)
27017 return error_mark_node;
27019 if (op != NOP_EXPR)
27021 rhs = cp_parser_assignment_expression (parser, false, NULL);
27022 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
27023 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27026 lhs = cp_parser_binary_expression (parser, false, false,
27027 PREC_ADDITIVE_EXPRESSION, NULL);
27028 token = cp_lexer_peek_token (parser->lexer);
27029 decl_first = lhs == decl;
27032 if (token->type != CPP_PLUS
27033 && token->type != CPP_MINUS)
27034 return error_mark_node;
27038 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
27039 cp_lexer_consume_token (parser->lexer);
27040 rhs = cp_parser_binary_expression (parser, false, false,
27041 PREC_ADDITIVE_EXPRESSION, NULL);
27042 token = cp_lexer_peek_token (parser->lexer);
27043 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
27045 if (lhs == NULL_TREE)
27047 if (op == PLUS_EXPR)
27050 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
27051 tf_warning_or_error);
27054 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
27055 ERROR_MARK, NULL, tf_warning_or_error);
27058 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
27062 if (rhs != decl || op == MINUS_EXPR)
27063 return error_mark_node;
27064 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
27067 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
27069 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27072 /* Parse the restricted form of the for statement allowed by OpenMP. */
27075 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
27077 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
27078 tree real_decl, initv, condv, incrv, declv;
27079 tree this_pre_body, cl;
27080 location_t loc_first;
27081 bool collapse_err = false;
27082 int i, collapse = 1, nbraces = 0;
27083 vec<tree, va_gc> *for_block = make_tree_vector ();
27085 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
27086 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
27087 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
27089 gcc_assert (collapse >= 1);
27091 declv = make_tree_vec (collapse);
27092 initv = make_tree_vec (collapse);
27093 condv = make_tree_vec (collapse);
27094 incrv = make_tree_vec (collapse);
27096 loc_first = cp_lexer_peek_token (parser->lexer)->location;
27098 for (i = 0; i < collapse; i++)
27100 int bracecount = 0;
27101 bool add_private_clause = false;
27104 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27106 cp_parser_error (parser, "for statement expected");
27109 loc = cp_lexer_consume_token (parser->lexer)->location;
27111 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27114 init = decl = real_decl = NULL;
27115 this_pre_body = push_stmt_list ();
27116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27118 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
27122 integer-type var = lb
27123 random-access-iterator-type var = lb
27124 pointer-type var = lb
27126 cp_decl_specifier_seq type_specifiers;
27128 /* First, try to parse as an initialized declaration. See
27129 cp_parser_condition, from whence the bulk of this is copied. */
27131 cp_parser_parse_tentatively (parser);
27132 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
27133 /*is_trailing_return=*/false,
27135 if (cp_parser_parse_definitely (parser))
27137 /* If parsing a type specifier seq succeeded, then this
27138 MUST be a initialized declaration. */
27139 tree asm_specification, attributes;
27140 cp_declarator *declarator;
27142 declarator = cp_parser_declarator (parser,
27143 CP_PARSER_DECLARATOR_NAMED,
27144 /*ctor_dtor_or_conv_p=*/NULL,
27145 /*parenthesized_p=*/NULL,
27146 /*member_p=*/false);
27147 attributes = cp_parser_attributes_opt (parser);
27148 asm_specification = cp_parser_asm_specification_opt (parser);
27150 if (declarator == cp_error_declarator)
27151 cp_parser_skip_to_end_of_statement (parser);
27155 tree pushed_scope, auto_node;
27157 decl = start_decl (declarator, &type_specifiers,
27158 SD_INITIALIZED, attributes,
27159 /*prefix_attributes=*/NULL_TREE,
27162 auto_node = type_uses_auto (TREE_TYPE (decl));
27163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27165 if (cp_lexer_next_token_is (parser->lexer,
27167 error ("parenthesized initialization is not allowed in "
27168 "OpenMP %<for%> loop");
27170 /* Trigger an error. */
27171 cp_parser_require (parser, CPP_EQ, RT_EQ);
27173 init = error_mark_node;
27174 cp_parser_skip_to_end_of_statement (parser);
27176 else if (CLASS_TYPE_P (TREE_TYPE (decl))
27177 || type_dependent_expression_p (decl)
27180 bool is_direct_init, is_non_constant_init;
27182 init = cp_parser_initializer (parser,
27184 &is_non_constant_init);
27189 = do_auto_deduction (TREE_TYPE (decl), init,
27192 if (!CLASS_TYPE_P (TREE_TYPE (decl))
27193 && !type_dependent_expression_p (decl))
27197 cp_finish_decl (decl, init, !is_non_constant_init,
27199 LOOKUP_ONLYCONVERTING);
27200 if (CLASS_TYPE_P (TREE_TYPE (decl)))
27202 vec_safe_push (for_block, this_pre_body);
27206 init = pop_stmt_list (this_pre_body);
27207 this_pre_body = NULL_TREE;
27212 cp_lexer_consume_token (parser->lexer);
27213 init = cp_parser_assignment_expression (parser, false, NULL);
27216 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27217 init = error_mark_node;
27219 cp_finish_decl (decl, NULL_TREE,
27220 /*init_const_expr_p=*/false,
27222 LOOKUP_ONLYCONVERTING);
27226 pop_scope (pushed_scope);
27232 /* If parsing a type specifier sequence failed, then
27233 this MUST be a simple expression. */
27234 cp_parser_parse_tentatively (parser);
27235 decl = cp_parser_primary_expression (parser, false, false,
27237 if (!cp_parser_error_occurred (parser)
27240 && CLASS_TYPE_P (TREE_TYPE (decl)))
27244 cp_parser_parse_definitely (parser);
27245 cp_parser_require (parser, CPP_EQ, RT_EQ);
27246 rhs = cp_parser_assignment_expression (parser, false, NULL);
27247 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27250 tf_warning_or_error));
27251 add_private_clause = true;
27256 cp_parser_abort_tentative_parse (parser);
27257 init = cp_parser_expression (parser, false, NULL);
27260 if (TREE_CODE (init) == MODIFY_EXPR
27261 || TREE_CODE (init) == MODOP_EXPR)
27262 real_decl = TREE_OPERAND (init, 0);
27267 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27270 this_pre_body = pop_stmt_list (this_pre_body);
27274 pre_body = push_stmt_list ();
27276 add_stmt (this_pre_body);
27277 pre_body = pop_stmt_list (pre_body);
27280 pre_body = this_pre_body;
27285 if (par_clauses != NULL && real_decl != NULL_TREE)
27288 for (c = par_clauses; *c ; )
27289 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27290 && OMP_CLAUSE_DECL (*c) == real_decl)
27292 error_at (loc, "iteration variable %qD"
27293 " should not be firstprivate", real_decl);
27294 *c = OMP_CLAUSE_CHAIN (*c);
27296 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27297 && OMP_CLAUSE_DECL (*c) == real_decl)
27299 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27300 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27301 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27302 OMP_CLAUSE_DECL (l) = real_decl;
27303 OMP_CLAUSE_CHAIN (l) = clauses;
27304 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27306 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27307 CP_OMP_CLAUSE_INFO (*c) = NULL;
27308 add_private_clause = false;
27312 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27313 && OMP_CLAUSE_DECL (*c) == real_decl)
27314 add_private_clause = false;
27315 c = &OMP_CLAUSE_CHAIN (*c);
27319 if (add_private_clause)
27322 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27324 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27325 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27326 && OMP_CLAUSE_DECL (c) == decl)
27328 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27329 && OMP_CLAUSE_DECL (c) == decl)
27330 error_at (loc, "iteration variable %qD "
27331 "should not be firstprivate",
27333 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27334 && OMP_CLAUSE_DECL (c) == decl)
27335 error_at (loc, "iteration variable %qD should not be reduction",
27340 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27341 OMP_CLAUSE_DECL (c) = decl;
27342 c = finish_omp_clauses (c);
27345 OMP_CLAUSE_CHAIN (c) = clauses;
27352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27353 cond = cp_parser_omp_for_cond (parser, decl);
27354 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27359 /* If decl is an iterator, preserve the operator on decl
27360 until finish_omp_for. */
27362 && ((processing_template_decl
27363 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27364 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27365 incr = cp_parser_omp_for_incr (parser, real_decl);
27367 incr = cp_parser_expression (parser, false, NULL);
27368 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27369 SET_EXPR_LOCATION (incr, input_location);
27372 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27373 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27374 /*or_comma=*/false,
27375 /*consume_paren=*/true);
27377 TREE_VEC_ELT (declv, i) = decl;
27378 TREE_VEC_ELT (initv, i) = init;
27379 TREE_VEC_ELT (condv, i) = cond;
27380 TREE_VEC_ELT (incrv, i) = incr;
27382 if (i == collapse - 1)
27385 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27386 in between the collapsed for loops to be still considered perfectly
27387 nested. Hopefully the final version clarifies this.
27388 For now handle (multiple) {'s and empty statements. */
27389 cp_parser_parse_tentatively (parser);
27392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27394 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27396 cp_lexer_consume_token (parser->lexer);
27399 else if (bracecount
27400 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27401 cp_lexer_consume_token (parser->lexer);
27404 loc = cp_lexer_peek_token (parser->lexer)->location;
27405 error_at (loc, "not enough collapsed for loops");
27406 collapse_err = true;
27407 cp_parser_abort_tentative_parse (parser);
27416 cp_parser_parse_definitely (parser);
27417 nbraces += bracecount;
27421 /* Note that we saved the original contents of this flag when we entered
27422 the structured block, and so we don't need to re-save it here. */
27423 parser->in_statement = IN_OMP_FOR;
27425 /* Note that the grammar doesn't call for a structured block here,
27426 though the loop as a whole is a structured block. */
27427 body = push_stmt_list ();
27428 cp_parser_statement (parser, NULL_TREE, false, NULL);
27429 body = pop_stmt_list (body);
27431 if (declv == NULL_TREE)
27434 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27435 pre_body, clauses);
27439 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27441 cp_lexer_consume_token (parser->lexer);
27444 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27445 cp_lexer_consume_token (parser->lexer);
27450 error_at (cp_lexer_peek_token (parser->lexer)->location,
27451 "collapsed loops not perfectly nested");
27453 collapse_err = true;
27454 cp_parser_statement_seq_opt (parser, NULL);
27455 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27460 while (!for_block->is_empty ())
27461 add_stmt (pop_stmt_list (for_block->pop ()));
27462 release_tree_vector (for_block);
27468 #pragma omp for for-clause[optseq] new-line
27471 #define OMP_FOR_CLAUSE_MASK \
27472 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27473 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27474 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27475 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27476 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27477 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27478 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27479 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27482 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27484 tree clauses, sb, ret;
27487 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27488 "#pragma omp for", pragma_tok);
27490 sb = begin_omp_structured_block ();
27491 save = cp_parser_begin_omp_structured_block (parser);
27493 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27495 cp_parser_end_omp_structured_block (parser, save);
27496 add_stmt (finish_omp_structured_block (sb));
27502 # pragma omp master new-line
27503 structured-block */
27506 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27508 cp_parser_require_pragma_eol (parser, pragma_tok);
27509 return c_finish_omp_master (input_location,
27510 cp_parser_omp_structured_block (parser));
27514 # pragma omp ordered new-line
27515 structured-block */
27518 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27521 cp_parser_require_pragma_eol (parser, pragma_tok);
27522 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27528 { section-sequence }
27531 section-directive[opt] structured-block
27532 section-sequence section-directive structured-block */
27535 cp_parser_omp_sections_scope (cp_parser *parser)
27537 tree stmt, substmt;
27538 bool error_suppress = false;
27541 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27544 stmt = push_stmt_list ();
27546 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27550 substmt = begin_omp_structured_block ();
27551 save = cp_parser_begin_omp_structured_block (parser);
27555 cp_parser_statement (parser, NULL_TREE, false, NULL);
27557 tok = cp_lexer_peek_token (parser->lexer);
27558 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27560 if (tok->type == CPP_CLOSE_BRACE)
27562 if (tok->type == CPP_EOF)
27566 cp_parser_end_omp_structured_block (parser, save);
27567 substmt = finish_omp_structured_block (substmt);
27568 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27569 add_stmt (substmt);
27574 tok = cp_lexer_peek_token (parser->lexer);
27575 if (tok->type == CPP_CLOSE_BRACE)
27577 if (tok->type == CPP_EOF)
27580 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27582 cp_lexer_consume_token (parser->lexer);
27583 cp_parser_require_pragma_eol (parser, tok);
27584 error_suppress = false;
27586 else if (!error_suppress)
27588 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27589 error_suppress = true;
27592 substmt = cp_parser_omp_structured_block (parser);
27593 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27594 add_stmt (substmt);
27596 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27598 substmt = pop_stmt_list (stmt);
27600 stmt = make_node (OMP_SECTIONS);
27601 TREE_TYPE (stmt) = void_type_node;
27602 OMP_SECTIONS_BODY (stmt) = substmt;
27609 # pragma omp sections sections-clause[optseq] newline
27612 #define OMP_SECTIONS_CLAUSE_MASK \
27613 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27614 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27615 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27616 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27617 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27620 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27624 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27625 "#pragma omp sections", pragma_tok);
27627 ret = cp_parser_omp_sections_scope (parser);
27629 OMP_SECTIONS_CLAUSES (ret) = clauses;
27635 # pragma parallel parallel-clause new-line
27636 # pragma parallel for parallel-for-clause new-line
27637 # pragma parallel sections parallel-sections-clause new-line */
27639 #define OMP_PARALLEL_CLAUSE_MASK \
27640 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27641 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27642 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27643 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27644 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27645 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27646 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27647 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27650 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27652 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27653 const char *p_name = "#pragma omp parallel";
27654 tree stmt, clauses, par_clause, ws_clause, block;
27655 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27657 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27661 cp_lexer_consume_token (parser->lexer);
27662 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27663 p_name = "#pragma omp parallel for";
27664 mask |= OMP_FOR_CLAUSE_MASK;
27665 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27667 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27669 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27670 const char *p = IDENTIFIER_POINTER (id);
27671 if (strcmp (p, "sections") == 0)
27673 cp_lexer_consume_token (parser->lexer);
27674 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27675 p_name = "#pragma omp parallel sections";
27676 mask |= OMP_SECTIONS_CLAUSE_MASK;
27677 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27681 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27682 block = begin_omp_parallel ();
27683 save = cp_parser_begin_omp_structured_block (parser);
27687 case PRAGMA_OMP_PARALLEL:
27688 cp_parser_statement (parser, NULL_TREE, false, NULL);
27689 par_clause = clauses;
27692 case PRAGMA_OMP_PARALLEL_FOR:
27693 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27694 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27697 case PRAGMA_OMP_PARALLEL_SECTIONS:
27698 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27699 stmt = cp_parser_omp_sections_scope (parser);
27701 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27705 gcc_unreachable ();
27708 cp_parser_end_omp_structured_block (parser, save);
27709 stmt = finish_omp_parallel (par_clause, block);
27710 if (p_kind != PRAGMA_OMP_PARALLEL)
27711 OMP_PARALLEL_COMBINED (stmt) = 1;
27716 # pragma omp single single-clause[optseq] new-line
27717 structured-block */
27719 #define OMP_SINGLE_CLAUSE_MASK \
27720 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27721 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27722 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27723 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27726 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27728 tree stmt = make_node (OMP_SINGLE);
27729 TREE_TYPE (stmt) = void_type_node;
27731 OMP_SINGLE_CLAUSES (stmt)
27732 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27733 "#pragma omp single", pragma_tok);
27734 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27736 return add_stmt (stmt);
27740 # pragma omp task task-clause[optseq] new-line
27741 structured-block */
27743 #define OMP_TASK_CLAUSE_MASK \
27744 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27745 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27746 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27747 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27748 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27749 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27750 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27751 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27754 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27756 tree clauses, block;
27759 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27760 "#pragma omp task", pragma_tok);
27761 block = begin_omp_task ();
27762 save = cp_parser_begin_omp_structured_block (parser);
27763 cp_parser_statement (parser, NULL_TREE, false, NULL);
27764 cp_parser_end_omp_structured_block (parser, save);
27765 return finish_omp_task (clauses, block);
27769 # pragma omp taskwait new-line */
27772 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27774 cp_parser_require_pragma_eol (parser, pragma_tok);
27775 finish_omp_taskwait ();
27779 # pragma omp taskyield new-line */
27782 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27784 cp_parser_require_pragma_eol (parser, pragma_tok);
27785 finish_omp_taskyield ();
27789 # pragma omp threadprivate (variable-list) */
27792 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27796 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27797 cp_parser_require_pragma_eol (parser, pragma_tok);
27799 finish_omp_threadprivate (vars);
27802 /* Main entry point to OpenMP statement pragmas. */
27805 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27809 switch (pragma_tok->pragma_kind)
27811 case PRAGMA_OMP_ATOMIC:
27812 cp_parser_omp_atomic (parser, pragma_tok);
27814 case PRAGMA_OMP_CRITICAL:
27815 stmt = cp_parser_omp_critical (parser, pragma_tok);
27817 case PRAGMA_OMP_FOR:
27818 stmt = cp_parser_omp_for (parser, pragma_tok);
27820 case PRAGMA_OMP_MASTER:
27821 stmt = cp_parser_omp_master (parser, pragma_tok);
27823 case PRAGMA_OMP_ORDERED:
27824 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27826 case PRAGMA_OMP_PARALLEL:
27827 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27829 case PRAGMA_OMP_SECTIONS:
27830 stmt = cp_parser_omp_sections (parser, pragma_tok);
27832 case PRAGMA_OMP_SINGLE:
27833 stmt = cp_parser_omp_single (parser, pragma_tok);
27835 case PRAGMA_OMP_TASK:
27836 stmt = cp_parser_omp_task (parser, pragma_tok);
27839 gcc_unreachable ();
27843 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27846 /* Transactional Memory parsing routines. */
27848 /* Parse a transaction attribute.
27854 ??? Simplify this when C++0x bracket attributes are
27855 implemented properly. */
27858 cp_parser_txn_attribute_opt (cp_parser *parser)
27861 tree attr_name, attr = NULL;
27863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27864 return cp_parser_attributes_opt (parser);
27866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27868 cp_lexer_consume_token (parser->lexer);
27869 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27872 token = cp_lexer_peek_token (parser->lexer);
27873 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27875 token = cp_lexer_consume_token (parser->lexer);
27877 attr_name = (token->type == CPP_KEYWORD
27878 /* For keywords, use the canonical spelling,
27879 not the parsed identifier. */
27880 ? ridpointers[(int) token->keyword]
27882 attr = build_tree_list (attr_name, NULL_TREE);
27885 cp_parser_error (parser, "expected identifier");
27887 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27889 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27893 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27895 transaction-statement:
27896 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27898 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27902 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27904 unsigned char old_in = parser->in_transaction;
27905 unsigned char this_in = 1, new_in;
27907 tree stmt, attrs, noex;
27909 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27910 || keyword == RID_TRANSACTION_RELAXED);
27911 token = cp_parser_require_keyword (parser, keyword,
27912 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27913 : RT_TRANSACTION_RELAXED));
27914 gcc_assert (token != NULL);
27916 if (keyword == RID_TRANSACTION_RELAXED)
27917 this_in |= TM_STMT_ATTR_RELAXED;
27920 attrs = cp_parser_txn_attribute_opt (parser);
27922 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27925 /* Parse a noexcept specification. */
27926 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27928 /* Keep track if we're in the lexical scope of an outer transaction. */
27929 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27931 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27933 parser->in_transaction = new_in;
27934 cp_parser_compound_statement (parser, NULL, false, false);
27935 parser->in_transaction = old_in;
27937 finish_transaction_stmt (stmt, NULL, this_in, noex);
27942 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27944 transaction-expression:
27945 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27946 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27950 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27952 unsigned char old_in = parser->in_transaction;
27953 unsigned char this_in = 1;
27958 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27959 || keyword == RID_TRANSACTION_RELAXED);
27962 error (keyword == RID_TRANSACTION_RELAXED
27963 ? G_("%<__transaction_relaxed%> without transactional memory "
27965 : G_("%<__transaction_atomic%> without transactional memory "
27966 "support enabled"));
27968 token = cp_parser_require_keyword (parser, keyword,
27969 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27970 : RT_TRANSACTION_RELAXED));
27971 gcc_assert (token != NULL);
27973 if (keyword == RID_TRANSACTION_RELAXED)
27974 this_in |= TM_STMT_ATTR_RELAXED;
27976 /* Set this early. This might mean that we allow transaction_cancel in
27977 an expression that we find out later actually has to be a constexpr.
27978 However, we expect that cxx_constant_value will be able to deal with
27979 this; also, if the noexcept has no constexpr, then what we parse next
27980 really is a transaction's body. */
27981 parser->in_transaction = this_in;
27983 /* Parse a noexcept specification. */
27984 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27987 if (!noex || !noex_expr
27988 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27990 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27992 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27993 finish_parenthesized_expr (expr);
27995 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27999 /* The only expression that is available got parsed for the noexcept
28000 already. noexcept is true then. */
28002 noex = boolean_true_node;
28005 expr = build_transaction_expr (token->location, expr, this_in, noex);
28006 parser->in_transaction = old_in;
28008 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
28009 return error_mark_node;
28011 return (flag_tm ? expr : error_mark_node);
28014 /* Parse a function-transaction-block.
28016 function-transaction-block:
28017 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
28019 __transaction_atomic txn-attribute[opt] function-try-block
28020 __transaction_relaxed ctor-initializer[opt] function-body
28021 __transaction_relaxed function-try-block
28025 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
28027 unsigned char old_in = parser->in_transaction;
28028 unsigned char new_in = 1;
28029 tree compound_stmt, stmt, attrs;
28030 bool ctor_initializer_p;
28033 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28034 || keyword == RID_TRANSACTION_RELAXED);
28035 token = cp_parser_require_keyword (parser, keyword,
28036 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28037 : RT_TRANSACTION_RELAXED));
28038 gcc_assert (token != NULL);
28040 if (keyword == RID_TRANSACTION_RELAXED)
28041 new_in |= TM_STMT_ATTR_RELAXED;
28044 attrs = cp_parser_txn_attribute_opt (parser);
28046 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28049 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
28051 parser->in_transaction = new_in;
28053 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28054 ctor_initializer_p = cp_parser_function_try_block (parser);
28056 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
28057 (parser, /*in_function_try_block=*/false);
28059 parser->in_transaction = old_in;
28061 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
28063 return ctor_initializer_p;
28066 /* Parse a __transaction_cancel statement.
28069 __transaction_cancel txn-attribute[opt] ;
28070 __transaction_cancel txn-attribute[opt] throw-expression ;
28072 ??? Cancel and throw is not yet implemented. */
28075 cp_parser_transaction_cancel (cp_parser *parser)
28078 bool is_outer = false;
28081 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
28082 RT_TRANSACTION_CANCEL);
28083 gcc_assert (token != NULL);
28085 attrs = cp_parser_txn_attribute_opt (parser);
28087 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
28089 /* ??? Parse cancel-and-throw here. */
28091 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28095 error_at (token->location, "%<__transaction_cancel%> without "
28096 "transactional memory support enabled");
28097 return error_mark_node;
28099 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
28101 error_at (token->location, "%<__transaction_cancel%> within a "
28102 "%<__transaction_relaxed%>");
28103 return error_mark_node;
28107 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
28108 && !is_tm_may_cancel_outer (current_function_decl))
28110 error_at (token->location, "outer %<__transaction_cancel%> not "
28111 "within outer %<__transaction_atomic%>");
28112 error_at (token->location,
28113 " or a %<transaction_may_cancel_outer%> function");
28114 return error_mark_node;
28117 else if (parser->in_transaction == 0)
28119 error_at (token->location, "%<__transaction_cancel%> not within "
28120 "%<__transaction_atomic%>");
28121 return error_mark_node;
28124 stmt = build_tm_abort_call (token->location, is_outer);
28133 static GTY (()) cp_parser *the_parser;
28136 /* Special handling for the first token or line in the file. The first
28137 thing in the file might be #pragma GCC pch_preprocess, which loads a
28138 PCH file, which is a GC collection point. So we need to handle this
28139 first pragma without benefit of an existing lexer structure.
28141 Always returns one token to the caller in *FIRST_TOKEN. This is
28142 either the true first token of the file, or the first token after
28143 the initial pragma. */
28146 cp_parser_initial_pragma (cp_token *first_token)
28150 cp_lexer_get_preprocessor_token (NULL, first_token);
28151 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
28154 cp_lexer_get_preprocessor_token (NULL, first_token);
28155 if (first_token->type == CPP_STRING)
28157 name = first_token->u.value;
28159 cp_lexer_get_preprocessor_token (NULL, first_token);
28160 if (first_token->type != CPP_PRAGMA_EOL)
28161 error_at (first_token->location,
28162 "junk at end of %<#pragma GCC pch_preprocess%>");
28165 error_at (first_token->location, "expected string literal");
28167 /* Skip to the end of the pragma. */
28168 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
28169 cp_lexer_get_preprocessor_token (NULL, first_token);
28171 /* Now actually load the PCH file. */
28173 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
28175 /* Read one more token to return to our caller. We have to do this
28176 after reading the PCH file in, since its pointers have to be
28178 cp_lexer_get_preprocessor_token (NULL, first_token);
28181 /* Normal parsing of a pragma token. Here we can (and must) use the
28185 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
28187 cp_token *pragma_tok;
28190 pragma_tok = cp_lexer_consume_token (parser->lexer);
28191 gcc_assert (pragma_tok->type == CPP_PRAGMA);
28192 parser->lexer->in_pragma = true;
28194 id = pragma_tok->pragma_kind;
28197 case PRAGMA_GCC_PCH_PREPROCESS:
28198 error_at (pragma_tok->location,
28199 "%<#pragma GCC pch_preprocess%> must be first");
28202 case PRAGMA_OMP_BARRIER:
28205 case pragma_compound:
28206 cp_parser_omp_barrier (parser, pragma_tok);
28209 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28210 "used in compound statements");
28217 case PRAGMA_OMP_FLUSH:
28220 case pragma_compound:
28221 cp_parser_omp_flush (parser, pragma_tok);
28224 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28225 "used in compound statements");
28232 case PRAGMA_OMP_TASKWAIT:
28235 case pragma_compound:
28236 cp_parser_omp_taskwait (parser, pragma_tok);
28239 error_at (pragma_tok->location,
28240 "%<#pragma omp taskwait%> may only be "
28241 "used in compound statements");
28248 case PRAGMA_OMP_TASKYIELD:
28251 case pragma_compound:
28252 cp_parser_omp_taskyield (parser, pragma_tok);
28255 error_at (pragma_tok->location,
28256 "%<#pragma omp taskyield%> may only be "
28257 "used in compound statements");
28264 case PRAGMA_OMP_THREADPRIVATE:
28265 cp_parser_omp_threadprivate (parser, pragma_tok);
28268 case PRAGMA_OMP_ATOMIC:
28269 case PRAGMA_OMP_CRITICAL:
28270 case PRAGMA_OMP_FOR:
28271 case PRAGMA_OMP_MASTER:
28272 case PRAGMA_OMP_ORDERED:
28273 case PRAGMA_OMP_PARALLEL:
28274 case PRAGMA_OMP_SECTIONS:
28275 case PRAGMA_OMP_SINGLE:
28276 case PRAGMA_OMP_TASK:
28277 if (context == pragma_external)
28279 cp_parser_omp_construct (parser, pragma_tok);
28282 case PRAGMA_OMP_SECTION:
28283 error_at (pragma_tok->location,
28284 "%<#pragma omp section%> may only be used in "
28285 "%<#pragma omp sections%> construct");
28289 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28290 c_invoke_pragma_handler (id);
28294 cp_parser_error (parser, "expected declaration specifiers");
28298 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28302 /* The interface the pragma parsers have to the lexer. */
28305 pragma_lex (tree *value)
28308 enum cpp_ttype ret;
28310 tok = cp_lexer_peek_token (the_parser->lexer);
28313 *value = tok->u.value;
28315 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28317 else if (ret == CPP_STRING)
28318 *value = cp_parser_string_literal (the_parser, false, false);
28321 cp_lexer_consume_token (the_parser->lexer);
28322 if (ret == CPP_KEYWORD)
28330 /* External interface. */
28332 /* Parse one entire translation unit. */
28335 c_parse_file (void)
28337 static bool already_called = false;
28339 if (already_called)
28341 sorry ("inter-module optimizations not implemented for C++");
28344 already_called = true;
28346 the_parser = cp_parser_new ();
28347 push_deferring_access_checks (flag_access_control
28348 ? dk_no_deferred : dk_no_check);
28349 cp_parser_translation_unit (the_parser);
28353 #include "gt-cp-parser.h"